Skip to content

Commit 8d7274b

Browse files
committed
alloc: fix reallocate_inplace implementation
The returned size is the new real size of the allocation.
1 parent d53874e commit 8d7274b

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/liballoc/heap.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,15 @@ mod imp {
182182

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

190196
#[inline]
@@ -250,9 +256,9 @@ mod imp {
250256
}
251257

252258
#[inline]
253-
pub unsafe fn reallocate_inplace(_ptr: *mut u8, _size: uint, _align: uint,
254-
_old_size: uint) -> bool {
255-
false
259+
pub unsafe fn reallocate_inplace(_ptr: *mut u8, size: uint, _align: uint,
260+
old_size: uint) -> bool {
261+
size == old_size
256262
}
257263

258264
#[inline]
@@ -312,9 +318,9 @@ mod imp {
312318
}
313319

314320
#[inline]
315-
pub unsafe fn reallocate_inplace(_ptr: *mut u8, _size: uint, _align: uint,
316-
_old_size: uint) -> bool {
317-
false
321+
pub unsafe fn reallocate_inplace(_ptr: *mut u8, size: uint, _align: uint,
322+
old_size: uint) -> bool {
323+
size == old_size
318324
}
319325

320326
#[inline]
@@ -335,10 +341,21 @@ mod imp {
335341
}
336342

337343
#[cfg(test)]
338-
mod bench {
344+
mod test {
339345
extern crate test;
340346
use self::test::Bencher;
341347

348+
#[test]
349+
fn basic_reallocate_inplace_noop() {
350+
unsafe {
351+
let size = 4000;
352+
let ptr = heap::allocate(size, 8);
353+
let ret = heap::reallocate_inplace(ptr, size, 8, size);
354+
heap::deallocate(ptr, size, 8);
355+
assert!(ret);
356+
}
357+
}
358+
342359
#[bench]
343360
fn alloc_owned_small(b: &mut Bencher) {
344361
b.iter(|| {

0 commit comments

Comments
 (0)