From 2506c1b429e952245295e54e71dac4b345e88984 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 6 Aug 2020 01:00:41 +0800 Subject: update Lua 5.4. --- src/lua/ldebug.c | 51 ++++---- src/lua/ldebug.h | 5 + src/lua/ldo.c | 15 ++- src/lua/lfunc.c | 5 +- src/lua/lgc.c | 349 ++++++++++++++++++++++++++++++++++-------------------- src/lua/lobject.h | 3 +- src/lua/lstate.c | 5 +- src/lua/lstate.h | 39 ++++-- src/lua/lvm.c | 2 +- 9 files changed, 303 insertions(+), 171 deletions(-) (limited to 'src') diff --git a/src/lua/ldebug.c b/src/lua/ldebug.c index afdc2b7..8cb00e5 100644 --- a/src/lua/ldebug.c +++ b/src/lua/ldebug.c @@ -33,10 +33,8 @@ #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) - -/* Active Lua function (given call info) */ -#define ci_func(ci) (clLvalue(s2v((ci)->func))) - +/* inverse of 'pcRel' */ +#define invpcRel(pc, p) ((p)->code + (pc) + 1) static const char *funcnamefromcode (lua_State *L, CallInfo *ci, const char **name); @@ -127,20 +125,18 @@ static void settraps (CallInfo *ci) { /* ** This function can be called during a signal, under "reasonable" ** assumptions. -** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by -** 'resethookcount') are for debug only, and it is no problem if they -** get arbitrary values (causes at most one wrong hook call). 'hookmask' -** is an atomic value. We assume that pointers are atomic too (e.g., gcc -** ensures that for all platforms where it runs). Moreover, 'hook' is -** always checked before being called (see 'luaD_hook'). +** Fields 'basehookcount' and 'hookcount' (set by 'resethookcount') +** are for debug only, and it is no problem if they get arbitrary +** values (causes at most one wrong hook call). 'hookmask' is an atomic +** value. We assume that pointers are atomic too (e.g., gcc ensures that +** for all platforms where it runs). Moreover, 'hook' is always checked +** before being called (see 'luaD_hook'). */ LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { if (func == NULL || mask == 0) { /* turn off hooks? */ mask = 0; func = NULL; } - if (isLua(L->ci)) - L->oldpc = L->ci->u.l.savedpc; L->hook = func; L->basehookcount = count; resethookcount(L); @@ -192,8 +188,8 @@ static const char *upvalname (const Proto *p, int uv) { static const char *findvararg (CallInfo *ci, int n, StkId *pos) { if (clLvalue(s2v(ci->func))->p->is_vararg) { int nextra = ci->u.l.nextraargs; - if (n <= nextra) { - *pos = ci->func - nextra + (n - 1); + if (n >= -nextra) { /* 'n' is negative */ + *pos = ci->func - nextra - (n + 1); return "(vararg)"; /* generic name for any vararg */ } } @@ -206,7 +202,7 @@ const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { const char *name = NULL; if (isLua(ci)) { if (n < 0) /* access to vararg values? */ - return findvararg(ci, -n, pos); + return findvararg(ci, n, pos); else name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); } @@ -787,18 +783,34 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { ** previous instruction 'oldpc'. */ static int changedline (const Proto *p, int oldpc, int newpc) { + if (p->lineinfo == NULL) /* no debug information? */ + return 0; while (oldpc++ < newpc) { if (p->lineinfo[oldpc] != 0) return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc)); } - return 0; /* no line changes in the way */ + return 0; /* no line changes between positions */ } +/* +** Traces the execution of a Lua function. Called before the execution +** of each opcode, when debug is on. 'L->oldpc' stores the last +** instruction traced, to detect line changes. When entering a new +** function, 'npci' will be zero and will test as a new line without +** the need for 'oldpc'; so, 'oldpc' does not need to be initialized +** before. Some exceptional conditions may return to a function without +** updating 'oldpc'. In that case, 'oldpc' may be invalid; if so, it is +** reset to zero. (A wrong but valid 'oldpc' at most causes an extra +** call to a line hook.) +*/ int luaG_traceexec (lua_State *L, const Instruction *pc) { CallInfo *ci = L->ci; lu_byte mask = L->hookmask; + const Proto *p = ci_func(ci)->p; int counthook; + /* 'L->oldpc' may be invalid; reset it in this case */ + int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0; if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */ ci->u.l.trap = 0; /* don't need to stop again */ return 0; /* turn off 'trap' */ @@ -819,15 +831,14 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ if (mask & LUA_MASKLINE) { - const Proto *p = ci_func(ci)->p; int npci = pcRel(pc, p); if (npci == 0 || /* call linehook when enter a new function, */ - pc <= L->oldpc || /* when jump back (loop), or when */ - changedline(p, pcRel(L->oldpc, p), npci)) { /* enter new line */ + pc <= invpcRel(oldpc, p) || /* when jump back (loop), or when */ + changedline(p, oldpc, npci)) { /* enter new line */ int newline = luaG_getfuncline(p, npci); luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */ } - L->oldpc = pc; /* 'pc' of last call to line hook */ + L->oldpc = npci; /* 'pc' of last call to line hook */ } if (L->status == LUA_YIELD) { /* did hook yield? */ if (counthook) diff --git a/src/lua/ldebug.h b/src/lua/ldebug.h index 1fe0efa..a0a5848 100644 --- a/src/lua/ldebug.h +++ b/src/lua/ldebug.h @@ -13,6 +13,11 @@ #define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1) + +/* Active Lua function (given call info) */ +#define ci_func(ci) (clLvalue(s2v((ci)->func))) + + #define resethookcount(L) (L->hookcount = L->basehookcount) /* diff --git a/src/lua/ldo.c b/src/lua/ldo.c index 4c976a1..5473815 100644 --- a/src/lua/ldo.c +++ b/src/lua/ldo.c @@ -327,7 +327,7 @@ static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ int delta = 0; if (isLuacode(ci)) { - Proto *p = clLvalue(s2v(ci->func))->p; + Proto *p = ci_func(ci)->p; if (p->is_vararg) delta = ci->u.l.nextraargs + p->numparams + 1; if (L->top < ci->top) @@ -340,8 +340,8 @@ static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ ci->func -= delta; } - if (isLua(ci->previous)) - L->oldpc = ci->previous->u.l.savedpc; /* update 'oldpc' */ + if (isLua(ci = ci->previous)) + L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */ return restorestack(L, oldtop); } @@ -515,14 +515,13 @@ void luaD_call (lua_State *L, StkId func, int nresults) { /* ** Similar to 'luaD_call', but does not allow yields during the call. -** If there is a stack overflow, freeing all CI structures will -** force the subsequent call to invoke 'luaE_extendCI', which then -** will raise any errors. */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { incXCcalls(L); - if (getCcalls(L) <= CSTACKERR) /* possible stack overflow? */ - luaE_freeCI(L); + if (getCcalls(L) <= CSTACKERR) { /* possible C stack overflow? */ + luaE_exitCcall(L); /* to compensate decrement in next call */ + luaE_enterCcall(L); /* check properly */ + } luaD_call(L, func, nResults); decXCcalls(L); } diff --git a/src/lua/lfunc.c b/src/lua/lfunc.c index 10100e5..f8c3c44 100644 --- a/src/lua/lfunc.c +++ b/src/lua/lfunc.c @@ -234,9 +234,10 @@ int luaF_close (lua_State *L, StkId level, int status) { luaF_unlinkupval(uv); setobj(L, slot, uv->v); /* move value to upvalue slot */ uv->v = slot; /* now current value lives here */ - if (!iswhite(uv)) + if (!iswhite(uv)) { /* neither white nor dead? */ gray2black(uv); /* closed upvalues cannot be gray */ - luaC_barrier(L, uv, slot); + luaC_barrier(L, uv, slot); + } } return status; } diff --git a/src/lua/lgc.c b/src/lua/lgc.c index f7fd7a5..cb820f9 100644 --- a/src/lua/lgc.c +++ b/src/lua/lgc.c @@ -60,13 +60,16 @@ #define PAUSEADJ 100 -/* mask to erase all color bits (plus gen. related stuff) */ -#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS | AGEBITS)) +/* mask to erase all color bits */ +#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS)) +/* mask to erase all GC bits */ +#define maskgcbits (maskcolors & ~AGEBITS) -/* macro to erase all color bits then sets only the current white bit */ + +/* macro to erase all color bits then set only the current white bit */ #define makewhite(g,x) \ - (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g))) + (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g))) #define white2gray(x) resetbits(x->marked, WHITEBITS) #define black2gray(x) resetbit(x->marked, BLACKBIT) @@ -77,16 +80,13 @@ #define keyiswhite(n) (keyiscollectable(n) && iswhite(gckey(n))) -#define checkconsistency(obj) \ - lua_longassert(!iscollectable(obj) || righttt(obj)) - /* ** Protected access to objects in values */ #define gcvalueN(o) (iscollectable(o) ? gcvalue(o) : NULL) -#define markvalue(g,o) { checkconsistency(o); \ +#define markvalue(g,o) { checkliveness(g->mainthread,o); \ if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } #define markkey(g, n) { if keyiswhite(n) reallymarkobject(g,gckey(n)); } @@ -181,14 +181,17 @@ static int iscleared (global_State *g, const GCObject *o) { /* -** barrier that moves collector forward, that is, mark the white object -** 'v' being pointed by the black object 'o'. (If in sweep phase, clear -** the black object to white [sweep it] to avoid other barrier calls for -** this same object.) In the generational mode, 'v' must also become -** old, if 'o' is old; however, it cannot be changed directly to OLD, -** because it may still point to non-old objects. So, it is marked as -** OLD0. In the next cycle it will become OLD1, and in the next it -** will finally become OLD (regular old). +** Barrier that moves collector forward, that is, marks the white object +** 'v' being pointed by the black object 'o'. In the generational +** mode, 'v' must also become old, if 'o' is old; however, it cannot +** be changed directly to OLD, because it may still point to non-old +** objects. So, it is marked as OLD0. In the next cycle it will become +** OLD1, and in the next it will finally become OLD (regular old). By +** then, any object it points to will also be old. If called in the +** incremental sweep phase, it clears the black object to white (sweep +** it) to avoid other barrier calls for this same object. (That cannot +** be done is generational mode, as its sweep does not distinguish +** whites from deads.) */ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { global_State *g = G(L); @@ -202,7 +205,8 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { } else { /* sweep phase */ lua_assert(issweepphase(g)); - makewhite(g, o); /* mark main obj. as white to avoid other barriers */ + if (g->gckind == KGC_INC) /* incremental mode? */ + makewhite(g, o); /* mark 'o' as white to avoid other barriers */ } } @@ -214,11 +218,12 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { void luaC_barrierback_ (lua_State *L, GCObject *o) { global_State *g = G(L); lua_assert(isblack(o) && !isdead(g, o)); - lua_assert(g->gckind != KGC_GEN || (isold(o) && getage(o) != G_TOUCHED1)); + lua_assert((g->gckind == KGC_GEN) == (isold(o) && getage(o) != G_TOUCHED1)); if (getage(o) != G_TOUCHED2) /* not already in gray list? */ linkobjgclist(o, g->grayagain); /* link it in 'grayagain' */ black2gray(o); /* make object gray (again) */ - setage(o, G_TOUCHED1); /* touched in current cycle */ + if (isold(o)) /* generational mode? */ + setage(o, G_TOUCHED1); /* touched in current cycle */ } @@ -324,25 +329,31 @@ static lu_mem markbeingfnz (global_State *g) { /* -** 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. +** For each non-marked thread, simulates a barrier between each open +** upvalue and its value. (If the thread is collected, the value will be +** assigned to the upvalue, but then it can be too late for the barrier +** to act. The "barrier" does not need to check colors: A non-marked +** thread must be young; upvalues cannot be older than their threads; so +** any visited upvalue must be young too.) Also removes the thread from +** the list, as it was already visited. Removes also threads with no +** upvalues, as they have nothing to be checked. (If the thread gets an +** upvalue later, it will be linked in the list again.) */ static int remarkupvals (global_State *g) { lua_State *thread; lua_State **p = &g->twups; - int work = 0; + int work = 0; /* estimate of how much work was done here */ while ((thread = *p) != NULL) { work++; - lua_assert(!isblack(thread)); /* threads are never black */ - if (isgray(thread) && thread->openupval != NULL) + if (!iswhite(thread) && thread->openupval != NULL) p = &thread->twups; /* keep marked thread with upvalues in the list */ else { /* thread is not marked or without upvalues */ UpVal *uv; + lua_assert(!isold(thread) || thread->openupval == NULL); *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) { + lua_assert(getage(uv) <= getage(thread)); work++; if (!iswhite(uv)) /* upvalue already visited? */ markvalue(g, uv->v); /* mark its value */ @@ -353,12 +364,17 @@ static int remarkupvals (global_State *g) { } +static void cleargraylists (global_State *g) { + g->gray = g->grayagain = NULL; + g->weak = g->allweak = g->ephemeron = NULL; +} + + /* ** mark root set and reset all gray lists, to start a new collection */ static void restartcollection (global_State *g) { - g->gray = g->grayagain = NULL; - g->weak = g->allweak = g->ephemeron = NULL; + cleargraylists(g); markobject(g, g->mainthread); markvalue(g, &g->l_registry); markmt(g); @@ -374,6 +390,32 @@ static void restartcollection (global_State *g) { ** ======================================================= */ + +/* +** Check whether object 'o' should be kept in the 'grayagain' list for +** post-processing by 'correctgraylist'. (It could put all old objects +** in the list and leave all the work to 'correctgraylist', but it is +** more efficient to avoid adding elements that will be removed.) Only +** TOUCHED1 objects need to be in the list. TOUCHED2 doesn't need to go +** back to a gray list, but then it must become OLD. (That is what +** 'correctgraylist' does when it finds a TOUCHED2 object.) +** It is defined as a macro because 'gclist' is not a unique field in +** different collectable objects. +*/ +#define genlink(g,o) genlink_(g, obj2gco(o), &(o)->gclist) + +static void genlink_ (global_State *g, GCObject *o, GCObject **pnext) { + lua_assert(isblack(o)); + if (getage(o) == G_TOUCHED1) { /* touched in this cycle? */ + *pnext = g->grayagain; /* link it back in 'grayagain' */ + g->grayagain = o; + black2gray(o); + } /* everything else do not need to be linked back */ + else if (getage(o) == G_TOUCHED2) + changeage(o, G_TOUCHED2, G_OLD); /* advance age */ +} + + /* ** Traverse a table with weak values and link it to proper list. During ** propagate phase, keep it in 'grayagain' list, to be revisited in the @@ -410,8 +452,9 @@ static void traverseweakvalue (global_State *g, Table *h) { ** the atomic phase, if table has any white->white entry, it has to ** be revisited during ephemeron convergence (as that key may turn ** black). Otherwise, if it has any white key, table has to be cleared -** (in the atomic phase). In generational mode, it (like all visited -** tables) must be kept in some gray list for post-processing. +** (in the atomic phase). In generational mode, some tables +** must be kept in some gray list for post-processing; this is done +** by 'genlink'. */ static int traverseephemeron (global_State *g, Table *h, int inv) { int marked = 0; /* true if an object is marked in this traversal */ @@ -450,10 +493,10 @@ static int traverseephemeron (global_State *g, Table *h, int inv) { linkgclist(h, g->ephemeron); /* have to propagate again */ else if (hasclears) /* table has white keys? */ linkgclist(h, g->allweak); /* may have to clean white keys */ - else if (g->gckind == KGC_GEN) - linkgclist(h, g->grayagain); /* keep it in some list */ - else - gray2black(h); + else { + gray2black(h); /* 'genlink' expects black objects */ + genlink(g, h); /* check whether collector still needs to see it */ + } return marked; } @@ -473,10 +516,7 @@ static void traversestrongtable (global_State *g, Table *h) { markvalue(g, gval(n)); } } - if (g->gckind == KGC_GEN) { - linkgclist(h, g->grayagain); /* keep it in some gray list */ - black2gray(h); - } + genlink(g, h); } @@ -488,7 +528,7 @@ static lu_mem traversetable (global_State *g, Table *h) { (cast_void(weakkey = strchr(svalue(mode), 'k')), cast_void(weakvalue = strchr(svalue(mode), 'v')), (weakkey || weakvalue))) { /* is really weak? */ - black2gray(h); /* keep table gray */ + black2gray(h); /* turn it back to gray, as it probably goes to a list */ if (!weakkey) /* strong keys? */ traverseweakvalue(g, h); else if (!weakvalue) /* strong values? */ @@ -507,10 +547,7 @@ static int traverseudata (global_State *g, Udata *u) { markobjectN(g, u->metatable); /* mark its metatable */ for (i = 0; i < u->nuvalue; i++) markvalue(g, &u->uv[i].uv); - if (g->gckind == KGC_GEN) { - linkgclist(u, g->grayagain); /* keep it in some gray list */ - black2gray(u); - } + genlink(g, u); return 1 + u->nuvalue; } @@ -559,12 +596,23 @@ static int traverseLclosure (global_State *g, LClosure *cl) { /* ** Traverse a thread, marking the elements in the stack up to its top -** and cleaning the rest of the stack in the final traversal. -** That ensures that the entire stack have valid (non-dead) objects. +** and cleaning the rest of the stack in the final traversal. That +** ensures that the entire stack have valid (non-dead) objects. +** Threads have no barriers. In gen. mode, old threads must be visited +** at every cycle, because they might point to young objects. In inc. +** mode, the thread can still be modified before the end of the cycle, +** and therefore it must be visited again in the atomic phase. To ensure +** these visits, threads must return to a gray list if they are not new +** (which can only happen in generational mode) or if the traverse is in +** the propagate phase (which can only happen in incremental mode). */ static int traversethread (global_State *g, lua_State *th) { UpVal *uv; StkId o = th->stack; + if (isold(th) || g->gcstate == GCSpropagate) { + linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ + black2gray(th); + } if (o == NULL) return 1; /* stack not completely built yet */ lua_assert(g->gcstate == GCSatomic || @@ -603,12 +651,7 @@ static lu_mem propagatemark (global_State *g) { case LUA_VLCL: return traverseLclosure(g, gco2lcl(o)); case LUA_VCCL: return traverseCclosure(g, gco2ccl(o)); case LUA_VPROTO: return traverseproto(g, gco2p(o)); - case LUA_VTHREAD: { - lua_State *th = gco2th(o); - linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ - black2gray(o); - return traversethread(g, th); - } + case LUA_VTHREAD: return traversethread(g, gco2th(o)); default: lua_assert(0); return 0; } } @@ -766,7 +809,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, int countin, freeobj(L, curr); /* erase 'curr' */ } else { /* change mark to 'white' */ - curr->marked = cast_byte((marked & maskcolors) | white); + curr->marked = cast_byte((marked & maskgcbits) | white); p = &curr->next; /* go to next element */ } } @@ -823,6 +866,8 @@ static GCObject *udata2finalize (global_State *g) { resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */ if (issweepphase(g)) makewhite(g, o); /* "sweep" object */ + else if (getage(o) == G_OLD1) + g->firstold1 = o; /* it is the first OLD1 object in the list */ return o; } @@ -896,15 +941,15 @@ static GCObject **findlast (GCObject **p) { /* ** Move all unreachable objects (or 'all' objects) that need ** finalization from list 'finobj' to list 'tobefnz' (to be finalized). -** (Note that objects after 'finobjold' cannot be white, so they -** don't need to be traversed. In incremental mode, 'finobjold' is NULL, +** (Note that objects after 'finobjold1' cannot be white, so they +** don't need to be traversed. In incremental mode, 'finobjold1' is NULL, ** so the whole list is traversed.) */ static void separatetobefnz (global_State *g, int all) { GCObject *curr; GCObject **p = &g->finobj; GCObject **lastnext = findlast(&g->tobefnz); - while ((curr = *p) != g->finobjold) { /* traverse all finalizable objects */ + while ((curr = *p) != g->finobjold1) { /* traverse all finalizable objects */ lua_assert(tofinalize(curr)); if (!(iswhite(curr) || all)) /* not being collected? */ p = &curr->next; /* don't bother with it */ @@ -920,6 +965,27 @@ static void separatetobefnz (global_State *g, int all) { } +/* +** If pointer 'p' points to 'o', move it to the next element. +*/ +static void checkpointer (GCObject **p, GCObject *o) { + if (o == *p) + *p = o->next; +} + + +/* +** Correct pointers to objects inside 'allgc' list when +** object 'o' is being removed from the list. +*/ +static void correctpointers (global_State *g, GCObject *o) { + checkpointer(&g->survival, o); + checkpointer(&g->old1, o); + checkpointer(&g->reallyold, o); + checkpointer(&g->firstold1, o); +} + + /* ** if object 'o' has a finalizer, remove it from 'allgc' list (must ** search the list to find it) and link it in 'finobj' list. @@ -936,14 +1002,8 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */ g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */ } - else { /* correct pointers into 'allgc' list, if needed */ - if (o == g->survival) - g->survival = o->next; - if (o == g->old) - g->old = o->next; - if (o == g->reallyold) - g->reallyold = o->next; - } + else + correctpointers(g, o); /* search for pointer pointing to 'o' */ for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ } *p = o->next; /* remove 'o' from 'allgc' list */ @@ -965,24 +1025,30 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { static void setpause (global_State *g); -/* mask to erase all color bits, not changing gen-related stuff */ -#define maskgencolors (~(bitmask(BLACKBIT) | WHITEBITS)) - - /* -** Sweep a list of objects, deleting dead ones and turning -** the non dead to old (without changing their colors). +** Sweep a list of objects to enter generational mode. Deletes dead +** objects and turns the non dead to old. All non-dead threads---which +** are now old---must be in a gray list. Everything else is not in a +** gray list. */ static void sweep2old (lua_State *L, GCObject **p) { GCObject *curr; + global_State *g = G(L); while ((curr = *p) != NULL) { if (iswhite(curr)) { /* is 'curr' dead? */ - lua_assert(isdead(G(L), curr)); + lua_assert(isdead(g, curr)); *p = curr->next; /* remove 'curr' from list */ freeobj(L, curr); /* erase 'curr' */ } else { /* all surviving objects become old */ setage(curr, G_OLD); + if (curr->tt == LUA_VTHREAD) { /* threads must be watched */ + lua_State *th = gco2th(curr); + linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ + black2gray(th); /* OK if already gray */ + } + else /* everything else is black */ + gray2black(curr); /* OK if already black */ p = &curr->next; /* go to next element */ } } @@ -995,9 +1061,13 @@ static void sweep2old (lua_State *L, GCObject **p) { ** during the sweep. So, any white object must be dead.) For ** non-dead objects, advance their ages and clear the color of ** new objects. (Old objects keep their colors.) +** The ages of G_TOUCHED1 and G_TOUCHED2 objects cannot be advanced +** here, because these old-generation objects are usually not swept +** here. They will all be advanced in 'correctgraylist'. That function +** will also remove objects turned white here from any gray list. */ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, - GCObject *limit) { + GCObject *limit, GCObject **pfirstold1) { static const lu_byte nextage[] = { G_SURVIVAL, /* from G_NEW */ G_OLD1, /* from G_SURVIVAL */ @@ -1016,9 +1086,15 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, freeobj(L, curr); /* erase 'curr' */ } else { /* correct mark and age */ - if (getage(curr) == G_NEW) - curr->marked = cast_byte((curr->marked & maskgencolors) | white); - setage(curr, nextage[getage(curr)]); + if (getage(curr) == G_NEW) { /* new objects go back to white */ + int marked = curr->marked & maskgcbits; /* erase GC bits */ + curr->marked = cast_byte(marked | G_SURVIVAL | white); + } + else { /* all other objects will be old, and so keep their color */ + setage(curr, nextage[getage(curr)]); + if (getage(curr) == G_OLD1 && *pfirstold1 == NULL) + *pfirstold1 = curr; /* first OLD1 object in the list */ + } p = &curr->next; /* go to next element */ } } @@ -1028,58 +1104,56 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, /* ** Traverse a list making all its elements white and clearing their -** age. +** age. In incremental mode, all objects are 'new' all the time, +** except for fixed strings (which are always old). */ static void whitelist (global_State *g, GCObject *p) { int white = luaC_white(g); for (; p != NULL; p = p->next) - p->marked = cast_byte((p->marked & maskcolors) | white); + p->marked = cast_byte((p->marked & maskgcbits) | white); } /* -** Correct a list of gray objects. +** Correct a list of gray objects. Return pointer to where rest of the +** list should be linked. ** Because this correction is done after sweeping, young objects might ** be turned white and still be in the list. They are only removed. -** For tables and userdata, advance 'touched1' to 'touched2'; 'touched2' -** objects become regular old and are removed from the list. -** For threads, just remove white ones from the list. +** 'TOUCHED1' objects are advanced to 'TOUCHED2' and remain on the list; +** Non-white threads also remain on the list; 'TOUCHED2' objects become +** regular old; they and anything else are removed from the list. */ static GCObject **correctgraylist (GCObject **p) { GCObject *curr; while ((curr = *p) != NULL) { - switch (curr->tt) { - case LUA_VTABLE: case LUA_VUSERDATA: { - GCObject **next = getgclist(curr); - if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ - lua_assert(isgray(curr)); - gray2black(curr); /* make it black, for next barrier */ - changeage(curr, G_TOUCHED1, G_TOUCHED2); - p = next; /* go to next element */ - } - else { /* not touched in this cycle */ - if (!iswhite(curr)) { /* not white? */ - lua_assert(isold(curr)); - if (getage(curr) == G_TOUCHED2) /* advance from G_TOUCHED2... */ - changeage(curr, G_TOUCHED2, G_OLD); /* ... to G_OLD */ - gray2black(curr); /* make it black */ - } - /* else, object is white: just remove it from this list */ - *p = *next; /* remove 'curr' from gray list */ - } - break; + GCObject **next = getgclist(curr); + if (iswhite(curr)) + goto remove; /* remove all white objects */ + else if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ + lua_assert(isgray(curr)); + gray2black(curr); /* make it black, for next barrier */ + changeage(curr, G_TOUCHED1, G_TOUCHED2); + goto remain; /* keep it in the list and go to next element */ + } + else if (curr->tt == LUA_VTHREAD) { + lua_assert(isgray(curr)); + goto remain; /* keep non-white threads on the list */ + } + else { /* everything else is removed */ + lua_assert(isold(curr)); /* young objects should be white here */ + if (getage(curr) == G_TOUCHED2) { /* advance from TOUCHED2... */ + changeage(curr, G_TOUCHED2, G_OLD); /* ... to OLD */ + lua_assert(isblack(curr)); /* TOUCHED2 objects are always black */ } - case LUA_VTHREAD: { - lua_State *th = gco2th(curr); - lua_assert(!isblack(th)); - if (iswhite(th)) /* new object? */ - *p = th->gclist; /* remove from gray list */ - else /* old threads remain gray */ - p = &th->gclist; /* go to next element */ - break; + else { + /* everything else in a gray list should be gray */ + lua_assert(isgray(curr)); + gray2black(curr); /* make object black (to be removed) */ } - default: lua_assert(0); /* nothing more could be gray here */ + goto remove; } + remove: *p = *next; continue; + remain: p = next; continue; } return p; } @@ -1100,7 +1174,7 @@ static void correctgraylists (global_State *g) { /* -** Mark 'OLD1' objects when starting a new young collection. +** Mark black 'OLD1' objects when starting a new young collection. ** Gray objects are already in some gray list, and so will be visited ** in the atomic step. */ @@ -1109,6 +1183,7 @@ static void markold (global_State *g, GCObject *from, GCObject *to) { for (p = from; p != to; p = p->next) { if (getage(p) == G_OLD1) { lua_assert(!iswhite(p)); + changeage(p, G_OLD1, G_OLD); /* now they are old */ if (isblack(p)) { black2gray(p); /* should be '2white', but gray works too */ reallymarkobject(g, p); @@ -1137,42 +1212,57 @@ static void finishgencycle (lua_State *L, global_State *g) { */ static void youngcollection (lua_State *L, global_State *g) { GCObject **psurvival; /* to point to first non-dead survival object */ + GCObject *dummy; /* dummy out parameter to 'sweepgen' */ lua_assert(g->gcstate == GCSpropagate); - markold(g, g->allgc, g->reallyold); + if (g->firstold1) { /* are there regular OLD1 objects? */ + markold(g, g->firstold1, g->reallyold); /* mark them */ + g->firstold1 = NULL; /* no more OLD1 objects (for now) */ + } markold(g, g->finobj, g->finobjrold); + markold(g, g->tobefnz, NULL); atomic(L); /* sweep nursery and get a pointer to its last live element */ - psurvival = sweepgen(L, g, &g->allgc, g->survival); - /* sweep 'survival' and 'old' */ - sweepgen(L, g, psurvival, g->reallyold); - g->reallyold = g->old; - g->old = *psurvival; /* 'survival' survivals are old now */ + g->gcstate = GCSswpallgc; + psurvival = sweepgen(L, g, &g->allgc, g->survival, &g->firstold1); + /* sweep 'survival' */ + sweepgen(L, g, psurvival, g->old1, &g->firstold1); + g->reallyold = g->old1; + g->old1 = *psurvival; /* 'survival' survivals are old now */ g->survival = g->allgc; /* all news are survivals */ /* repeat for 'finobj' lists */ - psurvival = sweepgen(L, g, &g->finobj, g->finobjsur); - /* sweep 'survival' and 'old' */ - sweepgen(L, g, psurvival, g->finobjrold); - g->finobjrold = g->finobjold; - g->finobjold = *psurvival; /* 'survival' survivals are old now */ + dummy = NULL; /* no 'firstold1' optimization for 'finobj' lists */ + psurvival = sweepgen(L, g, &g->finobj, g->finobjsur, &dummy); + /* sweep 'survival' */ + sweepgen(L, g, psurvival, g->finobjold1, &dummy); + g->finobjrold = g->finobjold1; + g->finobjold1 = *psurvival; /* 'survival' survivals are old now */ g->finobjsur = g->finobj; /* all news are survivals */ - sweepgen(L, g, &g->tobefnz, NULL); - + sweepgen(L, g, &g->tobefnz, NULL, &dummy); finishgencycle(L, g); } +/* +** Clears all gray lists, sweeps objects, and prepare sublists to enter +** generational mode. The sweeps remove dead objects and turn all +** surviving objects to old. Threads go back to 'grayagain'; everything +** else is turned black (not in any gray list). +*/ static void atomic2gen (lua_State *L, global_State *g) { + cleargraylists(g); /* sweep all elements making them old */ + g->gcstate = GCSswpallgc; sweep2old(L, &g->allgc); /* everything alive now is old */ - g->reallyold = g->old = g->survival = g->allgc; + g->reallyold = g->old1 = g->survival = g->allgc; + g->firstold1 = NULL; /* there are no OLD1 objects anywhere */ /* repeat for 'finobj' lists */ sweep2old(L, &g->finobj); - g->finobjrold = g->finobjold = g->finobjsur = g->finobj; + g->finobjrold = g->finobjold1 = g->finobjsur = g->finobj; sweep2old(L, &g->tobefnz); @@ -1185,8 +1275,9 @@ static void atomic2gen (lua_State *L, global_State *g) { /* ** Enter generational mode. Must go until the end of an atomic cycle -** to ensure that all threads and weak tables are in the gray lists. -** Then, turn all objects into old and finishes the collection. +** to ensure that all objects are correctly marked and weak tables +** are cleared. Then, turn all objects into old and finishes the +** collection. */ static lu_mem entergen (lua_State *L, global_State *g) { lu_mem numobjs; @@ -1205,10 +1296,10 @@ static lu_mem entergen (lua_State *L, global_State *g) { */ static void enterinc (global_State *g) { whitelist(g, g->allgc); - g->reallyold = g->old = g->survival = NULL; + g->reallyold = g->old1 = g->survival = NULL; whitelist(g, g->finobj); whitelist(g, g->tobefnz); - g->finobjrold = g->finobjold = g->finobjsur = NULL; + g->finobjrold = g->finobjold1 = g->finobjsur = NULL; g->gcstate = GCSpause; g->gckind = KGC_INC; g->lastatomic = 0; diff --git a/src/lua/lobject.h b/src/lua/lobject.h index 04a81d3..1620223 100644 --- a/src/lua/lobject.h +++ b/src/lua/lobject.h @@ -96,7 +96,8 @@ typedef struct TValue { /* ** Any value being manipulated by the program either is non ** collectable, or the collectable object has the right tag -** and it is not dead. +** and it is not dead. The option 'L == NULL' allows other +** macros using this one to be used where L is not available. */ #define checkliveness(L,obj) \ ((void)L, lua_longassert(!iscollectable(obj) || \ diff --git a/src/lua/lstate.c b/src/lua/lstate.c index 28853dc..86b3761 100644 --- a/src/lua/lstate.c +++ b/src/lua/lstate.c @@ -301,6 +301,7 @@ static void preinit_thread (lua_State *L, global_State *g) { L->openupval = NULL; L->status = LUA_OK; L->errfunc = 0; + L->oldpc = 0; } @@ -412,8 +413,8 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->gckind = KGC_INC; g->gcemergency = 0; g->finobj = g->tobefnz = g->fixedgc = NULL; - g->survival = g->old = g->reallyold = NULL; - g->finobjsur = g->finobjold = g->finobjrold = NULL; + g->firstold1 = g->survival = g->old1 = g->reallyold = NULL; + g->finobjsur = g->finobjold1 = g->finobjrold = NULL; g->sweepgc = NULL; g->gray = g->grayagain = NULL; g->weak = g->ephemeron = g->allweak = NULL; diff --git a/src/lua/lstate.h b/src/lua/lstate.h index 2e8bd6c..697d73b 100644 --- a/src/lua/lstate.h +++ b/src/lua/lstate.h @@ -32,13 +32,29 @@ ** ** 'allgc' -> 'survival': new objects; ** 'survival' -> 'old': objects that survived one collection; -** 'old' -> 'reallyold': objects that became old in last collection; +** 'old1' -> 'reallyold': objects that became old in last collection; ** 'reallyold' -> NULL: objects old for more than one cycle. ** ** 'finobj' -> 'finobjsur': new objects marked for finalization; -** 'finobjsur' -> 'finobjold': survived """"; -** 'finobjold' -> 'finobjrold': just old """"; +** 'finobjsur' -> 'finobjold1': survived """"; +** 'finobjold1' -> 'finobjrold': just old """"; ** 'finobjrold' -> NULL: really old """". +** +** All lists can contain elements older than their main ages, due +** to 'luaC_checkfinalizer' and 'udata2finalize', which move +** objects between the normal lists and the "marked for finalization" +** lists. Moreover, barriers can age young objects in young lists as +** OLD0, which then become OLD1. However, a list never contains +** elements younger than their main ages. +** +** The generational collector also uses a pointer 'firstold1', which +** points to the first OLD1 object in the list. It is used to optimize +** 'markold'. (Potentially OLD1 objects can be anywhere between 'allgc' +** and 'reallyold', but often the list has no OLD1 objects or they are +** after 'old1'.) Note the difference between it and 'old1': +** 'firstold1': no OLD1 objects before this point; there can be all +** ages after it. +** 'old1': no objects younger than OLD1 after this point. */ /* @@ -47,7 +63,7 @@ ** can become gray have such a field. The field is not the same ** in all objects, but it always has this name.) Any gray object ** must belong to one of these lists, and all objects in these lists -** must be gray: +** must be gray (with one exception explained below): ** ** 'gray': regular gray objects, still waiting to be visited. ** 'grayagain': objects that must be revisited at the atomic phase. @@ -58,6 +74,12 @@ ** 'weak': tables with weak values to be cleared; ** 'ephemeron': ephemeron tables with white->white entries; ** 'allweak': tables with weak keys and/or weak values to be cleared. +** +** The exception to that "gray rule" is the TOUCHED2 objects in +** generational mode. Those objects stay in a gray list (because they +** must be visited again at the end of the cycle), but they are marked +** black (because assignments to them must activate barriers, to move +** them back to TOUCHED1). */ @@ -257,10 +279,11 @@ typedef struct global_State { GCObject *fixedgc; /* list of objects not to be collected */ /* fields for generational collector */ GCObject *survival; /* start of objects that survived one GC cycle */ - GCObject *old; /* start of old objects */ - GCObject *reallyold; /* old objects with more than one cycle */ + GCObject *old1; /* start of old1 objects */ + GCObject *reallyold; /* objects more than one cycle old ("really old") */ + GCObject *firstold1; /* first OLD1 object in the list (if any) */ GCObject *finobjsur; /* list of survival objects with finalizers */ - GCObject *finobjold; /* list of old objects with finalizers */ + GCObject *finobjold1; /* list of old1 objects with finalizers */ GCObject *finobjrold; /* list of really old objects with finalizers */ struct lua_State *twups; /* list of threads with open upvalues */ lua_CFunction panic; /* to be called in unprotected errors */ @@ -286,7 +309,6 @@ struct lua_State { StkId top; /* first free slot in the stack */ global_State *l_G; CallInfo *ci; /* call info for current function */ - const Instruction *oldpc; /* last pc traced */ StkId stack_last; /* last free slot in the stack */ StkId stack; /* stack base */ UpVal *openupval; /* list of open upvalues in this stack */ @@ -297,6 +319,7 @@ struct lua_State { volatile lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ l_uint32 nCcalls; /* number of allowed nested C calls - 'nci' */ + int oldpc; /* last pc traced */ int stacksize; int basehookcount; int hookcount; diff --git a/src/lua/lvm.c b/src/lua/lvm.c index 66d451b..08681af 100644 --- a/src/lua/lvm.c +++ b/src/lua/lvm.c @@ -1794,7 +1794,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p)); if (trap) { luaD_hookcall(L, ci); - L->oldpc = pc + 1; /* next opcode will be seen as a "new" line */ + L->oldpc = 1; /* next opcode will be seen as a "new" line */ } updatebase(ci); /* function has new base after adjustment */ vmbreak; -- cgit v1.2.3-55-g6feb