kona_common/
malloc.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! This module contains an implementation of a basic memory allocator for client programs in
//! running on top of various FPVMs.
//!
//! The allocator is a linked list allocator based on the `dlmalloc` algorithm, which is a
//! well-known and widely used allocator software such as OS Kernels.

/// The global allocator for the program in FPVM environments.
#[cfg(any(target_arch = "mips", target_arch = "riscv64"))]
pub mod global_allocator {
    use linked_list_allocator::LockedHeap;

    /// The global allocator for the program in other profiles uses the [SpinLockedAllocator].
    #[global_allocator]
    static ALLOCATOR: LockedHeap = LockedHeap::empty();

    /// Initialize the [SpinLockedAllocator] with the following parameters:
    /// * `heap_start_addr` is the starting address of the heap memory region,
    /// * `heap_size` is the size of the heap memory region in bytes.
    ///
    /// # Safety
    /// This function is unsafe because the caller must ensure:
    /// * The allocator has not already been initialized.
    /// * The provided memory region must be valid, non-null, and not used by anything else.
    /// * After aligning the start and end addresses, the size of the heap must be > 0, or the
    ///   function will panic.
    pub unsafe fn init_allocator(heap_start_addr: *mut u8, heap_size: usize) {
        ALLOCATOR.lock().init(heap_start_addr, heap_size)
    }
}

/// Initialize heap memory for the `client` program with the given size.
///
/// # Safety
#[cfg_attr(
    any(target_arch = "mips", target_arch = "riscv64"),
    doc = "See [global_allocator::init_allocator] safety comment."
)]
#[cfg_attr(
    not(any(target_arch = "mips", target_arch = "riscv64")),
    doc = "This macro is entirely safe to invoke in non-MIPS and non-RISC-V64 profiles, and functions as a no-op."
)]
#[macro_export]
macro_rules! alloc_heap {
    ($size:expr) => {{
        #[cfg(any(target_arch = "mips", target_arch = "riscv64"))]
        {
            use kona_common::malloc::global_allocator::init_allocator;

            static mut HEAP: [u8; $size] = [0u8; $size];
            unsafe { init_allocator(HEAP.as_mut_ptr(), $size) }
        }
    }};
}