Antonito/zigalloc-rs
Use Zig allocators from your Rust project
Zig allocators for Rust applications, primarily useful for development and debugging.
This is a development/debugging tool, not a production allocator.
While Zig's allocators are production ready, this particular library hasn't been properly benchmarked.
For production applications, use proven allocators like jemalloc or mimalloc.
Add to your Cargo.toml
:
[dependencies]
zigalloc = { git = "https://github.com/Antonito/zigalloc-rs" }
Requirements:
#![feature(allocator_api)]
)ZigSmpAllocator
- Thread-safe general-purpose allocator (SmpAllocator)ZigArenaSmpAllocator
- Arena allocator for bulk deallocation (ArenaAllocator)ZigDebugAllocator
- Debug allocator with leak detection (DebugAllocator)ZigGlobalDebugAllocator
- Global debug allocator for app-wide leak detectionZigGlobalSmpAllocator
- Global SMP allocator for performance testingZigGlobalArenaSmpAllocator
- Global arena allocator (mainly for testing)#![feature(allocator_api)]
use zigalloc::ZigDebugAllocator;
fn main() {
let allocator = ZigDebugAllocator::new();
let mut vec = Vec::<u8, &ZigDebugAllocator>::with_capacity_in(1000, &allocator);
vec.push(42);
// Memory leaks will be reported when allocator is dropped
}
use zigalloc::ZigGlobalDebugAllocator;
#[global_allocator]
static GLOBAL: ZigGlobalDebugAllocator = ZigGlobalDebugAllocator;
fn main() {
// All allocations are now tracked for leaks
let vec = vec![1, 2, 3, 4, 5];
let map = std::collections::HashMap::new();
}
The repository includes simple examples for each allocator:
# Custom allocator examples
cargo run --example smp_allocator
cargo run --example arena_allocator
cargo run --example debug_allocator
# Global allocator examples
cargo run --example global_debug_allocator
cargo run --example global_smp_allocator
cargo run --example global_arena_allocator
The build process automatically:
zig
needs to be installed on the machine