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

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 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.

source

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,

source

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.

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 total_bytes(&self) -> usize

Returns total memory size in bytes of the heap.

source

pub fn used_bytes(&self) -> usize

Returns allocated memory size in bytes.

source

pub fn available_bytes(&self) -> usize

Returns available memory size in bytes.

Auto Trait Implementations§

§

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>,

§

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>,

§

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.