Struct Heap

Source
pub struct Heap<'a> { /* private fields */ }
Expand description

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.

Implementations§

Source§

impl<'a> Heap<'a>

Source

pub unsafe fn new( 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.

Source

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

Source

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

Source

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

Source

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

Source

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

Trait Implementations§

Source§

impl<'a> Send for Heap<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Heap<'a>

§

impl<'a> RefUnwindSafe for Heap<'a>

§

impl<'a> !Sync for Heap<'a>

§

impl<'a> Unpin for Heap<'a>

§

impl<'a> !UnwindSafe for Heap<'a>

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.