lua-users home
lua-l archive

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


On Thu, Sep 2, 2010 at 4:33 PM, Peter Cawley <lua@corsix.org> wrote:
> 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:

Unless I have vastly misunderstood your proposal, these user data
would never be garbage collected.  Explicit frees in Lua are no more
painful than explicit frees in C, but I'd prefer to let the garbage
collector do its job.

>
> 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);
> }
>
>