]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/solid/alloc.rs
Rollup merge of #105443 - compiler-errors:move-more, r=oli-obk
[rust.git] / library / std / src / sys / solid / alloc.rs
1 use crate::{
2     alloc::{GlobalAlloc, Layout, System},
3     sys::common::alloc::{realloc_fallback, MIN_ALIGN},
4 };
5
6 #[stable(feature = "alloc_system_type", since = "1.28.0")]
7 unsafe impl GlobalAlloc for System {
8     #[inline]
9     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
10         if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
11             unsafe { libc::malloc(layout.size()) as *mut u8 }
12         } else {
13             unsafe { libc::memalign(layout.align(), layout.size()) as *mut u8 }
14         }
15     }
16
17     #[inline]
18     unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
19         unsafe { libc::free(ptr as *mut libc::c_void) }
20     }
21
22     #[inline]
23     unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
24         unsafe {
25             if layout.align() <= MIN_ALIGN && layout.align() <= new_size {
26                 libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8
27             } else {
28                 realloc_fallback(self, ptr, layout, new_size)
29             }
30         }
31     }
32 }