Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use posix_memalign on almost all Unix targets #125271

Merged
merged 2 commits into from
May 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions library/std/src/sys/pal/unix/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ unsafe impl GlobalAlloc for System {
}

cfg_if::cfg_if! {
// We use posix_memalign wherever possible, but not all targets have that function.
// We use posix_memalign wherever possible, but some targets have very incomplete POSIX coverage
// so we need a fallback for those.
if #[cfg(any(
target_os = "redox",
target_os = "espidf",
target_os = "horizon",
target_os = "vita",
))] {
Expand All @@ -74,12 +73,11 @@ cfg_if::cfg_if! {
#[inline]
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
let mut out = ptr::null_mut();
// We prefer posix_memalign over aligned_malloc since with aligned_malloc,
// implementations are making almost arbitrary choices for which alignments are
// "supported", making it hard to use. For instance, some implementations require the
// size to be a multiple of the alignment (wasi emmalloc), while others require the
// alignment to be at least the pointer size (Illumos, macOS) -- which may or may not be
// standards-compliant, but that does not help us.
// We prefer posix_memalign over aligned_malloc since it is more widely available, and
// since with aligned_malloc, implementations are making almost arbitrary choices for
RalfJung marked this conversation as resolved.
Show resolved Hide resolved
// which alignments are "supported", making it hard to use. For instance, some
// implementations require the size to be a multiple of the alignment (wasi emmalloc),
// while others require the alignment to be at least the pointer size (Illumos, macOS).
// posix_memalign only has one, clear requirement: that the alignment be a multiple of
// `sizeof(void*)`. Since these are all powers of 2, we can just use max.
let align = layout.align().max(crate::mem::size_of::<usize>());
Expand Down
Loading