1use super::{MIN_ALIGN, realloc_fallback};
2use crate::alloc::{GlobalAlloc, Layout, System};
3use crate::ptr;
45#[stable(feature = "alloc_system_type", since = "1.28.0")]
6unsafe impl GlobalAlloc for System {
7#[inline]
8unsafe 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>.
13if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
14unsafe { 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{
24if layout.align() > (1 << 31) {
25return ptr::null_mut();
26 }
27 }
28unsafe { aligned_malloc(&layout) }
29 }
30 }
3132#[inline]
33unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
34// See the comment above in `alloc` for why this check looks the way it does.
35if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
36unsafe { libc::calloc(layout.size(), 1) as *mut u8 }
37 } else {
38let ptr = unsafe { self.alloc(layout) };
39if !ptr.is_null() {
40unsafe { ptr::write_bytes(ptr, 0, layout.size()) };
41 }
42 ptr
43 }
44 }
4546#[inline]
47unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
48unsafe { libc::free(ptr as *mut libc::c_void) }
49 }
5051#[inline]
52unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
53if layout.align() <= MIN_ALIGN && layout.align() <= new_size {
54unsafe { libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 }
55 } else {
56unsafe { realloc_fallback(self, ptr, layout, new_size) }
57 }
58 }
59}
6061cfg_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.
64if #[cfg(any(
65 target_os = "horizon",
66 target_os = "vita",
67 ))] {
68#[inline]
69unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
70unsafe { libc::memalign(layout.align(), layout.size()) as *mut u8 }
71 }
72 } else {
73#[inline]
74 #[cfg_attr(target_os = "vxworks", allow(unused_unsafe))]
75unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
76let 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.
84let align = layout.align().max(crate::mem::size_of::<usize>());
85let ret = unsafe { libc::posix_memalign(&mut out, align, layout.size()) };
86if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
87 }
88 }
89}