Skip to content

Commit

Permalink
alloc: fix reallocate_inplace implementation
Browse files Browse the repository at this point in the history
The returned size is the new real size of the allocation.
  • Loading branch information
thestinger committed Oct 2, 2014
1 parent d53874e commit 8d7274b
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/liballoc/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,15 @@ mod imp {

#[inline]
pub unsafe fn reallocate_inplace(ptr: *mut u8, size: uint, align: uint,
_old_size: uint) -> bool {
old_size: uint) -> bool {
let flags = align_to_flags(align);
je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) == size as size_t
let new_size = je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) as uint;
// checking for failure to shrink is tricky
if size < old_size {
usable_size(size, align) == new_size as uint
} else {
new_size >= size
}
}

#[inline]
Expand Down Expand Up @@ -250,9 +256,9 @@ mod imp {
}

#[inline]
pub unsafe fn reallocate_inplace(_ptr: *mut u8, _size: uint, _align: uint,
_old_size: uint) -> bool {
false
pub unsafe fn reallocate_inplace(_ptr: *mut u8, size: uint, _align: uint,
old_size: uint) -> bool {
size == old_size
}

#[inline]
Expand Down Expand Up @@ -312,9 +318,9 @@ mod imp {
}

#[inline]
pub unsafe fn reallocate_inplace(_ptr: *mut u8, _size: uint, _align: uint,
_old_size: uint) -> bool {
false
pub unsafe fn reallocate_inplace(_ptr: *mut u8, size: uint, _align: uint,
old_size: uint) -> bool {
size == old_size
}

#[inline]
Expand All @@ -335,10 +341,21 @@ mod imp {
}

#[cfg(test)]
mod bench {
mod test {
extern crate test;
use self::test::Bencher;

#[test]
fn basic_reallocate_inplace_noop() {
unsafe {
let size = 4000;
let ptr = heap::allocate(size, 8);
let ret = heap::reallocate_inplace(ptr, size, 8, size);
heap::deallocate(ptr, size, 8);
assert!(ret);
}
}

#[bench]
fn alloc_owned_small(b: &mut Bencher) {
b.iter(|| {
Expand Down

0 comments on commit 8d7274b

Please sign in to comment.