Struct alloc_buddy_simple::Heap
[−]
[src]
pub struct Heap<'a> { /* fields omitted */ }
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.
Methods
impl<'a> Heap<'a>
[src]
unsafe fn new(
heap_base: *mut u8,
heap_size: usize,
free_lists: &mut [*mut FreeBlock]
) -> Heap
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.
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.
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
.
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.
unsafe fn buddy(&self, order: usize, block: *mut u8) -> Option<*mut u8>
Given a block
with the specified order
, find the "buddy" block,
that is, the other half of the block we originally split it from,
and also the block we could potentially merge it with.
unsafe fn deallocate(&mut self, ptr: *mut u8, old_size: usize, align: usize)
Deallocate a block allocated using allocate
. Note that the
old_size
and align
values must match the values passed to
allocate
, or our heap will be corrupted.