Module elfmalloc::general [] [src]

Implementation of general allocator routines based off of the Slag allocator design.

The primary use of this module is to provide the rudaments of a malloc-compatible global allocator that can be used from C/C++ and Rust programs alike. The elfc crate that wraps this one exposes such an interface. It is currently possible to use this module as a Rust library, though we do not recommend it.

Using this Allocator from Rust

We currently rely on some global allocator (bsalloc) to be running to service normal heap allocations. As a result, this allocator cannot be used as a global allocator via the #[global_allocator] attribute. Currently the only way around this is to use the System allocator along with libelfc from the elfc crate loaded with LD_PRELOAD.

It is also possible to use this allocator using a Clone-based API. As alluded to elsewhere, the allocator is thread-safe and any handle on the allocator can be used to free a pointer from any other handle in any other thread. If you free a pointer alloc-ed by another DynamicAllocator, bad things will happen.

// all calls to `alloc` and `free` are unsafe
let mut elf = DynamicAllocator::new();
let ptr = elf.alloc(16) as *mut [u8; 16];
let mut elf_clone = elf.clone();
let res = thread::spawn(move || {
    elf_clone.alloc(24) as *mut [u8; 24]
}).join().unwrap();
elf.free(res);
elf.free(ptr);

This is probably a more limited use-case until custom allocators have better support in the Rust ecosystem. Even then, we suspect most programmers using a non-global allocator will instead want something more specialized, such as the LocalAllocator and MagazineAllocator object-specific allocators.

Modules

global

A global malloc-style interface to interact with a DynamicAllocator. All of these structures are lazily initailized.

Structs

DynamicAllocator

A Dynamic memory allocator, instantiated with sane defaults for various ElfMalloc type parameters.