I'm trying to write a lua_Alloc function (https://lua.org/manual/5.4/manual.html#lua_Alloc) using Zig's page_allocator. The allocator checks the size of what it is resizing and freeing, but I have to use anyopaque, which it cannot determine the size of. lua_Alloc does pass osize (the size of the current block of memory) as it's 3rd argument, but I can't see a way to pass that to destroy or resize. I'd also avoid the raw resize and alloc functions if possible.
My current code is as follows:
fn alloc(ud: ?*anyopaque, ptr: ?*anyopaque, osize: usize, nsize: usize) callconv(.C) ?*anyopaque {
const page = std.heap.page_allocator;
_ = ud;
_ = osize;
if (nsize == 0) {
page.destroy(ptr.?);
return null;
} else {
if (page.resize(ptr.?, nsize))
return ptr;
page.destroy(ptr.?);
return null;
}
}```
PS: I do know I can just use `luaL_newstate`, but, for the fun of it, I'd rather do it this way.