Expand description
Slab allocator for cache entries.
Cache workloads often allocate and free many identically-sized objects (e.g. fixed-size media segment headers, metadata records, or small frame descriptors). Standard heap allocators handle this correctly but can suffer from fragmentation and per-object bookkeeping overhead when the allocation rate is high.
This module provides a single-type slab allocator that pre-allocates contiguous slabs and hands out slots from a free-list. When a slot is freed it is returned to the free-list rather than returned to the system allocator, making future allocations O(1) with minimal bookkeeping.
§Design
- Each slab is a
Vec<Slot<T>>withslab_capacityslots. - A slot is either
Occupied(T)orFree. - A global
free_list: VecDeque<SlabIndex>keeps track of available slots across all slabs. - When the free list is empty a new slab is allocated.
- Handles are
SlotHandle { slab: usize, index: usize }and are used to retrieve or free a specific slot.
§Limitations
- Not thread-safe: wrap in
Mutex/RwLockfor concurrent use. - Does not shrink the slab pool when utilisation drops; use
compactto reclaim empty slabs.
Structs§
- Slab
Allocator - A fixed-slot slab allocator for type
T. - Slot
Handle - An opaque handle to a slot in a
SlabAllocator.
Enums§
- Slab
Error - Errors returned by
SlabAllocator.