Expand description
Frame allocator module: provides physical frame allocators for managing fixed-size memory frames.
§Physical Frame Allocators for Rust
This module provides abstractions and implementations for physical memory frame allocation. Frame allocators are a core component of operating system kernels and low-level memory managers.
§What is a Frame Allocator?
A frame allocator manages physical memory in fixed-size blocks called “frames” (typically 4 KiB each). It is responsible for handing out unused frames for use by the kernel, page tables, or user processes, and for reclaiming frames when they are no longer needed.
§Why Use Frame Allocators?
- Paging and Virtual Memory: Frame allocators are essential for mapping virtual memory to physical memory.
- OS Kernels: Any kernel that manages its own memory (paging, heap, stacks) needs a way to allocate and free physical frames.
- Predictability: By using fixed-size frames, fragmentation is minimized and allocation is fast and simple.
§When and How to Use
- Use a frame allocator when you need to allocate or free physical memory for page tables, kernel heaps, or user processes.
- Choose a bump allocator for simple, one-shot allocation (e.g., early boot, when you never free frames).
- Choose a free-list allocator when you need to support freeing and reusing frames (e.g., after boot, for dynamic memory management).
§Provided Types
- [
PhysFrame]: Represents a single physical frame of memory. - [
FrameAllocator]: Trait for frame allocators (allocate and deallocate frames). - [
BumpFrameAllocator]: Simple bump allocator for frames (fast, no reuse). - [
FreeListFrameAllocator]: Free-list allocator for frames (supports reuse).
§Safety
- The caller must ensure that the memory region given to an allocator is valid and not used elsewhere.
- Allocators do not check for aliasing or overlapping regions.
- All frame addresses are aligned to
FRAME_SIZE.
§Testing
The implementations here are tested for:
- Correct alignment and address calculation for frames
- Exhaustion and out-of-memory conditions
- No reuse in bump allocator, correct reuse in free-list allocator
- Double-free handling in free-list allocator
- Correct allocation order (LIFO) in free-list allocator
- Handling of zero-sized and unaligned regions
See the module’s tests for details.
Structs§
- Bump
Frame Allocator - A simple bump allocator for physical memory frames.
- Free
List Frame Allocator - A free list allocator for physical memory frames.