|
|
|
@ -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,55 +44,55 @@
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
|
|
static void reallymarkobject(global_State *g, GCObject *o);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** {======================================================
|
|
|
|
@ -98,15 +100,18 @@ static void reallymarkobject(global_State *g, GCObject *o);
|
|
|
|
|
** =======================================================
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** 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))
|
|
|
|
|
#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
|
|
|
|
@ -117,11 +122,13 @@ static void reallymarkobject(global_State *g, GCObject *o);
|
|
|
|
|
** associated nil value is enough to signal that the entry is logically
|
|
|
|
|
** empty.
|
|
|
|
|
*/
|
|
|
|
|
static void removeentry(Node *n) {
|
|
|
|
|
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
|
|
|
|
@ -129,15 +136,13 @@ static void removeentry(Node *n) {
|
|
|
|
|
** other objects: if really collected, cannot keep them; for objects
|
|
|
|
|
** being finalized, keep them in keys, but not in values
|
|
|
|
|
*/
|
|
|
|
|
static int iscleared(global_State *g, const TValue *o) {
|
|
|
|
|
if (!iscollectable(o))
|
|
|
|
|
return 0;
|
|
|
|
|
static int iscleared (global_State *g, const TValue *o) {
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@ -234,7 +239,7 @@ void luaC_barrier_(lua_State *L, GCObject *o, GCObject *v) {
|
|
|
|
|
** table if black must be turned to gray as black objects cannot point
|
|
|
|
|
** to white objects.
|
|
|
|
|
*/
|
|
|
|
|
void luaC_barrierback_(lua_State *L, Table *t) {
|
|
|
|
|
void luaC_barrierback_ (lua_State *L, Table *t) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
lua_assert(isblack(t) && !isdead(g, t));
|
|
|
|
|
black2gray(t); /* make table gray (again) */
|
|
|
|
@ -247,14 +252,16 @@ void luaC_barrierback_(lua_State *L, Table *t) {
|
|
|
|
|
** closures pointing to it. So, we assume that the object being assigned
|
|
|
|
|
** must be marked.
|
|
|
|
|
*/
|
|
|
|
|
void luaC_upvalbarrier_(lua_State *L, UpVal *uv) {
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
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! */
|
|
|
|
|
white2gray(o); /* they will be gray forever */
|
|
|
|
@ -263,15 +270,16 @@ 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.
|
|
|
|
|
*/
|
|
|
|
|
GCObject *luaC_newobj(lua_State *L, int tt, size_t sz) {
|
|
|
|
|
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)
|
|
|
|
@ -293,8 +304,8 @@ GCObject *luaC_newobj(lua_State *L, int tt, size_t sz) {
|
|
|
|
|
** to appropriate list to be visited (and turned black) later. (Open
|
|
|
|
|
** upvalues are already linked in 'headuv' list.)
|
|
|
|
|
*/
|
|
|
|
|
static void reallymarkobject(global_State *g, GCObject *o) {
|
|
|
|
|
reentry:
|
|
|
|
|
static void reallymarkobject (global_State *g, GCObject *o) {
|
|
|
|
|
reentry:
|
|
|
|
|
white2gray(o);
|
|
|
|
|
switch (o->tt) {
|
|
|
|
|
case LUA_TSHRSTR: {
|
|
|
|
@ -327,6 +338,7 @@ reentry:
|
|
|
|
|
linkgclist(gco2ccl(o), g->gray);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* RAVI changes */
|
|
|
|
|
case RAVI_TIARRAY:
|
|
|
|
|
case RAVI_TFARRAY:
|
|
|
|
|
case LUA_TTABLE: {
|
|
|
|
@ -345,29 +357,34 @@ reentry:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** mark metamethods for basic types
|
|
|
|
|
*/
|
|
|
|
|
static void markmt(global_State *g) {
|
|
|
|
|
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) {
|
|
|
|
|
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
|
|
|
|
|
** thread.) Remove from the list threads that no longer have upvalues and
|
|
|
|
|
** not-marked threads.
|
|
|
|
|
*/
|
|
|
|
|
static void remarkupvals(global_State *g) {
|
|
|
|
|
static void remarkupvals (global_State *g) {
|
|
|
|
|
lua_State *thread;
|
|
|
|
|
lua_State **p = &g->twups;
|
|
|
|
|
while ((thread = *p) != NULL) {
|
|
|
|
@ -388,10 +405,11 @@ static void remarkupvals(global_State *g) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** mark root set and reset all gray lists, to start a new collection
|
|
|
|
|
*/
|
|
|
|
|
static void restartcollection(global_State *g) {
|
|
|
|
|
static void restartcollection (global_State *g) {
|
|
|
|
|
g->gray = g->grayagain = NULL;
|
|
|
|
|
g->weak = g->allweak = g->ephemeron = NULL;
|
|
|
|
|
markobject(g, g->mainthread);
|
|
|
|
@ -402,6 +420,7 @@ static void restartcollection(global_State *g) {
|
|
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** {======================================================
|
|
|
|
|
** Traverse functions
|
|
|
|
@ -414,7 +433,7 @@ static void restartcollection(global_State *g) {
|
|
|
|
|
** atomic phase. In the atomic phase, if table has any white value,
|
|
|
|
|
** put it in 'weak' list, to be cleared.
|
|
|
|
|
*/
|
|
|
|
|
static void traverseweakvalue(global_State *g, Table *h) {
|
|
|
|
|
static void traverseweakvalue (global_State *g, Table *h) {
|
|
|
|
|
Node *n, *limit = gnodelast(h);
|
|
|
|
|
/* if there is array part, assume it may have white values (it is not
|
|
|
|
|
worth traversing it now just to check) */
|
|
|
|
@ -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
|
|
|
|
@ -446,7 +466,7 @@ static void traverseweakvalue(global_State *g, Table *h) {
|
|
|
|
|
** black). Otherwise, if it has any white key, table has to be cleared
|
|
|
|
|
** (in the atomic phase).
|
|
|
|
|
*/
|
|
|
|
|
static int traverseephemeron(global_State *g, Table *h) {
|
|
|
|
|
static int traverseephemeron (global_State *g, Table *h) {
|
|
|
|
|
int marked = 0; /* true if an object is marked in this traversal */
|
|
|
|
|
int hasclears = 0; /* true if table has white keys */
|
|
|
|
|
int hasww = 0; /* true if table has entry "white-key -> white-value" */
|
|
|
|
@ -484,7 +504,8 @@ static int traverseephemeron(global_State *g, Table *h) {
|
|
|
|
|
return marked;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void traversestrongtable(global_State *g, Table *h) {
|
|
|
|
|
|
|
|
|
|
static void traversestrongtable (global_State *g, Table *h) {
|
|
|
|
|
Node *n, *limit = gnodelast(h);
|
|
|
|
|
unsigned int i;
|
|
|
|
|
for (i = 0; i < h->sizearray; i++) /* traverse array part */
|
|
|
|
@ -501,12 +522,14 @@ static void traversestrongtable(global_State *g, Table *h) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static lu_mem traversetable(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,17 +541,20 @@ 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
|
|
|
|
|
** NULL, so the use of 'markobjectN')
|
|
|
|
|
*/
|
|
|
|
|
static int traverseproto(global_State *g, Proto *f) {
|
|
|
|
|
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,11 +568,16 @@ 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) {
|
|
|
|
|
|
|
|
|
|
static lu_mem traverseCclosure (global_State *g, CClosure *cl) {
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
|
|
|
|
|
markvalue(g, &cl->upvalue[i]);
|
|
|
|
@ -559,7 +590,7 @@ static lu_mem traverseCclosure(global_State *g, CClosure *cl) {
|
|
|
|
|
** (because then the value cannot be changed by the thread and the
|
|
|
|
|
** thread may not be traversed again)
|
|
|
|
|
*/
|
|
|
|
|
static lu_mem traverseLclosure(global_State *g, LClosure *cl) {
|
|
|
|
|
static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
|
|
|
|
|
int i;
|
|
|
|
|
markobjectN(g, cl->p); /* mark its prototype */
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
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,19 +626,22 @@ 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).
|
|
|
|
|
*/
|
|
|
|
|
static void propagatemark(global_State *g) {
|
|
|
|
|
static void propagatemark (global_State *g) {
|
|
|
|
|
lu_mem size;
|
|
|
|
|
GCObject *o = g->gray;
|
|
|
|
|
lua_assert(isgray(o));
|
|
|
|
|
gray2black(o);
|
|
|
|
|
switch (o->tt) {
|
|
|
|
|
/* RAVI changes */
|
|
|
|
|
case RAVI_TIARRAY:
|
|
|
|
|
case RAVI_TFARRAY:
|
|
|
|
|
case LUA_TTABLE: {
|
|
|
|
@ -644,11 +681,13 @@ static void propagatemark(global_State *g) {
|
|
|
|
|
g->GCmemtrav += size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void propagateall(global_State *g) {
|
|
|
|
|
|
|
|
|
|
static void propagateall (global_State *g) {
|
|
|
|
|
while (g->gray) propagatemark(g);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void convergeephemerons(global_State *g) {
|
|
|
|
|
|
|
|
|
|
static void convergeephemerons (global_State *g) {
|
|
|
|
|
int changed;
|
|
|
|
|
do {
|
|
|
|
|
GCObject *w;
|
|
|
|
@ -667,34 +706,38 @@ static void convergeephemerons(global_State *g) {
|
|
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** {======================================================
|
|
|
|
|
** Sweep Functions
|
|
|
|
|
** =======================================================
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** clear entries with unmarked keys from all weaktables in list 'l' up
|
|
|
|
|
** to element 'f'
|
|
|
|
|
*/
|
|
|
|
|
static void clearkeys(global_State *g, GCObject *l, GCObject *f) {
|
|
|
|
|
static void clearkeys (global_State *g, GCObject *l, GCObject *f) {
|
|
|
|
|
for (; l != f; l = gco2t(l)->gclist) {
|
|
|
|
|
Table *h = gco2t(l);
|
|
|
|
|
Node *n, *limit = gnodelast(h);
|
|
|
|
|
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'
|
|
|
|
|
*/
|
|
|
|
|
static void clearvalues(global_State *g, GCObject *l, GCObject *f) {
|
|
|
|
|
static void clearvalues (global_State *g, GCObject *l, GCObject *f) {
|
|
|
|
|
for (; l != f; l = gco2t(l)->gclist) {
|
|
|
|
|
Table *h = gco2t(l);
|
|
|
|
|
Node *n, *limit = gnodelast(h);
|
|
|
|
@ -713,22 +756,27 @@ static void clearvalues(global_State *g, GCObject *l, GCObject *f) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void luaC_upvdeccount(lua_State *L, UpVal *uv) {
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
static void freeobj (lua_State *L, GCObject *o) {
|
|
|
|
|
switch (o->tt) {
|
|
|
|
|
case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
|
|
|
|
|
case LUA_TLCL: {
|
|
|
|
@ -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,8 +805,10 @@ 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);
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
@ -766,7 +817,7 @@ static GCObject **sweeplist(lua_State *L, GCObject **p, lu_mem count);
|
|
|
|
|
** collection cycle. Return where to continue the traversal or NULL if
|
|
|
|
|
** list is finished.
|
|
|
|
|
*/
|
|
|
|
|
static GCObject **sweeplist(lua_State *L, GCObject **p, lu_mem count) {
|
|
|
|
|
static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
int ow = otherwhite(g);
|
|
|
|
|
int white = luaC_white(g); /* current white */
|
|
|
|
@ -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) {
|
|
|
|
|
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
|
|
|
|
@ -805,7 +860,7 @@ static GCObject **sweeptolive(lua_State *L, GCObject **p) {
|
|
|
|
|
/*
|
|
|
|
|
** If possible, shrink string table
|
|
|
|
|
*/
|
|
|
|
|
static void checkSizes(lua_State *L, global_State *g) {
|
|
|
|
|
static void checkSizes (lua_State *L, global_State *g) {
|
|
|
|
|
if (g->gckind != KGC_EMERGENCY) {
|
|
|
|
|
l_mem olddebt = g->GCdebt;
|
|
|
|
|
if (g->strt.nuse < g->strt.size / 4) /* string table too big? */
|
|
|
|
@ -814,23 +869,27 @@ static void checkSizes(lua_State *L, global_State *g) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static GCObject *udata2finalize(global_State *g) {
|
|
|
|
|
|
|
|
|
|
static GCObject *udata2finalize (global_State *g) {
|
|
|
|
|
GCObject *o = g->tobefnz; /* get first element */
|
|
|
|
|
lua_assert(tofinalize(o));
|
|
|
|
|
g->tobefnz = o->next; /* remove it from 'tobefnz' list */
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
static void GCTM (lua_State *L, int propagateerrors) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
const TValue *tm;
|
|
|
|
|
TValue v;
|
|
|
|
@ -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,40 +922,47 @@ static void GCTM(lua_State *L, int propagateerrors) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** call a few (up to 'g->gcfinnum') finalizers
|
|
|
|
|
*/
|
|
|
|
|
static int runafewfinalizers(lua_State *L) {
|
|
|
|
|
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) {
|
|
|
|
|
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;
|
|
|
|
|
static GCObject **findlast (GCObject **p) {
|
|
|
|
|
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)
|
|
|
|
|
*/
|
|
|
|
|
static void separatetobefnz(global_State *g, int all) {
|
|
|
|
|
static void separatetobefnz (global_State *g, int all) {
|
|
|
|
|
GCObject *curr;
|
|
|
|
|
GCObject **p = &g->finobj;
|
|
|
|
|
GCObject **lastnext = findlast(&g->tobefnz);
|
|
|
|
@ -911,11 +979,12 @@ 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.
|
|
|
|
|
*/
|
|
|
|
|
void luaC_checkfinalizer(lua_State *L, GCObject *o, Table *mt) {
|
|
|
|
|
void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
if (tofinalize(o) || /* obj. is already marked... */
|
|
|
|
|
gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */
|
|
|
|
@ -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,19 +1007,22 @@ 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'
|
|
|
|
|
** should be OK: it cannot be zero (because Lua cannot even start with
|
|
|
|
|
** less than PAUSEADJ bytes).
|
|
|
|
|
*/
|
|
|
|
|
static void setpause(global_State *g) {
|
|
|
|
|
static void setpause (global_State *g) {
|
|
|
|
|
l_mem threshold, debt;
|
|
|
|
|
l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
|
|
|
|
|
lua_assert(estimate > 0);
|
|
|
|
@ -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
|
|
|
|
@ -969,14 +1041,15 @@ static void setpause(global_State *g) {
|
|
|
|
|
** not need to skip objects created between "now" and the start of the
|
|
|
|
|
** real sweep.
|
|
|
|
|
*/
|
|
|
|
|
static void entersweep(lua_State *L) {
|
|
|
|
|
static void entersweep (lua_State *L) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
g->gcstate = GCSswpallgc;
|
|
|
|
|
lua_assert(g->sweepgc == NULL);
|
|
|
|
|
g->sweepgc = sweeplist(L, &g->allgc, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void luaC_freeallobjects(lua_State *L) {
|
|
|
|
|
|
|
|
|
|
void luaC_freeallobjects (lua_State *L) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
separatetobefnz(g, 1); /* separate all objects with finalizers */
|
|
|
|
|
lua_assert(g->finobj == NULL);
|
|
|
|
@ -990,7 +1063,8 @@ void luaC_freeallobjects(lua_State *L) {
|
|
|
|
|
lua_assert(g->strt.nuse == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static l_mem atomic(lua_State *L) {
|
|
|
|
|
|
|
|
|
|
static l_mem atomic (lua_State *L) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
l_mem work;
|
|
|
|
|
GCObject *origweak, *origall;
|
|
|
|
@ -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,11 +1126,12 @@ static lu_mem sweepstep(lua_State *L, global_State *g, int nextstate, GCObject *
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static lu_mem singlestep(lua_State *L) {
|
|
|
|
|
|
|
|
|
|
static lu_mem singlestep (lua_State *L) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
switch (g->gcstate) {
|
|
|
|
|
case GCSpause: {
|
|
|
|
|
g->GCmemtrav = g->strt.size * sizeof(GCObject *);
|
|
|
|
|
g->GCmemtrav = g->strt.size * sizeof(GCObject*);
|
|
|
|
|
restartcollection(g);
|
|
|
|
|
g->gcstate = GCSpropagate;
|
|
|
|
|
return g->GCmemtrav;
|
|
|
|
@ -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,24 +1181,26 @@ 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) {
|
|
|
|
|
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)
|
|
|
|
|
*/
|
|
|
|
|
static l_mem getdebt(global_State *g) {
|
|
|
|
|
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;
|
|
|
|
@ -1131,7 +1211,7 @@ static l_mem getdebt(global_State *g) {
|
|
|
|
|
/*
|
|
|
|
|
** performs a basic GC step when collector is running
|
|
|
|
|
*/
|
|
|
|
|
void luaC_step(lua_State *L) {
|
|
|
|
|
void luaC_step (lua_State *L) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
l_mem debt = getdebt(g); /* GC deficit (be paid now) */
|
|
|
|
|
if (!g->gcrunning) { /* not running? */
|
|
|
|
@ -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
|
|
|
|
@ -1160,7 +1241,7 @@ void luaC_step(lua_State *L) {
|
|
|
|
|
** to sweep all objects to turn them back to white (as white has not
|
|
|
|
|
** changed, nothing will be collected).
|
|
|
|
|
*/
|
|
|
|
|
void luaC_fullgc(lua_State *L, int isemergency) {
|
|
|
|
|
void luaC_fullgc (lua_State *L, int isemergency) {
|
|
|
|
|
global_State *g = G(L);
|
|
|
|
|
lua_assert(g->gckind == KGC_NORMAL);
|
|
|
|
|
if (isemergency) g->gckind = KGC_EMERGENCY; /* set flag */
|
|
|
|
@ -1179,3 +1260,5 @@ void luaC_fullgc(lua_State *L, int isemergency) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|