about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorDaniel Micay <danielmicay@gmail.com>2014-05-10 00:35:56 -0400
committerDaniel Micay <danielmicay@gmail.com>2014-05-10 19:58:17 -0400
commit7e84b221de541e9e1f3c97646c22ae57bcbc51bc (patch)
tree215d942a83427af7f1ecdea84a2a320de07e4fc0 /src/libstd
parent0621ccac626ef4ca15e2cdf0aceed13ad0d3f848 (diff)
downloadrust-7e84b221de541e9e1f3c97646c22ae57bcbc51bc.tar.gz
rust-7e84b221de541e9e1f3c97646c22ae57bcbc51bc.zip
vec: factor out some deallocation code
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/vec.rs21
1 files changed, 12 insertions, 9 deletions
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index 351d4f3eaff..aa10be1d1be 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -411,6 +411,13 @@ unsafe fn alloc_or_realloc<T>(ptr: *mut T, size: uint, old_size: uint) -> *mut T
     }
 }
 
+#[inline]
+unsafe fn dealloc<T>(ptr: *mut T, len: uint) {
+    if size_of::<T>() != 0 {
+        deallocate(ptr as *mut u8, len * size_of::<T>(), min_align_of::<T>())
+    }
+}
+
 impl<T> Vec<T> {
     /// Returns the number of elements the vector can hold without
     /// reallocating.
@@ -510,7 +517,7 @@ impl<T> Vec<T> {
         if self.len == 0 {
             if self.cap != 0 {
                 unsafe {
-                    deallocate(self.ptr as *mut u8, self.cap * size_of::<T>(), min_align_of::<T>())
+                    dealloc(self.ptr, self.cap)
                 }
                 self.cap = 0;
             }
@@ -658,7 +665,7 @@ impl<T> Vec<T> {
     pub fn move_iter(self) -> MoveItems<T> {
         unsafe {
             let iter = transmute(self.as_slice().iter());
-            let ptr = self.ptr as *mut u8;
+            let ptr = self.ptr;
             let cap = self.cap;
             forget(self);
             MoveItems { allocation: ptr, cap: cap, iter: iter }
@@ -1412,9 +1419,7 @@ impl<T> Drop for Vec<T> {
                 for x in self.as_mut_slice().iter() {
                     ptr::read(x);
                 }
-                if size_of::<T>() != 0 {
-                    deallocate(self.ptr as *mut u8, self.cap * size_of::<T>(), min_align_of::<T>())
-                }
+                dealloc(self.ptr, self.cap)
             }
         }
     }
@@ -1434,7 +1439,7 @@ impl<T:fmt::Show> fmt::Show for Vec<T> {
 
 /// An iterator that moves out of a vector.
 pub struct MoveItems<T> {
-    allocation: *mut u8, // the block of memory allocated for the vector
+    allocation: *mut T, // the block of memory allocated for the vector
     cap: uint, // the capacity of the vector
     iter: Items<'static, T>
 }
@@ -1469,9 +1474,7 @@ impl<T> Drop for MoveItems<T> {
         if self.cap != 0 {
             for _x in *self {}
             unsafe {
-                if size_of::<T>() != 0 {
-                    deallocate(self.allocation, self.cap * size_of::<T>(), min_align_of::<T>())
-                }
+                dealloc(self.allocation, self.cap);
             }
         }
     }