Rollup merge of #125271 - RalfJung:posix_memalign, r=workingjubilee · model-checking/verify-rust-std@38dcab9 (original) (raw)

Original file line number Diff line number Diff line change
@@ -59,10 +59,9 @@ unsafe impl GlobalAlloc for System {
59 59 }
60 60
61 61 cfg_if::cfg_if! {
62 -// We use posix_memalign wherever possible, but not all targets have that function.
62 +// We use posix_memalign wherever possible, but some targets have very incomplete POSIX coverage
63 +// so we need a fallback for those.
63 64 if #[cfg(any(
64 - target_os = "redox",
65 - target_os = "espidf",
66 65 target_os = "horizon",
67 66 target_os = "vita",
68 67 ))] {
@@ -74,12 +73,11 @@ cfg_if::cfg_if! {
74 73 #[inline]
75 74 unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
76 75 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.
76 +// We prefer posix_memalign over aligned_alloc since it is more widely available, and
77 +// since with aligned_alloc, implementations are making almost arbitrary choices for
78 +// which alignments are "supported", making it hard to use. For instance, some
79 +// implementations require the size to be a multiple of the alignment (wasi emmalloc),
80 +// while others require the alignment to be at least the pointer size (Illumos, macOS).
83 81 // posix_memalign only has one, clear requirement: that the alignment be a multiple of
84 82 // `sizeof(void*)`. Since these are all powers of 2, we can just use max.
85 83 let align = layout.align().max(crate::mem::size_of::<usize>());