pub struct SizeClassed<B: Allocator + FixedRange, const CLASSES: usize> { /* private fields */ }Expand description
SizeClassed allocator.
CLASSES size classes are configured at construction. An allocate
request is routed to the smallest class whose stride is >= size
AND whose stride is a multiple of the requested alignment (any
class with stride < align is skipped). Oversized or
over-aligned requests fall through to backing.allocate(layout).
Deallocation routes by provenance: if ptr falls inside one of
the class regions, it’s a class allocation and gets pushed onto
that class’s freelist. Otherwise it came from the fallback path
and is forwarded to backing.deallocate.
!Sync — concurrent allocate would race on the per-class
free_head / next_uncarved cells.
Reset is unsupported: like Slab, SizeClassed does not implement
Allocator::reset (it returns the default Err). Bulk reclaim is via
Drop (rebuild the allocator); there is no in-place “empty all classes”
operation. Do not reset the backing arena out-of-band — the per-class
freelists would then point into logically-reclaimed (but not re-zeroed)
memory.
Implementations§
Source§impl<B: Allocator + FixedRange, const CLASSES: usize> SizeClassed<B, CLASSES>
impl<B: Allocator + FixedRange, const CLASSES: usize> SizeClassed<B, CLASSES>
Sourcepub fn with_class_sizes(
backing: B,
class_sizes: [usize; CLASSES],
slots_per_class: u32,
) -> Result<Self, AllocError>
pub fn with_class_sizes( backing: B, class_sizes: [usize; CLASSES], slots_per_class: u32, ) -> Result<Self, AllocError>
Construct with explicit class sizes and a uniform per-class slot count.
Each class allocates a region of class_size * slots_per_class
bytes from backing. Class sizes must be strictly increasing
and each must be a power of two (the inferred per-class
alignment requirement). Returns Err(AllocError) if any
constraint is violated, if the backing rejects a class region,
or if CLASSES == 0.
FreeLink is 4 bytes (u32), so the minimum class size is 4.
Sourcepub fn class_sizes(&self) -> &[usize; CLASSES]
pub fn class_sizes(&self) -> &[usize; CLASSES]
Class sizes in bytes.
Source§impl<B: Allocator + FixedRange> SizeClassed<B, 8>
Convenience constructor for the spec’s default 8-class set.
impl<B: Allocator + FixedRange> SizeClassed<B, 8>
Convenience constructor for the spec’s default 8-class set.
Sourcepub fn with_default_classes(
backing: B,
slots_per_class: u32,
) -> Result<Self, AllocError>
pub fn with_default_classes( backing: B, slots_per_class: u32, ) -> Result<Self, AllocError>
Construct with the spec-default 8 classes (8, 16, 32, 64, 128,
256, 512, 1024 bytes) and slots_per_class per class.
Trait Implementations§
Source§impl<B: Allocator + FixedRange, const CLASSES: usize> Allocator for SizeClassed<B, CLASSES>
impl<B: Allocator + FixedRange, const CLASSES: usize> Allocator for SizeClassed<B, CLASSES>
Source§fn allocate(&self, layout: NonZeroLayout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: NonZeroLayout) -> Result<NonNull<[u8]>, AllocError>
layout. The returned slice’s length is
at least layout.size() but may be larger.Source§unsafe fn usable_size(
&self,
ptr: NonNull<u8>,
layout: NonZeroLayout,
) -> Option<usize>
unsafe fn usable_size( &self, ptr: NonNull<u8>, layout: NonZeroLayout, ) -> Option<usize>
None — implementors that track usable size
override. Read moreSource§fn capacity_bytes(&self) -> Option<usize>
fn capacity_bytes(&self) -> Option<usize>
None for unbounded
allocators like System. Used by Watermark to compute thresholds.