pub struct GlobalChunkAllocator<'a, const CHUNK_SIZE: usize = DEFAULT_CHUNK_SIZE>(_);
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)]
#![feature(const_ptr_is_null)]
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.
static mut HEAP: PageAligned<[u8; 1048576]> = heap!();
/// Backing storage for heap bookkeeping bitmap. (read+write) static memory in final executable.
static mut HEAP_BITMAP: PageAligned<[u8; 512]> = heap_bitmap!();
#[global_allocator]
static ALLOCATOR: GlobalChunkAllocator =
unsafe { GlobalChunkAllocator::new(HEAP.deref_mut_const(), HEAP_BITMAP.deref_mut_const()) };
Implementations
sourceimpl<'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.
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
sourceimpl<'a, const CHUNK_SIZE: usize> Debug for GlobalChunkAllocator<'a, CHUNK_SIZE>
impl<'a, const CHUNK_SIZE: usize> Debug for GlobalChunkAllocator<'a, CHUNK_SIZE>
sourceimpl<'a, const CHUNK_SIZE: usize> GlobalAlloc for GlobalChunkAllocator<'a, CHUNK_SIZE>
impl<'a, const CHUNK_SIZE: usize> GlobalAlloc for GlobalChunkAllocator<'a, CHUNK_SIZE>
sourceunsafe fn alloc(&self, layout: Layout) -> *mut u8
unsafe fn alloc(&self, layout: Layout) -> *mut u8
Allocate memory as described by the given layout
. Read more
sourceunsafe fn dealloc(&self, ptr: *mut u8, layout: Layout)
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout)
Deallocate the block of memory at the given ptr
pointer with the given layout
. Read more
Auto Trait Implementations
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
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