Heap

Struct Heap 

Source
pub struct Heap<const ORDER: usize> { /* private fields */ }
Expand description

A heap that uses buddy system with configurable order.

§Usage

Create a heap and add a memory region to it:

use buddy_system_allocator::*;
// The max order of the buddy system is `ORDER - 1`.
// For example, to create a heap with a maximum block size of 2^32 bytes,
// you should define the heap with `ORDER = 33`.
let mut heap = Heap::<33>::empty();
unsafe {
    heap.init(begin, size);
    // or
    heap.add_to_heap(begin, end);
}

Implementations§

Source§

impl<const ORDER: usize> Heap<ORDER>

Source

pub const fn new() -> Self

Create an empty heap

Source

pub const fn empty() -> Self

Create an empty heap

Source

pub unsafe fn add_to_heap(&mut self, start: usize, end: usize)

Add a range of memory [start, end) to the heap

Source

pub unsafe fn init(&mut self, start: usize, size: usize)

Add a range of memory [start, start+size) to the heap

Source

pub fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, ()>

Alloc a range of memory from the heap satifying layout requirements

Source

pub unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout)

Dealloc a range of memory from the heap

Source

pub fn stats_alloc_user(&self) -> usize

Return the number of bytes that user requests

Source

pub fn stats_alloc_actual(&self) -> usize

Return the number of bytes that are actually allocated

Source

pub fn stats_total_bytes(&self) -> usize

Return the total number of bytes in the heap

Trait Implementations§

Source§

impl<const ORDER: usize> Debug for Heap<ORDER>

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const ORDER: usize> Default for Heap<ORDER>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<const ORDER: usize> Freeze for Heap<ORDER>

§

impl<const ORDER: usize> RefUnwindSafe for Heap<ORDER>

§

impl<const ORDER: usize> Send for Heap<ORDER>

§

impl<const ORDER: usize> !Sync for Heap<ORDER>

§

impl<const ORDER: usize> Unpin for Heap<ORDER>

§

impl<const ORDER: usize> UnwindSafe for Heap<ORDER>

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.