std/sys/alloc/
unix.rs

1use super::{MIN_ALIGN, realloc_fallback};
2use crate::alloc::{GlobalAlloc, Layout, System};
3use crate::ptr;
4
5#[stable(feature = "alloc_system_type", since = "1.28.0")]
6unsafe impl GlobalAlloc for System {
7    #[inline]
8    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
9        // jemalloc provides alignment less than MIN_ALIGN for small allocations.
10        // So only rely on MIN_ALIGN if size >= align.
11        // Also see <https://github.com/rust-lang/rust/issues/45955> and
12        // <https://github.com/rust-lang/rust/issues/62251#issuecomment-507580914>.
13        if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
14            unsafe { libc::malloc(layout.size()) as *mut u8 }
15        } else {
16            // `posix_memalign` returns a non-aligned value if supplied a very
17            // large alignment on older versions of Apple's platforms (unknown
18            // exactly which version range, but the issue is definitely
19            // present in macOS 10.14 and iOS 13.3).
20            //
21            // <https://github.com/rust-lang/rust/issues/30170>
22            #[cfg(target_vendor = "apple")]
23            {
24                if layout.align() > (1 << 31) {
25                    return ptr::null_mut();
26                }
27            }
28            unsafe { aligned_malloc(&layout) }
29        }
30    }
31
32    #[inline]
33    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
34        // See the comment above in `alloc` for why this check looks the way it does.
35        if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
36            unsafe { libc::calloc(layout.size(), 1) as *mut u8 }
37        } else {
38            let ptr = unsafe { self.alloc(layout) };
39            if !ptr.is_null() {
40                unsafe { ptr::write_bytes(ptr, 0, layout.size()) };
41            }
42            ptr
43        }
44    }
45
46    #[inline]
47    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
48        unsafe { libc::free(ptr as *mut libc::c_void) }
49    }
50
51    #[inline]
52    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
53        if layout.align() <= MIN_ALIGN && layout.align() <= new_size {
54            unsafe { libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 }
55        } else {
56            unsafe { realloc_fallback(self, ptr, layout, new_size) }
57        }
58    }
59}
60
61cfg_if::cfg_if! {
62    // We use posix_memalign wherever possible, but some targets have very incomplete POSIX coverage
63    // so we need a fallback for those.
64    if #[cfg(any(
65        target_os = "horizon",
66        target_os = "vita",
67    ))] {
68        #[inline]
69        unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
70            unsafe { libc::memalign(layout.align(), layout.size()) as *mut u8 }
71        }
72    } else {
73        #[inline]
74        #[cfg_attr(target_os = "vxworks", allow(unused_unsafe))]
75        unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
76            let mut out = ptr::null_mut();
77            // We prefer posix_memalign over aligned_alloc since it is more widely available, and
78            // since with aligned_alloc, implementations are making almost arbitrary choices for
79            // which alignments are "supported", making it hard to use. For instance, some
80            // implementations require the size to be a multiple of the alignment (wasi emmalloc),
81            // while others require the alignment to be at least the pointer size (Illumos, macOS).
82            // posix_memalign only has one, clear requirement: that the alignment be a multiple of
83            // `sizeof(void*)`. Since these are all powers of 2, we can just use max.
84            let align = layout.align().max(crate::mem::size_of::<usize>());
85            let ret = unsafe { libc::posix_memalign(&mut out, align, layout.size()) };
86            if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
87        }
88    }
89}