pub struct Slab<'a, T: Pod + FixedLayout> { /* private fields */ }Expand description
A fixed-size slab allocator over a byte slice.
Tracks slot occupancy with an inline bitmap. Double-free, reads of freed slots, and writes to freed slots are all rejected.
Implementations§
Source§impl<'a, T: Pod + FixedLayout> Slab<'a, T>
impl<'a, T: Pod + FixedLayout> Slab<'a, T>
Sourcepub fn from_bytes_mut(data: &'a mut [u8]) -> Result<Self, ProgramError>
pub fn from_bytes_mut(data: &'a mut [u8]) -> Result<Self, ProgramError>
Parse a slab from a mutable byte slice.
Sourcepub fn init(data: &mut [u8], capacity: usize) -> Result<(), ProgramError>
pub fn init(data: &mut [u8], capacity: usize) -> Result<(), ProgramError>
Initialize a slab with the given capacity.
Must be called on a zeroed buffer. Sets up the free list and clears the occupancy bitmap.
Sourcepub fn is_slot_allocated(&self, index: u32) -> bool
pub fn is_slot_allocated(&self, index: u32) -> bool
Whether a slot index is currently allocated.
Sourcepub fn alloc(&mut self, value: T) -> Result<u32, ProgramError>
pub fn alloc(&mut self, value: T) -> Result<u32, ProgramError>
Allocate a slot and write the value. Returns the slot index.
Sourcepub fn free(&mut self, index: u32) -> Result<(), ProgramError>
pub fn free(&mut self, index: u32) -> Result<(), ProgramError>
Free a slot and return it to the free list.
Fails if the slot is not currently allocated (prevents double-free).
Sourcepub fn get(&self, index: u32) -> Result<T, ProgramError>
pub fn get(&self, index: u32) -> Result<T, ProgramError>
Read a value from a slot (copy).
Fails if the slot is not allocated.
Sourcepub fn get_ref(&self, index: u32) -> Result<&T, ProgramError>
pub fn get_ref(&self, index: u32) -> Result<&T, ProgramError>
Get a reference to a value in a slot.
Fails if the slot is not allocated.
Sourcepub fn get_mut(&mut self, index: u32) -> Result<&mut T, ProgramError>
pub fn get_mut(&mut self, index: u32) -> Result<&mut T, ProgramError>
Get a mutable reference to a value in a slot.
Fails if the slot is not allocated.
Sourcepub fn set(&mut self, index: u32, value: T) -> Result<(), ProgramError>
pub fn set(&mut self, index: u32, value: T) -> Result<(), ProgramError>
Write a value into an allocated slot.
Fails if the slot is not allocated.
Sourcepub const fn required_bytes(capacity: usize) -> usize
pub const fn required_bytes(capacity: usize) -> usize
Bytes required for a slab of given capacity.