mirror of https://github.com/rusefi/lua.git
revamping the incremental collector
Some simplifications (not counting bytes, couting only slots visited; no more 'gcfinnum'); more GC parameters; using vararg in 'lua_gc' to set parameters in different GC modes
This commit is contained in:
parent
4804bbd9bb
commit
72d82a296c
44
lapi.c
44
lapi.c
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
** $Id: lapi.c,v 2.266 2017/05/11 18:57:46 roberto Exp roberto $
|
||||
** $Id: lapi.c,v 2.267 2017/05/18 12:34:58 roberto Exp roberto $
|
||||
** Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
@ -1046,17 +1046,12 @@ LUA_API int lua_status (lua_State *L) {
|
|||
/*
|
||||
** Garbage-collection function
|
||||
*/
|
||||
|
||||
#if !defined(LUA_GENMAJORMUL)
|
||||
#define LUA_GENMAJORMUL 100
|
||||
#define LUA_GENMINORMUL 5
|
||||
#endif
|
||||
|
||||
LUA_API int lua_gc (lua_State *L, int what, int data) {
|
||||
LUA_API int lua_gc (lua_State *L, int what, ...) {
|
||||
va_list argp;
|
||||
int res = 0;
|
||||
global_State *g;
|
||||
global_State *g = G(L);
|
||||
lua_lock(L);
|
||||
g = G(L);
|
||||
va_start(argp, what);
|
||||
switch (what) {
|
||||
case LUA_GCSTOP: {
|
||||
g->gcrunning = 0;
|
||||
|
@ -1081,11 +1076,12 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
|
|||
break;
|
||||
}
|
||||
case LUA_GCSTEP: {
|
||||
int data = va_arg(argp, int);
|
||||
l_mem debt = 1; /* =1 to signal that it did an actual step */
|
||||
lu_byte oldrunning = g->gcrunning;
|
||||
g->gcrunning = 1; /* allow GC to run */
|
||||
if (data == 0) {
|
||||
luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */
|
||||
luaE_setdebt(g, 0); /* do a basic step */
|
||||
luaC_step(L);
|
||||
}
|
||||
else { /* add 'data' to total debt */
|
||||
|
@ -1099,14 +1095,15 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
|
|||
break;
|
||||
}
|
||||
case LUA_GCSETPAUSE: {
|
||||
res = g->gcpause;
|
||||
g->gcpause = data;
|
||||
int data = va_arg(argp, int);
|
||||
res = g->gcpause + 100;
|
||||
g->gcpause = (data >= 100) ? data - 100 : 0;
|
||||
break;
|
||||
}
|
||||
case LUA_GCSETSTEPMUL: {
|
||||
res = g->gcstepmul;
|
||||
if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */
|
||||
g->gcstepmul = data;
|
||||
int data = va_arg(argp, int);
|
||||
res = g->gcstepmul * 10;
|
||||
g->gcstepmul = data / 10;
|
||||
break;
|
||||
}
|
||||
case LUA_GCISRUNNING: {
|
||||
|
@ -1114,19 +1111,26 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
|
|||
break;
|
||||
}
|
||||
case LUA_GCGEN: {
|
||||
lu_byte aux = data & 0xff;
|
||||
g->genminormul = (aux == 0) ? LUA_GENMINORMUL : aux;
|
||||
aux = (data >> 8) & 0xff;
|
||||
g->genmajormul = (aux == 0) ? LUA_GENMAJORMUL : aux;
|
||||
int minormul = va_arg(argp, int);
|
||||
int majormul = va_arg(argp, int);
|
||||
g->genminormul = (minormul == 0) ? LUAI_GENMINORMUL : minormul;
|
||||
g->genmajormul = (majormul == 0) ? LUAI_GENMAJORMUL : majormul;
|
||||
luaC_changemode(L, KGC_GEN);
|
||||
break;
|
||||
}
|
||||
case LUA_GCINC: {
|
||||
int pause = va_arg(argp, int);
|
||||
int stepmul = va_arg(argp, int);
|
||||
int stepsize = va_arg(argp, int);
|
||||
g->gcpause = (pause == 0) ? LUAI_GCPAUSE : pause;
|
||||
g->gcstepmul = (stepmul == 0) ? LUAI_GCMUL : stepmul;
|
||||
g->gcstepsize = (stepsize == 0) ? LUAI_GCSTEPSIZE : stepsize;
|
||||
luaC_changemode(L, KGC_INC);
|
||||
break;
|
||||
}
|
||||
default: res = -1; /* invalid option */
|
||||
}
|
||||
va_end(argp);
|
||||
lua_unlock(L);
|
||||
return res;
|
||||
}
|
||||
|
|
39
lbaselib.c
39
lbaselib.c
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
** $Id: lbaselib.c,v 1.314 2016/09/05 19:06:34 roberto Exp roberto $
|
||||
** $Id: lbaselib.c,v 1.315 2017/02/23 21:07:34 roberto Exp roberto $
|
||||
** Basic library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
@ -178,19 +178,46 @@ static int luaB_collectgarbage (lua_State *L) {
|
|||
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
|
||||
LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
|
||||
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
|
||||
int ex = (int)luaL_optinteger(L, 2, 0);
|
||||
int res = lua_gc(L, o, ex);
|
||||
switch (o) {
|
||||
case LUA_GCCOUNT: {
|
||||
int b = lua_gc(L, LUA_GCCOUNTB, 0);
|
||||
lua_pushnumber(L, (lua_Number)res + ((lua_Number)b/1024));
|
||||
int k = lua_gc(L, o);
|
||||
int b = lua_gc(L, LUA_GCCOUNTB);
|
||||
lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024));
|
||||
return 1;
|
||||
}
|
||||
case LUA_GCSTEP: case LUA_GCISRUNNING: {
|
||||
case LUA_GCSTEP: {
|
||||
int step = (int)luaL_optinteger(L, 2, 0);
|
||||
int res = lua_gc(L, o, step);
|
||||
lua_pushboolean(L, res);
|
||||
return 1;
|
||||
}
|
||||
case LUA_GCSETPAUSE:
|
||||
case LUA_GCSETSTEPMUL: {
|
||||
int p = (int)luaL_optinteger(L, 2, 0);
|
||||
int previous = lua_gc(L, o, p);
|
||||
lua_pushinteger(L, previous);
|
||||
return 1;
|
||||
}
|
||||
case LUA_GCISRUNNING: {
|
||||
int res = lua_gc(L, o);
|
||||
lua_pushboolean(L, res);
|
||||
return 1;
|
||||
}
|
||||
case LUA_GCGEN: {
|
||||
int minormul = (int)luaL_optinteger(L, 2, 0);
|
||||
int majormul = (int)luaL_optinteger(L, 3, 0);
|
||||
lua_gc(L, o, minormul, majormul);
|
||||
return 0;
|
||||
}
|
||||
case LUA_GCINC: {
|
||||
int pause = (int)luaL_optinteger(L, 2, 0);
|
||||
int stepmul = (int)luaL_optinteger(L, 3, 0);
|
||||
int stepsize = (int)luaL_optinteger(L, 4, 0);
|
||||
lua_gc(L, o, pause, stepmul, stepsize);
|
||||
return 0;
|
||||
}
|
||||
default: {
|
||||
int res = lua_gc(L, o);
|
||||
lua_pushinteger(L, res);
|
||||
return 1;
|
||||
}
|
||||
|
|
280
lgc.c
280
lgc.c
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
** $Id: lgc.c,v 2.227 2017/04/30 20:43:26 roberto Exp roberto $
|
||||
** $Id: lgc.c,v 2.228 2017/05/04 13:32:01 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
@ -27,23 +27,29 @@
|
|||
|
||||
|
||||
/*
|
||||
** cost of sweeping one element (the size of a small object divided
|
||||
** by some adjust for the sweep speed)
|
||||
** Maximum number of elements to sweep in each single step.
|
||||
** (Large enough to dissipate fixed overheads but small enough
|
||||
** to allow small steps for the collector.)
|
||||
*/
|
||||
#define GCSWEEPCOST ((sizeof(TString) + 4) / 4)
|
||||
#define GCSWEEPMAX 100
|
||||
|
||||
/* maximum number of elements to sweep in each single step */
|
||||
#define GCSWEEPMAX (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4))
|
||||
|
||||
/* cost of calling one finalizer */
|
||||
#define GCFINALIZECOST GCSWEEPCOST
|
||||
/*
|
||||
** Maximum number of finalizers to call in each single step.
|
||||
*/
|
||||
#define GCFINMAX 10
|
||||
|
||||
|
||||
/*
|
||||
** macro to adjust 'stepmul': 'stepmul' is actually used like
|
||||
** 'stepmul / STEPMULADJ' (value chosen by tests)
|
||||
** Cost of calling one finalizer.
|
||||
*/
|
||||
#define STEPMULADJ 200
|
||||
#define GCFINALIZECOST 50
|
||||
|
||||
|
||||
/*
|
||||
** The equivalent, in bytes, of one unit of "work" (visiting a slot,
|
||||
** sweeping an object, etc.) * 10 (for scaling)
|
||||
*/
|
||||
#define WORK2MEM (sizeof(TValue) * 10)
|
||||
|
||||
|
||||
/*
|
||||
|
@ -86,7 +92,7 @@
|
|||
#define markobjectN(g,t) { if (t) markobject(g,t); }
|
||||
|
||||
static void reallymarkobject (global_State *g, GCObject *o);
|
||||
static l_mem atomic (lua_State *L);
|
||||
static lu_mem atomic (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
|
@ -246,21 +252,15 @@ static void reallymarkobject (global_State *g, GCObject *o) {
|
|||
reentry:
|
||||
white2gray(o);
|
||||
switch (o->tt) {
|
||||
case LUA_TSHRSTR: {
|
||||
gray2black(o);
|
||||
g->GCmemtrav += sizelstring(gco2ts(o)->shrlen);
|
||||
break;
|
||||
}
|
||||
case LUA_TSHRSTR:
|
||||
case LUA_TLNGSTR: {
|
||||
gray2black(o);
|
||||
g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen);
|
||||
break;
|
||||
}
|
||||
case LUA_TUSERDATA: {
|
||||
TValue uvalue;
|
||||
markobjectN(g, gco2u(o)->metatable); /* mark its metatable */
|
||||
gray2black(o);
|
||||
g->GCmemtrav += sizeudata(gco2u(o));
|
||||
getuservalue(g->mainthread, gco2u(o), &uvalue);
|
||||
if (valiswhite(&uvalue)) { /* markvalue(g, &uvalue); */
|
||||
o = gcvalue(&uvalue);
|
||||
|
@ -270,7 +270,6 @@ static void reallymarkobject (global_State *g, GCObject *o) {
|
|||
}
|
||||
case LUA_TUPVAL: {
|
||||
UpVal *uv = gco2upv(o);
|
||||
g->GCmemtrav += sizeof(UpVal);
|
||||
if (!upisopen(uv)) /* open upvalues are kept gray */
|
||||
gray2black(o);
|
||||
markvalue(g, uv->v); /* mark its content */
|
||||
|
@ -314,10 +313,14 @@ static void markmt (global_State *g) {
|
|||
/*
|
||||
** mark all objects in list of being-finalized
|
||||
*/
|
||||
static void markbeingfnz (global_State *g) {
|
||||
static lu_mem markbeingfnz (global_State *g) {
|
||||
GCObject *o;
|
||||
for (o = g->tobefnz; o != NULL; o = o->next)
|
||||
lu_mem count = 0;
|
||||
for (o = g->tobefnz; o != NULL; o = o->next) {
|
||||
count++;
|
||||
markobject(g, o);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
|
@ -327,10 +330,12 @@ static void markbeingfnz (global_State *g) {
|
|||
** thread.) Remove from the list threads that no longer have upvalues and
|
||||
** not-marked threads.
|
||||
*/
|
||||
static void remarkupvals (global_State *g) {
|
||||
static int remarkupvals (global_State *g) {
|
||||
lua_State *thread;
|
||||
lua_State **p = &g->twups;
|
||||
int work = 0;
|
||||
while ((thread = *p) != NULL) {
|
||||
work++;
|
||||
lua_assert(!isblack(thread)); /* threads are never black */
|
||||
if (isgray(thread) && thread->openupval != NULL)
|
||||
p = &thread->twups; /* keep marked thread with upvalues in the list */
|
||||
|
@ -339,11 +344,13 @@ static void remarkupvals (global_State *g) {
|
|||
*p = thread->twups; /* remove thread from the list */
|
||||
thread->twups = thread; /* mark that it is out of list */
|
||||
for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) {
|
||||
work++;
|
||||
if (!iswhite(uv)) /* upvalue already visited? */
|
||||
markvalue(g, uv->v); /* mark its value */
|
||||
}
|
||||
}
|
||||
}
|
||||
return work;
|
||||
}
|
||||
|
||||
|
||||
|
@ -491,8 +498,7 @@ 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 1 + h->sizearray + 2 * allocsizenode(h);
|
||||
}
|
||||
|
||||
|
||||
|
@ -539,38 +545,33 @@ static int traverseproto (global_State *g, Proto *f) {
|
|||
markobjectN(g, f->p[i]);
|
||||
for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
|
||||
markobjectN(g, f->locvars[i].varname);
|
||||
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 1 + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars;
|
||||
}
|
||||
|
||||
|
||||
static lu_mem traverseCclosure (global_State *g, CClosure *cl) {
|
||||
static int traverseCclosure (global_State *g, CClosure *cl) {
|
||||
int i;
|
||||
for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
|
||||
markvalue(g, &cl->upvalue[i]);
|
||||
return sizeCclosure(cl->nupvalues);
|
||||
return 1 + cl->nupvalues;
|
||||
}
|
||||
|
||||
/*
|
||||
** Traverse a Lua closure, marking its prototype and its upvalues.
|
||||
** (Both can be NULL while closure is being created.)
|
||||
*/
|
||||
static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
|
||||
static int traverseLclosure (global_State *g, LClosure *cl) {
|
||||
int i;
|
||||
markobjectN(g, cl->p); /* mark its prototype */
|
||||
for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */
|
||||
UpVal *uv = cl->upvals[i];
|
||||
markobjectN(g, uv); /* mark upvalue */
|
||||
}
|
||||
return sizeLclosure(cl->nupvalues);
|
||||
return 1 + cl->nupvalues;
|
||||
}
|
||||
|
||||
|
||||
static lu_mem traversethread (global_State *g, lua_State *th) {
|
||||
static int traversethread (global_State *g, lua_State *th) {
|
||||
StkId o = th->stack;
|
||||
if (o == NULL)
|
||||
return 1; /* stack not completely built yet */
|
||||
|
@ -590,8 +591,7 @@ static lu_mem traversethread (global_State *g, lua_State *th) {
|
|||
}
|
||||
else if (!g->gcemergency)
|
||||
luaD_shrinkstack(th); /* do not change stack in emergency cycle */
|
||||
return (sizeof(lua_State) + sizeof(TValue) * th->stacksize +
|
||||
sizeof(CallInfo) * th->nci);
|
||||
return 1 + th->stacksize;
|
||||
}
|
||||
|
||||
|
||||
|
@ -599,51 +599,47 @@ static lu_mem traversethread (global_State *g, lua_State *th) {
|
|||
** traverse one gray object, turning it to black (except for threads,
|
||||
** which are always gray).
|
||||
*/
|
||||
static void propagatemark (global_State *g) {
|
||||
lu_mem size;
|
||||
static lu_mem propagatemark (global_State *g) {
|
||||
GCObject *o = g->gray;
|
||||
gray2black(o);
|
||||
switch (o->tt) {
|
||||
case LUA_TTABLE: {
|
||||
Table *h = gco2t(o);
|
||||
g->gray = h->gclist; /* remove from 'gray' list */
|
||||
size = traversetable(g, h);
|
||||
break;
|
||||
return traversetable(g, h);
|
||||
}
|
||||
case LUA_TLCL: {
|
||||
LClosure *cl = gco2lcl(o);
|
||||
g->gray = cl->gclist; /* remove from 'gray' list */
|
||||
size = traverseLclosure(g, cl);
|
||||
break;
|
||||
return traverseLclosure(g, cl);
|
||||
}
|
||||
case LUA_TCCL: {
|
||||
CClosure *cl = gco2ccl(o);
|
||||
g->gray = cl->gclist; /* remove from 'gray' list */
|
||||
size = traverseCclosure(g, cl);
|
||||
break;
|
||||
return traverseCclosure(g, cl);
|
||||
}
|
||||
case LUA_TTHREAD: {
|
||||
lua_State *th = gco2th(o);
|
||||
g->gray = th->gclist; /* remove from 'gray' list */
|
||||
linkgclist(th, g->grayagain); /* insert into 'grayagain' list */
|
||||
black2gray(o);
|
||||
size = traversethread(g, th);
|
||||
break;
|
||||
return traversethread(g, th);
|
||||
}
|
||||
case LUA_TPROTO: {
|
||||
Proto *p = gco2p(o);
|
||||
g->gray = p->gclist; /* remove from 'gray' list */
|
||||
size = traverseproto(g, p);
|
||||
break;
|
||||
return traverseproto(g, p);
|
||||
}
|
||||
default: lua_assert(0); return;
|
||||
default: lua_assert(0); return 0;
|
||||
}
|
||||
g->GCmemtrav += size;
|
||||
}
|
||||
|
||||
|
||||
static void propagateall (global_State *g) {
|
||||
while (g->gray) propagatemark(g);
|
||||
static lu_mem propagateall (global_State *g) {
|
||||
lu_mem tot = 0;
|
||||
while (g->gray)
|
||||
tot += propagatemark(g);
|
||||
return tot;
|
||||
}
|
||||
|
||||
|
||||
|
@ -719,7 +715,7 @@ static void clearvalues (global_State *g, GCObject *l, GCObject *f) {
|
|||
setnilvalue(o); /* remove value */
|
||||
}
|
||||
for (n = gnode(h, 0); n < limit; n++) {
|
||||
if (!ttisnil(gval(n)) && iscleared(g, gval(n))) {
|
||||
if (iscleared(g, gval(n))) {
|
||||
setnilvalue(gval(n)); /* remove value ... */
|
||||
removeentry(n); /* and remove entry from table */
|
||||
}
|
||||
|
@ -770,22 +766,20 @@ 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
|
||||
** sweep at most 'countin' elements from a list of GCObjects erasing dead
|
||||
** objects, where a dead object is one marked with the old (non current)
|
||||
** white; change all non-dead objects back to white, preparing for next
|
||||
** collection cycle. Return where to continue the traversal or NULL if
|
||||
** list is finished.
|
||||
** list is finished. ('*countout' gets the number of elements traversed.)
|
||||
*/
|
||||
static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
||||
static GCObject **sweeplist (lua_State *L, GCObject **p, int countin,
|
||||
int *countout) {
|
||||
global_State *g = G(L);
|
||||
int ow = otherwhite(g);
|
||||
int i;
|
||||
int white = luaC_white(g); /* current white */
|
||||
while (*p != NULL && count-- > 0) {
|
||||
for (i = 0; *p != NULL && i < countin; i++) {
|
||||
GCObject *curr = *p;
|
||||
int marked = curr->marked;
|
||||
if (isdeadm(ow, marked)) { /* is 'curr' dead? */
|
||||
|
@ -797,6 +791,8 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
|||
p = &curr->next; /* go to next element */
|
||||
}
|
||||
}
|
||||
if (countout)
|
||||
*countout = i; /* number of elements traversed */
|
||||
return (*p == NULL) ? NULL : p;
|
||||
}
|
||||
|
||||
|
@ -807,7 +803,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
|||
static GCObject **sweeptolive (lua_State *L, GCObject **p) {
|
||||
GCObject **old = p;
|
||||
do {
|
||||
p = sweeplist(L, p, 1);
|
||||
p = sweeplist(L, p, 1, NULL);
|
||||
} while (p == old);
|
||||
return p;
|
||||
}
|
||||
|
@ -892,16 +888,13 @@ static void GCTM (lua_State *L, int propagateerrors) {
|
|||
|
||||
|
||||
/*
|
||||
** call a few (up to 'g->gcfinnum') finalizers
|
||||
** Call a few finalizers
|
||||
*/
|
||||
static int runafewfinalizers (lua_State *L) {
|
||||
static int runafewfinalizers (lua_State *L, int n) {
|
||||
global_State *g = G(L);
|
||||
unsigned int i;
|
||||
lua_assert(!g->tobefnz || g->gcfinnum > 0);
|
||||
for (i = 0; g->tobefnz && i < g->gcfinnum; i++)
|
||||
int i;
|
||||
for (i = 0; i < n && g->tobefnz; 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;
|
||||
}
|
||||
|
||||
|
@ -1285,9 +1278,6 @@ static void genstep (lua_State *L, global_State *g) {
|
|||
//lua_checkmemory(L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
@ -1299,26 +1289,28 @@ static void genstep (lua_State *L, global_State *g) {
|
|||
|
||||
|
||||
/*
|
||||
** 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).
|
||||
** Set the "time" to wait before starting a new GC cycle; cycle will
|
||||
** start when memory use hits the threshold of ('estimate' * gcpause /
|
||||
** PAUSEADJ). (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) {
|
||||
l_mem threshold, debt;
|
||||
int pause = g->gcpause + 100;
|
||||
l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
|
||||
lua_assert(estimate > 0);
|
||||
threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */
|
||||
? estimate * g->gcpause /* no overflow */
|
||||
threshold = (pause < MAX_LMEM / estimate) /* overflow? */
|
||||
? estimate * pause /* no overflow */
|
||||
: MAX_LMEM; /* overflow; truncate to maximum */
|
||||
debt = gettotalbytes(g) - threshold;
|
||||
if (debt > 0) debt = 0;
|
||||
luaE_setdebt(g, debt);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Enter first sweep phase.
|
||||
** The call to 'sweeplist' tries to make pointer point to an object
|
||||
** The call to 'sweeptolive' makes the pointer point to an object
|
||||
** inside the list (instead of to the header), so that the real sweep do
|
||||
** not need to skip objects created between "now" and the start of the
|
||||
** real sweep.
|
||||
|
@ -1327,11 +1319,15 @@ 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);
|
||||
g->sweepgc = sweeptolive(L, &g->allgc);
|
||||
}
|
||||
|
||||
|
||||
static void deletealllist (lua_State *L, GCObject *p, GCObject *limit) {
|
||||
/*
|
||||
** Delete all objects in list 'p' until (but not including) object
|
||||
** 'limit'.
|
||||
*/
|
||||
static void deletelist (lua_State *L, GCObject *p, GCObject *limit) {
|
||||
while (p != limit) {
|
||||
GCObject *next = p->next;
|
||||
freeobj(L, p);
|
||||
|
@ -1340,52 +1336,50 @@ static void deletealllist (lua_State *L, GCObject *p, GCObject *limit) {
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
** Call all finalizers of the objects in the given Lua state, and
|
||||
** then free all objects, except for the main thread.
|
||||
*/
|
||||
void luaC_freeallobjects (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
luaC_changemode(L, KGC_INC);
|
||||
separatetobefnz(g, 1); /* separate all objects with finalizers */
|
||||
lua_assert(g->finobj == NULL);
|
||||
callallpendingfinalizers(L);
|
||||
deletealllist(L, g->allgc, obj2gco(g->mainthread));
|
||||
deletealllist(L, g->finobj, NULL);
|
||||
deletealllist(L, g->fixedgc, NULL); /* collect fixed objects */
|
||||
deletelist(L, g->allgc, obj2gco(g->mainthread));
|
||||
deletelist(L, g->finobj, NULL);
|
||||
deletelist(L, g->fixedgc, NULL); /* collect fixed objects */
|
||||
lua_assert(g->strt.nuse == 0);
|
||||
}
|
||||
|
||||
|
||||
static l_mem atomic (lua_State *L) {
|
||||
static lu_mem atomic (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
l_mem work;
|
||||
lu_mem work = 0;
|
||||
GCObject *origweak, *origall;
|
||||
GCObject *grayagain = g->grayagain; /* save original list */
|
||||
g->grayagain = NULL;
|
||||
lua_assert(g->ephemeron == NULL && g->weak == NULL);
|
||||
lua_assert(!iswhite(g->mainthread));
|
||||
g->gcstate = GCSatomic;
|
||||
g->GCmemtrav = 0; /* start counting work */
|
||||
markobject(g, L); /* mark running thread */
|
||||
/* registry and global metatables may be changed by API */
|
||||
markvalue(g, &g->l_registry);
|
||||
markmt(g); /* mark global metatables */
|
||||
/* remark occasional upvalues of (maybe) dead threads */
|
||||
remarkupvals(g);
|
||||
propagateall(g); /* propagate changes */
|
||||
work = g->GCmemtrav; /* stop counting (do not recount 'grayagain') */
|
||||
work += remarkupvals(g);
|
||||
work += propagateall(g); /* propagate changes */
|
||||
g->gray = grayagain;
|
||||
propagateall(g); /* traverse 'grayagain' list */
|
||||
g->GCmemtrav = 0; /* restart counting */
|
||||
work += propagateall(g); /* traverse 'grayagain' list */
|
||||
convergeephemerons(g);
|
||||
/* at this point, all strongly accessible objects are marked. */
|
||||
/* Clear values from weak tables, before checking finalizers */
|
||||
clearvalues(g, g->weak, NULL);
|
||||
clearvalues(g, g->allweak, NULL);
|
||||
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 */
|
||||
markbeingfnz(g); /* mark objects that will be finalized */
|
||||
propagateall(g); /* remark, to propagate 'resurrection' */
|
||||
g->GCmemtrav = 0; /* restart counting */
|
||||
work += markbeingfnz(g); /* mark objects that will be finalized */
|
||||
work += propagateall(g); /* remark, to propagate 'resurrection' */
|
||||
convergeephemerons(g);
|
||||
/* at this point, all resurrected objects are marked. */
|
||||
/* remove dead objects from weak tables */
|
||||
|
@ -1398,24 +1392,24 @@ static l_mem atomic (lua_State *L) {
|
|||
clearprotolist(g);
|
||||
g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
|
||||
lua_assert(g->gray == NULL);
|
||||
work += g->GCmemtrav; /* complete counting */
|
||||
return work; /* estimate of memory marked by 'atomic' */
|
||||
return work; /* estimate of slots marked by 'atomic' */
|
||||
}
|
||||
|
||||
|
||||
static lu_mem sweepstep (lua_State *L, global_State *g,
|
||||
int nextstate, GCObject **nextlist) {
|
||||
static int 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);
|
||||
int count;
|
||||
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX, &count);
|
||||
g->GCestimate += g->GCdebt - olddebt; /* update estimate */
|
||||
if (g->sweepgc) /* is there still something to sweep? */
|
||||
return (GCSWEEPMAX * GCSWEEPCOST);
|
||||
return count;
|
||||
}
|
||||
else { /* enter next state */
|
||||
g->gcstate = nextstate;
|
||||
g->sweepgc = nextlist;
|
||||
return 0; /* no work done */
|
||||
}
|
||||
/* else enter next state */
|
||||
g->gcstate = nextstate;
|
||||
g->sweepgc = nextlist;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
@ -1423,23 +1417,21 @@ static lu_mem singlestep (lua_State *L) {
|
|||
global_State *g = G(L);
|
||||
switch (g->gcstate) {
|
||||
case GCSpause: {
|
||||
g->GCmemtrav = g->strt.size * sizeof(GCObject*);
|
||||
restartcollection(g);
|
||||
g->gcstate = GCSpropagate;
|
||||
return g->GCmemtrav;
|
||||
return 1;
|
||||
}
|
||||
case GCSpropagate: {
|
||||
g->GCmemtrav = 0;
|
||||
if (g->gray == NULL) /* no more gray objects? */
|
||||
if (g->gray == NULL) { /* no more gray objects? */
|
||||
g->gcstate = GCSenteratomic; /* finish propagate phase */
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
propagatemark(g); /* traverse one gray object */
|
||||
return g->GCmemtrav; /* memory traversed in this step */
|
||||
return propagatemark(g); /* traverse one gray object */
|
||||
}
|
||||
case GCSenteratomic: {
|
||||
lu_mem work;
|
||||
propagateall(g); /* make sure gray list is empty */
|
||||
work = atomic(L); /* work is what was traversed by 'atomic' */
|
||||
lu_mem work = 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 */;
|
||||
return work;
|
||||
|
@ -1460,8 +1452,8 @@ static lu_mem singlestep (lua_State *L) {
|
|||
}
|
||||
case GCScallfin: { /* call remaining finalizers */
|
||||
if (g->tobefnz && !g->gcemergency) {
|
||||
int n = runafewfinalizers(L);
|
||||
return (n * GCFINALIZECOST);
|
||||
int n = runafewfinalizers(L, GCFINMAX);
|
||||
return n * GCFINALIZECOST;
|
||||
}
|
||||
else { /* emergency mode or no more finalizers */
|
||||
g->gcstate = GCSpause; /* finish collection */
|
||||
|
@ -1485,45 +1477,36 @@ void luaC_runtilstate (lua_State *L, int statesmask) {
|
|||
|
||||
|
||||
/*
|
||||
** get GC debt and convert it from Kb to 'work units' (avoid zero debt
|
||||
** and overflows)
|
||||
*/
|
||||
static l_mem getdebt (global_State *g) {
|
||||
l_mem debt = g->GCdebt;
|
||||
int stepmul = g->gcstepmul;
|
||||
if (debt <= 0) return 0; /* minimal debt */
|
||||
else {
|
||||
debt = (debt / STEPMULADJ) + 1;
|
||||
debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM;
|
||||
return debt;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** performs a basic incremental step
|
||||
** Performs a basic incremental step. The debt and step size are
|
||||
** converted from bytes to "units of work"; then the function loops
|
||||
** running single steps until adding that many units of work or
|
||||
** finishing a cycle (pause state). Finally, it sets the debt that
|
||||
** controls when next step will be performed.
|
||||
*/
|
||||
static void incstep (lua_State *L, global_State *g) {
|
||||
l_mem debt = getdebt(g); /* GC deficit (to be paid now) */
|
||||
int stepmul = (g->gcstepmul | 1); /* avoid division by 0 */
|
||||
l_mem debt = (g->GCdebt / WORK2MEM) * stepmul;
|
||||
l_mem stepsize = cast(l_mem, 1) << g->gcstepsize;
|
||||
stepsize = -((stepsize / WORK2MEM) * stepmul);
|
||||
do { /* repeat until pause or enough "credit" (negative debt) */
|
||||
lu_mem work = singlestep(L); /* perform one single step */
|
||||
debt -= work;
|
||||
} while (debt > -GCSTEPSIZE && g->gcstate != GCSpause);
|
||||
} while (debt > stepsize && g->gcstate != GCSpause);
|
||||
if (g->gcstate == GCSpause)
|
||||
setpause(g); /* pause until next cycle */
|
||||
else {
|
||||
debt = (debt / g->gcstepmul) * STEPMULADJ; /* convert 'work units' to Kb */
|
||||
debt = (debt / stepmul) * WORK2MEM; /* convert 'work units' to bytes */
|
||||
luaE_setdebt(g, debt);
|
||||
runafewfinalizers(L);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** performs a basic GC step when collector is running
|
||||
** performs a basic GC step if collector is running
|
||||
*/
|
||||
void luaC_step (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
if (!g->gcrunning) /* not running? */
|
||||
luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */
|
||||
luaE_setdebt(g, -MAX_LMEM); /* avoid being called without need */
|
||||
else if (g->gckind == KGC_INC)
|
||||
incstep(L, g);
|
||||
else
|
||||
|
@ -1532,9 +1515,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
|
||||
** unexpected ways (running finalizers and shrinking some structures).
|
||||
** Perform a full collection in incremental mode.
|
||||
** Before running the collection, check 'keepinvariant'; if it is true,
|
||||
** there may be some objects marked as black, so the collector has
|
||||
** to sweep all objects to turn them back to white (as white has not
|
||||
|
@ -1553,6 +1534,11 @@ static void fullinc (lua_State *L, global_State *g) {
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
** Performs a full GC cycle; if 'isemergency', set a flag to avoid
|
||||
** some operations which could change the interpreter state in some
|
||||
** unexpected ways (running finalizers and shrinking some structures).
|
||||
*/
|
||||
void luaC_fullgc (lua_State *L, int isemergency) {
|
||||
global_State *g = G(L);
|
||||
lua_assert(!g->gcemergency);
|
||||
|
|
20
lgc.h
20
lgc.h
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
** $Id: lgc.h,v 2.96 2017/04/11 18:41:09 roberto Exp roberto $
|
||||
** $Id: lgc.h,v 2.97 2017/05/04 13:32:01 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
@ -25,14 +25,6 @@
|
|||
*/
|
||||
|
||||
|
||||
|
||||
/* how much to allocate before next GC step */
|
||||
#if !defined(GCSTEPSIZE)
|
||||
/* ~100 small strings */
|
||||
#define GCSTEPSIZE (cast_int(100 * sizeof(TString)))
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Possible states of the Garbage Collector
|
||||
*/
|
||||
|
@ -125,6 +117,16 @@
|
|||
check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t)))
|
||||
|
||||
|
||||
/* Default Values for GC parameters */
|
||||
#define LUAI_GENMAJORMUL 100
|
||||
#define LUAI_GENMINORMUL 5
|
||||
|
||||
#define LUAI_GCPAUSE 100 /* 100% */
|
||||
#define LUAI_GCMUL 10
|
||||
/* how much to allocate before next GC step (log2) */
|
||||
#define LUAI_GCSTEPSIZE 13 /* 8 KB */
|
||||
|
||||
|
||||
/*
|
||||
** Does one step of collection when debt becomes positive. 'pre'/'pos'
|
||||
** allows some adjustments to be done only when needed. macro
|
||||
|
|
12
lstate.c
12
lstate.c
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
** $Id: lstate.c,v 2.138 2017/04/24 16:59:26 roberto Exp roberto $
|
||||
** $Id: lstate.c,v 2.139 2017/05/04 13:32:01 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
@ -28,14 +28,6 @@
|
|||
#include "ltm.h"
|
||||
|
||||
|
||||
#if !defined(LUAI_GCPAUSE)
|
||||
#define LUAI_GCPAUSE 200 /* 200% */
|
||||
#endif
|
||||
|
||||
#if !defined(LUAI_GCMUL)
|
||||
#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** a macro to help the creation of a unique random seed when a state is
|
||||
|
@ -328,9 +320,9 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
|||
g->twups = NULL;
|
||||
g->totalbytes = sizeof(LG);
|
||||
g->GCdebt = 0;
|
||||
g->gcfinnum = 0;
|
||||
g->gcpause = LUAI_GCPAUSE;
|
||||
g->gcstepmul = LUAI_GCMUL;
|
||||
g->gcstepsize = LUAI_GCSTEPSIZE;
|
||||
for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
|
||||
if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
|
||||
/* memory allocation error: free partial state */
|
||||
|
|
9
lstate.h
9
lstate.h
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
** $Id: lstate.h,v 2.140 2017/05/04 13:32:01 roberto Exp roberto $
|
||||
** $Id: lstate.h,v 2.141 2017/05/13 13:54:47 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
@ -139,7 +139,6 @@ typedef struct global_State {
|
|||
void *ud; /* auxiliary data to 'frealloc' */
|
||||
l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
|
||||
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
|
||||
lu_mem GCmemtrav; /* memory traversed by the GC */
|
||||
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
|
||||
stringtable strt; /* hash table for strings */
|
||||
TValue l_registry;
|
||||
|
@ -151,6 +150,9 @@ typedef struct global_State {
|
|||
lu_byte genmajormul; /* control for major generational collections */
|
||||
lu_byte gcrunning; /* true if GC is running */
|
||||
lu_byte gcemergency; /* true if this is an emergency collection */
|
||||
lu_byte gcpause; /* size of pause between successive GCs */
|
||||
lu_byte gcstepmul; /* GC "speed" */
|
||||
lu_byte gcstepsize; /* (log2 of) GC granularity */
|
||||
GCObject *allgc; /* list of all collectable objects */
|
||||
GCObject **sweepgc; /* current position of sweep in list */
|
||||
GCObject *finobj; /* list of collectable objects with finalizers */
|
||||
|
@ -170,9 +172,6 @@ typedef struct global_State {
|
|||
GCObject *finobjold; /* list of old objects with finalizers */
|
||||
GCObject *finobjrold; /* list of really old objects with finalizers */
|
||||
struct lua_State *twups; /* list of threads with open upvalues */
|
||||
unsigned int gcfinnum; /* number of finalizers to call in each GC step */
|
||||
int gcpause; /* size of pause between successive GCs */
|
||||
int gcstepmul; /* GC 'granularity' */
|
||||
lua_CFunction panic; /* to be called in unprotected errors */
|
||||
struct lua_State *mainthread;
|
||||
const lua_Number *version; /* pointer to version number */
|
||||
|
|
4
lua.h
4
lua.h
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
** $Id: lua.h,v 1.333 2017/02/23 21:07:34 roberto Exp roberto $
|
||||
** $Id: lua.h,v 1.334 2017/05/18 12:34:58 roberto Exp roberto $
|
||||
** Lua - A Scripting Language
|
||||
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
||||
** See Copyright Notice at the end of this file
|
||||
|
@ -311,7 +311,7 @@ LUA_API int (lua_isyieldable) (lua_State *L);
|
|||
#define LUA_GCGEN 10
|
||||
#define LUA_GCINC 11
|
||||
|
||||
LUA_API int (lua_gc) (lua_State *L, int what, int data);
|
||||
LUA_API int (lua_gc) (lua_State *L, int what, ...);
|
||||
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in New Issue