lua/lgc.c

358 lines
8.4 KiB
C
Raw Normal View History

1997-09-16 23:25:59 +04:00
/*
** $Id: lgc.c,v 1.77 2001/01/19 13:20:30 roberto Exp roberto $
1997-09-16 23:25:59 +04:00
** Garbage Collector
** See Copyright Notice in lua.h
*/
#include "lua.h"
1997-09-16 23:25:59 +04:00
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
1999-11-10 18:40:46 +03:00
#include "lmem.h"
1997-09-16 23:25:59 +04:00
#include "lobject.h"
#include "lstate.h"
1997-09-16 23:25:59 +04:00
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
typedef struct GCState {
Hash *tmark; /* list of marked tables to be visited */
Closure *cmark; /* list of marked closures to be visited */
} GCState;
1997-09-16 23:25:59 +04:00
2000-04-14 22:12:35 +04:00
2000-10-05 16:14:08 +04:00
static void markobject (GCState *st, TObject *o);
1997-09-16 23:25:59 +04:00
1999-12-14 21:33:29 +03:00
/* mark a string; marks larger than 1 cannot be changed */
#define strmark(s) {if ((s)->marked == 0) (s)->marked = 1;}
1997-09-16 23:25:59 +04:00
static void protomark (Proto *f) {
if (!f->marked) {
1997-09-16 23:25:59 +04:00
int i;
f->marked = 1;
strmark(f->source);
for (i=0; i<f->sizekstr; i++)
strmark(f->kstr[i]);
for (i=0; i<f->sizekproto; i++)
protomark(f->kproto[i]);
for (i=0; i<f->sizelocvars; i++) /* mark local-variable names */
strmark(f->locvars[i].varname);
1997-09-16 23:25:59 +04:00
}
}
2000-10-05 16:14:08 +04:00
static void markclosure (GCState *st, Closure *cl) {
if (!ismarked(cl)) {
if (!cl->isC)
protomark(cl->f.l);
cl->mark = st->cmark; /* chain it for later traversal */
st->cmark = cl;
}
}
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 16:14:08 +04:00
static void markobject (GCState *st, TObject *o) {
1997-09-16 23:25:59 +04:00
switch (ttype(o)) {
2000-10-05 16:14:08 +04:00
case LUA_TUSERDATA: case LUA_TSTRING:
strmark(tsvalue(o));
1997-09-16 23:25:59 +04:00
break;
2000-10-05 16:14:08 +04:00
case LUA_TMARK:
markclosure(st, infovalue(o)->func);
break;
case LUA_TFUNCTION:
markclosure(st, clvalue(o));
break;
case LUA_TTABLE: {
marktable(st, hvalue(o));
2000-06-26 23:28:31 +04:00
break;
}
default: break; /* numbers, etc */
1997-09-16 23:25:59 +04:00
}
}
static void markstacks (lua_State *L, GCState *st) {
lua_State *L1 = L;
do { /* for each thread */
StkId o;
marktable(st, L1->gt); /* mark table of globals */
for (o=L1->stack; o<L1->top; o++)
markobject(st, o);
lua_assert(L->previous->next == L && L->next->previous == L);
L1 = L1->next;
} while (L1 != L);
}
static void marklock (global_State *G, GCState *st) {
int i;
for (i=0; i<G->nref; i++) {
if (G->refArray[i].st == LOCK)
markobject(st, &G->refArray[i].o);
}
}
static void marktagmethods (global_State *G, GCState *st) {
int e;
for (e=0; e<TM_N; e++) {
int t;
for (t=0; t<G->ntag; t++) {
Closure *cl = luaT_gettm(G, t, e);
if (cl) markclosure(st, cl);
}
}
}
static void markall (lua_State *L) {
GCState st;
st.cmark = NULL;
st.tmark = NULL;
marktagmethods(G(L), &st); /* mark tag methods */
markstacks(L, &st); /* mark all stacks */
marklock(G(L), &st); /* mark locked objects */
for (;;) { /* mark tables and closures */
if (st.cmark) {
int i;
Closure *f = st.cmark; /* get first closure from list */
st.cmark = f->mark; /* remove it from list */
for (i=0; i<f->nupvalues; i++) /* mark its upvalues */
markobject(&st, &f->upvalue[i]);
}
else if (st.tmark) {
int i;
Hash *h = st.tmark; /* get first table from list */
st.tmark = h->mark; /* remove it from list */
for (i=0; i<h->size; i++) {
Node *n = node(h, i);
2000-10-05 16:14:08 +04:00
if (ttype(key(n)) != LUA_TNIL) {
if (ttype(val(n)) == LUA_TNIL)
luaH_remove(h, key(n)); /* dead element; try to remove it */
markobject(&st, &n->key);
markobject(&st, &n->val);
}
}
}
else break; /* nothing else to mark */
}
}
2000-08-28 21:57:04 +04:00
static int hasmark (const TObject *o) {
/* valid only for locked objects */
switch (ttype(o)) {
2000-10-05 16:14:08 +04:00
case LUA_TSTRING: case LUA_TUSERDATA:
2000-08-28 21:57:04 +04:00
return tsvalue(o)->marked;
2000-10-05 16:14:08 +04:00
case LUA_TTABLE:
2000-08-28 21:57:04 +04:00
return ismarked(hvalue(o));
2000-10-05 16:14:08 +04:00
case LUA_TFUNCTION:
2000-09-25 18:52:10 +04:00
return ismarked(clvalue(o));
2000-08-28 21:57:04 +04:00
default: /* number */
return 1;
}
}
/* macro for internal debugging; check if a link of free refs is valid */
#define VALIDLINK(L, st,n) (NONEXT <= (st) && (st) < (n))
static void invalidaterefs (global_State *G) {
int n = G->nref;
2000-08-28 21:57:04 +04:00
int i;
for (i=0; i<n; i++) {
struct Ref *r = &G->refArray[i];
2000-08-28 21:57:04 +04:00
if (r->st == HOLD && !hasmark(&r->o))
r->st = COLLECTED;
lua_assert((r->st == LOCK && hasmark(&r->o)) ||
2000-08-28 21:57:04 +04:00
(r->st == HOLD && hasmark(&r->o)) ||
r->st == COLLECTED ||
r->st == NONEXT ||
(r->st < n && VALIDLINK(L, G->refArray[r->st].st, n)));
2000-08-28 21:57:04 +04:00
}
lua_assert(VALIDLINK(L, G->refFree, n));
2000-08-28 21:57:04 +04:00
}
static void collectproto (lua_State *L) {
Proto **p = &G(L)->rootproto;
2000-03-10 21:37:44 +03:00
Proto *next;
while ((next = *p) != NULL) {
if (next->marked) {
next->marked = 0;
p = &next->next;
}
else {
*p = next->next;
luaF_freeproto(L, next);
}
}
}
static void collectclosure (lua_State *L) {
Closure **p = &G(L)->rootcl;
Closure *next;
while ((next = *p) != NULL) {
if (ismarked(next)) {
next->mark = next; /* unmark */
p = &next->next;
}
else {
*p = next->next;
luaF_freeclosure(L, next);
}
}
}
static void collecttable (lua_State *L) {
Hash **p = &G(L)->roottable;
Hash *next;
while ((next = *p) != NULL) {
if (ismarked(next)) {
next->mark = next; /* unmark */
p = &next->next;
}
else {
*p = next->next;
luaH_free(L, next);
}
}
}
static void checktab (lua_State *L, stringtable *tb) {
if (tb->nuse < (luint32)(tb->size/4) && tb->size > 10)
luaS_resize(L, tb, tb->size/2); /* table is too big */
}
2000-10-02 18:47:43 +04:00
static void collectstrings (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];
2000-05-10 20:33:20 +04:00
TString *next;
while ((next = *p) != NULL) {
2000-10-02 18:47:43 +04:00
if (next->marked && !all) { /* preserve? */
if (next->marked < FIXMARK) /* does not change FIXMARKs */
next->marked = 0;
p = &next->nexthash;
}
else { /* collect */
2000-05-10 20:33:20 +04:00
*p = next->nexthash;
G(L)->strt.nuse--;
luaM_free(L, next, sizestring(next->len));
}
}
}
checktab(L, &G(L)->strt);
2000-05-10 20:33:20 +04:00
}
2000-10-02 18:47:43 +04:00
static void collectudata (lua_State *L, int all) {
int i;
for (i=0; i<G(L)->udt.size; i++) { /* for each list */
TString **p = &G(L)->udt.hash[i];
TString *next;
while ((next = *p) != NULL) {
lua_assert(next->marked <= 1);
2000-10-02 18:47:43 +04:00
if (next->marked && !all) { /* preserve? */
next->marked = 0;
p = &next->nexthash;
}
else { /* collect */
int tag = next->u.d.tag;
*p = next->nexthash;
next->nexthash = G(L)->TMtable[tag].collected; /* chain udata */
G(L)->TMtable[tag].collected = next;
G(L)->udt.nuse--;
}
}
}
checktab(L, &G(L)->udt);
}
2000-10-02 18:47:43 +04:00
#define MINBUFFER 256
static void checkMbuffer (lua_State *L) {
if (G(L)->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
size_t newsize = G(L)->Mbuffsize/2; /* still larger than MINBUFFER */
luaM_reallocvector(L, G(L)->Mbuffer, G(L)->Mbuffsize, newsize, char);
G(L)->Mbuffsize = newsize;
2000-10-02 18:47:43 +04:00
}
}
static void callgcTM (lua_State *L, const TObject *obj) {
Closure *tm = luaT_gettmbyObj(G(L), obj, TM_GC);
if (tm != NULL) {
2000-10-02 18:47:43 +04:00
int oldah = L->allowhooks;
L->allowhooks = 0; /* stop debug hooks during GC tag methods */
luaD_checkstack(L, 2);
setclvalue(L->top, tm);
setobj(L->top+1, obj);
L->top += 2;
luaD_call(L, L->top-2, 0);
2000-10-02 18:47:43 +04:00
L->allowhooks = oldah; /* restore hooks */
}
}
static void callgcTMudata (lua_State *L) {
int tag;
G(L)->GCthreshold = 2*G(L)->nblocks; /* avoid GC during tag methods */
for (tag=G(L)->ntag-1; tag>=0; tag--) { /* for each tag (in reverse order) */
2000-10-02 18:47:43 +04:00
TString *udata;
while ((udata = G(L)->TMtable[tag].collected) != NULL) {
TObject obj;
G(L)->TMtable[tag].collected = udata->nexthash; /* remove it from list */
setuvalue(&obj, udata);
callgcTM(L, &obj);
luaM_free(L, udata, sizeudata(udata->len));
}
}
}
void luaC_collect (lua_State *L, int all) {
2000-10-02 18:47:43 +04:00
collectudata(L, all);
callgcTMudata(L);
2000-10-02 18:47:43 +04:00
collectstrings(L, all);
collecttable(L);
collectproto(L);
collectclosure(L);
1997-09-16 23:25:59 +04:00
}
2000-10-02 18:47:43 +04:00
static void luaC_collectgarbage (lua_State *L) {
markall(L);
invalidaterefs(G(L)); /* check unlocked references */
luaC_collect(L, 0);
2000-10-02 18:47:43 +04:00
checkMbuffer(L);
G(L)->GCthreshold = 2*G(L)->nblocks; /* set new threshold */
callgcTM(L, &luaO_nilobject);
1997-09-16 23:25:59 +04:00
}
void luaC_checkGC (lua_State *L) {
if (G(L)->nblocks >= G(L)->GCthreshold)
2000-10-02 18:47:43 +04:00
luaC_collectgarbage(L);
1997-09-16 23:25:59 +04:00
}