Expand description
§RuVix Physical Memory Allocator
This crate provides a buddy allocator for physical page frame allocation as part of the RuVix Cognition Kernel (ADR-087).
§Overview
The buddy allocator manages physical memory using power-of-two block sizes, enabling efficient allocation and deallocation with minimal fragmentation. It supports block sizes from 4KB (single page) to 2MB (512 pages).
§Architecture
+------------------+
| BuddyAllocator |
|------------------|
| free_lists[10] | <- One list per order (0-9)
| base_addr | <- Start of managed memory
| total_pages | <- Total pages under management
| stats | <- Allocation statistics
+------------------+
|
v
+------------------+
| Free Lists |
|------------------|
| Order 0: 4KB | <- Single pages
| Order 1: 8KB | <- 2 pages
| Order 2: 16KB | <- 4 pages
| ... |
| Order 9: 2MB | <- 512 pages
+------------------+§Features
std: Enable standard library supportalloc: Enable alloc crate supportstats: Enable detailed statistics collectiondebug-alloc: Enable debug assertions for allocation tracking
§Example
ⓘ
use ruvix_physmem::{BuddyAllocator, PhysAddr, PAGE_SIZE};
// Create allocator for 16MB of memory starting at physical address 0x1000_0000
let mut allocator = BuddyAllocator::new(PhysAddr::new(0x1000_0000), 4096);
// Allocate 4 contiguous pages (16KB)
if let Some(addr) = allocator.alloc_pages(4) {
// Use the memory...
allocator.dealloc_pages(addr, 4);
}Structs§
- Allocator
Stats - Statistics for the buddy allocator.
- Buddy
Allocator - A buddy allocator for physical page frames.
- Page
Frame - A physical page frame.
- Page
Order - The order of a page frame block.
- Phys
Addr - A physical memory address.
Enums§
- Kernel
Error - Kernel error codes.
- Phys
MemError - Physical memory allocator errors.
Constants§
- MAX_
BLOCK_ PAGES - Maximum block size in pages (2^(MAX_ORDER-1) = 512 pages).
- MAX_
BLOCK_ SIZE - Maximum block size in bytes (2MB).
- MAX_
ORDER - Maximum order for buddy allocation (2^9 = 512 pages = 2MB).
- MIN_
ORDER - Minimum allocation unit (single page).
- PAGE_
SHIFT - Page size shift (log2(PAGE_SIZE)).
- PAGE_
SIZE - Page size in bytes (4KB).
Functions§
- align_
down - Aligns an address down to the nearest page boundary.
- align_
up - Aligns an address up to the nearest page boundary.
- is_
page_ aligned - Checks if an address is page-aligned.
- order_
to_ bytes - Calculates the block size in bytes for a given order.
- order_
to_ pages - Calculates the number of pages for a given order.
- pages_
to_ order - Calculates the order required for a given number of pages.