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