RawBlock: seal base and add Stack::index_dangling_or_frame

Direct accesses to `base` are replaced with dedicated methods with
explicit safety requirements.
This commit is contained in:
Emilie Burgun
2026-05-10 15:07:08 +02:00
parent 3c5818a040
commit 898b6b2b25
5 changed files with 105 additions and 28 deletions

View File

@@ -89,14 +89,20 @@ impl Index<usize> for Stack {
#[inline]
fn index(&self, index: usize) -> &Self::Output {
unsafe { &*self.buf.base.add(index).cast() }
unsafe {
let ptr = self.buf.get_unchecked(index);
&*ptr.cast::<HeapCellValue>()
}
}
}
impl IndexMut<usize> for Stack {
#[inline]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
unsafe { &mut *self.buf.base.add(index).cast_mut().cast() }
unsafe {
let ptr = self.buf.get_unchecked(index);
&mut *ptr.cast_mut().cast::<HeapCellValue>()
}
}
}
@@ -241,33 +247,57 @@ impl Stack {
}
}
fn get_raw(&self, index: usize) -> *const u8 {
debug_assert!(index < self.buf.used_bytes());
unsafe { self.buf.get_unchecked(index) }
}
#[inline(always)]
pub(crate) fn index_and_frame(&self, e: usize) -> &AndFrame {
unsafe { &*self.buf.base.add(e).cast() }
let ptr = self.get_raw(e);
unsafe { &*ptr.cast::<AndFrame>() }
}
#[inline(always)]
pub(crate) fn index_and_frame_mut(&mut self, e: usize) -> &mut AndFrame {
unsafe {
// This is doing alignment wrong
let ptr = self.buf.base.add(e);
&mut *(ptr as *mut AndFrame)
}
let ptr = self.get_raw(e);
unsafe { &mut *ptr.cast_mut().cast::<AndFrame>() }
}
#[inline(always)]
pub(crate) fn index_or_frame(&self, b: usize) -> &OrFrame {
unsafe { &*self.buf.base.add(b).cast() }
let ptr = self.get_raw(b);
unsafe { &*ptr.cast::<OrFrame>() }
}
#[inline(always)]
pub(crate) fn index_or_frame_mut(&mut self, b: usize) -> &mut OrFrame {
unsafe { &mut *self.buf.base.add(b).cast_mut().cast() }
let ptr = self.get_raw(b);
unsafe { &mut *ptr.cast_mut().cast::<OrFrame>() }
}
/// # Safety
///
/// The stack must contain a valid OrFrame at [`self.top()`](Self::top),
/// which can only be achieved by allocating it in the first place and later truncating the stack.
///
/// No allocation must have been done since the last call to [`truncate()`](Self::truncate).
#[inline(always)]
pub(crate) unsafe fn index_dangling_or_frame(&self) -> &OrFrame {
unsafe {
let ptr = self.buf.get_unchecked(self.top());
&*ptr.cast::<OrFrame>()
}
}
#[inline(always)]
pub(crate) fn truncate(&mut self, b: usize) {
self.buf.shrink(b);
self.buf.shift_back(b);
}
}