simple_chunk_allocator

Struct GlobalChunkAllocator

source
pub struct GlobalChunkAllocator<'a, const CHUNK_SIZE: usize = DEFAULT_CHUNK_SIZE>(/* private fields */);
Expand description

Synchronized high-level wrapper around ChunkAllocator that implements the Rust traits GlobalAlloc which enables the usage as global allocator. The method GlobalChunkAllocator::allocator_api_glue returns an object of type AllocatorApiGlue which can be used with the allocator_api feature.

#![feature(const_mut_refs)]

use simple_chunk_allocator::{heap, heap_bitmap, GlobalChunkAllocator, PageAligned};

// The macros help to get a correctly sized arrays types.
// I page-align them for better caching and to improve the availability of
// page-aligned addresses.

/// Backing storage for heap (1Mib). (read+write) static memory in final executable.

/// heap!: first argument is chunk amount, second argument is size of each chunk.
///        If no arguments are provided it falls back to defaults.
///        Example: `heap!(chunks=16, chunksize=256)`.
static mut HEAP: PageAligned<[u8; 1048576]> = heap!();
/// Backing storage for heap bookkeeping bitmap. (read+write) static memory in final executable.
/// heap_bitmap!: first argument is amount of chunks.
///               If no argument is provided it falls back to a default.
///               Example: `heap_bitmap!(chunks=16)`.
static mut HEAP_BITMAP: PageAligned<[u8; 512]> = heap_bitmap!();

// please make sure that the backing memory is at least CHUNK_SIZE aligned; better page-aligned
#[global_allocator]
static ALLOCATOR: GlobalChunkAllocator =
    unsafe { GlobalChunkAllocator::new(HEAP.deref_mut_const(), HEAP_BITMAP.deref_mut_const()) };

Implementations§

source§

impl<'a, const CHUNK_SIZE: usize> GlobalChunkAllocator<'a, CHUNK_SIZE>

source

pub const fn new(heap: &'a mut [u8], bitmap: &'a mut [u8]) -> Self

High-level constructor around ChunkAllocator::<CHUNK_SIZE>::new_const.

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 fn usage(&self) -> f32

Wrapper around ChunkAllocator::usage.

source

pub const fn allocator_api_glue<'b>( &'b self, ) -> AllocatorApiGlue<'a, 'b, CHUNK_SIZE>

Returns an instance of AllocatorApiGlue.

Trait Implementations§

source§

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

source§

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

Formats the value using the given formatter. Read more
source§

impl<'a, const CHUNK_SIZE: usize> GlobalAlloc for GlobalChunkAllocator<'a, CHUNK_SIZE>

source§

unsafe fn alloc(&self, layout: Layout) -> *mut u8

Allocates memory as described by the given layout. Read more
source§

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout)

Deallocates the block of memory at the given ptr pointer with the given layout. Read more
source§

unsafe fn realloc( &self, ptr: *mut u8, layout: Layout, new_size: usize, ) -> *mut u8

Shrinks or grows a block of memory to the given new_size in bytes. The block is described by the given ptr pointer and layout. Read more
1.28.0 · source§

unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8

Behaves like alloc, but also ensures that the contents are set to zero before being returned. Read more

Auto Trait Implementations§

§

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

§

impl<'a, const CHUNK_SIZE: usize = DEFAULT_CHUNK_SIZE> !RefUnwindSafe for GlobalChunkAllocator<'a, CHUNK_SIZE>

§

impl<'a, const CHUNK_SIZE: usize> Send for GlobalChunkAllocator<'a, CHUNK_SIZE>

§

impl<'a, const CHUNK_SIZE: usize> Sync for GlobalChunkAllocator<'a, CHUNK_SIZE>

§

impl<'a, const CHUNK_SIZE: usize> Unpin for GlobalChunkAllocator<'a, CHUNK_SIZE>

§

impl<'a, const CHUNK_SIZE: usize = DEFAULT_CHUNK_SIZE> !UnwindSafe for GlobalChunkAllocator<'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.