Skip to content

Commit a6426cb

Browse files
committed
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]
1 parent 2bc4d3e commit a6426cb

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

src/liballoc/heap.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint)
3838
/// Extends or shrinks the allocation referenced by `ptr` to `size` bytes of
3939
/// memory in-place.
4040
///
41-
/// Returns true if successful, otherwise false if the allocation was not
42-
/// altered.
41+
/// If the operation succeeds, it returns `usable_size(size, align)` and if it
42+
/// fails (or is a no-op) it returns `usable_size(old_size, align)`.
4343
///
4444
/// Behavior is undefined if the requested size is 0 or the alignment is not a
4545
/// 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)
4949
/// create the allocation referenced by `ptr`. The `old_size` parameter may be
5050
/// any value in range_inclusive(requested_size, usable_size).
5151
#[inline]
52-
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> bool {
52+
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> uint {
5353
imp::reallocate_inplace(ptr, old_size, size, align)
5454
}
5555

@@ -178,16 +178,10 @@ mod imp {
178178
}
179179

180180
#[inline]
181-
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint,
182-
align: uint) -> bool {
181+
pub unsafe fn reallocate_inplace(ptr: *mut u8, _old_size: uint, size: uint,
182+
align: uint) -> uint {
183183
let flags = align_to_flags(align);
184-
let new_size = je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) as uint;
185-
// checking for failure to shrink is tricky
186-
if size < old_size {
187-
usable_size(size, align) == new_size as uint
188-
} else {
189-
new_size >= size
190-
}
184+
je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) as uint
191185
}
192186

193187
#[inline]
@@ -260,9 +254,9 @@ mod imp {
260254
}
261255

262256
#[inline]
263-
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, size: uint,
264-
_align: uint) -> bool {
265-
size == old_size
257+
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, _size: uint,
258+
_align: uint) -> uint {
259+
old_size
266260
}
267261

268262
#[inline]
@@ -328,9 +322,9 @@ mod imp {
328322
}
329323

330324
#[inline]
331-
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, size: uint,
332-
_align: uint) -> bool {
333-
size == old_size
325+
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, _size: uint,
326+
_align: uint) -> uint {
327+
old_size
334328
}
335329

336330
#[inline]
@@ -363,7 +357,7 @@ mod test {
363357
let ptr = heap::allocate(size, 8);
364358
let ret = heap::reallocate_inplace(ptr, size, size, 8);
365359
heap::deallocate(ptr, size, 8);
366-
assert!(ret);
360+
assert_eq!(ret, heap::usable_size(size, 8));
367361
}
368362
}
369363

0 commit comments

Comments
 (0)