pub struct SlabAllocator<T> { /* private fields */ }Expand description
A fixed-slot slab allocator for type T.
§Example
use oximedia_cache::slab_allocator::SlabAllocator;
let mut alloc: SlabAllocator<Vec<u8>> = SlabAllocator::new(16);
let h = alloc.allocate(vec![0u8; 1024]).unwrap();
assert_eq!(alloc.get(h).unwrap().len(), 1024);
alloc.free(h).unwrap();Implementations§
Source§impl<T> SlabAllocator<T>
impl<T> SlabAllocator<T>
Sourcepub fn new(slab_capacity: usize) -> Self
pub fn new(slab_capacity: usize) -> Self
Create a new allocator that allocates slabs of slab_capacity slots.
A capacity of 0 is treated as 1.
Sourcepub fn allocate(&mut self, value: T) -> Result<SlotHandle, SlabError>
pub fn allocate(&mut self, value: T) -> Result<SlotHandle, SlabError>
Allocate a slot for value.
Returns a SlotHandle that can be used to retrieve or free the value.
If no free slots are available a new slab is allocated.
Sourcepub fn free(&mut self, handle: SlotHandle) -> Result<(), SlabError>
pub fn free(&mut self, handle: SlotHandle) -> Result<(), SlabError>
Free the slot identified by handle, making it available for reuse.
Returns Err if the handle is out of range or the slot is already
free.
Sourcepub fn get(&self, handle: SlotHandle) -> Result<&T, SlabError>
pub fn get(&self, handle: SlotHandle) -> Result<&T, SlabError>
Return an immutable reference to the value at handle.
Sourcepub fn get_mut(&mut self, handle: SlotHandle) -> Result<&mut T, SlabError>
pub fn get_mut(&mut self, handle: SlotHandle) -> Result<&mut T, SlabError>
Return a mutable reference to the value at handle.
Sourcepub fn compact(&mut self) -> usize
pub fn compact(&mut self) -> usize
Compact the allocator by removing fully-empty slabs.
Returns the number of slabs reclaimed. Note that this invalidates
all existing SlotHandles whose slab index ≥ the first reclaimed
slab. Callers must not use stale handles after compaction.
In practice you should call compact only during a cache quiesce
window (e.g. on process idle or periodic maintenance).
Sourcepub fn live_count(&self) -> usize
pub fn live_count(&self) -> usize
Number of live (allocated) objects.
Sourcepub fn total_slots(&self) -> usize
pub fn total_slots(&self) -> usize
Total number of slots across all slabs (live + free).
Sourcepub fn slab_count(&self) -> usize
pub fn slab_count(&self) -> usize
Number of slabs currently allocated.
Sourcepub fn free_slots(&self) -> usize
pub fn free_slots(&self) -> usize
Number of free slots available without growing.
Sourcepub fn utilisation(&self) -> f64
pub fn utilisation(&self) -> f64
Utilisation ratio: live_count / total_slots, or 0.0 if no slots.