From 7e84b221de541e9e1f3c97646c22ae57bcbc51bc Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sat, 10 May 2014 00:35:56 -0400 Subject: [PATCH] vec: factor out some deallocation code --- src/libstd/vec.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 351d4f3eaffb..aa10be1d1be4 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -411,6 +411,13 @@ unsafe fn alloc_or_realloc(ptr: *mut T, size: uint, old_size: uint) -> *mut T } } +#[inline] +unsafe fn dealloc(ptr: *mut T, len: uint) { + if size_of::() != 0 { + deallocate(ptr as *mut u8, len * size_of::(), min_align_of::()) + } +} + impl Vec { /// Returns the number of elements the vector can hold without /// reallocating. @@ -510,7 +517,7 @@ impl Vec { if self.len == 0 { if self.cap != 0 { unsafe { - deallocate(self.ptr as *mut u8, self.cap * size_of::(), min_align_of::()) + dealloc(self.ptr, self.cap) } self.cap = 0; } @@ -658,7 +665,7 @@ impl Vec { pub fn move_iter(self) -> MoveItems { 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 Drop for Vec { for x in self.as_mut_slice().iter() { ptr::read(x); } - if size_of::() != 0 { - deallocate(self.ptr as *mut u8, self.cap * size_of::(), min_align_of::()) - } + dealloc(self.ptr, self.cap) } } } @@ -1434,7 +1439,7 @@ impl fmt::Show for Vec { /// An iterator that moves out of a vector. pub struct MoveItems { - 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 Drop for MoveItems { if self.cap != 0 { for _x in *self {} unsafe { - if size_of::() != 0 { - deallocate(self.allocation, self.cap * size_of::(), min_align_of::()) - } + dealloc(self.allocation, self.cap); } } }