pub struct Heap<'a> { /* private fields */ }Expand description
The interface to a heap. This data structure is stored outside the heap somewhere, because every single byte of our heap is potentially available for allocation.
Implementations§
Source§impl<'a> Heap<'a>
impl<'a> Heap<'a>
Sourcepub unsafe fn new(
heap_base: *mut u8,
heap_size: usize,
free_lists: &mut [*mut FreeBlock],
) -> Heap<'_>
pub unsafe fn new( heap_base: *mut u8, heap_size: usize, free_lists: &mut [*mut FreeBlock], ) -> Heap<'_>
Create a new heap. heap_base must be aligned on a
MIN_HEAP_ALIGN boundary, heap_size must be a power of 2, and
heap_size / 2.pow(free_lists.len()-1) must be greater than or
equal to size_of::<FreeBlock>(). Passing in invalid parameters
may do horrible things.
Sourcepub fn allocation_size(&self, size: usize, align: usize) -> Option<usize>
pub fn allocation_size(&self, size: usize, align: usize) -> Option<usize>
Figure out what size block we’ll need to fulfill an allocation
request. This is deterministic, and it does not depend on what
we’ve already allocated. In particular, it’s important to be able
to calculate the same allocation_size when freeing memory as we
did when allocating it, or everything will break horribly.
Sourcepub fn allocation_order(&self, size: usize, align: usize) -> Option<usize>
pub fn allocation_order(&self, size: usize, align: usize) -> Option<usize>
The “order” of an allocation is how many times we need to double
min_block_size in order to get a large enough block, as well as
the index we use into free_lists.
Sourcepub unsafe fn allocate(&mut self, size: usize, align: usize) -> *mut u8
pub unsafe fn allocate(&mut self, size: usize, align: usize) -> *mut u8
Allocate a block of memory large enough to contain size bytes,
and aligned on align. This will return NULL if the align is
greater than MIN_HEAP_ALIGN, if align is not a power of 2, or
if we can’t find enough memory.
All allocated memory must be passed to deallocate with the same
size and align parameter, or else horrible things will happen.