lua-users home
lua-l archive

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

> Don't you think that this (below) will be much simpler and more
> versatile?
> LUA_API void
> lua_gcaccount(lua_State *L, long amount)
> {
>     L->nblocks += amount;
> }

You can achieve this same effect with the current API. Istead of add to 
`nblocks', you subtract from the `threshold'.

Because Lua recomputes the threshold when it does a GC cycle, you need
a GC fallback to set it back to the value you want. The final code
would be something like this:

int extracount = 0;  /* extra memory to be counted in Lua (in Kbytes) */

/* function to be set as tag method for GC - nil */
int gc_tm (lua_State *L) {
  lua_setgcthreshold(L, lua_getgcthreshold(L) + extracount);

void lua_gcaccount (lua_State *L, int amount) {
  int newt;
  extracount += amount;
  newt = lua_getgcthreshold(L) - amount;
  if (newt < 0) newt = 0;
  lua_setgcthreshold(L, newt);


-- Roberto