Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] update align to flags in excess methods #35

Merged
merged 1 commit into from
Nov 25, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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