diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-07-06 02:37:59 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-06 02:37:59 +0200 |
| commit | 3c4a6c860609fbf135fdd92996a65f14c3eb00da (patch) | |
| tree | ff75246055e34acbf405f731b9ba4787fefa78cb /src/libstd/sys/unix | |
| parent | 0383be8577adfa0c1b8e17887eae2fcc54eab81a (diff) | |
| parent | 2e47fc3bcd46e548d7168e2ba631b3dfa8464d01 (diff) | |
| download | rust-3c4a6c860609fbf135fdd92996a65f14c3eb00da.tar.gz rust-3c4a6c860609fbf135fdd92996a65f14c3eb00da.zip | |
Rollup merge of #62296 - RalfJung:memalign, r=alexcrichton
request at least ptr-size alignment from posix_memalign Fixes https://github.com/rust-lang/rust/issues/62251
Diffstat (limited to 'src/libstd/sys/unix')
| -rw-r--r-- | src/libstd/sys/unix/alloc.rs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/src/libstd/sys/unix/alloc.rs b/src/libstd/sys/unix/alloc.rs index 8e8f5017da7..2c2dd3b77ea 100644 --- a/src/libstd/sys/unix/alloc.rs +++ b/src/libstd/sys/unix/alloc.rs @@ -6,6 +6,10 @@ use crate::alloc::{GlobalAlloc, Layout, System}; 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> and + // <https://github.com/rust-lang/rust/issues/62251#issuecomment-507580914>. if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { libc::malloc(layout.size()) as *mut u8 } else { @@ -21,6 +25,7 @@ unsafe impl GlobalAlloc for System { #[inline] unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { + // See the comment above in `alloc` for why this check looks the way it does. if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { libc::calloc(layout.size(), 1) as *mut u8 } else { @@ -80,7 +85,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(crate::mem::size_of::<usize>()); + let ret = libc::posix_memalign(&mut out, align, layout.size()); if ret != 0 { ptr::null_mut() } else { |
