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,
impl<T, LOCK, const SLAB_SIZE: usize> SlabCache<T, LOCK, SLAB_SIZE>where
LOCK: LockTrait,
Sourcepub fn new() -> Result<SlabCache<T, LOCK, SLAB_SIZE>>
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()?;Sourcepub fn alloc(&mut self) -> Result<SlabBox<T, LOCK, SLAB_SIZE>>
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,
impl<T, LOCK, const SLAB_SIZE: usize> Clone for SlabCache<T, LOCK, SLAB_SIZE>where
LOCK: LockTrait,
Source§fn clone(&self) -> Self
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)
fn clone_from(&mut self, source: &Self)
source. Read more