lgc.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  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);
  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);
  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. TString *smode;
  466. markobjectN(g, h->metatable);
  467. if (mode && ttisshrstring(mode) && /* is there a weak mode? */
  468. (cast_void(smode = tsvalue(mode)),
  469. cast_void(weakkey = strchr(getshrstr(smode), 'k')),
  470. cast_void(weakvalue = strchr(getshrstr(smode), 'v')),
  471. (weakkey || weakvalue))) { /* is really weak? */
  472. if (!weakkey) /* strong keys? */
  473. traverseweakvalue(g, h);
  474. else if (!weakvalue) /* strong values? */
  475. traverseephemeron(g, h, 0);
  476. else /* all weak */
  477. linkgclist(h, g->allweak); /* nothing to traverse now */
  478. }
  479. else /* not weak */
  480. traversestrongtable(g, h);
  481. return 1 + h->alimit + 2 * allocsizenode(h);
  482. }
  483. static int traverseudata (global_State *g, Udata *u) {
  484. int i;
  485. markobjectN(g, u->metatable); /* mark its metatable */
  486. for (i = 0; i < u->nuvalue; i++)
  487. markvalue(g, &u->uv[i].uv);
  488. genlink(g, obj2gco(u));
  489. return 1 + u->nuvalue;
  490. }
  491. /*
  492. ** Traverse a prototype. (While a prototype is being build, its
  493. ** arrays can be larger than needed; the extra slots are filled with
  494. ** NULL, so the use of 'markobjectN')
  495. */
  496. static int traverseproto (global_State *g, Proto *f) {
  497. int i;
  498. markobjectN(g, f->source);
  499. for (i = 0; i < f->sizek; i++) /* mark literals */
  500. markvalue(g, &f->k[i]);
  501. for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */
  502. markobjectN(g, f->upvalues[i].name);
  503. for (i = 0; i < f->sizep; i++) /* mark nested protos */
  504. markobjectN(g, f->p[i]);
  505. for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
  506. markobjectN(g, f->locvars[i].varname);
  507. return 1 + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars;
  508. }
  509. static int traverseCclosure (global_State *g, CClosure *cl) {
  510. int i;
  511. for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
  512. markvalue(g, &cl->upvalue[i]);
  513. return 1 + cl->nupvalues;
  514. }
  515. /*
  516. ** Traverse a Lua closure, marking its prototype and its upvalues.
  517. ** (Both can be NULL while closure is being created.)
  518. */
  519. static int traverseLclosure (global_State *g, LClosure *cl) {
  520. int i;
  521. markobjectN(g, cl->p); /* mark its prototype */
  522. for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */
  523. UpVal *uv = cl->upvals[i];
  524. markobjectN(g, uv); /* mark upvalue */
  525. }
  526. return 1 + cl->nupvalues;
  527. }
  528. /*
  529. ** Traverse a thread, marking the elements in the stack up to its top
  530. ** and cleaning the rest of the stack in the final traversal. That
  531. ** ensures that the entire stack have valid (non-dead) objects.
  532. ** Threads have no barriers. In gen. mode, old threads must be visited
  533. ** at every cycle, because they might point to young objects. In inc.
  534. ** mode, the thread can still be modified before the end of the cycle,
  535. ** and therefore it must be visited again in the atomic phase. To ensure
  536. ** these visits, threads must return to a gray list if they are not new
  537. ** (which can only happen in generational mode) or if the traverse is in
  538. ** the propagate phase (which can only happen in incremental mode).
  539. */
  540. static int traversethread (global_State *g, lua_State *th) {
  541. UpVal *uv;
  542. StkId o = th->stack.p;
  543. if (isold(th) || g->gcstate == GCSpropagate)
  544. linkgclist(th, g->grayagain); /* insert into 'grayagain' list */
  545. if (o == NULL)
  546. return 1; /* stack not completely built yet */
  547. lua_assert(g->gcstate == GCSatomic ||
  548. th->openupval == NULL || isintwups(th));
  549. for (; o < th->top.p; o++) /* mark live elements in the stack */
  550. markvalue(g, s2v(o));
  551. for (uv = th->openupval; uv != NULL; uv = uv->u.open.next)
  552. markobject(g, uv); /* open upvalues cannot be collected */
  553. if (g->gcstate == GCSatomic) { /* final traversal? */
  554. if (!g->gcemergency)
  555. luaD_shrinkstack(th); /* do not change stack in emergency cycle */
  556. for (o = th->top.p; o < th->stack_last.p + EXTRA_STACK; o++)
  557. setnilvalue(s2v(o)); /* clear dead stack slice */
  558. /* 'remarkupvals' may have removed thread from 'twups' list */
  559. if (!isintwups(th) && th->openupval != NULL) {
  560. th->twups = g->twups; /* link it back to the list */
  561. g->twups = th;
  562. }
  563. }
  564. return 1 + stacksize(th);
  565. }
  566. /*
  567. ** traverse one gray object, turning it to black.
  568. */
  569. static lu_mem propagatemark (global_State *g) {
  570. GCObject *o = g->gray;
  571. nw2black(o);
  572. g->gray = *getgclist(o); /* remove from 'gray' list */
  573. switch (o->tt) {
  574. case LUA_VTABLE: return traversetable(g, gco2t(o));
  575. case LUA_VUSERDATA: return traverseudata(g, gco2u(o));
  576. case LUA_VLCL: return traverseLclosure(g, gco2lcl(o));
  577. case LUA_VCCL: return traverseCclosure(g, gco2ccl(o));
  578. case LUA_VPROTO: return traverseproto(g, gco2p(o));
  579. case LUA_VTHREAD: return traversethread(g, gco2th(o));
  580. default: lua_assert(0); return 0;
  581. }
  582. }
  583. static lu_mem propagateall (global_State *g) {
  584. lu_mem tot = 0;
  585. while (g->gray)
  586. tot += propagatemark(g);
  587. return tot;
  588. }
  589. /*
  590. ** Traverse all ephemeron tables propagating marks from keys to values.
  591. ** Repeat until it converges, that is, nothing new is marked. 'dir'
  592. ** inverts the direction of the traversals, trying to speed up
  593. ** convergence on chains in the same table.
  594. **
  595. */
  596. static void convergeephemerons (global_State *g) {
  597. int changed;
  598. int dir = 0;
  599. do {
  600. GCObject *w;
  601. GCObject *next = g->ephemeron; /* get ephemeron list */
  602. g->ephemeron = NULL; /* tables may return to this list when traversed */
  603. changed = 0;
  604. while ((w = next) != NULL) { /* for each ephemeron table */
  605. Table *h = gco2t(w);
  606. next = h->gclist; /* list is rebuilt during loop */
  607. nw2black(h); /* out of the list (for now) */
  608. if (traverseephemeron(g, h, dir)) { /* marked some value? */
  609. propagateall(g); /* propagate changes */
  610. changed = 1; /* will have to revisit all ephemeron tables */
  611. }
  612. }
  613. dir = !dir; /* invert direction next time */
  614. } while (changed); /* repeat until no more changes */
  615. }
  616. /* }====================================================== */
  617. /*
  618. ** {======================================================
  619. ** Sweep Functions
  620. ** =======================================================
  621. */
  622. /*
  623. ** clear entries with unmarked keys from all weaktables in list 'l'
  624. */
  625. static void clearbykeys (global_State *g, GCObject *l) {
  626. for (; l; l = gco2t(l)->gclist) {
  627. Table *h = gco2t(l);
  628. Node *limit = gnodelast(h);
  629. Node *n;
  630. for (n = gnode(h, 0); n < limit; n++) {
  631. if (iscleared(g, gckeyN(n))) /* unmarked key? */
  632. setempty(gval(n)); /* remove entry */
  633. if (isempty(gval(n))) /* is entry empty? */
  634. clearkey(n); /* clear its key */
  635. }
  636. }
  637. }
  638. /*
  639. ** clear entries with unmarked values from all weaktables in list 'l' up
  640. ** to element 'f'
  641. */
  642. static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) {
  643. for (; l != f; l = gco2t(l)->gclist) {
  644. Table *h = gco2t(l);
  645. Node *n, *limit = gnodelast(h);
  646. unsigned int i;
  647. unsigned int asize = luaH_realasize(h);
  648. for (i = 0; i < asize; i++) {
  649. GCObject *o = gcvalarr(h, i);
  650. if (iscleared(g, o)) /* value was collected? */
  651. *getArrTag(h, i) = LUA_VEMPTY; /* remove entry */
  652. }
  653. for (n = gnode(h, 0); n < limit; n++) {
  654. if (iscleared(g, gcvalueN(gval(n)))) /* unmarked value? */
  655. setempty(gval(n)); /* remove entry */
  656. if (isempty(gval(n))) /* is entry empty? */
  657. clearkey(n); /* clear its key */
  658. }
  659. }
  660. }
  661. static void freeupval (lua_State *L, UpVal *uv) {
  662. if (upisopen(uv))
  663. luaF_unlinkupval(uv);
  664. luaM_free(L, uv);
  665. }
  666. static void freeobj (lua_State *L, GCObject *o) {
  667. switch (o->tt) {
  668. case LUA_VPROTO:
  669. luaF_freeproto(L, gco2p(o));
  670. break;
  671. case LUA_VUPVAL:
  672. freeupval(L, gco2upv(o));
  673. break;
  674. case LUA_VLCL: {
  675. LClosure *cl = gco2lcl(o);
  676. luaM_freemem(L, cl, sizeLclosure(cl->nupvalues));
  677. break;
  678. }
  679. case LUA_VCCL: {
  680. CClosure *cl = gco2ccl(o);
  681. luaM_freemem(L, cl, sizeCclosure(cl->nupvalues));
  682. break;
  683. }
  684. case LUA_VTABLE:
  685. luaH_free(L, gco2t(o));
  686. break;
  687. case LUA_VTHREAD:
  688. luaE_freethread(L, gco2th(o));
  689. break;
  690. case LUA_VUSERDATA: {
  691. Udata *u = gco2u(o);
  692. luaM_freemem(L, o, sizeudata(u->nuvalue, u->len));
  693. break;
  694. }
  695. case LUA_VSHRSTR: {
  696. TString *ts = gco2ts(o);
  697. luaS_remove(L, ts); /* remove it from hash table */
  698. luaM_freemem(L, ts, sizelstring(ts->shrlen));
  699. break;
  700. }
  701. case LUA_VLNGSTR: {
  702. TString *ts = gco2ts(o);
  703. luaM_freemem(L, ts, sizelstring(ts->u.lnglen));
  704. break;
  705. }
  706. default: lua_assert(0);
  707. }
  708. }
  709. /*
  710. ** sweep at most 'countin' elements from a list of GCObjects erasing dead
  711. ** objects, where a dead object is one marked with the old (non current)
  712. ** white; change all non-dead objects back to white, preparing for next
  713. ** collection cycle. Return where to continue the traversal or NULL if
  714. ** list is finished. ('*countout' gets the number of elements traversed.)
  715. */
  716. static GCObject **sweeplist (lua_State *L, GCObject **p, int countin,
  717. int *countout) {
  718. global_State *g = G(L);
  719. int ow = otherwhite(g);
  720. int i;
  721. int white = luaC_white(g); /* current white */
  722. for (i = 0; *p != NULL && i < countin; i++) {
  723. GCObject *curr = *p;
  724. int marked = curr->marked;
  725. if (isdeadm(ow, marked)) { /* is 'curr' dead? */
  726. *p = curr->next; /* remove 'curr' from list */
  727. freeobj(L, curr); /* erase 'curr' */
  728. }
  729. else { /* change mark to 'white' */
  730. curr->marked = cast_byte((marked & ~maskgcbits) | white);
  731. p = &curr->next; /* go to next element */
  732. }
  733. }
  734. if (countout)
  735. *countout = i; /* number of elements traversed */
  736. return (*p == NULL) ? NULL : p;
  737. }
  738. /*
  739. ** sweep a list until a live object (or end of list)
  740. */
  741. static GCObject **sweeptolive (lua_State *L, GCObject **p) {
  742. GCObject **old = p;
  743. do {
  744. p = sweeplist(L, p, 1, NULL);
  745. } while (p == old);
  746. return p;
  747. }
  748. /* }====================================================== */
  749. /*
  750. ** {======================================================
  751. ** Finalization
  752. ** =======================================================
  753. */
  754. /*
  755. ** If possible, shrink string table.
  756. */
  757. static void checkSizes (lua_State *L, global_State *g) {
  758. if (!g->gcemergency) {
  759. if (g->strt.nuse < g->strt.size / 4) { /* string table too big? */
  760. l_mem olddebt = g->GCdebt;
  761. luaS_resize(L, g->strt.size / 2);
  762. g->GCestimate += g->GCdebt - olddebt; /* correct estimate */
  763. }
  764. }
  765. }
  766. /*
  767. ** Get the next udata to be finalized from the 'tobefnz' list, and
  768. ** link it back into the 'allgc' list.
  769. */
  770. static GCObject *udata2finalize (global_State *g) {
  771. GCObject *o = g->tobefnz; /* get first element */
  772. lua_assert(tofinalize(o));
  773. g->tobefnz = o->next; /* remove it from 'tobefnz' list */
  774. o->next = g->allgc; /* return it to 'allgc' list */
  775. g->allgc = o;
  776. resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */
  777. if (issweepphase(g))
  778. makewhite(g, o); /* "sweep" object */
  779. else if (getage(o) == G_OLD1)
  780. g->firstold1 = o; /* it is the first OLD1 object in the list */
  781. return o;
  782. }
  783. static void dothecall (lua_State *L, void *ud) {
  784. UNUSED(ud);
  785. luaD_callnoyield(L, L->top.p - 2, 0);
  786. }
  787. static void GCTM (lua_State *L) {
  788. global_State *g = G(L);
  789. const TValue *tm;
  790. TValue v;
  791. lua_assert(!g->gcemergency);
  792. setgcovalue(L, &v, udata2finalize(g));
  793. tm = luaT_gettmbyobj(L, &v, TM_GC);
  794. if (!notm(tm)) { /* is there a finalizer? */
  795. int status;
  796. lu_byte oldah = L->allowhook;
  797. int oldgcstp = g->gcstp;
  798. g->gcstp |= GCSTPGC; /* avoid GC steps */
  799. L->allowhook = 0; /* stop debug hooks during GC metamethod */
  800. setobj2s(L, L->top.p++, tm); /* push finalizer... */
  801. setobj2s(L, L->top.p++, &v); /* ... and its argument */
  802. L->ci->callstatus |= CIST_FIN; /* will run a finalizer */
  803. status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top.p - 2), 0);
  804. L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */
  805. L->allowhook = oldah; /* restore hooks */
  806. g->gcstp = oldgcstp; /* restore state */
  807. if (l_unlikely(status != LUA_OK)) { /* error while running __gc? */
  808. luaE_warnerror(L, "__gc");
  809. L->top.p--; /* pops error object */
  810. }
  811. }
  812. }
  813. /*
  814. ** Call a few finalizers
  815. */
  816. static int runafewfinalizers (lua_State *L, int n) {
  817. global_State *g = G(L);
  818. int i;
  819. for (i = 0; i < n && g->tobefnz; i++)
  820. GCTM(L); /* call one finalizer */
  821. return i;
  822. }
  823. /*
  824. ** call all pending finalizers
  825. */
  826. static void callallpendingfinalizers (lua_State *L) {
  827. global_State *g = G(L);
  828. while (g->tobefnz)
  829. GCTM(L);
  830. }
  831. /*
  832. ** find last 'next' field in list 'p' list (to add elements in its end)
  833. */
  834. static GCObject **findlast (GCObject **p) {
  835. while (*p != NULL)
  836. p = &(*p)->next;
  837. return p;
  838. }
  839. /*
  840. ** Move all unreachable objects (or 'all' objects) that need
  841. ** finalization from list 'finobj' to list 'tobefnz' (to be finalized).
  842. ** (Note that objects after 'finobjold1' cannot be white, so they
  843. ** don't need to be traversed. In incremental mode, 'finobjold1' is NULL,
  844. ** so the whole list is traversed.)
  845. */
  846. static void separatetobefnz (global_State *g, int all) {
  847. GCObject *curr;
  848. GCObject **p = &g->finobj;
  849. GCObject **lastnext = findlast(&g->tobefnz);
  850. while ((curr = *p) != g->finobjold1) { /* traverse all finalizable objects */
  851. lua_assert(tofinalize(curr));
  852. if (!(iswhite(curr) || all)) /* not being collected? */
  853. p = &curr->next; /* don't bother with it */
  854. else {
  855. if (curr == g->finobjsur) /* removing 'finobjsur'? */
  856. g->finobjsur = curr->next; /* correct it */
  857. *p = curr->next; /* remove 'curr' from 'finobj' list */
  858. curr->next = *lastnext; /* link at the end of 'tobefnz' list */
  859. *lastnext = curr;
  860. lastnext = &curr->next;
  861. }
  862. }
  863. }
  864. /*
  865. ** If pointer 'p' points to 'o', move it to the next element.
  866. */
  867. static void checkpointer (GCObject **p, GCObject *o) {
  868. if (o == *p)
  869. *p = o->next;
  870. }
  871. /*
  872. ** Correct pointers to objects inside 'allgc' list when
  873. ** object 'o' is being removed from the list.
  874. */
  875. static void correctpointers (global_State *g, GCObject *o) {
  876. checkpointer(&g->survival, o);
  877. checkpointer(&g->old1, o);
  878. checkpointer(&g->reallyold, o);
  879. checkpointer(&g->firstold1, o);
  880. }
  881. /*
  882. ** if object 'o' has a finalizer, remove it from 'allgc' list (must
  883. ** search the list to find it) and link it in 'finobj' list.
  884. */
  885. void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
  886. global_State *g = G(L);
  887. if (tofinalize(o) || /* obj. is already marked... */
  888. gfasttm(g, mt, TM_GC) == NULL || /* or has no finalizer... */
  889. (g->gcstp & GCSTPCLS)) /* or closing state? */
  890. return; /* nothing to be done */
  891. else { /* move 'o' to 'finobj' list */
  892. GCObject **p;
  893. if (issweepphase(g)) {
  894. makewhite(g, o); /* "sweep" object 'o' */
  895. if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */
  896. g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */
  897. }
  898. else
  899. correctpointers(g, o);
  900. /* search for pointer pointing to 'o' */
  901. for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ }
  902. *p = o->next; /* remove 'o' from 'allgc' list */
  903. o->next = g->finobj; /* link it in 'finobj' list */
  904. g->finobj = o;
  905. l_setbit(o->marked, FINALIZEDBIT); /* mark it as such */
  906. }
  907. }
  908. /* }====================================================== */
  909. /*
  910. ** {======================================================
  911. ** Generational Collector
  912. ** =======================================================
  913. */
  914. /*
  915. ** Set the "time" to wait before starting a new GC cycle; cycle will
  916. ** start when memory use hits the threshold of ('estimate' * pause /
  917. ** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero,
  918. ** because Lua cannot even start with less than PAUSEADJ bytes).
  919. */
  920. static void setpause (global_State *g) {
  921. l_mem threshold, debt;
  922. int pause = getgcparam(g->gcpause);
  923. l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
  924. lua_assert(estimate > 0);
  925. threshold = (pause < MAX_LMEM / estimate) /* overflow? */
  926. ? estimate * pause /* no overflow */
  927. : MAX_LMEM; /* overflow; truncate to maximum */
  928. debt = gettotalbytes(g) - threshold;
  929. if (debt > 0) debt = 0;
  930. luaE_setdebt(g, debt);
  931. }
  932. /*
  933. ** Sweep a list of objects to enter generational mode. Deletes dead
  934. ** objects and turns the non dead to old. All non-dead threads---which
  935. ** are now old---must be in a gray list. Everything else is not in a
  936. ** gray list. Open upvalues are also kept gray.
  937. */
  938. static void sweep2old (lua_State *L, GCObject **p) {
  939. GCObject *curr;
  940. global_State *g = G(L);
  941. while ((curr = *p) != NULL) {
  942. if (iswhite(curr)) { /* is 'curr' dead? */
  943. lua_assert(isdead(g, curr));
  944. *p = curr->next; /* remove 'curr' from list */
  945. freeobj(L, curr); /* erase 'curr' */
  946. }
  947. else { /* all surviving objects become old */
  948. setage(curr, G_OLD);
  949. if (curr->tt == LUA_VTHREAD) { /* threads must be watched */
  950. lua_State *th = gco2th(curr);
  951. linkgclist(th, g->grayagain); /* insert into 'grayagain' list */
  952. }
  953. else if (curr->tt == LUA_VUPVAL && upisopen(gco2upv(curr)))
  954. set2gray(curr); /* open upvalues are always gray */
  955. else /* everything else is black */
  956. nw2black(curr);
  957. p = &curr->next; /* go to next element */
  958. }
  959. }
  960. }
  961. /*
  962. ** Sweep for generational mode. Delete dead objects. (Because the
  963. ** collection is not incremental, there are no "new white" objects
  964. ** during the sweep. So, any white object must be dead.) For
  965. ** non-dead objects, advance their ages and clear the color of
  966. ** new objects. (Old objects keep their colors.)
  967. ** The ages of G_TOUCHED1 and G_TOUCHED2 objects cannot be advanced
  968. ** here, because these old-generation objects are usually not swept
  969. ** here. They will all be advanced in 'correctgraylist'. That function
  970. ** will also remove objects turned white here from any gray list.
  971. */
  972. static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p,
  973. GCObject *limit, GCObject **pfirstold1) {
  974. static const lu_byte nextage[] = {
  975. G_SURVIVAL, /* from G_NEW */
  976. G_OLD1, /* from G_SURVIVAL */
  977. G_OLD1, /* from G_OLD0 */
  978. G_OLD, /* from G_OLD1 */
  979. G_OLD, /* from G_OLD (do not change) */
  980. G_TOUCHED1, /* from G_TOUCHED1 (do not change) */
  981. G_TOUCHED2 /* from G_TOUCHED2 (do not change) */
  982. };
  983. int white = luaC_white(g);
  984. GCObject *curr;
  985. while ((curr = *p) != limit) {
  986. if (iswhite(curr)) { /* is 'curr' dead? */
  987. lua_assert(!isold(curr) && isdead(g, curr));
  988. *p = curr->next; /* remove 'curr' from list */
  989. freeobj(L, curr); /* erase 'curr' */
  990. }
  991. else { /* correct mark and age */
  992. if (getage(curr) == G_NEW) { /* new objects go back to white */
  993. int marked = curr->marked & ~maskgcbits; /* erase GC bits */
  994. curr->marked = cast_byte(marked | G_SURVIVAL | white);
  995. }
  996. else { /* all other objects will be old, and so keep their color */
  997. setage(curr, nextage[getage(curr)]);
  998. if (getage(curr) == G_OLD1 && *pfirstold1 == NULL)
  999. *pfirstold1 = curr; /* first OLD1 object in the list */
  1000. }
  1001. p = &curr->next; /* go to next element */
  1002. }
  1003. }
  1004. return p;
  1005. }
  1006. /*
  1007. ** Traverse a list making all its elements white and clearing their
  1008. ** age. In incremental mode, all objects are 'new' all the time,
  1009. ** except for fixed strings (which are always old).
  1010. */
  1011. static void whitelist (global_State *g, GCObject *p) {
  1012. int white = luaC_white(g);
  1013. for (; p != NULL; p = p->next)
  1014. p->marked = cast_byte((p->marked & ~maskgcbits) | white);
  1015. }
  1016. /*
  1017. ** Correct a list of gray objects. Return pointer to where rest of the
  1018. ** list should be linked.
  1019. ** Because this correction is done after sweeping, young objects might
  1020. ** be turned white and still be in the list. They are only removed.
  1021. ** 'TOUCHED1' objects are advanced to 'TOUCHED2' and remain on the list;
  1022. ** Non-white threads also remain on the list; 'TOUCHED2' objects become
  1023. ** regular old; they and anything else are removed from the list.
  1024. */
  1025. static GCObject **correctgraylist (GCObject **p) {
  1026. GCObject *curr;
  1027. while ((curr = *p) != NULL) {
  1028. GCObject **next = getgclist(curr);
  1029. if (iswhite(curr))
  1030. goto remove; /* remove all white objects */
  1031. else if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */
  1032. lua_assert(isgray(curr));
  1033. nw2black(curr); /* make it black, for next barrier */
  1034. changeage(curr, G_TOUCHED1, G_TOUCHED2);
  1035. goto remain; /* keep it in the list and go to next element */
  1036. }
  1037. else if (curr->tt == LUA_VTHREAD) {
  1038. lua_assert(isgray(curr));
  1039. goto remain; /* keep non-white threads on the list */
  1040. }
  1041. else { /* everything else is removed */
  1042. lua_assert(isold(curr)); /* young objects should be white here */
  1043. if (getage(curr) == G_TOUCHED2) /* advance from TOUCHED2... */
  1044. changeage(curr, G_TOUCHED2, G_OLD); /* ... to OLD */
  1045. nw2black(curr); /* make object black (to be removed) */
  1046. goto remove;
  1047. }
  1048. remove: *p = *next; continue;
  1049. remain: p = next; continue;
  1050. }
  1051. return p;
  1052. }
  1053. /*
  1054. ** Correct all gray lists, coalescing them into 'grayagain'.
  1055. */
  1056. static void correctgraylists (global_State *g) {
  1057. GCObject **list = correctgraylist(&g->grayagain);
  1058. *list = g->weak; g->weak = NULL;
  1059. list = correctgraylist(list);
  1060. *list = g->allweak; g->allweak = NULL;
  1061. list = correctgraylist(list);
  1062. *list = g->ephemeron; g->ephemeron = NULL;
  1063. correctgraylist(list);
  1064. }
  1065. /*
  1066. ** Mark black 'OLD1' objects when starting a new young collection.
  1067. ** Gray objects are already in some gray list, and so will be visited
  1068. ** in the atomic step.
  1069. */
  1070. static void markold (global_State *g, GCObject *from, GCObject *to) {
  1071. GCObject *p;
  1072. for (p = from; p != to; p = p->next) {
  1073. if (getage(p) == G_OLD1) {
  1074. lua_assert(!iswhite(p));
  1075. changeage(p, G_OLD1, G_OLD); /* now they are old */
  1076. if (isblack(p))
  1077. reallymarkobject(g, p);
  1078. }
  1079. }
  1080. }
  1081. /*
  1082. ** Finish a young-generation collection.
  1083. */
  1084. static void finishgencycle (lua_State *L, global_State *g) {
  1085. correctgraylists(g);
  1086. checkSizes(L, g);
  1087. g->gcstate = GCSpropagate; /* skip restart */
  1088. if (!g->gcemergency)
  1089. callallpendingfinalizers(L);
  1090. }
  1091. /*
  1092. ** Does a young collection. First, mark 'OLD1' objects. Then does the
  1093. ** atomic step. Then, sweep all lists and advance pointers. Finally,
  1094. ** finish the collection.
  1095. */
  1096. static void youngcollection (lua_State *L, global_State *g) {
  1097. GCObject **psurvival; /* to point to first non-dead survival object */
  1098. GCObject *dummy; /* dummy out parameter to 'sweepgen' */
  1099. lua_assert(g->gcstate == GCSpropagate);
  1100. if (g->firstold1) { /* are there regular OLD1 objects? */
  1101. markold(g, g->firstold1, g->reallyold); /* mark them */
  1102. g->firstold1 = NULL; /* no more OLD1 objects (for now) */
  1103. }
  1104. markold(g, g->finobj, g->finobjrold);
  1105. markold(g, g->tobefnz, NULL);
  1106. atomic(L);
  1107. /* sweep nursery and get a pointer to its last live element */
  1108. g->gcstate = GCSswpallgc;
  1109. psurvival = sweepgen(L, g, &g->allgc, g->survival, &g->firstold1);
  1110. /* sweep 'survival' */
  1111. sweepgen(L, g, psurvival, g->old1, &g->firstold1);
  1112. g->reallyold = g->old1;
  1113. g->old1 = *psurvival; /* 'survival' survivals are old now */
  1114. g->survival = g->allgc; /* all news are survivals */
  1115. /* repeat for 'finobj' lists */
  1116. dummy = NULL; /* no 'firstold1' optimization for 'finobj' lists */
  1117. psurvival = sweepgen(L, g, &g->finobj, g->finobjsur, &dummy);
  1118. /* sweep 'survival' */
  1119. sweepgen(L, g, psurvival, g->finobjold1, &dummy);
  1120. g->finobjrold = g->finobjold1;
  1121. g->finobjold1 = *psurvival; /* 'survival' survivals are old now */
  1122. g->finobjsur = g->finobj; /* all news are survivals */
  1123. sweepgen(L, g, &g->tobefnz, NULL, &dummy);
  1124. finishgencycle(L, g);
  1125. }
  1126. /*
  1127. ** Clears all gray lists, sweeps objects, and prepare sublists to enter
  1128. ** generational mode. The sweeps remove dead objects and turn all
  1129. ** surviving objects to old. Threads go back to 'grayagain'; everything
  1130. ** else is turned black (not in any gray list).
  1131. */
  1132. static void atomic2gen (lua_State *L, global_State *g) {
  1133. cleargraylists(g);
  1134. /* sweep all elements making them old */
  1135. g->gcstate = GCSswpallgc;
  1136. sweep2old(L, &g->allgc);
  1137. /* everything alive now is old */
  1138. g->reallyold = g->old1 = g->survival = g->allgc;
  1139. g->firstold1 = NULL; /* there are no OLD1 objects anywhere */
  1140. /* repeat for 'finobj' lists */
  1141. sweep2old(L, &g->finobj);
  1142. g->finobjrold = g->finobjold1 = g->finobjsur = g->finobj;
  1143. sweep2old(L, &g->tobefnz);
  1144. g->gckind = KGC_GEN;
  1145. g->lastatomic = 0;
  1146. g->GCestimate = gettotalbytes(g); /* base for memory control */
  1147. finishgencycle(L, g);
  1148. }
  1149. /*
  1150. ** Set debt for the next minor collection, which will happen when
  1151. ** memory grows 'genminormul'%.
  1152. */
  1153. static void setminordebt (global_State *g) {
  1154. luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul));
  1155. }
  1156. /*
  1157. ** Enter generational mode. Must go until the end of an atomic cycle
  1158. ** to ensure that all objects are correctly marked and weak tables
  1159. ** are cleared. Then, turn all objects into old and finishes the
  1160. ** collection.
  1161. */
  1162. static lu_mem entergen (lua_State *L, global_State *g) {
  1163. lu_mem numobjs;
  1164. luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */
  1165. luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */
  1166. numobjs = atomic(L); /* propagates all and then do the atomic stuff */
  1167. atomic2gen(L, g);
  1168. setminordebt(g); /* set debt assuming next cycle will be minor */
  1169. return numobjs;
  1170. }
  1171. /*
  1172. ** Enter incremental mode. Turn all objects white, make all
  1173. ** intermediate lists point to NULL (to avoid invalid pointers),
  1174. ** and go to the pause state.
  1175. */
  1176. static void enterinc (global_State *g) {
  1177. whitelist(g, g->allgc);
  1178. g->reallyold = g->old1 = g->survival = NULL;
  1179. whitelist(g, g->finobj);
  1180. whitelist(g, g->tobefnz);
  1181. g->finobjrold = g->finobjold1 = g->finobjsur = NULL;
  1182. g->gcstate = GCSpause;
  1183. g->gckind = KGC_INC;
  1184. g->lastatomic = 0;
  1185. }
  1186. /*
  1187. ** Change collector mode to 'newmode'.
  1188. */
  1189. void luaC_changemode (lua_State *L, int newmode) {
  1190. global_State *g = G(L);
  1191. if (newmode != g->gckind) {
  1192. if (newmode == KGC_GEN) /* entering generational mode? */
  1193. entergen(L, g);
  1194. else
  1195. enterinc(g); /* entering incremental mode */
  1196. }
  1197. g->lastatomic = 0;
  1198. }
  1199. /*
  1200. ** Does a full collection in generational mode.
  1201. */
  1202. static lu_mem fullgen (lua_State *L, global_State *g) {
  1203. enterinc(g);
  1204. return entergen(L, g);
  1205. }
  1206. /*
  1207. ** Does a major collection after last collection was a "bad collection".
  1208. **
  1209. ** When the program is building a big structure, it allocates lots of
  1210. ** memory but generates very little garbage. In those scenarios,
  1211. ** the generational mode just wastes time doing small collections, and
  1212. ** major collections are frequently what we call a "bad collection", a
  1213. ** collection that frees too few objects. To avoid the cost of switching
  1214. ** between generational mode and the incremental mode needed for full
  1215. ** (major) collections, the collector tries to stay in incremental mode
  1216. ** after a bad collection, and to switch back to generational mode only
  1217. ** after a "good" collection (one that traverses less than 9/8 objects
  1218. ** of the previous one).
  1219. ** The collector must choose whether to stay in incremental mode or to
  1220. ** switch back to generational mode before sweeping. At this point, it
  1221. ** does not know the real memory in use, so it cannot use memory to
  1222. ** decide whether to return to generational mode. Instead, it uses the
  1223. ** number of objects traversed (returned by 'atomic') as a proxy. The
  1224. ** field 'g->lastatomic' keeps this count from the last collection.
  1225. ** ('g->lastatomic != 0' also means that the last collection was bad.)
  1226. */
  1227. static void stepgenfull (lua_State *L, global_State *g) {
  1228. lu_mem newatomic; /* count of traversed objects */
  1229. lu_mem lastatomic = g->lastatomic; /* count from last collection */
  1230. if (g->gckind == KGC_GEN) /* still in generational mode? */
  1231. enterinc(g); /* enter incremental mode */
  1232. luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */
  1233. newatomic = atomic(L); /* mark everybody */
  1234. if (newatomic < lastatomic + (lastatomic >> 3)) { /* good collection? */
  1235. atomic2gen(L, g); /* return to generational mode */
  1236. setminordebt(g);
  1237. }
  1238. else { /* another bad collection; stay in incremental mode */
  1239. g->GCestimate = gettotalbytes(g); /* first estimate */
  1240. entersweep(L);
  1241. luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */
  1242. setpause(g);
  1243. g->lastatomic = newatomic;
  1244. }
  1245. }
  1246. /*
  1247. ** Does a generational "step".
  1248. ** Usually, this means doing a minor collection and setting the debt to
  1249. ** make another collection when memory grows 'genminormul'% larger.
  1250. **
  1251. ** However, there are exceptions. If memory grows 'genmajormul'%
  1252. ** larger than it was at the end of the last major collection (kept
  1253. ** in 'g->GCestimate'), the function does a major collection. At the
  1254. ** end, it checks whether the major collection was able to free a
  1255. ** decent amount of memory (at least half the growth in memory since
  1256. ** previous major collection). If so, the collector keeps its state,
  1257. ** and the next collection will probably be minor again. Otherwise,
  1258. ** we have what we call a "bad collection". In that case, set the field
  1259. ** 'g->lastatomic' to signal that fact, so that the next collection will
  1260. ** go to 'stepgenfull'.
  1261. **
  1262. ** 'GCdebt <= 0' means an explicit call to GC step with "size" zero;
  1263. ** in that case, do a minor collection.
  1264. */
  1265. static void genstep (lua_State *L, global_State *g) {
  1266. if (g->lastatomic != 0) /* last collection was a bad one? */
  1267. stepgenfull(L, g); /* do a full step */
  1268. else {
  1269. lu_mem majorbase = g->GCestimate; /* memory after last major collection */
  1270. lu_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul);
  1271. if (g->GCdebt > 0 && gettotalbytes(g) > majorbase + majorinc) {
  1272. lu_mem numobjs = fullgen(L, g); /* do a major collection */
  1273. if (gettotalbytes(g) < majorbase + (majorinc / 2)) {
  1274. /* collected at least half of memory growth since last major
  1275. collection; keep doing minor collections. */
  1276. lua_assert(g->lastatomic == 0);
  1277. }
  1278. else { /* bad collection */
  1279. g->lastatomic = numobjs; /* signal that last collection was bad */
  1280. setpause(g); /* do a long wait for next (major) collection */
  1281. }
  1282. }
  1283. else { /* regular case; do a minor collection */
  1284. youngcollection(L, g);
  1285. setminordebt(g);
  1286. g->GCestimate = majorbase; /* preserve base value */
  1287. }
  1288. }
  1289. lua_assert(isdecGCmodegen(g));
  1290. }
  1291. /* }====================================================== */
  1292. /*
  1293. ** {======================================================
  1294. ** GC control
  1295. ** =======================================================
  1296. */
  1297. /*
  1298. ** Enter first sweep phase.
  1299. ** The call to 'sweeptolive' makes the pointer point to an object
  1300. ** inside the list (instead of to the header), so that the real sweep do
  1301. ** not need to skip objects created between "now" and the start of the
  1302. ** real sweep.
  1303. */
  1304. static void entersweep (lua_State *L) {
  1305. global_State *g = G(L);
  1306. g->gcstate = GCSswpallgc;
  1307. lua_assert(g->sweepgc == NULL);
  1308. g->sweepgc = sweeptolive(L, &g->allgc);
  1309. }
  1310. /*
  1311. ** Delete all objects in list 'p' until (but not including) object
  1312. ** 'limit'.
  1313. */
  1314. static void deletelist (lua_State *L, GCObject *p, GCObject *limit) {
  1315. while (p != limit) {
  1316. GCObject *next = p->next;
  1317. freeobj(L, p);
  1318. p = next;
  1319. }
  1320. }
  1321. /*
  1322. ** Call all finalizers of the objects in the given Lua state, and
  1323. ** then free all objects, except for the main thread.
  1324. */
  1325. void luaC_freeallobjects (lua_State *L) {
  1326. global_State *g = G(L);
  1327. g->gcstp = GCSTPCLS; /* no extra finalizers after here */
  1328. luaC_changemode(L, KGC_INC);
  1329. separatetobefnz(g, 1); /* separate all objects with finalizers */
  1330. lua_assert(g->finobj == NULL);
  1331. callallpendingfinalizers(L);
  1332. deletelist(L, g->allgc, obj2gco(g->mainthread));
  1333. lua_assert(g->finobj == NULL); /* no new finalizers */
  1334. deletelist(L, g->fixedgc, NULL); /* collect fixed objects */
  1335. lua_assert(g->strt.nuse == 0);
  1336. }
  1337. static lu_mem atomic (lua_State *L) {
  1338. global_State *g = G(L);
  1339. lu_mem work = 0;
  1340. GCObject *origweak, *origall;
  1341. GCObject *grayagain = g->grayagain; /* save original list */
  1342. g->grayagain = NULL;
  1343. lua_assert(g->ephemeron == NULL && g->weak == NULL);
  1344. lua_assert(!iswhite(g->mainthread));
  1345. g->gcstate = GCSatomic;
  1346. markobject(g, L); /* mark running thread */
  1347. /* registry and global metatables may be changed by API */
  1348. markvalue(g, &g->l_registry);
  1349. markmt(g); /* mark global metatables */
  1350. work += propagateall(g); /* empties 'gray' list */
  1351. /* remark occasional upvalues of (maybe) dead threads */
  1352. work += remarkupvals(g);
  1353. work += propagateall(g); /* propagate changes */
  1354. g->gray = grayagain;
  1355. work += propagateall(g); /* traverse 'grayagain' list */
  1356. convergeephemerons(g);
  1357. /* at this point, all strongly accessible objects are marked. */
  1358. /* Clear values from weak tables, before checking finalizers */
  1359. clearbyvalues(g, g->weak, NULL);
  1360. clearbyvalues(g, g->allweak, NULL);
  1361. origweak = g->weak; origall = g->allweak;
  1362. separatetobefnz(g, 0); /* separate objects to be finalized */
  1363. work += markbeingfnz(g); /* mark objects that will be finalized */
  1364. work += propagateall(g); /* remark, to propagate 'resurrection' */
  1365. convergeephemerons(g);
  1366. /* at this point, all resurrected objects are marked. */
  1367. /* remove dead objects from weak tables */
  1368. clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron tables */
  1369. clearbykeys(g, g->allweak); /* clear keys from all 'allweak' tables */
  1370. /* clear values from resurrected weak tables */
  1371. clearbyvalues(g, g->weak, origweak);
  1372. clearbyvalues(g, g->allweak, origall);
  1373. luaS_clearcache(g);
  1374. g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
  1375. lua_assert(g->gray == NULL);
  1376. return work; /* estimate of slots marked by 'atomic' */
  1377. }
  1378. static int sweepstep (lua_State *L, global_State *g,
  1379. int nextstate, GCObject **nextlist) {
  1380. if (g->sweepgc) {
  1381. l_mem olddebt = g->GCdebt;
  1382. int count;
  1383. g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX, &count);
  1384. g->GCestimate += g->GCdebt - olddebt; /* update estimate */
  1385. return count;
  1386. }
  1387. else { /* enter next state */
  1388. g->gcstate = nextstate;
  1389. g->sweepgc = nextlist;
  1390. return 0; /* no work done */
  1391. }
  1392. }
  1393. static lu_mem singlestep (lua_State *L) {
  1394. global_State *g = G(L);
  1395. lu_mem work;
  1396. lua_assert(!g->gcstopem); /* collector is not reentrant */
  1397. g->gcstopem = 1; /* no emergency collections while collecting */
  1398. switch (g->gcstate) {
  1399. case GCSpause: {
  1400. restartcollection(g);
  1401. g->gcstate = GCSpropagate;
  1402. work = 1;
  1403. break;
  1404. }
  1405. case GCSpropagate: {
  1406. if (g->gray == NULL) { /* no more gray objects? */
  1407. g->gcstate = GCSenteratomic; /* finish propagate phase */
  1408. work = 0;
  1409. }
  1410. else
  1411. work = propagatemark(g); /* traverse one gray object */
  1412. break;
  1413. }
  1414. case GCSenteratomic: {
  1415. work = atomic(L); /* work is what was traversed by 'atomic' */
  1416. entersweep(L);
  1417. g->GCestimate = gettotalbytes(g); /* first estimate */
  1418. break;
  1419. }
  1420. case GCSswpallgc: { /* sweep "regular" objects */
  1421. work = sweepstep(L, g, GCSswpfinobj, &g->finobj);
  1422. break;
  1423. }
  1424. case GCSswpfinobj: { /* sweep objects with finalizers */
  1425. work = sweepstep(L, g, GCSswptobefnz, &g->tobefnz);
  1426. break;
  1427. }
  1428. case GCSswptobefnz: { /* sweep objects to be finalized */
  1429. work = sweepstep(L, g, GCSswpend, NULL);
  1430. break;
  1431. }
  1432. case GCSswpend: { /* finish sweeps */
  1433. checkSizes(L, g);
  1434. g->gcstate = GCScallfin;
  1435. work = 0;
  1436. break;
  1437. }
  1438. case GCScallfin: { /* call remaining finalizers */
  1439. if (g->tobefnz && !g->gcemergency) {
  1440. g->gcstopem = 0; /* ok collections during finalizers */
  1441. work = runafewfinalizers(L, GCFINMAX) * GCFINALIZECOST;
  1442. }
  1443. else { /* emergency mode or no more finalizers */
  1444. g->gcstate = GCSpause; /* finish collection */
  1445. work = 0;
  1446. }
  1447. break;
  1448. }
  1449. default: lua_assert(0); return 0;
  1450. }
  1451. g->gcstopem = 0;
  1452. return work;
  1453. }
  1454. /*
  1455. ** advances the garbage collector until it reaches a state allowed
  1456. ** by 'statemask'
  1457. */
  1458. void luaC_runtilstate (lua_State *L, int statesmask) {
  1459. global_State *g = G(L);
  1460. while (!testbit(statesmask, g->gcstate))
  1461. singlestep(L);
  1462. }
  1463. /*
  1464. ** Performs a basic incremental step. The debt and step size are
  1465. ** converted from bytes to "units of work"; then the function loops
  1466. ** running single steps until adding that many units of work or
  1467. ** finishing a cycle (pause state). Finally, it sets the debt that
  1468. ** controls when next step will be performed.
  1469. */
  1470. static void incstep (lua_State *L, global_State *g) {
  1471. int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */
  1472. l_mem debt = (g->GCdebt / WORK2MEM) * stepmul;
  1473. l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem))
  1474. ? ((cast(l_mem, 1) << g->gcstepsize) / WORK2MEM) * stepmul
  1475. : MAX_LMEM; /* overflow; keep maximum value */
  1476. do { /* repeat until pause or enough "credit" (negative debt) */
  1477. lu_mem work = singlestep(L); /* perform one single step */
  1478. debt -= work;
  1479. } while (debt > -stepsize && g->gcstate != GCSpause);
  1480. if (g->gcstate == GCSpause)
  1481. setpause(g); /* pause until next cycle */
  1482. else {
  1483. debt = (debt / stepmul) * WORK2MEM; /* convert 'work units' to bytes */
  1484. luaE_setdebt(g, debt);
  1485. }
  1486. }
  1487. /*
  1488. ** Performs a basic GC step if collector is running. (If collector is
  1489. ** not running, set a reasonable debt to avoid it being called at
  1490. ** every single check.)
  1491. */
  1492. void luaC_step (lua_State *L) {
  1493. global_State *g = G(L);
  1494. if (!gcrunning(g)) /* not running? */
  1495. luaE_setdebt(g, -2000);
  1496. else {
  1497. if(isdecGCmodegen(g))
  1498. genstep(L, g);
  1499. else
  1500. incstep(L, g);
  1501. }
  1502. }
  1503. /*
  1504. ** Perform a full collection in incremental mode.
  1505. ** Before running the collection, check 'keepinvariant'; if it is true,
  1506. ** there may be some objects marked as black, so the collector has
  1507. ** to sweep all objects to turn them back to white (as white has not
  1508. ** changed, nothing will be collected).
  1509. */
  1510. static void fullinc (lua_State *L, global_State *g) {
  1511. if (keepinvariant(g)) /* black objects? */
  1512. entersweep(L); /* sweep everything to turn them back to white */
  1513. /* finish any pending sweep phase to start a new cycle */
  1514. luaC_runtilstate(L, bitmask(GCSpause));
  1515. luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */
  1516. g->gcstate = GCSenteratomic; /* go straight to atomic phase ??? */
  1517. luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */
  1518. /* estimate must be correct after a full GC cycle */
  1519. lua_assert(g->GCestimate == gettotalbytes(g));
  1520. luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */
  1521. setpause(g);
  1522. }
  1523. /*
  1524. ** Performs a full GC cycle; if 'isemergency', set a flag to avoid
  1525. ** some operations which could change the interpreter state in some
  1526. ** unexpected ways (running finalizers and shrinking some structures).
  1527. */
  1528. void luaC_fullgc (lua_State *L, int isemergency) {
  1529. global_State *g = G(L);
  1530. lua_assert(!g->gcemergency);
  1531. g->gcemergency = isemergency; /* set flag */
  1532. if (g->gckind == KGC_INC)
  1533. fullinc(L, g);
  1534. else
  1535. fullgen(L, g);
  1536. g->gcemergency = 0;
  1537. }
  1538. /* }====================================================== */