Skip to main content

SizeClassed

Struct SizeClassed 

Source
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>

Source

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.

Source

pub fn backing(&self) -> &B

Borrow the inner backing.

Source

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.

Source

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>

Source§

fn allocate(&self, layout: NonZeroLayout) -> Result<NonNull<[u8]>, AllocError>

Allocate a block satisfying 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>

Usable size of an existing allocation, if the allocator can report it. Defaults to None — implementors that track usable size override. Read more
Source§

fn capacity_bytes(&self) -> Option<usize>

Total bytes this allocator can issue, if bounded. None for unbounded allocators like System. Used by Watermark to compute thresholds.
Source§

fn corruption_events(&self) -> u64

Detected freelist / metadata corruption events observed by this allocator since construction. Read more
Source§

fn allocate_zeroed( &self, layout: NonZeroLayout, ) -> Result<NonNull<[u8]>, AllocError>

Allocate a zero-initialized block.
Source§

unsafe fn grow( &self, ptr: NonNull<u8>, old: NonZeroLayout, new: NonZeroLayout, ) -> Result<NonNull<[u8]>, AllocError>

Grow an allocation in place if possible, otherwise allocate-copy-free. Read more
Source§

unsafe fn shrink( &self, ptr: NonNull<u8>, old: NonZeroLayout, new: NonZeroLayout, ) -> Result<NonNull<[u8]>, AllocError>

Shrink an allocation in place if possible, otherwise allocate-copy-free. Read more
Source§

fn reset(&mut self) -> Result<(), AllocError>

Reclaim everything previously allocated. Default impl returns AllocError — only arena-style allocators implement a meaningful reset. Read more
Source§

impl<B: Allocator + FixedRange, const CLASSES: usize> Deallocator for SizeClassed<B, CLASSES>

Source§

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: NonZeroLayout)

Release a previously allocated block. Read more
Source§

impl<B: Allocator + FixedRange, const CLASSES: usize> Drop for SizeClassed<B, CLASSES>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
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

Auto Trait Implementations§

§

impl<B, const CLASSES: usize> !Freeze for SizeClassed<B, CLASSES>

§

impl<B, const CLASSES: usize> !RefUnwindSafe for SizeClassed<B, CLASSES>

§

impl<B, const CLASSES: usize> !Sync for SizeClassed<B, CLASSES>

§

impl<B, const CLASSES: usize> Send for SizeClassed<B, CLASSES>
where B: Send,

§

impl<B, const CLASSES: usize> Unpin for SizeClassed<B, CLASSES>
where B: Unpin,

§

impl<B, const CLASSES: usize> UnsafeUnpin for SizeClassed<B, CLASSES>
where B: UnsafeUnpin,

§

impl<B, const CLASSES: usize> UnwindSafe for SizeClassed<B, CLASSES>
where B: UnwindSafe,

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> 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.