1997-09-17 03:25:59 +08:00
|
|
|
/*
|
2001-06-14 02:51:20 +08:00
|
|
|
** $Id: lgc.c,v 1.103 2001/06/12 18:43:13 roberto Exp roberto $
|
1997-09-17 03:25:59 +08:00
|
|
|
** Garbage Collector
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2001-03-26 22:31:49 +08:00
|
|
|
#define LUA_PRIVATE
|
2000-06-12 21:52:05 +08:00
|
|
|
#include "lua.h"
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "ldo.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lgc.h"
|
1999-11-10 23:40:46 +08:00
|
|
|
#include "lmem.h"
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "lobject.h"
|
1997-11-20 01:29:23 +08:00
|
|
|
#include "lstate.h"
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltable.h"
|
|
|
|
#include "ltm.h"
|
|
|
|
|
|
|
|
|
2001-02-02 23:13:05 +08:00
|
|
|
|
2000-08-08 04:21:34 +08:00
|
|
|
typedef struct GCState {
|
|
|
|
Hash *tmark; /* list of marked tables to be visited */
|
|
|
|
Closure *cmark; /* list of marked closures to be visited */
|
|
|
|
} GCState;
|
1997-09-17 03:25:59 +08:00
|
|
|
|
2000-04-15 02:12:35 +08:00
|
|
|
|
2000-08-08 04:21:34 +08:00
|
|
|
|
1999-12-15 02:33:29 +08:00
|
|
|
/* mark a string; marks larger than 1 cannot be changed */
|
2000-08-08 04:21:34 +08:00
|
|
|
#define strmark(s) {if ((s)->marked == 0) (s)->marked = 1;}
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-08 04:21:34 +08:00
|
|
|
static void protomark (Proto *f) {
|
1999-10-05 01:51:04 +08:00
|
|
|
if (!f->marked) {
|
1997-09-17 03:25:59 +08:00
|
|
|
int i;
|
1999-10-05 01:51:04 +08:00
|
|
|
f->marked = 1;
|
2000-08-08 04:21:34 +08:00
|
|
|
strmark(f->source);
|
2001-06-06 02:17:01 +08:00
|
|
|
for (i=0; i<f->sizek; i++) {
|
|
|
|
if (ttype(f->k+i) == LUA_TSTRING)
|
|
|
|
strmark(tsvalue(f->k+i));
|
|
|
|
}
|
2000-12-28 20:55:41 +08:00
|
|
|
for (i=0; i<f->sizekproto; i++)
|
2000-08-08 04:21:34 +08:00
|
|
|
protomark(f->kproto[i]);
|
2000-12-28 20:55:41 +08:00
|
|
|
for (i=0; i<f->sizelocvars; i++) /* mark local-variable names */
|
2000-08-23 01:44:17 +08:00
|
|
|
strmark(f->locvars[i].varname);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-05 20:14:08 +08:00
|
|
|
static void markclosure (GCState *st, Closure *cl) {
|
|
|
|
if (!ismarked(cl)) {
|
2001-02-21 02:28:11 +08:00
|
|
|
if (!cl->isC) {
|
|
|
|
lua_assert(cl->nupvalues == cl->f.l->nupvalues);
|
2000-10-05 20:14:08 +08:00
|
|
|
protomark(cl->f.l);
|
2001-02-21 02:28:11 +08:00
|
|
|
}
|
2000-10-05 20:14:08 +08:00
|
|
|
cl->mark = st->cmark; /* chain it for later traversal */
|
|
|
|
st->cmark = cl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-23 02:01:38 +08:00
|
|
|
static void marktable (GCState *st, Hash *h) {
|
|
|
|
if (!ismarked(h)) {
|
|
|
|
h->mark = st->tmark; /* chain it in list of marked */
|
|
|
|
st->tmark = h;
|
2000-10-05 21:00:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-05 20:14:08 +08:00
|
|
|
static void markobject (GCState *st, TObject *o) {
|
1997-09-17 03:25:59 +08:00
|
|
|
switch (ttype(o)) {
|
2001-06-07 02:00:19 +08:00
|
|
|
case LUA_TSTRING:
|
2000-08-08 04:21:34 +08:00
|
|
|
strmark(tsvalue(o));
|
1997-09-17 03:25:59 +08:00
|
|
|
break;
|
2001-06-07 02:00:19 +08:00
|
|
|
case LUA_TUSERDATA:
|
2001-06-07 23:01:21 +08:00
|
|
|
if (!ismarkedudata(uvalue(o)))
|
|
|
|
switchudatamark(uvalue(o));
|
2001-06-07 02:00:19 +08:00
|
|
|
break;
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TFUNCTION:
|
|
|
|
markclosure(st, clvalue(o));
|
|
|
|
break;
|
|
|
|
case LUA_TTABLE: {
|
2001-01-23 02:01:38 +08:00
|
|
|
marktable(st, hvalue(o));
|
2000-06-27 03:28:31 +08:00
|
|
|
break;
|
2000-08-08 04:21:34 +08:00
|
|
|
}
|
2000-03-30 04:19:20 +08:00
|
|
|
default: break; /* numbers, etc */
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-23 02:01:38 +08:00
|
|
|
static void markstacks (lua_State *L, GCState *st) {
|
|
|
|
lua_State *L1 = L;
|
|
|
|
do { /* for each thread */
|
2001-02-08 02:13:49 +08:00
|
|
|
StkId o, lim;
|
2001-01-23 02:01:38 +08:00
|
|
|
marktable(st, L1->gt); /* mark table of globals */
|
|
|
|
for (o=L1->stack; o<L1->top; o++)
|
|
|
|
markobject(st, o);
|
2001-03-08 02:09:25 +08:00
|
|
|
lim = (L1->stack_last - L1->ci->base > MAXSTACK) ? L1->ci->base+MAXSTACK
|
|
|
|
: L1->stack_last;
|
2001-02-08 02:13:49 +08:00
|
|
|
for (; o<=lim; o++) setnilvalue(o);
|
|
|
|
lua_assert(L1->previous->next == L1 && L1->next->previous == L1);
|
2001-01-23 02:01:38 +08:00
|
|
|
L1 = L1->next;
|
|
|
|
} while (L1 != L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void marktagmethods (global_State *G, GCState *st) {
|
2001-01-26 00:45:36 +08:00
|
|
|
int t;
|
|
|
|
for (t=0; t<G->ntag; t++) {
|
|
|
|
struct TM *tm = &G->TMtable[t];
|
|
|
|
int e;
|
|
|
|
if (tm->name) strmark(tm->name);
|
|
|
|
for (e=0; e<TM_N; e++) {
|
|
|
|
Closure *cl = tm->method[e];
|
2001-01-23 02:01:38 +08:00
|
|
|
if (cl) markclosure(st, cl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-26 21:18:00 +08:00
|
|
|
static void traverseclosure (GCState *st, Closure *f) {
|
|
|
|
int i;
|
|
|
|
for (i=0; i<f->nupvalues; i++) /* mark its upvalues */
|
|
|
|
markobject(st, &f->upvalue[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-11 22:42:41 +08:00
|
|
|
static void removekey (Node *n) {
|
|
|
|
if (ttype_key(n) != LUA_TNIL && ttype_key(n) != LUA_TNUMBER)
|
|
|
|
n->key_value.ts = NULL; /* dead key; remove it */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-26 21:18:00 +08:00
|
|
|
static void traversetable (GCState *st, Hash *h) {
|
|
|
|
int i;
|
2001-04-11 22:42:41 +08:00
|
|
|
int mode = h->weakmode;
|
|
|
|
if (mode == (LUA_WEAK_KEY | LUA_WEAK_VALUE))
|
|
|
|
return; /* avoid traversing if both keys and values are weak */
|
2001-01-26 21:18:00 +08:00
|
|
|
for (i=0; i<h->size; i++) {
|
|
|
|
Node *n = node(h, i);
|
2001-04-11 22:42:41 +08:00
|
|
|
if (ttype(val(n)) == LUA_TNIL)
|
|
|
|
removekey(n);
|
2001-01-26 21:18:00 +08:00
|
|
|
else {
|
2001-01-30 01:17:26 +08:00
|
|
|
lua_assert(ttype_key(n) != LUA_TNIL);
|
2001-04-11 22:42:41 +08:00
|
|
|
if (ttype_key(n) != LUA_TNUMBER && !(mode & LUA_WEAK_KEY)) {
|
|
|
|
TObject k;
|
|
|
|
setkey2obj(&k, n);
|
|
|
|
markobject(st, &k);
|
2001-01-30 01:17:26 +08:00
|
|
|
}
|
2001-04-11 22:42:41 +08:00
|
|
|
if (!(mode & LUA_WEAK_VALUE))
|
|
|
|
markobject(st, &n->val);
|
2001-01-26 21:18:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-08 04:21:34 +08:00
|
|
|
static void markall (lua_State *L) {
|
|
|
|
GCState st;
|
|
|
|
st.cmark = NULL;
|
2001-01-23 02:01:38 +08:00
|
|
|
st.tmark = NULL;
|
2001-01-19 21:20:30 +08:00
|
|
|
marktagmethods(G(L), &st); /* mark tag methods */
|
2001-01-23 02:01:38 +08:00
|
|
|
markstacks(L, &st); /* mark all stacks */
|
2001-01-26 00:45:36 +08:00
|
|
|
marktable(&st, G(L)->type2tag);
|
2001-04-18 01:35:54 +08:00
|
|
|
marktable(&st, G(L)->registry);
|
|
|
|
marktable(&st, G(L)->weakregistry);
|
2000-08-08 04:21:34 +08:00
|
|
|
for (;;) { /* mark tables and closures */
|
|
|
|
if (st.cmark) {
|
|
|
|
Closure *f = st.cmark; /* get first closure from list */
|
|
|
|
st.cmark = f->mark; /* remove it from list */
|
2001-01-26 21:18:00 +08:00
|
|
|
traverseclosure(&st, f);
|
2000-08-08 04:21:34 +08:00
|
|
|
}
|
|
|
|
else if (st.tmark) {
|
|
|
|
Hash *h = st.tmark; /* get first table from list */
|
|
|
|
st.tmark = h->mark; /* remove it from list */
|
2001-01-26 21:18:00 +08:00
|
|
|
traversetable(&st, h);
|
2000-08-08 04:21:34 +08:00
|
|
|
}
|
|
|
|
else break; /* nothing else to mark */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int hasmark (const TObject *o) {
|
2001-01-18 23:59:09 +08:00
|
|
|
switch (ttype(o)) {
|
2001-06-07 02:00:19 +08:00
|
|
|
case LUA_TSTRING:
|
2000-08-29 01:57:04 +08:00
|
|
|
return tsvalue(o)->marked;
|
2001-06-07 02:00:19 +08:00
|
|
|
case LUA_TUSERDATA:
|
2001-06-07 23:01:21 +08:00
|
|
|
return ismarkedudata(uvalue(o));
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TTABLE:
|
2000-08-29 01:57:04 +08:00
|
|
|
return ismarked(hvalue(o));
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TFUNCTION:
|
2000-09-25 22:52:10 +08:00
|
|
|
return ismarked(clvalue(o));
|
2001-04-11 22:42:41 +08:00
|
|
|
default: /* number, nil */
|
2000-08-29 01:57:04 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-11 22:42:41 +08:00
|
|
|
static void invalidatetable (Hash *h) {
|
|
|
|
int i;
|
|
|
|
for (i=0; i<h->size; i++) {
|
|
|
|
Node *n = node(h, i);
|
|
|
|
TObject k;
|
|
|
|
if (ttype(val(n)) == LUA_TNIL) continue; /* empty node */
|
|
|
|
setkey2obj(&k, n);
|
|
|
|
if (!hasmark(val(n)) || !hasmark(&k)) {
|
|
|
|
setnilvalue(val(n)); /* remove value */
|
|
|
|
removekey(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void invalidatetables (global_State *G) {
|
|
|
|
Hash *h;
|
|
|
|
for (h = G->roottable; h; h = h->next) {
|
|
|
|
if (ismarked(h) && h->weakmode)
|
|
|
|
invalidatetable(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
1999-11-22 21:12:07 +08:00
|
|
|
static void collectproto (lua_State *L) {
|
2001-01-19 21:20:30 +08:00
|
|
|
Proto **p = &G(L)->rootproto;
|
2000-03-11 02:37:44 +08:00
|
|
|
Proto *next;
|
1999-10-05 01:51:04 +08:00
|
|
|
while ((next = *p) != NULL) {
|
|
|
|
if (next->marked) {
|
|
|
|
next->marked = 0;
|
|
|
|
p = &next->next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*p = next->next;
|
1999-11-22 21:12:07 +08:00
|
|
|
luaF_freeproto(L, next);
|
1999-10-05 01:51:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 21:12:07 +08:00
|
|
|
static void collectclosure (lua_State *L) {
|
2001-01-19 21:20:30 +08:00
|
|
|
Closure **p = &G(L)->rootcl;
|
1999-10-05 01:51:04 +08:00
|
|
|
Closure *next;
|
|
|
|
while ((next = *p) != NULL) {
|
2000-08-08 04:21:34 +08:00
|
|
|
if (ismarked(next)) {
|
|
|
|
next->mark = next; /* unmark */
|
1999-10-05 01:51:04 +08:00
|
|
|
p = &next->next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*p = next->next;
|
1999-11-22 21:12:07 +08:00
|
|
|
luaF_freeclosure(L, next);
|
1999-10-05 01:51:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 21:12:07 +08:00
|
|
|
static void collecttable (lua_State *L) {
|
2001-01-19 21:20:30 +08:00
|
|
|
Hash **p = &G(L)->roottable;
|
1999-10-05 01:51:04 +08:00
|
|
|
Hash *next;
|
|
|
|
while ((next = *p) != NULL) {
|
2000-08-08 04:21:34 +08:00
|
|
|
if (ismarked(next)) {
|
|
|
|
next->mark = next; /* unmark */
|
1999-10-05 01:51:04 +08:00
|
|
|
p = &next->next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*p = next->next;
|
1999-11-22 21:12:07 +08:00
|
|
|
luaH_free(L, next);
|
1999-10-05 01:51:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-13 02:43:13 +08:00
|
|
|
void luaC_collectudata (lua_State *L) {
|
2001-06-07 02:00:19 +08:00
|
|
|
Udata **p = &G(L)->rootudata;
|
|
|
|
Udata *next;
|
|
|
|
while ((next = *p) != NULL) {
|
2001-06-07 23:01:21 +08:00
|
|
|
if (ismarkedudata(next)) {
|
|
|
|
switchudatamark(next); /* unmark */
|
2001-06-07 02:00:19 +08:00
|
|
|
p = &next->next;
|
|
|
|
}
|
|
|
|
else { /* collect */
|
|
|
|
int tag = next->tag;
|
|
|
|
*p = next->next;
|
|
|
|
next->next = G(L)->TMtable[tag].collected; /* chain udata */
|
|
|
|
G(L)->TMtable[tag].collected = next;
|
|
|
|
}
|
|
|
|
}
|
2000-05-31 02:54:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-02 22:47:43 +08:00
|
|
|
static void collectstrings (lua_State *L, int all) {
|
1999-10-05 01:51:04 +08:00
|
|
|
int i;
|
2001-01-19 21:20:30 +08:00
|
|
|
for (i=0; i<G(L)->strt.size; i++) { /* for each list */
|
|
|
|
TString **p = &G(L)->strt.hash[i];
|
2000-05-11 00:33:20 +08:00
|
|
|
TString *next;
|
|
|
|
while ((next = *p) != NULL) {
|
2000-10-02 22:47:43 +08:00
|
|
|
if (next->marked && !all) { /* preserve? */
|
2000-05-31 02:54:49 +08:00
|
|
|
if (next->marked < FIXMARK) /* does not change FIXMARKs */
|
|
|
|
next->marked = 0;
|
|
|
|
p = &next->nexthash;
|
|
|
|
}
|
|
|
|
else { /* collect */
|
2000-05-11 00:33:20 +08:00
|
|
|
*p = next->nexthash;
|
2001-01-19 21:20:30 +08:00
|
|
|
G(L)->strt.nuse--;
|
2000-12-28 20:55:41 +08:00
|
|
|
luaM_free(L, next, sizestring(next->len));
|
1999-10-05 01:51:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-06-07 02:00:19 +08:00
|
|
|
if (G(L)->strt.nuse < (ls_nstr)(G(L)->strt.size/4) &&
|
|
|
|
G(L)->strt.size > MINPOWER2)
|
|
|
|
luaS_resize(L, G(L)->strt.size/2); /* table is too big */
|
2000-05-11 00:33:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-31 02:54:49 +08:00
|
|
|
|
2000-10-02 22:47:43 +08:00
|
|
|
#define MINBUFFER 256
|
|
|
|
static void checkMbuffer (lua_State *L) {
|
2001-01-19 21:20:30 +08:00
|
|
|
if (G(L)->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
|
|
|
|
size_t newsize = G(L)->Mbuffsize/2; /* still larger than MINBUFFER */
|
2001-02-24 01:17:25 +08:00
|
|
|
luaM_reallocvector(L, G(L)->Mbuffer, G(L)->Mbuffsize, newsize, l_char);
|
2001-01-19 21:20:30 +08:00
|
|
|
G(L)->Mbuffsize = newsize;
|
2000-10-02 22:47:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-18 23:59:09 +08:00
|
|
|
static void callgcTM (lua_State *L, const TObject *obj) {
|
2001-01-19 21:20:30 +08:00
|
|
|
Closure *tm = luaT_gettmbyObj(G(L), obj, TM_GC);
|
2000-10-05 21:00:17 +08:00
|
|
|
if (tm != NULL) {
|
2000-10-02 22:47:43 +08:00
|
|
|
int oldah = L->allowhooks;
|
2001-06-09 03:01:38 +08:00
|
|
|
StkId top = L->top;
|
2000-10-02 22:47:43 +08:00
|
|
|
L->allowhooks = 0; /* stop debug hooks during GC tag methods */
|
2000-05-31 02:54:49 +08:00
|
|
|
luaD_checkstack(L, 2);
|
2001-06-09 03:01:38 +08:00
|
|
|
setclvalue(top, tm);
|
|
|
|
setobj(top+1, obj);
|
2000-05-31 02:54:49 +08:00
|
|
|
L->top += 2;
|
2001-06-09 03:01:38 +08:00
|
|
|
luaD_call(L, top);
|
|
|
|
L->top = top; /* restore top */
|
2000-10-02 22:47:43 +08:00
|
|
|
L->allowhooks = oldah; /* restore hooks */
|
2000-05-31 02:54:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-13 02:43:13 +08:00
|
|
|
void luaC_callgcTMudata (lua_State *L) {
|
2000-05-31 02:54:49 +08:00
|
|
|
int tag;
|
2001-01-19 21:20:30 +08:00
|
|
|
for (tag=G(L)->ntag-1; tag>=0; tag--) { /* for each tag (in reverse order) */
|
2001-06-07 02:00:19 +08:00
|
|
|
Udata *udata;
|
2001-01-19 21:20:30 +08:00
|
|
|
while ((udata = G(L)->TMtable[tag].collected) != NULL) {
|
2001-01-18 23:59:09 +08:00
|
|
|
TObject obj;
|
2001-06-07 02:00:19 +08:00
|
|
|
G(L)->TMtable[tag].collected = udata->next; /* remove it from list */
|
2001-01-18 23:59:09 +08:00
|
|
|
setuvalue(&obj, udata);
|
|
|
|
callgcTM(L, &obj);
|
2000-12-28 20:55:41 +08:00
|
|
|
luaM_free(L, udata, sizeudata(udata->len));
|
2000-05-31 02:54:49 +08:00
|
|
|
}
|
|
|
|
}
|
1999-10-05 01:51:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 21:12:07 +08:00
|
|
|
void luaC_collect (lua_State *L, int all) {
|
2001-06-13 02:43:13 +08:00
|
|
|
luaC_collectudata(L);
|
2000-10-02 22:47:43 +08:00
|
|
|
collectstrings(L, all);
|
1999-11-22 21:12:07 +08:00
|
|
|
collecttable(L);
|
|
|
|
collectproto(L);
|
|
|
|
collectclosure(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-03 00:23:20 +08:00
|
|
|
void luaC_collectgarbage (lua_State *L) {
|
1999-11-22 21:12:07 +08:00
|
|
|
markall(L);
|
2001-04-11 22:42:41 +08:00
|
|
|
invalidatetables(G(L));
|
1999-11-22 21:12:07 +08:00
|
|
|
luaC_collect(L, 0);
|
2001-06-14 02:51:20 +08:00
|
|
|
checkMbuffer(L);
|
|
|
|
G(L)->GCthreshold = 2*G(L)->nblocks; /* set new threshold */
|
|
|
|
luaC_callgcTMudata(L);
|
2000-05-31 02:54:49 +08:00
|
|
|
callgcTM(L, &luaO_nilobject);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|