|
|
@ -1,5 +1,5 @@
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
** $Id: lgc.c,v 2.116 2011/12/02 13:18:41 roberto Exp $
|
|
|
|
** $Id: lgc.c,v 2.133 2012/05/31 21:28:59 roberto Exp $
|
|
|
|
** Garbage Collector
|
|
|
|
** Garbage Collector
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -24,34 +24,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* how much to allocate before next GC step */
|
|
|
|
/*
|
|
|
|
#define GCSTEPSIZE 1024
|
|
|
|
** cost of sweeping one element (the size of a small object divided
|
|
|
|
|
|
|
|
** by some adjust for the sweep speed)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define GCSWEEPCOST ((sizeof(TString) + 4) / 4)
|
|
|
|
|
|
|
|
|
|
|
|
/* maximum number of elements to sweep in each single step */
|
|
|
|
/* maximum number of elements to sweep in each single step */
|
|
|
|
#define GCSWEEPMAX 40
|
|
|
|
#define GCSWEEPMAX (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4))
|
|
|
|
|
|
|
|
|
|
|
|
/* cost of sweeping one element */
|
|
|
|
|
|
|
|
#define GCSWEEPCOST 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* maximum number of finalizers to call in each GC step */
|
|
|
|
/* maximum number of finalizers to call in each GC step */
|
|
|
|
#define GCFINALIZENUM 4
|
|
|
|
#define GCFINALIZENUM 4
|
|
|
|
|
|
|
|
|
|
|
|
/* cost of marking the root set */
|
|
|
|
|
|
|
|
#define GCROOTCOST 10
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* cost of atomic step */
|
|
|
|
/*
|
|
|
|
#define GCATOMICCOST 1000
|
|
|
|
** macro to adjust 'stepmul': 'stepmul' is actually used like
|
|
|
|
|
|
|
|
** 'stepmul / STEPMULADJ' (value chosen by tests)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define STEPMULADJ 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
** macro to adjust 'pause': 'pause' is actually used like
|
|
|
|
|
|
|
|
** 'pause / PAUSEADJ' (value chosen by tests)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define PAUSEADJ 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* basic cost to traverse one object (to be added to the links the
|
|
|
|
|
|
|
|
object may have) */
|
|
|
|
|
|
|
|
#define TRAVCOST 5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
** standard negative debt for GC; a reasonable "time" to wait before
|
|
|
|
** standard negative debt for GC; a reasonable "time" to wait before
|
|
|
|
** starting a new cycle
|
|
|
|
** starting a new cycle
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
#define stddebt(g) (-cast(l_mem, gettotalbytes(g)/100) * g->gcpause)
|
|
|
|
#define stddebtest(g,e) (-cast(l_mem, (e)/PAUSEADJ) * g->gcpause)
|
|
|
|
|
|
|
|
#define stddebt(g) stddebtest(g, gettotalbytes(g))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
@ -65,8 +71,6 @@
|
|
|
|
#define white2gray(x) resetbits(gch(x)->marked, WHITEBITS)
|
|
|
|
#define white2gray(x) resetbits(gch(x)->marked, WHITEBITS)
|
|
|
|
#define black2gray(x) resetbit(gch(x)->marked, BLACKBIT)
|
|
|
|
#define black2gray(x) resetbit(gch(x)->marked, BLACKBIT)
|
|
|
|
|
|
|
|
|
|
|
|
#define stringmark(s) ((void)((s) && resetbits((s)->tsv.marked, WHITEBITS)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define isfinalized(x) testbit(gch(x)->marked, FINALIZEDBIT)
|
|
|
|
#define isfinalized(x) testbit(gch(x)->marked, FINALIZEDBIT)
|
|
|
|
|
|
|
|
|
|
|
@ -123,10 +127,10 @@ static void removeentry (Node *n) {
|
|
|
|
** other objects: if really collected, cannot keep them; for objects
|
|
|
|
** other objects: if really collected, cannot keep them; for objects
|
|
|
|
** being finalized, keep them in keys, but not in values
|
|
|
|
** being finalized, keep them in keys, but not in values
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static int iscleared (const TValue *o) {
|
|
|
|
static int iscleared (global_State *g, const TValue *o) {
|
|
|
|
if (!iscollectable(o)) return 0;
|
|
|
|
if (!iscollectable(o)) return 0;
|
|
|
|
else if (ttisstring(o)) {
|
|
|
|
else if (ttisstring(o)) {
|
|
|
|
stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */
|
|
|
|
markobject(g, rawtsvalue(o)); /* strings are `values', so are never weak */
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else return iswhite(gcvalue(o));
|
|
|
|
else return iswhite(gcvalue(o));
|
|
|
@ -217,7 +221,8 @@ void luaC_checkupvalcolor (global_State *g, UpVal *uv) {
|
|
|
|
GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list,
|
|
|
|
GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list,
|
|
|
|
int offset) {
|
|
|
|
int offset) {
|
|
|
|
global_State *g = G(L);
|
|
|
|
global_State *g = G(L);
|
|
|
|
GCObject *o = obj2gco(cast(char *, luaM_newobject(L, tt, sz)) + offset);
|
|
|
|
char *raw = cast(char *, luaM_newobject(L, novariant(tt), sz));
|
|
|
|
|
|
|
|
GCObject *o = obj2gco(raw + offset);
|
|
|
|
if (list == NULL)
|
|
|
|
if (list == NULL)
|
|
|
|
list = &g->allgc; /* standard list for collectable objects */
|
|
|
|
list = &g->allgc; /* standard list for collectable objects */
|
|
|
|
gch(o)->marked = luaC_white(g);
|
|
|
|
gch(o)->marked = luaC_white(g);
|
|
|
@ -239,54 +244,63 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
** mark an object. Userdata and closed upvalues are visited and turned
|
|
|
|
** mark an object. Userdata, strings, and closed upvalues are visited
|
|
|
|
** black here. Strings remain gray (it is the same as making them
|
|
|
|
** and turned black here. Other objects are marked gray and added
|
|
|
|
** black). Other objects are marked gray and added to appropriate list
|
|
|
|
** to appropriate list to be visited (and turned black) later. (Open
|
|
|
|
** to be visited (and turned black) later. (Open upvalues are already
|
|
|
|
** upvalues are already linked in 'headuv' list.)
|
|
|
|
** linked in 'headuv' list.)
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static void reallymarkobject (global_State *g, GCObject *o) {
|
|
|
|
static void reallymarkobject (global_State *g, GCObject *o) {
|
|
|
|
lua_assert(iswhite(o) && !isdead(g, o));
|
|
|
|
lu_mem size;
|
|
|
|
white2gray(o);
|
|
|
|
white2gray(o);
|
|
|
|
switch (gch(o)->tt) {
|
|
|
|
switch (gch(o)->tt) {
|
|
|
|
case LUA_TSTRING: {
|
|
|
|
case LUA_TSHRSTR:
|
|
|
|
return; /* for strings, gray is as good as black */
|
|
|
|
case LUA_TLNGSTR: {
|
|
|
|
|
|
|
|
size = sizestring(gco2ts(o));
|
|
|
|
|
|
|
|
break; /* nothing else to mark; make it black */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case LUA_TUSERDATA: {
|
|
|
|
case LUA_TUSERDATA: {
|
|
|
|
Table *mt = gco2u(o)->metatable;
|
|
|
|
Table *mt = gco2u(o)->metatable;
|
|
|
|
markobject(g, mt);
|
|
|
|
markobject(g, mt);
|
|
|
|
markobject(g, gco2u(o)->env);
|
|
|
|
markobject(g, gco2u(o)->env);
|
|
|
|
gray2black(o); /* all pointers marked */
|
|
|
|
size = sizeudata(gco2u(o));
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case LUA_TUPVAL: {
|
|
|
|
case LUA_TUPVAL: {
|
|
|
|
UpVal *uv = gco2uv(o);
|
|
|
|
UpVal *uv = gco2uv(o);
|
|
|
|
markvalue(g, uv->v);
|
|
|
|
markvalue(g, uv->v);
|
|
|
|
if (uv->v == &uv->u.value) /* closed? (open upvalues remain gray) */
|
|
|
|
if (uv->v != &uv->u.value) /* open? */
|
|
|
|
gray2black(o); /* make it black */
|
|
|
|
return; /* open upvalues remain gray */
|
|
|
|
|
|
|
|
size = sizeof(UpVal);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case LUA_TLCL: {
|
|
|
|
|
|
|
|
gco2lcl(o)->gclist = g->gray;
|
|
|
|
|
|
|
|
g->gray = o;
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case LUA_TFUNCTION: {
|
|
|
|
case LUA_TCCL: {
|
|
|
|
gco2cl(o)->c.gclist = g->gray;
|
|
|
|
gco2ccl(o)->gclist = g->gray;
|
|
|
|
g->gray = o;
|
|
|
|
g->gray = o;
|
|
|
|
break;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case LUA_TTABLE: {
|
|
|
|
case LUA_TTABLE: {
|
|
|
|
linktable(gco2t(o), &g->gray);
|
|
|
|
linktable(gco2t(o), &g->gray);
|
|
|
|
break;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case LUA_TTHREAD: {
|
|
|
|
case LUA_TTHREAD: {
|
|
|
|
gco2th(o)->gclist = g->gray;
|
|
|
|
gco2th(o)->gclist = g->gray;
|
|
|
|
g->gray = o;
|
|
|
|
g->gray = o;
|
|
|
|
break;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case LUA_TPROTO: {
|
|
|
|
case LUA_TPROTO: {
|
|
|
|
gco2p(o)->gclist = g->gray;
|
|
|
|
gco2p(o)->gclist = g->gray;
|
|
|
|
g->gray = o;
|
|
|
|
g->gray = o;
|
|
|
|
break;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default: lua_assert(0);
|
|
|
|
default: lua_assert(0); return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gray2black(o);
|
|
|
|
|
|
|
|
g->GCmemtrav += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -359,7 +373,7 @@ static void traverseweakvalue (global_State *g, Table *h) {
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
lua_assert(!ttisnil(gkey(n)));
|
|
|
|
lua_assert(!ttisnil(gkey(n)));
|
|
|
|
markvalue(g, gkey(n)); /* mark key */
|
|
|
|
markvalue(g, gkey(n)); /* mark key */
|
|
|
|
if (!hasclears && iscleared(gval(n))) /* is there a white value? */
|
|
|
|
if (!hasclears && iscleared(g, gval(n))) /* is there a white value? */
|
|
|
|
hasclears = 1; /* table will have to be cleared */
|
|
|
|
hasclears = 1; /* table will have to be cleared */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -388,7 +402,7 @@ static int traverseephemeron (global_State *g, Table *h) {
|
|
|
|
checkdeadkey(n);
|
|
|
|
checkdeadkey(n);
|
|
|
|
if (ttisnil(gval(n))) /* entry is empty? */
|
|
|
|
if (ttisnil(gval(n))) /* entry is empty? */
|
|
|
|
removeentry(n); /* remove it */
|
|
|
|
removeentry(n); /* remove it */
|
|
|
|
else if (iscleared(gkey(n))) { /* key is not marked (yet)? */
|
|
|
|
else if (iscleared(g, gkey(n))) { /* key is not marked (yet)? */
|
|
|
|
hasclears = 1; /* table must be cleared */
|
|
|
|
hasclears = 1; /* table must be cleared */
|
|
|
|
if (valiswhite(gval(n))) /* value not marked yet? */
|
|
|
|
if (valiswhite(gval(n))) /* value not marked yet? */
|
|
|
|
prop = 1; /* must propagate again */
|
|
|
|
prop = 1; /* must propagate again */
|
|
|
@ -426,30 +440,26 @@ static void traversestrongtable (global_State *g, Table *h) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int traversetable (global_State *g, Table *h) {
|
|
|
|
static lu_mem traversetable (global_State *g, Table *h) {
|
|
|
|
|
|
|
|
const char *weakkey, *weakvalue;
|
|
|
|
const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
|
|
|
|
const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
|
|
|
|
markobject(g, h->metatable);
|
|
|
|
markobject(g, h->metatable);
|
|
|
|
if (mode && ttisstring(mode)) { /* is there a weak mode? */
|
|
|
|
if (mode && ttisstring(mode) && /* is there a weak mode? */
|
|
|
|
int weakkey = (strchr(svalue(mode), 'k') != NULL);
|
|
|
|
((weakkey = strchr(svalue(mode), 'k')),
|
|
|
|
int weakvalue = (strchr(svalue(mode), 'v') != NULL);
|
|
|
|
(weakvalue = strchr(svalue(mode), 'v')),
|
|
|
|
if (weakkey || weakvalue) { /* is really weak? */
|
|
|
|
(weakkey || weakvalue))) { /* is really weak? */
|
|
|
|
black2gray(obj2gco(h)); /* keep table gray */
|
|
|
|
black2gray(obj2gco(h)); /* keep table gray */
|
|
|
|
if (!weakkey) { /* strong keys? */
|
|
|
|
if (!weakkey) /* strong keys? */
|
|
|
|
traverseweakvalue(g, h);
|
|
|
|
traverseweakvalue(g, h);
|
|
|
|
return TRAVCOST + sizenode(h);
|
|
|
|
else if (!weakvalue) /* strong values? */
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (!weakvalue) { /* strong values? */
|
|
|
|
|
|
|
|
traverseephemeron(g, h);
|
|
|
|
traverseephemeron(g, h);
|
|
|
|
return TRAVCOST + h->sizearray + sizenode(h);
|
|
|
|
else /* all weak */
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
linktable(h, &g->allweak); /* nothing to traverse now */
|
|
|
|
linktable(h, &g->allweak); /* nothing to traverse now */
|
|
|
|
return TRAVCOST;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* else go through */
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else /* not weak */
|
|
|
|
traversestrongtable(g, h);
|
|
|
|
traversestrongtable(g, h);
|
|
|
|
return TRAVCOST + h->sizearray + (2 * sizenode(h));
|
|
|
|
return sizeof(Table) + sizeof(TValue) * h->sizearray +
|
|
|
|
|
|
|
|
sizeof(Node) * sizenode(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -457,86 +467,101 @@ static int traverseproto (global_State *g, Proto *f) {
|
|
|
|
int i;
|
|
|
|
int i;
|
|
|
|
if (f->cache && iswhite(obj2gco(f->cache)))
|
|
|
|
if (f->cache && iswhite(obj2gco(f->cache)))
|
|
|
|
f->cache = NULL; /* allow cache to be collected */
|
|
|
|
f->cache = NULL; /* allow cache to be collected */
|
|
|
|
stringmark(f->source);
|
|
|
|
markobject(g, f->source);
|
|
|
|
for (i = 0; i < f->sizek; i++) /* mark literals */
|
|
|
|
for (i = 0; i < f->sizek; i++) /* mark literals */
|
|
|
|
markvalue(g, &f->k[i]);
|
|
|
|
markvalue(g, &f->k[i]);
|
|
|
|
for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */
|
|
|
|
for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */
|
|
|
|
stringmark(f->upvalues[i].name);
|
|
|
|
markobject(g, f->upvalues[i].name);
|
|
|
|
for (i = 0; i < f->sizep; i++) /* mark nested protos */
|
|
|
|
for (i = 0; i < f->sizep; i++) /* mark nested protos */
|
|
|
|
markobject(g, f->p[i]);
|
|
|
|
markobject(g, f->p[i]);
|
|
|
|
for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
|
|
|
|
for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
|
|
|
|
stringmark(f->locvars[i].varname);
|
|
|
|
markobject(g, f->locvars[i].varname);
|
|
|
|
return TRAVCOST + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars;
|
|
|
|
return sizeof(Proto) + sizeof(Instruction) * f->sizecode +
|
|
|
|
|
|
|
|
sizeof(Proto *) * f->sizep +
|
|
|
|
|
|
|
|
sizeof(TValue) * f->sizek +
|
|
|
|
|
|
|
|
sizeof(int) * f->sizelineinfo +
|
|
|
|
|
|
|
|
sizeof(LocVar) * f->sizelocvars +
|
|
|
|
|
|
|
|
sizeof(Upvaldesc) * f->sizeupvalues;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int traverseclosure (global_State *g, Closure *cl) {
|
|
|
|
static lu_mem traverseCclosure (global_State *g, CClosure *cl) {
|
|
|
|
if (cl->c.isC) {
|
|
|
|
|
|
|
|
int i;
|
|
|
|
int i;
|
|
|
|
for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
|
|
|
|
for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
|
|
|
|
markvalue(g, &cl->c.upvalue[i]);
|
|
|
|
markvalue(g, &cl->upvalue[i]);
|
|
|
|
|
|
|
|
return sizeCclosure(cl->nupvalues);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
|
|
|
|
static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
|
|
|
|
int i;
|
|
|
|
int i;
|
|
|
|
lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
|
|
|
|
markobject(g, cl->p); /* mark its prototype */
|
|
|
|
markobject(g, cl->l.p); /* mark its prototype */
|
|
|
|
for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
|
|
|
|
for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */
|
|
|
|
markobject(g, cl->upvals[i]);
|
|
|
|
markobject(g, cl->l.upvals[i]);
|
|
|
|
return sizeLclosure(cl->nupvalues);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRAVCOST + cl->c.nupvalues;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int traversestack (global_State *g, lua_State *L) {
|
|
|
|
static lu_mem traversestack (global_State *g, lua_State *th) {
|
|
|
|
StkId o = L->stack;
|
|
|
|
StkId o = th->stack;
|
|
|
|
if (o == NULL)
|
|
|
|
if (o == NULL)
|
|
|
|
return 1; /* stack not completely built yet */
|
|
|
|
return 1; /* stack not completely built yet */
|
|
|
|
for (; o < L->top; o++)
|
|
|
|
for (; o < th->top; o++)
|
|
|
|
markvalue(g, o);
|
|
|
|
markvalue(g, o);
|
|
|
|
if (g->gcstate == GCSatomic) { /* final traversal? */
|
|
|
|
if (g->gcstate == GCSatomic) { /* final traversal? */
|
|
|
|
StkId lim = L->stack + L->stacksize; /* real end of stack */
|
|
|
|
StkId lim = th->stack + th->stacksize; /* real end of stack */
|
|
|
|
for (; o < lim; o++) /* clear not-marked stack slice */
|
|
|
|
for (; o < lim; o++) /* clear not-marked stack slice */
|
|
|
|
setnilvalue(o);
|
|
|
|
setnilvalue(o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRAVCOST + cast_int(o - L->stack);
|
|
|
|
return sizeof(lua_State) + sizeof(TValue) * th->stacksize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
** traverse one gray object, turning it to black (except for threads,
|
|
|
|
** traverse one gray object, turning it to black (except for threads,
|
|
|
|
** which are always gray).
|
|
|
|
** which are always gray).
|
|
|
|
** Returns number of values traversed.
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static int propagatemark (global_State *g) {
|
|
|
|
static void propagatemark (global_State *g) {
|
|
|
|
|
|
|
|
lu_mem size;
|
|
|
|
GCObject *o = g->gray;
|
|
|
|
GCObject *o = g->gray;
|
|
|
|
lua_assert(isgray(o));
|
|
|
|
lua_assert(isgray(o));
|
|
|
|
gray2black(o);
|
|
|
|
gray2black(o);
|
|
|
|
switch (gch(o)->tt) {
|
|
|
|
switch (gch(o)->tt) {
|
|
|
|
case LUA_TTABLE: {
|
|
|
|
case LUA_TTABLE: {
|
|
|
|
Table *h = gco2t(o);
|
|
|
|
Table *h = gco2t(o);
|
|
|
|
g->gray = h->gclist;
|
|
|
|
g->gray = h->gclist; /* remove from 'gray' list */
|
|
|
|
return traversetable(g, h);
|
|
|
|
size = traversetable(g, h);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case LUA_TFUNCTION: {
|
|
|
|
case LUA_TLCL: {
|
|
|
|
Closure *cl = gco2cl(o);
|
|
|
|
LClosure *cl = gco2lcl(o);
|
|
|
|
g->gray = cl->c.gclist;
|
|
|
|
g->gray = cl->gclist; /* remove from 'gray' list */
|
|
|
|
return traverseclosure(g, cl);
|
|
|
|
size = traverseLclosure(g, cl);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case LUA_TCCL: {
|
|
|
|
|
|
|
|
CClosure *cl = gco2ccl(o);
|
|
|
|
|
|
|
|
g->gray = cl->gclist; /* remove from 'gray' list */
|
|
|
|
|
|
|
|
size = traverseCclosure(g, cl);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case LUA_TTHREAD: {
|
|
|
|
case LUA_TTHREAD: {
|
|
|
|
lua_State *th = gco2th(o);
|
|
|
|
lua_State *th = gco2th(o);
|
|
|
|
g->gray = th->gclist;
|
|
|
|
g->gray = th->gclist; /* remove from 'gray' list */
|
|
|
|
th->gclist = g->grayagain;
|
|
|
|
th->gclist = g->grayagain;
|
|
|
|
g->grayagain = o;
|
|
|
|
g->grayagain = o; /* insert into 'grayagain' list */
|
|
|
|
black2gray(o);
|
|
|
|
black2gray(o);
|
|
|
|
return traversestack(g, th);
|
|
|
|
size = traversestack(g, th);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case LUA_TPROTO: {
|
|
|
|
case LUA_TPROTO: {
|
|
|
|
Proto *p = gco2p(o);
|
|
|
|
Proto *p = gco2p(o);
|
|
|
|
g->gray = p->gclist;
|
|
|
|
g->gray = p->gclist; /* remove from 'gray' list */
|
|
|
|
return traverseproto(g, p);
|
|
|
|
size = traverseproto(g, p);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default: lua_assert(0); return 0;
|
|
|
|
default: lua_assert(0); return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g->GCmemtrav += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -599,12 +624,12 @@ static void convergeephemerons (global_State *g) {
|
|
|
|
** clear entries with unmarked keys from all weaktables in list 'l' up
|
|
|
|
** clear entries with unmarked keys from all weaktables in list 'l' up
|
|
|
|
** to element 'f'
|
|
|
|
** to element 'f'
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static void clearkeys (GCObject *l, GCObject *f) {
|
|
|
|
static void clearkeys (global_State *g, GCObject *l, GCObject *f) {
|
|
|
|
for (; l != f; l = gco2t(l)->gclist) {
|
|
|
|
for (; l != f; l = gco2t(l)->gclist) {
|
|
|
|
Table *h = gco2t(l);
|
|
|
|
Table *h = gco2t(l);
|
|
|
|
Node *n, *limit = gnodelast(h);
|
|
|
|
Node *n, *limit = gnodelast(h);
|
|
|
|
for (n = gnode(h, 0); n < limit; n++) {
|
|
|
|
for (n = gnode(h, 0); n < limit; n++) {
|
|
|
|
if (!ttisnil(gval(n)) && (iscleared(gkey(n)))) {
|
|
|
|
if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) {
|
|
|
|
setnilvalue(gval(n)); /* remove value ... */
|
|
|
|
setnilvalue(gval(n)); /* remove value ... */
|
|
|
|
removeentry(n); /* and remove entry from table */
|
|
|
|
removeentry(n); /* and remove entry from table */
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -617,18 +642,18 @@ static void clearkeys (GCObject *l, GCObject *f) {
|
|
|
|
** clear entries with unmarked values from all weaktables in list 'l' up
|
|
|
|
** clear entries with unmarked values from all weaktables in list 'l' up
|
|
|
|
** to element 'f'
|
|
|
|
** to element 'f'
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static void clearvalues (GCObject *l, GCObject *f) {
|
|
|
|
static void clearvalues (global_State *g, GCObject *l, GCObject *f) {
|
|
|
|
for (; l != f; l = gco2t(l)->gclist) {
|
|
|
|
for (; l != f; l = gco2t(l)->gclist) {
|
|
|
|
Table *h = gco2t(l);
|
|
|
|
Table *h = gco2t(l);
|
|
|
|
Node *n, *limit = gnodelast(h);
|
|
|
|
Node *n, *limit = gnodelast(h);
|
|
|
|
int i;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < h->sizearray; i++) {
|
|
|
|
for (i = 0; i < h->sizearray; i++) {
|
|
|
|
TValue *o = &h->array[i];
|
|
|
|
TValue *o = &h->array[i];
|
|
|
|
if (iscleared(o)) /* value was collected? */
|
|
|
|
if (iscleared(g, o)) /* value was collected? */
|
|
|
|
setnilvalue(o); /* remove value */
|
|
|
|
setnilvalue(o); /* remove value */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (n = gnode(h, 0); n < limit; n++) {
|
|
|
|
for (n = gnode(h, 0); n < limit; n++) {
|
|
|
|
if (!ttisnil(gval(n)) && iscleared(gval(n))) {
|
|
|
|
if (!ttisnil(gval(n)) && iscleared(g, gval(n))) {
|
|
|
|
setnilvalue(gval(n)); /* remove value ... */
|
|
|
|
setnilvalue(gval(n)); /* remove value ... */
|
|
|
|
removeentry(n); /* and remove entry from table */
|
|
|
|
removeentry(n); /* and remove entry from table */
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -640,13 +665,22 @@ static void clearvalues (GCObject *l, GCObject *f) {
|
|
|
|
static void freeobj (lua_State *L, GCObject *o) {
|
|
|
|
static void freeobj (lua_State *L, GCObject *o) {
|
|
|
|
switch (gch(o)->tt) {
|
|
|
|
switch (gch(o)->tt) {
|
|
|
|
case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
|
|
|
|
case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
|
|
|
|
case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break;
|
|
|
|
case LUA_TLCL: {
|
|
|
|
|
|
|
|
luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case LUA_TCCL: {
|
|
|
|
|
|
|
|
luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break;
|
|
|
|
case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break;
|
|
|
|
case LUA_TTABLE: luaH_free(L, gco2t(o)); break;
|
|
|
|
case LUA_TTABLE: luaH_free(L, gco2t(o)); break;
|
|
|
|
case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break;
|
|
|
|
case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break;
|
|
|
|
case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break;
|
|
|
|
case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break;
|
|
|
|
case LUA_TSTRING: {
|
|
|
|
case LUA_TSHRSTR:
|
|
|
|
G(L)->strt.nuse--;
|
|
|
|
G(L)->strt.nuse--;
|
|
|
|
|
|
|
|
/* go through */
|
|
|
|
|
|
|
|
case LUA_TLNGSTR: {
|
|
|
|
luaM_freemem(L, o, sizestring(gco2ts(o)));
|
|
|
|
luaM_freemem(L, o, sizestring(gco2ts(o)));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -689,7 +723,6 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
|
|
|
int ow = otherwhite(g);
|
|
|
|
int ow = otherwhite(g);
|
|
|
|
int toclear, toset; /* bits to clear and to set in all live objects */
|
|
|
|
int toclear, toset; /* bits to clear and to set in all live objects */
|
|
|
|
int tostop; /* stop sweep when this is true */
|
|
|
|
int tostop; /* stop sweep when this is true */
|
|
|
|
l_mem debt = g->GCdebt; /* current debt */
|
|
|
|
|
|
|
|
if (isgenerational(g)) { /* generational mode? */
|
|
|
|
if (isgenerational(g)) { /* generational mode? */
|
|
|
|
toclear = ~0; /* clear nothing */
|
|
|
|
toclear = ~0; /* clear nothing */
|
|
|
|
toset = bitmask(OLDBIT); /* set the old bit of all surviving objects */
|
|
|
|
toset = bitmask(OLDBIT); /* set the old bit of all surviving objects */
|
|
|
@ -708,19 +741,30 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
|
|
|
freeobj(L, curr); /* erase 'curr' */
|
|
|
|
freeobj(L, curr); /* erase 'curr' */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
|
|
|
|
if (testbits(marked, tostop))
|
|
|
|
|
|
|
|
return NULL; /* stop sweeping this list */
|
|
|
|
if (gch(curr)->tt == LUA_TTHREAD)
|
|
|
|
if (gch(curr)->tt == LUA_TTHREAD)
|
|
|
|
sweepthread(L, gco2th(curr)); /* sweep thread's upvalues */
|
|
|
|
sweepthread(L, gco2th(curr)); /* sweep thread's upvalues */
|
|
|
|
if (testbits(marked, tostop)) {
|
|
|
|
|
|
|
|
static GCObject *nullp = NULL;
|
|
|
|
|
|
|
|
p = &nullp; /* stop sweeping this list */
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update marks */
|
|
|
|
/* update marks */
|
|
|
|
gch(curr)->marked = cast_byte((marked & toclear) | toset);
|
|
|
|
gch(curr)->marked = cast_byte((marked & toclear) | toset);
|
|
|
|
p = &gch(curr)->next; /* go to next element */
|
|
|
|
p = &gch(curr)->next; /* go to next element */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
luaE_setdebt(g, debt); /* sweeping should not change debt */
|
|
|
|
return (*p == NULL) ? NULL : p;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
** sweep a list until a live object (or end of list)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) {
|
|
|
|
|
|
|
|
GCObject ** old = p;
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
p = sweeplist(L, p, 1);
|
|
|
|
|
|
|
|
} while (p == old);
|
|
|
|
|
|
|
|
if (n) *n += i;
|
|
|
|
return p;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -783,12 +827,14 @@ static void GCTM (lua_State *L, int propagateerrors) {
|
|
|
|
L->allowhook = oldah; /* restore hooks */
|
|
|
|
L->allowhook = oldah; /* restore hooks */
|
|
|
|
g->gcrunning = running; /* restore state */
|
|
|
|
g->gcrunning = running; /* restore state */
|
|
|
|
if (status != LUA_OK && propagateerrors) { /* error while running __gc? */
|
|
|
|
if (status != LUA_OK && propagateerrors) { /* error while running __gc? */
|
|
|
|
if (status == LUA_ERRRUN) { /* is there an error msg.? */
|
|
|
|
if (status == LUA_ERRRUN) { /* is there an error object? */
|
|
|
|
luaO_pushfstring(L, "error in __gc metamethod (%s)",
|
|
|
|
const char *msg = (ttisstring(L->top - 1))
|
|
|
|
lua_tostring(L, -1));
|
|
|
|
? svalue(L->top - 1)
|
|
|
|
|
|
|
|
: "no message";
|
|
|
|
|
|
|
|
luaO_pushfstring(L, "error in __gc metamethod (%s)", msg);
|
|
|
|
status = LUA_ERRGCMM; /* error in __gc metamethod */
|
|
|
|
status = LUA_ERRGCMM; /* error in __gc metamethod */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
luaD_throw(L, status); /* re-send error */
|
|
|
|
luaD_throw(L, status); /* re-throw error */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -834,11 +880,20 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
|
|
|
|
return; /* nothing to be done */
|
|
|
|
return; /* nothing to be done */
|
|
|
|
else { /* move 'o' to 'finobj' list */
|
|
|
|
else { /* move 'o' to 'finobj' list */
|
|
|
|
GCObject **p;
|
|
|
|
GCObject **p;
|
|
|
|
for (p = &g->allgc; *p != o; p = &gch(*p)->next) ;
|
|
|
|
GCheader *ho = gch(o);
|
|
|
|
*p = gch(o)->next; /* remove 'o' from root list */
|
|
|
|
if (g->sweepgc == &ho->next) { /* avoid removing current sweep object */
|
|
|
|
gch(o)->next = g->finobj; /* link it in list 'finobj' */
|
|
|
|
lua_assert(issweepphase(g));
|
|
|
|
|
|
|
|
g->sweepgc = sweeptolive(L, g->sweepgc, NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* search for pointer pointing to 'o' */
|
|
|
|
|
|
|
|
for (p = &g->allgc; *p != o; p = &gch(*p)->next) { /* empty */ }
|
|
|
|
|
|
|
|
*p = ho->next; /* remove 'o' from root list */
|
|
|
|
|
|
|
|
ho->next = g->finobj; /* link it in list 'finobj' */
|
|
|
|
g->finobj = o;
|
|
|
|
g->finobj = o;
|
|
|
|
l_setbit(gch(o)->marked, SEPARATED); /* mark it as such */
|
|
|
|
l_setbit(ho->marked, SEPARATED); /* mark it as such */
|
|
|
|
|
|
|
|
if (!keepinvariant(g)) /* not keeping invariant? */
|
|
|
|
|
|
|
|
makewhite(g, o); /* "sweep" object */
|
|
|
|
|
|
|
|
else
|
|
|
|
resetoldbit(o); /* see MOVE OLD rule */
|
|
|
|
resetoldbit(o); /* see MOVE OLD rule */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -856,6 +911,28 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
|
|
|
|
#define sweepphases \
|
|
|
|
#define sweepphases \
|
|
|
|
(bitmask(GCSsweepstring) | bitmask(GCSsweepudata) | bitmask(GCSsweep))
|
|
|
|
(bitmask(GCSsweepstring) | bitmask(GCSsweepudata) | bitmask(GCSsweep))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
** enter first sweep phase (strings) and prepare pointers for other
|
|
|
|
|
|
|
|
** sweep phases. The calls to 'sweeptolive' make pointers point to an
|
|
|
|
|
|
|
|
** object inside 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 sweep.
|
|
|
|
|
|
|
|
** Returns how many objects it sweeped.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int entersweep (lua_State *L) {
|
|
|
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
g->gcstate = GCSsweepstring;
|
|
|
|
|
|
|
|
lua_assert(g->sweepgc == NULL && g->sweepfin == NULL);
|
|
|
|
|
|
|
|
/* prepare to sweep strings, finalizable objects, and regular objects */
|
|
|
|
|
|
|
|
g->sweepstrgc = 0;
|
|
|
|
|
|
|
|
g->sweepfin = sweeptolive(L, &g->finobj, &n);
|
|
|
|
|
|
|
|
g->sweepgc = sweeptolive(L, &g->allgc, &n);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
** change GC mode
|
|
|
|
** change GC mode
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -865,15 +942,14 @@ void luaC_changemode (lua_State *L, int mode) {
|
|
|
|
if (mode == KGC_GEN) { /* change to generational mode */
|
|
|
|
if (mode == KGC_GEN) { /* change to generational mode */
|
|
|
|
/* make sure gray lists are consistent */
|
|
|
|
/* make sure gray lists are consistent */
|
|
|
|
luaC_runtilstate(L, bitmask(GCSpropagate));
|
|
|
|
luaC_runtilstate(L, bitmask(GCSpropagate));
|
|
|
|
g->lastmajormem = gettotalbytes(g);
|
|
|
|
g->GCestimate = gettotalbytes(g);
|
|
|
|
g->gckind = KGC_GEN;
|
|
|
|
g->gckind = KGC_GEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { /* change to incremental mode */
|
|
|
|
else { /* change to incremental mode */
|
|
|
|
/* sweep all objects to turn them back to white
|
|
|
|
/* sweep all objects to turn them back to white
|
|
|
|
(as white has not changed, nothing extra will be collected) */
|
|
|
|
(as white has not changed, nothing extra will be collected) */
|
|
|
|
g->sweepstrgc = 0;
|
|
|
|
|
|
|
|
g->gcstate = GCSsweepstring;
|
|
|
|
|
|
|
|
g->gckind = KGC_NORMAL;
|
|
|
|
g->gckind = KGC_NORMAL;
|
|
|
|
|
|
|
|
entersweep(L);
|
|
|
|
luaC_runtilstate(L, ~sweepphases);
|
|
|
|
luaC_runtilstate(L, ~sweepphases);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -907,8 +983,9 @@ void luaC_freeallobjects (lua_State *L) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void atomic (lua_State *L) {
|
|
|
|
static l_mem atomic (lua_State *L) {
|
|
|
|
global_State *g = G(L);
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
|
|
|
l_mem work = -g->GCmemtrav; /* start counting work */
|
|
|
|
GCObject *origweak, *origall;
|
|
|
|
GCObject *origweak, *origall;
|
|
|
|
lua_assert(!iswhite(obj2gco(g->mainthread)));
|
|
|
|
lua_assert(!iswhite(obj2gco(g->mainthread)));
|
|
|
|
markobject(g, L); /* mark running thread */
|
|
|
|
markobject(g, L); /* mark running thread */
|
|
|
@ -917,77 +994,87 @@ static void atomic (lua_State *L) {
|
|
|
|
markmt(g); /* mark basic metatables */
|
|
|
|
markmt(g); /* mark basic metatables */
|
|
|
|
/* remark occasional upvalues of (maybe) dead threads */
|
|
|
|
/* remark occasional upvalues of (maybe) dead threads */
|
|
|
|
remarkupvals(g);
|
|
|
|
remarkupvals(g);
|
|
|
|
|
|
|
|
propagateall(g); /* propagate changes */
|
|
|
|
|
|
|
|
work += g->GCmemtrav; /* stop counting (do not (re)count grays) */
|
|
|
|
/* traverse objects caught by write barrier and by 'remarkupvals' */
|
|
|
|
/* traverse objects caught by write barrier and by 'remarkupvals' */
|
|
|
|
retraversegrays(g);
|
|
|
|
retraversegrays(g);
|
|
|
|
|
|
|
|
work -= g->GCmemtrav; /* restart counting */
|
|
|
|
convergeephemerons(g);
|
|
|
|
convergeephemerons(g);
|
|
|
|
/* at this point, all strongly accessible objects are marked. */
|
|
|
|
/* at this point, all strongly accessible objects are marked. */
|
|
|
|
/* clear values from weak tables, before checking finalizers */
|
|
|
|
/* clear values from weak tables, before checking finalizers */
|
|
|
|
clearvalues(g->weak, NULL);
|
|
|
|
clearvalues(g, g->weak, NULL);
|
|
|
|
clearvalues(g->allweak, NULL);
|
|
|
|
clearvalues(g, g->allweak, NULL);
|
|
|
|
origweak = g->weak; origall = g->allweak;
|
|
|
|
origweak = g->weak; origall = g->allweak;
|
|
|
|
|
|
|
|
work += g->GCmemtrav; /* stop counting (objects being finalized) */
|
|
|
|
separatetobefnz(L, 0); /* separate objects to be finalized */
|
|
|
|
separatetobefnz(L, 0); /* separate objects to be finalized */
|
|
|
|
markbeingfnz(g); /* mark userdata that will be finalized */
|
|
|
|
markbeingfnz(g); /* mark objects that will be finalized */
|
|
|
|
propagateall(g); /* remark, to propagate `preserveness' */
|
|
|
|
propagateall(g); /* remark, to propagate `preserveness' */
|
|
|
|
|
|
|
|
work -= g->GCmemtrav; /* restart counting */
|
|
|
|
convergeephemerons(g);
|
|
|
|
convergeephemerons(g);
|
|
|
|
/* at this point, all resurrected objects are marked. */
|
|
|
|
/* at this point, all resurrected objects are marked. */
|
|
|
|
/* remove dead objects from weak tables */
|
|
|
|
/* remove dead objects from weak tables */
|
|
|
|
clearkeys(g->ephemeron, NULL); /* clear keys from all ephemeron tables */
|
|
|
|
clearkeys(g, g->ephemeron, NULL); /* clear keys from all ephemeron tables */
|
|
|
|
clearkeys(g->allweak, NULL); /* clear keys from all allweak tables */
|
|
|
|
clearkeys(g, g->allweak, NULL); /* clear keys from all allweak tables */
|
|
|
|
/* clear values from resurrected weak tables */
|
|
|
|
/* clear values from resurrected weak tables */
|
|
|
|
clearvalues(g->weak, origweak);
|
|
|
|
clearvalues(g, g->weak, origweak);
|
|
|
|
clearvalues(g->allweak, origall);
|
|
|
|
clearvalues(g, g->allweak, origall);
|
|
|
|
g->sweepstrgc = 0; /* prepare to sweep strings */
|
|
|
|
|
|
|
|
g->gcstate = GCSsweepstring;
|
|
|
|
|
|
|
|
g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
|
|
|
|
g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
|
|
|
|
/*lua_checkmemory(L);*/
|
|
|
|
work += g->GCmemtrav; /* complete counting */
|
|
|
|
|
|
|
|
return work; /* estimate of memory marked by 'atomic' */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static l_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) {
|
|
|
|
case GCSpause: {
|
|
|
|
case GCSpause: {
|
|
|
|
|
|
|
|
g->GCmemtrav = 0; /* start to count memory traversed */
|
|
|
|
if (!isgenerational(g))
|
|
|
|
if (!isgenerational(g))
|
|
|
|
markroot(g); /* start a new collection */
|
|
|
|
markroot(g); /* start a new collection */
|
|
|
|
/* in any case, root must be marked */
|
|
|
|
/* in any case, root must be marked at this point */
|
|
|
|
lua_assert(!iswhite(obj2gco(g->mainthread))
|
|
|
|
lua_assert(!iswhite(obj2gco(g->mainthread))
|
|
|
|
&& !iswhite(gcvalue(&g->l_registry)));
|
|
|
|
&& !iswhite(gcvalue(&g->l_registry)));
|
|
|
|
g->gcstate = GCSpropagate;
|
|
|
|
g->gcstate = GCSpropagate;
|
|
|
|
return GCROOTCOST;
|
|
|
|
return g->GCmemtrav;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case GCSpropagate: {
|
|
|
|
case GCSpropagate: {
|
|
|
|
if (g->gray)
|
|
|
|
if (g->gray) {
|
|
|
|
return propagatemark(g);
|
|
|
|
lu_mem oldtrav = g->GCmemtrav;
|
|
|
|
|
|
|
|
propagatemark(g);
|
|
|
|
|
|
|
|
return g->GCmemtrav - oldtrav; /* memory traversed in this step */
|
|
|
|
|
|
|
|
}
|
|
|
|
else { /* no more `gray' objects */
|
|
|
|
else { /* no more `gray' objects */
|
|
|
|
|
|
|
|
lu_mem work;
|
|
|
|
|
|
|
|
int sw;
|
|
|
|
g->gcstate = GCSatomic; /* finish mark phase */
|
|
|
|
g->gcstate = GCSatomic; /* finish mark phase */
|
|
|
|
atomic(L);
|
|
|
|
g->GCestimate = g->GCmemtrav; /* save what was counted */;
|
|
|
|
return GCATOMICCOST;
|
|
|
|
work = atomic(L); /* add what was traversed by 'atomic' */
|
|
|
|
|
|
|
|
g->GCestimate += work; /* estimate of total memory traversed */
|
|
|
|
|
|
|
|
sw = entersweep(L);
|
|
|
|
|
|
|
|
return work + sw * GCSWEEPCOST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case GCSsweepstring: {
|
|
|
|
case GCSsweepstring: {
|
|
|
|
if (g->sweepstrgc < g->strt.size) {
|
|
|
|
int i;
|
|
|
|
sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]);
|
|
|
|
for (i = 0; i < GCSWEEPMAX && g->sweepstrgc + i < g->strt.size; i++)
|
|
|
|
return GCSWEEPCOST;
|
|
|
|
sweepwholelist(L, &g->strt.hash[g->sweepstrgc + i]);
|
|
|
|
}
|
|
|
|
g->sweepstrgc += i;
|
|
|
|
else { /* no more strings to sweep */
|
|
|
|
if (g->sweepstrgc >= g->strt.size) /* no more strings to sweep? */
|
|
|
|
g->sweepgc = &g->finobj; /* prepare to sweep finalizable objects */
|
|
|
|
|
|
|
|
g->gcstate = GCSsweepudata;
|
|
|
|
g->gcstate = GCSsweepudata;
|
|
|
|
return 0;
|
|
|
|
return i * GCSWEEPCOST;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case GCSsweepudata: {
|
|
|
|
case GCSsweepudata: {
|
|
|
|
if (*g->sweepgc) {
|
|
|
|
if (g->sweepfin) {
|
|
|
|
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
|
|
|
|
g->sweepfin = sweeplist(L, g->sweepfin, GCSWEEPMAX);
|
|
|
|
return GCSWEEPMAX*GCSWEEPCOST;
|
|
|
|
return GCSWEEPMAX*GCSWEEPCOST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
g->sweepgc = &g->allgc; /* go to next phase */
|
|
|
|
|
|
|
|
g->gcstate = GCSsweep;
|
|
|
|
g->gcstate = GCSsweep;
|
|
|
|
return GCSWEEPCOST;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case GCSsweep: {
|
|
|
|
case GCSsweep: {
|
|
|
|
if (*g->sweepgc) {
|
|
|
|
if (g->sweepgc) {
|
|
|
|
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
|
|
|
|
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
|
|
|
|
return GCSWEEPMAX*GCSWEEPCOST;
|
|
|
|
return GCSWEEPMAX*GCSWEEPCOST;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1018,43 +1105,52 @@ void luaC_runtilstate (lua_State *L, int statesmask) {
|
|
|
|
|
|
|
|
|
|
|
|
static void generationalcollection (lua_State *L) {
|
|
|
|
static void generationalcollection (lua_State *L) {
|
|
|
|
global_State *g = G(L);
|
|
|
|
global_State *g = G(L);
|
|
|
|
if (g->lastmajormem == 0) { /* signal for another major collection? */
|
|
|
|
if (g->GCestimate == 0) { /* signal for another major collection? */
|
|
|
|
luaC_fullgc(L, 0); /* perform a full regular collection */
|
|
|
|
luaC_fullgc(L, 0); /* perform a full regular collection */
|
|
|
|
g->lastmajormem = gettotalbytes(g); /* update control */
|
|
|
|
g->GCestimate = gettotalbytes(g); /* update control */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
|
|
|
|
lu_mem estimate = g->GCestimate;
|
|
|
|
luaC_runtilstate(L, ~bitmask(GCSpause)); /* run complete cycle */
|
|
|
|
luaC_runtilstate(L, ~bitmask(GCSpause)); /* run complete cycle */
|
|
|
|
luaC_runtilstate(L, bitmask(GCSpause));
|
|
|
|
luaC_runtilstate(L, bitmask(GCSpause));
|
|
|
|
if (gettotalbytes(g) > g->lastmajormem/100 * g->gcmajorinc)
|
|
|
|
if (gettotalbytes(g) > (estimate / 100) * g->gcmajorinc)
|
|
|
|
g->lastmajormem = 0; /* signal for a major collection */
|
|
|
|
g->GCestimate = 0; /* signal for a major collection */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
luaE_setdebt(g, stddebt(g));
|
|
|
|
luaE_setdebt(g, stddebt(g));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void step (lua_State *L) {
|
|
|
|
static void incstep (lua_State *L) {
|
|
|
|
global_State *g = G(L);
|
|
|
|
global_State *g = G(L);
|
|
|
|
l_mem lim = g->gcstepmul; /* how much to work */
|
|
|
|
l_mem debt = g->GCdebt;
|
|
|
|
|
|
|
|
int stepmul = g->gcstepmul;
|
|
|
|
|
|
|
|
if (stepmul < 40) stepmul = 40; /* avoid ridiculous low values */
|
|
|
|
|
|
|
|
/* convert debt from Kb to 'work units' (avoid zero debt and overflows) */
|
|
|
|
|
|
|
|
debt = (debt / STEPMULADJ) + 1;
|
|
|
|
|
|
|
|
debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM;
|
|
|
|
do { /* always perform at least one single step */
|
|
|
|
do { /* always perform at least one single step */
|
|
|
|
lim -= singlestep(L);
|
|
|
|
lu_mem work = singlestep(L); /* do some work */
|
|
|
|
} while (lim > 0 && g->gcstate != GCSpause);
|
|
|
|
debt -= work;
|
|
|
|
if (g->gcstate != GCSpause)
|
|
|
|
} while (debt > -GCSTEPSIZE && g->gcstate != GCSpause);
|
|
|
|
luaE_setdebt(g, g->GCdebt - GCSTEPSIZE);
|
|
|
|
if (g->gcstate == GCSpause)
|
|
|
|
|
|
|
|
debt = stddebtest(g, g->GCestimate); /* pause until next cycle */
|
|
|
|
else
|
|
|
|
else
|
|
|
|
luaE_setdebt(g, stddebt(g));
|
|
|
|
debt = (debt / stepmul) * STEPMULADJ; /* convert 'work units' to Kb */
|
|
|
|
|
|
|
|
luaE_setdebt(g, debt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
** performs a basic GC step even if the collector is stopped
|
|
|
|
** performs a basic GC step
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void luaC_forcestep (lua_State *L) {
|
|
|
|
void luaC_forcestep (lua_State *L) {
|
|
|
|
global_State *g = G(L);
|
|
|
|
global_State *g = G(L);
|
|
|
|
int i;
|
|
|
|
int i;
|
|
|
|
if (isgenerational(g)) generationalcollection(L);
|
|
|
|
if (isgenerational(g)) generationalcollection(L);
|
|
|
|
else step(L);
|
|
|
|
else incstep(L);
|
|
|
|
for (i = 0; i < GCFINALIZENUM && g->tobefnz; i++)
|
|
|
|
/* run a few finalizers (or all of them at the end of a collect cycle) */
|
|
|
|
GCTM(L, 1); /* Call a few pending finalizers */
|
|
|
|
for (i = 0; g->tobefnz && (i < GCFINALIZENUM || g->gcstate == GCSpause); i++)
|
|
|
|
|
|
|
|
GCTM(L, 1); /* call one finalizer */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -1062,10 +1158,13 @@ void luaC_forcestep (lua_State *L) {
|
|
|
|
** performs a basic GC step only if collector is running
|
|
|
|
** performs a basic GC step only if collector is running
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void luaC_step (lua_State *L) {
|
|
|
|
void luaC_step (lua_State *L) {
|
|
|
|
if (G(L)->gcrunning) luaC_forcestep(L);
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
|
|
|
if (g->gcrunning) luaC_forcestep(L);
|
|
|
|
|
|
|
|
else luaE_setdebt(g, -GCSTEPSIZE); /* avoid being called too often */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
** performs a full GC cycle; if "isemergency", does not call
|
|
|
|
** performs a full GC cycle; if "isemergency", does not call
|
|
|
|
** finalizers (which could change stack positions)
|
|
|
|
** finalizers (which could change stack positions)
|
|
|
@ -1073,16 +1172,19 @@ void luaC_step (lua_State *L) {
|
|
|
|
void luaC_fullgc (lua_State *L, int isemergency) {
|
|
|
|
void luaC_fullgc (lua_State *L, int isemergency) {
|
|
|
|
global_State *g = G(L);
|
|
|
|
global_State *g = G(L);
|
|
|
|
int origkind = g->gckind;
|
|
|
|
int origkind = g->gckind;
|
|
|
|
|
|
|
|
int someblack = keepinvariant(g);
|
|
|
|
lua_assert(origkind != KGC_EMERGENCY);
|
|
|
|
lua_assert(origkind != KGC_EMERGENCY);
|
|
|
|
if (!isemergency) /* do not run finalizers during emergency GC */
|
|
|
|
if (isemergency) /* do not run finalizers during emergency GC */
|
|
|
|
|
|
|
|
g->gckind = KGC_EMERGENCY;
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
g->gckind = KGC_NORMAL;
|
|
|
|
callallpendingfinalizers(L, 1);
|
|
|
|
callallpendingfinalizers(L, 1);
|
|
|
|
if (keepinvariant(g)) { /* marking phase? */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (someblack) { /* may there be some black objects? */
|
|
|
|
/* must sweep all objects to turn them back to white
|
|
|
|
/* must sweep all objects to turn them back to white
|
|
|
|
(as white has not changed, nothing will be collected) */
|
|
|
|
(as white has not changed, nothing will be collected) */
|
|
|
|
g->sweepstrgc = 0;
|
|
|
|
entersweep(L);
|
|
|
|
g->gcstate = GCSsweepstring;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g->gckind = isemergency ? KGC_EMERGENCY : KGC_NORMAL;
|
|
|
|
|
|
|
|
/* finish any pending sweep phase to start a new cycle */
|
|
|
|
/* finish any pending sweep phase to start a new cycle */
|
|
|
|
luaC_runtilstate(L, bitmask(GCSpause));
|
|
|
|
luaC_runtilstate(L, bitmask(GCSpause));
|
|
|
|
/* run entire collector */
|
|
|
|
/* run entire collector */
|
|
|
|