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