Skip to main content

SlabAllocator

Struct SlabAllocator 

Source
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>

Source

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.

Source

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.

Source

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.

Source

pub fn get(&self, handle: SlotHandle) -> Result<&T, SlabError>

Return an immutable reference to the value at handle.

Source

pub fn get_mut(&mut self, handle: SlotHandle) -> Result<&mut T, SlabError>

Return a mutable reference to the value at handle.

Source

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).

Source

pub fn live_count(&self) -> usize

Number of live (allocated) objects.

Source

pub fn total_slots(&self) -> usize

Total number of slots across all slabs (live + free).

Source

pub fn slab_count(&self) -> usize

Number of slabs currently allocated.

Source

pub fn free_slots(&self) -> usize

Number of free slots available without growing.

Source

pub fn utilisation(&self) -> f64

Utilisation ratio: live_count / total_slots, or 0.0 if no slots.

Auto Trait Implementations§

§

impl<T> Freeze for SlabAllocator<T>

§

impl<T> RefUnwindSafe for SlabAllocator<T>
where T: RefUnwindSafe,

§

impl<T> Send for SlabAllocator<T>
where T: Send,

§

impl<T> Sync for SlabAllocator<T>
where T: Sync,

§

impl<T> Unpin for SlabAllocator<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for SlabAllocator<T>

§

impl<T> UnwindSafe for SlabAllocator<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.