Expand description
buddy-slab-allocator Memory Allocator
This crate implements a high-performance memory allocator designed for embedded and kernel environments, featuring:
- Buddy page allocator for page-level allocation
- Slab allocator for small object allocation
- Global allocator coordination
- Zero
stddependency (fully#![no_std])
§Features
- Buddy Page Allocator: Efficient page-level memory allocation with automatic merging
- Slab Byte Allocator: Fast small object allocation (≤2048 bytes)
- Global Allocator: Automatic selection between page and slab allocation based on size
- No_std Compatible: Fully
#![no_std]for embedded/kernel use - Optional Logging: Conditional compilation with
logfeature - Memory Tracking: Detailed statistics with
trackingfeature
§Quick Start
use buddy_slab_allocator::{GlobalAllocator, PageAllocator};
use core::alloc::Layout;
const PAGE_SIZE: usize = 0x1000;
let mut allocator = GlobalAllocator::<PAGE_SIZE>::new();
// Initialize with memory region
let heap_start = 0x8000_0000;
let heap_size = 16 * 1024 * 1024; // 16MB
allocator.init(heap_start, heap_size).unwrap();
// Allocate pages
let addr = allocator.alloc_pages(4, PAGE_SIZE).unwrap();
// Use the allocated memory...
allocator.dealloc_pages(addr, 4);§Small Object Allocation
use buddy_slab_allocator::GlobalAllocator;
use core::alloc::Layout;
const PAGE_SIZE: usize = 0x1000;
let mut allocator = GlobalAllocator::<PAGE_SIZE>::new();
allocator.init(0x8000_0000, 16 * 1024 * 1024).unwrap();
// Small allocations go through slab allocator
let layout = Layout::from_size_align(64, 8).unwrap();
let ptr = allocator.alloc(layout).unwrap();
// Use the allocated memory...
allocator.dealloc(ptr, layout);§Statistics Tracking
use buddy_slab_allocator::GlobalAllocator;
const PAGE_SIZE: usize = 0x1000;
let mut allocator = GlobalAllocator::<PAGE_SIZE>::new();
allocator.init(0x8000_0000, 16 * 1024 * 1024).unwrap();
let stats = allocator.get_stats();
println!("Total pages: {}", stats.total_pages);
println!("Used pages: {}", stats.used_pages);
println!("Free pages: {}", stats.free_pages);Re-exports§
pub use buddy::BuddyPageAllocator;pub use buddy::DEFAULT_MAX_ORDER;pub use buddy::MAX_ZONES;pub use page_allocator::CompositePageAllocator;pub use slab::slab_byte_allocator::PageAllocatorForSlab;pub use slab::slab_byte_allocator::SizeClass;pub use slab::slab_byte_allocator::SlabByteAllocator;pub use global_allocator::GlobalAllocator;
Modules§
- buddy
- Buddy page allocator module
- global_
allocator - Global allocator implementation.
- page_
allocator - Page allocator with contiguous block combination support.
- slab
- Slab allocator implementation.
Enums§
- Alloc
Error - The error type used for allocation operations.
Constants§
- DEFAULT_
PAGE_ SIZE - Default page size for backward compatibility (4KB)
Traits§
- Addr
Translator - Address translator used by allocators to reason about physical addresses.
- Base
Allocator - The base allocator trait inherited by other allocator traits.
- Byte
Allocator - Byte-granularity allocator for arbitrary-size allocations.
- IdAllocator
- ID allocator for managing unique identifiers (e.g., thread IDs).
- Page
Allocator - Page-granularity allocator for managing memory in pages.
Type Aliases§
- Alloc
Result - A
Resulttype withAllocErroras the error type.