]> git.proxmox.com Git - rustc.git/blame - library/std/src/sys/unix/alloc.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / library / std / src / sys / unix / alloc.rs
CommitLineData
532ac7d7 1use crate::alloc::{GlobalAlloc, Layout, System};
60c5eb7d 2use crate::ptr;
cdc7bbd5 3use crate::sys::common::alloc::{realloc_fallback, MIN_ALIGN};
a1dfa0c6
XL
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 {
416331ca
XL
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>.
a1dfa0c6
XL
13 if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
14 libc::malloc(layout.size()) as *mut u8
15 } else {
16 #[cfg(target_os = "macos")]
17 {
18 if layout.align() > (1 << 31) {
60c5eb7d 19 return ptr::null_mut();
a1dfa0c6
XL
20 }
21 }
22 aligned_malloc(&layout)
23 }
24 }
25
26 #[inline]
27 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
416331ca 28 // See the comment above in `alloc` for why this check looks the way it does.
a1dfa0c6
XL
29 if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
30 libc::calloc(layout.size(), 1) as *mut u8
31 } else {
416331ca 32 let ptr = self.alloc(layout);
a1dfa0c6
XL
33 if !ptr.is_null() {
34 ptr::write_bytes(ptr, 0, layout.size());
35 }
36 ptr
37 }
38 }
39
40 #[inline]
41 unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
42 libc::free(ptr as *mut libc::c_void)
43 }
44
45 #[inline]
46 unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
47 if layout.align() <= MIN_ALIGN && layout.align() <= new_size {
48 libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8
49 } else {
50 realloc_fallback(self, ptr, layout, new_size)
51 }
52 }
53}
54
1b1a35ee
XL
55cfg_if::cfg_if! {
56 if #[cfg(any(
57 target_os = "android",
58 target_os = "illumos",
59 target_os = "redox",
94222f64 60 target_os = "solaris",
923072b8
FG
61 target_os = "espidf",
62 target_os = "horizon"
1b1a35ee
XL
63 ))] {
64 #[inline]
65 unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
66 // On android we currently target API level 9 which unfortunately
67 // doesn't have the `posix_memalign` API used below. Instead we use
68 // `memalign`, but this unfortunately has the property on some systems
69 // where the memory returned cannot be deallocated by `free`!
70 //
71 // Upon closer inspection, however, this appears to work just fine with
72 // Android, so for this platform we should be fine to call `memalign`
73 // (which is present in API level 9). Some helpful references could
74 // possibly be chromium using memalign [1], attempts at documenting that
75 // memalign + free is ok [2] [3], or the current source of chromium
76 // which still uses memalign on android [4].
77 //
78 // [1]: https://codereview.chromium.org/10796020/
79 // [2]: https://code.google.com/p/android/issues/detail?id=35391
80 // [3]: https://bugs.chromium.org/p/chromium/issues/detail?id=138579
81 // [4]: https://chromium.googlesource.com/chromium/src/base/+/master/
82 // /memory/aligned_memory.cc
83 libc::memalign(layout.align(), layout.size()) as *mut u8
84 }
85 } else if #[cfg(target_os = "wasi")] {
86 #[inline]
87 unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
88 libc::aligned_alloc(layout.align(), layout.size()) as *mut u8
89 }
90 } else {
91 #[inline]
92 unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
93 let mut out = ptr::null_mut();
94 // posix_memalign requires that the alignment be a multiple of `sizeof(void*)`.
95 // Since these are all powers of 2, we can just use max.
96 let align = layout.align().max(crate::mem::size_of::<usize>());
97 let ret = libc::posix_memalign(&mut out, align, layout.size());
98 if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
99 }
100 }
a1dfa0c6 101}