From 1773d5f9534f13d816af682fb414be5b0e1f53ba Mon Sep 17 00:00:00 2001 From: gnzlbg Date: Sat, 25 Nov 2017 10:46:09 +0100 Subject: [PATCH] [bugfix] update align to flags in excess methods - Also rename `align_to_flags(align, size)` to `layout_to_flags(layout)` because it was taking its parameters in a weird order (typically its size first and align second) and size + align is actually `Layout`. --- src/lib.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d1e376d9a4..b30cd52ed5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,16 +47,16 @@ fn mallocx_align(a: usize) -> c_int { a.trailing_zeros() as c_int } -fn align_to_flags(align: usize, size: usize) -> c_int { +fn layout_to_flags(layout: &Layout) -> c_int { // If our alignment is less than the minimum alignment they we may not // have to pass special flags asking for a higher alignment. If the // alignment is greater than the size, however, then this hits a sort of odd // case where we still need to ask for a custom alignment. See #25 for more // info. - if align <= MIN_ALIGN && align <= size { + if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { 0 } else { - mallocx_align(align) + mallocx_align(layout.align()) } } @@ -136,7 +136,7 @@ unsafe impl Alloc for Jemalloc { unsafe impl<'a> Alloc for &'a Jemalloc { #[inline] unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { - let flags = align_to_flags(layout.align(), layout.size()); + let flags = layout_to_flags(&layout); let ptr = ffi::mallocx(layout.size(), flags); if ptr.is_null() { Err(AllocErr::Exhausted { request: layout }) @@ -152,7 +152,7 @@ unsafe impl<'a> Alloc for &'a Jemalloc { let ptr = if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { ffi::calloc(1, layout.size()) } else { - let flags = align_to_flags(layout.align(), layout.size()) | ffi::MALLOCX_ZERO; + let flags = layout_to_flags(&layout) | ffi::MALLOCX_ZERO; ffi::mallocx(layout.size(), flags) }; if ptr.is_null() { @@ -164,7 +164,7 @@ unsafe impl<'a> Alloc for &'a Jemalloc { #[inline] unsafe fn alloc_excess(&mut self, layout: Layout) -> Result { - let flags = align_to_flags(layout.align()); + let flags = layout_to_flags(&layout); let ptr = ffi::mallocx(layout.size(), flags); if ptr.is_null() { Err(AllocErr::Exhausted { request: layout }) @@ -176,7 +176,7 @@ unsafe impl<'a> Alloc for &'a Jemalloc { #[inline] unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) { - let flags = align_to_flags(layout.align(), layout.size()); + let flags = layout_to_flags(&layout); ffi::sdallocx(ptr as *mut c_void, layout.size(), flags) } @@ -188,7 +188,7 @@ unsafe impl<'a> Alloc for &'a Jemalloc { if old_layout.align() != new_layout.align() { return Err(AllocErr::Unsupported { details: "cannot change align" }) } - let flags = align_to_flags(new_layout.align(), new_layout.size()); + let flags = layout_to_flags(&new_layout); let ptr = ffi::rallocx(ptr as *mut c_void, new_layout.size(), flags); if ptr.is_null() { Err(AllocErr::Exhausted { request: new_layout }) @@ -205,7 +205,7 @@ unsafe impl<'a> Alloc for &'a Jemalloc { if old_layout.align() != new_layout.align() { return Err(AllocErr::Unsupported { details: "cannot change align" }) } - let flags = align_to_flags(new_layout.align()); + let flags = layout_to_flags(&new_layout); let ptr = ffi::rallocx(ptr as *mut c_void, new_layout.size(), flags); if ptr.is_null() { Err(AllocErr::Exhausted { request: new_layout }) @@ -221,7 +221,7 @@ unsafe impl<'a> Alloc for &'a Jemalloc { #[inline] fn usable_size(&self, layout: &Layout) -> (usize, usize) { - let flags = align_to_flags(layout.align(), layout.size()); + let flags = layout_to_flags(&layout); unsafe { let max = ffi::nallocx(layout.size(), flags); (layout.size(), max) @@ -244,7 +244,7 @@ unsafe impl<'a> Alloc for &'a Jemalloc { if old_layout.align() != new_layout.align() { return Err(CannotReallocInPlace) } - let flags = align_to_flags(new_layout.align(), new_layout.size()); + let flags = layout_to_flags(&new_layout); let size = ffi::xallocx(ptr as *mut c_void, new_layout.size(), 0, flags); if size >= new_layout.size() { Err(CannotReallocInPlace)