Skip to content

Commit b5e9194

Browse files
committed
Merge pull request #7682 from thestinger/vec
vec::with_capacity: do one alloc for non-managed + ptr module improvements
2 parents 41dcec2 + 6f5be90 commit b5e9194

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/libstd/ptr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use cast;
1414
use option::{Option, Some, None};
1515
use sys;
1616
use unstable::intrinsics;
17+
use util::swap;
1718

1819
#[cfg(not(test))] use cmp::{Eq, Ord};
1920
use uint;
@@ -177,9 +178,9 @@ pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
177178
let t: *mut T = &mut tmp;
178179

179180
// Perform the swap
180-
copy_memory(t, x, 1);
181-
copy_memory(x, y, 1);
182-
copy_memory(y, t, 1);
181+
copy_nonoverlapping_memory(t, x, 1);
182+
copy_memory(x, y, 1); // `x` and `y` may overlap
183+
copy_nonoverlapping_memory(y, t, 1);
183184

184185
// y and t now point to the same thing, but we need to completely forget `tmp`
185186
// because it's no longer relevant.
@@ -192,7 +193,7 @@ pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
192193
*/
193194
#[inline]
194195
pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
195-
swap_ptr(dest, &mut src);
196+
swap(cast::transmute(dest), &mut src); // cannot overlap
196197
src
197198
}
198199

@@ -202,8 +203,7 @@ pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
202203
#[inline(always)]
203204
pub unsafe fn read_ptr<T>(src: *mut T) -> T {
204205
let mut tmp: T = intrinsics::uninit();
205-
let t: *mut T = &mut tmp;
206-
copy_memory(t, src, 1);
206+
copy_nonoverlapping_memory(&mut tmp, src, 1);
207207
tmp
208208
}
209209

src/libstd/vec.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use option::{None, Option, Some};
2626
use ptr::to_unsafe_ptr;
2727
use ptr;
2828
use ptr::RawPtr;
29-
use rt::global_heap::realloc_raw;
29+
use rt::global_heap::{malloc_raw, realloc_raw};
3030
use sys;
3131
use sys::size_of;
3232
use uint;
@@ -95,12 +95,31 @@ pub fn to_owned<T:Copy>(t: &[T]) -> ~[T] {
9595
}
9696

9797
/// Creates a new vector with a capacity of `capacity`
98+
#[cfg(stage0)]
9899
pub fn with_capacity<T>(capacity: uint) -> ~[T] {
99100
let mut vec = ~[];
100101
vec.reserve(capacity);
101102
vec
102103
}
103104

105+
/// Creates a new vector with a capacity of `capacity`
106+
#[cfg(not(stage0))]
107+
pub fn with_capacity<T>(capacity: uint) -> ~[T] {
108+
unsafe {
109+
if contains_managed::<T>() {
110+
let mut vec = ~[];
111+
vec.reserve(capacity);
112+
vec
113+
} else {
114+
let alloc = capacity * sys::nonzero_size_of::<T>();
115+
let ptr = malloc_raw(alloc + size_of::<raw::VecRepr>()) as *mut raw::VecRepr;
116+
(*ptr).unboxed.alloc = alloc;
117+
(*ptr).unboxed.fill = 0;
118+
cast::transmute(ptr)
119+
}
120+
}
121+
}
122+
104123
/**
105124
* Builds a vector by calling a provided function with an argument
106125
* function that pushes an element to the back of a vector.

0 commit comments

Comments
 (0)