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.
As this allocator may allocate more memory than required (because of the chunk size), realloc/grow operations are no-ops in certain cases.
Implementations§
source§impl<'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.
sourcepub fn allocate(
&mut self,
layout: Layout,
) -> Result<NonNull<[u8]>, ChunkAllocatorError>
pub fn allocate( &mut self, layout: Layout, ) -> Result<NonNull<[u8]>, ChunkAllocatorError>
Allocates memory according to the specific layout.
sourcepub unsafe fn deallocate(&mut self, ptr: NonNull<u8>, layout: Layout)
pub unsafe fn deallocate(&mut self, ptr: NonNull<u8>, layout: Layout)
sourcepub unsafe fn realloc(
&mut self,
ptr: NonNull<u8>,
old_layout: Layout,
new_size: usize,
) -> Result<NonNull<[u8]>, ChunkAllocatorError>
pub unsafe fn realloc( &mut self, ptr: NonNull<u8>, old_layout: Layout, new_size: usize, ) -> Result<NonNull<[u8]>, ChunkAllocatorError>
Reallocs the memory. This might be a cheap operation if the new size is still smaller or equal to the chunk size. Otherwise, this falls back to the default implementation of the Global allocator from Rust.
§Safety
Unsafe if memory gets de-allocated that is still in use.