allow the frontier of the RawVec to be offset by a trait function

This commit is contained in:
Mark Thom
2020-02-12 23:52:30 -07:00
parent 5a6333129b
commit 1c4e2c0ed6
2 changed files with 15 additions and 7 deletions

View File

@@ -7,6 +7,7 @@ use std::ptr;
pub(crate) trait RawVecTraits {
fn init_size() -> usize;
fn align() -> usize;
fn base_offset(base: *const u8) -> *const u8;
}
pub(crate) struct RawVec<T: RawVecTraits> {
@@ -39,7 +40,7 @@ impl<T: RawVecTraits> RawVec<T> {
self.base = alloc::alloc(layout) as *const _;
self.size = T::init_size();
self.top = self.base.offset(T::align() as isize);
self.top = T::base_offset(self.base);
} else {
let layout = alloc::Layout::from_size_align_unchecked(T::init_size(), T::align());
let top_dist = self.top as usize - self.base as usize;
@@ -75,10 +76,10 @@ impl<T: RawVecTraits> RawVec<T> {
#[inline]
pub(crate)
unsafe fn new_block(&mut self, block_size: usize) -> *const u8 {
unsafe fn new_block(&mut self, size: usize) -> *const u8 {
loop {
if self.free_space() >= block_size {
return (self.top as usize + block_size) as *const _;
if self.free_space() >= size {
return (self.top as usize + size) as *const _;
} else {
self.grow();
}

View File

@@ -19,6 +19,13 @@ impl RawVecTraits for StackTraits {
fn align() -> usize {
mem::align_of::<Addr>()
}
#[inline]
fn base_offset(base: *const u8) -> *const u8 {
unsafe {
base.offset(Self::align() as isize)
}
}
}
const fn prelude_size<Prelude>() -> usize {