A portable, customizable Zig implementation of Doug Lea's dlmalloc, translated and restructured from the original C implementation.
- Portable: Works across different architectures and operating systems
- Customizable: Extensive configuration options for different use cases
- Thread-safe: Optional thread safety with configurable locking
- Memory efficient: Implements the proven dlmalloc algorithms
- Platform abstraction: Clean separation between core logic and platform-specific code
- Zig-native: Provides both C-compatible API and idiomatic Zig allocator interface
- Linux: x86_64, aarch64, arm, riscv64
- macOS: x86_64, aarch64 (Apple Silicon)
- Windows: x86_64
- WASI: WebAssembly System Interface
- Other Unix-like systems with basic POSIX support
# Build the library
zig build
# Run tests
zig build test
# Build examples
zig build examples
# Cross-compile for all supported targets
zig build cross-compile
# Run benchmarks
zig build benchmarkconst dlmalloc = @import("dlmalloc-zig");
// Basic allocation
const ptr = dlmalloc.malloc(1024);
defer dlmalloc.free(ptr);
// Use as Zig allocator
var zig_allocator = dlmalloc.ZigAllocator.init(dlmalloc.Config{});
const allocator = zig_allocator.allocator();
const slice = try allocator.alloc(u8, 100);
defer allocator.free(slice);The allocator can be customized through the Config struct:
const config = dlmalloc.Config{
.max_fast = 64, // Max size for fast bins
.trim_threshold = 128 * 1024, // When to trim memory
.mmap_threshold = 128 * 1024, // When to use mmap
.use_mmap = true, // Enable mmap support
.use_sbrk = true, // Enable sbrk support
.use_locks = false, // Thread safety
.debug = false, // Debug mode
};malloc(size)- Allocate memoryfree(ptr)- Free memorycalloc(num, size)- Allocate zero-initialized memoryrealloc(ptr, size)- Reallocate memorymemalign(align, size)- Allocate aligned memoryvalloc(size)- Allocate page-aligned memorymalloc_usable_size(ptr)- Get usable size of allocation
All functions are also available with dl prefix:
dlmalloc(),dlfree(),dlcalloc(), etc.
mallopt(param, value)- Configure allocator parametersmalloc_stats()- Print memory statistics
ZigAllocator- Zig-native allocator implementationConfig- Configuration structurePlatform- Platform abstraction utilities
The implementation is structured into several modules:
src/dlmalloc.zig- Main API and global allocatorsrc/allocator.zig- Core allocation algorithmssrc/chunk.zig- Memory chunk managementsrc/platform.zig- Platform abstraction layersrc/c_compat.zig- Dynamic C header inclusionsrc/config.zig- Configuration system
The platform layer handles:
- Memory mapping (mmap/VirtualAlloc)
- Heap extension (sbrk)
- Page size detection
- Pointer validation
- Platform-specific optimizations
Thread safety can be enabled through configuration:
const config = dlmalloc.Config{
.use_locks = true,
// ... other options
};The allocator implements several optimization strategies:
- Fast bins for small, frequently allocated sizes
- Small bins for medium-sized allocations
- mmap for large allocations
- Coalescing of adjacent free blocks
- Top chunk management for heap extension
See benchmarks/ for performance testing.
Enable debug mode for additional checks:
const config = dlmalloc.Config{
.debug = true,
.abort_on_corruption = true,
};Configure the build with options:
zig build -Duse_mmap=false -Ddebug=true -Duse_locks=trueAvailable options:
use_mmap- Enable mmap support (default: true)use_sbrk- Enable sbrk support (default: true)use_locks- Enable thread safety (default: false)debug- Enable debug mode (default: false)dl_prefix- Use dl prefix for functions (default: false)
Run the test suite:
zig build testRun examples:
# Standard allocator example with defer patterns
zig build std-example
# Basic usage example
zig build example
# External .a library usage
cd examples/external_usage && zig build runThe build process creates libdlmalloc-zig.a in zig-out/lib/ that can be linked from external projects:
// External project build.zig
exe.addLibraryPath(b.path("path/to/zig-out/lib"));
exe.linkSystemLibrary("dlmalloc-zig");
exe.linkLibC();See examples/external_usage/ for a complete example showing:
- Direct C API usage with manual memory management
- Wrapped Zig allocator interface with
defersupport - Complex data structures with automatic cleanup
Tests cover:
- Basic allocation/deallocation
- Alignment requirements
- Reallocation behavior
- Edge cases and error conditions
- Platform compatibility
This project follows Zig coding conventions. Please:
- Run
zig build fmtbefore committing - Ensure
zig build testpasses - Test on multiple platforms when possible
- Update documentation for API changes
This code is released to the public domain, following the original dlmalloc license. Use, modify, and redistribute without restriction.
Based on dlmalloc 2.7.2 by Doug Lea. Zig implementation and platform abstraction by the dlmalloc-zig contributors.