lgc.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  1. /*
  2. ** $Id: lgc.c $
  3. ** Garbage Collector
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lgc_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "lua.h"
  12. #include "ldebug.h"
  13. #include "ldo.h"
  14. #include "lfunc.h"
  15. #include "lgc.h"
  16. #include "lmem.h"
  17. #include "lobject.h"
  18. #include "lstate.h"
  19. #include "lstring.h"
  20. #include "ltable.h"
  21. #include "ltm.h"
  22. /*
  23. ** Maximum number of elements to sweep in each single step.
  24. ** (Large enough to dissipate fixed overheads but small enough
  25. ** to allow small steps for the collector.)
  26. */
  27. #define GCSWEEPMAX 100
  28. /*
  29. ** Maximum number of finalizers to call in each single step.
  30. */
  31. #define GCFINMAX 10
  32. /*
  33. ** Cost of calling one finalizer.
  34. */
  35. #define GCFINALIZECOST 50
  36. /*
  37. ** The equivalent, in bytes, of one unit of "work" (visiting a slot,
  38. ** sweeping an object, etc.)
  39. */
  40. #define WORK2MEM sizeof(TValue)
  41. /*
  42. ** macro to adjust 'pause': 'pause' is actually used like
  43. ** 'pause / PAUSEADJ' (value chosen by tests)
  44. */
  45. #define PAUSEADJ 100
  46. /* mask with all color bits */
  47. #define maskcolors (bitmask(BLACKBIT) | WHITEBITS)
  48. /* mask with all GC bits */
  49. #define maskgcbits (maskcolors | AGEBITS)
  50. /* macro to erase all color bits then set only the current white bit */
  51. #define makewhite(g,x) \
  52. (x->marked = cast_byte((x->marked & ~maskcolors) | luaC_white(g)))
  53. /* make an object gray (neither white nor black) */
  54. #define set2gray(x) resetbits(x->marked, maskcolors)
  55. /* make an object black (coming from any color) */
  56. #define set2black(x) \
  57. (x->marked = cast_byte((x->marked & ~WHITEBITS) | bitmask(BLACKBIT)))
  58. #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
  59. #define keyiswhite(n) (keyiscollectable(n) && iswhite(gckey(n)))
  60. /*
  61. ** Protected access to objects in values
  62. */
  63. #define gcvalueN(o) (iscollectable(o) ? gcvalue(o) : NULL)
  64. /*
  65. ** Access to collectable objects in array part of tables
  66. */
  67. #define gcvalarr(t,i) \
  68. ((*getArrTag(t,i) & BIT_ISCOLLECTABLE) ? getArrVal(t,i)->gc : NULL)
  69. #define markvalue(g,o) { checkliveness(g->mainthread,o); \
  70. if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
  71. #define markkey(g, n) { if keyiswhite(n) reallymarkobject(g,gckey(n)); }
  72. #define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); }
  73. /*
  74. ** mark an object that can be NULL (either because it is really optional,
  75. ** or it was stripped as debug info, or inside an uncompleted structure)
  76. */
  77. #define markobjectN(g,t) { if (t) markobject(g,t); }
  78. static void reallymarkobject (global_State *g, GCObject *o);
  79. static lu_mem atomic (lua_State *L);
  80. static void entersweep (lua_State *L);
  81. /*
  82. ** {======================================================
  83. ** Generic functions
  84. ** =======================================================
  85. */
  86. /*
  87. ** one after last element in a hash array
  88. */
  89. #define gnodelast(h) gnode(h, cast_sizet(sizenode(h)))
  90. static GCObject **getgclist (GCObject *o) {
  91. switch (o->tt) {
  92. case LUA_VTABLE: return &gco2t(o)->gclist;
  93. case LUA_VLCL: return &gco2lcl(o)->gclist;
  94. case LUA_VCCL: return &gco2ccl(o)->gclist;
  95. case LUA_VTHREAD: return &gco2th(o)->gclist;
  96. case LUA_VPROTO: return &gco2p(o)->gclist;
  97. case LUA_VUSERDATA: {
  98. Udata *u = gco2u(o);
  99. lua_assert(u->nuvalue > 0);
  100. return &u->gclist;
  101. }
  102. default: lua_assert(0); return 0;
  103. }
  104. }
  105. /*
  106. ** Link a collectable object 'o' with a known type into the list 'p'.
  107. ** (Must be a macro to access the 'gclist' field in different types.)
  108. */
  109. #define linkgclist(o,p) linkgclist_(obj2gco(o), &(o)->gclist, &(p))
  110. static void linkgclist_ (GCObject *o, GCObject **pnext, GCObject **list) {
  111. lua_assert(!isgray(o)); /* cannot be in a gray list */
  112. *pnext = *list;
  113. *list = o;
  114. set2gray(o); /* now it is */
  115. }
  116. /*
  117. ** Link a generic collectable object 'o' into the list 'p'.
  118. */
  119. #define linkobjgclist(o,p) linkgclist_(obj2gco(o), getgclist(o), &(p))
  120. /*
  121. ** Clear keys for empty entries in tables. If entry is empty, mark its
  122. ** entry as dead. This allows the collection of the key, but keeps its
  123. ** entry in the table: its removal could break a chain and could break
  124. ** a table traversal. Other places never manipulate dead keys, because
  125. ** its associated empty value is enough to signal that the entry is
  126. ** logically empty.
  127. */
  128. static void clearkey (Node *n) {
  129. lua_assert(isempty(gval(n)));
  130. if (keyiscollectable(n))
  131. setdeadkey(n); /* unused key; remove it */
  132. }
  133. /*
  134. ** tells whether a key or value can be cleared from a weak
  135. ** table. Non-collectable objects are never removed from weak
  136. ** tables. Strings behave as 'values', so are never removed too. for
  137. ** other objects: if really collected, cannot keep them; for objects
  138. ** being finalized, keep them in keys, but not in values
  139. */
  140. static int iscleared (global_State *g, const GCObject *o) {
  141. if (o == NULL) return 0; /* non-collectable value */
  142. else if (novariant(o->tt) == LUA_TSTRING) {
  143. markobject(g, o); /* strings are 'values', so are never weak */
  144. return 0;
  145. }
  146. else return iswhite(o);
  147. }
  148. /*
  149. ** Barrier that moves collector forward, that is, marks the white object
  150. ** 'v' being pointed by the black object 'o'. In the generational
  151. ** mode, 'v' must also become old, if 'o' is old; however, it cannot
  152. ** be changed directly to OLD, because it may still point to non-old
  153. ** objects. So, it is marked as OLD0. In the next cycle it will become
  154. ** OLD1, and in the next it will finally become OLD (regular old). By
  155. ** then, any object it points to will also be old. If called in the
  156. ** incremental sweep phase, it clears the black object to white (sweep
  157. ** it) to avoid other barrier calls for this same object. (That cannot
  158. ** be done is generational mode, as its sweep does not distinguish
  159. ** whites from deads.)
  160. */
  161. void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) {
  162. global_State *g = G(L);
  163. lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
  164. if (keepinvariant(g)) { /* must keep invariant? */
  165. reallymarkobject(g, v); /* restore invariant */
  166. if (isold(o)) {
  167. lua_assert(!isold(v)); /* white object could not be old */
  168. setage(v, G_OLD0); /* restore generational invariant */
  169. }
  170. }
  171. else { /* sweep phase */
  172. lua_assert(issweepphase(g));
  173. if (g->gckind == KGC_INC) /* incremental mode? */
  174. makewhite(g, o); /* mark 'o' as white to avoid other barriers */
  175. }
  176. }
  177. /*
  178. ** barrier that moves collector backward, that is, mark the black object
  179. ** pointing to a white object as gray again.
  180. */
  181. void luaC_barrierback_ (lua_State *L, GCObject *o) {
  182. global_State *g = G(L);
  183. lua_assert(isblack(o) && !isdead(g, o));
  184. lua_assert((g->gckind == KGC_GEN) == (isold(o) && getage(o) != G_TOUCHED1));
  185. if (getage(o) == G_TOUCHED2) /* already in gray list? */
  186. set2gray(o); /* make it gray to become touched1 */
  187. else /* link it in 'grayagain' and paint it gray */
  188. linkobjgclist(o, g->grayagain);
  189. if (isold(o)) /* generational mode? */
  190. setage(o, G_TOUCHED1); /* touched in current cycle */
  191. }
  192. void luaC_fix (lua_State *L, GCObject *o) {
  193. global_State *g = G(L);
  194. lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */
  195. set2gray(o); /* they will be gray forever */
  196. setage(o, G_OLD); /* and old forever */
  197. g->allgc = o->next; /* remove object from 'allgc' list */
  198. o->next = g->fixedgc; /* link it to 'fixedgc' list */
  199. g->fixedgc = o;
  200. }
  201. /*
  202. ** create a new collectable object (with given type, size, and offset)
  203. ** and link it to 'allgc' list.
  204. */
  205. GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) {
  206. global_State *g = G(L);
  207. char *p = cast_charp(luaM_newobject(L, novariant(tt), sz));
  208. GCObject *o = cast(GCObject *, p + offset);
  209. o->marked = luaC_white(g);
  210. o->tt = tt;
  211. o->next = g->allgc;
  212. g->allgc = o;
  213. return o;
  214. }
  215. GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) {
  216. return luaC_newobjdt(L, tt, sz, 0);
  217. }
  218. /* }====================================================== */
  219. /*
  220. ** {======================================================
  221. ** Mark functions
  222. ** =======================================================
  223. */
  224. /*
  225. ** Mark an object. Userdata with no user values, strings, and closed
  226. ** upvalues are visited and turned black here. Open upvalues are
  227. ** already indirectly linked through their respective threads in the
  228. ** 'twups' list, so they don't go to the gray list; nevertheless, they
  229. ** are kept gray to avoid barriers, as their values will be revisited
  230. ** by the thread or by 'remarkupvals'. Other objects are added to the
  231. ** gray list to be visited (and turned black) later. Both userdata and
  232. ** upvalues can call this function recursively, but this recursion goes
  233. ** for at most two levels: An upvalue cannot refer to another upvalue
  234. ** (only closures can), and a userdata's metatable must be a table.
  235. */
  236. static void reallymarkobject (global_State *g, GCObject *o) {
  237. switch (o->tt) {
  238. case LUA_VSHRSTR:
  239. case LUA_VLNGSTR: {
  240. set2black(o); /* nothing to visit */
  241. break;
  242. }
  243. case LUA_VUPVAL: {
  244. UpVal *uv = gco2upv(o);
  245. if (upisopen(uv))
  246. set2gray(uv); /* open upvalues are kept gray */
  247. else
  248. set2black(uv); /* closed upvalues are visited here */
  249. markvalue(g, uv->v.p); /* mark its content */
  250. break;
  251. }
  252. case LUA_VUSERDATA: {
  253. Udata *u = gco2u(o);
  254. if (u->nuvalue == 0) { /* no user values? */
  255. markobjectN(g, u->metatable); /* mark its metatable */
  256. set2black(u); /* nothing else to mark */
  257. break;
  258. }
  259. /* else... */
  260. } /* FALLTHROUGH */
  261. case LUA_VLCL: case LUA_VCCL: case LUA_VTABLE:
  262. case LUA_VTHREAD: case LUA_VPROTO: {
  263. linkobjgclist(o, g->gray); /* to be visited later */
  264. break;
  265. }
  266. default: lua_assert(0); break;
  267. }
  268. }
  269. /*
  270. ** mark metamethods for basic types
  271. */
  272. static void markmt (global_State *g) {
  273. int i;
  274. for (i=0; i < LUA_NUMTAGS; i++)
  275. markobjectN(g, g->mt[i]);
  276. }
  277. /*
  278. ** mark all objects in list of being-finalized
  279. */
  280. static lu_mem markbeingfnz (global_State *g) {
  281. GCObject *o;
  282. lu_mem count = 0;
  283. for (o = g->tobefnz; o != NULL; o = o->next) {
  284. count++;
  285. markobject(g, o);
  286. }
  287. return count;
  288. }
  289. /*
  290. ** For each non-marked thread, simulates a barrier between each open
  291. ** upvalue and its value. (If the thread is collected, the value will be
  292. ** assigned to the upvalue, but then it can be too late for the barrier
  293. ** to act. The "barrier" does not need to check colors: A non-marked
  294. ** thread must be young; upvalues cannot be older than their threads; so
  295. ** any visited upvalue must be young too.) Also removes the thread from
  296. ** the list, as it was already visited. Removes also threads with no
  297. ** upvalues, as they have nothing to be checked. (If the thread gets an
  298. ** upvalue later, it will be linked in the list again.)
  299. */
  300. static int remarkupvals (global_State *g) {
  301. lua_State *thread;
  302. lua_State **p = &g->twups;
  303. int work = 0; /* estimate of how much work was done here */
  304. while ((thread = *p) != NULL) {
  305. work++;
  306. if (!iswhite(thread) && thread->openupval != NULL)
  307. p = &thread->twups; /* keep marked thread with upvalues in the list */
  308. else { /* thread is not marked or without upvalues */
  309. UpVal *uv;
  310. lua_assert(!isold(thread) || thread->openupval == NULL);
  311. *p = thread->twups; /* remove thread from the list */
  312. thread->twups = thread; /* mark that it is out of list */
  313. for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) {
  314. lua_assert(getage(uv) <= getage(thread));
  315. work++;
  316. if (!iswhite(uv)) { /* upvalue already visited? */
  317. lua_assert(upisopen(uv) && isgray(uv));
  318. markvalue(g, uv->v.p); /* mark its value */
  319. }
  320. }
  321. }
  322. }
  323. return work;
  324. }
  325. static void cleargraylists (global_State *g) {
  326. g->gray = g->grayagain = NULL;
  327. g->weak = g->allweak = g->ephemeron = NULL;
  328. }
  329. /*
  330. ** mark root set and reset all gray lists, to start a new collection
  331. */
  332. static void restartcollection (global_State *g) {
  333. cleargraylists(g);
  334. markobject(g, g->mainthread);
  335. markvalue(g, &g->l_registry);
  336. markmt(g);
  337. markbeingfnz(g); /* mark any finalizing object left from previous cycle */
  338. }
  339. /* }====================================================== */
  340. /*
  341. ** {======================================================
  342. ** Traverse functions
  343. ** =======================================================
  344. */
  345. /*
  346. ** Check whether object 'o' should be kept in the 'grayagain' list for
  347. ** post-processing by 'correctgraylist'. (It could put all old objects
  348. ** in the list and leave all the work to 'correctgraylist', but it is
  349. ** more efficient to avoid adding elements that will be removed.) Only
  350. ** TOUCHED1 objects need to be in the list. TOUCHED2 doesn't need to go
  351. ** back to a gray list, but then it must become OLD. (That is what
  352. ** 'correctgraylist' does when it finds a TOUCHED2 object.)
  353. */
  354. static void genlink (global_State *g, GCObject *o) {
  355. lua_assert(isblack(o));
  356. if (getage(o) == G_TOUCHED1) { /* touched in this cycle? */
  357. linkobjgclist(o, g->grayagain); /* link it back in 'grayagain' */
  358. } /* everything else do not need to be linked back */
  359. else if (getage(o) == G_TOUCHED2)
  360. changeage(o, G_TOUCHED2, G_OLD); /* advance age */
  361. }
  362. /*
  363. ** Traverse a table with weak values and link it to proper list. During
  364. ** propagate phase, keep it in 'grayagain' list, to be revisited in the
  365. ** atomic phase. In the atomic phase, if table has any white value,
  366. ** put it in 'weak' list, to be cleared.
  367. */
  368. static void traverseweakvalue (global_State *g, Table *h) {
  369. Node *n, *limit = gnodelast(h);
  370. /* if there is array part, assume it may have white values (it is not
  371. worth traversing it now just to check) */
  372. int hasclears = (h->alimit > 0);
  373. for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */
  374. if (isempty(gval(n))) /* entry is empty? */
  375. clearkey(n); /* clear its key */
  376. else {
  377. lua_assert(!keyisnil(n));
  378. markkey(g, n);
  379. if (!hasclears && iscleared(g, gcvalueN(gval(n)))) /* a white value? */
  380. hasclears = 1; /* table will have to be cleared */
  381. }
  382. }
  383. if (g->gcstate == GCSatomic && hasclears)
  384. linkgclist(h, g->weak); /* has to be cleared later */
  385. else
  386. linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */
  387. }
  388. /*
  389. ** Traverse an ephemeron table and link it to proper list. Returns true
  390. ** iff any object was marked during this traversal (which implies that
  391. ** convergence has to continue). During propagation phase, keep table
  392. ** in 'grayagain' list, to be visited again in the atomic phase. In
  393. ** the atomic phase, if table has any white->white entry, it has to
  394. ** be revisited during ephemeron convergence (as that key may turn
  395. ** black). Otherwise, if it has any white key, table has to be cleared
  396. ** (in the atomic phase). In generational mode, some tables
  397. ** must be kept in some gray list for post-processing; this is done
  398. ** by 'genlink'.
  399. */
  400. static int traverseephemeron (global_State *g, Table *h, int inv) {
  401. int marked = 0; /* true if an object is marked in this traversal */
  402. int hasclears = 0; /* true if table has white keys */
  403. int hasww = 0; /* true if table has entry "white-key -> white-value" */
  404. unsigned int i;
  405. unsigned int asize = luaH_realasize(h);
  406. unsigned int nsize = sizenode(h);
  407. /* traverse array part */
  408. for (i = 0; i < asize; i++) {
  409. GCObject *o = gcvalarr(h, i + 1);
  410. if (o != NULL && iswhite(o)) {
  411. marked = 1;
  412. reallymarkobject(g, o);
  413. }
  414. }
  415. /* traverse hash part; if 'inv', traverse descending
  416. (see 'convergeephemerons') */
  417. for (i = 0; i < nsize; i++) {
  418. Node *n = inv ? gnode(h, nsize - 1 - i) : gnode(h, i);
  419. if (isempty(gval(n))) /* entry is empty? */
  420. clearkey(n); /* clear its key */
  421. else if (iscleared(g, gckeyN(n))) { /* key is not marked (yet)? */
  422. hasclears = 1; /* table must be cleared */
  423. if (valiswhite(gval(n))) /* value not marked yet? */
  424. hasww = 1; /* white-white entry */
  425. }
  426. else if (valiswhite(gval(n))) { /* value not marked yet? */
  427. marked = 1;
  428. reallymarkobject(g, gcvalue(gval(n))); /* mark it now */
  429. }
  430. }
  431. /* link table into proper list */
  432. if (g->gcstate == GCSpropagate)
  433. linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */
  434. else if (hasww) /* table has white->white entries? */
  435. linkgclist(h, g->ephemeron); /* have to propagate again */
  436. else if (hasclears) /* table has white keys? */
  437. linkgclist(h, g->allweak); /* may have to clean white keys */
  438. else
  439. genlink(g, obj2gco(h)); /* check whether collector still needs to see it */
  440. return marked;
  441. }
  442. static void traversestrongtable (global_State *g, Table *h) {
  443. Node *n, *limit = gnodelast(h);
  444. unsigned int i;
  445. unsigned int asize = luaH_realasize(h);
  446. for (i = 0; i < asize; i++) { /* traverse array part */
  447. GCObject *o = gcvalarr(h, i + 1);
  448. if (o != NULL && iswhite(o))
  449. reallymarkobject(g, o);
  450. }
  451. for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */
  452. if (isempty(gval(n))) /* entry is empty? */
  453. clearkey(n); /* clear its key */
  454. else {
  455. lua_assert(!keyisnil(n));
  456. markkey(g, n);
  457. markvalue(g, gval(n));
  458. }
  459. }
  460. genlink(g, obj2gco(h));
  461. }
  462. static lu_mem traversetable (global_State *g, Table *h) {
  463. const char *weakkey, *weakvalue;
  464. const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
  465. markobjectN(g, h->metatable);
  466. if (mode && ttisstring(mode) && /* is there a weak mode? */
  467. (cast_void(weakkey = strchr(svalue(mode), 'k')),
  468. cast_void(weakvalue = strchr(svalue(mode), 'v')),
  469. (weakkey || weakvalue))) { /* is really weak? */
  470. if (!weakkey) /* strong keys? */
  471. traverseweakvalue(g, h);
  472. else if (!weakvalue) /* strong values? */
  473. traverseephemeron(g, h, 0);
  474. else /* all weak */
  475. linkgclist(h, g->allweak); /* nothing to traverse now */
  476. }
  477. else /* not weak */
  478. traversestrongtable(g, h);
  479. return 1 + h->alimit + 2 * allocsizenode(h);
  480. }
  481. static int traverseudata (global_State *g, Udata *u) {
  482. int i;
  483. markobjectN(g, u->metatable); /* mark its metatable */
  484. for (i = 0; i < u->nuvalue; i++)
  485. markvalue(g, &u->uv[i].uv);
  486. genlink(g, obj2gco(u));
  487. return 1 + u->nuvalue;
  488. }
  489. /*
  490. ** Traverse a prototype. (While a prototype is being build, its
  491. ** arrays can be larger than needed; the extra slots are filled with
  492. ** NULL, so the use of 'markobjectN')
  493. */
  494. static int traverseproto (global_State *g, Proto *f) {
  495. int i;
  496. markobjectN(g, f->source);
  497. for (i = 0; i < f->sizek; i++) /* mark literals */
  498. markvalue(g, &f->k[i]);
  499. for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */
  500. markobjectN(g, f->upvalues[i].name);
  501. for (i = 0; i < f->sizep; i++) /* mark nested protos */
  502. markobjectN(g, f->p[i]);
  503. for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
  504. markobjectN(g, f->locvars[i].varname);
  505. return 1 + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars;
  506. }
  507. static int traverseCclosure (global_State *g, CClosure *cl) {
  508. int i;
  509. for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
  510. markvalue(g, &cl->upvalue[i]);
  511. return 1 + cl->nupvalues;
  512. }
  513. /*
  514. ** Traverse a Lua closure, marking its prototype and its upvalues.
  515. ** (Both can be NULL while closure is being created.)
  516. */
  517. static int traverseLclosure (global_State *g, LClosure *cl) {
  518. int i;
  519. markobjectN(g, cl->p); /* mark its prototype */
  520. for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */
  521. UpVal *uv = cl->upvals[i];
  522. markobjectN(g, uv); /* mark upvalue */
  523. }
  524. return 1 + cl->nupvalues;
  525. }
  526. /*
  527. ** Traverse a thread, marking the elements in the stack up to its top
  528. ** and cleaning the rest of the stack in the final traversal. That
  529. ** ensures that the entire stack have valid (non-dead) objects.
  530. ** Threads have no barriers. In gen. mode, old threads must be visited
  531. ** at every cycle, because they might point to young objects. In inc.
  532. ** mode, the thread can still be modified before the end of the cycle,
  533. ** and therefore it must be visited again in the atomic phase. To ensure
  534. ** these visits, threads must return to a gray list if they are not new
  535. ** (which can only happen in generational mode) or if the traverse is in
  536. ** the propagate phase (which can only happen in incremental mode).
  537. */
  538. static int traversethread (global_State *g, lua_State *th) {
  539. UpVal *uv;
  540. StkId o = th->stack.p;
  541. if (isold(th) || g->gcstate == GCSpropagate)
  542. linkgclist(th, g->grayagain); /* insert into 'grayagain' list */
  543. if (o == NULL)
  544. return 1; /* stack not completely built yet */
  545. lua_assert(g->gcstate == GCSatomic ||
  546. th->openupval == NULL || isintwups(th));
  547. for (; o < th->top.p; o++) /* mark live elements in the stack */
  548. markvalue(g, s2v(o));
  549. for (uv = th->openupval; uv != NULL; uv = uv->u.open.next)
  550. markobject(g, uv); /* open upvalues cannot be collected */
  551. if (g->gcstate == GCSatomic) { /* final traversal? */
  552. for (; o < th->stack_last.p + EXTRA_STACK; o++)
  553. setnilvalue(s2v(o)); /* clear dead stack slice */
  554. /* 'remarkupvals' may have removed thread from 'twups' list */
  555. if (!isintwups(th) && th->openupval != NULL) {
  556. th->twups = g->twups; /* link it back to the list */
  557. g->twups = th;
  558. }
  559. }
  560. else if (!g->gcemergency)
  561. luaD_shrinkstack(th); /* do not change stack in emergency cycle */
  562. return 1 + stacksize(th);
  563. }
  564. /*
  565. ** traverse one gray object, turning it to black.
  566. */
  567. static lu_mem propagatemark (global_State *g) {
  568. GCObject *o = g->gray;
  569. nw2black(o);
  570. g->gray = *getgclist(o); /* remove from 'gray' list */
  571. switch (o->tt) {
  572. case LUA_VTABLE: return traversetable(g, gco2t(o));
  573. case LUA_VUSERDATA: return traverseudata(g, gco2u(o));
  574. case LUA_VLCL: return traverseLclosure(g, gco2lcl(o));
  575. case LUA_VCCL: return traverseCclosure(g, gco2ccl(o));
  576. case LUA_VPROTO: return traverseproto(g, gco2p(o));
  577. case LUA_VTHREAD: return traversethread(g, gco2th(o));
  578. default: lua_assert(0); return 0;
  579. }
  580. }
  581. static lu_mem propagateall (global_State *g) {
  582. lu_mem tot = 0;
  583. while (g->gray)
  584. tot += propagatemark(g);
  585. return tot;
  586. }
  587. /*
  588. ** Traverse all ephemeron tables propagating marks from keys to values.
  589. ** Repeat until it converges, that is, nothing new is marked. 'dir'
  590. ** inverts the direction of the traversals, trying to speed up
  591. ** convergence on chains in the same table.
  592. **
  593. */
  594. static void convergeephemerons (global_State *g) {
  595. int changed;
  596. int dir = 0;
  597. do {
  598. GCObject *w;
  599. GCObject *next = g->ephemeron; /* get ephemeron list */
  600. g->ephemeron = NULL; /* tables may return to this list when traversed */
  601. changed = 0;
  602. while ((w = next) != NULL) { /* for each ephemeron table */
  603. Table *h = gco2t(w);
  604. next = h->gclist; /* list is rebuilt during loop */
  605. nw2black(h); /* out of the list (for now) */
  606. if (traverseephemeron(g, h, dir)) { /* marked some value? */
  607. propagateall(g); /* propagate changes */
  608. changed = 1; /* will have to revisit all ephemeron tables */
  609. }
  610. }
  611. dir = !dir; /* invert direction next time */
  612. } while (changed); /* repeat until no more changes */
  613. }
  614. /* }====================================================== */
  615. /*
  616. ** {======================================================
  617. ** Sweep Functions
  618. ** =======================================================
  619. */
  620. /*
  621. ** clear entries with unmarked keys from all weaktables in list 'l'
  622. */
  623. static void clearbykeys (global_State *g, GCObject *l) {
  624. for (; l; l = gco2t(l)->gclist) {
  625. Table *h = gco2t(l);
  626. Node *limit = gnodelast(h);
  627. Node *n;
  628. for (n = gnode(h, 0); n < limit; n++) {
  629. if (iscleared(g, gckeyN(n))) /* unmarked key? */
  630. setempty(gval(n)); /* remove entry */
  631. if (isempty(gval(n))) /* is entry empty? */
  632. clearkey(n); /* clear its key */
  633. }
  634. }
  635. }
  636. /*
  637. ** clear entries with unmarked values from all weaktables in list 'l' up
  638. ** to element 'f'
  639. */
  640. static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) {
  641. for (; l != f; l = gco2t(l)->gclist) {
  642. Table *h = gco2t(l);
  643. Node *n, *limit = gnodelast(h);
  644. unsigned int i;
  645. unsigned int asize = luaH_realasize(h);
  646. for (i = 0; i < asize; i++) {
  647. GCObject *o = gcvalarr(h, i + 1);
  648. if (iscleared(g, o)) /* value was collected? */
  649. *getArrTag(h, i + 1) = LUA_VEMPTY; /* remove entry */
  650. }
  651. for (n = gnode(h, 0); n < limit; n++) {
  652. if (iscleared(g, gcvalueN(gval(n)))) /* unmarked value? */
  653. setempty(gval(n)); /* remove entry */
  654. if (isempty(gval(n))) /* is entry empty? */
  655. clearkey(n); /* clear its key */
  656. }
  657. }
  658. }
  659. static void freeupval (lua_State *L, UpVal *uv) {
  660. if (upisopen(uv))
  661. luaF_unlinkupval(uv);
  662. luaM_free(L, uv);
  663. }
  664. static void freeobj (lua_State *L, GCObject *o) {
  665. switch (o->tt) {
  666. case LUA_VPROTO:
  667. luaF_freeproto(L, gco2p(o));
  668. break;
  669. case LUA_VUPVAL:
  670. freeupval(L, gco2upv(o));
  671. break;
  672. case LUA_VLCL: {
  673. LClosure *cl = gco2lcl(o);
  674. luaM_freemem(L, cl, sizeLclosure(cl->nupvalues));
  675. break;
  676. }
  677. case LUA_VCCL: {
  678. CClosure *cl = gco2ccl(o);
  679. luaM_freemem(L, cl, sizeCclosure(cl->nupvalues));
  680. break;
  681. }
  682. case LUA_VTABLE:
  683. luaH_free(L, gco2t(o));
  684. break;
  685. case LUA_VTHREAD:
  686. luaE_freethread(L, gco2th(o));
  687. break;
  688. case LUA_VUSERDATA: {
  689. Udata *u = gco2u(o);
  690. luaM_freemem(L, o, sizeudata(u->nuvalue, u->len));
  691. break;
  692. }
  693. case LUA_VSHRSTR: {
  694. TString *ts = gco2ts(o);
  695. luaS_remove(L, ts); /* remove it from hash table */
  696. luaM_freemem(L, ts, sizelstring(ts->shrlen));
  697. break;
  698. }
  699. case LUA_VLNGSTR: {
  700. TString *ts = gco2ts(o);
  701. luaM_freemem(L, ts, sizelstring(ts->u.lnglen));
  702. break;
  703. }
  704. default: lua_assert(0);
  705. }
  706. }
  707. /*
  708. ** sweep at most 'countin' elements from a list of GCObjects erasing dead
  709. ** objects, where a dead object is one marked with the old (non current)
  710. ** white; change all non-dead objects back to white, preparing for next
  711. ** collection cycle. Return where to continue the traversal or NULL if
  712. ** list is finished. ('*countout' gets the number of elements traversed.)
  713. */
  714. static GCObject **sweeplist (lua_State *L, GCObject **p, int countin,
  715. int *countout) {
  716. global_State *g = G(L);
  717. int ow = otherwhite(g);
  718. int i;
  719. int white = luaC_white(g); /* current white */
  720. for (i = 0; *p != NULL && i < countin; i++) {
  721. GCObject *curr = *p;
  722. int marked = curr->marked;
  723. if (isdeadm(ow, marked)) { /* is 'curr' dead? */
  724. *p = curr->next; /* remove 'curr' from list */
  725. freeobj(L, curr); /* erase 'curr' */
  726. }
  727. else { /* change mark to 'white' */
  728. curr->marked = cast_byte((marked & ~maskgcbits) | white);
  729. p = &curr->next; /* go to next element */
  730. }
  731. }
  732. if (countout)
  733. *countout = i; /* number of elements traversed */
  734. return (*p == NULL) ? NULL : p;
  735. }
  736. /*
  737. ** sweep a list until a live object (or end of list)
  738. */
  739. static GCObject **sweeptolive (lua_State *L, GCObject **p) {
  740. GCObject **old = p;
  741. do {
  742. p = sweeplist(L, p, 1, NULL);
  743. } while (p == old);
  744. return p;
  745. }
  746. /* }====================================================== */
  747. /*
  748. ** {======================================================
  749. ** Finalization
  750. ** =======================================================
  751. */
  752. /*
  753. ** If possible, shrink string table.
  754. */
  755. static void checkSizes (lua_State *L, global_State *g) {
  756. if (!g->gcemergency) {
  757. if (g->strt.nuse < g->strt.size / 4) { /* string table too big? */
  758. l_mem olddebt = g->GCdebt;
  759. luaS_resize(L, g->strt.size / 2);
  760. g->GCestimate += g->GCdebt - olddebt; /* correct estimate */
  761. }
  762. }
  763. }
  764. /*
  765. ** Get the next udata to be finalized from the 'tobefnz' list, and
  766. ** link it back into the 'allgc' list.
  767. */
  768. static GCObject *udata2finalize (global_State *g) {
  769. GCObject *o = g->tobefnz; /* get first element */
  770. lua_assert(tofinalize(o));
  771. g->tobefnz = o->next; /* remove it from 'tobefnz' list */
  772. o->next = g->allgc; /* return it to 'allgc' list */
  773. g->allgc = o;
  774. resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */
  775. if (issweepphase(g))
  776. makewhite(g, o); /* "sweep" object */
  777. else if (getage(o) == G_OLD1)
  778. g->firstold1 = o; /* it is the first OLD1 object in the list */
  779. return o;
  780. }
  781. static void dothecall (lua_State *L, void *ud) {
  782. UNUSED(ud);
  783. luaD_callnoyield(L, L->top.p - 2, 0);
  784. }
  785. static void GCTM (lua_State *L) {
  786. global_State *g = G(L);
  787. const TValue *tm;
  788. TValue v;
  789. lua_assert(!g->gcemergency);
  790. setgcovalue(L, &v, udata2finalize(g));
  791. tm = luaT_gettmbyobj(L, &v, TM_GC);
  792. if (!notm(tm)) { /* is there a finalizer? */
  793. int status;
  794. lu_byte oldah = L->allowhook;
  795. int oldgcstp = g->gcstp;
  796. g->gcstp |= GCSTPGC; /* avoid GC steps */
  797. L->allowhook = 0; /* stop debug hooks during GC metamethod */
  798. setobj2s(L, L->top.p++, tm); /* push finalizer... */
  799. setobj2s(L, L->top.p++, &v); /* ... and its argument */
  800. L->ci->callstatus |= CIST_FIN; /* will run a finalizer */
  801. status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top.p - 2), 0);
  802. L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */
  803. L->allowhook = oldah; /* restore hooks */
  804. g->gcstp = oldgcstp; /* restore state */
  805. if (l_unlikely(status != LUA_OK)) { /* error while running __gc? */
  806. luaE_warnerror(L, "__gc");
  807. L->top.p--; /* pops error object */
  808. }
  809. }
  810. }
  811. /*
  812. ** Call a few finalizers
  813. */
  814. static int runafewfinalizers (lua_State *L, int n) {
  815. global_State *g = G(L);
  816. int i;
  817. for (i = 0; i < n && g->tobefnz; i++)
  818. GCTM(L); /* call one finalizer */
  819. return i;
  820. }
  821. /*
  822. ** call all pending finalizers
  823. */
  824. static void callallpendingfinalizers (lua_State *L) {
  825. global_State *g = G(L);
  826. while (g->tobefnz)
  827. GCTM(L);
  828. }
  829. /*
  830. ** find last 'next' field in list 'p' list (to add elements in its end)
  831. */
  832. static GCObject **findlast (GCObject **p) {
  833. while (*p != NULL)
  834. p = &(*p)->next;
  835. return p;
  836. }
  837. /*
  838. ** Move all unreachable objects (or 'all' objects) that need
  839. ** finalization from list 'finobj' to list 'tobefnz' (to be finalized).
  840. ** (Note that objects after 'finobjold1' cannot be white, so they
  841. ** don't need to be traversed. In incremental mode, 'finobjold1' is NULL,
  842. ** so the whole list is traversed.)
  843. */
  844. static void separatetobefnz (global_State *g, int all) {
  845. GCObject *curr;
  846. GCObject **p = &g->finobj;
  847. GCObject **lastnext = findlast(&g->tobefnz);
  848. while ((curr = *p) != g->finobjold1) { /* traverse all finalizable objects */
  849. lua_assert(tofinalize(curr));
  850. if (!(iswhite(curr) || all)) /* not being collected? */
  851. p = &curr->next; /* don't bother with it */
  852. else {
  853. if (curr == g->finobjsur) /* removing 'finobjsur'? */
  854. g->finobjsur = curr->next; /* correct it */
  855. *p = curr->next; /* remove 'curr' from 'finobj' list */
  856. curr->next = *lastnext; /* link at the end of 'tobefnz' list */
  857. *lastnext = curr;
  858. lastnext = &curr->next;
  859. }
  860. }
  861. }
  862. /*
  863. ** If pointer 'p' points to 'o', move it to the next element.
  864. */
  865. static void checkpointer (GCObject **p, GCObject *o) {
  866. if (o == *p)
  867. *p = o->next;
  868. }
  869. /*
  870. ** Correct pointers to objects inside 'allgc' list when
  871. ** object 'o' is being removed from the list.
  872. */
  873. static void correctpointers (global_State *g, GCObject *o) {
  874. checkpointer(&g->survival, o);
  875. checkpointer(&g->old1, o);
  876. checkpointer(&g->reallyold, o);
  877. checkpointer(&g->firstold1, o);
  878. }
  879. /*
  880. ** if object 'o' has a finalizer, remove it from 'allgc' list (must
  881. ** search the list to find it) and link it in 'finobj' list.
  882. */
  883. void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
  884. global_State *g = G(L);
  885. if (tofinalize(o) || /* obj. is already marked... */
  886. gfasttm(g, mt, TM_GC) == NULL || /* or has no finalizer... */
  887. (g->gcstp & GCSTPCLS)) /* or closing state? */
  888. return; /* nothing to be done */
  889. else { /* move 'o' to 'finobj' list */
  890. GCObject **p;
  891. if (issweepphase(g)) {
  892. makewhite(g, o); /* "sweep" object 'o' */
  893. if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */
  894. g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */
  895. }
  896. else
  897. correctpointers(g, o);
  898. /* search for pointer pointing to 'o' */
  899. for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ }
  900. *p = o->next; /* remove 'o' from 'allgc' list */
  901. o->next = g->finobj; /* link it in 'finobj' list */
  902. g->finobj = o;
  903. l_setbit(o->marked, FINALIZEDBIT); /* mark it as such */
  904. }
  905. }
  906. /* }====================================================== */
  907. /*
  908. ** {======================================================
  909. ** Generational Collector
  910. ** =======================================================
  911. */
  912. /*
  913. ** Set the "time" to wait before starting a new GC cycle; cycle will
  914. ** start when memory use hits the threshold of ('estimate' * pause /
  915. ** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero,
  916. ** because Lua cannot even start with less than PAUSEADJ bytes).
  917. */
  918. static void setpause (global_State *g) {
  919. l_mem threshold, debt;
  920. int pause = getgcparam(g->gcpause);
  921. l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
  922. lua_assert(estimate > 0);
  923. threshold = (pause < MAX_LMEM / estimate) /* overflow? */
  924. ? estimate * pause /* no overflow */
  925. : MAX_LMEM; /* overflow; truncate to maximum */
  926. debt = gettotalbytes(g) - threshold;
  927. if (debt > 0) debt = 0;
  928. luaE_setdebt(g, debt);
  929. }
  930. /*
  931. ** Sweep a list of objects to enter generational mode. Deletes dead
  932. ** objects and turns the non dead to old. All non-dead threads---which
  933. ** are now old---must be in a gray list. Everything else is not in a
  934. ** gray list. Open upvalues are also kept gray.
  935. */
  936. static void sweep2old (lua_State *L, GCObject **p) {
  937. GCObject *curr;
  938. global_State *g = G(L);
  939. while ((curr = *p) != NULL) {
  940. if (iswhite(curr)) { /* is 'curr' dead? */
  941. lua_assert(isdead(g, curr));
  942. *p = curr->next; /* remove 'curr' from list */
  943. freeobj(L, curr); /* erase 'curr' */
  944. }
  945. else { /* all surviving objects become old */
  946. setage(curr, G_OLD);
  947. if (curr->tt == LUA_VTHREAD) { /* threads must be watched */
  948. lua_State *th = gco2th(curr);
  949. linkgclist(th, g->grayagain); /* insert into 'grayagain' list */
  950. }
  951. else if (curr->tt == LUA_VUPVAL && upisopen(gco2upv(curr)))
  952. set2gray(curr); /* open upvalues are always gray */
  953. else /* everything else is black */
  954. nw2black(curr);
  955. p = &curr->next; /* go to next element */
  956. }
  957. }
  958. }
  959. /*
  960. ** Sweep for generational mode. Delete dead objects. (Because the
  961. ** collection is not incremental, there are no "new white" objects
  962. ** during the sweep. So, any white object must be dead.) For
  963. ** non-dead objects, advance their ages and clear the color of
  964. ** new objects. (Old objects keep their colors.)
  965. ** The ages of G_TOUCHED1 and G_TOUCHED2 objects cannot be advanced
  966. ** here, because these old-generation objects are usually not swept
  967. ** here. They will all be advanced in 'correctgraylist'. That function
  968. ** will also remove objects turned white here from any gray list.
  969. */
  970. static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p,
  971. GCObject *limit, GCObject **pfirstold1) {
  972. static const lu_byte nextage[] = {
  973. G_SURVIVAL, /* from G_NEW */
  974. G_OLD1, /* from G_SURVIVAL */
  975. G_OLD1, /* from G_OLD0 */
  976. G_OLD, /* from G_OLD1 */
  977. G_OLD, /* from G_OLD (do not change) */
  978. G_TOUCHED1, /* from G_TOUCHED1 (do not change) */
  979. G_TOUCHED2 /* from G_TOUCHED2 (do not change) */
  980. };
  981. int white = luaC_white(g);
  982. GCObject *curr;
  983. while ((curr = *p) != limit) {
  984. if (iswhite(curr)) { /* is 'curr' dead? */
  985. lua_assert(!isold(curr) && isdead(g, curr));
  986. *p = curr->next; /* remove 'curr' from list */
  987. freeobj(L, curr); /* erase 'curr' */
  988. }
  989. else { /* correct mark and age */
  990. if (getage(curr) == G_NEW) { /* new objects go back to white */
  991. int marked = curr->marked & ~maskgcbits; /* erase GC bits */
  992. curr->marked = cast_byte(marked | G_SURVIVAL | white);
  993. }
  994. else { /* all other objects will be old, and so keep their color */
  995. setage(curr, nextage[getage(curr)]);
  996. if (getage(curr) == G_OLD1 && *pfirstold1 == NULL)
  997. *pfirstold1 = curr; /* first OLD1 object in the list */
  998. }
  999. p = &curr->next; /* go to next element */
  1000. }
  1001. }
  1002. return p;
  1003. }
  1004. /*
  1005. ** Traverse a list making all its elements white and clearing their
  1006. ** age. In incremental mode, all objects are 'new' all the time,
  1007. ** except for fixed strings (which are always old).
  1008. */
  1009. static void whitelist (global_State *g, GCObject *p) {
  1010. int white = luaC_white(g);
  1011. for (; p != NULL; p = p->next)
  1012. p->marked = cast_byte((p->marked & ~maskgcbits) | white);
  1013. }
  1014. /*
  1015. ** Correct a list of gray objects. Return pointer to where rest of the
  1016. ** list should be linked.
  1017. ** Because this correction is done after sweeping, young objects might
  1018. ** be turned white and still be in the list. They are only removed.
  1019. ** 'TOUCHED1' objects are advanced to 'TOUCHED2' and remain on the list;
  1020. ** Non-white threads also remain on the list; 'TOUCHED2' objects become
  1021. ** regular old; they and anything else are removed from the list.
  1022. */
  1023. static GCObject **correctgraylist (GCObject **p) {
  1024. GCObject *curr;
  1025. while ((curr = *p) != NULL) {
  1026. GCObject **next = getgclist(curr);
  1027. if (iswhite(curr))
  1028. goto remove; /* remove all white objects */
  1029. else if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */
  1030. lua_assert(isgray(curr));
  1031. nw2black(curr); /* make it black, for next barrier */
  1032. changeage(curr, G_TOUCHED1, G_TOUCHED2);
  1033. goto remain; /* keep it in the list and go to next element */
  1034. }
  1035. else if (curr->tt == LUA_VTHREAD) {
  1036. lua_assert(isgray(curr));
  1037. goto remain; /* keep non-white threads on the list */
  1038. }
  1039. else { /* everything else is removed */
  1040. lua_assert(isold(curr)); /* young objects should be white here */
  1041. if (getage(curr) == G_TOUCHED2) /* advance from TOUCHED2... */
  1042. changeage(curr, G_TOUCHED2, G_OLD); /* ... to OLD */
  1043. nw2black(curr); /* make object black (to be removed) */
  1044. goto remove;
  1045. }
  1046. remove: *p = *next; continue;
  1047. remain: p = next; continue;
  1048. }
  1049. return p;
  1050. }
  1051. /*
  1052. ** Correct all gray lists, coalescing them into 'grayagain'.
  1053. */
  1054. static void correctgraylists (global_State *g) {
  1055. GCObject **list = correctgraylist(&g->grayagain);
  1056. *list = g->weak; g->weak = NULL;
  1057. list = correctgraylist(list);
  1058. *list = g->allweak; g->allweak = NULL;
  1059. list = correctgraylist(list);
  1060. *list = g->ephemeron; g->ephemeron = NULL;
  1061. correctgraylist(list);
  1062. }
  1063. /*
  1064. ** Mark black 'OLD1' objects when starting a new young collection.
  1065. ** Gray objects are already in some gray list, and so will be visited
  1066. ** in the atomic step.
  1067. */
  1068. static void markold (global_State *g, GCObject *from, GCObject *to) {
  1069. GCObject *p;
  1070. for (p = from; p != to; p = p->next) {
  1071. if (getage(p) == G_OLD1) {
  1072. lua_assert(!iswhite(p));
  1073. changeage(p, G_OLD1, G_OLD); /* now they are old */
  1074. if (isblack(p))
  1075. reallymarkobject(g, p);
  1076. }
  1077. }
  1078. }
  1079. /*
  1080. ** Finish a young-generation collection.
  1081. */
  1082. static void finishgencycle (lua_State *L, global_State *g) {
  1083. correctgraylists(g);
  1084. checkSizes(L, g);
  1085. g->gcstate = GCSpropagate; /* skip restart */
  1086. if (!g->gcemergency)
  1087. callallpendingfinalizers(L);
  1088. }
  1089. /*
  1090. ** Does a young collection. First, mark 'OLD1' objects. Then does the
  1091. ** atomic step. Then, sweep all lists and advance pointers. Finally,
  1092. ** finish the collection.
  1093. */
  1094. static void youngcollection (lua_State *L, global_State *g) {
  1095. GCObject **psurvival; /* to point to first non-dead survival object */
  1096. GCObject *dummy; /* dummy out parameter to 'sweepgen' */
  1097. lua_assert(g->gcstate == GCSpropagate);
  1098. if (g->firstold1) { /* are there regular OLD1 objects? */
  1099. markold(g, g->firstold1, g->reallyold); /* mark them */
  1100. g->firstold1 = NULL; /* no more OLD1 objects (for now) */
  1101. }
  1102. markold(g, g->finobj, g->finobjrold);
  1103. markold(g, g->tobefnz, NULL);
  1104. atomic(L);
  1105. /* sweep nursery and get a pointer to its last live element */
  1106. g->gcstate = GCSswpallgc;
  1107. psurvival = sweepgen(L, g, &g->allgc, g->survival, &g->firstold1);
  1108. /* sweep 'survival' */
  1109. sweepgen(L, g, psurvival, g->old1, &g->firstold1);
  1110. g->reallyold = g->old1;
  1111. g->old1 = *psurvival; /* 'survival' survivals are old now */
  1112. g->survival = g->allgc; /* all news are survivals */
  1113. /* repeat for 'finobj' lists */
  1114. dummy = NULL; /* no 'firstold1' optimization for 'finobj' lists */
  1115. psurvival = sweepgen(L, g, &g->finobj, g->finobjsur, &dummy);
  1116. /* sweep 'survival' */
  1117. sweepgen(L, g, psurvival, g->finobjold1, &dummy);
  1118. g->finobjrold = g->finobjold1;
  1119. g->finobjold1 = *psurvival; /* 'survival' survivals are old now */
  1120. g->finobjsur = g->finobj; /* all news are survivals */
  1121. sweepgen(L, g, &g->tobefnz, NULL, &dummy);
  1122. finishgencycle(L, g);
  1123. }
  1124. /*
  1125. ** Clears all gray lists, sweeps objects, and prepare sublists to enter
  1126. ** generational mode. The sweeps remove dead objects and turn all
  1127. ** surviving objects to old. Threads go back to 'grayagain'; everything
  1128. ** else is turned black (not in any gray list).
  1129. */
  1130. static void atomic2gen (lua_State *L, global_State *g) {
  1131. cleargraylists(g);
  1132. /* sweep all elements making them old */
  1133. g->gcstate = GCSswpallgc;
  1134. sweep2old(L, &g->allgc);
  1135. /* everything alive now is old */
  1136. g->reallyold = g->old1 = g->survival = g->allgc;
  1137. g->firstold1 = NULL; /* there are no OLD1 objects anywhere */
  1138. /* repeat for 'finobj' lists */
  1139. sweep2old(L, &g->finobj);
  1140. g->finobjrold = g->finobjold1 = g->finobjsur = g->finobj;
  1141. sweep2old(L, &g->tobefnz);
  1142. g->gckind = KGC_GEN;
  1143. g->lastatomic = 0;
  1144. g->GCestimate = gettotalbytes(g); /* base for memory control */
  1145. finishgencycle(L, g);
  1146. }
  1147. /*
  1148. ** Set debt for the next minor collection, which will happen when
  1149. ** memory grows 'genminormul'%.
  1150. */
  1151. static void setminordebt (global_State *g) {
  1152. luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul));
  1153. }
  1154. /*
  1155. ** Enter generational mode. Must go until the end of an atomic cycle
  1156. ** to ensure that all objects are correctly marked and weak tables
  1157. ** are cleared. Then, turn all objects into old and finishes the
  1158. ** collection.
  1159. */
  1160. static lu_mem entergen (lua_State *L, global_State *g) {
  1161. lu_mem numobjs;
  1162. luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */
  1163. luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */
  1164. numobjs = atomic(L); /* propagates all and then do the atomic stuff */
  1165. atomic2gen(L, g);
  1166. setminordebt(g); /* set debt assuming next cycle will be minor */
  1167. return numobjs;
  1168. }
  1169. /*
  1170. ** Enter incremental mode. Turn all objects white, make all
  1171. ** intermediate lists point to NULL (to avoid invalid pointers),
  1172. ** and go to the pause state.
  1173. */
  1174. static void enterinc (global_State *g) {
  1175. whitelist(g, g->allgc);
  1176. g->reallyold = g->old1 = g->survival = NULL;
  1177. whitelist(g, g->finobj);
  1178. whitelist(g, g->tobefnz);
  1179. g->finobjrold = g->finobjold1 = g->finobjsur = NULL;
  1180. g->gcstate = GCSpause;
  1181. g->gckind = KGC_INC;
  1182. g->lastatomic = 0;
  1183. }
  1184. /*
  1185. ** Change collector mode to 'newmode'.
  1186. */
  1187. void luaC_changemode (lua_State *L, int newmode) {
  1188. global_State *g = G(L);
  1189. if (newmode != g->gckind) {
  1190. if (newmode == KGC_GEN) /* entering generational mode? */
  1191. entergen(L, g);
  1192. else
  1193. enterinc(g); /* entering incremental mode */
  1194. }
  1195. g->lastatomic = 0;
  1196. }
  1197. /*
  1198. ** Does a full collection in generational mode.
  1199. */
  1200. static lu_mem fullgen (lua_State *L, global_State *g) {
  1201. enterinc(g);
  1202. return entergen(L, g);
  1203. }
  1204. /*
  1205. ** Does a major collection after last collection was a "bad collection".
  1206. **
  1207. ** When the program is building a big structure, it allocates lots of
  1208. ** memory but generates very little garbage. In those scenarios,
  1209. ** the generational mode just wastes time doing small collections, and
  1210. ** major collections are frequently what we call a "bad collection", a
  1211. ** collection that frees too few objects. To avoid the cost of switching
  1212. ** between generational mode and the incremental mode needed for full
  1213. ** (major) collections, the collector tries to stay in incremental mode
  1214. ** after a bad collection, and to switch back to generational mode only
  1215. ** after a "good" collection (one that traverses less than 9/8 objects
  1216. ** of the previous one).
  1217. ** The collector must choose whether to stay in incremental mode or to
  1218. ** switch back to generational mode before sweeping. At this point, it
  1219. ** does not know the real memory in use, so it cannot use memory to
  1220. ** decide whether to return to generational mode. Instead, it uses the
  1221. ** number of objects traversed (returned by 'atomic') as a proxy. The
  1222. ** field 'g->lastatomic' keeps this count from the last collection.
  1223. ** ('g->lastatomic != 0' also means that the last collection was bad.)
  1224. */
  1225. static void stepgenfull (lua_State *L, global_State *g) {
  1226. lu_mem newatomic; /* count of traversed objects */
  1227. lu_mem lastatomic = g->lastatomic; /* count from last collection */
  1228. if (g->gckind == KGC_GEN) /* still in generational mode? */
  1229. enterinc(g); /* enter incremental mode */
  1230. luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */
  1231. newatomic = atomic(L); /* mark everybody */
  1232. if (newatomic < lastatomic + (lastatomic >> 3)) { /* good collection? */
  1233. atomic2gen(L, g); /* return to generational mode */
  1234. setminordebt(g);
  1235. }
  1236. else { /* another bad collection; stay in incremental mode */
  1237. g->GCestimate = gettotalbytes(g); /* first estimate */;
  1238. entersweep(L);
  1239. luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */
  1240. setpause(g);
  1241. g->lastatomic = newatomic;
  1242. }
  1243. }
  1244. /*
  1245. ** Does a generational "step".
  1246. ** Usually, this means doing a minor collection and setting the debt to
  1247. ** make another collection when memory grows 'genminormul'% larger.
  1248. **
  1249. ** However, there are exceptions. If memory grows 'genmajormul'%
  1250. ** larger than it was at the end of the last major collection (kept
  1251. ** in 'g->GCestimate'), the function does a major collection. At the
  1252. ** end, it checks whether the major collection was able to free a
  1253. ** decent amount of memory (at least half the growth in memory since
  1254. ** previous major collection). If so, the collector keeps its state,
  1255. ** and the next collection will probably be minor again. Otherwise,
  1256. ** we have what we call a "bad collection". In that case, set the field
  1257. ** 'g->lastatomic' to signal that fact, so that the next collection will
  1258. ** go to 'stepgenfull'.
  1259. **
  1260. ** 'GCdebt <= 0' means an explicit call to GC step with "size" zero;
  1261. ** in that case, do a minor collection.
  1262. */
  1263. static void genstep (lua_State *L, global_State *g) {
  1264. if (g->lastatomic != 0) /* last collection was a bad one? */
  1265. stepgenfull(L, g); /* do a full step */
  1266. else {
  1267. lu_mem majorbase = g->GCestimate; /* memory after last major collection */
  1268. lu_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul);
  1269. if (g->GCdebt > 0 && gettotalbytes(g) > majorbase + majorinc) {
  1270. lu_mem numobjs = fullgen(L, g); /* do a major collection */
  1271. if (gettotalbytes(g) < majorbase + (majorinc / 2)) {
  1272. /* collected at least half of memory growth since last major
  1273. collection; keep doing minor collections. */
  1274. lua_assert(g->lastatomic == 0);
  1275. }
  1276. else { /* bad collection */
  1277. g->lastatomic = numobjs; /* signal that last collection was bad */
  1278. setpause(g); /* do a long wait for next (major) collection */
  1279. }
  1280. }
  1281. else { /* regular case; do a minor collection */
  1282. youngcollection(L, g);
  1283. setminordebt(g);
  1284. g->GCestimate = majorbase; /* preserve base value */
  1285. }
  1286. }
  1287. lua_assert(isdecGCmodegen(g));
  1288. }
  1289. /* }====================================================== */
  1290. /*
  1291. ** {======================================================
  1292. ** GC control
  1293. ** =======================================================
  1294. */
  1295. /*
  1296. ** Enter first sweep phase.
  1297. ** The call to 'sweeptolive' makes the pointer point to an object
  1298. ** inside the list (instead of to the header), so that the real sweep do
  1299. ** not need to skip objects created between "now" and the start of the
  1300. ** real sweep.
  1301. */
  1302. static void entersweep (lua_State *L) {
  1303. global_State *g = G(L);
  1304. g->gcstate = GCSswpallgc;
  1305. lua_assert(g->sweepgc == NULL);
  1306. g->sweepgc = sweeptolive(L, &g->allgc);
  1307. }
  1308. /*
  1309. ** Delete all objects in list 'p' until (but not including) object
  1310. ** 'limit'.
  1311. */
  1312. static void deletelist (lua_State *L, GCObject *p, GCObject *limit) {
  1313. while (p != limit) {
  1314. GCObject *next = p->next;
  1315. freeobj(L, p);
  1316. p = next;
  1317. }
  1318. }
  1319. /*
  1320. ** Call all finalizers of the objects in the given Lua state, and
  1321. ** then free all objects, except for the main thread.
  1322. */
  1323. void luaC_freeallobjects (lua_State *L) {
  1324. global_State *g = G(L);
  1325. g->gcstp = GCSTPCLS; /* no extra finalizers after here */
  1326. luaC_changemode(L, KGC_INC);
  1327. separatetobefnz(g, 1); /* separate all objects with finalizers */
  1328. lua_assert(g->finobj == NULL);
  1329. callallpendingfinalizers(L);
  1330. deletelist(L, g->allgc, obj2gco(g->mainthread));
  1331. lua_assert(g->finobj == NULL); /* no new finalizers */
  1332. deletelist(L, g->fixedgc, NULL); /* collect fixed objects */
  1333. lua_assert(g->strt.nuse == 0);
  1334. }
  1335. static lu_mem atomic (lua_State *L) {
  1336. global_State *g = G(L);
  1337. lu_mem work = 0;
  1338. GCObject *origweak, *origall;
  1339. GCObject *grayagain = g->grayagain; /* save original list */
  1340. g->grayagain = NULL;
  1341. lua_assert(g->ephemeron == NULL && g->weak == NULL);
  1342. lua_assert(!iswhite(g->mainthread));
  1343. g->gcstate = GCSatomic;
  1344. markobject(g, L); /* mark running thread */
  1345. /* registry and global metatables may be changed by API */
  1346. markvalue(g, &g->l_registry);
  1347. markmt(g); /* mark global metatables */
  1348. work += propagateall(g); /* empties 'gray' list */
  1349. /* remark occasional upvalues of (maybe) dead threads */
  1350. work += remarkupvals(g);
  1351. work += propagateall(g); /* propagate changes */
  1352. g->gray = grayagain;
  1353. work += propagateall(g); /* traverse 'grayagain' list */
  1354. convergeephemerons(g);
  1355. /* at this point, all strongly accessible objects are marked. */
  1356. /* Clear values from weak tables, before checking finalizers */
  1357. clearbyvalues(g, g->weak, NULL);
  1358. clearbyvalues(g, g->allweak, NULL);
  1359. origweak = g->weak; origall = g->allweak;
  1360. separatetobefnz(g, 0); /* separate objects to be finalized */
  1361. work += markbeingfnz(g); /* mark objects that will be finalized */
  1362. work += propagateall(g); /* remark, to propagate 'resurrection' */
  1363. convergeephemerons(g);
  1364. /* at this point, all resurrected objects are marked. */
  1365. /* remove dead objects from weak tables */
  1366. clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron tables */
  1367. clearbykeys(g, g->allweak); /* clear keys from all 'allweak' tables */
  1368. /* clear values from resurrected weak tables */
  1369. clearbyvalues(g, g->weak, origweak);
  1370. clearbyvalues(g, g->allweak, origall);
  1371. luaS_clearcache(g);
  1372. g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
  1373. lua_assert(g->gray == NULL);
  1374. return work; /* estimate of slots marked by 'atomic' */
  1375. }
  1376. static int sweepstep (lua_State *L, global_State *g,
  1377. int nextstate, GCObject **nextlist) {
  1378. if (g->sweepgc) {
  1379. l_mem olddebt = g->GCdebt;
  1380. int count;
  1381. g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX, &count);
  1382. g->GCestimate += g->GCdebt - olddebt; /* update estimate */
  1383. return count;
  1384. }
  1385. else { /* enter next state */
  1386. g->gcstate = nextstate;
  1387. g->sweepgc = nextlist;
  1388. return 0; /* no work done */
  1389. }
  1390. }
  1391. static lu_mem singlestep (lua_State *L) {
  1392. global_State *g = G(L);
  1393. lu_mem work;
  1394. lua_assert(!g->gcstopem); /* collector is not reentrant */
  1395. g->gcstopem = 1; /* no emergency collections while collecting */
  1396. switch (g->gcstate) {
  1397. case GCSpause: {
  1398. restartcollection(g);
  1399. g->gcstate = GCSpropagate;
  1400. work = 1;
  1401. break;
  1402. }
  1403. case GCSpropagate: {
  1404. if (g->gray == NULL) { /* no more gray objects? */
  1405. g->gcstate = GCSenteratomic; /* finish propagate phase */
  1406. work = 0;
  1407. }
  1408. else
  1409. work = propagatemark(g); /* traverse one gray object */
  1410. break;
  1411. }
  1412. case GCSenteratomic: {
  1413. work = atomic(L); /* work is what was traversed by 'atomic' */
  1414. entersweep(L);
  1415. g->GCestimate = gettotalbytes(g); /* first estimate */;
  1416. break;
  1417. }
  1418. case GCSswpallgc: { /* sweep "regular" objects */
  1419. work = sweepstep(L, g, GCSswpfinobj, &g->finobj);
  1420. break;
  1421. }
  1422. case GCSswpfinobj: { /* sweep objects with finalizers */
  1423. work = sweepstep(L, g, GCSswptobefnz, &g->tobefnz);
  1424. break;
  1425. }
  1426. case GCSswptobefnz: { /* sweep objects to be finalized */
  1427. work = sweepstep(L, g, GCSswpend, NULL);
  1428. break;
  1429. }
  1430. case GCSswpend: { /* finish sweeps */
  1431. checkSizes(L, g);
  1432. g->gcstate = GCScallfin;
  1433. work = 0;
  1434. break;
  1435. }
  1436. case GCScallfin: { /* call remaining finalizers */
  1437. if (g->tobefnz && !g->gcemergency) {
  1438. g->gcstopem = 0; /* ok collections during finalizers */
  1439. work = runafewfinalizers(L, GCFINMAX) * GCFINALIZECOST;
  1440. }
  1441. else { /* emergency mode or no more finalizers */
  1442. g->gcstate = GCSpause; /* finish collection */
  1443. work = 0;
  1444. }
  1445. break;
  1446. }
  1447. default: lua_assert(0); return 0;
  1448. }
  1449. g->gcstopem = 0;
  1450. return work;
  1451. }
  1452. /*
  1453. ** advances the garbage collector until it reaches a state allowed
  1454. ** by 'statemask'
  1455. */
  1456. void luaC_runtilstate (lua_State *L, int statesmask) {
  1457. global_State *g = G(L);
  1458. while (!testbit(statesmask, g->gcstate))
  1459. singlestep(L);
  1460. }
  1461. /*
  1462. ** Performs a basic incremental step. The debt and step size are
  1463. ** converted from bytes to "units of work"; then the function loops
  1464. ** running single steps until adding that many units of work or
  1465. ** finishing a cycle (pause state). Finally, it sets the debt that
  1466. ** controls when next step will be performed.
  1467. */
  1468. static void incstep (lua_State *L, global_State *g) {
  1469. int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */
  1470. l_mem debt = (g->GCdebt / WORK2MEM) * stepmul;
  1471. l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem))
  1472. ? ((cast(l_mem, 1) << g->gcstepsize) / WORK2MEM) * stepmul
  1473. : MAX_LMEM; /* overflow; keep maximum value */
  1474. do { /* repeat until pause or enough "credit" (negative debt) */
  1475. lu_mem work = singlestep(L); /* perform one single step */
  1476. debt -= work;
  1477. } while (debt > -stepsize && g->gcstate != GCSpause);
  1478. if (g->gcstate == GCSpause)
  1479. setpause(g); /* pause until next cycle */
  1480. else {
  1481. debt = (debt / stepmul) * WORK2MEM; /* convert 'work units' to bytes */
  1482. luaE_setdebt(g, debt);
  1483. }
  1484. }
  1485. /*
  1486. ** Performs a basic GC step if collector is running. (If collector is
  1487. ** not running, set a reasonable debt to avoid it being called at
  1488. ** every single check.)
  1489. */
  1490. void luaC_step (lua_State *L) {
  1491. global_State *g = G(L);
  1492. if (!gcrunning(g)) /* not running? */
  1493. luaE_setdebt(g, -2000);
  1494. else {
  1495. if(isdecGCmodegen(g))
  1496. genstep(L, g);
  1497. else
  1498. incstep(L, g);
  1499. }
  1500. }
  1501. /*
  1502. ** Perform a full collection in incremental mode.
  1503. ** Before running the collection, check 'keepinvariant'; if it is true,
  1504. ** there may be some objects marked as black, so the collector has
  1505. ** to sweep all objects to turn them back to white (as white has not
  1506. ** changed, nothing will be collected).
  1507. */
  1508. static void fullinc (lua_State *L, global_State *g) {
  1509. if (keepinvariant(g)) /* black objects? */
  1510. entersweep(L); /* sweep everything to turn them back to white */
  1511. /* finish any pending sweep phase to start a new cycle */
  1512. luaC_runtilstate(L, bitmask(GCSpause));
  1513. luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */
  1514. /* estimate must be correct after a full GC cycle */
  1515. lua_assert(g->GCestimate == gettotalbytes(g));
  1516. luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */
  1517. setpause(g);
  1518. }
  1519. /*
  1520. ** Performs a full GC cycle; if 'isemergency', set a flag to avoid
  1521. ** some operations which could change the interpreter state in some
  1522. ** unexpected ways (running finalizers and shrinking some structures).
  1523. */
  1524. void luaC_fullgc (lua_State *L, int isemergency) {
  1525. global_State *g = G(L);
  1526. lua_assert(!g->gcemergency);
  1527. g->gcemergency = isemergency; /* set flag */
  1528. if (g->gckind == KGC_INC)
  1529. fullinc(L, g);
  1530. else
  1531. fullgen(L, g);
  1532. g->gcemergency = 0;
  1533. }
  1534. /* }====================================================== */