diff --git a/lapi.c b/lapi.c index a1796db3..b4dbc8ed 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.141 2010/11/18 19:15:00 roberto Exp roberto $ +** $Id: lapi.c,v 2.141 2010/11/26 14:32:31 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -960,14 +960,16 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { g = G(L); switch (what) { case LUA_GCSTOP: { - stopgc(g); + g->gcrunning = 0; break; } case LUA_GCRESTART: { g->GCdebt = 0; + g->gcrunning = 1; break; } case LUA_GCCOLLECT: { + g->gcrunning = 1; /* restart collector if stopped ?? */ luaC_fullgc(L, 0); break; } @@ -981,7 +983,8 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { break; } case LUA_GCSTEP: { - int stopped = gcstopped(g); + int running = g->gcrunning; + g->gcrunning = 1; /* allow steps */ if (g->gckind == KGC_GEN) { /* generational mode? */ res = (g->lastmajormem == 0); /* 1 if will do major collection */ luaC_step(L); /* do a single step */ @@ -995,8 +998,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { } } } - if (stopped) /* collector was stopped? */ - stopgc(g); /* keep it that way */ + g->gcrunning = running; /* restore previous state */ break; } case LUA_GCSETPAUSE: { @@ -1015,7 +1017,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { break; } case LUA_GCISRUNNING: { - res = !gcstopped(g); + res = g->gcrunning; break; } case LUA_GCGEN: { /* change collector to generational mode */ diff --git a/lgc.c b/lgc.c index 0a59ddc7..bc9f0d4a 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.104 2010/11/26 14:32:31 roberto Exp roberto $ +** $Id: lgc.c,v 2.105 2010/12/03 11:48:25 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -715,15 +715,15 @@ static void GCTM (lua_State *L, int propagateerrors) { if (tm != NULL && ttisfunction(tm)) { /* is there a finalizer? */ int status; lu_byte oldah = L->allowhook; - lu_mem oldd = g->GCdebt; + int running = g->gcrunning; L->allowhook = 0; /* stop debug hooks during GC tag method */ - stopgc(g); /* avoid GC steps */ + g->gcrunning = 0; /* avoid GC steps */ setobj2s(L, L->top, tm); /* push finalizer... */ setobj2s(L, L->top + 1, &v); /* ... and its argument */ L->top += 2; /* and (next line) call the finalizer */ status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); L->allowhook = oldah; /* restore hooks */ - g->GCdebt = oldd; /* restore threshold */ + g->gcrunning = running; /* restore state */ if (status != LUA_OK && propagateerrors) { /* error while running __gc? */ if (status == LUA_ERRRUN) { /* is there an error msg.? */ luaO_pushfstring(L, "error in __gc tag method (%s)", @@ -984,11 +984,14 @@ static void step (lua_State *L) { void luaC_step (lua_State *L) { - int i; - if (isgenerational(G(L))) generationalcollection(L); - else step(L); - for (i = 0; i < GCFINALIZENUM && G(L)->tobefnz; i++) - GCTM(L, 1); /* Call a few pending finalizers */ + global_State *g = G(L); + if (g->gcrunning) { + int i; + if (isgenerational(g)) generationalcollection(L); + else step(L); + for (i = 0; i < GCFINALIZENUM && g->tobefnz; i++) + GCTM(L, 1); /* Call a few pending finalizers */ + } } diff --git a/lgc.h b/lgc.h index 49ca6bf5..8a430294 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.46 2010/12/02 19:51:15 roberto Exp roberto $ +** $Id: lgc.h,v 2.47 2010/12/17 12:02:29 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -52,10 +52,6 @@ #define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic) -#define gcstopped(g) ((g)->GCdebt == MIN_LMEM) -#define stopgc(g) ((g)->GCdebt = MIN_LMEM) - - /* ** some useful bit tricks */ diff --git a/lmem.c b/lmem.c index fd49a789..31c03364 100644 --- a/lmem.c +++ b/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.78 2010/05/04 18:10:02 roberto Exp roberto $ +** $Id: lmem.c,v 1.79 2010/05/05 18:49:56 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -79,14 +79,14 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { size_t realosize = (block) ? osize : 0; lua_assert((realosize == 0) == (block == NULL)); #if defined(HARDMEMTESTS) - if (nsize > realosize && !gcstopped(g)) + if (nsize > realosize && g->gcrunning) luaC_fullgc(L, 1); /* force a GC whenever possible */ #endif newblock = (*g->frealloc)(g->ud, block, osize, nsize); if (newblock == NULL && nsize > 0) { api_check(L, nsize > realosize, "realloc cannot fail when shrinking a block"); - if (!gcstopped(g)) { + if (g->gcrunning) { luaC_fullgc(L, 1); /* try to free some memory... */ newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ } @@ -95,8 +95,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { } lua_assert((nsize == 0) == (newblock == NULL)); g->totalbytes = (g->totalbytes - realosize) + nsize; - if (!gcstopped(g)) - g->GCdebt += nsize; /* give some credit to garbage collector */ + g->GCdebt += nsize; /* give some credit to garbage collector */ #if defined(TRACEMEM) { /* auxiliary patch to monitor garbage collection. ** To plot, gnuplot with following command: @@ -108,9 +107,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { if ((total % 200) == 0) { if (f == NULL) f = fopen(TRACEMEM, "w"); fprintf(f, "%lu %u %d %d\n", total, - g->totalbytes, - gcstopped(g) ? 0 : g->GCdebt, - g->gcstate * 1000); + g->totalbytes, g->GCdebt, g->gcstate * 1000); } } #endif diff --git a/lstate.c b/lstate.c index fee415b6..d13a3ad5 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.86 2010/09/03 14:14:01 roberto Exp roberto $ +** $Id: lstate.c,v 2.87 2010/11/26 14:32:31 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -155,6 +155,7 @@ static void f_luaopen (lua_State *L, void *ud) { g->memerrmsg = luaS_newliteral(L, MEMERRMSG); luaS_fix(g->memerrmsg); /* it should never be collected */ g->GCdebt = 0; + g->gcrunning = 1; /* allow gc */ } @@ -241,7 +242,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->mainthread = L; g->uvhead.u.l.prev = &g->uvhead; g->uvhead.u.l.next = &g->uvhead; - stopgc(g); /* no GC while building state */ + g->gcrunning = 0; /* no GC while building state */ g->lastmajormem = 0; g->strt.size = 0; g->strt.nuse = 0; diff --git a/lstate.h b/lstate.h index 40c3ac16..fd1b2d66 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.68 2010/10/29 17:52:46 roberto Exp roberto $ +** $Id: lstate.h,v 2.69 2010/11/26 14:32:31 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -123,6 +123,7 @@ typedef struct global_State { lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ + lu_byte gcrunning; /* true if GC is running */ int sweepstrgc; /* position of sweep in `strt' */ GCObject *allgc; /* list of all collectable objects */ GCObject *finobj; /* list of collectable objects with finalizers */