[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`.
This commit is contained in:
gnzlbg 2017-11-25 10:46:09 +01:00
parent b87f8b806a
commit 1773d5f953
1 changed files with 11 additions and 11 deletions

View File

@ -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<Excess, AllocErr> {
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)