An interface to a generic allocator so a no_std rust library can allocate memory, with, or without stdlib being linked.
An interface to a generic allocator so a no_std rust library can allocate memory, with, or without stdlib being linked.
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.
There are 3 modes for allocating memory, each with advantages and disadvantages
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:
…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{HeapAllocUninitialized::::new()};
{ // heap test
let mut x = halloc.alloc_cell(100000);
x[0] = 4;
let mut y = halloc.alloc_cell(110000);
y[0] = 5;
let mut z = halloc.alloc_cell(120000);
z[0] = 6;
assert_eq!(y[0], 5);
halloc.free_cell(y);
assert_eq!(x[0], 4);
assert_eq!(x[9], 0);
assert_eq!(z[0], 6);
...
}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
…Forbid unsafe and moving of the Calloc malloc into the unsafe bin so we can forbid unsafe using the compiler.
Note: this is a breaking change for downstreams that referenced CallocBackingStore/AllocatorC without the "unsafe" feature (e.g. brotli's "seccomp" feature, which should now also enable alloc-no-stdlib/unsafe).
No open issues yet, or sync has not completed.