Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/vec-alloc.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fn grow(&mut self) {
// we need to make. We lose the ability to allocate e.g. 2/3rds of
// the address space with a single Vec of i16's on 32-bit though.
// Alas, poor Yorick -- I knew him, Horatio.
assert!(old_num_bytes <= (::std::isize::MAX as usize) / 2,
assert!(old_num_bytes <= (isize::MAX as usize) / 2,
"capacity overflow");

let new_num_bytes = old_num_bytes * 2;
Expand Down
22 changes: 16 additions & 6 deletions src/vec-final.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ use std::ptr::{Unique, NonNull, self};
use std::mem;
use std::ops::{Deref, DerefMut};
use std::marker::PhantomData;
use std::alloc::{AllocRef, GlobalAlloc, Layout, Global, handle_alloc_error};
use std::alloc::{
AllocInit,
AllocRef,
Global,
GlobalAlloc,
Layout,
ReallocPlacement,
handle_alloc_error
};

struct RawVec<T> {
ptr: Unique<T>,
Expand All @@ -34,14 +42,16 @@ impl<T> RawVec<T> {
assert!(elem_size != 0, "capacity overflow");

let (new_cap, ptr) = if self.cap == 0 {
let ptr = Global.alloc(Layout::array::<T>(1).unwrap());
let ptr = Global.alloc(Layout::array::<T>(1).unwrap(), AllocInit::Uninitialized);
(1, ptr)
} else {
let new_cap = 2 * self.cap;
let c: NonNull<T> = self.ptr.into();
let ptr = Global.realloc(c.cast(),
Layout::array::<T>(self.cap).unwrap(),
Layout::array::<T>(new_cap).unwrap().size());
let ptr = Global.grow(c.cast(),
Layout::array::<T>(self.cap).unwrap(),
Layout::array::<T>(new_cap).unwrap().size(),
ReallocPlacement::MayMove,
AllocInit::Uninitialized);
(new_cap, ptr)
};

Expand All @@ -52,7 +62,7 @@ impl<T> RawVec<T> {
mem::align_of::<T>(),
))
}
let (ptr, _) = ptr.unwrap();
let ptr = ptr.unwrap().ptr;

self.ptr = Unique::new_unchecked(ptr.as_ptr() as *mut _);
self.cap = new_cap;
Expand Down