|
|
@ -9,7 +9,15 @@ use std::ptr::{Unique, NonNull, self};
|
|
|
|
use std::mem;
|
|
|
|
use std::mem;
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
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> {
|
|
|
|
struct RawVec<T> {
|
|
|
|
ptr: Unique<T>,
|
|
|
|
ptr: Unique<T>,
|
|
|
@ -34,14 +42,16 @@ impl<T> RawVec<T> {
|
|
|
|
assert!(elem_size != 0, "capacity overflow");
|
|
|
|
assert!(elem_size != 0, "capacity overflow");
|
|
|
|
|
|
|
|
|
|
|
|
let (new_cap, ptr) = if self.cap == 0 {
|
|
|
|
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)
|
|
|
|
(1, ptr)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
let new_cap = 2 * self.cap;
|
|
|
|
let new_cap = 2 * self.cap;
|
|
|
|
let c: NonNull<T> = self.ptr.into();
|
|
|
|
let c: NonNull<T> = self.ptr.into();
|
|
|
|
let ptr = Global.realloc(c.cast(),
|
|
|
|
let ptr = Global.grow(c.cast(),
|
|
|
|
Layout::array::<T>(self.cap).unwrap(),
|
|
|
|
Layout::array::<T>(self.cap).unwrap(),
|
|
|
|
Layout::array::<T>(new_cap).unwrap().size());
|
|
|
|
Layout::array::<T>(new_cap).unwrap().size(),
|
|
|
|
|
|
|
|
ReallocPlacement::MayMove,
|
|
|
|
|
|
|
|
AllocInit::Uninitialized);
|
|
|
|
(new_cap, ptr)
|
|
|
|
(new_cap, ptr)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -52,7 +62,7 @@ impl<T> RawVec<T> {
|
|
|
|
mem::align_of::<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.ptr = Unique::new_unchecked(ptr.as_ptr() as *mut _);
|
|
|
|
self.cap = new_cap;
|
|
|
|
self.cap = new_cap;
|
|
|
|