LCOV - code coverage report
Current view: top level - src - lgc.c Coverage Total Hit
Test: Lua 5.3.6 Lines: 81.8 % 571 467
Test Date: 2024-04-28 10:23:15
Legend: Lines: hit not hit

            Line data    Source code
       1              : /*
       2              : ** $Id: lgc.c,v 2.215.1.2 2017/08/31 16:15:27 roberto Exp $
       3              : ** Garbage Collector
       4              : ** See Copyright Notice in lua.h
       5              : */
       6              : 
       7              : #define lgc_c
       8              : #define LUA_CORE
       9              : 
      10              : #include "lprefix.h"
      11              : 
      12              : 
      13              : #include <string.h>
      14              : 
      15              : #include "lua.h"
      16              : 
      17              : #include "ldebug.h"
      18              : #include "ldo.h"
      19              : #include "lfunc.h"
      20              : #include "lgc.h"
      21              : #include "lmem.h"
      22              : #include "lobject.h"
      23              : #include "lstate.h"
      24              : #include "lstring.h"
      25              : #include "ltable.h"
      26              : #include "ltm.h"
      27              : 
      28              : 
      29              : /*
      30              : ** internal state for collector while inside the atomic phase. The
      31              : ** collector should never be in this state while running regular code.
      32              : */
      33              : #define GCSinsideatomic         (GCSpause + 1)
      34              : 
      35              : /*
      36              : ** cost of sweeping one element (the size of a small object divided
      37              : ** by some adjust for the sweep speed)
      38              : */
      39              : #define GCSWEEPCOST     ((sizeof(TString) + 4) / 4)
      40              : 
      41              : /* maximum number of elements to sweep in each single step */
      42              : #define GCSWEEPMAX      (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4))
      43              : 
      44              : /* cost of calling one finalizer */
      45              : #define GCFINALIZECOST  GCSWEEPCOST
      46              : 
      47              : 
      48              : /*
      49              : ** macro to adjust 'stepmul': 'stepmul' is actually used like
      50              : ** 'stepmul / STEPMULADJ' (value chosen by tests)
      51              : */
      52              : #define STEPMULADJ              200
      53              : 
      54              : 
      55              : /*
      56              : ** macro to adjust 'pause': 'pause' is actually used like
      57              : ** 'pause / PAUSEADJ' (value chosen by tests)
      58              : */
      59              : #define PAUSEADJ                100
      60              : 
      61              : 
      62              : /*
      63              : ** 'makewhite' erases all color bits then sets only the current white
      64              : ** bit
      65              : */
      66              : #define maskcolors      (~(bitmask(BLACKBIT) | WHITEBITS))
      67              : #define makewhite(g,x)  \
      68              :  (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g)))
      69              : 
      70              : #define white2gray(x)   resetbits(x->marked, WHITEBITS)
      71              : #define black2gray(x)   resetbit(x->marked, BLACKBIT)
      72              : 
      73              : 
      74              : #define valiswhite(x)   (iscollectable(x) && iswhite(gcvalue(x)))
      75              : 
      76              : #define checkdeadkey(n) lua_assert(!ttisdeadkey(gkey(n)) || ttisnil(gval(n)))
      77              : 
      78              : 
      79              : #define checkconsistency(obj)  \
      80              :   lua_longassert(!iscollectable(obj) || righttt(obj))
      81              : 
      82              : 
      83              : #define markvalue(g,o) { checkconsistency(o); \
      84              :   if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
      85              : 
      86              : #define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); }
      87              : 
      88              : /*
      89              : ** mark an object that can be NULL (either because it is really optional,
      90              : ** or it was stripped as debug info, or inside an uncompleted structure)
      91              : */
      92              : #define markobjectN(g,t)        { if (t) markobject(g,t); }
      93              : 
      94              : static void reallymarkobject (global_State *g, GCObject *o);
      95              : 
      96              : 
      97              : /*
      98              : ** {======================================================
      99              : ** Generic functions
     100              : ** =======================================================
     101              : */
     102              : 
     103              : 
     104              : /*
     105              : ** one after last element in a hash array
     106              : */
     107              : #define gnodelast(h)    gnode(h, cast(size_t, sizenode(h)))
     108              : 
     109              : 
     110              : /*
     111              : ** link collectable object 'o' into list pointed by 'p'
     112              : */
     113              : #define linkgclist(o,p) ((o)->gclist = (p), (p) = obj2gco(o))
     114              : 
     115              : 
     116              : /*
     117              : ** If key is not marked, mark its entry as dead. This allows key to be
     118              : ** collected, but keeps its entry in the table.  A dead node is needed
     119              : ** when Lua looks up for a key (it may be part of a chain) and when
     120              : ** traversing a weak table (key might be removed from the table during
     121              : ** traversal). Other places never manipulate dead keys, because its
     122              : ** associated nil value is enough to signal that the entry is logically
     123              : ** empty.
     124              : */
     125        20853 : static void removeentry (Node *n) {
     126              :   lua_assert(ttisnil(gval(n)));
     127        20853 :   if (valiswhite(gkey(n)))
     128            1 :     setdeadvalue(wgkey(n));  /* unused and unmarked key; remove it */
     129        20853 : }
     130              : 
     131              : 
     132              : /*
     133              : ** tells whether a key or value can be cleared from a weak
     134              : ** table. Non-collectable objects are never removed from weak
     135              : ** tables. Strings behave as 'values', so are never removed too. for
     136              : ** other objects: if really collected, cannot keep them; for objects
     137              : ** being finalized, keep them in keys, but not in values
     138              : */
     139            0 : static int iscleared (global_State *g, const TValue *o) {
     140            0 :   if (!iscollectable(o)) return 0;
     141            0 :   else if (ttisstring(o)) {
     142            0 :     markobject(g, tsvalue(o));  /* strings are 'values', so are never weak */
     143            0 :     return 0;
     144              :   }
     145            0 :   else return iswhite(gcvalue(o));
     146              : }
     147              : 
     148              : 
     149              : /*
     150              : ** barrier that moves collector forward, that is, mark the white object
     151              : ** being pointed by a black object. (If in sweep phase, clear the black
     152              : ** object to white [sweep it] to avoid other barrier calls for this
     153              : ** same object.)
     154              : */
     155          879 : void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) {
     156          879 :   global_State *g = G(L);
     157              :   lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
     158          879 :   if (keepinvariant(g))  /* must keep invariant? */
     159          879 :     reallymarkobject(g, v);  /* restore invariant */
     160              :   else {  /* sweep phase */
     161              :     lua_assert(issweepphase(g));
     162            0 :     makewhite(g, o);  /* mark main obj. as white to avoid other barriers */
     163              :   }
     164          879 : }
     165              : 
     166              : 
     167              : /*
     168              : ** barrier that moves collector backward, that is, mark the black object
     169              : ** pointing to a white object as gray again.
     170              : */
     171          348 : void luaC_barrierback_ (lua_State *L, Table *t) {
     172          348 :   global_State *g = G(L);
     173              :   lua_assert(isblack(t) && !isdead(g, t));
     174          348 :   black2gray(t);  /* make table gray (again) */
     175          348 :   linkgclist(t, g->grayagain);
     176          348 : }
     177              : 
     178              : 
     179              : /*
     180              : ** barrier for assignments to closed upvalues. Because upvalues are
     181              : ** shared among closures, it is impossible to know the color of all
     182              : ** closures pointing to it. So, we assume that the object being assigned
     183              : ** must be marked.
     184              : */
     185         4944 : void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) {
     186         4944 :   global_State *g = G(L);
     187         4944 :   GCObject *o = gcvalue(uv->v);
     188              :   lua_assert(!upisopen(uv));  /* ensured by macro luaC_upvalbarrier */
     189         4944 :   if (keepinvariant(g))
     190         1988 :     markobject(g, o);
     191         4944 : }
     192              : 
     193              : 
     194         5088 : void luaC_fix (lua_State *L, GCObject *o) {
     195         5088 :   global_State *g = G(L);
     196              :   lua_assert(g->allgc == o);  /* object must be 1st in 'allgc' list! */
     197         5088 :   white2gray(o);  /* they will be gray forever */
     198         5088 :   g->allgc = o->next;  /* remove object from 'allgc' list */
     199         5088 :   o->next = g->fixedgc;  /* link it to 'fixedgc' list */
     200         5088 :   g->fixedgc = o;
     201         5088 : }
     202              : 
     203              : 
     204              : /*
     205              : ** create a new collectable object (with given type and size) and link
     206              : ** it to 'allgc' list.
     207              : */
     208        65929 : GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) {
     209        65929 :   global_State *g = G(L);
     210        65929 :   GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz));
     211        65929 :   o->marked = luaC_white(g);
     212        65929 :   o->tt = tt;
     213        65929 :   o->next = g->allgc;
     214        65929 :   g->allgc = o;
     215        65929 :   return o;
     216              : }
     217              : 
     218              : /* }====================================================== */
     219              : 
     220              : 
     221              : 
     222              : /*
     223              : ** {======================================================
     224              : ** Mark functions
     225              : ** =======================================================
     226              : */
     227              : 
     228              : 
     229              : /*
     230              : ** mark an object. Userdata, strings, and closed upvalues are visited
     231              : ** and turned black here. Other objects are marked gray and added
     232              : ** to appropriate list to be visited (and turned black) later. (Open
     233              : ** upvalues are already linked in 'headuv' list.)
     234              : */
     235        46110 : static void reallymarkobject (global_State *g, GCObject *o) {
     236        46110 :  reentry:
     237        46110 :   white2gray(o);
     238        46110 :   switch (o->tt) {
     239        34719 :     case LUA_TSHRSTR: {
     240        34719 :       gray2black(o);
     241        34719 :       g->GCmemtrav += sizelstring(gco2ts(o)->shrlen);
     242        34719 :       break;
     243              :     }
     244         1001 :     case LUA_TLNGSTR: {
     245         1001 :       gray2black(o);
     246         1001 :       g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen);
     247         1001 :       break;
     248              :     }
     249          483 :     case LUA_TUSERDATA: {
     250              :       TValue uvalue;
     251          483 :       markobjectN(g, gco2u(o)->metatable);  /* mark its metatable */
     252          483 :       gray2black(o);
     253          483 :       g->GCmemtrav += sizeudata(gco2u(o));
     254          483 :       getuservalue(g->mainthread, gco2u(o), &uvalue);
     255          483 :       if (valiswhite(&uvalue)) {  /* markvalue(g, &uvalue); */
     256            0 :         o = gcvalue(&uvalue);
     257            0 :         goto reentry;
     258              :       }
     259          483 :       break;
     260              :     }
     261         2278 :     case LUA_TLCL: {
     262         2278 :       linkgclist(gco2lcl(o), g->gray);
     263         2278 :       break;
     264              :     }
     265          784 :     case LUA_TCCL: {
     266          784 :       linkgclist(gco2ccl(o), g->gray);
     267          784 :       break;
     268              :     }
     269         3321 :     case LUA_TTABLE: {
     270         3321 :       linkgclist(gco2t(o), g->gray);
     271         3321 :       break;
     272              :     }
     273          280 :     case LUA_TTHREAD: {
     274          280 :       linkgclist(gco2th(o), g->gray);
     275          280 :       break;
     276              :     }
     277         3244 :     case LUA_TPROTO: {
     278         3244 :       linkgclist(gco2p(o), g->gray);
     279         3244 :       break;
     280              :     }
     281            0 :     default: lua_assert(0); break;
     282              :   }
     283        46110 : }
     284              : 
     285              : 
     286              : /*
     287              : ** mark metamethods for basic types
     288              : */
     289          510 : static void markmt (global_State *g) {
     290              :   int i;
     291         5100 :   for (i=0; i < LUA_NUMTAGS; i++)
     292         4590 :     markobjectN(g, g->mt[i]);
     293          510 : }
     294              : 
     295              : 
     296              : /*
     297              : ** mark all objects in list of being-finalized
     298              : */
     299          510 : static void markbeingfnz (global_State *g) {
     300              :   GCObject *o;
     301          516 :   for (o = g->tobefnz; o != NULL; o = o->next)
     302            6 :     markobject(g, o);
     303          510 : }
     304              : 
     305              : 
     306              : /*
     307              : ** Mark all values stored in marked open upvalues from non-marked threads.
     308              : ** (Values from marked threads were already marked when traversing the
     309              : ** thread.) Remove from the list threads that no longer have upvalues and
     310              : ** not-marked threads.
     311              : */
     312          245 : static void remarkupvals (global_State *g) {
     313              :   lua_State *thread;
     314          245 :   lua_State **p = &g->twups;
     315          285 :   while ((thread = *p) != NULL) {
     316              :     lua_assert(!isblack(thread));  /* threads are never black */
     317           40 :     if (isgray(thread) && thread->openupval != NULL)
     318           21 :       p = &thread->twups;  /* keep marked thread with upvalues in the list */
     319              :     else {  /* thread is not marked or without upvalues */
     320              :       UpVal *uv;
     321           19 :       *p = thread->twups;  /* remove thread from the list */
     322           19 :       thread->twups = thread;  /* mark that it is out of list */
     323           19 :       for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) {
     324            0 :         if (uv->u.open.touched) {
     325            0 :           markvalue(g, uv->v);  /* remark upvalue's value */
     326            0 :           uv->u.open.touched = 0;
     327              :         }
     328              :       }
     329              :     }
     330              :   }
     331          245 : }
     332              : 
     333              : 
     334              : /*
     335              : ** mark root set and reset all gray lists, to start a new collection
     336              : */
     337          265 : static void restartcollection (global_State *g) {
     338          265 :   g->gray = g->grayagain = NULL;
     339          265 :   g->weak = g->allweak = g->ephemeron = NULL;
     340          265 :   markobject(g, g->mainthread);
     341          265 :   markvalue(g, &g->l_registry);
     342          265 :   markmt(g);
     343          265 :   markbeingfnz(g);  /* mark any finalizing object left from previous cycle */
     344          265 : }
     345              : 
     346              : /* }====================================================== */
     347              : 
     348              : 
     349              : /*
     350              : ** {======================================================
     351              : ** Traverse functions
     352              : ** =======================================================
     353              : */
     354              : 
     355              : /*
     356              : ** Traverse a table with weak values and link it to proper list. During
     357              : ** propagate phase, keep it in 'grayagain' list, to be revisited in the
     358              : ** atomic phase. In the atomic phase, if table has any white value,
     359              : ** put it in 'weak' list, to be cleared.
     360              : */
     361            0 : static void traverseweakvalue (global_State *g, Table *h) {
     362            0 :   Node *n, *limit = gnodelast(h);
     363              :   /* if there is array part, assume it may have white values (it is not
     364              :      worth traversing it now just to check) */
     365            0 :   int hasclears = (h->sizearray > 0);
     366            0 :   for (n = gnode(h, 0); n < limit; n++) {  /* traverse hash part */
     367              :     checkdeadkey(n);
     368            0 :     if (ttisnil(gval(n)))  /* entry is empty? */
     369            0 :       removeentry(n);  /* remove it */
     370              :     else {
     371              :       lua_assert(!ttisnil(gkey(n)));
     372            0 :       markvalue(g, gkey(n));  /* mark key */
     373            0 :       if (!hasclears && iscleared(g, gval(n)))  /* is there a white value? */
     374            0 :         hasclears = 1;  /* table will have to be cleared */
     375              :     }
     376              :   }
     377            0 :   if (g->gcstate == GCSpropagate)
     378            0 :     linkgclist(h, g->grayagain);  /* must retraverse it in atomic phase */
     379            0 :   else if (hasclears)
     380            0 :     linkgclist(h, g->weak);  /* has to be cleared later */
     381            0 : }
     382              : 
     383              : 
     384              : /*
     385              : ** Traverse an ephemeron table and link it to proper list. Returns true
     386              : ** iff any object was marked during this traversal (which implies that
     387              : ** convergence has to continue). During propagation phase, keep table
     388              : ** in 'grayagain' list, to be visited again in the atomic phase. In
     389              : ** the atomic phase, if table has any white->white entry, it has to
     390              : ** be revisited during ephemeron convergence (as that key may turn
     391              : ** black). Otherwise, if it has any white key, table has to be cleared
     392              : ** (in the atomic phase).
     393              : */
     394            0 : static int traverseephemeron (global_State *g, Table *h) {
     395            0 :   int marked = 0;  /* true if an object is marked in this traversal */
     396            0 :   int hasclears = 0;  /* true if table has white keys */
     397            0 :   int hasww = 0;  /* true if table has entry "white-key -> white-value" */
     398            0 :   Node *n, *limit = gnodelast(h);
     399              :   unsigned int i;
     400              :   /* traverse array part */
     401            0 :   for (i = 0; i < h->sizearray; i++) {
     402            0 :     if (valiswhite(&h->array[i])) {
     403            0 :       marked = 1;
     404            0 :       reallymarkobject(g, gcvalue(&h->array[i]));
     405              :     }
     406              :   }
     407              :   /* traverse hash part */
     408            0 :   for (n = gnode(h, 0); n < limit; n++) {
     409              :     checkdeadkey(n);
     410            0 :     if (ttisnil(gval(n)))  /* entry is empty? */
     411            0 :       removeentry(n);  /* remove it */
     412            0 :     else if (iscleared(g, gkey(n))) {  /* key is not marked (yet)? */
     413            0 :       hasclears = 1;  /* table must be cleared */
     414            0 :       if (valiswhite(gval(n)))  /* value not marked yet? */
     415            0 :         hasww = 1;  /* white-white entry */
     416              :     }
     417            0 :     else if (valiswhite(gval(n))) {  /* value not marked yet? */
     418            0 :       marked = 1;
     419            0 :       reallymarkobject(g, gcvalue(gval(n)));  /* mark it now */
     420              :     }
     421              :   }
     422              :   /* link table into proper list */
     423            0 :   if (g->gcstate == GCSpropagate)
     424            0 :     linkgclist(h, g->grayagain);  /* must retraverse it in atomic phase */
     425            0 :   else if (hasww)  /* table has white->white entries? */
     426            0 :     linkgclist(h, g->ephemeron);  /* have to propagate again */
     427            0 :   else if (hasclears)  /* table has white keys? */
     428            0 :     linkgclist(h, g->allweak);  /* may have to clean white keys */
     429            0 :   return marked;
     430              : }
     431              : 
     432              : 
     433         3589 : static void traversestrongtable (global_State *g, Table *h) {
     434         3589 :   Node *n, *limit = gnodelast(h);
     435              :   unsigned int i;
     436         6449 :   for (i = 0; i < h->sizearray; i++)  /* traverse array part */
     437         2860 :     markvalue(g, &h->array[i]);
     438        62671 :   for (n = gnode(h, 0); n < limit; n++) {  /* traverse hash part */
     439              :     checkdeadkey(n);
     440        59082 :     if (ttisnil(gval(n)))  /* entry is empty? */
     441        20853 :       removeentry(n);  /* remove it */
     442              :     else {
     443              :       lua_assert(!ttisnil(gkey(n)));
     444        38229 :       markvalue(g, gkey(n));  /* mark key */
     445        38229 :       markvalue(g, gval(n));  /* mark value */
     446              :     }
     447              :   }
     448         3589 : }
     449              : 
     450              : 
     451         3589 : static lu_mem traversetable (global_State *g, Table *h) {
     452              :   const char *weakkey, *weakvalue;
     453         3589 :   const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
     454         3589 :   markobjectN(g, h->metatable);
     455         3589 :   if (mode && ttisstring(mode) &&  /* is there a weak mode? */
     456            0 :       ((weakkey = strchr(svalue(mode), 'k')),
     457            0 :        (weakvalue = strchr(svalue(mode), 'v')),
     458            0 :        (weakkey || weakvalue))) {  /* is really weak? */
     459            0 :     black2gray(h);  /* keep table gray */
     460            0 :     if (!weakkey)  /* strong keys? */
     461            0 :       traverseweakvalue(g, h);
     462            0 :     else if (!weakvalue)  /* strong values? */
     463            0 :       traverseephemeron(g, h);
     464              :     else  /* all weak */
     465            0 :       linkgclist(h, g->allweak);  /* nothing to traverse now */
     466              :   }
     467              :   else  /* not weak */
     468         3589 :     traversestrongtable(g, h);
     469         3589 :   return sizeof(Table) + sizeof(TValue) * h->sizearray +
     470         3589 :                          sizeof(Node) * cast(size_t, allocsizenode(h));
     471              : }
     472              : 
     473              : 
     474              : /*
     475              : ** Traverse a prototype. (While a prototype is being build, its
     476              : ** arrays can be larger than needed; the extra slots are filled with
     477              : ** NULL, so the use of 'markobjectN')
     478              : */
     479         3125 : static int traverseproto (global_State *g, Proto *f) {
     480              :   int i;
     481         3125 :   if (f->cache && iswhite(f->cache))
     482          513 :     f->cache = NULL;  /* allow cache to be collected */
     483         3125 :   markobjectN(g, f->source);
     484        23534 :   for (i = 0; i < f->sizek; i++)  /* mark literals */
     485        20409 :     markvalue(g, &f->k[i]);
     486         8524 :   for (i = 0; i < f->sizeupvalues; i++)  /* mark upvalue names */
     487         5399 :     markobjectN(g, f->upvalues[i].name);
     488         5536 :   for (i = 0; i < f->sizep; i++)  /* mark nested protos */
     489         2411 :     markobjectN(g, f->p[i]);
     490        11870 :   for (i = 0; i < f->sizelocvars; i++)  /* mark local-variable names */
     491         8745 :     markobjectN(g, f->locvars[i].varname);
     492         3125 :   return sizeof(Proto) + sizeof(Instruction) * f->sizecode +
     493         3125 :                          sizeof(Proto *) * f->sizep +
     494         3125 :                          sizeof(TValue) * f->sizek +
     495         3125 :                          sizeof(int) * f->sizelineinfo +
     496         6250 :                          sizeof(LocVar) * f->sizelocvars +
     497         3125 :                          sizeof(Upvaldesc) * f->sizeupvalues;
     498              : }
     499              : 
     500              : 
     501          783 : static lu_mem traverseCclosure (global_State *g, CClosure *cl) {
     502              :   int i;
     503         1574 :   for (i = 0; i < cl->nupvalues; i++)  /* mark its upvalues */
     504          791 :     markvalue(g, &cl->upvalue[i]);
     505          783 :   return sizeCclosure(cl->nupvalues);
     506              : }
     507              : 
     508              : /*
     509              : ** open upvalues point to values in a thread, so those values should
     510              : ** be marked when the thread is traversed except in the atomic phase
     511              : ** (because then the value cannot be changed by the thread and the
     512              : ** thread may not be traversed again)
     513              : */
     514         2235 : static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
     515              :   int i;
     516         2235 :   markobjectN(g, cl->p);  /* mark its prototype */
     517        10260 :   for (i = 0; i < cl->nupvalues; i++) {  /* mark its upvalues */
     518         8025 :     UpVal *uv = cl->upvals[i];
     519         8025 :     if (uv != NULL) {
     520         7999 :       if (upisopen(uv) && g->gcstate != GCSinsideatomic)
     521         1143 :         uv->u.open.touched = 1;  /* can be marked in 'remarkupvals' */
     522              :       else
     523         6856 :         markvalue(g, uv->v);
     524              :     }
     525              :   }
     526         2235 :   return sizeLclosure(cl->nupvalues);
     527              : }
     528              : 
     529              : 
     530          531 : static lu_mem traversethread (global_State *g, lua_State *th) {
     531          531 :   StkId o = th->stack;
     532          531 :   if (o == NULL)
     533            0 :     return 1;  /* stack not completely built yet */
     534              :   lua_assert(g->gcstate == GCSinsideatomic ||
     535              :              th->openupval == NULL || isintwups(th));
     536         8920 :   for (; o < th->top; o++)  /* mark live elements in the stack */
     537         8389 :     markvalue(g, o);
     538          531 :   if (g->gcstate == GCSinsideatomic) {  /* final traversal? */
     539          259 :     StkId lim = th->stack + th->stacksize;  /* real end of stack */
     540        10150 :     for (; o < lim; o++)  /* clear not-marked stack slice */
     541         9891 :       setnilvalue(o);
     542              :     /* 'remarkupvals' may have removed thread from 'twups' list */
     543          259 :     if (!isintwups(th) && th->openupval != NULL) {
     544            0 :       th->twups = g->twups;  /* link it back to the list */
     545            0 :       g->twups = th;
     546              :     }
     547              :   }
     548          272 :   else if (g->gckind != KGC_EMERGENCY)
     549          272 :     luaD_shrinkstack(th); /* do not change stack in emergency cycle */
     550          531 :   return (sizeof(lua_State) + sizeof(TValue) * th->stacksize +
     551          531 :           sizeof(CallInfo) * th->nci);
     552              : }
     553              : 
     554              : 
     555              : /*
     556              : ** traverse one gray object, turning it to black (except for threads,
     557              : ** which are always gray).
     558              : */
     559        10263 : static void propagatemark (global_State *g) {
     560              :   lu_mem size;
     561        10263 :   GCObject *o = g->gray;
     562              :   lua_assert(isgray(o));
     563        10263 :   gray2black(o);
     564        10263 :   switch (o->tt) {
     565         3589 :     case LUA_TTABLE: {
     566         3589 :       Table *h = gco2t(o);
     567         3589 :       g->gray = h->gclist;  /* remove from 'gray' list */
     568         3589 :       size = traversetable(g, h);
     569         3589 :       break;
     570              :     }
     571         2235 :     case LUA_TLCL: {
     572         2235 :       LClosure *cl = gco2lcl(o);
     573         2235 :       g->gray = cl->gclist;  /* remove from 'gray' list */
     574         2235 :       size = traverseLclosure(g, cl);
     575         2235 :       break;
     576              :     }
     577          783 :     case LUA_TCCL: {
     578          783 :       CClosure *cl = gco2ccl(o);
     579          783 :       g->gray = cl->gclist;  /* remove from 'gray' list */
     580          783 :       size = traverseCclosure(g, cl);
     581          783 :       break;
     582              :     }
     583          531 :     case LUA_TTHREAD: {
     584          531 :       lua_State *th = gco2th(o);
     585          531 :       g->gray = th->gclist;  /* remove from 'gray' list */
     586          531 :       linkgclist(th, g->grayagain);  /* insert into 'grayagain' list */
     587          531 :       black2gray(o);
     588          531 :       size = traversethread(g, th);
     589          531 :       break;
     590              :     }
     591         3125 :     case LUA_TPROTO: {
     592         3125 :       Proto *p = gco2p(o);
     593         3125 :       g->gray = p->gclist;  /* remove from 'gray' list */
     594         3125 :       size = traverseproto(g, p);
     595         3125 :       break;
     596              :     }
     597            0 :     default: lua_assert(0); return;
     598              :   }
     599        10263 :   g->GCmemtrav += size;
     600              : }
     601              : 
     602              : 
     603          980 : static void propagateall (global_State *g) {
     604         2755 :   while (g->gray) propagatemark(g);
     605          980 : }
     606              : 
     607              : 
     608          490 : static void convergeephemerons (global_State *g) {
     609              :   int changed;
     610              :   do {
     611              :     GCObject *w;
     612          490 :     GCObject *next = g->ephemeron;  /* get ephemeron list */
     613          490 :     g->ephemeron = NULL;  /* tables may return to this list when traversed */
     614          490 :     changed = 0;
     615          490 :     while ((w = next) != NULL) {
     616            0 :       next = gco2t(w)->gclist;
     617            0 :       if (traverseephemeron(g, gco2t(w))) {  /* traverse marked some value? */
     618            0 :         propagateall(g);  /* propagate changes */
     619            0 :         changed = 1;  /* will have to revisit all ephemeron tables */
     620              :       }
     621              :     }
     622          490 :   } while (changed);
     623          490 : }
     624              : 
     625              : /* }====================================================== */
     626              : 
     627              : 
     628              : /*
     629              : ** {======================================================
     630              : ** Sweep Functions
     631              : ** =======================================================
     632              : */
     633              : 
     634              : 
     635              : /*
     636              : ** clear entries with unmarked keys from all weaktables in list 'l' up
     637              : ** to element 'f'
     638              : */
     639          490 : static void clearkeys (global_State *g, GCObject *l, GCObject *f) {
     640          490 :   for (; l != f; l = gco2t(l)->gclist) {
     641            0 :     Table *h = gco2t(l);
     642            0 :     Node *n, *limit = gnodelast(h);
     643            0 :     for (n = gnode(h, 0); n < limit; n++) {
     644            0 :       if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) {
     645            0 :         setnilvalue(gval(n));  /* remove value ... */
     646              :       }
     647            0 :       if (ttisnil(gval(n)))  /* is entry empty? */
     648            0 :         removeentry(n);  /* remove entry from table */
     649              :     }
     650              :   }
     651          490 : }
     652              : 
     653              : 
     654              : /*
     655              : ** clear entries with unmarked values from all weaktables in list 'l' up
     656              : ** to element 'f'
     657              : */
     658          980 : static void clearvalues (global_State *g, GCObject *l, GCObject *f) {
     659          980 :   for (; l != f; l = gco2t(l)->gclist) {
     660            0 :     Table *h = gco2t(l);
     661            0 :     Node *n, *limit = gnodelast(h);
     662              :     unsigned int i;
     663            0 :     for (i = 0; i < h->sizearray; i++) {
     664            0 :       TValue *o = &h->array[i];
     665            0 :       if (iscleared(g, o))  /* value was collected? */
     666            0 :         setnilvalue(o);  /* remove value */
     667              :     }
     668            0 :     for (n = gnode(h, 0); n < limit; n++) {
     669            0 :       if (!ttisnil(gval(n)) && iscleared(g, gval(n))) {
     670            0 :         setnilvalue(gval(n));  /* remove value ... */
     671            0 :         removeentry(n);  /* and remove entry from table */
     672              :       }
     673              :     }
     674              :   }
     675          980 : }
     676              : 
     677              : 
     678         9942 : void luaC_upvdeccount (lua_State *L, UpVal *uv) {
     679              :   lua_assert(uv->refcount > 0);
     680         9942 :   uv->refcount--;
     681         9942 :   if (uv->refcount == 0 && !upisopen(uv))
     682         6049 :     luaM_free(L, uv);
     683         9942 : }
     684              : 
     685              : 
     686         3342 : static void freeLclosure (lua_State *L, LClosure *cl) {
     687              :   int i;
     688        13325 :   for (i = 0; i < cl->nupvalues; i++) {
     689         9983 :     UpVal *uv = cl->upvals[i];
     690         9983 :     if (uv)
     691         9942 :       luaC_upvdeccount(L, uv);
     692              :   }
     693         3342 :   luaM_freemem(L, cl, sizeLclosure(cl->nupvalues));
     694         3342 : }
     695              : 
     696              : 
     697        61516 : static void freeobj (lua_State *L, GCObject *o) {
     698        61516 :   switch (o->tt) {
     699         2182 :     case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
     700         3342 :     case LUA_TLCL: {
     701         3342 :       freeLclosure(L, gco2lcl(o));
     702         3342 :       break;
     703              :     }
     704          404 :     case LUA_TCCL: {
     705          404 :       luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues));
     706          404 :       break;
     707              :     }
     708         8663 :     case LUA_TTABLE: luaH_free(L, gco2t(o)); break;
     709           27 :     case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break;
     710          371 :     case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break;
     711        41908 :     case LUA_TSHRSTR:
     712        41908 :       luaS_remove(L, gco2ts(o));  /* remove it from hash table */
     713        41908 :       luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen));
     714        41908 :       break;
     715         4619 :     case LUA_TLNGSTR: {
     716         4619 :       luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen));
     717         4619 :       break;
     718              :     }
     719        61516 :     default: lua_assert(0);
     720              :   }
     721        61516 : }
     722              : 
     723              : 
     724              : #define sweepwholelist(L,p)     sweeplist(L,p,MAX_LUMEM)
     725              : static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count);
     726              : 
     727              : 
     728              : /*
     729              : ** sweep at most 'count' elements from a list of GCObjects erasing dead
     730              : ** objects, where a dead object is one marked with the old (non current)
     731              : ** white; change all non-dead objects back to white, preparing for next
     732              : ** collection cycle. Return where to continue the traversal or NULL if
     733              : ** list is finished.
     734              : */
     735         1880 : static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
     736         1880 :   global_State *g = G(L);
     737         1880 :   int ow = otherwhite(g);
     738         1880 :   int white = luaC_white(g);  /* current white */
     739       103339 :   while (*p != NULL && count-- > 0) {
     740       101459 :     GCObject *curr = *p;
     741       101459 :     int marked = curr->marked;
     742       101459 :     if (isdeadm(ow, marked)) {  /* is 'curr' dead? */
     743        61516 :       *p = curr->next;  /* remove 'curr' from list */
     744        61516 :       freeobj(L, curr);  /* erase 'curr' */
     745              :     }
     746              :     else {  /* change mark to 'white' */
     747        39943 :       curr->marked = cast_byte((marked & maskcolors) | white);
     748        39943 :       p = &curr->next;  /* go to next element */
     749              :     }
     750              :   }
     751         1880 :   return (*p == NULL) ? NULL : p;
     752              : }
     753              : 
     754              : 
     755              : /*
     756              : ** sweep a list until a live object (or end of list)
     757              : */
     758            0 : static GCObject **sweeptolive (lua_State *L, GCObject **p) {
     759            0 :   GCObject **old = p;
     760              :   do {
     761            0 :     p = sweeplist(L, p, 1);
     762            0 :   } while (p == old);
     763            0 :   return p;
     764              : }
     765              : 
     766              : /* }====================================================== */
     767              : 
     768              : 
     769              : /*
     770              : ** {======================================================
     771              : ** Finalization
     772              : ** =======================================================
     773              : */
     774              : 
     775              : /*
     776              : ** If possible, shrink string table
     777              : */
     778          245 : static void checkSizes (lua_State *L, global_State *g) {
     779          245 :   if (g->gckind != KGC_EMERGENCY) {
     780          245 :     l_mem olddebt = g->GCdebt;
     781          245 :     if (g->strt.nuse < g->strt.size / 4)  /* string table too big? */
     782           11 :       luaS_resize(L, g->strt.size / 2);  /* shrink it a little */
     783          245 :     g->GCestimate += g->GCdebt - olddebt;  /* update estimate */
     784              :   }
     785          245 : }
     786              : 
     787              : 
     788          444 : static GCObject *udata2finalize (global_State *g) {
     789          444 :   GCObject *o = g->tobefnz;  /* get first element */
     790              :   lua_assert(tofinalize(o));
     791          444 :   g->tobefnz = o->next;  /* remove it from 'tobefnz' list */
     792          444 :   o->next = g->allgc;  /* return it to 'allgc' list */
     793          444 :   g->allgc = o;
     794          444 :   resetbit(o->marked, FINALIZEDBIT);  /* object is "normal" again */
     795          444 :   if (issweepphase(g))
     796            9 :     makewhite(g, o);  /* "sweep" object */
     797          444 :   return o;
     798              : }
     799              : 
     800              : 
     801          444 : static void dothecall (lua_State *L, void *ud) {
     802              :   UNUSED(ud);
     803          444 :   luaD_callnoyield(L, L->top - 2, 0);
     804          444 : }
     805              : 
     806              : 
     807          444 : static void GCTM (lua_State *L, int propagateerrors) {
     808          444 :   global_State *g = G(L);
     809              :   const TValue *tm;
     810              :   TValue v;
     811          444 :   setgcovalue(L, &v, udata2finalize(g));
     812          444 :   tm = luaT_gettmbyobj(L, &v, TM_GC);
     813          444 :   if (tm != NULL && ttisfunction(tm)) {  /* is there a finalizer? */
     814              :     int status;
     815          444 :     lu_byte oldah = L->allowhook;
     816          444 :     int running  = g->gcrunning;
     817          444 :     L->allowhook = 0;  /* stop debug hooks during GC metamethod */
     818          444 :     g->gcrunning = 0;  /* avoid GC steps */
     819          444 :     setobj2s(L, L->top, tm);  /* push finalizer... */
     820          444 :     setobj2s(L, L->top + 1, &v);  /* ... and its argument */
     821          444 :     L->top += 2;  /* and (next line) call the finalizer */
     822          444 :     L->ci->callstatus |= CIST_FIN;  /* will run a finalizer */
     823          444 :     status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0);
     824          444 :     L->ci->callstatus &= ~CIST_FIN;  /* not running a finalizer anymore */
     825          444 :     L->allowhook = oldah;  /* restore hooks */
     826          444 :     g->gcrunning = running;  /* restore state */
     827          444 :     if (status != LUA_OK && propagateerrors) {  /* error while running __gc? */
     828            0 :       if (status == LUA_ERRRUN) {  /* is there an error object? */
     829            0 :         const char *msg = (ttisstring(L->top - 1))
     830            0 :                             ? svalue(L->top - 1)
     831            0 :                             : "no message";
     832            0 :         luaO_pushfstring(L, "error in __gc metamethod (%s)", msg);
     833            0 :         status = LUA_ERRGCMM;  /* error in __gc metamethod */
     834              :       }
     835            0 :       luaD_throw(L, status);  /* re-throw error */
     836              :     }
     837              :   }
     838          444 : }
     839              : 
     840              : 
     841              : /*
     842              : ** call a few (up to 'g->gcfinnum') finalizers
     843              : */
     844          936 : static int runafewfinalizers (lua_State *L) {
     845          936 :   global_State *g = G(L);
     846              :   unsigned int i;
     847              :   lua_assert(!g->tobefnz || g->gcfinnum > 0);
     848          942 :   for (i = 0; g->tobefnz && i < g->gcfinnum; i++)
     849            6 :     GCTM(L, 1);  /* call one finalizer */
     850         1872 :   g->gcfinnum = (!g->tobefnz) ? 0  /* nothing more to finalize? */
     851          936 :                     : g->gcfinnum * 2;  /* else call a few more next time */
     852          936 :   return i;
     853              : }
     854              : 
     855              : 
     856              : /*
     857              : ** call all pending finalizers
     858              : */
     859           88 : static void callallpendingfinalizers (lua_State *L) {
     860           88 :   global_State *g = G(L);
     861          526 :   while (g->tobefnz)
     862          438 :     GCTM(L, 0);
     863           88 : }
     864              : 
     865              : 
     866              : /*
     867              : ** find last 'next' field in list 'p' list (to add elements in its end)
     868              : */
     869          333 : static GCObject **findlast (GCObject **p) {
     870          333 :   while (*p != NULL)
     871            0 :     p = &(*p)->next;
     872          333 :   return p;
     873              : }
     874              : 
     875              : 
     876              : /*
     877              : ** move all unreachable objects (or 'all' objects) that need
     878              : ** finalization from list 'finobj' to list 'tobefnz' (to be finalized)
     879              : */
     880          333 : static void separatetobefnz (global_State *g, int all) {
     881              :   GCObject *curr;
     882          333 :   GCObject **p = &g->finobj;
     883          333 :   GCObject **lastnext = findlast(&g->tobefnz);
     884         1341 :   while ((curr = *p) != NULL) {  /* traverse all finalizable objects */
     885              :     lua_assert(tofinalize(curr));
     886         1008 :     if (!(iswhite(curr) || all))  /* not being collected? */
     887          564 :       p = &curr->next;  /* don't bother with it */
     888              :     else {
     889          444 :       *p = curr->next;  /* remove 'curr' from 'finobj' list */
     890          444 :       curr->next = *lastnext;  /* link at the end of 'tobefnz' list */
     891          444 :       *lastnext = curr;
     892          444 :       lastnext = &curr->next;
     893              :     }
     894              :   }
     895          333 : }
     896              : 
     897              : 
     898              : /*
     899              : ** if object 'o' has a finalizer, remove it from 'allgc' list (must
     900              : ** search the list to find it) and link it in 'finobj' list.
     901              : */
     902          577 : void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
     903          577 :   global_State *g = G(L);
     904          577 :   if (tofinalize(o) ||                 /* obj. is already marked... */
     905          577 :       gfasttm(g, mt, TM_GC) == NULL)   /* or has no finalizer? */
     906           97 :     return;  /* nothing to be done */
     907              :   else {  /* move 'o' to 'finobj' list */
     908              :     GCObject **p;
     909          480 :     if (issweepphase(g)) {
     910            3 :       makewhite(g, o);  /* "sweep" object 'o' */
     911            3 :       if (g->sweepgc == &o->next)  /* should not remove 'sweepgc' object */
     912            0 :         g->sweepgc = sweeptolive(L, g->sweepgc);  /* change 'sweepgc' */
     913              :     }
     914              :     /* search for pointer pointing to 'o' */
     915          568 :     for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ }
     916          480 :     *p = o->next;  /* remove 'o' from 'allgc' list */
     917          480 :     o->next = g->finobj;  /* link it in 'finobj' list */
     918          480 :     g->finobj = o;
     919          480 :     l_setbit(o->marked, FINALIZEDBIT);  /* mark it as such */
     920              :   }
     921              : }
     922              : 
     923              : /* }====================================================== */
     924              : 
     925              : 
     926              : 
     927              : /*
     928              : ** {======================================================
     929              : ** GC control
     930              : ** =======================================================
     931              : */
     932              : 
     933              : 
     934              : /*
     935              : ** Set a reasonable "time" to wait before starting a new GC cycle; cycle
     936              : ** will start when memory use hits threshold. (Division by 'estimate'
     937              : ** should be OK: it cannot be zero (because Lua cannot even start with
     938              : ** less than PAUSEADJ bytes).
     939              : */
     940          244 : static void setpause (global_State *g) {
     941              :   l_mem threshold, debt;
     942          244 :   l_mem estimate = g->GCestimate / PAUSEADJ;  /* adjust 'estimate' */
     943              :   lua_assert(estimate > 0);
     944          488 :   threshold = (g->gcpause < MAX_LMEM / estimate)  /* overflow? */
     945          244 :             ? estimate * g->gcpause  /* no overflow */
     946          244 :             : MAX_LMEM;  /* overflow; truncate to maximum */
     947          244 :   debt = gettotalbytes(g) - threshold;
     948          244 :   luaE_setdebt(g, debt);
     949          244 : }
     950              : 
     951              : 
     952              : /*
     953              : ** Enter first sweep phase.
     954              : ** The call to 'sweeplist' tries to make pointer point to an object
     955              : ** inside the list (instead of to the header), so that the real sweep do
     956              : ** not need to skip objects created between "now" and the start of the
     957              : ** real sweep.
     958              : */
     959          246 : static void entersweep (lua_State *L) {
     960          246 :   global_State *g = G(L);
     961          246 :   g->gcstate = GCSswpallgc;
     962              :   lua_assert(g->sweepgc == NULL);
     963          246 :   g->sweepgc = sweeplist(L, &g->allgc, 1);
     964          246 : }
     965              : 
     966              : 
     967           88 : void luaC_freeallobjects (lua_State *L) {
     968           88 :   global_State *g = G(L);
     969           88 :   separatetobefnz(g, 1);  /* separate all objects with finalizers */
     970              :   lua_assert(g->finobj == NULL);
     971           88 :   callallpendingfinalizers(L);
     972              :   lua_assert(g->tobefnz == NULL);
     973           88 :   g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */
     974           88 :   g->gckind = KGC_NORMAL;
     975           88 :   sweepwholelist(L, &g->finobj);
     976           88 :   sweepwholelist(L, &g->allgc);
     977           88 :   sweepwholelist(L, &g->fixedgc);  /* collect fixed objects */
     978              :   lua_assert(g->strt.nuse == 0);
     979           88 : }
     980              : 
     981              : 
     982          245 : static l_mem atomic (lua_State *L) {
     983          245 :   global_State *g = G(L);
     984              :   l_mem work;
     985              :   GCObject *origweak, *origall;
     986          245 :   GCObject *grayagain = g->grayagain;  /* save original list */
     987              :   lua_assert(g->ephemeron == NULL && g->weak == NULL);
     988              :   lua_assert(!iswhite(g->mainthread));
     989          245 :   g->gcstate = GCSinsideatomic;
     990          245 :   g->GCmemtrav = 0;  /* start counting work */
     991          245 :   markobject(g, L);  /* mark running thread */
     992              :   /* registry and global metatables may be changed by API */
     993          245 :   markvalue(g, &g->l_registry);
     994          245 :   markmt(g);  /* mark global metatables */
     995              :   /* remark occasional upvalues of (maybe) dead threads */
     996          245 :   remarkupvals(g);
     997          245 :   propagateall(g);  /* propagate changes */
     998          245 :   work = g->GCmemtrav;  /* stop counting (do not recount 'grayagain') */
     999          245 :   g->gray = grayagain;
    1000          245 :   propagateall(g);  /* traverse 'grayagain' list */
    1001          245 :   g->GCmemtrav = 0;  /* restart counting */
    1002          245 :   convergeephemerons(g);
    1003              :   /* at this point, all strongly accessible objects are marked. */
    1004              :   /* Clear values from weak tables, before checking finalizers */
    1005          245 :   clearvalues(g, g->weak, NULL);
    1006          245 :   clearvalues(g, g->allweak, NULL);
    1007          245 :   origweak = g->weak; origall = g->allweak;
    1008          245 :   work += g->GCmemtrav;  /* stop counting (objects being finalized) */
    1009          245 :   separatetobefnz(g, 0);  /* separate objects to be finalized */
    1010          245 :   g->gcfinnum = 1;  /* there may be objects to be finalized */
    1011          245 :   markbeingfnz(g);  /* mark objects that will be finalized */
    1012          245 :   propagateall(g);  /* remark, to propagate 'resurrection' */
    1013          245 :   g->GCmemtrav = 0;  /* restart counting */
    1014          245 :   convergeephemerons(g);
    1015              :   /* at this point, all resurrected objects are marked. */
    1016              :   /* remove dead objects from weak tables */
    1017          245 :   clearkeys(g, g->ephemeron, NULL);  /* clear keys from all ephemeron tables */
    1018          245 :   clearkeys(g, g->allweak, NULL);  /* clear keys from all 'allweak' tables */
    1019              :   /* clear values from resurrected weak tables */
    1020          245 :   clearvalues(g, g->weak, origweak);
    1021          245 :   clearvalues(g, g->allweak, origall);
    1022          245 :   luaS_clearcache(g);
    1023          245 :   g->currentwhite = cast_byte(otherwhite(g));  /* flip current white */
    1024          245 :   work += g->GCmemtrav;  /* complete counting */
    1025          245 :   return work;  /* estimate of memory marked by 'atomic' */
    1026              : }
    1027              : 
    1028              : 
    1029         1370 : static lu_mem sweepstep (lua_State *L, global_State *g,
    1030              :                          int nextstate, GCObject **nextlist) {
    1031         1370 :   if (g->sweepgc) {
    1032         1370 :     l_mem olddebt = g->GCdebt;
    1033         1370 :     g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
    1034         1370 :     g->GCestimate += g->GCdebt - olddebt;  /* update estimate */
    1035         1370 :     if (g->sweepgc)  /* is there still something to sweep? */
    1036          635 :       return (GCSWEEPMAX * GCSWEEPCOST);
    1037              :   }
    1038              :   /* else enter next state */
    1039          735 :   g->gcstate = nextstate;
    1040          735 :   g->sweepgc = nextlist;
    1041          735 :   return 0;
    1042              : }
    1043              : 
    1044              : 
    1045        10859 : static lu_mem singlestep (lua_State *L) {
    1046        10859 :   global_State *g = G(L);
    1047        10859 :   switch (g->gcstate) {
    1048          265 :     case GCSpause: {
    1049          265 :       g->GCmemtrav = g->strt.size * sizeof(GCObject*);
    1050          265 :       restartcollection(g);
    1051          265 :       g->gcstate = GCSpropagate;
    1052          265 :       return g->GCmemtrav;
    1053              :     }
    1054         8488 :     case GCSpropagate: {
    1055         8488 :       g->GCmemtrav = 0;
    1056              :       lua_assert(g->gray);
    1057         8488 :       propagatemark(g);
    1058         8488 :        if (g->gray == NULL)  /* no more gray objects? */
    1059          246 :         g->gcstate = GCSatomic;  /* finish propagate phase */
    1060         8488 :       return g->GCmemtrav;  /* memory traversed in this step */
    1061              :     }
    1062          245 :     case GCSatomic: {
    1063              :       lu_mem work;
    1064          245 :       propagateall(g);  /* make sure gray list is empty */
    1065          245 :       work = atomic(L);  /* work is what was traversed by 'atomic' */
    1066          245 :       entersweep(L);
    1067          245 :       g->GCestimate = gettotalbytes(g);  /* first estimate */;
    1068          245 :       return work;
    1069              :     }
    1070          880 :     case GCSswpallgc: {  /* sweep "regular" objects */
    1071          880 :       return sweepstep(L, g, GCSswpfinobj, &g->finobj);
    1072              :     }
    1073          245 :     case GCSswpfinobj: {  /* sweep objects with finalizers */
    1074          245 :       return sweepstep(L, g, GCSswptobefnz, &g->tobefnz);
    1075              :     }
    1076          245 :     case GCSswptobefnz: {  /* sweep objects to be finalized */
    1077          245 :       return sweepstep(L, g, GCSswpend, NULL);
    1078              :     }
    1079          245 :     case GCSswpend: {  /* finish sweeps */
    1080          245 :       makewhite(g, g->mainthread);  /* sweep main thread */
    1081          245 :       checkSizes(L, g);
    1082          245 :       g->gcstate = GCScallfin;
    1083          245 :       return 0;
    1084              :     }
    1085          246 :     case GCScallfin: {  /* call remaining finalizers */
    1086          246 :       if (g->tobefnz && g->gckind != KGC_EMERGENCY) {
    1087            1 :         int n = runafewfinalizers(L);
    1088            1 :         return (n * GCFINALIZECOST);
    1089              :       }
    1090              :       else {  /* emergency mode or no more finalizers */
    1091          245 :         g->gcstate = GCSpause;  /* finish collection */
    1092          245 :         return 0;
    1093              :       }
    1094              :     }
    1095            0 :     default: lua_assert(0); return 0;
    1096              :   }
    1097              : }
    1098              : 
    1099              : 
    1100              : /*
    1101              : ** advances the garbage collector until it reaches a state allowed
    1102              : ** by 'statemask'
    1103              : */
    1104            8 : void luaC_runtilstate (lua_State *L, int statesmask) {
    1105            8 :   global_State *g = G(L);
    1106          326 :   while (!testbit(statesmask, g->gcstate))
    1107          318 :     singlestep(L);
    1108            8 : }
    1109              : 
    1110              : 
    1111              : /*
    1112              : ** get GC debt and convert it from Kb to 'work units' (avoid zero debt
    1113              : ** and overflows)
    1114              : */
    1115         1177 : static l_mem getdebt (global_State *g) {
    1116         1177 :   l_mem debt = g->GCdebt;
    1117         1177 :   int stepmul = g->gcstepmul;
    1118         1177 :   if (debt <= 0) return 0;  /* minimal debt */
    1119              :   else {
    1120         1174 :     debt = (debt / STEPMULADJ) + 1;
    1121         1174 :     debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM;
    1122         1174 :     return debt;
    1123              :   }
    1124              : }
    1125              : 
    1126              : /*
    1127              : ** performs a basic GC step when collector is running
    1128              : */
    1129         1177 : void luaC_step (lua_State *L) {
    1130         1177 :   global_State *g = G(L);
    1131         1177 :   l_mem debt = getdebt(g);  /* GC deficit (be paid now) */
    1132         1177 :   if (!g->gcrunning) {  /* not running? */
    1133            0 :     luaE_setdebt(g, -GCSTEPSIZE * 10);  /* avoid being called too often */
    1134            0 :     return;
    1135              :   }
    1136              :   do {  /* repeat until pause or enough "credit" (negative debt) */
    1137        10541 :     lu_mem work = singlestep(L);  /* perform one single step */
    1138        10541 :     debt -= work;
    1139        10541 :   } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause);
    1140         1177 :   if (g->gcstate == GCSpause)
    1141          242 :     setpause(g);  /* pause until next cycle */
    1142              :   else {
    1143          935 :     debt = (debt / g->gcstepmul) * STEPMULADJ;  /* convert 'work units' to Kb */
    1144          935 :     luaE_setdebt(g, debt);
    1145          935 :     runafewfinalizers(L);
    1146              :   }
    1147              : }
    1148              : 
    1149              : 
    1150              : /*
    1151              : ** Performs a full GC cycle; if 'isemergency', set a flag to avoid
    1152              : ** some operations which could change the interpreter state in some
    1153              : ** unexpected ways (running finalizers and shrinking some structures).
    1154              : ** Before running the collection, check 'keepinvariant'; if it is true,
    1155              : ** there may be some objects marked as black, so the collector has
    1156              : ** to sweep all objects to turn them back to white (as white has not
    1157              : ** changed, nothing will be collected).
    1158              : */
    1159            2 : void luaC_fullgc (lua_State *L, int isemergency) {
    1160            2 :   global_State *g = G(L);
    1161              :   lua_assert(g->gckind == KGC_NORMAL);
    1162            2 :   if (isemergency) g->gckind = KGC_EMERGENCY;  /* set flag */
    1163            2 :   if (keepinvariant(g)) {  /* black objects? */
    1164            1 :     entersweep(L); /* sweep everything to turn them back to white */
    1165              :   }
    1166              :   /* finish any pending sweep phase to start a new cycle */
    1167            2 :   luaC_runtilstate(L, bitmask(GCSpause));
    1168            2 :   luaC_runtilstate(L, ~bitmask(GCSpause));  /* start new collection */
    1169            2 :   luaC_runtilstate(L, bitmask(GCScallfin));  /* run up to finalizers */
    1170              :   /* estimate must be correct after a full GC cycle */
    1171              :   lua_assert(g->GCestimate == gettotalbytes(g));
    1172            2 :   luaC_runtilstate(L, bitmask(GCSpause));  /* finish collection */
    1173            2 :   g->gckind = KGC_NORMAL;
    1174            2 :   setpause(g);
    1175            2 : }
    1176              : 
    1177              : /* }====================================================== */
    1178              : 
    1179              : 
        

Generated by: LCOV version 2.0-1