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