lgc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /*
  2. ** $Id: lgc.c,v 2.41 2007/10/29 16:51:20 roberto Exp roberto $
  3. ** Garbage Collector
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #define lgc_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lfunc.h"
  13. #include "lgc.h"
  14. #include "lmem.h"
  15. #include "lobject.h"
  16. #include "lstate.h"
  17. #include "lstring.h"
  18. #include "ltable.h"
  19. #include "ltm.h"
  20. #define GCSTEPSIZE 1024u
  21. #define GCSWEEPMAX 40
  22. #define GCSWEEPCOST 10
  23. #define GCFINALIZECOST 100
  24. #define maskmarks cast_byte(~(bitmask(BLACKBIT)|WHITEBITS))
  25. #define makewhite(g,x) \
  26. ((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g)))
  27. #define white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
  28. #define black2gray(x) resetbit((x)->gch.marked, BLACKBIT)
  29. #define stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT)
  30. #define isfinalized(u) testbit((u)->marked, FINALIZEDBIT)
  31. #define markfinalized(u) l_setbit((u)->marked, FINALIZEDBIT)
  32. #define markvalue(g,o) { checkconsistency(o); \
  33. if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); }
  34. #define markobject(g,t) { if ((t) && iswhite(obj2gco(t))) \
  35. reallymarkobject(g, obj2gco(t)); }
  36. #define setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpause)
  37. static void linktable (Table *h, GCObject **p) {
  38. h->gclist = *p;
  39. *p = obj2gco(h);
  40. }
  41. static void removeentry (Node *n) {
  42. lua_assert(ttisnil(gval(n)));
  43. if (iscollectable(gkey(n)))
  44. setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */
  45. }
  46. /*
  47. ** The next function tells whether a key or value can be cleared from
  48. ** a weak table. Non-collectable objects are never removed from weak
  49. ** tables. Strings behave as `values', so are never removed too. for
  50. ** other objects: if really collected, cannot keep them; for userdata
  51. ** being finalized, keep them in keys, but not in values
  52. */
  53. static int iscleared (const TValue *o, int iskey) {
  54. if (!iscollectable(o)) return 0;
  55. if (ttisstring(o)) {
  56. stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */
  57. return 0;
  58. }
  59. return iswhite(gcvalue(o)) ||
  60. (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o))));
  61. }
  62. static void reallymarkobject (global_State *g, GCObject *o) {
  63. lua_assert(iswhite(o) && !isdead(g, o));
  64. white2gray(o);
  65. switch (o->gch.tt) {
  66. case LUA_TSTRING: {
  67. return;
  68. }
  69. case LUA_TUSERDATA: {
  70. Table *mt = gco2u(o)->metatable;
  71. gray2black(o); /* udata are never gray */
  72. markobject(g, mt);
  73. markobject(g, gco2u(o)->env);
  74. return;
  75. }
  76. case LUA_TUPVAL: {
  77. UpVal *uv = gco2uv(o);
  78. markvalue(g, uv->v);
  79. if (uv->v == &uv->u.value) /* closed? */
  80. gray2black(o); /* open upvalues are never black */
  81. return;
  82. }
  83. case LUA_TFUNCTION: {
  84. gco2cl(o)->c.gclist = g->gray;
  85. g->gray = o;
  86. break;
  87. }
  88. case LUA_TTABLE: {
  89. linktable(gco2h(o), &g->gray);
  90. break;
  91. }
  92. case LUA_TTHREAD: {
  93. gco2th(o)->gclist = g->gray;
  94. g->gray = o;
  95. break;
  96. }
  97. case LUA_TPROTO: {
  98. gco2p(o)->gclist = g->gray;
  99. g->gray = o;
  100. break;
  101. }
  102. default: lua_assert(0);
  103. }
  104. }
  105. static void marktmu (global_State *g) {
  106. GCObject *u = g->tmudata;
  107. if (u) {
  108. do {
  109. u = u->gch.next;
  110. makewhite(g, u); /* may be marked, if left from previous GC */
  111. reallymarkobject(g, u);
  112. } while (u != g->tmudata);
  113. }
  114. }
  115. /* move `dead' udata that need finalization to list `tmudata' */
  116. size_t luaC_separateudata (lua_State *L, int all) {
  117. global_State *g = G(L);
  118. size_t deadmem = 0;
  119. GCObject **p = &g->mainthread->next;
  120. GCObject *curr;
  121. while ((curr = *p) != NULL) {
  122. if (!(iswhite(curr) || all) || isfinalized(gco2u(curr)))
  123. p = &curr->gch.next; /* don't bother with them */
  124. else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) {
  125. markfinalized(gco2u(curr)); /* don't need finalization */
  126. p = &curr->gch.next;
  127. }
  128. else { /* must call its gc method */
  129. deadmem += sizeudata(gco2u(curr));
  130. markfinalized(gco2u(curr));
  131. *p = curr->gch.next;
  132. /* link `curr' at the end of `tmudata' list */
  133. if (g->tmudata == NULL) /* list is empty? */
  134. g->tmudata = curr->gch.next = curr; /* creates a circular list */
  135. else {
  136. curr->gch.next = g->tmudata->gch.next;
  137. g->tmudata->gch.next = curr;
  138. g->tmudata = curr;
  139. }
  140. }
  141. }
  142. return deadmem;
  143. }
  144. static void traverseweakvalue (global_State *g, Table *h) {
  145. int i = sizenode(h);
  146. while (i--) {
  147. Node *n = gnode(h, i);
  148. lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n)));
  149. if (ttisnil(gval(n)))
  150. removeentry(n); /* remove empty entries */
  151. else {
  152. lua_assert(!ttisnil(gkey(n)));
  153. markvalue(g, gkey(n));
  154. }
  155. }
  156. linktable(h, &g->weak);
  157. }
  158. static int traverseephemeron (global_State *g, Table *h) {
  159. int marked = 0;
  160. int hasclears = 0;
  161. int i = h->sizearray;
  162. while (i--) { /* mark array part (numeric keys are 'strong') */
  163. if (iscollectable(&h->array[i]) && iswhite(gcvalue(&h->array[i]))) {
  164. marked = 1;
  165. reallymarkobject(g, gcvalue(&h->array[i]));
  166. }
  167. }
  168. i = sizenode(h);
  169. while (i--) {
  170. Node *n = gnode(h, i);
  171. lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n)));
  172. if (ttisnil(gval(n))) /* entry is empty? */
  173. removeentry(n); /* remove it */
  174. else if (iscollectable(gval(n)) && iswhite(gcvalue(gval(n)))) {
  175. /* value is not marked yet */
  176. if (iscleared(key2tval(n), 1)) /* key is not marked (yet)? */
  177. hasclears = 1; /* may have to propagate mark from key to value */
  178. else { /* mark value only if key is marked */
  179. marked = 1; /* some mark changed status */
  180. reallymarkobject(g, gcvalue(gval(n)));
  181. }
  182. }
  183. }
  184. if (hasclears)
  185. linktable(h, &g->ephemeron);
  186. else /* nothing to propagate */
  187. linktable(h, &g->weak); /* avoid convergence phase */
  188. return marked;
  189. }
  190. static void traversestrongtable (global_State *g, Table *h) {
  191. int i;
  192. i = h->sizearray;
  193. while (i--)
  194. markvalue(g, &h->array[i]);
  195. i = sizenode(h);
  196. while (i--) {
  197. Node *n = gnode(h, i);
  198. lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n)));
  199. if (ttisnil(gval(n)))
  200. removeentry(n); /* remove empty entries */
  201. else {
  202. lua_assert(!ttisnil(gkey(n)));
  203. markvalue(g, gkey(n));
  204. markvalue(g, gval(n));
  205. }
  206. }
  207. }
  208. static void traversetable (global_State *g, Table *h) {
  209. const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
  210. markobject(g, h->metatable);
  211. if (mode && ttisstring(mode)) { /* is there a weak mode? */
  212. int weakkey = (strchr(svalue(mode), 'k') != NULL);
  213. int weakvalue = (strchr(svalue(mode), 'v') != NULL);
  214. if (weakkey || weakvalue) { /* is really weak? */
  215. black2gray(obj2gco(h)); /* keep table gray */
  216. if (!weakkey) /* strong keys? */
  217. traverseweakvalue(g, h);
  218. else if (!weakvalue) /* strong values? */
  219. traverseephemeron(g, h);
  220. else {
  221. lua_assert(weakkey && weakvalue); /* nothing to traverse now */
  222. linktable(h, &g->allweak);
  223. }
  224. return;
  225. } /* else go through */
  226. }
  227. traversestrongtable(g, h);
  228. }
  229. /*
  230. ** All marks are conditional because a GC may happen while the
  231. ** prototype is still being created
  232. */
  233. static void traverseproto (global_State *g, Proto *f) {
  234. int i;
  235. if (f->source) stringmark(f->source);
  236. for (i=0; i<f->sizek; i++) /* mark literals */
  237. markvalue(g, &f->k[i]);
  238. for (i=0; i<f->sizeupvalues; i++) { /* mark upvalue names */
  239. if (f->upvalues[i])
  240. stringmark(f->upvalues[i]);
  241. }
  242. for (i=0; i<f->sizep; i++) /* mark nested protos */
  243. markobject(g, f->p[i]);
  244. for (i=0; i<f->sizelocvars; i++) { /* mark local-variable names */
  245. if (f->locvars[i].varname)
  246. stringmark(f->locvars[i].varname);
  247. }
  248. }
  249. static void traverseclosure (global_State *g, Closure *cl) {
  250. markobject(g, cl->c.env);
  251. if (cl->c.isC) {
  252. int i;
  253. for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
  254. markvalue(g, &cl->c.upvalue[i]);
  255. }
  256. else {
  257. int i;
  258. lua_assert(cl->l.nupvalues == cl->l.p->nups);
  259. markobject(g, cl->l.p);
  260. for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */
  261. markobject(g, cl->l.upvals[i]);
  262. }
  263. }
  264. static void checkstacksizes (lua_State *L, StkId max) {
  265. int ci_used = cast_int(L->ci - L->base_ci); /* number of `ci' in use */
  266. int s_used = cast_int(max - L->stack); /* part of stack in use */
  267. if (L->size_ci > LUAI_MAXCALLS) /* handling overflow? */
  268. return; /* do not touch the stacks */
  269. if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
  270. luaD_reallocCI(L, L->size_ci/2); /* still big enough... */
  271. condhardstacktests(luaD_reallocCI(L, ci_used + 1));
  272. if (4*s_used < L->stacksize &&
  273. 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize)
  274. luaD_reallocstack(L, L->stacksize/2); /* still big enough... */
  275. condhardstacktests(luaD_reallocstack(L, s_used));
  276. }
  277. static void traversestack (global_State *g, lua_State *l) {
  278. StkId o, lim;
  279. CallInfo *ci;
  280. if (l->stack == NULL || l->base_ci == NULL)
  281. return; /* stack not completely built yet */
  282. markvalue(g, gt(l));
  283. lim = l->top;
  284. for (ci = l->base_ci; ci <= l->ci; ci++) {
  285. lua_assert(ci->top <= l->stack_last);
  286. if (lim < ci->top) lim = ci->top;
  287. }
  288. for (o = l->stack; o < l->top; o++)
  289. markvalue(g, o);
  290. for (; o <= lim; o++)
  291. setnilvalue(o);
  292. if (!g->emergencygc) /* cannot change stack in emergency... */
  293. checkstacksizes(l, lim); /* ...(mutator does not expect that change) */
  294. }
  295. /*
  296. ** traverse one gray object, turning it to black.
  297. ** Returns `quantity' traversed.
  298. */
  299. static l_mem propagatemark (global_State *g) {
  300. GCObject *o = g->gray;
  301. lua_assert(isgray(o));
  302. gray2black(o);
  303. switch (o->gch.tt) {
  304. case LUA_TTABLE: {
  305. Table *h = gco2h(o);
  306. g->gray = h->gclist;
  307. traversetable(g, h);
  308. return sizeof(Table) + sizeof(TValue) * h->sizearray +
  309. sizeof(Node) * sizenode(h);
  310. }
  311. case LUA_TFUNCTION: {
  312. Closure *cl = gco2cl(o);
  313. g->gray = cl->c.gclist;
  314. traverseclosure(g, cl);
  315. return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) :
  316. sizeLclosure(cl->l.nupvalues);
  317. }
  318. case LUA_TTHREAD: {
  319. lua_State *th = gco2th(o);
  320. g->gray = th->gclist;
  321. th->gclist = g->grayagain;
  322. g->grayagain = o;
  323. black2gray(o);
  324. traversestack(g, th);
  325. return sizeof(lua_State) + sizeof(TValue) * th->stacksize +
  326. sizeof(CallInfo) * th->size_ci;
  327. }
  328. case LUA_TPROTO: {
  329. Proto *p = gco2p(o);
  330. g->gray = p->gclist;
  331. traverseproto(g, p);
  332. return sizeof(Proto) + sizeof(Instruction) * p->sizecode +
  333. sizeof(Proto *) * p->sizep +
  334. sizeof(TValue) * p->sizek +
  335. sizeof(int) * p->sizelineinfo +
  336. sizeof(LocVar) * p->sizelocvars +
  337. sizeof(TString *) * p->sizeupvalues;
  338. }
  339. default: lua_assert(0); return 0;
  340. }
  341. }
  342. static size_t propagateall (global_State *g) {
  343. size_t m = 0;
  344. while (g->gray) m += propagatemark(g);
  345. return m;
  346. }
  347. static void convergeephemerons (global_State *g) {
  348. int changed;
  349. do {
  350. GCObject *w;
  351. GCObject *next = g->ephemeron;
  352. g->ephemeron = NULL;
  353. changed = 0;
  354. while ((w = next) != NULL) {
  355. next = gco2h(w)->gclist;
  356. if (traverseephemeron(g, gco2h(w))) {
  357. changed = 1;
  358. propagateall(g);
  359. }
  360. }
  361. } while (changed);
  362. }
  363. /*
  364. ** clear collected entries from weaktables
  365. */
  366. static void cleartable (GCObject *l) {
  367. while (l) {
  368. Table *h = gco2h(l);
  369. int i = h->sizearray;
  370. while (i--) {
  371. TValue *o = &h->array[i];
  372. if (iscleared(o, 0)) /* value was collected? */
  373. setnilvalue(o); /* remove value */
  374. }
  375. i = sizenode(h);
  376. while (i--) {
  377. Node *n = gnode(h, i);
  378. if (!ttisnil(gval(n)) && /* non-empty entry? */
  379. (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) {
  380. setnilvalue(gval(n)); /* remove value ... */
  381. removeentry(n); /* remove entry from table */
  382. }
  383. }
  384. l = h->gclist;
  385. }
  386. }
  387. static void freeobj (lua_State *L, GCObject *o) {
  388. switch (o->gch.tt) {
  389. case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
  390. case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break;
  391. case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break;
  392. case LUA_TTABLE: luaH_free(L, gco2h(o)); break;
  393. case LUA_TTHREAD: {
  394. lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread);
  395. luaE_freethread(L, gco2th(o));
  396. break;
  397. }
  398. case LUA_TSTRING: {
  399. G(L)->strt.nuse--;
  400. luaM_freemem(L, o, sizestring(gco2ts(o)));
  401. break;
  402. }
  403. case LUA_TUSERDATA: {
  404. luaM_freemem(L, o, sizeudata(gco2u(o)));
  405. break;
  406. }
  407. default: lua_assert(0);
  408. }
  409. }
  410. #define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM)
  411. static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
  412. GCObject *curr;
  413. global_State *g = G(L);
  414. int deadmask = otherwhite(g);
  415. while ((curr = *p) != NULL && count-- > 0) {
  416. if (curr->gch.tt == LUA_TTHREAD) /* sweep open upvalues of each thread */
  417. sweepwholelist(L, &gco2th(curr)->openupval);
  418. if ((curr->gch.marked ^ WHITEBITS) & deadmask) { /* not dead? */
  419. lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT));
  420. makewhite(g, curr); /* make it white (for next cycle) */
  421. p = &curr->gch.next;
  422. }
  423. else { /* must erase `curr' */
  424. lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT));
  425. *p = curr->gch.next;
  426. if (curr == g->rootgc) /* is the first element of the list? */
  427. g->rootgc = curr->gch.next; /* adjust first */
  428. freeobj(L, curr);
  429. }
  430. }
  431. return p;
  432. }
  433. static void checkSizes (lua_State *L) {
  434. global_State *g = G(L);
  435. /* check size of string hash */
  436. if (g->strt.nuse < cast(lu_int32, g->strt.size/4) &&
  437. g->strt.size > MINSTRTABSIZE*2)
  438. luaS_resize(L, g->strt.size/2); /* table is too big */
  439. /* check size of buffer */
  440. if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) { /* buffer too big? */
  441. size_t newsize = luaZ_sizebuffer(&g->buff) / 2;
  442. luaZ_resizebuffer(L, &g->buff, newsize);
  443. }
  444. }
  445. static Udata *udata2finalize (global_State *g) {
  446. GCObject *o = g->tmudata->gch.next; /* get first element */
  447. Udata *udata = rawgco2u(o);
  448. /* remove udata from `tmudata' */
  449. if (o == g->tmudata) /* last element? */
  450. g->tmudata = NULL;
  451. else
  452. g->tmudata->gch.next = udata->uv.next;
  453. udata->uv.next = g->mainthread->next; /* return it to `root' list */
  454. g->mainthread->next = o;
  455. makewhite(g, o);
  456. return udata;
  457. }
  458. static void GCTM (lua_State *L) {
  459. global_State *g = G(L);
  460. Udata *udata = udata2finalize(g);
  461. const TValue *tm = fasttm(L, udata->uv.metatable, TM_GC);
  462. if (tm != NULL) {
  463. lu_byte oldah = L->allowhook;
  464. lu_mem oldt = g->GCthreshold;
  465. L->allowhook = 0; /* stop debug hooks during GC tag method */
  466. g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */
  467. setobj2s(L, L->top, tm);
  468. setuvalue(L, L->top+1, udata);
  469. L->top += 2;
  470. luaD_call(L, L->top - 2, 0);
  471. L->allowhook = oldah; /* restore hooks */
  472. g->GCthreshold = oldt; /* restore threshold */
  473. }
  474. }
  475. /*
  476. ** Call all GC tag methods
  477. */
  478. void luaC_callGCTM (lua_State *L) {
  479. global_State *g = G(L);
  480. GCObject *last = g->tmudata;
  481. GCObject *curr;
  482. if (last == NULL) return; /* empty list? */
  483. do {
  484. curr = g->tmudata->gch.next; /* element to be collected */
  485. GCTM(L);
  486. } while (curr != last); /* go only until original last */
  487. /* do not finalize new udata created during previous finalizations */
  488. while (g->tmudata)
  489. udata2finalize(g); /* simply remove them from list */
  490. }
  491. void luaC_freeall (lua_State *L) {
  492. global_State *g = G(L);
  493. int i;
  494. g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT); /* mask to collect all elements */
  495. sweepwholelist(L, &g->rootgc);
  496. for (i = 0; i < g->strt.size; i++) /* free all string lists */
  497. sweepwholelist(L, &g->strt.hash[i]);
  498. }
  499. static void markmt (global_State *g) {
  500. int i;
  501. for (i=0; i<NUM_TAGS; i++)
  502. markobject(g, g->mt[i]);
  503. }
  504. /* mark root set */
  505. static void markroot (lua_State *L) {
  506. global_State *g = G(L);
  507. g->gray = NULL;
  508. g->grayagain = NULL;
  509. g->weak = g->ephemeron = g->allweak = NULL;
  510. markobject(g, g->mainthread);
  511. /* make global table be traversed before main stack */
  512. markvalue(g, gt(g->mainthread));
  513. markvalue(g, registry(L));
  514. markmt(g);
  515. g->gcstate = GCSpropagate;
  516. }
  517. static void remarkupvals (global_State *g) {
  518. UpVal *uv;
  519. for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
  520. lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
  521. if (isgray(obj2gco(uv)))
  522. markvalue(g, uv->v);
  523. }
  524. }
  525. static void atomic (lua_State *L) {
  526. global_State *g = G(L);
  527. size_t udsize; /* total size of userdata to be finalized */
  528. /* remark occasional upvalues of (maybe) dead threads */
  529. remarkupvals(g);
  530. /* traverse objects cautch by write barrier and by 'remarkupvals' */
  531. propagateall(g);
  532. /* remark weak tables */
  533. g->gray = g->weak;
  534. g->weak = NULL;
  535. lua_assert(!iswhite(obj2gco(g->mainthread)));
  536. markobject(g, L); /* mark running thread */
  537. markmt(g); /* mark basic metatables (again) */
  538. propagateall(g);
  539. /* remark ephemeron tables */
  540. g->gray = g->ephemeron;
  541. g->ephemeron = NULL;
  542. propagateall(g);
  543. /* remark gray again */
  544. g->gray = g->grayagain;
  545. g->grayagain = NULL;
  546. propagateall(g);
  547. convergeephemerons(g);
  548. udsize = luaC_separateudata(L, 0); /* separate userdata to be finalized */
  549. marktmu(g); /* mark `preserved' userdata */
  550. udsize += propagateall(g); /* remark, to propagate `preserveness' */
  551. convergeephemerons(g);
  552. /* remove collected objects from weak tables */
  553. cleartable(g->weak);
  554. cleartable(g->ephemeron);
  555. cleartable(g->allweak);
  556. /* flip current white */
  557. g->currentwhite = cast_byte(otherwhite(g));
  558. g->sweepstrgc = 0;
  559. g->sweepgc = &g->rootgc;
  560. g->gcstate = GCSsweepstring;
  561. g->estimate = g->totalbytes - udsize; /* first estimate */
  562. }
  563. #define correctestimate(g,s) {lu_mem old = g->totalbytes; s; \
  564. lua_assert(old >= g->totalbytes); g->estimate -= old - g->totalbytes;}
  565. static l_mem singlestep (lua_State *L) {
  566. global_State *g = G(L);
  567. /*lua_checkmemory(L);*/
  568. switch (g->gcstate) {
  569. case GCSpause: {
  570. markroot(L); /* start a new collection */
  571. return 0;
  572. }
  573. case GCSpropagate: {
  574. if (g->gray)
  575. return propagatemark(g);
  576. else { /* no more `gray' objects */
  577. atomic(L); /* finish mark phase */
  578. return 0;
  579. }
  580. }
  581. case GCSsweepstring: {
  582. correctestimate(g, sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]));
  583. if (g->sweepstrgc >= g->strt.size) /* nothing more to sweep? */
  584. g->gcstate = GCSsweep; /* end sweep-string phase */
  585. return GCSWEEPCOST;
  586. }
  587. case GCSsweep: {
  588. correctestimate(g, g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX));
  589. if (*g->sweepgc == NULL) /* nothing more to sweep? */
  590. g->gcstate = GCSfinalize; /* end sweep phase */
  591. return GCSWEEPMAX*GCSWEEPCOST;
  592. }
  593. case GCSfinalize: {
  594. if (g->tmudata) {
  595. GCTM(L);
  596. if (g->estimate > GCFINALIZECOST)
  597. g->estimate -= GCFINALIZECOST;
  598. return GCFINALIZECOST;
  599. }
  600. else {
  601. correctestimate(g, checkSizes(L));
  602. g->gcstate = GCSpause; /* end collection */
  603. g->gcdept = 0;
  604. return 0;
  605. }
  606. }
  607. default: lua_assert(0); return 0;
  608. }
  609. }
  610. void luaC_step (lua_State *L) {
  611. global_State *g = G(L);
  612. l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul;
  613. lua_assert(!g->emergencygc);
  614. if (lim == 0)
  615. lim = (MAX_LUMEM-1)/2; /* no limit */
  616. g->gcdept += g->totalbytes - g->GCthreshold;
  617. do {
  618. lim -= singlestep(L);
  619. if (g->gcstate == GCSpause)
  620. break;
  621. } while (lim > 0);
  622. if (g->gcstate != GCSpause) {
  623. if (g->gcdept < GCSTEPSIZE)
  624. g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/g->gcstepmul;*/
  625. else {
  626. g->gcdept -= GCSTEPSIZE;
  627. g->GCthreshold = g->totalbytes;
  628. }
  629. }
  630. else {
  631. lua_assert(g->totalbytes >= g->estimate);
  632. setthreshold(g);
  633. }
  634. }
  635. void luaC_fullgc (lua_State *L, int isemergency) {
  636. int stopstate;
  637. global_State *g = G(L);
  638. g->emergencygc = isemergency;
  639. if (g->gcstate <= GCSpropagate) {
  640. /* reset sweep marks to sweep all elements (returning them to white) */
  641. g->sweepstrgc = 0;
  642. g->sweepgc = &g->rootgc;
  643. /* reset other collector lists */
  644. g->gray = NULL;
  645. g->grayagain = NULL;
  646. g->weak = g->ephemeron = g->allweak = NULL;
  647. g->gcstate = GCSsweepstring;
  648. }
  649. lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate);
  650. /* finish any pending sweep phase */
  651. while (g->gcstate != GCSfinalize) {
  652. lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep);
  653. singlestep(L);
  654. }
  655. markroot(L);
  656. /* do not run finalizers during emergency GC */
  657. stopstate = isemergency ? GCSfinalize : GCSpause;
  658. while (g->gcstate != stopstate)
  659. singlestep(L);
  660. setthreshold(g);
  661. g->emergencygc = 0;
  662. }
  663. void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) {
  664. global_State *g = G(L);
  665. lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
  666. lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
  667. lua_assert(ttype(&o->gch) != LUA_TTABLE);
  668. /* must keep invariant? */
  669. if (g->gcstate == GCSpropagate)
  670. reallymarkobject(g, v); /* restore invariant */
  671. else /* don't mind */
  672. makewhite(g, o); /* mark as white just to avoid other barriers */
  673. }
  674. void luaC_barrierback (lua_State *L, Table *t) {
  675. global_State *g = G(L);
  676. GCObject *o = obj2gco(t);
  677. lua_assert(isblack(o) && !isdead(g, o));
  678. lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
  679. black2gray(o); /* make table gray (again) */
  680. t->gclist = g->grayagain;
  681. g->grayagain = o;
  682. }
  683. void luaC_link (lua_State *L, GCObject *o, lu_byte tt) {
  684. global_State *g = G(L);
  685. o->gch.next = g->rootgc;
  686. g->rootgc = o;
  687. o->gch.marked = luaC_white(g);
  688. o->gch.tt = tt;
  689. }
  690. void luaC_linkupval (lua_State *L, UpVal *uv) {
  691. global_State *g = G(L);
  692. GCObject *o = obj2gco(uv);
  693. o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */
  694. g->rootgc = o;
  695. if (isgray(o)) {
  696. if (g->gcstate == GCSpropagate) {
  697. gray2black(o); /* closed upvalues need barrier */
  698. luaC_barrier(L, uv, uv->v);
  699. }
  700. else { /* sweep phase: sweep it (turning it into white) */
  701. makewhite(g, o);
  702. lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
  703. }
  704. }
  705. }