@@ -89,21 +89,18 @@ cfg_if::cfg_if! {
8989 // /memory/aligned_memory.cc
9090 libc:: memalign( layout. align( ) , layout. size( ) ) as * mut u8
9191 }
92- } else if #[ cfg( target_os = "wasi" ) ] {
93- #[ inline]
94- unsafe fn aligned_malloc( layout: & Layout ) -> * mut u8 {
95- // C11 aligned_alloc requires that the size be a multiple of the alignment.
96- // Layout already checks that the size rounded up doesn't overflow isize::MAX.
97- let align = layout. align( ) ;
98- let size = layout. size( ) . next_multiple_of( align) ;
99- libc:: aligned_alloc( align, size) as * mut u8
100- }
10192 } else {
10293 #[ inline]
10394 unsafe fn aligned_malloc( layout: & Layout ) -> * mut u8 {
10495 let mut out = ptr:: null_mut( ) ;
105- // posix_memalign requires that the alignment be a multiple of `sizeof(void*)`.
106- // Since these are all powers of 2, we can just use max.
96+ // We prefer posix_memalign over aligned_malloc since with aligned_malloc,
97+ // implementations are making almost arbitrary choices for which alignments are
98+ // "supported", making it hard to use. For instance, some implementations require the
99+ // size to be a multiple of the alignment (wasi emmalloc), while others require the
100+ // alignment to be at least the pointer size (Illumos, macOS) -- which may or may not be
101+ // standards-compliant, but that does not help us.
102+ // posix_memalign only has one, clear requirement: that the alignment be a multiple of
103+ // `sizeof(void*)`. Since these are all powers of 2, we can just use max.
107104 let align = layout. align( ) . max( crate :: mem:: size_of:: <usize >( ) ) ;
108105 let ret = libc:: posix_memalign( & mut out, align, layout. size( ) ) ;
109106 if ret != 0 { ptr:: null_mut( ) } else { out as * mut u8 }
0 commit comments