FrameAllocator

Struct FrameAllocator 

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

A frame allocator that uses buddy system, requiring a global allocator.

The max order of the allocator is determined by the const generic parameter ORDER (MAX_ORDER = ORDER - 1). The frame allocator will only be able to allocate ranges of size up to 2MAX_ORDER, out of a total range of size at most 2MAX_ORDER + 1 - 1.

§Usage

Create a frame allocator and add some frames to it:

use buddy_system_allocator::*;
// Notice that the max order is `ORDER - 1`.
let mut frame = FrameAllocator::<33>::new();
assert!(frame.alloc(1).is_none());

frame.add_frame(0, 3);
let num = frame.alloc(1);
assert_eq!(num, Some(2));
let num = frame.alloc(2);
assert_eq!(num, Some(0));

Implementations§

Source§

impl<const ORDER: usize> FrameAllocator<ORDER>

Source

pub const fn new() -> Self

Create an empty frame allocator

Source

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

Add a range of frame number [start, end) to the allocator

Source

pub fn insert(&mut self, range: Range<usize>)

Add a range of frames to the allocator.

Source

pub fn alloc(&mut self, count: usize) -> Option<usize>

Allocate a range of frames from the allocator, returning the first frame of the allocated range.

Source

pub fn alloc_aligned(&mut self, layout: Layout) -> Option<usize>

Allocate a range of frames with the given size and alignment from the allocator, returning the first frame of the allocated range. The allocated size is the maximum of the next power of two of the given size and the alignment.

Source

pub fn dealloc(&mut self, start_frame: usize, count: usize)

Deallocate a range of frames [frame, frame+count) from the frame allocator.

The range should be exactly the same when it was allocated, as in heap allocator

Source

pub fn dealloc_aligned(&mut self, start_frame: usize, layout: Layout)

Deallocate a range of frames which was previously allocated by [alloc_aligned].

The layout must be exactly the same as when it was allocated.

Trait Implementations§

Source§

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

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

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

§

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

§

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

§

impl<const ORDER: usize> Sync for FrameAllocator<ORDER>

§

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

§

impl<const ORDER: usize> UnwindSafe for FrameAllocator<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.