Skip to main content

std/sys/alloc/
unix.rs

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