Skip to main content

luaur_vm/functions/
l_alloc.rs

1pub unsafe extern "C" fn l_alloc(
2    ud: *mut core::ffi::c_void,
3    ptr: *mut core::ffi::c_void,
4    osize: usize,
5    nsize: usize,
6) -> *mut core::ffi::c_void {
7    let _ = ud;
8    let _ = osize;
9
10    unsafe {
11        if nsize == 0 {
12            let _ = ptr;
13            if !ptr.is_null() {
14                let _ = libc_free(ptr);
15            }
16            core::ptr::null_mut()
17        } else {
18            if ptr.is_null() {
19                libc_realloc(core::ptr::null_mut(), nsize)
20            } else {
21                libc_realloc(ptr, nsize)
22            }
23        }
24    }
25}
26
27// The C allocator surface. On native targets these `extern "C"` symbols bind
28// the platform libc. On `wasm32-unknown-unknown` there is no libc, so they
29// resolve at link time to `luaur_common::wasm_libc`'s size-prefixed allocator
30// (backed by Rust's global allocator) — the VM allocates real memory in the
31// browser rather than the previous null-returning wasm stub.
32extern "C" {
33    fn free(ptr: *mut core::ffi::c_void);
34    fn realloc(ptr: *mut core::ffi::c_void, size: usize) -> *mut core::ffi::c_void;
35}
36
37unsafe fn libc_free(ptr: *mut core::ffi::c_void) {
38    free(ptr);
39}
40
41unsafe fn libc_realloc(ptr: *mut core::ffi::c_void, nsize: usize) -> *mut core::ffi::c_void {
42    realloc(ptr, nsize)
43}