I am working in an environment where I cannot use heap memory but only stack memory. To not be constrained by the #[no_std] enviroment I tried to use stack memory as heap memory with the linked-list-allocator crate. This was my approach.
use linked_list_allocator::LockedHeap;
use std::mem::MaybeUninit;
#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();
pub unsafe fn init_heap(heap_start: usize, heap_size: usize) {
ALLOCATOR.lock().init(heap_start, heap_size);
}
fn main() {
const HEAP_SIZE: usize = 2048;
let mut heap: MaybeUninit<[u8; HEAP_SIZE]> = MaybeUninit::zeroed();
unsafe { init_heap(heap.as_mut_ptr() as usize, HEAP_SIZE) }
println!(
"{} {} {} {} {} {}",
"This", "String", "Has", "Been", "Dynamically", "Allocated"
);
}
Compiling and running the following code on my laptop with an llvm-target of x86_64-unknown-linux-gnu and a rustc version 1.49.0-nightly I get the following error:
memory allocation of 4 bytes failedAborted (core dumped)
Is there some rust compiler's assumptions that I am infringing or is my usage of the linked-list-allocator wrong?
Edit: Given the answer and comments from Masklinn here is a working example:
#![feature(start)]
use linked_list_allocator::LockedHeap;
#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();
pub unsafe fn init_heap(heap_start: usize, heap_size: usize) {
ALLOCATOR.lock().init(heap_start, heap_size);
}
#[start]
fn main(_argn: isize, _argv: *const *const u8) -> isize {
const HEAP_SIZE: usize = 2048;
let mut heap = [0u8; HEAP_SIZE];
unsafe { init_heap(heap.as_mut_ptr() as usize, HEAP_SIZE) }
std::mem::forget(heap);
println!(
"{} {} {} {} {} {}",
"This", "String", "Has", "Been", "Dynamically", "Allocated"
);
0
}