parent
5637ed7566
commit
675475c4d3
7 changed files with 144 additions and 7 deletions
|
@ -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()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue