Skip to main content

Crate buddy_slab_allocator

Crate buddy_slab_allocator 

Source
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 std dependency (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 log feature
  • Memory Tracking: Detailed statistics with tracking feature

§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§

AllocError
The error type used for allocation.

Constants§

DEFAULT_PAGE_SIZE
Default page size for backward compatibility (4KB)

Traits§

AddrTranslator
Address translator used by allocators to reason about physical addresses.
BaseAllocator
The base allocator inherited by other allocators.
ByteAllocator
Byte-granularity allocator.
IdAllocator
Used to allocate unique IDs (e.g., thread ID).
PageAllocator
Page-granularity allocator.

Type Aliases§

AllocResult
A Result type with AllocError as the error type.