Struct slab_allocator_rs::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 a buddy system 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 grow(
&mut self,
mem_start_addr: usize,
mem_size: usize,
slab: HeapAllocator
)
pub unsafe fn grow(
&mut self,
mem_start_addr: usize,
mem_size: usize,
slab: HeapAllocator
)
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.
In case of buddy system allocator the memory can only be extended.
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<NonNull<u8>, ()>
pub fn allocate(&mut self, layout: Layout) -> Result<NonNull<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 ()
.
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 probably fast
when chunk size is > 4096,
sourcepub unsafe fn deallocate(&mut self, ptr: NonNull<u8>, layout: Layout)
pub unsafe fn deallocate(&mut self, ptr: NonNull<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.
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 probably fast
for blocks > 4096 bytes.
Safety
Undefined behavior may occur for invalid arguments, thus this function is unsafe.
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 layout_to_allocator(layout: &Layout) -> HeapAllocator
pub fn layout_to_allocator(layout: &Layout) -> HeapAllocator
Finds allocator to use based on layout size and alignment