Struct slab_allocator::Heap
source · pub struct Heap { /* private fields */ }
Expand description
A fixed size heap backed by multiple slabs with blocks of different sizes. Allocations over 4096 bytes are served by linked list allocator.
Implementations§
source§impl Heap
impl Heap
sourcepub unsafe fn new(heap_start_addr: usize, heap_size: usize) -> Heap
pub unsafe fn new(heap_start_addr: usize, heap_size: usize) -> Heap
Creates a new heap with the given heap_start_addr
and heap_size
. The start address must be valid
and the memory in the [heap_start_addr, heap_start_addr + heap_size)
range must not be used for
anything else.
Safety
This function is unsafe because it can cause undefined behavior if the given address is invalid.
sourcepub unsafe fn add_memory(&mut self, heap_start_addr: usize, heap_size: usize)
pub unsafe fn add_memory(&mut self, heap_start_addr: usize, heap_size: usize)
Adds memory to the heap. The start address must be valid
and the memory in the [mem_start_addr, mem_start_addr + heap_size)
range must not be used for
anything else.
Safety
This function is unsafe because it can cause undefined behavior if the given address is invalid.
sourcepub fn allocate(&mut self, layout: Layout) -> Result<usize, AllocError>
pub fn allocate(&mut self, layout: Layout) -> Result<usize, AllocError>
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 Err
.
This function finds the slab of lowest size which can still accommodate the given chunk.
The runtime is in O(1)
for chunks of size <= 4096, and O(n)
when chunk size is > 4096,
sourcepub unsafe fn deallocate(&mut self, ptr: usize, layout: Layout)
pub unsafe fn deallocate(&mut self, ptr: usize, 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.
This function finds the slab which contains address of ptr
and adds the blocks beginning
with ptr
address to the list of free blocks.
This operation is in O(1)
for blocks <= 4096 bytes and O(n)
for blocks > 4096 bytes.
Safety
This function is unsafe because it can cause undefined behavior if the given address is invalid.
sourcepub fn usable_size(&self, layout: Layout) -> (usize, usize)
pub fn usable_size(&self, layout: Layout) -> (usize, usize)
Returns bounds on the guaranteed usable size of a successful
allocation created with the specified layout
.
sourcepub fn total_bytes(&self) -> usize
pub fn total_bytes(&self) -> usize
Returns total memory size in bytes of the heap.
sourcepub fn used_bytes(&self) -> usize
pub fn used_bytes(&self) -> usize
Returns allocated memory size in bytes.
sourcepub fn available_bytes(&self) -> usize
pub fn available_bytes(&self) -> usize
Returns available memory size in bytes.