pub struct ChunkedRangeAlloc { /* private fields */ }Expand description
A simple range allocator for chunked external memory, see Self::alloc
Implementations§
Source§impl ChunkedRangeAlloc
impl ChunkedRangeAlloc
Sourcepub fn new(chunk_size: NonZeroU32) -> Self
pub fn new(chunk_size: NonZeroU32) -> Self
Constructs a new, empty ChunkedRangeAlloc with specified chunk_size
Sourcepub fn from_allocations<'a>(
chunk_size: NonZeroU32,
allocations: impl IntoIterator<Item = &'a Allocation>,
) -> Option<Self>
pub fn from_allocations<'a>( chunk_size: NonZeroU32, allocations: impl IntoIterator<Item = &'a Allocation>, ) -> Option<Self>
Constructs ChunkedRangeAlloc with specified chunk_size and inserts allocations
Returns None if any allocation is invalid or overlaps
Sourcepub fn chunk_size(&self) -> NonZeroU32
pub fn chunk_size(&self) -> NonZeroU32
Chunk size
Sourcepub fn alloc(&mut self, size: NonZeroU32, align: NonZeroU32) -> Allocation
pub fn alloc(&mut self, size: NonZeroU32, align: NonZeroU32) -> Allocation
Allocates memory with specified size and align, alignment MUST be power of two
Use NonZeroU32::MIN as align if alignment is not required
Attempts to best-fit find suitable contiguous range, otherwise creates new chunk
Caller is responsible for allocating external memory when encountering new chunk index, chunk indices are contiguous
§Panics
- panics if
sizeexceedsSelf::chunk_size - panics if
alignis not power of two - panics if chunk count exceeds
u32::MAX
Sourcepub fn free(&mut self, allocation: Allocation) -> bool
pub fn free(&mut self, allocation: Allocation) -> bool
Frees specified allocation
Returns true if chunk with index specified in allocation becomes completely free
§Panics
Panics if allocation is invalid or double freed
Sourcepub fn chunk_count(&self) -> usize
pub fn chunk_count(&self) -> usize
Amount of tracked chunks, both active and free/unused
Sourcepub fn active_chunks(&self) -> usize
pub fn active_chunks(&self) -> usize
Amount of chunks containing live allocations
Sourcepub fn unused_chunks(&self) -> usize
pub fn unused_chunks(&self) -> usize
Shorthand for chunk_count() - active_chunks()
Sourcepub fn allocated_memory(&self) -> u64
pub fn allocated_memory(&self) -> u64
Sum of currently allocated Allocation::lens
Sourcepub fn used_memory(&self) -> u64
pub fn used_memory(&self) -> u64
Shorthand for active_chunks() * chunk_size()
Sourcepub fn unused_memory(&self) -> u64
pub fn unused_memory(&self) -> u64
Shorthand for used_memory() - allocated_memory()