simple_chunk_allocator

Struct 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.

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>

source

pub const fn chunk_size(&self) -> usize

Returns the used chunk size.

source

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.

source

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.

source

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.

source

pub const fn capacity(&self) -> usize

Capacity in bytes of the allocator.

source

pub const fn chunk_count(&self) -> usize

Returns number of chunks.

source

pub fn usage(&self) -> f32

Returns the current memory usage in percentage rounded to two decimal places.

source

pub fn allocate( &mut self, layout: Layout, ) -> Result<NonNull<[u8]>, ChunkAllocatorError>

Allocates memory according to the specific layout.

source

pub unsafe fn deallocate(&mut self, ptr: NonNull<u8>, layout: Layout)

Deallocates the given pointer.

§Safety

Unsafe if memory gets de-allocated that is still in use.

source

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.

Trait Implementations§

source§

impl<'a, const CHUNK_SIZE: usize> Debug for ChunkAllocator<'a, CHUNK_SIZE>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, const CHUNK_SIZE: usize = DEFAULT_CHUNK_SIZE> !Freeze for ChunkAllocator<'a, CHUNK_SIZE>

§

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.