lua-users home
lua-l archive

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


As the subject says, I've implemented a per-tag explicit garbage collection.  It is 
my opinion that this kind of feature is necessary for applications that are 
sensitive to resource usage on the C-side.  Put in another way, it provides more 
granularity into Lua's implicit garbage collection semantics from the explicit 
malloc/free world of C.  I'd like to know what people think of this, and whether or 
not people think this, or something like it should go into a future version of Lua.  
Should I also add this is a proposal on http://lua-users.org/wiki/?

I am currently using Lua 4.0 so I don't know what forward porting is required for 
4.1, but in any event here are my additions:

To src/lgc.c I added:

-------------------------------------------------------------------
/* 
 * Extension to LUA's garbage collection system for user data
 * Paul Hsieh
 */
void luaC_udatacollectgarbage (lua_State *L, int tag) {
  TObject o;
  TString *udata;

  markall(L);
  invalidaterefs(L);  /* check unlocked references */
  collectudata(L, 0);

  ttype(&o) = LUA_TUSERDATA;
  L->GCthreshold = 2*L->nblocks;  /* avoid GC during tag methods */
  while ((udata = L->TMtable[tag].collected) != NULL) {
    L->TMtable[tag].collected = udata->nexthash;  /* remove it from list */
    tsvalue(&o) = udata;
    callgcTM(L, &o);
    luaM_free(L, udata);
  }

  checkMbuffer(L);
  L->GCthreshold = 2*L->nblocks;  /* set new threshold */

  callgcTM(L, &luaO_nilobject);
}
-------------------------------------------------------------------

to src/lapi.c I added:

-------------------------------------------------------------------
/* 
 * Extension to LUA's garbage collection system for user data
 * Paul Hsieh
 */
LUA_API void lua_udatacollectgarbage (lua_State *L, int tag) {
  luaC_udatacollectgarbage (L, tag);
}
-------------------------------------------------------------------

to include/lua.h I added:

-------------------------------------------------------------------
/* 
 * Extension to LUA's garbage collection system for user data
 * Paul Hsieh
 */
LUA_API void  lua_udatacollectgarbage (lua_State *L, int tag);
-------------------------------------------------------------------

It appears to work as expected.  However, just as a matter of good design, it might 
make sense to perform per tagged userdata core garbage collection from a seperate 
function that is callable both from luaC_udatacollectgarbage() and callgcTMudata().


--
Paul Hsieh
qed@pobox.com