pub struct SubAllocator { /* private fields */ }Expand description
Bump-pointer sub-allocator that partitions a single large backing buffer into smaller regions.
Allocation is O(1); individual free is O(n) over live allocations;
defrag compacts the live allocations to the front of the buffer,
reclaiming all freed space.
Implementations§
Source§impl SubAllocator
impl SubAllocator
Sourcepub fn new(backing_size: u64, alignment: u64) -> Self
pub fn new(backing_size: u64, alignment: u64) -> Self
Create a new SubAllocator backed by a buffer of backing_size bytes,
with all offsets aligned to alignment bytes.
alignment is clamped to at least 1.
Sourcepub fn alloc(&mut self, size: u64) -> Option<SubAllocation>
pub fn alloc(&mut self, size: u64) -> Option<SubAllocation>
Allocate size bytes from the backing buffer.
Returns Some(SubAllocation) if there is room, None if the backing
buffer is exhausted.
Sourcepub fn free(&mut self, id: u64)
pub fn free(&mut self, id: u64)
Mark the allocation with the given id as freed.
Freed allocations are not reclaimed until defrag is called.
Sourcepub fn defrag(&mut self)
pub fn defrag(&mut self)
Compact all live allocations to the front of the backing buffer, reclaiming the space left by freed allocations.
After defragmentation the bump pointer is set to just after the last
live allocation, making that space available for future alloc calls.
Sourcepub fn utilization(&self) -> f64
pub fn utilization(&self) -> f64
Fraction of the backing buffer that is currently occupied by live allocations (0.0 = empty, 1.0 = full).
Sourcepub fn allocation_count(&self) -> usize
pub fn allocation_count(&self) -> usize
Number of live allocations.
Sourcepub fn current_offset(&self) -> u64
pub fn current_offset(&self) -> u64
Current value of the bump pointer (first unassigned byte offset).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SubAllocator
impl RefUnwindSafe for SubAllocator
impl Send for SubAllocator
impl Sync for SubAllocator
impl Unpin for SubAllocator
impl UnsafeUnpin for SubAllocator
impl UnwindSafe for SubAllocator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more