1
Fork 0

Specialize Vec::from_elem<u8> to use calloc or memset

Fixes #38723.
This commit is contained in:
Matt Brubeck 2017-03-09 17:53:01 -08:00
parent 5637ed7566
commit 675475c4d3
7 changed files with 144 additions and 7 deletions

View file

@ -81,7 +81,18 @@ impl<T> RawVec<T> {
/// # Aborts
///
/// Aborts on OOM
#[inline]
pub fn with_capacity(cap: usize) -> Self {
RawVec::allocate(cap, false)
}
/// Like `with_capacity` but guarantees the buffer is zeroed.
#[inline]
pub fn with_capacity_zeroed(cap: usize) -> Self {
RawVec::allocate(cap, true)
}
fn allocate(cap: usize, zeroed: bool) -> Self {
unsafe {
let elem_size = mem::size_of::<T>();
@ -93,7 +104,11 @@ impl<T> RawVec<T> {
heap::EMPTY as *mut u8
} else {
let align = mem::align_of::<T>();
let ptr = heap::allocate(alloc_size, align);
let ptr = if zeroed {
heap::allocate_zeroed(alloc_size, align)
} else {
heap::allocate(alloc_size, align)
};
if ptr.is_null() {
oom()
}