From 85b9ec82247f6e06b802188af35d09d3159625df Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 11 Sep 2020 15:31:51 +0800 Subject: update Lua. --- src/lua/lfunc.c | 2 +- src/lua/lgc.c | 143 ++++++++++++++++++++++++++++-------------------------- src/lua/lgc.h | 29 ++++++----- src/lua/lobject.h | 6 +-- src/lua/lstate.h | 14 +++--- src/lua/ltable.c | 2 +- src/lua/ltable.h | 7 ++- src/lua/ltm.h | 9 ++++ src/lua/lundump.c | 3 ++ 9 files changed, 121 insertions(+), 94 deletions(-) (limited to 'src') diff --git a/src/lua/lfunc.c b/src/lua/lfunc.c index f8c3c44..88d4532 100644 --- a/src/lua/lfunc.c +++ b/src/lua/lfunc.c @@ -235,7 +235,7 @@ int luaF_close (lua_State *L, StkId level, int status) { setobj(L, slot, uv->v); /* move value to upvalue slot */ uv->v = slot; /* now current value lives here */ if (!iswhite(uv)) { /* neither white nor dead? */ - gray2black(uv); /* closed upvalues cannot be gray */ + nw2black(uv); /* closed upvalues cannot be gray */ luaC_barrier(L, uv, slot); } } diff --git a/src/lua/lgc.c b/src/lua/lgc.c index cb820f9..4a7bcae 100644 --- a/src/lua/lgc.c +++ b/src/lua/lgc.c @@ -60,19 +60,24 @@ #define PAUSEADJ 100 -/* mask to erase all color bits */ -#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS)) +/* mask with all color bits */ +#define maskcolors (bitmask(BLACKBIT) | WHITEBITS) -/* mask to erase all GC bits */ -#define maskgcbits (maskcolors & ~AGEBITS) +/* mask with all GC bits */ +#define maskgcbits (maskcolors | AGEBITS) /* 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) +/* make an object gray (neither white nor black) */ +#define set2gray(x) resetbits(x->marked, maskcolors) + + +/* make an object black (coming from any color) */ +#define set2black(x) \ + (x->marked = cast_byte((x->marked & ~WHITEBITS) | bitmask(BLACKBIT))) #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) @@ -135,15 +140,23 @@ static GCObject **getgclist (GCObject *o) { /* -** Link a collectable object 'o' with a known type into list pointed by 'p'. +** Link a collectable object 'o' with a known type into the list 'p'. +** (Must be a macro to access the 'gclist' field in different types.) */ -#define linkgclist(o,p) ((o)->gclist = (p), (p) = obj2gco(o)) +#define linkgclist(o,p) linkgclist_(obj2gco(o), &(o)->gclist, &(p)) + +static void linkgclist_ (GCObject *o, GCObject **pnext, GCObject **list) { + lua_assert(!isgray(o)); /* cannot be in a gray list */ + *pnext = *list; + *list = o; + set2gray(o); /* now it is */ +} /* -** Link a generic collectable object 'o' into list pointed by 'p'. +** Link a generic collectable object 'o' into the list 'p'. */ -#define linkobjgclist(o,p) (*getgclist(o) = (p), (p) = obj2gco(o)) +#define linkobjgclist(o,p) linkgclist_(obj2gco(o), getgclist(o), &(p)) @@ -219,9 +232,10 @@ 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)); - if (getage(o) != G_TOUCHED2) /* not already in gray list? */ - linkobjgclist(o, g->grayagain); /* link it in 'grayagain' */ - black2gray(o); /* make object gray (again) */ + if (getage(o) == G_TOUCHED2) /* already in gray list? */ + set2gray(o); /* make it gray to become touched1 */ + else /* link it in 'grayagain' and paint it gray */ + linkobjgclist(o, g->grayagain); if (isold(o)) /* generational mode? */ setage(o, G_TOUCHED1); /* touched in current cycle */ } @@ -230,7 +244,7 @@ void luaC_barrierback_ (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 */ + set2gray(o); /* they will be gray forever */ setage(o, G_OLD); /* and old forever */ g->allgc = o->next; /* remove object from 'allgc' list */ o->next = g->fixedgc; /* link it to 'fixedgc' list */ @@ -264,24 +278,30 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { /* -** Mark an object. Userdata, strings, and closed upvalues are visited -** and turned black here. Other objects are marked gray and added -** to appropriate list to be visited (and turned black) later. (Open -** upvalues are already linked in 'headuv' list. They are kept gray -** to avoid barriers, as their values will be revisited by the thread.) +** Mark an object. Userdata with no user values, strings, and closed +** upvalues are visited and turned black here. Open upvalues are +** already indirectly linked through their respective threads in the +** 'twups' list, so they don't go to the gray list; nevertheless, they +** are kept gray to avoid barriers, as their values will be revisited +** by the thread or by 'remarkupvals'. Other objects are added to the +** gray list to be visited (and turned black) later. Both userdata and +** upvalues can call this function recursively, but this recursion goes +** for at most two levels: An upvalue cannot refer to another upvalue +** (only closures can), and a userdata's metatable must be a table. */ static void reallymarkobject (global_State *g, GCObject *o) { - white2gray(o); switch (o->tt) { case LUA_VSHRSTR: case LUA_VLNGSTR: { - gray2black(o); + set2black(o); /* nothing to visit */ break; } case LUA_VUPVAL: { UpVal *uv = gco2upv(o); - if (!upisopen(uv)) /* open upvalues are kept gray */ - gray2black(o); + if (upisopen(uv)) + set2gray(uv); /* open upvalues are kept gray */ + else + set2black(o); /* closed upvalues are visited here */ markvalue(g, uv->v); /* mark its content */ break; } @@ -289,14 +309,14 @@ static void reallymarkobject (global_State *g, GCObject *o) { Udata *u = gco2u(o); if (u->nuvalue == 0) { /* no user values? */ markobjectN(g, u->metatable); /* mark its metatable */ - gray2black(o); /* nothing else to mark */ + set2black(o); /* nothing else to mark */ break; } /* else... */ } /* FALLTHROUGH */ case LUA_VLCL: case LUA_VCCL: case LUA_VTABLE: case LUA_VTHREAD: case LUA_VPROTO: { - linkobjgclist(o, g->gray); + linkobjgclist(o, g->gray); /* to be visited later */ break; } default: lua_assert(0); break; @@ -355,8 +375,10 @@ static int remarkupvals (global_State *g) { for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) { lua_assert(getage(uv) <= getage(thread)); work++; - if (!iswhite(uv)) /* upvalue already visited? */ + if (!iswhite(uv)) { /* upvalue already visited? */ + lua_assert(upisopen(uv) && isgray(uv)); markvalue(g, uv->v); /* mark its value */ + } } } } @@ -399,17 +421,11 @@ static void restartcollection (global_State *g) { ** 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) { +static void genlink (global_State *g, GCObject *o) { 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); + linkobjgclist(o, g->grayagain); /* link it back in 'grayagain' */ } /* everything else do not need to be linked back */ else if (getage(o) == G_TOUCHED2) changeage(o, G_TOUCHED2, G_OLD); /* advance age */ @@ -493,10 +509,8 @@ 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 { - gray2black(h); /* 'genlink' expects black objects */ - genlink(g, h); /* check whether collector still needs to see it */ - } + else + genlink(g, obj2gco(h)); /* check whether collector still needs to see it */ return marked; } @@ -516,7 +530,7 @@ static void traversestrongtable (global_State *g, Table *h) { markvalue(g, gval(n)); } } - genlink(g, h); + genlink(g, obj2gco(h)); } @@ -528,7 +542,6 @@ 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); /* turn it back to gray, as it probably goes to a list */ if (!weakkey) /* strong keys? */ traverseweakvalue(g, h); else if (!weakvalue) /* strong values? */ @@ -547,7 +560,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); - genlink(g, u); + genlink(g, obj2gco(u)); return 1 + u->nuvalue; } @@ -609,10 +622,8 @@ static int traverseLclosure (global_State *g, LClosure *cl) { static int traversethread (global_State *g, lua_State *th) { UpVal *uv; StkId o = th->stack; - if (isold(th) || g->gcstate == GCSpropagate) { + 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 || @@ -638,12 +649,11 @@ static int traversethread (global_State *g, lua_State *th) { /* -** traverse one gray object, turning it to black (except for threads, -** which are always gray). +** traverse one gray object, turning it to black. */ static lu_mem propagatemark (global_State *g) { GCObject *o = g->gray; - gray2black(o); + nw2black(o); g->gray = *getgclist(o); /* remove from 'gray' list */ switch (o->tt) { case LUA_VTABLE: return traversetable(g, gco2t(o)); @@ -681,8 +691,10 @@ static void convergeephemerons (global_State *g) { g->ephemeron = NULL; /* tables may return to this list when traversed */ changed = 0; while ((w = next) != NULL) { /* for each ephemeron table */ - next = gco2t(w)->gclist; /* list is rebuilt during loop */ - if (traverseephemeron(g, gco2t(w), dir)) { /* marked some value? */ + Table *h = gco2t(w); + next = h->gclist; /* list is rebuilt during loop */ + nw2black(h); /* out of the list (for now) */ + if (traverseephemeron(g, h, dir)) { /* marked some value? */ propagateall(g); /* propagate changes */ changed = 1; /* will have to revisit all ephemeron tables */ } @@ -809,7 +821,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 & maskgcbits) | white); + curr->marked = cast_byte((marked & ~maskgcbits) | white); p = &curr->next; /* go to next element */ } } @@ -1028,8 +1040,8 @@ static void setpause (global_State *g); /* ** 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. +** are now old---must be in a gray list. Everything else is not in a +** gray list. Open upvalues are also kept gray. */ static void sweep2old (lua_State *L, GCObject **p) { GCObject *curr; @@ -1045,10 +1057,11 @@ static void sweep2old (lua_State *L, GCObject **p) { 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 if (curr->tt == LUA_VUPVAL && upisopen(gco2upv(curr))) + set2gray(curr); /* open upvalues are always gray */ else /* everything else is black */ - gray2black(curr); /* OK if already black */ + nw2black(curr); p = &curr->next; /* go to next element */ } } @@ -1087,7 +1100,7 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, } else { /* correct mark and age */ if (getage(curr) == G_NEW) { /* new objects go back to white */ - int marked = curr->marked & maskgcbits; /* erase GC bits */ + 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 */ @@ -1110,7 +1123,7 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, 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 & maskgcbits) | white); + p->marked = cast_byte((p->marked & ~maskgcbits) | white); } @@ -1131,7 +1144,7 @@ static GCObject **correctgraylist (GCObject **p) { 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 */ + nw2black(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 */ } @@ -1141,15 +1154,9 @@ static GCObject **correctgraylist (GCObject **p) { } else { /* everything else is removed */ lua_assert(isold(curr)); /* young objects should be white here */ - if (getage(curr) == G_TOUCHED2) { /* advance from TOUCHED2... */ + 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 */ - } - else { - /* everything else in a gray list should be gray */ - lua_assert(isgray(curr)); - gray2black(curr); /* make object black (to be removed) */ - } + nw2black(curr); /* make object black (to be removed) */ goto remove; } remove: *p = *next; continue; @@ -1184,10 +1191,8 @@ static void markold (global_State *g, GCObject *from, GCObject *to) { 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 */ + if (isblack(p)) reallymarkobject(g, p); - } } } } diff --git a/src/lua/lgc.h b/src/lua/lgc.h index b972472..073e2a4 100644 --- a/src/lua/lgc.h +++ b/src/lua/lgc.h @@ -12,16 +12,16 @@ #include "lstate.h" /* -** Collectable objects may have one of three colors: white, which -** means the object is not marked; gray, which means the -** object is marked, but its references may be not marked; and -** black, which means that the object and all its references are marked. -** The main invariant of the garbage collector, while marking objects, -** is that a black object can never point to a white one. Moreover, -** any gray object must be in a "gray list" (gray, grayagain, weak, -** allweak, ephemeron) so that it can be visited again before finishing -** the collection cycle. These lists have no meaning when the invariant -** is not being enforced (e.g., sweep phase). +** Collectable objects may have one of three colors: white, which means +** the object is not marked; gray, which means the object is marked, but +** its references may be not marked; and black, which means that the +** object and all its references are marked. The main invariant of the +** garbage collector, while marking objects, is that a black object can +** never point to a white one. Moreover, any gray object must be in a +** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it +** can be visited again before finishing the collection cycle. (Open +** upvalues are an exception to this rule.) These lists have no meaning +** when the invariant is not being enforced (e.g., sweep phase). */ @@ -69,14 +69,16 @@ /* ** Layout for bit use in 'marked' field. First three bits are -** used for object "age" in generational mode. Last bit is free -** to be used by respective objects. +** used for object "age" in generational mode. Last bit is used +** by tests. */ #define WHITE0BIT 3 /* object is white (type 0) */ #define WHITE1BIT 4 /* object is white (type 1) */ #define BLACKBIT 5 /* object is black */ #define FINALIZEDBIT 6 /* object has been marked for finalization */ +#define TESTBIT 7 + #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) @@ -94,7 +96,8 @@ #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) #define changewhite(x) ((x)->marked ^= WHITEBITS) -#define gray2black(x) l_setbit((x)->marked, BLACKBIT) +#define nw2black(x) \ + check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT)) #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS) diff --git a/src/lua/lobject.h b/src/lua/lobject.h index 1620223..a9d4578 100644 --- a/src/lua/lobject.h +++ b/src/lua/lobject.h @@ -704,9 +704,9 @@ typedef union Node { */ #define BITRAS (1 << 7) -#define isrealasize(t) (!((t)->marked & BITRAS)) -#define setrealasize(t) ((t)->marked &= cast_byte(~BITRAS)) -#define setnorealasize(t) ((t)->marked |= BITRAS) +#define isrealasize(t) (!((t)->flags & BITRAS)) +#define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS)) +#define setnorealasize(t) ((t)->flags |= BITRAS) typedef struct Table { diff --git a/src/lua/lstate.h b/src/lua/lstate.h index 697d73b..1b6bcdf 100644 --- a/src/lua/lstate.h +++ b/src/lua/lstate.h @@ -63,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 (with one exception explained below): +** must be gray (with two exceptions explained below): ** ** 'gray': regular gray objects, still waiting to be visited. ** 'grayagain': objects that must be revisited at the atomic phase. @@ -75,11 +75,13 @@ ** '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). +** The exceptions to that "gray rule" are: +** - TOUCHED2 objects in generational mode 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). +** - Open upvales are kept gray to avoid barriers, but they stay out +** of gray lists. (They don't even have a 'gclist' field.) */ diff --git a/src/lua/ltable.c b/src/lua/ltable.c index d7eb69a..5a0d066 100644 --- a/src/lua/ltable.c +++ b/src/lua/ltable.c @@ -583,7 +583,7 @@ Table *luaH_new (lua_State *L) { GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table)); Table *t = gco2t(o); t->metatable = NULL; - t->flags = cast_byte(~0); + t->flags = cast_byte(maskflags); /* table has no metamethod fields */ t->array = NULL; t->alimit = 0; setnodevector(L, t, 0); diff --git a/src/lua/ltable.h b/src/lua/ltable.h index ebd7f8e..c0060f4 100644 --- a/src/lua/ltable.h +++ b/src/lua/ltable.h @@ -15,7 +15,12 @@ #define gnext(n) ((n)->u.next) -#define invalidateTMcache(t) ((t)->flags = 0) +/* +** Clear all bits of fast-access metamethods, which means that the table +** may have any of these metamethods. (First access that fails after the +** clearing will set the bit again.) +*/ +#define invalidateTMcache(t) ((t)->flags &= ~maskflags) /* true when 't' is using 'dummynode' as its hash part */ diff --git a/src/lua/ltm.h b/src/lua/ltm.h index 99b545e..73b833c 100644 --- a/src/lua/ltm.h +++ b/src/lua/ltm.h @@ -45,6 +45,15 @@ typedef enum { } TMS; +/* +** Mask with 1 in all fast-access methods. A 1 in any of these bits +** in the flag of a (meta)table means the metatable does not have the +** corresponding metamethod field. (Bit 7 of the flag is used for +** 'isrealasize'.) +*/ +#define maskflags (~(~0u << (TM_EQ + 1))) + + /* ** Test whether there is no tagmethod. ** (Because tagmethods use raw accesses, the result may be an "empty" nil.) diff --git a/src/lua/lundump.c b/src/lua/lundump.c index cb124d6..5aa55c4 100644 --- a/src/lua/lundump.c +++ b/src/lua/lundump.c @@ -120,7 +120,10 @@ static TString *loadStringN (LoadState *S, Proto *p) { } else { /* long string */ ts = luaS_createlngstrobj(L, size); /* create string */ + setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */ + luaD_inctop(L); loadVector(S, getstr(ts), size); /* load directly in final place */ + L->top--; /* pop string */ } luaC_objbarrier(L, p, ts); return ts; -- cgit v1.2.3-55-g6feb