From a6426cb43dd24d0949755da57da85f3739fd9230 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Fri, 24 Oct 2014 19:58:26 -0400 Subject: [PATCH] return the new usable size from reallocate_inplace The real size is also more useful than just a boolean, and the caller can easily determine if the operation failed from the real size. In most cases, the caller is only going to be growing the allocation so a branch can be avoided. [breaking-change] --- src/liballoc/heap.rs | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/liballoc/heap.rs b/src/liballoc/heap.rs index 20569d336a8..6827ea1479d 100644 --- a/src/liballoc/heap.rs +++ b/src/liballoc/heap.rs @@ -38,8 +38,8 @@ pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) /// Extends or shrinks the allocation referenced by `ptr` to `size` bytes of /// memory in-place. /// -/// Returns true if successful, otherwise false if the allocation was not -/// altered. +/// If the operation succeeds, it returns `usable_size(size, align)` and if it +/// fails (or is a no-op) it returns `usable_size(old_size, align)`. /// /// Behavior is undefined if the requested size is 0 or the alignment is not a /// power of 2. The alignment must be no larger than the largest supported page @@ -49,7 +49,7 @@ pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) /// create the allocation referenced by `ptr`. The `old_size` parameter may be /// any value in range_inclusive(requested_size, usable_size). #[inline] -pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> bool { +pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> uint { imp::reallocate_inplace(ptr, old_size, size, align) } @@ -178,16 +178,10 @@ mod imp { } #[inline] - pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint, - align: uint) -> bool { + pub unsafe fn reallocate_inplace(ptr: *mut u8, _old_size: uint, size: uint, + align: uint) -> uint { let flags = align_to_flags(align); - 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 - } + je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) as uint } #[inline] @@ -260,9 +254,9 @@ mod imp { } #[inline] - pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, size: uint, - _align: uint) -> bool { - size == old_size + pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, _size: uint, + _align: uint) -> uint { + old_size } #[inline] @@ -328,9 +322,9 @@ mod imp { } #[inline] - pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, size: uint, - _align: uint) -> bool { - size == old_size + pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, _size: uint, + _align: uint) -> uint { + old_size } #[inline] @@ -363,7 +357,7 @@ mod test { let ptr = heap::allocate(size, 8); let ret = heap::reallocate_inplace(ptr, size, size, 8); heap::deallocate(ptr, size, 8); - assert!(ret); + assert_eq!(ret, heap::usable_size(size, 8)); } }