lua_Alloc

Type Alias lua_Alloc 

Source
pub type lua_Alloc = extern "C-unwind" fn(ud: *mut c_void, ptr: *mut c_void, osize: usize, nsize: usize) -> *mut c_void;
Expand description

The type of the memory allocator function used by Luau’s VM.

  • ud is the same userdata pointer as was passed to lua_newstate.
  • ptr is the pointer to the memory block to be reallocated, or null if osize is zero.
  • osize is the size of the memory block pointed to by ptr.
  • nsize is the new size of the memory block to be allocated.

If osize is zero, ptr will be null and the function should allocate a new memory block of size nsize.

If nsize is zero, the function should free the memory block pointed to be ptr.

If osize and nsize are both non-zero, the function should reallocate the memory block pointed to by ptr to the new size nsize.

§Example

extern "C-unwind" fn alloc(
    ud: *mut c_void,
    ptr: *mut c_void,
    osize: usize,
    nsize: usize
) -> *mut c_void {
    if nsize == 0 {
        unsafe { libc::free(ptr) };
        std::ptr::null_mut()    
    } else {
        unsafe { libc::realloc(ptr, nsize) }
    }
}