buddy-slab-allocator
A high-performance page-level and byte-level memory allocator designed for embedded/kernel environments.
Features
- Buddy Page Allocator: Page-level memory allocation
- Slab Byte Allocator: Small object allocation
- Composite Page Allocator: Unified multi-region page allocation interface
- Global Allocator: Coordinates page and byte allocators with unified allocation interface
- Zero
stdDependency: Fully#![no_std], suitable for embedded and kernel environments - Conditional Logging: Support
logfeature for logging, no dependencies by default - Memory Tracking: Support
trackingfeature for detailed statistics
Quick Start
Add Dependency
Add to your Cargo.toml:
[]
= "0.1.0"
# Optional features
= { = "0.1.0", = ["log"] } # Enable logging
= { = "0.1.0", = ["tracking"] } # Enable tracking
Basic Usage
Using Global Allocator
use GlobalAllocator;
use Layout;
// Create global allocator
let mut global = new;
// Initialize the global allocator with memory region
let heap_start = 0x8000_0000;
let heap_size = 16 * 1024 * 1024; // 16MB
global.init.unwrap;
// Or add multiple memory pools
global.add_memory.unwrap;
global.add_memory.unwrap;
// Small object allocation (automatically uses Slab allocator)
let small_layout = from_size_align.unwrap;
let small_ptr = global.alloc.unwrap;
// Large object allocation (automatically uses page allocator)
let large_layout = from_size_align.unwrap;
let large_ptr = global.alloc.unwrap;
// Free memory
global.dealloc;
global.dealloc;
Using Page Allocator Directly
use CompositePageAllocator;
const PAGE_SIZE: usize = 0x1000;
let mut page_alloc = new;
// Initialize with memory region
page_alloc.init.unwrap;
// Allocate pages
let addr = page_alloc.alloc_pages.unwrap;
// Use the allocated memory...
page_alloc.dealloc_pages;
Using Slab Allocator Directly
use SlabByteAllocator;
use PageAllocatorForSlab;
use CompositePageAllocator;
use Layout;
const PAGE_SIZE: usize = 0x1000;
let mut page_alloc = new;
page_alloc.init.unwrap;
let mut slab_alloc = new;
// Small allocations are fast
let layout = from_size_align.unwrap;
let ptr = slab_alloc.alloc.unwrap;
// Free memory
slab_alloc.dealloc;
Features Details
Conditional Logging
Enable logging via log feature:
= { = "0.1.0", = ["log"] }
After enabling, you can use standard log crate macros to log allocation events:
info!;
When disabled, log calls are compiled to no-ops with zero runtime overhead.
Memory Tracking
Enable detailed memory usage tracking via tracking feature:
= { = "0.1.0", = ["tracking"] }
After enabling, you can:
- Collect statistics of bytes allocated for each memory usage
- Record backtrace information for each allocation
- Track allocation generation changes
Performance
- Fast Allocation: Small object allocation has O(1) time complexity
- Memory Efficiency: Buddy algorithm effectively reduces external fragmentation
- Auto Merge: Freed pages automatically merge to reduce fragmentation
Testing
Run the test suite:
# Run all tests
# Run tests with logging enabled
# Run tests with tracking enabled
Benchmarking
This project includes comprehensive benchmarks to evaluate performance and stability under various conditions. For detailed instructions, see benches/README.md.
# Run all benchmarks
For detailed usage instructions, refer to benches/README.md.
Documentation
API documentation is available on docs.rs.
To build and view documentation locally:
License
This project is licensed under:
- GPL-3.0-or-later OR
- Apache-2.0 OR
- MIT
You may choose any of these licenses for your use.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Repository
https://github.com/arceos-hypervisor/buddy-slab-allocator
Chinese Documentation
中文文档请查看 README_CN.md