holium_rs_sdk/internal/
memory.rs

1use std::alloc::{alloc, Layout};
2use std::mem;
3
4pub struct Slice {
5    pub ptr: u32,
6    pub len: u32,
7}
8
9/// Allows to allocate memory space for a given size. Used when transmitting data from the host to
10/// a guest module.
11#[no_mangle]
12pub extern "C" fn __hbindgen_mem_alloc(size: usize) -> *mut u8 {
13    let align = mem::align_of::<usize>();
14    if let Ok(layout) = Layout::from_size_align(size, align) {
15        unsafe {
16            if layout.size() > 0 {
17                let ptr = alloc(layout);
18                if !ptr.is_null() {
19                    return ptr;
20                }
21            } else {
22                return align as *mut u8;
23            }
24        }
25    }
26
27    std::process::abort();
28}