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 ofu64words in the bitmap. Must be at leasttotal_bitmap_bits(TOTAL_PAGES) / 64 + 1. UseBuddyAllocator::REQUIRED_BITMAP_WORDSto compute this.
Implementations§
Source§impl<const TOTAL_PAGES: usize, const BITMAP_WORDS: usize> BuddyAllocator<TOTAL_PAGES, BITMAP_WORDS>
impl<const TOTAL_PAGES: usize, const BITMAP_WORDS: usize> BuddyAllocator<TOTAL_PAGES, BITMAP_WORDS>
Sourcepub const REQUIRED_BITMAP_WORDS: usize
pub const REQUIRED_BITMAP_WORDS: usize
The number of bitmap u64 words required for TOTAL_PAGES.
Sourcepub fn new(base: PhysAddr) -> RvmResult<Self>
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.
Sourcepub fn alloc_pages(&mut self, order: usize) -> RvmResult<PhysAddr>
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.
Sourcepub fn free_pages(&mut self, addr: PhysAddr, order: usize) -> RvmResult<()>
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.
Sourcepub fn free_page_count(&self) -> usize
pub fn free_page_count(&self) -> usize
Return the total number of free pages across all orders.