Skip to main content

BuddyAllocator

Struct BuddyAllocator 

Source
pub struct BuddyAllocator<const TOTAL_PAGES: usize, const BITMAP_WORDS: usize> { /* private fields */ }
Expand description

A buddy allocator managing TOTAL_PAGES of physical memory.

The allocator is entirely stack-allocated with a fixed-size bitmap. TOTAL_PAGES must be a power of two and at most 2^MAX_ORDER * some_count.

§Type Parameters

  • TOTAL_PAGES: The total number of 4 KiB pages managed. Must be a power of two.
  • BITMAP_WORDS: The number of u64 words in the bitmap. Must be at least total_bitmap_bits(TOTAL_PAGES) / 64 + 1. Use BuddyAllocator::REQUIRED_BITMAP_WORDS to compute this.

Implementations§

Source§

impl<const TOTAL_PAGES: usize, const BITMAP_WORDS: usize> BuddyAllocator<TOTAL_PAGES, BITMAP_WORDS>

Source

pub const REQUIRED_BITMAP_WORDS: usize

The number of bitmap u64 words required for TOTAL_PAGES.

Source

pub fn new(base: PhysAddr) -> RvmResult<Self>

Create a new buddy allocator managing memory starting at base.

All blocks are initially marked as free. base must be page-aligned.

§Errors

Returns RvmError::AlignmentError if base is not page-aligned. Returns RvmError::ResourceLimitExceeded if BITMAP_WORDS is insufficient.

Source

pub fn alloc_pages(&mut self, order: usize) -> RvmResult<PhysAddr>

Allocate 2^order contiguous pages.

Returns the base PhysAddr of the allocated block.

Uses trailing_zeros on bitmap words for fast first-free-block scanning: O(1) per 64-bit word instead of checking bit-by-bit.

§Errors

Returns RvmError::OutOfMemory if no block of the requested size is available.

Source

pub fn free_pages(&mut self, addr: PhysAddr, order: usize) -> RvmResult<()>

Free a previously allocated block of 2^order pages starting at addr.

The caller must ensure addr was returned by a prior alloc_pages(order) call.

§Errors

Returns RvmError::AlignmentError if the address is invalid or misaligned. Returns RvmError::InvalidTierTransition if the order exceeds the maximum. Returns RvmError::InternalError on double-free detection.

Source

pub fn free_page_count(&self) -> usize

Return the total number of free pages across all orders.

Auto Trait Implementations§

§

impl<const TOTAL_PAGES: usize, const BITMAP_WORDS: usize> Freeze for BuddyAllocator<TOTAL_PAGES, BITMAP_WORDS>

§

impl<const TOTAL_PAGES: usize, const BITMAP_WORDS: usize> RefUnwindSafe for BuddyAllocator<TOTAL_PAGES, BITMAP_WORDS>

§

impl<const TOTAL_PAGES: usize, const BITMAP_WORDS: usize> Send for BuddyAllocator<TOTAL_PAGES, BITMAP_WORDS>
where [u64; BITMAP_WORDS]: Send,

§

impl<const TOTAL_PAGES: usize, const BITMAP_WORDS: usize> Sync for BuddyAllocator<TOTAL_PAGES, BITMAP_WORDS>
where [u64; BITMAP_WORDS]: Sync,

§

impl<const TOTAL_PAGES: usize, const BITMAP_WORDS: usize> Unpin for BuddyAllocator<TOTAL_PAGES, BITMAP_WORDS>

§

impl<const TOTAL_PAGES: usize, const BITMAP_WORDS: usize> UnsafeUnpin for BuddyAllocator<TOTAL_PAGES, BITMAP_WORDS>

§

impl<const TOTAL_PAGES: usize, const BITMAP_WORDS: usize> UnwindSafe for BuddyAllocator<TOTAL_PAGES, BITMAP_WORDS>

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.