Struct 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

Source

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.

Source

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.

Source

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,

Source

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.

Source

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.

Source

pub fn layout_to_allocator(layout: &Layout) -> HeapAllocator

Finds allocator to use based on layout size and alignment

Auto Trait Implementations§

§

impl Freeze for Heap

§

impl RefUnwindSafe for Heap

§

impl Send for Heap

§

impl !Sync for Heap

§

impl Unpin for Heap

§

impl !UnwindSafe for Heap

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.