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.
impl BestFitAllocator
Implementation of the BestFitAllocator.
pub const MIN_RANGE_SIZE: usize
Sourcepub unsafe fn add_range(&mut self, range: &Range<PhysAddr>) -> Result<(), Error>
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.
Sourceconst fn align_up() -> usize
const fn align_up() -> usize
Calculates the padding required to align the block. Note: We only align to 128bit.
Returns the padding in bytes.
Sourcefn select_block(
&mut self,
size: usize,
requested: Option<PhysAddr>,
) -> Result<(NonNull<u8>, Option<NonNull<u8>>), Error>
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.
Sourceunsafe fn user_ptr(ptr: NonNull<u8>) -> NonNull<u8>
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.
Sourceunsafe fn control_ptr(ptr: NonNull<u8>) -> NonNull<u8>
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.
unsafe fn contains(meta: &BestFitMeta, target: PhysAddr, size: usize) -> bool
Trait Implementations§
Source§impl Allocator for BestFitAllocator
Implementation of the Allocator trait for BestFitAllocator.
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>
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.