1997-09-16 23:25:59 +04:00
|
|
|
/*
|
1999-09-27 22:00:25 +04:00
|
|
|
** $Id: lgc.c,v 1.25 1999/08/16 20:52:00 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Garbage Collector
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "ldo.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lgc.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
1997-11-19 20:29:23 +03:00
|
|
|
#include "lstate.h"
|
1997-09-16 23:25:59 +04:00
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltable.h"
|
|
|
|
#include "ltm.h"
|
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int markobject (TObject *o);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** =======================================================
|
|
|
|
** REF mechanism
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
int luaC_ref (const TObject *o, int lock) {
|
1997-09-16 23:25:59 +04:00
|
|
|
int ref;
|
|
|
|
if (ttype(o) == LUA_T_NIL)
|
1999-08-11 21:00:59 +04:00
|
|
|
ref = LUA_REFNIL;
|
1997-09-16 23:25:59 +04:00
|
|
|
else {
|
1997-11-19 20:29:23 +03:00
|
|
|
for (ref=0; ref<L->refSize; ref++)
|
|
|
|
if (L->refArray[ref].status == FREE)
|
1999-02-25 18:16:26 +03:00
|
|
|
break;
|
|
|
|
if (ref == L->refSize) { /* no more empty spaces? */
|
1999-02-26 18:48:55 +03:00
|
|
|
luaM_growvector(L->refArray, L->refSize, 1, struct ref, refEM, MAX_INT);
|
1999-02-25 18:16:26 +03:00
|
|
|
L->refSize++;
|
|
|
|
}
|
1997-11-19 20:29:23 +03:00
|
|
|
L->refArray[ref].o = *o;
|
|
|
|
L->refArray[ref].status = lock ? LOCK : HOLD;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
void lua_unref (int ref) {
|
1997-11-19 20:29:23 +03:00
|
|
|
if (ref >= 0 && ref < L->refSize)
|
|
|
|
L->refArray[ref].status = FREE;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
const TObject *luaC_getref (int ref) {
|
1999-08-11 21:00:59 +04:00
|
|
|
if (ref == LUA_REFNIL)
|
1997-11-03 23:45:23 +03:00
|
|
|
return &luaO_nilobject;
|
1997-11-19 20:29:23 +03:00
|
|
|
if (ref >= 0 && ref < L->refSize &&
|
|
|
|
(L->refArray[ref].status == LOCK || L->refArray[ref].status == HOLD))
|
|
|
|
return &L->refArray[ref].o;
|
1997-09-16 23:25:59 +04:00
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
static void travlock (void) {
|
1997-09-16 23:25:59 +04:00
|
|
|
int i;
|
1997-11-19 20:29:23 +03:00
|
|
|
for (i=0; i<L->refSize; i++)
|
|
|
|
if (L->refArray[i].status == LOCK)
|
|
|
|
markobject(&L->refArray[i].o);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
static int ismarked (const TObject *o) {
|
1997-12-11 17:48:46 +03:00
|
|
|
/* valid only for locked objects */
|
1997-09-16 23:25:59 +04:00
|
|
|
switch (o->ttype) {
|
|
|
|
case LUA_T_STRING: case LUA_T_USERDATA:
|
1997-09-26 19:02:26 +04:00
|
|
|
return o->value.ts->head.marked;
|
1998-01-09 17:44:55 +03:00
|
|
|
case LUA_T_ARRAY:
|
|
|
|
return o->value.a->head.marked;
|
1997-12-15 19:17:20 +03:00
|
|
|
case LUA_T_CLOSURE:
|
1997-09-16 23:25:59 +04:00
|
|
|
return o->value.cl->head.marked;
|
1997-12-15 19:17:20 +03:00
|
|
|
case LUA_T_PROTO:
|
|
|
|
return o->value.tf->head.marked;
|
1997-12-11 17:48:46 +03:00
|
|
|
#ifdef DEBUG
|
1997-12-15 19:17:20 +03:00
|
|
|
case LUA_T_LINE: case LUA_T_CLMARK:
|
|
|
|
case LUA_T_CMARK: case LUA_T_PMARK:
|
1998-03-10 00:49:52 +03:00
|
|
|
LUA_INTERNALERROR("invalid type");
|
1997-12-11 17:48:46 +03:00
|
|
|
#endif
|
1997-10-24 21:17:24 +04:00
|
|
|
default: /* nil, number or cproto */
|
1997-09-16 23:25:59 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
static void invalidaterefs (void) {
|
1997-09-16 23:25:59 +04:00
|
|
|
int i;
|
1997-11-19 20:29:23 +03:00
|
|
|
for (i=0; i<L->refSize; i++)
|
|
|
|
if (L->refArray[i].status == HOLD && !ismarked(&L->refArray[i].o))
|
|
|
|
L->refArray[i].status = COLLECTED;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
void luaC_hashcallIM (Hash *l) {
|
1997-09-16 23:25:59 +04:00
|
|
|
TObject t;
|
|
|
|
ttype(&t) = LUA_T_ARRAY;
|
|
|
|
for (; l; l=(Hash *)l->head.next) {
|
|
|
|
avalue(&t) = l;
|
|
|
|
luaD_gcIM(&t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
void luaC_strcallIM (TaggedString *l) {
|
1997-09-16 23:25:59 +04:00
|
|
|
TObject o;
|
|
|
|
ttype(&o) = LUA_T_USERDATA;
|
1997-09-26 19:02:26 +04:00
|
|
|
for (; l; l=(TaggedString *)l->head.next)
|
|
|
|
if (l->constindex == -1) { /* is userdata? */
|
|
|
|
tsvalue(&o) = l;
|
|
|
|
luaD_gcIM(&o);
|
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
static GCnode *listcollect (GCnode *l) {
|
1997-09-26 20:46:20 +04:00
|
|
|
GCnode *frees = NULL;
|
|
|
|
while (l) {
|
|
|
|
GCnode *next = l->next;
|
|
|
|
l->marked = 0;
|
|
|
|
while (next && !next->marked) {
|
|
|
|
l->next = next->next;
|
|
|
|
next->next = frees;
|
|
|
|
frees = next;
|
|
|
|
next = l->next;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
1997-09-26 20:46:20 +04:00
|
|
|
l = next;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
return frees;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-09-27 22:00:25 +04:00
|
|
|
/*
|
|
|
|
** mark a string; marks bigger than 1 cannot be changed.
|
|
|
|
*/
|
|
|
|
#define strmark(s) {if ((s)->head.marked == 0) (s)->head.marked = 1;}
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
1999-01-22 21:08:03 +03:00
|
|
|
static void protomark (TProtoFunc *f) {
|
1997-09-16 23:25:59 +04:00
|
|
|
if (!f->head.marked) {
|
|
|
|
int i;
|
|
|
|
f->head.marked = 1;
|
1999-03-05 00:23:39 +03:00
|
|
|
strmark(f->source);
|
1999-09-27 22:00:25 +04:00
|
|
|
for (i=f->nconsts-1; i>=0; i--)
|
1997-09-16 23:25:59 +04:00
|
|
|
markobject(&f->consts[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
static void closuremark (Closure *f) {
|
1997-09-16 23:25:59 +04:00
|
|
|
if (!f->head.marked) {
|
|
|
|
int i;
|
|
|
|
f->head.marked = 1;
|
1997-10-16 14:59:34 +04:00
|
|
|
for (i=f->nelems; i>=0; i--)
|
1997-09-16 23:25:59 +04:00
|
|
|
markobject(&f->consts[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
static void hashmark (Hash *h) {
|
1997-09-16 23:25:59 +04:00
|
|
|
if (!h->head.marked) {
|
|
|
|
int i;
|
|
|
|
h->head.marked = 1;
|
1999-09-27 22:00:25 +04:00
|
|
|
for (i=nhash(h)-1; i>=0; i--) {
|
1997-09-16 23:25:59 +04:00
|
|
|
Node *n = node(h,i);
|
|
|
|
if (ttype(ref(n)) != LUA_T_NIL) {
|
|
|
|
markobject(&n->ref);
|
|
|
|
markobject(&n->val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
static void globalmark (void) {
|
1997-09-26 19:02:26 +04:00
|
|
|
TaggedString *g;
|
1998-03-10 00:49:52 +03:00
|
|
|
for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next){
|
|
|
|
LUA_ASSERT(g->constindex >= 0, "userdata in global list");
|
1998-03-06 19:54:42 +03:00
|
|
|
if (g->u.s.globalval.ttype != LUA_T_NIL) {
|
|
|
|
markobject(&g->u.s.globalval);
|
1997-09-26 19:02:26 +04:00
|
|
|
strmark(g); /* cannot collect non nil global variables */
|
|
|
|
}
|
1998-03-10 00:49:52 +03:00
|
|
|
}
|
1997-09-26 19:02:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
static int markobject (TObject *o) {
|
1997-09-16 23:25:59 +04:00
|
|
|
switch (ttype(o)) {
|
|
|
|
case LUA_T_USERDATA: case LUA_T_STRING:
|
|
|
|
strmark(tsvalue(o));
|
|
|
|
break;
|
|
|
|
case LUA_T_ARRAY:
|
|
|
|
hashmark(avalue(o));
|
|
|
|
break;
|
1997-12-15 19:17:20 +03:00
|
|
|
case LUA_T_CLOSURE: case LUA_T_CLMARK:
|
|
|
|
closuremark(o->value.cl);
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
1997-12-15 19:17:20 +03:00
|
|
|
case LUA_T_PROTO: case LUA_T_PMARK:
|
1997-09-16 23:25:59 +04:00
|
|
|
protomark(o->value.tf);
|
|
|
|
break;
|
1997-10-24 21:17:24 +04:00
|
|
|
default: break; /* numbers, cprotos, etc */
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
static void markall (void) {
|
1997-09-16 23:25:59 +04:00
|
|
|
luaD_travstack(markobject); /* mark stack objects */
|
1997-09-26 19:02:26 +04:00
|
|
|
globalmark(); /* mark global variable values and names */
|
1997-09-16 23:25:59 +04:00
|
|
|
travlock(); /* mark locked objects */
|
|
|
|
luaT_travtagmethods(markobject); /* mark fallbacks */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
long lua_collectgarbage (long limit) {
|
1997-11-19 20:29:23 +03:00
|
|
|
unsigned long recovered = L->nblocks; /* to subtract nblocks after gc */
|
1997-09-16 23:25:59 +04:00
|
|
|
Hash *freetable;
|
|
|
|
TaggedString *freestr;
|
|
|
|
TProtoFunc *freefunc;
|
|
|
|
Closure *freeclos;
|
|
|
|
markall();
|
|
|
|
invalidaterefs();
|
|
|
|
freestr = luaS_collector();
|
1997-11-19 20:29:23 +03:00
|
|
|
freetable = (Hash *)listcollect(&(L->roottable));
|
|
|
|
freefunc = (TProtoFunc *)listcollect(&(L->rootproto));
|
|
|
|
freeclos = (Closure *)listcollect(&(L->rootcl));
|
|
|
|
L->GCthreshold *= 4; /* to avoid GC during GC */
|
1997-12-01 23:31:25 +03:00
|
|
|
luaC_hashcallIM(freetable); /* GC tag methods for tables */
|
|
|
|
luaC_strcallIM(freestr); /* GC tag methods for userdata */
|
1997-11-03 23:45:23 +03:00
|
|
|
luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
|
1997-09-16 23:25:59 +04:00
|
|
|
luaH_free(freetable);
|
|
|
|
luaS_free(freestr);
|
|
|
|
luaF_freeproto(freefunc);
|
|
|
|
luaF_freeclosure(freeclos);
|
1997-11-19 20:29:23 +03:00
|
|
|
recovered = recovered-L->nblocks;
|
|
|
|
L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
|
1997-09-16 23:25:59 +04:00
|
|
|
return recovered;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
void luaC_checkGC (void) {
|
1997-11-19 20:29:23 +03:00
|
|
|
if (L->nblocks >= L->GCthreshold)
|
1997-09-16 23:25:59 +04:00
|
|
|
lua_collectgarbage(0);
|
|
|
|
}
|
|
|
|
|