1997-09-16 23:25:59 +04:00
|
|
|
/*
|
1999-11-26 21:59:20 +03:00
|
|
|
** $Id: lgc.c,v 1.33 1999/11/23 13:58:02 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Garbage Collector
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
#define LUA_REENTRANT
|
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"
|
1999-10-04 21:51:04 +04:00
|
|
|
#include "lref.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"
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static int markobject (lua_State *L, TObject *o);
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
1999-10-04 21:51:04 +04:00
|
|
|
/* mark a string; marks bigger than 1 cannot be changed */
|
1999-11-22 16:12:07 +03:00
|
|
|
#define strmark(L, s) {if ((s)->marked == 0) (s)->marked = 1;}
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static void protomark (lua_State *L, TProtoFunc *f) {
|
1999-10-04 21:51:04 +04:00
|
|
|
if (!f->marked) {
|
1997-09-16 23:25:59 +04:00
|
|
|
int i;
|
1999-10-04 21:51:04 +04:00
|
|
|
f->marked = 1;
|
1999-11-22 16:12:07 +03:00
|
|
|
strmark(L, f->source);
|
1999-09-27 22:00:25 +04:00
|
|
|
for (i=f->nconsts-1; i>=0; i--)
|
1999-11-22 16:12:07 +03:00
|
|
|
markobject(L, &f->consts[i]);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static void closuremark (lua_State *L, Closure *f) {
|
1999-10-04 21:51:04 +04:00
|
|
|
if (!f->marked) {
|
1997-09-16 23:25:59 +04:00
|
|
|
int i;
|
1999-10-04 21:51:04 +04:00
|
|
|
f->marked = 1;
|
1997-10-16 14:59:34 +04:00
|
|
|
for (i=f->nelems; i>=0; i--)
|
1999-11-22 16:12:07 +03:00
|
|
|
markobject(L, &f->consts[i]);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static void hashmark (lua_State *L, Hash *h) {
|
1999-10-04 21:51:04 +04:00
|
|
|
if (!h->marked) {
|
1997-09-16 23:25:59 +04:00
|
|
|
int i;
|
1999-10-04 21:51:04 +04:00
|
|
|
h->marked = 1;
|
1999-10-14 23:13:31 +04:00
|
|
|
for (i=h->size-1; i>=0; i--) {
|
1999-11-23 16:58:02 +03:00
|
|
|
Node *n = node(h,i);
|
|
|
|
if (ttype(key(n)) != LUA_T_NIL) {
|
1999-11-22 16:12:07 +03:00
|
|
|
markobject(L, &n->key);
|
|
|
|
markobject(L, &n->val);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static void travglobal (lua_State *L) {
|
1999-11-04 20:23:12 +03:00
|
|
|
GlobalVar *gv;
|
|
|
|
for (gv=L->rootglobal; gv; gv=gv->next) {
|
1999-11-22 16:12:07 +03:00
|
|
|
LUA_ASSERT(L, gv->name->u.s.gv == gv, "inconsistent global name");
|
1999-11-04 20:23:12 +03:00
|
|
|
if (gv->value.ttype != LUA_T_NIL) {
|
1999-11-22 16:12:07 +03:00
|
|
|
strmark(L, gv->name); /* cannot collect non nil global variables */
|
|
|
|
markobject(L, &gv->value);
|
1997-09-26 19:02:26 +04:00
|
|
|
}
|
1998-03-10 00:49:52 +03:00
|
|
|
}
|
1997-09-26 19:02:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static void travstack (lua_State *L) {
|
1999-10-04 21:51:04 +04:00
|
|
|
StkId i;
|
|
|
|
for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
|
1999-11-22 16:12:07 +03:00
|
|
|
markobject(L, L->stack.stack+i);
|
1999-10-04 21:51:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static void travlock (lua_State *L) {
|
1999-10-04 21:51:04 +04:00
|
|
|
int i;
|
1999-11-10 18:40:46 +03:00
|
|
|
for (i=0; i<L->refSize; i++) {
|
|
|
|
if (L->refArray[i].st == LOCK)
|
1999-11-22 16:12:07 +03:00
|
|
|
markobject(L, &L->refArray[i].o);
|
1999-11-10 18:40:46 +03:00
|
|
|
}
|
1999-10-04 21:51:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static int markobject (lua_State *L, TObject *o) {
|
1997-09-16 23:25:59 +04:00
|
|
|
switch (ttype(o)) {
|
|
|
|
case LUA_T_USERDATA: case LUA_T_STRING:
|
1999-11-22 16:12:07 +03:00
|
|
|
strmark(L, tsvalue(o));
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
|
|
|
case LUA_T_ARRAY:
|
1999-11-22 16:12:07 +03:00
|
|
|
hashmark(L, avalue(o));
|
1997-09-16 23:25:59 +04:00
|
|
|
break;
|
1997-12-15 19:17:20 +03:00
|
|
|
case LUA_T_CLOSURE: case LUA_T_CLMARK:
|
1999-11-22 16:12:07 +03:00
|
|
|
closuremark(L, 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:
|
1999-11-22 16:12:07 +03:00
|
|
|
protomark(L, o->value.tf);
|
1997-09-16 23:25:59 +04:00
|
|
|
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-11-22 16:12:07 +03:00
|
|
|
static void collectproto (lua_State *L) {
|
1999-10-04 21:51:04 +04:00
|
|
|
TProtoFunc **p = &L->rootproto;
|
|
|
|
TProtoFunc *next;
|
|
|
|
while ((next = *p) != NULL) {
|
|
|
|
if (next->marked) {
|
|
|
|
next->marked = 0;
|
|
|
|
p = &next->next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*p = next->next;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaF_freeproto(L, next);
|
1999-10-04 21:51:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static void collectclosure (lua_State *L) {
|
1999-10-04 21:51:04 +04:00
|
|
|
Closure **p = &L->rootcl;
|
|
|
|
Closure *next;
|
|
|
|
while ((next = *p) != NULL) {
|
|
|
|
if (next->marked) {
|
|
|
|
next->marked = 0;
|
|
|
|
p = &next->next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*p = next->next;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaF_freeclosure(L, next);
|
1999-10-04 21:51:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static void collecttable (lua_State *L) {
|
1999-10-04 21:51:04 +04:00
|
|
|
Hash **p = &L->roottable;
|
|
|
|
Hash *next;
|
|
|
|
while ((next = *p) != NULL) {
|
|
|
|
if (next->marked) {
|
|
|
|
next->marked = 0;
|
|
|
|
p = &next->next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*p = next->next;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaH_free(L, next);
|
1999-10-04 21:51:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-04 20:23:12 +03:00
|
|
|
/*
|
|
|
|
** remove from the global list globals whose names will be collected
|
|
|
|
** (the global itself is freed when its name is freed)
|
|
|
|
*/
|
1999-11-22 16:12:07 +03:00
|
|
|
static void clear_global_list (lua_State *L, int limit) {
|
1999-11-04 20:23:12 +03:00
|
|
|
GlobalVar **p = &L->rootglobal;
|
|
|
|
GlobalVar *next;
|
1999-10-04 21:51:04 +04:00
|
|
|
while ((next = *p) != NULL) {
|
1999-11-04 20:23:12 +03:00
|
|
|
if (next->name->marked >= limit) p = &next->next;
|
|
|
|
else *p = next->next;
|
1999-10-04 21:51:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** collect all elements with `marked' < `limit'.
|
|
|
|
** with limit=1, that means all unmarked elements;
|
1999-11-22 16:12:07 +03:00
|
|
|
** with limit=MAX_INT, that means all elements.
|
1999-10-04 21:51:04 +04:00
|
|
|
*/
|
1999-11-22 16:12:07 +03:00
|
|
|
static void collectstring (lua_State *L, int limit) {
|
1999-10-04 21:51:04 +04:00
|
|
|
TObject o; /* to call userdata 'gc' tag method */
|
|
|
|
int i;
|
|
|
|
ttype(&o) = LUA_T_USERDATA;
|
1999-11-22 16:12:07 +03:00
|
|
|
clear_global_list(L, limit);
|
1999-10-11 20:13:42 +04:00
|
|
|
for (i=0; i<NUM_HASHS; i++) { /* for each hash table */
|
1999-10-04 21:51:04 +04:00
|
|
|
stringtable *tb = &L->string_root[i];
|
|
|
|
int j;
|
1999-10-11 20:13:42 +04:00
|
|
|
for (j=0; j<tb->size; j++) { /* for each list */
|
|
|
|
TaggedString **p = &tb->hash[j];
|
|
|
|
TaggedString *next;
|
|
|
|
while ((next = *p) != NULL) {
|
|
|
|
if (next->marked >= limit) {
|
|
|
|
if (next->marked < FIXMARK) /* does not change FIXMARKs */
|
|
|
|
next->marked = 0;
|
|
|
|
p = &next->nexthash;
|
|
|
|
}
|
|
|
|
else { /* collect */
|
|
|
|
if (next->constindex == -1) { /* is userdata? */
|
|
|
|
tsvalue(&o) = next;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaD_gcIM(L, &o);
|
1999-10-11 20:13:42 +04:00
|
|
|
}
|
|
|
|
*p = next->nexthash;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaS_free(L, next);
|
1999-10-11 20:13:42 +04:00
|
|
|
tb->nuse--;
|
1999-10-04 21:51:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-10-14 23:13:31 +04:00
|
|
|
if ((tb->nuse+1)*6 < tb->size)
|
1999-11-26 21:59:20 +03:00
|
|
|
luaS_resize(L, tb, tb->size/2); /* table is too big */
|
1999-10-04 21:51:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef LUA_COMPAT_GC
|
1999-11-22 16:12:07 +03:00
|
|
|
static void tableTM (lua_State *L) {
|
1999-10-04 21:51:04 +04:00
|
|
|
Hash *p;
|
|
|
|
TObject o;
|
|
|
|
ttype(&o) = LUA_T_ARRAY;
|
|
|
|
for (p = L->roottable; p; p = p->next) {
|
|
|
|
if (!p->marked) {
|
|
|
|
avalue(&o) = p;
|
1999-11-22 16:12:07 +03:00
|
|
|
luaD_gcIM(L, &o);
|
1999-10-04 21:51:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
1999-11-22 16:12:07 +03:00
|
|
|
#define tableTM(L) /* do nothing */
|
1999-10-04 21:51:04 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static void markall (lua_State *L) {
|
|
|
|
travstack(L); /* mark stack objects */
|
|
|
|
travglobal(L); /* mark global variable values and names */
|
|
|
|
travlock(L); /* mark locked objects */
|
|
|
|
luaT_travtagmethods(L, markobject); /* mark tag methods */
|
1999-10-04 21:51:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void luaC_collect (lua_State *L, int all) {
|
1999-10-04 21:51:04 +04:00
|
|
|
L->GCthreshold *= 4; /* to avoid GC during GC */
|
1999-11-22 16:12:07 +03:00
|
|
|
tableTM(L); /* call TM for tables (if LUA_COMPAT_GC) */
|
|
|
|
collecttable(L);
|
|
|
|
collectstring(L, all?MAX_INT:1);
|
|
|
|
collectproto(L);
|
|
|
|
collectclosure(L);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
long lua_collectgarbage (lua_State *L, long limit) {
|
1997-11-19 20:29:23 +03:00
|
|
|
unsigned long recovered = L->nblocks; /* to subtract nblocks after gc */
|
1999-11-22 16:12:07 +03:00
|
|
|
markall(L);
|
|
|
|
luaR_invalidaterefs(L);
|
|
|
|
luaC_collect(L, 0);
|
|
|
|
luaD_gcIM(L, &luaO_nilobject); /* GC tag method for nil (signal end of GC) */
|
1999-10-04 21:51:04 +04:00
|
|
|
recovered = recovered - L->nblocks;
|
1997-11-19 20:29:23 +03:00
|
|
|
L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
|
1999-11-10 18:40:46 +03:00
|
|
|
if (L->Mbuffsize > L->Mbuffnext*4) { /* is buffer too big? */
|
|
|
|
L->Mbuffsize /= 2; /* still larger than Mbuffnext*2 */
|
1999-11-22 16:12:07 +03:00
|
|
|
luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
|
1999-11-10 18:40:46 +03:00
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
return recovered;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void luaC_checkGC (lua_State *L) {
|
1997-11-19 20:29:23 +03:00
|
|
|
if (L->nblocks >= L->GCthreshold)
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_collectgarbage(L, 0);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|