|
@@ -1,5 +1,5 @@
|
|
|
/*
|
|
|
-** $Id: lgc.c,v 1.146 2002/08/16 14:45:55 roberto Exp roberto $
|
|
|
+** $Id: lgc.c,v 1.147 2002/08/16 20:00:28 roberto Exp roberto $
|
|
|
** Garbage Collector
|
|
|
** See Copyright Notice in lua.h
|
|
|
*/
|
|
@@ -34,28 +34,24 @@ typedef struct GCState {
|
|
|
#define resetbit(x,b) ((x) &= cast(lu_byte, ~(1<<(b))))
|
|
|
#define testbit(x,b) ((x) & (1<<(b)))
|
|
|
|
|
|
+#define mark(x) setbit((x)->gch.marked, 0)
|
|
|
+#define unmark(x) resetbit((x)->gch.marked, 0)
|
|
|
+#define ismarked(x) ((x)->gch.marked & ((1<<4)|1))
|
|
|
|
|
|
#define strmark(s) setbit((s)->tsv.marked, 0)
|
|
|
#define strunmark(s) resetbit((s)->tsv.marked, 0)
|
|
|
|
|
|
|
|
|
+#define isfinalized(u) (!testbit((u)->uv.marked, 1))
|
|
|
+#define markfinalized(u) resetbit((u)->uv.marked, 1)
|
|
|
|
|
|
-/* mark tricks for userdata */
|
|
|
-#define isudmarked(u) testbit(u->uv.marked, 0)
|
|
|
-#define markud(u) setbit(u->uv.marked, 0)
|
|
|
-#define unmarkud(u) resetbit(u->uv.marked, 0)
|
|
|
|
|
|
-#define isfinalized(u) testbit(u->uv.marked, 1)
|
|
|
-#define markfinalized(u) setbit(u->uv.marked, 1)
|
|
|
+static void reallymarkobject (GCState *st, GCObject *o);
|
|
|
|
|
|
+#define markobject(st,o) \
|
|
|
+ if (iscollectable(o)) reallymarkobject(st,(o)->value.gc)
|
|
|
|
|
|
-#define ismarkable(o) (!((1 << ttype(o)) & \
|
|
|
- ((1 << LUA_TNIL) | (1 << LUA_TNUMBER) | \
|
|
|
- (1 << LUA_TBOOLEAN) | (1 << LUA_TLIGHTUSERDATA))))
|
|
|
-
|
|
|
-static void reallymarkobject (GCState *st, TObject *o);
|
|
|
-
|
|
|
-#define markobject(st,o) if (ismarkable(o)) reallymarkobject(st,o)
|
|
|
+#define marktable(st,t) reallymarkobject(st, cast(GCObject *, (t)))
|
|
|
|
|
|
|
|
|
static void markproto (Proto *f) {
|
|
@@ -76,63 +72,46 @@ static void markproto (Proto *f) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-static void marktable (GCState *st, Table *h) {
|
|
|
- if (!h->marked) {
|
|
|
- h->marked = 1;
|
|
|
- h->gclist = st->tmark; /* chain it for later traversal */
|
|
|
- st->tmark = h;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
|
|
|
static void markclosure (GCState *st, Closure *cl) {
|
|
|
- if (!cl->c.marked) {
|
|
|
- cl->c.marked = 1;
|
|
|
- if (cl->c.isC) {
|
|
|
- int i;
|
|
|
- for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
|
|
|
- markobject(st, &cl->c.upvalue[i]);
|
|
|
- }
|
|
|
- else {
|
|
|
- int i;
|
|
|
- lua_assert(cl->l.nupvalues == cl->l.p->nupvalues);
|
|
|
- marktable(st, hvalue(&cl->l.g));
|
|
|
- markproto(cl->l.p);
|
|
|
- for (i=0; i<cl->l.nupvalues; i++) { /* mark its upvalues */
|
|
|
- UpVal *u = cl->l.upvals[i];
|
|
|
- if (!u->marked) {
|
|
|
- markobject(st, &u->value);
|
|
|
- u->marked = 1;
|
|
|
- }
|
|
|
+ if (cl->c.isC) {
|
|
|
+ int i;
|
|
|
+ for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
|
|
|
+ markobject(st, &cl->c.upvalue[i]);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ int i;
|
|
|
+ lua_assert(cl->l.nupvalues == cl->l.p->nupvalues);
|
|
|
+ marktable(st, hvalue(&cl->l.g));
|
|
|
+ markproto(cl->l.p);
|
|
|
+ for (i=0; i<cl->l.nupvalues; i++) { /* mark its upvalues */
|
|
|
+ UpVal *u = cl->l.upvals[i];
|
|
|
+ if (!u->marked) {
|
|
|
+ markobject(st, &u->value);
|
|
|
+ u->marked = 1;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
-static void markudata (GCState *st, Udata *u) {
|
|
|
- markud(u);
|
|
|
- marktable(st, u->uv.metatable);
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-static void reallymarkobject (GCState *st, TObject *o) {
|
|
|
- switch (ttype(o)) {
|
|
|
- case LUA_TSTRING:
|
|
|
- strmark(tsvalue(o));
|
|
|
- break;
|
|
|
- case LUA_TUSERDATA:
|
|
|
- if (!isudmarked(uvalue(o)))
|
|
|
- markudata(st, uvalue(o));
|
|
|
+static void reallymarkobject (GCState *st, GCObject *o) {
|
|
|
+ if (ismarked(o)) return;
|
|
|
+ mark(o);
|
|
|
+ switch (o->gch.tt) {
|
|
|
+ case LUA_TFUNCTION: {
|
|
|
+ markclosure(st, &o->cl);
|
|
|
break;
|
|
|
- case LUA_TFUNCTION:
|
|
|
- markclosure(st, clvalue(o));
|
|
|
+ }
|
|
|
+ case LUA_TUSERDATA: {
|
|
|
+ marktable(st, (&o->u)->uv.metatable);
|
|
|
break;
|
|
|
+ }
|
|
|
case LUA_TTABLE: {
|
|
|
- marktable(st, hvalue(o));
|
|
|
+ (&o->h)->gclist = st->tmark; /* chain it for later traversal */
|
|
|
+ st->tmark = &o->h;
|
|
|
break;
|
|
|
}
|
|
|
- default: lua_assert(0); /* should not be called with other types */
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -177,22 +156,29 @@ static void traversestacks (GCState *st) {
|
|
|
|
|
|
|
|
|
static void marktmu (GCState *st) {
|
|
|
- Udata *u;
|
|
|
- for (u = G(st->L)->tmudata; u; u = u->uv.next)
|
|
|
- markudata(st, u);
|
|
|
+ GCObject *u;
|
|
|
+ for (u = G(st->L)->tmudata; u; u = u->uv.next) {
|
|
|
+ mark(u);
|
|
|
+ marktable(st, (&u->u)->uv.metatable);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
/* move `dead' udata that need finalization to list `tmudata' */
|
|
|
static void separateudata (lua_State *L) {
|
|
|
- Udata **p = &G(L)->rootudata;
|
|
|
- Udata *curr;
|
|
|
- Udata *collected = NULL; /* to collect udata with gc event */
|
|
|
- Udata **lastcollected = &collected;
|
|
|
+ GCObject **p = &G(L)->rootudata;
|
|
|
+ GCObject *curr;
|
|
|
+ GCObject *collected = NULL; /* to collect udata with gc event */
|
|
|
+ GCObject **lastcollected = &collected;
|
|
|
while ((curr = *p) != NULL) {
|
|
|
- if (isudmarked(curr) || isfinalized(curr) ||
|
|
|
- (fasttm(L, curr->uv.metatable, TM_GC) == NULL))
|
|
|
+ lua_assert(curr->gch.tt == LUA_TUSERDATA);
|
|
|
+ if (ismarked(curr) || isfinalized(&curr->u))
|
|
|
+ p = &curr->uv.next; /* don't bother with them */
|
|
|
+
|
|
|
+ else if (fasttm(L, (&curr->u)->uv.metatable, TM_GC) == NULL) {
|
|
|
+ markfinalized(&curr->u); /* don't need finalization */
|
|
|
p = &curr->uv.next;
|
|
|
+ }
|
|
|
else { /* must call its gc method */
|
|
|
*p = curr->uv.next;
|
|
|
curr->uv.next = NULL; /* link `curr' at the end of `collected' list */
|
|
@@ -208,7 +194,7 @@ static void separateudata (lua_State *L) {
|
|
|
|
|
|
static void removekey (Node *n) {
|
|
|
setnilvalue(val(n)); /* remove corresponding value ... */
|
|
|
- if (ismarkable(key(n)))
|
|
|
+ if (iscollectable(key(n)))
|
|
|
setttype(key(n), LUA_TNONE); /* dead key; remove it */
|
|
|
}
|
|
|
|
|
@@ -251,20 +237,10 @@ static void propagatemarks (GCState *st) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int ismarked (const TObject *o) {
|
|
|
- switch (ttype(o)) {
|
|
|
- case LUA_TUSERDATA:
|
|
|
- return isudmarked(uvalue(o));
|
|
|
- case LUA_TTABLE:
|
|
|
- return hvalue(o)->marked;
|
|
|
- case LUA_TFUNCTION:
|
|
|
- return clvalue(o)->c.marked;
|
|
|
- case LUA_TSTRING:
|
|
|
- strmark(tsvalue(o)); /* strings are `values', so are never weak */
|
|
|
- /* go through */
|
|
|
- default: /* number, nil, boolean, udataval */
|
|
|
- return 1;
|
|
|
- }
|
|
|
+static int valismarked (const TObject *o) {
|
|
|
+ if (ttisstring(o))
|
|
|
+ strmark(tsvalue(o)); /* strings are `values', so are never weak */
|
|
|
+ return !iscollectable(o) || testbit(o->value.gc->gch.marked, 0);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -279,7 +255,7 @@ static void cleartablekeys (GCState *st) {
|
|
|
int i = sizenode(h);
|
|
|
while (i--) {
|
|
|
Node *n = node(h, i);
|
|
|
- if (!ismarked(key(n))) /* key was collected? */
|
|
|
+ if (!valismarked(key(n))) /* key was collected? */
|
|
|
removekey(n); /* remove entry from table */
|
|
|
}
|
|
|
}
|
|
@@ -297,13 +273,13 @@ static void cleartablevalues (GCState *st) {
|
|
|
int i = sizearray(h);
|
|
|
while (i--) {
|
|
|
TObject *o = &h->array[i];
|
|
|
- if (!ismarked(o)) /* value was collected? */
|
|
|
+ if (!valismarked(o)) /* value was collected? */
|
|
|
setnilvalue(o); /* remove value */
|
|
|
}
|
|
|
i = sizenode(h);
|
|
|
while (i--) {
|
|
|
Node *n = node(h, i);
|
|
|
- if (!ismarked(val(n))) /* value was collected? */
|
|
|
+ if (!valismarked(val(n))) /* value was collected? */
|
|
|
removekey(n); /* remove entry from table */
|
|
|
}
|
|
|
}
|
|
@@ -311,103 +287,47 @@ static void cleartablevalues (GCState *st) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-static void sweepproto (lua_State *L) {
|
|
|
- Proto **p = &G(L)->rootproto;
|
|
|
- Proto *curr;
|
|
|
- while ((curr = *p) != NULL) {
|
|
|
- if (curr->marked) {
|
|
|
- curr->marked = 0;
|
|
|
- p = &curr->next;
|
|
|
- }
|
|
|
- else {
|
|
|
- *p = curr->next;
|
|
|
- luaF_freeproto(L, curr);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-static void sweepclosures (lua_State *L) {
|
|
|
- Closure **p = &G(L)->rootcl;
|
|
|
- Closure *curr;
|
|
|
- while ((curr = *p) != NULL) {
|
|
|
- if (curr->c.marked) {
|
|
|
- curr->c.marked = 0;
|
|
|
- p = &curr->c.next;
|
|
|
- }
|
|
|
- else {
|
|
|
- *p = curr->c.next;
|
|
|
- luaF_freeclosure(L, curr);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-static void sweepupval (lua_State *L) {
|
|
|
- UpVal **v = &G(L)->rootupval;
|
|
|
- UpVal *curr;
|
|
|
- while ((curr = *v) != NULL) {
|
|
|
- if (curr->marked) {
|
|
|
- curr->marked = 0; /* unmark */
|
|
|
- v = &curr->next; /* next */
|
|
|
- }
|
|
|
- else {
|
|
|
- *v = curr->next; /* next */
|
|
|
- luaM_freelem(L, curr);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-static void sweeptable (lua_State *L) {
|
|
|
- Table **p = &G(L)->roottable;
|
|
|
- Table *curr;
|
|
|
- while ((curr = *p) != NULL) {
|
|
|
- if (curr->marked) {
|
|
|
- curr->marked = 0;
|
|
|
- p = &curr->next;
|
|
|
+static void freeobj (lua_State *L, GCObject *o) {
|
|
|
+ switch (o->gch.tt) {
|
|
|
+ case LUA_TPROTO: luaF_freeproto(L, &o->p); break;
|
|
|
+ case LUA_TFUNCTION: luaF_freeclosure(L, &o->cl); break;
|
|
|
+ case LUA_TUPVAL: luaM_freelem(L, &o->uv); break;
|
|
|
+ case LUA_TTABLE: luaH_free(L, &o->h); break;
|
|
|
+ case LUA_TSTRING: {
|
|
|
+ luaM_free(L, o, sizestring((&o->ts)->tsv.len));
|
|
|
+ break;
|
|
|
}
|
|
|
- else {
|
|
|
- *p = curr->next;
|
|
|
- luaH_free(L, curr);
|
|
|
+ case LUA_TUSERDATA: {
|
|
|
+ luaM_free(L, o, sizeudata((&o->u)->uv.len));
|
|
|
+ break;
|
|
|
}
|
|
|
+ default: lua_assert(0);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-static void sweepudata (lua_State *L) {
|
|
|
- Udata **p = &G(L)->rootudata;
|
|
|
- Udata *curr;
|
|
|
+static int sweeplist (lua_State *L, GCObject **p, int limit) {
|
|
|
+ GCObject *curr;
|
|
|
+ int count = 0; /* number of collected items */
|
|
|
while ((curr = *p) != NULL) {
|
|
|
- if (isudmarked(curr)) {
|
|
|
- unmarkud(curr);
|
|
|
- p = &curr->uv.next;
|
|
|
+ if (curr->gch.marked > limit) {
|
|
|
+ unmark(curr);
|
|
|
+ p = &curr->gch.next;
|
|
|
}
|
|
|
else {
|
|
|
- *p = curr->uv.next;
|
|
|
- luaM_free(L, curr, sizeudata(curr->uv.len));
|
|
|
+ count++;
|
|
|
+ *p = curr->gch.next;
|
|
|
+ freeobj(L, curr);
|
|
|
}
|
|
|
}
|
|
|
+ return count;
|
|
|
}
|
|
|
|
|
|
|
|
|
static void sweepstrings (lua_State *L, int all) {
|
|
|
int i;
|
|
|
for (i=0; i<G(L)->strt.size; i++) { /* for each list */
|
|
|
- TString **p = &G(L)->strt.hash[i];
|
|
|
- TString *curr;
|
|
|
- while ((curr = *p) != NULL) {
|
|
|
- if (curr->tsv.marked && !all) { /* preserve? */
|
|
|
- strunmark(curr);
|
|
|
- p = &curr->tsv.nexthash;
|
|
|
- }
|
|
|
- else { /* collect */
|
|
|
- *p = curr->tsv.nexthash;
|
|
|
- G(L)->strt.nuse--;
|
|
|
- luaM_free(L, curr, sizestring(curr->tsv.len));
|
|
|
- }
|
|
|
- }
|
|
|
+ G(L)->strt.nuse -= sweeplist(L, &G(L)->strt.hash[i], all);
|
|
|
}
|
|
|
if (G(L)->strt.nuse < cast(ls_nstr, G(L)->strt.size/4) &&
|
|
|
G(L)->strt.size > MINSTRTABSIZE*2)
|
|
@@ -442,14 +362,14 @@ static void callGCTM (lua_State *L) {
|
|
|
setallowhook(L, 0); /* stop debug hooks during GC tag methods */
|
|
|
L->top++; /* reserve space to keep udata while runs its gc method */
|
|
|
while (G(L)->tmudata != NULL) {
|
|
|
- Udata *udata = G(L)->tmudata;
|
|
|
+ GCObject *udata = G(L)->tmudata;
|
|
|
G(L)->tmudata = udata->uv.next; /* remove udata from `tmudata' */
|
|
|
udata->uv.next = G(L)->rootudata; /* return it to `root' list */
|
|
|
G(L)->rootudata = udata;
|
|
|
- setuvalue(L->top - 1, udata); /* keep a reference to it */
|
|
|
- unmarkud(udata);
|
|
|
- markfinalized(udata);
|
|
|
- do1gcTM(L, udata);
|
|
|
+ setuvalue(L->top - 1, &udata->u); /* keep a reference to it */
|
|
|
+ unmark(udata);
|
|
|
+ markfinalized(&udata->u);
|
|
|
+ do1gcTM(L, &udata->u);
|
|
|
}
|
|
|
L->top--;
|
|
|
setallowhook(L, oldah); /* restore hooks */
|
|
@@ -463,12 +383,10 @@ void luaC_callallgcTM (lua_State *L) {
|
|
|
|
|
|
|
|
|
void luaC_sweep (lua_State *L, int all) {
|
|
|
- sweepudata(L);
|
|
|
+ if (all) all = 256; /* larger than any mark */
|
|
|
+ sweeplist(L, &G(L)->rootudata, all);
|
|
|
sweepstrings(L, all);
|
|
|
- sweeptable(L);
|
|
|
- sweepproto(L);
|
|
|
- sweepupval(L);
|
|
|
- sweepclosures(L);
|
|
|
+ sweeplist(L, &G(L)->rootgc, all);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -482,7 +400,8 @@ void luaC_collectgarbage (lua_State *L) {
|
|
|
cleartablevalues(&st);
|
|
|
separateudata(L); /* separate userdata to be preserved */
|
|
|
marktmu(&st); /* mark `preserved' userdata */
|
|
|
- propagatemarks(&st); /* remark */
|
|
|
+ propagatemarks(&st); /* remark, to propagate `preserveness' */
|
|
|
+ cleartablevalues(&st); /* again, for eventual weak preserved tables */
|
|
|
cleartablekeys(&st);
|
|
|
luaC_sweep(L, 0);
|
|
|
checkMbuffer(L);
|
|
@@ -490,3 +409,11 @@ void luaC_collectgarbage (lua_State *L) {
|
|
|
callGCTM(L);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+void luaC_link (lua_State *L, GCObject *o, int tt) {
|
|
|
+ o->gch.next = G(L)->rootgc;
|
|
|
+ G(L)->rootgc = o;
|
|
|
+ o->gch.marked = 0;
|
|
|
+ o->gch.tt = tt;
|
|
|
+}
|
|
|
+
|