| Crates.io | chopalloc |
| lib.rs | chopalloc |
| version | 1.0.0 |
| created_at | 2025-11-02 13:28:24.671883+00 |
| updated_at | 2025-11-02 13:28:24.671883+00 |
| description | A minimal, no_std buddy allocator for embedded systems and bare-metal environments with optional O(1) bitmap optimization |
| homepage | |
| repository | https://github.com/Chirping666/chopalloc-rs |
| max_upload_size | |
| id | 1913125 |
| size | 77,185 |
A minimal, no_std buddy allocator for embedded systems and bare-metal environments.
bitmap (default): Enables O(1) buddy checking using a bitmap. Disable for pure free-list implementation (O(n) checks, less memory overhead).The MAX_ORDER const generic parameter determines how many block size levels the allocator manages.
Formula: MAX_ORDER = log2(memory_size_in_bytes) + 1
Examples:
BuddyAllocator::<11> (2^10 = 1024)BuddyAllocator::<13> (2^12 = 4096)BuddyAllocator::<17> (2^16 = 65536)The allocator will manage blocks from 2^0 up to 2^(MAX_ORDER-1) bytes.
use chopalloc::BuddyAllocator;
use core::alloc::Layout;
// With bitmap (default)
#[repr(align(1024))]
static mut MEMORY: [u8; 1024] = [0; 1024];
static mut BITMAP: [u64; 64] = [0; 64];
let allocator = unsafe {
let memory_ptr = NonNull::new(MEMORY.as_mut_ptr()).unwrap();
let bitmap_words = BuddyAllocator::<11>::calculate_bitmap_words_needed(1024);
let bitmap_slice = &mut BITMAP[..bitmap_words];
BuddyAllocator::<11>::new(memory_ptr, 1024, bitmap_slice).unwrap()
};
let layout = Layout::from_size_align(64, 64).unwrap();
let ptr = allocator.try_allocate(layout).unwrap();
allocator.try_deallocate(ptr, layout).unwrap();
// In Cargo.toml: chopalloc = { version = "1.0", default-features = false }
let allocator = unsafe {
let memory_ptr = NonNull::new(MEMORY.as_mut_ptr()).unwrap();
BuddyAllocator::<11>::new(memory_ptr, 1024).unwrap() // No bitmap parameter
};
The backing memory region must be aligned to the allocator's largest block size
(BuddyAllocator::largest_block_size()). Use #[repr(align(...))] for static buffers:
#[repr(align(1024))] // Align to largest block size
static mut MEMORY: [u8; 1024] = [0; 1024];
See examples/ for:
basic_usage.rs - Fundamental operationsfragmentation_demo.rs - Buddy merging behaviorembedded_simulation.rs - Simulated embedded systemno_bitmap.rs - Free-list-only mode (compile with --no-default-features)