Struct BestFitAllocator

Source
pub struct BestFitAllocator { /* private fields */ }
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.

Implementations§

Source§

impl BestFitAllocator

Implementation of the BestFitAllocator.

Source

pub const MIN_RANGE_SIZE: usize = 17usize

Source

pub const fn new() -> Self

Creates a new BestFitAllocator.

Returns the new BestFitAllocator.

Source

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

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.

Trait Implementations§

Source§

impl Allocator for BestFitAllocator

Implementation of the Allocator trait for BestFitAllocator.

Source§

fn malloc<T>( &mut self, size: usize, align: usize, ) -> Result<NonNull<T>, KernelError>

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

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.