Skip to content

Commit

Permalink
Merge pull request #35 from gnzlbg/fix_excess
Browse files Browse the repository at this point in the history
[bugfix] update align to flags in excess methods
  • Loading branch information
alexcrichton authored Nov 25, 2017
2 parents b87f8b8 + 1773d5f commit d422418
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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 })
Expand All @@ -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() {
Expand All @@ -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 })
Expand All @@ -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)
}

Expand All @@ -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 })
Expand All @@ -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 })
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit d422418

Please sign in to comment.