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>
impl<const ORDER: usize> Heap<ORDER>
Sourcepub unsafe fn add_to_heap(&mut self, start: usize, end: usize)
pub unsafe fn add_to_heap(&mut self, start: usize, end: usize)
Add a range of memory [start, end) to the heap
Sourcepub unsafe fn init(&mut self, start: usize, size: usize)
pub unsafe fn init(&mut self, start: usize, size: usize)
Add a range of memory [start, start+size) to the heap
Sourcepub fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, ()>
pub fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, ()>
Alloc a range of memory from the heap satifying layout requirements
Sourcepub unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout)
pub unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout)
Dealloc a range of memory from the heap
Sourcepub fn stats_alloc_user(&self) -> usize
pub fn stats_alloc_user(&self) -> usize
Return the number of bytes that user requests
Sourcepub fn stats_alloc_actual(&self) -> usize
pub fn stats_alloc_actual(&self) -> usize
Return the number of bytes that are actually allocated
Sourcepub fn stats_total_bytes(&self) -> usize
pub fn stats_total_bytes(&self) -> usize
Return the total number of bytes in the heap
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more