Skip to content

Commit ab53b48

Browse files
committed
use posix_memalign on all Unix targets
1 parent 6579ed8 commit ab53b48

File tree

1 file changed

+13
-29
lines changed

1 file changed

+13
-29
lines changed

library/std/src/sys/pal/unix/alloc.rs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,17 @@ unsafe impl GlobalAlloc for System {
5858
}
5959
}
6060

61-
cfg_if::cfg_if! {
62-
// We use posix_memalign wherever possible, but not all targets have that function.
63-
if #[cfg(any(
64-
target_os = "redox",
65-
target_os = "espidf",
66-
target_os = "horizon",
67-
target_os = "vita",
68-
))] {
69-
#[inline]
70-
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
71-
libc::memalign(layout.align(), layout.size()) as *mut u8
72-
}
73-
} else {
74-
#[inline]
75-
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
76-
let mut out = ptr::null_mut();
77-
// We prefer posix_memalign over aligned_malloc since with aligned_malloc,
78-
// implementations are making almost arbitrary choices for which alignments are
79-
// "supported", making it hard to use. For instance, some implementations require the
80-
// size to be a multiple of the alignment (wasi emmalloc), while others require the
81-
// alignment to be at least the pointer size (Illumos, macOS) -- which may or may not be
82-
// standards-compliant, but that does not help us.
83-
// posix_memalign only has one, clear requirement: that the alignment be a multiple of
84-
// `sizeof(void*)`. Since these are all powers of 2, we can just use max.
85-
let align = layout.align().max(crate::mem::size_of::<usize>());
86-
let ret = libc::posix_memalign(&mut out, align, layout.size());
87-
if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
88-
}
89-
}
61+
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
62+
let mut out = ptr::null_mut();
63+
// We prefer posix_memalign over aligned_malloc since it is more widely available, and since
64+
// with aligned_malloc, implementations are making almost arbitrary choices for which alignments
65+
// are "supported", making it hard to use. For instance, some implementations require the size
66+
// to be a multiple of the alignment (wasi emmalloc), while others require the alignment to be
67+
// at least the pointer size (Illumos, macOS) -- which may or may not be standards-compliant,
68+
// but that does not help us.
69+
// posix_memalign only has one, clear requirement: that the alignment be a multiple of
70+
// `sizeof(void*)`. Since these are all powers of 2, we can just use max.
71+
let align = layout.align().max(crate::mem::size_of::<usize>());
72+
let ret = libc::posix_memalign(&mut out, align, layout.size());
73+
if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
9074
}

0 commit comments

Comments
 (0)