lua/lgc.c

355 lines
8.5 KiB
C
Raw Normal View History

1997-09-17 03:25:59 +08:00
/*
2000-10-02 22:47:43 +08:00
** $Id: lgc.c,v 1.68 2000/09/29 12:42:13 roberto Exp roberto $
1997-09-17 03:25:59 +08:00
** Garbage Collector
** See Copyright Notice in lua.h
*/
#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"
#include "lstate.h"
1997-09-17 03:25:59 +08: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-17 03:25:59 +08:00
2000-04-15 02:12:35 +08:00
static int markobject (GCState *st, TObject *o);
1997-09-17 03:25:59 +08:00
1999-12-15 02:33:29 +08:00
/* mark a string; marks larger than 1 cannot be changed */
#define strmark(s) {if ((s)->marked == 0) (s)->marked = 1;}
1997-09-17 03:25:59 +08:00
static void protomark (Proto *f) {
if (!f->marked) {
1997-09-17 03:25:59 +08:00
int i;
f->marked = 1;
strmark(f->source);
2000-06-05 22:56:18 +08:00
for (i=0; i<f->nkstr; i++)
strmark(f->kstr[i]);
2000-06-05 22:56:18 +08:00
for (i=0; i<f->nkproto; i++)
protomark(f->kproto[i]);
for (i=0; i<f->nlocvars; i++) /* mark local-variable names */
strmark(f->locvars[i].varname);
1997-09-17 03:25:59 +08:00
}
}
static void markstack (lua_State *L, GCState *st) {
StkId o;
for (o=L->stack; o<L->top; o++)
markobject(st, o);
}
static void marklock (lua_State *L, GCState *st) {
int i;
1999-11-10 23:40:46 +08:00
for (i=0; i<L->refSize; i++) {
if (L->refArray[i].st == LOCK)
markobject(st, &L->refArray[i].o);
}
}
static void marktagmethods (lua_State *L, GCState *st) {
int e;
for (e=0; e<IM_N; e++) {
int t;
for (t=0; t<=L->last_tag; t++)
markobject(st, luaT_getim(L, t,e));
1999-11-10 23:40:46 +08:00
}
}
static int markobject (GCState *st, TObject *o) {
1997-09-17 03:25:59 +08:00
switch (ttype(o)) {
2000-03-11 02:37:44 +08:00
case TAG_USERDATA: case TAG_STRING:
strmark(tsvalue(o));
1997-09-17 03:25:59 +08:00
break;
case TAG_TABLE: {
if (!ismarked(hvalue(o))) {
hvalue(o)->mark = st->tmark; /* chain it in list of marked */
st->tmark = hvalue(o);
}
2000-06-27 03:28:31 +08:00
break;
}
2000-06-27 03:28:31 +08:00
case TAG_LMARK: {
Closure *cl = infovalue(o)->func;
if (!ismarked(cl)) {
protomark(cl->f.l);
cl->mark = st->cmark; /* chain it for later traversal */
st->cmark = cl;
}
2000-06-27 03:28:31 +08:00
break;
}
case TAG_LCLOSURE:
protomark(clvalue(o)->f.l);
/* go through */
2000-03-31 04:55:50 +08:00
case TAG_CCLOSURE: case TAG_CMARK:
if (!ismarked(clvalue(o))) {
clvalue(o)->mark = st->cmark; /* chain it for later traversal */
st->cmark = clvalue(o);
}
1997-09-17 03:25:59 +08:00
break;
default: break; /* numbers, etc */
1997-09-17 03:25:59 +08:00
}
return 0;
}
static void markall (lua_State *L) {
GCState st;
st.cmark = NULL;
st.tmark = L->gt; /* put table of globals in mark list */
L->gt->mark = NULL;
marktagmethods(L, &st); /* mark tag methods */
markstack(L, &st); /* mark stack objects */
marklock(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);
if (ttype(key(n)) != TAG_NIL) {
if (ttype(val(n)) == TAG_NIL)
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-29 01:57:04 +08:00
static int hasmark (const TObject *o) {
/* valid only for locked objects */
switch (o->ttype) {
case TAG_STRING: case TAG_USERDATA:
return tsvalue(o)->marked;
case TAG_TABLE:
return ismarked(hvalue(o));
case TAG_LCLOSURE: case TAG_CCLOSURE:
2000-09-25 22:52:10 +08:00
return ismarked(clvalue(o));
2000-08-29 01:57:04 +08: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 (lua_State *L) {
int n = L->refSize;
int i;
for (i=0; i<n; i++) {
struct Ref *r = &L->refArray[i];
if (r->st == HOLD && !hasmark(&r->o))
r->st = COLLECTED;
LUA_ASSERT((r->st == LOCK && hasmark(&r->o)) ||
(r->st == HOLD && hasmark(&r->o)) ||
r->st == COLLECTED ||
r->st == NONEXT ||
(r->st < n && VALIDLINK(L, L->refArray[r->st].st, n)),
"inconsistent ref table");
}
LUA_ASSERT(VALIDLINK(L, L->refFree, n), "inconsistent ref table");
}
static void collectproto (lua_State *L) {
2000-03-11 02:37:44 +08:00
Proto **p = &L->rootproto;
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 = &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 = &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 < (lint32)(tb->size/4) && tb->size > 10)
luaS_resize(L, tb, tb->size/2); /* table is too big */
}
2000-10-02 22:47:43 +08:00
static void collectstrings (lua_State *L, int all) {
int i;
for (i=0; i<L->strt.size; i++) { /* for each list */
TString **p = &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? */
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;
L->strt.nuse--;
L->nblocks -= sizestring(next->u.s.len);
2000-05-11 00:33:20 +08:00
luaM_free(L, next);
}
}
}
checktab(L, &L->strt);
2000-05-11 00:33:20 +08:00
}
2000-10-02 22:47:43 +08:00
static void collectudata (lua_State *L, int all) {
int i;
for (i=0; i<L->udt.size; i++) { /* for each list */
TString **p = &L->udt.hash[i];
TString *next;
while ((next = *p) != NULL) {
2000-06-30 22:35:17 +08:00
LUA_ASSERT(next->marked <= 1, "udata cannot be fixed");
2000-10-02 22:47:43 +08:00
if (next->marked && !all) { /* preserve? */
next->marked = 0;
p = &next->nexthash;
}
else { /* collect */
int tag = next->u.d.tag;
if (tag > L->last_tag) tag = TAG_USERDATA;
*p = next->nexthash;
next->nexthash = L->IMtable[tag].collected; /* chain udata */
L->IMtable[tag].collected = next;
L->nblocks -= gcsizeudata;
L->udt.nuse--;
}
}
}
checktab(L, &L->udt);
}
2000-10-02 22:47:43 +08:00
#define MINBUFFER 256
static void checkMbuffer (lua_State *L) {
if (L->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
size_t newsize = L->Mbuffsize/2; /* still larger than MINBUFFER */
L->nblocks += (newsize - L->Mbuffsize)*sizeof(char);
L->Mbuffsize = newsize;
luaM_reallocvector(L, L->Mbuffer, newsize, char);
}
}
static void callgcTM (lua_State *L, const TObject *o) {
const TObject *im = luaT_getimbyObj(L, o, IM_GC);
if (ttype(im) != TAG_NIL) {
2000-10-02 22:47:43 +08:00
int oldah = L->allowhooks;
L->allowhooks = 0; /* stop debug hooks during GC tag methods */
luaD_checkstack(L, 2);
*(L->top) = *im;
*(L->top+1) = *o;
L->top += 2;
luaD_call(L, L->top-2, 0);
2000-10-02 22:47:43 +08:00
L->allowhooks = oldah; /* restore hooks */
}
}
static void callgcTMudata (lua_State *L) {
int tag;
TObject o;
ttype(&o) = TAG_USERDATA;
2000-10-02 22:47:43 +08:00
L->GCthreshold = 2*L->nblocks; /* avoid GC during tag methods */
for (tag=L->last_tag; tag>=0; tag--) { /* for each tag (in reverse order) */
TString *udata;
while ((udata = L->IMtable[tag].collected) != NULL) {
L->IMtable[tag].collected = udata->nexthash; /* remove it from list */
tsvalue(&o) = udata;
callgcTM(L, &o);
luaM_free(L, udata);
}
}
}
void luaC_collect (lua_State *L, int all) {
2000-10-02 22:47:43 +08:00
collectudata(L, all);
callgcTMudata(L);
2000-10-02 22:47:43 +08:00
collectstrings(L, all);
collecttable(L);
collectproto(L);
collectclosure(L);
1997-09-17 03:25:59 +08:00
}
2000-10-02 22:47:43 +08:00
static void luaC_collectgarbage (lua_State *L) {
markall(L);
2000-10-02 22:47:43 +08:00
invalidaterefs(L); /* check unlocked references */
luaC_collect(L, 0);
2000-10-02 22:47:43 +08:00
checkMbuffer(L);
L->GCthreshold = 2*L->nblocks; /* set new threshold */
callgcTM(L, &luaO_nilobject);
1997-09-17 03:25:59 +08:00
}
void luaC_checkGC (lua_State *L) {
if (L->nblocks >= L->GCthreshold)
2000-10-02 22:47:43 +08:00
luaC_collectgarbage(L);
1997-09-17 03:25:59 +08:00
}