Struct block_allocator::FixedSizeBlockAllocator
source · pub struct FixedSizeBlockAllocator { /* private fields */ }
Implementations§
source§impl FixedSizeBlockAllocator
impl FixedSizeBlockAllocator
sourcepub unsafe fn init(&mut self, heap_start: usize, heap_size: usize)
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize)
Initialize the allocator with the given heap bounds.
This function is unsafe because the caller must guarantee that the given heap bounds are valid and that the heap is unused. This method must be called only once.
sourcepub unsafe fn allocate(&mut self, layout: Layout) -> *mut u8
pub unsafe fn allocate(&mut self, layout: Layout) -> *mut u8
Allocates a chunk of the given size with the given alignment. Returns a pointer to the beginning of that chunk if it was successful. Else it returns a null pointer.
Allocator first tries to find the smallest block size that is greater or equal to the required size. If a block of that size is available then it is returned, otherwise it tries to allocate from the fallback allocator.
sourcepub unsafe fn deallocate(&mut self, ptr: *mut u8, layout: Layout)
pub unsafe fn deallocate(&mut self, ptr: *mut u8, layout: Layout)
Frees the given allocation. ptr
must be a pointer returned
by a call to the allocate
function with identical size and alignment. Undefined
behavior may occur for invalid arguments, thus this function is unsafe.
If the allocation returned is one of the fixed block sizes, then it is returned to the head of the block list. Otherwise, it is deallocated using the fallback allocator.