Browse Source

new GC state to sweep 'localgc' list + small changes in sweep control

pull/9/head
Roberto Ierusalimschy 11 years ago
parent
commit
1bf4faec64
  1. 68
      lgc.c
  2. 12
      lgc.h
  3. 4
      lstate.c
  4. 5
      lstate.h
  5. 5
      ltests.c

68
lgc.c

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.c,v 2.155 2013/08/28 18:30:26 roberto Exp roberto $ ** $Id: lgc.c,v 2.156 2013/08/29 13:34:16 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -979,12 +979,9 @@ static void setpause (global_State *g, l_mem estimate) {
} }
#define sweepphases (bitmask(GCSsweepudata) | bitmask(GCSsweep))
/* /*
** enter first sweep phase and prepare pointers for other sweep phases. ** Enter first sweep phase.
** The calls to 'sweeptolive' make pointers point to an object inside ** The call to 'sweeptolive' makes pointer point to an object inside
** the list (instead of to the header), so that the real sweep do not ** the list (instead of to the header), so that the real sweep do not
** need to skip objects created between "now" and the start of the real ** need to skip objects created between "now" and the start of the real
** sweep. ** sweep.
@ -993,11 +990,9 @@ static void setpause (global_State *g, l_mem estimate) {
static int entersweep (lua_State *L) { static int entersweep (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
int n = 0; int n = 0;
g->gcstate = GCSsweepudata; g->gcstate = GCSsweeplocal;
lua_assert(g->sweepgc == NULL && g->sweepfin == NULL); lua_assert(g->sweepgc == NULL);
/* prepare to sweep finalizable objects and regular objects */ g->sweepgc = sweeptolive(L, &g->localgc, &n);
g->sweepfin = sweeptolive(L, &g->finobj, &n);
g->sweepgc = sweeptolive(L, &g->allgc, &n);
return n; return n;
} }
@ -1068,6 +1063,20 @@ static l_mem atomic (lua_State *L) {
} }
static lu_mem sweepstep (lua_State *L, global_State *g,
int nextstate, GCObject **nextlist) {
if (g->sweepgc) { /* is there still something to sweep? */
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
return (GCSWEEPMAX * GCSWEEPCOST);
}
else { /* next phase */
g->gcstate = nextstate;
g->sweepgc = nextlist;
return 0;
}
}
static lu_mem singlestep (lua_State *L) { static lu_mem singlestep (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
switch (g->gcstate) { switch (g->gcstate) {
@ -1096,30 +1105,21 @@ static lu_mem singlestep (lua_State *L) {
sw = entersweep(L); sw = entersweep(L);
return work + sw * GCSWEEPCOST; return work + sw * GCSWEEPCOST;
} }
case GCSsweepudata: { case GCSsweeplocal: {
if (g->sweepfin) { return sweepstep(L, g, GCSsweepfin, &g->finobj);
g->sweepfin = sweeplist(L, g->sweepfin, GCSWEEPMAX);
return GCSWEEPMAX*GCSWEEPCOST;
}
else {
sweepwholelist(L, &g->localgc);
g->gcstate = GCSsweep;
return GCLOCALPAUSE / 4; /* some magic for now */
}
} }
case GCSsweep: { case GCSsweepfin: {
if (g->sweepgc) { return sweepstep(L, g, GCSsweepall, &g->allgc);
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); }
return GCSWEEPMAX*GCSWEEPCOST; case GCSsweepall: {
} return sweepstep(L, g, GCSsweepmainth, NULL);
else { }
/* sweep main thread */ case GCSsweepmainth: { /* sweep main thread */
GCObject *mt = obj2gco(g->mainthread); GCObject *mt = obj2gco(g->mainthread);
sweeplist(L, &mt, 1); sweeplist(L, &mt, 1);
checkBuffer(L); checkBuffer(L);
g->gcstate = GCSpause; /* finish collection */ g->gcstate = GCSpause; /* finish collection */
return GCSWEEPCOST; return GCSWEEPCOST;
}
} }
default: lua_assert(0); return 0; default: lua_assert(0); return 0;
} }

12
lgc.h

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.h,v 2.68 2013/08/27 20:04:00 roberto Exp roberto $ ** $Id: lgc.h,v 2.69 2013/08/29 13:49:57 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -38,13 +38,15 @@
*/ */
#define GCSpropagate 0 #define GCSpropagate 0
#define GCSatomic 1 #define GCSatomic 1
#define GCSsweepudata 2 #define GCSsweeplocal 2
#define GCSsweep 3 #define GCSsweepfin 3
#define GCSpause 4 #define GCSsweepall 4
#define GCSsweepmainth 5
#define GCSpause 6
#define issweepphase(g) \ #define issweepphase(g) \
(GCSsweepudata <= (g)->gcstate && (g)->gcstate <= GCSsweep) (GCSsweeplocal <= (g)->gcstate && (g)->gcstate <= GCSsweepmainth)
/* /*

4
lstate.c

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.c,v 2.107 2013/08/27 18:53:35 roberto Exp roberto $ ** $Id: lstate.c,v 2.108 2013/08/28 18:30:26 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -301,7 +301,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
g->finobj = NULL; g->finobj = NULL;
g->tobefnz = NULL; g->tobefnz = NULL;
g->fixedgc = NULL; g->fixedgc = NULL;
g->sweepgc = g->sweepfin = NULL; g->sweepgc = NULL;
g->gray = g->grayagain = NULL; g->gray = g->grayagain = NULL;
g->weak = g->ephemeron = g->allweak = NULL; g->weak = g->ephemeron = g->allweak = NULL;
g->totalbytes = sizeof(LG); g->totalbytes = sizeof(LG);

5
lstate.h

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.h,v 2.90 2013/08/26 12:41:10 roberto Exp roberto $ ** $Id: lstate.h,v 2.91 2013/08/27 18:53:35 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -122,8 +122,7 @@ typedef struct global_State {
GCObject *allgc; /* list of all collectable objects */ GCObject *allgc; /* list of all collectable objects */
GCObject *localgc; /* list of local objects */ GCObject *localgc; /* list of local objects */
GCObject *finobj; /* list of collectable objects with finalizers */ GCObject *finobj; /* list of collectable objects with finalizers */
GCObject **sweepgc; /* current position of sweep in list 'allgc' */ GCObject **sweepgc; /* current position of sweep in list */
GCObject **sweepfin; /* current position of sweep in list 'finobj' */
GCObject *gray; /* list of gray objects */ GCObject *gray; /* list of gray objects */
GCObject *grayagain; /* list of objects to be traversed atomically */ GCObject *grayagain; /* list of objects to be traversed atomically */
GCObject *weak; /* list of tables with weak values */ GCObject *weak; /* list of tables with weak values */

5
ltests.c

@ -1,5 +1,5 @@
/* /*
** $Id: ltests.c,v 2.150 2013/08/27 18:53:35 roberto Exp roberto $ ** $Id: ltests.c,v 2.151 2013/08/27 20:04:00 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -653,7 +653,8 @@ static int gc_local (lua_State *L) {
static int gc_state (lua_State *L) { static int gc_state (lua_State *L) {
static const char *statenames[] = {"propagate", "atomic", static const char *statenames[] = {"propagate", "atomic",
"sweepudata", "sweep", "pause", ""}; "sweeplocal", "sweepfin", "sweepall", "sweepmainth",
"pause", ""};
int option = luaL_checkoption(L, 1, "", statenames); int option = luaL_checkoption(L, 1, "", statenames);
if (option == GCSpause + 1) { if (option == GCSpause + 1) {
lua_pushstring(L, statenames[G(L)->gcstate]); lua_pushstring(L, statenames[G(L)->gcstate]);

Loading…
Cancel
Save