lgc.c 56 KB

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