RawBlock: seal most fields, replace top with a capacity field

This commit is contained in:
Emilie Burgun
2026-05-10 13:53:58 +02:00
parent 8dffd72db5
commit 3c5818a040
2 changed files with 77 additions and 32 deletions

View File

@@ -183,7 +183,7 @@ impl Stack {
let frame_size = AndFrame::size_of(num_cells); let frame_size = AndFrame::size_of(num_cells);
unsafe { unsafe {
let e = (*self.buf.ptr.get_mut()).addr() - self.buf.base.addr(); let e = self.buf.used_bytes();
let new_ptr = self.alloc(frame_size)?; let new_ptr = self.alloc(frame_size)?;
let mut offset = prelude_size::<AndFramePrelude>(); let mut offset = prelude_size::<AndFramePrelude>();
@@ -209,14 +209,14 @@ impl Stack {
} }
pub(crate) fn top(&self) -> usize { pub(crate) fn top(&self) -> usize {
unsafe { (*self.buf.ptr.get()).addr() - self.buf.base.addr() } self.buf.used_bytes()
} }
pub(crate) fn allocate_or_frame(&mut self, num_cells: usize) -> Result<usize, AllocError> { pub(crate) fn allocate_or_frame(&mut self, num_cells: usize) -> Result<usize, AllocError> {
let frame_size = OrFrame::size_of(num_cells); let frame_size = OrFrame::size_of(num_cells);
unsafe { unsafe {
let b = (*self.buf.ptr.get_mut()).addr() - self.buf.base.addr(); let b = self.buf.used_bytes();
let new_ptr = self.alloc(frame_size)?; let new_ptr = self.alloc(frame_size)?;
let mut offset = prelude_size::<OrFramePrelude>(); let mut offset = prelude_size::<OrFramePrelude>();
@@ -267,11 +267,7 @@ impl Stack {
#[inline(always)] #[inline(always)]
pub(crate) fn truncate(&mut self, b: usize) { pub(crate) fn truncate(&mut self, b: usize) {
let base = unsafe { self.buf.base.add(b) }; self.buf.shrink(b);
if base < (*self.buf.ptr.get_mut()) {
*self.buf.ptr.get_mut() = base.cast_mut();
}
} }
} }

View File

@@ -11,11 +11,13 @@ pub trait RawBlockTraits {
fn align() -> usize; fn align() -> usize;
} }
/// A block of memory with fast, lock-free appends.
#[derive(Debug)] #[derive(Debug)]
pub struct RawBlock<T: RawBlockTraits> { pub struct RawBlock<T: RawBlockTraits> {
pub base: *const u8, pub base: *const u8,
pub top: *const u8, capacity: usize,
pub ptr: UnsafeCell<*mut u8>,
ptr: UnsafeCell<*mut u8>,
_marker: PhantomData<T>, _marker: PhantomData<T>,
} }
@@ -24,7 +26,7 @@ impl<T: RawBlockTraits> RawBlock<T> {
pub fn empty_block() -> Self { pub fn empty_block() -> Self {
RawBlock { RawBlock {
base: ptr::null(), base: ptr::null(),
top: ptr::null(), capacity: 0,
ptr: UnsafeCell::new(ptr::null_mut()), ptr: UnsafeCell::new(ptr::null_mut()),
_marker: PhantomData, _marker: PhantomData,
} }
@@ -48,7 +50,7 @@ impl<T: RawBlockTraits> RawBlock<T> {
return Err(AllocError); return Err(AllocError);
} }
self.base = new_base; self.base = new_base;
self.top = self.base.add(cap); self.capacity = cap;
*self.ptr.get_mut() = self.base.cast_mut(); *self.ptr.get_mut() = self.base.cast_mut();
Ok(()) Ok(())
} }
@@ -57,7 +59,7 @@ impl<T: RawBlockTraits> RawBlock<T> {
if self.base.is_null() { if self.base.is_null() {
self.init_at_size(T::init_size()) self.init_at_size(T::init_size())
} else { } else {
let size = self.size(); let size = self.capacity();
let layout = alloc::Layout::from_size_align_unchecked(size, T::align()); let layout = alloc::Layout::from_size_align_unchecked(size, T::align());
let new_base = alloc::realloc(self.base.cast_mut(), layout, size * 2).cast_const(); let new_base = alloc::realloc(self.base.cast_mut(), layout, size * 2).cast_const();
@@ -65,46 +67,69 @@ impl<T: RawBlockTraits> RawBlock<T> {
Err(AllocError) Err(AllocError)
} else { } else {
self.base = new_base; self.base = new_base;
self.top = self.base.add(size * 2); self.capacity = size * 2;
*self.ptr.get_mut() = self.base.add(size).cast_mut(); *self.ptr.get_mut() = (self.base as usize + size) as *mut _;
Ok(()) Ok(())
} }
} }
} }
pub unsafe fn grow_new(&self) -> Result<Self, AllocError> { pub unsafe fn grow_new(&self) -> Result<Self, AllocError> {
self.debug_check_invariants();
if self.base.is_null() { if self.base.is_null() {
Self::new() Self::new()
} else { } else {
let mut new_block = Self::empty_block(); let mut new_block = Self::empty_block();
new_block.init_at_size(self.size() * 2)?; new_block.init_at_size(self.capacity() * 2)?;
let allocated = (*self.ptr.get()).addr() - self.base.addr(); let allocated = self.used_bytes();
self.base.copy_to(new_block.base.cast_mut(), allocated); self.base.copy_to(new_block.base.cast_mut(), allocated);
*new_block.ptr.get_mut() = new_block.base.add(allocated).cast_mut(); *new_block.ptr.get_mut() = new_block.base.add(allocated).cast_mut();
new_block.debug_check_invariants();
Ok(new_block) Ok(new_block)
} }
} }
#[inline(always)]
fn debug_check_invariants(&self) {
if cfg!(debug_assertions) {
unsafe {
assert!(
*self.ptr.get() as *const _ >= self.base,
"self.ptr = {:?} < {:?} = self.base",
*self.ptr.get(),
self.base
);
}
assert!(self.used_bytes() <= self.capacity());
}
}
#[inline] #[inline]
pub fn size(&self) -> usize { pub fn capacity(&self) -> usize {
self.top.addr() - self.base.addr() self.capacity
}
#[inline]
pub fn used_bytes(&self) -> usize {
// TODO: safety: UnsafeCell.get()
// TODO: safety: prove that ∀Γ: reachable, Γ |- (ptr, base): same alloc
unsafe { (*self.ptr.get()).offset_from(self.base) as usize }
} }
#[inline(always)] #[inline(always)]
unsafe fn free_space(&self) -> usize { unsafe fn free_bytes(&self) -> usize {
debug_assert!( self.capacity() - self.used_bytes()
*self.ptr.get() as *const _ >= self.base,
"self.ptr = {:?} < {:?} = self.base",
*self.ptr.get(),
self.base
);
self.top.addr() - (*self.ptr.get()).addr()
} }
pub unsafe fn alloc(&self, size: usize) -> *mut u8 { pub unsafe fn alloc(&self, size: usize) -> *mut u8 {
let aligned_size = size.next_multiple_of(size); self.debug_check_invariants();
if self.free_space() >= aligned_size {
let aligned_size = size.next_multiple_of(T::align());
if self.free_bytes() >= aligned_size {
// TODO: make this an atomic add
let ptr = *self.ptr.get(); let ptr = *self.ptr.get();
*self.ptr.get() = ptr.add(aligned_size) as *mut _; *self.ptr.get() = ptr.add(aligned_size) as *mut _;
ptr ptr
@@ -112,17 +137,41 @@ impl<T: RawBlockTraits> RawBlock<T> {
ptr::null_mut() ptr::null_mut()
} }
} }
/// Moves `ptr` back to `new_size`.
///
/// Note that this method does *not* deallocate what was placed in the [`RawBlock`].
pub fn shrink(&mut self, new_size: usize) {
self.debug_check_invariants();
assert!(
new_size <= self.used_bytes(),
"Shrink cannot grow: new_size = {:?} > allocated = {:?}",
new_size,
self.used_bytes()
);
// SAFETY:
// - Asserted: new_size <= self.capacity
// - Definition: self.base := alloc(self.capacity)
let new_ptr = unsafe { self.base.add(new_size) };
debug_assert!(new_ptr as usize <= (*self.ptr.get_mut()) as usize,);
*self.ptr.get_mut() = new_ptr as *mut u8;
self.debug_check_invariants();
}
} }
impl<T: RawBlockTraits> Drop for RawBlock<T> { impl<T: RawBlockTraits> Drop for RawBlock<T> {
fn drop(&mut self) { fn drop(&mut self) {
if !self.base.is_null() { if !self.base.is_null() {
unsafe { unsafe {
let layout = alloc::Layout::from_size_align_unchecked(self.size(), T::align()); let layout = alloc::Layout::from_size_align_unchecked(self.capacity(), T::align());
alloc::dealloc(self.base as *mut _, layout); alloc::dealloc(self.base as *mut _, layout);
} }
self.top = ptr::null();
self.base = ptr::null(); self.base = ptr::null();
*self.ptr.get_mut() = ptr::null_mut(); *self.ptr.get_mut() = ptr::null_mut();
} }