Skip to main content

Module slab_allocator

Module slab_allocator 

Source
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>> with slab_capacity slots.
  • A slot is either Occupied(T) or Free.
  • 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 / RwLock for concurrent use.
  • Does not shrink the slab pool when utilisation drops; use compact to reclaim empty slabs.

Structs§

SlabAllocator
A fixed-slot slab allocator for type T.
SlotHandle
An opaque handle to a slot in a SlabAllocator.

Enums§

SlabError
Errors returned by SlabAllocator.