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>
impl<'a, const CHUNK_SIZE: usize> GlobalChunkAllocator<'a, CHUNK_SIZE>
sourcepub const fn new(heap: &'a mut [u8], bitmap: &'a mut [u8]) -> Self
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.
sourcepub fn usage(&self) -> f32
pub fn usage(&self) -> f32
Wrapper around ChunkAllocator::usage
.
sourcepub const fn allocator_api_glue<'b>(
&'b self,
) -> AllocatorApiGlue<'a, 'b, CHUNK_SIZE>
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>
impl<'a, const CHUNK_SIZE: usize> Debug for GlobalChunkAllocator<'a, CHUNK_SIZE>
source§impl<'a, const CHUNK_SIZE: usize> GlobalAlloc for GlobalChunkAllocator<'a, CHUNK_SIZE>
impl<'a, const CHUNK_SIZE: usize> GlobalAlloc for GlobalChunkAllocator<'a, CHUNK_SIZE>
source§unsafe fn alloc(&self, layout: Layout) -> *mut u8
unsafe fn alloc(&self, layout: Layout) -> *mut u8
layout
. Read more