allow the frontier of the RawVec to be offset by a trait function
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user