lgc.c 20 KB

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