BestFitAllocator

Struct BestFitAllocator 

Source
pub struct BestFitAllocator {
    head: Option<NonNull<u8>>,
}
Expand description

This is an allocator implementation that uses the best fit strategy. That does mean, when we allocate a block, we try to find the smallest block that fits the requested size. Blocks are stored in a singly linked list. The important part is that the linked list is stored in-line with the memory blocks. This means that every block has a header that contains the size of the block and a pointer to the next block.

Fields§

§head: Option<NonNull<u8>>

Head of the free block list.

Implementations§

Source§

impl BestFitAllocator

Implementation of the BestFitAllocator.

Source

pub const MIN_RANGE_SIZE: usize

Source

pub const fn new() -> Self

Creates a new BestFitAllocator.

Returns the new BestFitAllocator.

Source

pub unsafe fn add_range(&mut self, range: &Range<PhysAddr>) -> Result<(), Error>

Adds a range of memory to the allocator.

range - The range of memory to add.

Returns Ok(()) if the range was added successfully, otherwise an error.

§Safety

The range must be valid, 128bit aligned and must not overlapping with any other current or future range. The range must also be at least as large as MIN_RANGE_SIZE. Also the range must stay valid, for the whole lifetime of the allocator. Also the lifetime of any allocation is only valid as long as the allocator is valid.

Source

const fn align_up() -> usize

Calculates the padding required to align the block. Note: We only align to 128bit.

Returns the padding in bytes.

Source

fn select_block( &mut self, size: usize, requested: Option<PhysAddr>, ) -> Result<(NonNull<u8>, Option<NonNull<u8>>), Error>

Selects the best fit block for the given size.

size - The size of the block.

Returns the control pointer to the block and the control pointer to the previous block.

Source

unsafe fn user_ptr(ptr: NonNull<u8>) -> NonNull<u8>

Calculates the user pointer from the control pointer.

ptr - The control pointer.

Returns the user pointer.

§Safety

The ptr must be a valid control pointer. Note: After the allocator which allocated the pointer is dropped, the control pointer is always considered invalid.

Source

unsafe fn control_ptr(ptr: NonNull<u8>) -> NonNull<u8>

Calculates the control pointer from the user pointer.

ptr - The user pointer.

Returns the control pointer.

§Safety

The ptr must be a valid user pointer. Note: After the allocator which allocated the pointer is dropped, the user pointer is always considered invalid.

Source

unsafe fn contains(meta: &BestFitMeta, target: PhysAddr, size: usize) -> bool

Trait Implementations§

Source§

impl Allocator for BestFitAllocator

Implementation of the Allocator trait for BestFitAllocator.

Source§

fn malloc<T>( &mut self, size: usize, align: usize, request: Option<PhysAddr>, ) -> Result<NonNull<T>, Error>

Allocates a block of memory with the given size and alignment. Note: This function will always yield an invalid align for align > 128bit.

size - The size of the block. align - The alignment of the block.

Returns the user pointer to the block if successful, otherwise an error.

Source§

unsafe fn free<T>(&mut self, ptr: NonNull<T>, size: usize)

Frees a block of memory.

ptr - The pointer to the block. size - The size of the block. (This is used to check if the size of the block is correct.)

Source§

impl Debug for BestFitAllocator

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.