Framework for allocating memory in #![no_std] modules.
Requirements
- Rust 1.6
Documentation
Currently there is no standard way to allocate memory from within a module that is no_std. This provides a mechanism to describe a memory allocation that can be satisfied entirely on the stack, by unsafely linking to calloc, or by unsafely referencing a mutable global variable. This library currently will leak memory if free_cell isn't specifically invoked on memory.
However, if linked by a library that actually can depend on the stdlib then that library can simply pass in a few allocators and use the standard Box allocation and will free automatically.
This library should also make it possible to entirely jail a rust application that needs dynamic allocations by preallocating a maximum limit of data upfront using calloc and using seccomp to disallow future syscalls.
Usage
There are 3 modes for allocating memory, each with advantages and disadvantages
On the stack
This is possible without the stdlib at all However, this eats into the natural ulimit on the stack depth and generally limits the program to only a few megs of dynamically allocated data
Example:
// First define a struct to hold all the array on the stack.
declare_stack_allocator_struct!;
// since generics cannot be used, the actual struct to hold the memory must be defined with a macro
...
// in the code where the memory must be used, first the array needs to be readied
let mut stack_buffer = define_allocator_memory_pool!;
// then an allocator needs to be made and pointed to the stack_buffer on the stack
// the final argument tells the system if free'd data should be zero'd before being
// reused by a subsequent call to alloc_cell
let mut ags = new_allocator;
On the heap
This uses the standard Box facilities to allocate memory
let mut halloc = new;
for _i in 1..10
On the heap, but uninitialized
This does allocate data every time it is requested, but it does not allocate the memory, so naturally it is unsafe. The caller must initialize the memory properly
let mut halloc = unsafe;
On the heap in a single pool allocation
This does a single big allocation on the heap, after which no further usage of the stdlib will happen. This can be useful for a jailed application that wishes to restrict syscalls at this point
use HeapPrealloc;
...
let mut heap_global_buffer = define_allocator_memory_pool!;
let mut ags = new_allocator;
On the heap, uninitialized
This does a single big allocation on the heap, after which no further usage of the stdlib will happen. This can be useful for a jailed application that wishes to restrict syscalls at this point. This option keep does not set the memory to a valid value, so it is necessarily marked unsafe
use HeapPrealloc;
...
let mut heap_global_buffer = unsafe;
let mut ags = new_allocator;
With calloc
This is the most efficient way to get a zero'd dynamically sized buffer without the stdlib It does invoke the C calloc function and hence must invoke unsafe code. In this version, the number of cells are fixed to the parameter specified in the struct definition (4096 in this example)
extern
declare_stack_allocator_struct!;
...
// the buffer is defined with 200 megs of zero'd memory from calloc
let mut calloc_global_buffer = unsafe ;
// and assigned to a new_allocator
let mut ags = new_allocator;
With a static, mutable buffer
If a single buffer of data is needed for the entire span of the application Then the simplest way to do so without a zero operation on the memory and without using the stdlib is to simply have a global allocated structure. Accessing mutable static variables requires unsafe code; however, so this code will invoke an unsafe block.
Make sure to only reference global_buffer in a single place, at a single time in the code If it is used from two places or at different times, undefined behavior may result, since multiple allocators may get access to global_buffer.
declare_stack_allocator_struct!;
define_allocator_memory_pool!;
...
// this references a global buffer
let mut ags = new_allocator;
unsafe
Contributors
- Daniel Reiter Horn