lgc.c 56 KB

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