pub struct StackAlloc<B: FixedRange> { /* private fields */ }Expand description
LIFO-discipline allocator over a FixedRange backing.
Maintains a frame stack so nested alloc/free with arbitrary depth works
correctly. Each allocate pushes the new frame’s offset; deallocate
validates the supplied pointer matches the top of the stack and pops.
Implementations§
Source§impl<B: FixedRange> StackAlloc<B>
impl<B: FixedRange> StackAlloc<B>
Sourcepub fn new(backing: B) -> Result<Self, AllocError>
pub fn new(backing: B) -> Result<Self, AllocError>
Construct a LIFO allocator over backing’s entire range.
The frame stack starts at capacity 0 and grows on demand via the
global allocator. For workloads with a known maximum nesting
depth, prefer Self::with_max_depth to pre-reserve the frame
stack and avoid heap reallocations on the alloc hot path.
Sourcepub fn with_max_depth(backing: B, max_depth: usize) -> Result<Self, AllocError>
pub fn with_max_depth(backing: B, max_depth: usize) -> Result<Self, AllocError>
Construct with a hint for the maximum frame depth. The frame
stack is pre-allocated with max_depth capacity, so the alloc
hot path does not trigger a Vec grow / reallocation for the
first max_depth nested allocations.
If max_depth is exceeded at runtime the Vec will grow
normally — the constructor’s value is a hint, not a hard cap.
Pass 0 for the same behavior as Self::new.
Trait Implementations§
Source§impl<B: FixedRange> Allocator for StackAlloc<B>
impl<B: FixedRange> Allocator for StackAlloc<B>
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§fn capacity_bytes(&self) -> Option<usize>
fn capacity_bytes(&self) -> Option<usize>
None for unbounded
allocators like System. Used by Watermark to compute thresholds.Source§fn allocate_zeroed(
&self,
layout: NonZeroLayout,
) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed( &self, layout: NonZeroLayout, ) -> Result<NonNull<[u8]>, AllocError>
Source§unsafe fn grow(
&self,
ptr: NonNull<u8>,
old: NonZeroLayout,
new: NonZeroLayout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow( &self, ptr: NonNull<u8>, old: NonZeroLayout, new: NonZeroLayout, ) -> Result<NonNull<[u8]>, AllocError>
Source§unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old: NonZeroLayout,
new: NonZeroLayout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn shrink( &self, ptr: NonNull<u8>, old: NonZeroLayout, new: NonZeroLayout, ) -> Result<NonNull<[u8]>, AllocError>
Source§fn reset(&mut self) -> Result<(), AllocError>
fn reset(&mut self) -> Result<(), AllocError>
AllocError — only arena-style allocators implement a meaningful
reset. Read moreSource§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 more