]> git.lizzy.rs Git - rust.git/commitdiff
request at least ptr-size alignment from posix_memalign
authorRalf Jung <post@ralfj.de>
Tue, 2 Jul 2019 07:17:38 +0000 (09:17 +0200)
committerRalf Jung <post@ralfj.de>
Tue, 2 Jul 2019 07:20:41 +0000 (09:20 +0200)
src/libstd/sys/unix/alloc.rs

index 8e8f5017da75a1abc8fde700edb510caa45a13d0..c5b6a360dd3449bc936afc62b45ae0e3f929fdb1 100644 (file)
@@ -1,11 +1,15 @@
 use crate::ptr;
 use crate::sys_common::alloc::{MIN_ALIGN, realloc_fallback};
 use crate::alloc::{GlobalAlloc, Layout, System};
+use crate::mem;
 
 #[stable(feature = "alloc_system_type", since = "1.28.0")]
 unsafe impl GlobalAlloc for System {
     #[inline]
     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
+        // jemalloc provides alignment less than MIN_ALIGN for small allocations.
+        // So only rely on MIN_ALIGN if size >= align.
+        // Also see <https://github.com/rust-lang/rust/issues/45955>.
         if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
             libc::malloc(layout.size()) as *mut u8
         } else {
@@ -21,6 +25,9 @@ unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
 
     #[inline]
     unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
+        // jemalloc provides alignment less than MIN_ALIGN for small allocations.
+        // So only rely on MIN_ALIGN if size >= align.
+        // Also see <https://github.com/rust-lang/rust/issues/45955>.
         if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
             libc::calloc(layout.size(), 1) as *mut u8
         } else {
@@ -80,7 +87,10 @@ unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
 #[inline]
 unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
     let mut out = ptr::null_mut();
-    let ret = libc::posix_memalign(&mut out, layout.align(), layout.size());
+    // posix_memalign requires 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(mem::size_of::<usize>());
+    let ret = libc::posix_memalign(&mut out, align, layout.size());
     if ret != 0 {
         ptr::null_mut()
     } else {