Struct simple_chunk_allocator::ChunkAllocator
source · [−]pub struct ChunkAllocator<'a, const CHUNK_SIZE: usize = DEFAULT_CHUNK_SIZE> { /* private fields */ }
Expand description
Low-level chunk allocator that operates on the provided backing memory. Allocates memory with a variant of the strategies next-fit and best-fit.
The default chunk size is DEFAULT_CHUNK_SIZE
. A large chunk size has the negative impact
that small allocations will consume at least one chunk. A small chunk size has the negative
impact that the allocation may take slightly longer.
Implementations
sourceimpl<'a, const CHUNK_SIZE: usize> ChunkAllocator<'a, CHUNK_SIZE>
impl<'a, const CHUNK_SIZE: usize> ChunkAllocator<'a, CHUNK_SIZE>
sourcepub const fn chunk_size(&self) -> usize
pub const fn chunk_size(&self) -> usize
Returns the used chunk size.
sourcepub const fn min_alignment(&self) -> usize
pub const fn min_alignment(&self) -> usize
Returns the minimum guaranteed alignment by the allocator per chunk. A chunk will never be at an address like 0x13, i.e., unaligned.
sourcepub const fn new(
heap: &'a mut [u8],
bitmap: &'a mut [u8]
) -> Result<Self, ChunkAllocatorError>
pub const fn new(
heap: &'a mut [u8],
bitmap: &'a mut [u8]
) -> Result<Self, ChunkAllocatorError>
Creates a new allocator object. Verifies that the provided memory has the correct properties. Zeroes the bitmap.
- heap length must be a multiple of
CHUNK_SIZE
- the heap must be not empty
- the bitmap must match the number of chunks
- the heap must be at least aligned to CHUNK_SIZE.
It is recommended that the heap and the bitmap both start at page-aligned addresses for better performance and to enable a faster search for correctly aligned addresses.
WARNING: During const initialization it is not possible to check the alignment of the provided buffer. Please make sure that the data is at least aligned to the chunk_size. The recommended alignment is page-alignment.
sourcepub const fn new_const(heap: &'a mut [u8], bitmap: &'a mut [u8]) -> Self
pub const fn new_const(heap: &'a mut [u8], bitmap: &'a mut [u8]) -> Self
Version of Self::new
that panics instead of returning a result. Useful for globally
static const contexts. The panic will happen during compile time and not during run time.
Self::new
can’t be used in such scenarios because unwrap()
on the
Result is not a const function (yet).
WARNING: During const initialization it is not possible to check the alignment of the provided buffer. Please make sure that the data is at least aligned to the chunk_size. The recommended alignment is page-alignment.
sourcepub const fn chunk_count(&self) -> usize
pub const fn chunk_count(&self) -> usize
Returns number of chunks.
sourcepub fn usage(&self) -> f32
pub fn usage(&self) -> f32
Returns the current memory usage in percentage rounded to two decimal places.
pub fn allocate(
&mut self,
layout: Layout
) -> Result<NonNull<[u8]>, ChunkAllocatorError>
Trait Implementations
Auto Trait Implementations
impl<'a, const CHUNK_SIZE: usize = DEFAULT_CHUNK_SIZE> !RefUnwindSafe for ChunkAllocator<'a, CHUNK_SIZE>
impl<'a, const CHUNK_SIZE: usize> Send for ChunkAllocator<'a, CHUNK_SIZE>
impl<'a, const CHUNK_SIZE: usize = DEFAULT_CHUNK_SIZE> !Sync for ChunkAllocator<'a, CHUNK_SIZE>
impl<'a, const CHUNK_SIZE: usize> Unpin for ChunkAllocator<'a, CHUNK_SIZE>
impl<'a, const CHUNK_SIZE: usize = DEFAULT_CHUNK_SIZE> !UnwindSafe for ChunkAllocator<'a, CHUNK_SIZE>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more