Skip to content

Commit 7e84b22

Browse files
committed
vec: factor out some deallocation code
1 parent 0621cca commit 7e84b22

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/libstd/vec.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,13 @@ unsafe fn alloc_or_realloc<T>(ptr: *mut T, size: uint, old_size: uint) -> *mut T
411411
}
412412
}
413413

414+
#[inline]
415+
unsafe fn dealloc<T>(ptr: *mut T, len: uint) {
416+
if size_of::<T>() != 0 {
417+
deallocate(ptr as *mut u8, len * size_of::<T>(), min_align_of::<T>())
418+
}
419+
}
420+
414421
impl<T> Vec<T> {
415422
/// Returns the number of elements the vector can hold without
416423
/// reallocating.
@@ -510,7 +517,7 @@ impl<T> Vec<T> {
510517
if self.len == 0 {
511518
if self.cap != 0 {
512519
unsafe {
513-
deallocate(self.ptr as *mut u8, self.cap * size_of::<T>(), min_align_of::<T>())
520+
dealloc(self.ptr, self.cap)
514521
}
515522
self.cap = 0;
516523
}
@@ -658,7 +665,7 @@ impl<T> Vec<T> {
658665
pub fn move_iter(self) -> MoveItems<T> {
659666
unsafe {
660667
let iter = transmute(self.as_slice().iter());
661-
let ptr = self.ptr as *mut u8;
668+
let ptr = self.ptr;
662669
let cap = self.cap;
663670
forget(self);
664671
MoveItems { allocation: ptr, cap: cap, iter: iter }
@@ -1412,9 +1419,7 @@ impl<T> Drop for Vec<T> {
14121419
for x in self.as_mut_slice().iter() {
14131420
ptr::read(x);
14141421
}
1415-
if size_of::<T>() != 0 {
1416-
deallocate(self.ptr as *mut u8, self.cap * size_of::<T>(), min_align_of::<T>())
1417-
}
1422+
dealloc(self.ptr, self.cap)
14181423
}
14191424
}
14201425
}
@@ -1434,7 +1439,7 @@ impl<T:fmt::Show> fmt::Show for Vec<T> {
14341439

14351440
/// An iterator that moves out of a vector.
14361441
pub struct MoveItems<T> {
1437-
allocation: *mut u8, // the block of memory allocated for the vector
1442+
allocation: *mut T, // the block of memory allocated for the vector
14381443
cap: uint, // the capacity of the vector
14391444
iter: Items<'static, T>
14401445
}
@@ -1469,9 +1474,7 @@ impl<T> Drop for MoveItems<T> {
14691474
if self.cap != 0 {
14701475
for _x in *self {}
14711476
unsafe {
1472-
if size_of::<T>() != 0 {
1473-
deallocate(self.allocation, self.cap * size_of::<T>(), min_align_of::<T>())
1474-
}
1477+
dealloc(self.allocation, self.cap);
14751478
}
14761479
}
14771480
}

0 commit comments

Comments
 (0)