lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Sep 2, 2010 at 9:27 PM, Ted Unangst <ted.unangst@gmail.com> wrote:
> And allocating the real data as userdata too, and then hanging a reference
> to it, gets to be a pain.

I don't see why it should be any more painful than normal memory
management in C, using something like the following:

void* malloc_lua(size_t sz, lua_State* L)
{
  void* p = lua_newuserdata(L, sz);
  lua_pushlightuserdata(L, p);
  lua_insert(L, -2);
  lua_settable(L, LUA_REGISTRYINDEX);
  return p;
}

void free_lua(void* p, lua_State* L)
{
  lua_pushlightuserdata(L, p);
  lua_pushnil(L);
  lua_settable(L, LUA_REGISTRYINDEX);
}