about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorDaniel Micay <danielmicay@gmail.com>2014-10-02 03:29:39 -0400
committerDaniel Micay <danielmicay@gmail.com>2014-10-02 15:13:34 -0400
commit8d7274b31efe248322f9b0d6a3082a92317f1dfa (patch)
treebce4971b689a99799728e08b415602a97785da7a /src/liballoc
parentd53874eccf0657d5d9c0a9ed9f84380d27d1c423 (diff)
downloadrust-8d7274b31efe248322f9b0d6a3082a92317f1dfa.tar.gz
rust-8d7274b31efe248322f9b0d6a3082a92317f1dfa.zip
alloc: fix reallocate_inplace implementation
The returned size is the new real size of the allocation.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/heap.rs35
1 files changed, 26 insertions, 9 deletions
diff --git a/src/liballoc/heap.rs b/src/liballoc/heap.rs
index 0d2872bcba0..72384e25c3b 100644
--- a/src/liballoc/heap.rs
+++ b/src/liballoc/heap.rs
@@ -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]
@@ -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]
@@ -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]
@@ -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(|| {