Skip to main content

islet_rmm/
allocator.rs

1use core::mem::MaybeUninit;
2use core::ptr::addr_of_mut;
3use linked_list_allocator::LockedHeap;
4
5use crate::config::RMM_HEAP_SIZE;
6
7static mut HEAP: [MaybeUninit<u8>; RMM_HEAP_SIZE] = [MaybeUninit::uninit(); RMM_HEAP_SIZE];
8#[global_allocator]
9static mut ALLOCATOR: LockedHeap = LockedHeap::empty();
10
11/// Initializes the global allocator with a heap backed by the `HEAP` array.
12///
13/// # Safety
14///
15/// - This function must be called exactly once before any memory allocation occurs.
16///   Calling it multiple times or after allocations have started can lead to undefined behavior.
17pub unsafe fn init() {
18    ALLOCATOR.lock().init_from_slice(&mut *addr_of_mut!(HEAP));
19}
20
21pub fn get_used_size() -> usize {
22    unsafe { ALLOCATOR.lock().used() }
23}