Skip to main content

SlabCache

Struct SlabCache 

Source
pub struct SlabCache<T, LOCK, const SLAB_SIZE: usize>
where LOCK: LockTrait,
{ /* private fields */ }
Expand description

A thread-safe, reference-counted handle to a Master-Slave slab hierarchy.

SlabCache allows you to allocate fixed-size objects of type T with $O(1)$ complexity. It manages the expansion of memory by adding Slave slabs when needed.

Implementations§

Source§

impl<T, LOCK, const SLAB_SIZE: usize> SlabCache<T, LOCK, SLAB_SIZE>
where LOCK: LockTrait,

Source

pub fn new() -> Result<SlabCache<T, LOCK, SLAB_SIZE>>

Creates a new SlabCache and initializes the Master Slab.

This will trigger an initial system allocation for the first Master segment.

§Errors
  • SlabError::OutOfMemory: If the initial system allocation fails.
§Example
let mut cache = SlabCache::<MyStruct, SpinLock, 4096>::new()?;
let my_obj = cache.alloc()?;
Source

pub fn alloc(&mut self) -> Result<SlabBox<T, LOCK, SLAB_SIZE>>

Allocates a new object of type T from the cache.

The returned SlabBox acts like a Box<T>, automatically returning the memory to the slab when it goes out of scope.

If the current Master and its Slaves are full, this method will automatically allocate a new Slave slab and link it to the hierarchy.

§Errors
  • SlabError::OutOfMemory: If no space is available and a new Slave cannot be created.
  • SlabError::FatalError: If the cache’s master pointer is null.

Trait Implementations§

Source§

impl<T, LOCK, const SLAB_SIZE: usize> Clone for SlabCache<T, LOCK, SLAB_SIZE>
where LOCK: LockTrait,

Source§

fn clone(&self) -> Self

Creates a new handle to the same Master slab.

This increments the internal reference count of the Master slab, ensuring it remains valid even if the original SlabCache is dropped.

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, LOCK, const SLAB_SIZE: usize> Drop for SlabCache<T, LOCK, SLAB_SIZE>
where LOCK: LockTrait,

Source§

fn drop(&mut self)

Drops the handle and potentially cleans up the allocator.

Decrements the Master’s reference count. If this was the last handle and the Master has no active Slaves or used slots, the memory is returned to the system.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T, LOCK, const SLAB_SIZE: usize> Send for SlabCache<T, LOCK, SLAB_SIZE>
where T: Send, LOCK: LockTrait + Sync,

Source§

impl<T, LOCK, const SLAB_SIZE: usize> Sync for SlabCache<T, LOCK, SLAB_SIZE>
where T: Send, LOCK: LockTrait + Sync,

Auto Trait Implementations§

§

impl<T, LOCK, const SLAB_SIZE: usize> Freeze for SlabCache<T, LOCK, SLAB_SIZE>

§

impl<T, LOCK, const SLAB_SIZE: usize> !RefUnwindSafe for SlabCache<T, LOCK, SLAB_SIZE>

§

impl<T, LOCK, const SLAB_SIZE: usize> Unpin for SlabCache<T, LOCK, SLAB_SIZE>

§

impl<T, LOCK, const SLAB_SIZE: usize> UnsafeUnpin for SlabCache<T, LOCK, SLAB_SIZE>

§

impl<T, LOCK, const SLAB_SIZE: usize> !UnwindSafe for SlabCache<T, LOCK, SLAB_SIZE>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.