|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
/*
|
|
|
|
|
** $Id: lgc.c,v 2.215 2016/12/22 13:08:50 roberto Exp $
|
|
|
|
|
** $Id: lgc.c,v 2.215.1.2 2017/08/31 16:15:27 roberto Exp $
|
|
|
|
|
** Garbage Collector
|
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
|
*/
|
|
|
|
@ -9,6 +9,7 @@
|
|
|
|
|
|
|
|
|
|
#include "lprefix.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
@ -24,6 +25,7 @@
|
|
|
|
|
#include "ltable.h"
|
|
|
|
|
#include "ltm.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** internal state for collector while inside the atomic phase. The
|
|
|
|
|
** collector should never be in this state while running regular code.
|
|
|
|
@ -42,72 +44,75 @@
|
|
|
|
|
/* cost of calling one finalizer */
|
|
|
|
|
#define GCFINALIZECOST GCSWEEPCOST
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** macro to adjust 'stepmul': 'stepmul' is actually used like
|
|
|
|
|
** 'stepmul / STEPMULADJ' (value chosen by tests)
|
|
|
|
|
*/
|
|
|
|
|
#define STEPMULADJ 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** macro to adjust 'pause': 'pause' is actually used like
|
|
|
|
|
** 'pause / PAUSEADJ' (value chosen by tests)
|
|
|
|
|
*/
|
|
|
|
|
#define PAUSEADJ 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** 'makewhite' erases all color bits then sets only the current white
|
|
|
|
|
** bit
|
|
|
|
|
*/
|
|
|
|
|
#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS))
|
|
|
|
|
#define makewhite(g, x) (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g)))
|
|
|
|
|
#define makewhite(g,x) \
|
|
|
|
|
(x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g)))
|
|
|
|
|
|
|
|
|
|
#define white2gray(x) resetbits(x->marked, WHITEBITS)
|
|
|
|
|
#define black2gray(x) resetbit(x->marked, BLACKBIT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
|
|
|
|
|
|
|
|
|
|
#define checkdeadkey(n) lua_assert(!ttisdeadkey(gkey(n)) || ttisnil(gval(n)))
|
|
|
|
|
|
|
|
|
|
#define checkconsistency(obj) lua_longassert(!iscollectable(obj) || righttt(obj))
|
|
|
|
|
|
|
|
|
|
#define markvalue(g, o) \
|
|
|
|
|
{ \
|
|
|
|
|
checkconsistency(o); \
|
|
|
|
|
if (valiswhite(o)) reallymarkobject(g, gcvalue(o)); \
|
|
|
|
|
}
|
|
|
|
|
#define checkconsistency(obj) \
|
|
|
|
|
lua_longassert(!iscollectable(obj) || righttt(obj))
|
|
|
|
|
|
|
|
|
|
#define markobject(g, t) \
|
|
|
|
|
{ \
|
|
|
|
|
if (iswhite(t)) reallymarkobject(g, obj2gco(t)); \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define markvalue(g,o) { checkconsistency(o); \
|
|
|
|
|
if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
|
|
|
|
|
|
|
|
|
|
#define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); }
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** mark an object that can be NULL (either because it is really optional,
|
|
|
|
|
** or it was stripped as debug info, or inside an uncompleted structure)
|
|
|
|
|
*/
|
|
|
|
|
#define markobjectN(g, t) \
|
|
|
|
|
{ \
|
|
|
|
|
if (t) markobject(g, t); \
|
|
|
|
|
}
|
|
|
|
|
#define markobjectN(g,t) { if (t) markobject(g,t); }
|
|
|
|
|
|
|
|
|
|
static void reallymarkobject (global_State *g, GCObject *o);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** {======================================================
|
|
|
|
|
** Generic functions
|
|
|
|
|
** =======================================================
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** one after last element in a hash array
|
|
|
|
|
*/
|
|
|
|
|
#define gnodelast(h) gnode(h, cast(size_t, sizenode(h)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** link collectable object 'o' into list pointed by 'p'
|
|
|
|
|
*/
|
|
|
|
|
#define linkgclist(o,p) ((o)->gclist = (p), (p) = obj2gco(o))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** If key is not marked, mark its entry as dead. This allows key to be
|
|
|
|
|
** collected, but keeps its entry in the table. A dead node is needed
|
|
|
|
@ -119,9 +124,11 @@ static void reallymarkobject(global_State *g, GCObject *o);
|
|
|
|
|
*/
|
|
|
|
|
static void removeentry (Node *n) {
|
|
|
|
|
lua_assert(ttisnil(gval(n)));
|
|
|
|
|
if (valiswhite(gkey(n))) setdeadvalue(wgkey(n)); /* unused and unmarked key; remove it */
|
|
|
|
|
if (valiswhite(gkey(n)))
|
|
|
|
|
setdeadvalue(wgkey(n)); /* unused and unmarked key; remove it */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** tells whether a key or value can be cleared from a weak
|
|
|
|
|
** table. Non-collectable objects are never removed from weak
|
|
|
|
@ -130,14 +137,12 @@ static void removeentry(Node *n) {
|
|
|
|
|
** being finalized, keep them in keys, but not in values
|
|
|
|
|
*/
|
|
|
|
|
static int iscleared (global_State *g, const TValue *o) {
|
|
|
|
|
if (!iscollectable(o))
|
|
|
|
|
return 0;
|
|
|
|
|
if (!iscollectable(o)) return 0;
|
|
|
|
|
else if (ttisstring(o)) {
|
|
|
|
|
markobject(g, tsvalue(o)); /* strings are 'values', so are never weak */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return iswhite(gcvalue(o));
|
|
|
|
|
else return iswhite(gcvalue(o));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@ -251,9 +256,11 @@ void luaC_upvalbarrier_(lua_State *L, UpVal *uv) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
GCObject *o = gcvalue(uv->v);
|
|
|
|
|
lua_assert(!upisopen(uv)); /* ensured by macro luaC_upvalbarrier */
|
|
|
|
|
if (keepinvariant(g)) markobject(g, o);
|
|
|
|
|
if (keepinvariant(g))
|
|
|
|
|
markobject(g, o);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void luaC_fix (lua_State *L, GCObject *o) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */
|
|
|
|
@ -263,6 +270,7 @@ void luaC_fix(lua_State *L, GCObject *o) {
|
|
|
|
|
g->fixedgc = o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** create a new collectable object (with given type and size) and link
|
|
|
|
|
** it to 'allgc' list.
|
|
|
|
@ -271,7 +279,7 @@ GCObject *luaC_newobj(lua_State *L, int tt, size_t sz) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz));
|
|
|
|
|
o->marked = luaC_white(g);
|
|
|
|
|
lua_assert(tt <= 127); /* Must fit in a byte */
|
|
|
|
|
lua_assert(tt <= 127); /* RAVI: Must fit in a byte */
|
|
|
|
|
o->tt = tt;
|
|
|
|
|
o->next = g->allgc;
|
|
|
|
|
g->allgc = o;
|
|
|
|
@ -280,12 +288,15 @@ GCObject *luaC_newobj(lua_State *L, int tt, size_t sz) {
|
|
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** {======================================================
|
|
|
|
|
** Mark functions
|
|
|
|
|
** =======================================================
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** mark an object. Userdata, strings, and closed upvalues are visited
|
|
|
|
|
** and turned black here. Other objects (functions, tables, threads, protos)
|
|
|
|
@ -327,6 +338,7 @@ reentry:
|
|
|
|
|
linkgclist(gco2ccl(o), g->gray);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* RAVI changes */
|
|
|
|
|
case RAVI_TIARRAY:
|
|
|
|
|
case RAVI_TFARRAY:
|
|
|
|
|
case LUA_TTABLE: {
|
|
|
|
@ -345,22 +357,27 @@ reentry:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** mark metamethods for basic types
|
|
|
|
|
*/
|
|
|
|
|
static void markmt (global_State *g) {
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < LUA_NUMTAGS; i++) markobjectN(g, g->mt[i]);
|
|
|
|
|
for (i=0; i < LUA_NUMTAGS; i++)
|
|
|
|
|
markobjectN(g, g->mt[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** mark all objects in list of being-finalized
|
|
|
|
|
*/
|
|
|
|
|
static void markbeingfnz (global_State *g) {
|
|
|
|
|
GCObject *o;
|
|
|
|
|
for (o = g->tobefnz; o != NULL; o = o->next) markobject(g, o);
|
|
|
|
|
for (o = g->tobefnz; o != NULL; o = o->next)
|
|
|
|
|
markobject(g, o);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Mark all values stored in marked open upvalues from non-marked threads.
|
|
|
|
|
** (Values from marked threads were already marked when traversing the
|
|
|
|
@ -388,6 +405,7 @@ static void remarkupvals(global_State *g) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** mark root set and reset all gray lists, to start a new collection
|
|
|
|
|
*/
|
|
|
|
@ -402,6 +420,7 @@ static void restartcollection(global_State *g) {
|
|
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** {======================================================
|
|
|
|
|
** Traverse functions
|
|
|
|
@ -436,6 +455,7 @@ static void traverseweakvalue(global_State *g, Table *h) {
|
|
|
|
|
linkgclist(h, g->weak); /* has to be cleared later */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Traverse an ephemeron table and link it to proper list. Returns true
|
|
|
|
|
** iff any object was marked during this traversal (which implies that
|
|
|
|
@ -484,6 +504,7 @@ static int traverseephemeron(global_State *g, Table *h) {
|
|
|
|
|
return marked;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void traversestrongtable (global_State *g, Table *h) {
|
|
|
|
|
Node *n, *limit = gnodelast(h);
|
|
|
|
|
unsigned int i;
|
|
|
|
@ -501,12 +522,14 @@ static void traversestrongtable(global_State *g, Table *h) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static lu_mem traversetable (global_State *g, Table *h) {
|
|
|
|
|
const char *weakkey, *weakvalue;
|
|
|
|
|
const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
|
|
|
|
|
markobjectN(g, h->metatable);
|
|
|
|
|
if (mode && ttisstring(mode) && /* is there a weak mode? */
|
|
|
|
|
((weakkey = strchr(svalue(mode), 'k')), (weakvalue = strchr(svalue(mode), 'v')),
|
|
|
|
|
((weakkey = strchr(svalue(mode), 'k')),
|
|
|
|
|
(weakvalue = strchr(svalue(mode), 'v')),
|
|
|
|
|
(weakkey || weakvalue))) { /* is really weak? */
|
|
|
|
|
black2gray(h); /* keep table gray */
|
|
|
|
|
if (!weakkey) /* strong keys? */
|
|
|
|
@ -518,9 +541,11 @@ static lu_mem traversetable(global_State *g, Table *h) {
|
|
|
|
|
}
|
|
|
|
|
else /* not weak */
|
|
|
|
|
traversestrongtable(g, h);
|
|
|
|
|
return sizeof(Table) + sizeof(TValue) * h->sizearray + sizeof(Node) * cast(size_t, allocsizenode(h));
|
|
|
|
|
return sizeof(Table) + sizeof(TValue) * h->sizearray +
|
|
|
|
|
sizeof(Node) * cast(size_t, allocsizenode(h));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Traverse a prototype. (While a prototype is being build, its
|
|
|
|
|
** arrays can be larger than needed; the extra slots are filled with
|
|
|
|
@ -528,7 +553,8 @@ static lu_mem traversetable(global_State *g, Table *h) {
|
|
|
|
|
*/
|
|
|
|
|
static int traverseproto (global_State *g, Proto *f) {
|
|
|
|
|
int i;
|
|
|
|
|
if (f->cache && iswhite(f->cache)) f->cache = NULL; /* allow cache to be collected */
|
|
|
|
|
if (f->cache && iswhite(f->cache))
|
|
|
|
|
f->cache = NULL; /* allow cache to be collected */
|
|
|
|
|
markobjectN(g, f->source);
|
|
|
|
|
for (i = 0; i < f->sizek; i++) /* mark literals */
|
|
|
|
|
markvalue(g, &f->k[i]);
|
|
|
|
@ -542,10 +568,15 @@ static int traverseproto(global_State *g, Proto *f) {
|
|
|
|
|
markobjectN(g, f->locvars[i].varname);
|
|
|
|
|
markobjectN(g, f->locvars[i].usertype); /* RAVI change */
|
|
|
|
|
}
|
|
|
|
|
return sizeof(Proto) + sizeof(Instruction) * f->sizecode + sizeof(Proto *) * f->sizep + sizeof(TValue) * f->sizek +
|
|
|
|
|
sizeof(int) * f->sizelineinfo + sizeof(LocVar) * f->sizelocvars + sizeof(Upvaldesc) * f->sizeupvalues;
|
|
|
|
|
return sizeof(Proto) + sizeof(Instruction) * f->sizecode +
|
|
|
|
|
sizeof(Proto *) * f->sizep +
|
|
|
|
|
sizeof(TValue) * f->sizek +
|
|
|
|
|
sizeof(int) * f->sizelineinfo +
|
|
|
|
|
sizeof(LocVar) * f->sizelocvars +
|
|
|
|
|
sizeof(Upvaldesc) * f->sizeupvalues;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static lu_mem traverseCclosure (global_State *g, CClosure *cl) {
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
|
|
|
|
@ -574,10 +605,13 @@ static lu_mem traverseLclosure(global_State *g, LClosure *cl) {
|
|
|
|
|
return sizeLclosure(cl->nupvalues);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static lu_mem traversethread (global_State *g, lua_State *th) {
|
|
|
|
|
StkId o = th->stack;
|
|
|
|
|
if (o == NULL) return 1; /* stack not completely built yet */
|
|
|
|
|
lua_assert(g->gcstate == GCSinsideatomic || th->openupval == NULL || isintwups(th));
|
|
|
|
|
if (o == NULL)
|
|
|
|
|
return 1; /* stack not completely built yet */
|
|
|
|
|
lua_assert(g->gcstate == GCSinsideatomic ||
|
|
|
|
|
th->openupval == NULL || isintwups(th));
|
|
|
|
|
for (; o < th->top; o++) /* mark live elements in the stack */
|
|
|
|
|
markvalue(g, o);
|
|
|
|
|
if (g->gcstate == GCSinsideatomic) { /* final traversal? */
|
|
|
|
@ -592,9 +626,11 @@ static lu_mem traversethread(global_State *g, lua_State *th) {
|
|
|
|
|
}
|
|
|
|
|
else if (g->gckind != KGC_EMERGENCY)
|
|
|
|
|
luaD_shrinkstack(th); /* do not change stack in emergency cycle */
|
|
|
|
|
return (sizeof(lua_State) + sizeof(TValue) * th->stacksize + sizeof(CallInfo) * th->nci);
|
|
|
|
|
return (sizeof(lua_State) + sizeof(TValue) * th->stacksize +
|
|
|
|
|
sizeof(CallInfo) * th->nci);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** traverse one gray object, turning it to black (except for threads,
|
|
|
|
|
** which are always gray).
|
|
|
|
@ -605,6 +641,7 @@ static void propagatemark(global_State *g) {
|
|
|
|
|
lua_assert(isgray(o));
|
|
|
|
|
gray2black(o);
|
|
|
|
|
switch (o->tt) {
|
|
|
|
|
/* RAVI changes */
|
|
|
|
|
case RAVI_TIARRAY:
|
|
|
|
|
case RAVI_TFARRAY:
|
|
|
|
|
case LUA_TTABLE: {
|
|
|
|
@ -644,10 +681,12 @@ static void propagatemark(global_State *g) {
|
|
|
|
|
g->GCmemtrav += size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void propagateall (global_State *g) {
|
|
|
|
|
while (g->gray) propagatemark(g);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void convergeephemerons (global_State *g) {
|
|
|
|
|
int changed;
|
|
|
|
|
do {
|
|
|
|
@ -667,12 +706,14 @@ static void convergeephemerons(global_State *g) {
|
|
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** {======================================================
|
|
|
|
|
** Sweep Functions
|
|
|
|
|
** =======================================================
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** clear entries with unmarked keys from all weaktables in list 'l' up
|
|
|
|
|
** to element 'f'
|
|
|
|
@ -684,12 +725,14 @@ static void clearkeys(global_State *g, GCObject *l, GCObject *f) {
|
|
|
|
|
for (n = gnode(h, 0); n < limit; n++) {
|
|
|
|
|
if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) {
|
|
|
|
|
setnilvalue(gval(n)); /* remove value ... */
|
|
|
|
|
removeentry(n); /* and remove entry from table */
|
|
|
|
|
}
|
|
|
|
|
if (ttisnil(gval(n))) /* is entry empty? */
|
|
|
|
|
removeentry(n); /* remove entry from table */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** clear entries with unmarked values from all weaktables in list 'l' up
|
|
|
|
|
** to element 'f'
|
|
|
|
@ -713,21 +756,26 @@ static void clearvalues(global_State *g, GCObject *l, GCObject *f) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void luaC_upvdeccount (lua_State *L, UpVal *uv) {
|
|
|
|
|
lua_assert(uv->refcount > 0);
|
|
|
|
|
uv->refcount--;
|
|
|
|
|
if (uv->refcount == 0 && !upisopen(uv)) luaM_free(L, uv);
|
|
|
|
|
if (uv->refcount == 0 && !upisopen(uv))
|
|
|
|
|
luaM_free(L, uv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void freeLclosure (lua_State *L, LClosure *cl) {
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < cl->nupvalues; i++) {
|
|
|
|
|
UpVal *uv = cl->upvals[i];
|
|
|
|
|
if (uv) luaC_upvdeccount(L, uv);
|
|
|
|
|
if (uv)
|
|
|
|
|
luaC_upvdeccount(L, uv);
|
|
|
|
|
}
|
|
|
|
|
luaM_freemem(L, cl, sizeLclosure(cl->nupvalues));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void freeobj (lua_State *L, GCObject *o) {
|
|
|
|
|
switch (o->tt) {
|
|
|
|
|
case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
|
|
|
|
@ -739,6 +787,7 @@ static void freeobj(lua_State *L, GCObject *o) {
|
|
|
|
|
luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* RAVI changes */
|
|
|
|
|
case RAVI_TFARRAY:
|
|
|
|
|
case RAVI_TIARRAY:
|
|
|
|
|
case LUA_TTABLE: luaH_free(L, gco2t(o)); break;
|
|
|
|
@ -756,9 +805,11 @@ static void freeobj(lua_State *L, GCObject *o) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM)
|
|
|
|
|
static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** sweep at most 'count' elements from a list of GCObjects erasing dead
|
|
|
|
|
** objects, where a dead object is one marked with the old (non current)
|
|
|
|
@ -785,17 +836,21 @@ static GCObject **sweeplist(lua_State *L, GCObject **p, lu_mem count) {
|
|
|
|
|
return (*p == NULL) ? NULL : p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** sweep a list until a live object (or end of list)
|
|
|
|
|
*/
|
|
|
|
|
static GCObject **sweeptolive (lua_State *L, GCObject **p) {
|
|
|
|
|
GCObject **old = p;
|
|
|
|
|
do { p = sweeplist(L, p, 1); } while (p == old);
|
|
|
|
|
do {
|
|
|
|
|
p = sweeplist(L, p, 1);
|
|
|
|
|
} while (p == old);
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** {======================================================
|
|
|
|
|
** Finalization
|
|
|
|
@ -814,6 +869,7 @@ static void checkSizes(lua_State *L, global_State *g) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static GCObject *udata2finalize (global_State *g) {
|
|
|
|
|
GCObject *o = g->tobefnz; /* get first element */
|
|
|
|
|
lua_assert(tofinalize(o));
|
|
|
|
@ -821,15 +877,18 @@ static GCObject *udata2finalize(global_State *g) {
|
|
|
|
|
o->next = g->allgc; /* return it to 'allgc' list */
|
|
|
|
|
g->allgc = o;
|
|
|
|
|
resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */
|
|
|
|
|
if (issweepphase(g)) makewhite(g, o); /* "sweep" object */
|
|
|
|
|
if (issweepphase(g))
|
|
|
|
|
makewhite(g, o); /* "sweep" object */
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void dothecall (lua_State *L, void *ud) {
|
|
|
|
|
UNUSED(ud);
|
|
|
|
|
luaD_callnoyield(L, L->top - 2, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void GCTM (lua_State *L, int propagateerrors) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
const TValue *tm;
|
|
|
|
@ -852,7 +911,9 @@ static void GCTM(lua_State *L, int propagateerrors) {
|
|
|
|
|
g->gcrunning = running; /* restore state */
|
|
|
|
|
if (status != LUA_OK && propagateerrors) { /* error while running __gc? */
|
|
|
|
|
if (status == LUA_ERRRUN) { /* is there an error object? */
|
|
|
|
|
const char *msg = (ttisstring(L->top - 1)) ? svalue(L->top - 1) : "no message";
|
|
|
|
|
const char *msg = (ttisstring(L->top - 1))
|
|
|
|
|
? svalue(L->top - 1)
|
|
|
|
|
: "no message";
|
|
|
|
|
luaO_pushfstring(L, "error in __gc metamethod (%s)", msg);
|
|
|
|
|
status = LUA_ERRGCMM; /* error in __gc metamethod */
|
|
|
|
|
}
|
|
|
|
@ -861,6 +922,7 @@ static void GCTM(lua_State *L, int propagateerrors) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** call a few (up to 'g->gcfinnum') finalizers
|
|
|
|
|
*/
|
|
|
|
@ -868,28 +930,34 @@ static int runafewfinalizers(lua_State *L) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
unsigned int i;
|
|
|
|
|
lua_assert(!g->tobefnz || g->gcfinnum > 0);
|
|
|
|
|
for (i = 0; g->tobefnz && i < g->gcfinnum; i++) GCTM(L, 1); /* call one finalizer */
|
|
|
|
|
for (i = 0; g->tobefnz && i < g->gcfinnum; i++)
|
|
|
|
|
GCTM(L, 1); /* call one finalizer */
|
|
|
|
|
g->gcfinnum = (!g->tobefnz) ? 0 /* nothing more to finalize? */
|
|
|
|
|
: g->gcfinnum * 2; /* else call a few more next time */
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** call all pending finalizers
|
|
|
|
|
*/
|
|
|
|
|
static void callallpendingfinalizers (lua_State *L) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
while (g->tobefnz) GCTM(L, 0);
|
|
|
|
|
while (g->tobefnz)
|
|
|
|
|
GCTM(L, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** find last 'next' field in list 'p' list (to add elements in its end)
|
|
|
|
|
*/
|
|
|
|
|
static GCObject **findlast (GCObject **p) {
|
|
|
|
|
while (*p != NULL) p = &(*p)->next;
|
|
|
|
|
while (*p != NULL)
|
|
|
|
|
p = &(*p)->next;
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** move all unreachable objects (or 'all' objects) that need
|
|
|
|
|
** finalization from list 'finobj' to list 'tobefnz' (to be finalized)
|
|
|
|
@ -911,6 +979,7 @@ static void separatetobefnz(global_State *g, int all) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** if object 'o' has a finalizer, remove it from 'allgc' list (must
|
|
|
|
|
** search the list to find it) and link it in 'finobj' list.
|
|
|
|
@ -928,8 +997,7 @@ void luaC_checkfinalizer(lua_State *L, GCObject *o, Table *mt) {
|
|
|
|
|
g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */
|
|
|
|
|
}
|
|
|
|
|
/* search for pointer pointing to 'o' */
|
|
|
|
|
for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */
|
|
|
|
|
}
|
|
|
|
|
for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ }
|
|
|
|
|
*p = o->next; /* remove 'o' from 'allgc' list */
|
|
|
|
|
o->next = g->finobj; /* link it in 'finobj' list */
|
|
|
|
|
g->finobj = o;
|
|
|
|
@ -939,12 +1007,15 @@ void luaC_checkfinalizer(lua_State *L, GCObject *o, Table *mt) {
|
|
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** {======================================================
|
|
|
|
|
** GC control
|
|
|
|
|
** =======================================================
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Set a reasonable "time" to wait before starting a new GC cycle; cycle
|
|
|
|
|
** will start when memory use hits threshold. (Division by 'estimate'
|
|
|
|
@ -962,6 +1033,7 @@ static void setpause(global_State *g) {
|
|
|
|
|
luaE_setdebt(g, debt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Enter first sweep phase.
|
|
|
|
|
** The call to 'sweeplist' tries to make pointer point to an object
|
|
|
|
@ -976,6 +1048,7 @@ static void entersweep(lua_State *L) {
|
|
|
|
|
g->sweepgc = sweeplist(L, &g->allgc, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void luaC_freeallobjects (lua_State *L) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
separatetobefnz(g, 1); /* separate all objects with finalizers */
|
|
|
|
@ -990,6 +1063,7 @@ void luaC_freeallobjects(lua_State *L) {
|
|
|
|
|
lua_assert(g->strt.nuse == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static l_mem atomic (lua_State *L) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
l_mem work;
|
|
|
|
@ -1015,8 +1089,7 @@ static l_mem atomic(lua_State *L) {
|
|
|
|
|
/* Clear values from weak tables, before checking finalizers */
|
|
|
|
|
clearvalues(g, g->weak, NULL);
|
|
|
|
|
clearvalues(g, g->allweak, NULL);
|
|
|
|
|
origweak = g->weak;
|
|
|
|
|
origall = g->allweak;
|
|
|
|
|
origweak = g->weak; origall = g->allweak;
|
|
|
|
|
work += g->GCmemtrav; /* stop counting (objects being finalized) */
|
|
|
|
|
separatetobefnz(g, 0); /* separate objects to be finalized */
|
|
|
|
|
g->gcfinnum = 1; /* there may be objects to be finalized */
|
|
|
|
@ -1037,7 +1110,9 @@ static l_mem atomic(lua_State *L) {
|
|
|
|
|
return work; /* estimate of memory marked by 'atomic' */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static lu_mem sweepstep(lua_State *L, global_State *g, int nextstate, GCObject **nextlist) {
|
|
|
|
|
|
|
|
|
|
static lu_mem sweepstep (lua_State *L, global_State *g,
|
|
|
|
|
int nextstate, GCObject **nextlist) {
|
|
|
|
|
if (g->sweepgc) {
|
|
|
|
|
l_mem olddebt = g->GCdebt;
|
|
|
|
|
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
|
|
|
|
@ -1051,6 +1126,7 @@ static lu_mem sweepstep(lua_State *L, global_State *g, int nextstate, GCObject *
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static lu_mem singlestep (lua_State *L) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
switch (g->gcstate) {
|
|
|
|
@ -1073,15 +1149,17 @@ static lu_mem singlestep(lua_State *L) {
|
|
|
|
|
propagateall(g); /* make sure gray list is empty */
|
|
|
|
|
work = atomic(L); /* work is what was traversed by 'atomic' */
|
|
|
|
|
entersweep(L);
|
|
|
|
|
g->GCestimate = gettotalbytes(g); /* first estimate */
|
|
|
|
|
;
|
|
|
|
|
g->GCestimate = gettotalbytes(g); /* first estimate */;
|
|
|
|
|
return work;
|
|
|
|
|
}
|
|
|
|
|
case GCSswpallgc: { /* sweep "regular" objects */ return sweepstep(L, g, GCSswpfinobj, &g->finobj);
|
|
|
|
|
case GCSswpallgc: { /* sweep "regular" objects */
|
|
|
|
|
return sweepstep(L, g, GCSswpfinobj, &g->finobj);
|
|
|
|
|
}
|
|
|
|
|
case GCSswpfinobj: { /* sweep objects with finalizers */ return sweepstep(L, g, GCSswptobefnz, &g->tobefnz);
|
|
|
|
|
case GCSswpfinobj: { /* sweep objects with finalizers */
|
|
|
|
|
return sweepstep(L, g, GCSswptobefnz, &g->tobefnz);
|
|
|
|
|
}
|
|
|
|
|
case GCSswptobefnz: { /* sweep objects to be finalized */ return sweepstep(L, g, GCSswpend, NULL);
|
|
|
|
|
case GCSswptobefnz: { /* sweep objects to be finalized */
|
|
|
|
|
return sweepstep(L, g, GCSswpend, NULL);
|
|
|
|
|
}
|
|
|
|
|
case GCSswpend: { /* finish sweeps */
|
|
|
|
|
makewhite(g, g->mainthread); /* sweep main thread */
|
|
|
|
@ -1103,15 +1181,18 @@ static lu_mem singlestep(lua_State *L) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** advances the garbage collector until it reaches a state allowed
|
|
|
|
|
** by 'statemask'
|
|
|
|
|
*/
|
|
|
|
|
void luaC_runtilstate (lua_State *L, int statesmask) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
while (!testbit(statesmask, g->gcstate)) singlestep(L);
|
|
|
|
|
while (!testbit(statesmask, g->gcstate))
|
|
|
|
|
singlestep(L);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** get GC debt and convert it from Kb to 'work units' (avoid zero debt
|
|
|
|
|
** and overflows)
|
|
|
|
@ -1119,8 +1200,7 @@ void luaC_runtilstate(lua_State *L, int statesmask) {
|
|
|
|
|
static l_mem getdebt (global_State *g) {
|
|
|
|
|
l_mem debt = g->GCdebt;
|
|
|
|
|
int stepmul = g->gcstepmul;
|
|
|
|
|
if (debt <= 0)
|
|
|
|
|
return 0; /* minimal debt */
|
|
|
|
|
if (debt <= 0) return 0; /* minimal debt */
|
|
|
|
|
else {
|
|
|
|
|
debt = (debt / STEPMULADJ) + 1;
|
|
|
|
|
debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM;
|
|
|
|
@ -1151,6 +1231,7 @@ void luaC_step(lua_State *L) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Performs a full GC cycle; if 'isemergency', set a flag to avoid
|
|
|
|
|
** some operations which could change the interpreter state in some
|
|
|
|
@ -1179,3 +1260,5 @@ void luaC_fullgc(lua_State *L, int isemergency) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|