Skip to main content

Crate ruvix_physmem

Crate ruvix_physmem 

Source
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 support
  • alloc: Enable alloc crate support
  • stats: Enable detailed statistics collection
  • debug-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§

AllocatorStats
Statistics for the buddy allocator.
BuddyAllocator
A buddy allocator for physical page frames.
PageFrame
A physical page frame.
PageOrder
The order of a page frame block.
PhysAddr
A physical memory address.

Enums§

KernelError
Kernel error codes.
PhysMemError
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.