lgc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. ** $Id: lgc.c,v 1.132 2002/03/20 18:54:29 roberto Exp roberto $
  3. ** Garbage Collector
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #include "lua.h"
  8. #include "ldebug.h"
  9. #include "ldo.h"
  10. #include "lfunc.h"
  11. #include "lgc.h"
  12. #include "lmem.h"
  13. #include "lobject.h"
  14. #include "lstate.h"
  15. #include "lstring.h"
  16. #include "ltable.h"
  17. #include "ltm.h"
  18. typedef struct GCState {
  19. Table *tmark; /* list of marked tables to be visited */
  20. Table *toclear; /* list of visited weak tables (to be cleared after GC) */
  21. lua_State *L;
  22. } GCState;
  23. /* mark a string; marks larger than 1 cannot be changed */
  24. #define strmark(s) {if ((s)->tsv.marked == 0) (s)->tsv.marked = 1;}
  25. /* mark tricks for userdata */
  26. #define isudmarked(u) (u->uv.len & 1)
  27. #define markud(u) (u->uv.len |= 1)
  28. #define unmarkud(u) (u->uv.len &= (~(size_t)1))
  29. #define isfinalized(u) (u->uv.len & 2)
  30. #define markfinalized(u) (u->uv.len |= 2)
  31. /* mark tricks for upvalues (assume that open upvalues are always marked) */
  32. #define isupvalmarked(uv) ((uv)->v != &(uv)->value)
  33. #define ismarkable(o) (!((1 << ttype(o)) & \
  34. ((1 << LUA_TNIL) | (1 << LUA_TNUMBER) | (1 << LUA_TBOOLEAN))))
  35. static void reallymarkobject (GCState *st, TObject *o);
  36. #define markobject(st,o) if (ismarkable(o)) reallymarkobject(st,o)
  37. static void protomark (Proto *f) {
  38. if (!f->marked) {
  39. int i;
  40. f->marked = 1;
  41. strmark(f->source);
  42. for (i=0; i<f->sizek; i++) {
  43. if (ttype(f->k+i) == LUA_TSTRING)
  44. strmark(tsvalue(f->k+i));
  45. }
  46. for (i=0; i<f->sizep; i++)
  47. protomark(f->p[i]);
  48. for (i=0; i<f->sizelocvars; i++) /* mark local-variable names */
  49. strmark(f->locvars[i].varname);
  50. }
  51. lua_assert(luaG_checkcode(f));
  52. }
  53. static void markclosure (GCState *st, Closure *cl) {
  54. if (!cl->c.marked) {
  55. cl->c.marked = 1;
  56. if (cl->c.isC) {
  57. int i;
  58. for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
  59. markobject(st, &cl->c.upvalue[i]);
  60. }
  61. else {
  62. int i;
  63. lua_assert(cl->l.nupvalues == cl->l.p->nupvalues);
  64. protomark(cl->l.p);
  65. for (i=0; i<cl->l.nupvalues; i++) { /* mark its upvalues */
  66. UpVal *u = cl->l.upvals[i];
  67. if (!isupvalmarked(u)) {
  68. markobject(st, &u->value);
  69. u->v = NULL; /* mark it! */
  70. }
  71. }
  72. }
  73. }
  74. }
  75. static void marktable (GCState *st, Table *h) {
  76. if (!ismarked(h)) {
  77. h->mark = st->tmark; /* chain it for later traversal */
  78. st->tmark = h;
  79. marktable(st, h->metatable);
  80. }
  81. }
  82. static void markudata (GCState *st, Udata *u) {
  83. markud(u);
  84. marktable(st, u->uv.metatable);
  85. }
  86. static void reallymarkobject (GCState *st, TObject *o) {
  87. switch (ttype(o)) {
  88. case LUA_TSTRING:
  89. strmark(tsvalue(o));
  90. break;
  91. case LUA_TUSERDATA:
  92. if (!isudmarked(uvalue(o)))
  93. markudata(st, uvalue(o));
  94. break;
  95. case LUA_TFUNCTION:
  96. markclosure(st, clvalue(o));
  97. break;
  98. case LUA_TTABLE: {
  99. marktable(st, hvalue(o));
  100. break;
  101. }
  102. default: lua_assert(0); /* should not be called with other types */
  103. }
  104. }
  105. static void checkstacksizes (lua_State *L, StkId max) {
  106. int used = L->ci - L->base_ci; /* number of `ci' in use */
  107. if (4*used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
  108. luaD_reallocCI(L, L->size_ci/2); /* still big enough... */
  109. used = max - L->stack; /* part of stack in use */
  110. if (4*used < L->stacksize && 2*BASIC_STACK_SIZE < L->stacksize)
  111. luaD_reallocstack(L, L->stacksize/2); /* still big enough... */
  112. }
  113. static void markstacks (GCState *st) {
  114. lua_State *L1 = st->L;
  115. do { /* for each thread */
  116. StkId o, lim;
  117. CallInfo *ci;
  118. if (L1->base_ci == NULL) { /* incomplete state? */
  119. lua_assert(L1 != st->L);
  120. L1 = L1->next;
  121. luaE_closethread(st->L, L1->previous); /* collect it */
  122. continue;
  123. }
  124. for (o=L1->stack; o<L1->top; o++)
  125. markobject(st, o);
  126. lim = o;
  127. for (ci = L1->base_ci; ci <= L1->ci; ci++) {
  128. lua_assert(ci->top <= L1->stack_last);
  129. if (lim < ci->top) lim = ci->top;
  130. }
  131. for (; o<=lim; o++) setnilvalue(o);
  132. checkstacksizes(L1, lim);
  133. lua_assert(L1->previous->next == L1 && L1->next->previous == L1);
  134. L1 = L1->next;
  135. } while (L1 != st->L);
  136. }
  137. static void marktmu (GCState *st) {
  138. Udata *u;
  139. for (u = G(st->L)->tmudata; u; u = u->uv.next)
  140. markudata(st, u);
  141. }
  142. /* move `dead' udata that need finalization to list `tmudata' */
  143. static void separateudata (lua_State *L) {
  144. Udata **p = &G(L)->rootudata;
  145. Udata *curr;
  146. Udata *collected = NULL; /* to collect udata with gc event */
  147. Udata **lastcollected = &collected;
  148. while ((curr = *p) != NULL) {
  149. if (isudmarked(curr) || isfinalized(curr) ||
  150. (fasttm(L, curr->uv.metatable, TM_GC) == NULL))
  151. p = &curr->uv.next;
  152. else { /* must call its gc method */
  153. *p = curr->uv.next;
  154. curr->uv.next = NULL; /* link `curr' at the end of `collected' list */
  155. *lastcollected = curr;
  156. lastcollected = &curr->uv.next;
  157. }
  158. }
  159. /* insert collected udata with gc event into `tmudata' list */
  160. *lastcollected = G(L)->tmudata;
  161. G(L)->tmudata = collected;
  162. }
  163. static void removekey (Node *n) {
  164. lua_assert(ttype(val(n)) == LUA_TNIL);
  165. if (ttype(key(n)) != LUA_TNIL && ttype(key(n)) != LUA_TNUMBER)
  166. setttype(key(n), LUA_TNONE); /* dead key; remove it */
  167. }
  168. static void traversetable (GCState *st, Table *h) {
  169. int i;
  170. const TObject *mode;
  171. int weakkey = 0;
  172. int weakvalue = 0;
  173. marktable(st, h->metatable);
  174. mode = fasttm(st->L, h->metatable, TM_WEAKMODE);
  175. if (mode) { /* weak table? must be cleared after GC... */
  176. h->mark = st->toclear; /* put in the appropriate list */
  177. st->toclear = h;
  178. if (ttype(mode) == LUA_TSTRING) {
  179. weakkey = (strchr(svalue(mode), 'k') != NULL);
  180. weakvalue = (strchr(svalue(mode), 'v') != NULL);
  181. }
  182. }
  183. if (!weakvalue) {
  184. i = sizearray(h);
  185. while (i--)
  186. markobject(st, &h->array[i]);
  187. }
  188. i = sizenode(h);
  189. while (i--) {
  190. Node *n = node(h, i);
  191. if (ttype(val(n)) != LUA_TNIL) {
  192. lua_assert(ttype(key(n)) != LUA_TNIL);
  193. if (!weakkey) markobject(st, key(n));
  194. if (!weakvalue) markobject(st, val(n));
  195. }
  196. }
  197. }
  198. static void propagatemarks (GCState *st) {
  199. while (st->tmark) { /* traverse marked tables */
  200. Table *h = st->tmark; /* get first table from list */
  201. st->tmark = h->mark; /* remove it from list */
  202. traversetable(st, h);
  203. }
  204. }
  205. static int hasmark (const TObject *o) {
  206. switch (ttype(o)) {
  207. case LUA_TSTRING:
  208. return tsvalue(o)->tsv.marked;
  209. case LUA_TUSERDATA:
  210. return isudmarked(uvalue(o));
  211. case LUA_TTABLE:
  212. return ismarked(hvalue(o));
  213. case LUA_TFUNCTION:
  214. return clvalue(o)->c.marked;
  215. default: /* number, nil, boolean */
  216. return 1;
  217. }
  218. }
  219. /*
  220. ** clear (set to nil) keys and values from weaktables that were collected
  221. */
  222. static void cleartables (Table *h) {
  223. for (; h; h = h->mark) {
  224. int i;
  225. i = sizearray(h);
  226. while (i--) {
  227. TObject *o = &h->array[i];
  228. if (!hasmark(o))
  229. setnilvalue(o); /* remove value */
  230. }
  231. i = sizenode(h);
  232. while (i--) {
  233. Node *n = node(h, i);
  234. if (!hasmark(val(n)) || !hasmark(key(n))) {
  235. setnilvalue(val(n)); /* remove value ... */
  236. removekey(n); /* ... and key */
  237. }
  238. }
  239. }
  240. }
  241. static void collectproto (lua_State *L) {
  242. Proto **p = &G(L)->rootproto;
  243. Proto *curr;
  244. while ((curr = *p) != NULL) {
  245. if (curr->marked) {
  246. curr->marked = 0;
  247. p = &curr->next;
  248. }
  249. else {
  250. *p = curr->next;
  251. luaF_freeproto(L, curr);
  252. }
  253. }
  254. }
  255. static void collectclosures (lua_State *L) {
  256. Closure **p = &G(L)->rootcl;
  257. Closure *curr;
  258. while ((curr = *p) != NULL) {
  259. if (curr->c.marked) {
  260. curr->c.marked = 0;
  261. p = &curr->c.next;
  262. }
  263. else {
  264. *p = curr->c.next;
  265. luaF_freeclosure(L, curr);
  266. }
  267. }
  268. }
  269. static void collectupval (lua_State *L) {
  270. UpVal **v = &G(L)->rootupval;
  271. UpVal *curr;
  272. while ((curr = *v) != NULL) {
  273. if (isupvalmarked(curr)) {
  274. lua_assert(curr->v == NULL);
  275. curr->v = &curr->value; /* unmark */
  276. v = &curr->next; /* next */
  277. }
  278. else {
  279. *v = curr->next; /* next */
  280. luaM_freelem(L, curr);
  281. }
  282. }
  283. }
  284. static void collecttable (lua_State *L) {
  285. Table **p = &G(L)->roottable;
  286. Table *curr;
  287. while ((curr = *p) != NULL) {
  288. if (ismarked(curr)) {
  289. curr->mark = curr; /* unmark */
  290. p = &curr->next;
  291. }
  292. else {
  293. *p = curr->next;
  294. luaH_free(L, curr);
  295. }
  296. }
  297. }
  298. static void collectudata (lua_State *L) {
  299. Udata **p = &G(L)->rootudata;
  300. Udata *curr;
  301. while ((curr = *p) != NULL) {
  302. if (isudmarked(curr)) {
  303. unmarkud(curr);
  304. p = &curr->uv.next;
  305. }
  306. else {
  307. *p = curr->uv.next;
  308. lua_assert(isfinalized(curr) ||
  309. fasttm(L, curr->uv.metatable, TM_GC) == NULL);
  310. luaM_free(L, curr, sizeudata(curr->uv.len & (~(size_t)3)));
  311. }
  312. }
  313. }
  314. static void collectstrings (lua_State *L, int all) {
  315. int i;
  316. for (i=0; i<G(L)->strt.size; i++) { /* for each list */
  317. TString **p = &G(L)->strt.hash[i];
  318. TString *curr;
  319. while ((curr = *p) != NULL) {
  320. if (curr->tsv.marked && !all) { /* preserve? */
  321. if (curr->tsv.marked < FIXMARK) /* does not change FIXMARKs */
  322. curr->tsv.marked = 0;
  323. p = &curr->tsv.nexthash;
  324. }
  325. else { /* collect */
  326. *p = curr->tsv.nexthash;
  327. G(L)->strt.nuse--;
  328. luaM_free(L, curr, sizestring(curr->tsv.len));
  329. }
  330. }
  331. }
  332. if (G(L)->strt.nuse < cast(ls_nstr, G(L)->strt.size/4) &&
  333. G(L)->strt.size > MINSTRTABSIZE*2)
  334. luaS_resize(L, G(L)->strt.size/2); /* table is too big */
  335. }
  336. #define MINBUFFER 256
  337. static void checkMbuffer (lua_State *L) {
  338. if (G(L)->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
  339. size_t newsize = G(L)->Mbuffsize/2; /* still larger than MINBUFFER */
  340. luaM_reallocvector(L, G(L)->Mbuffer, G(L)->Mbuffsize, newsize, char);
  341. G(L)->Mbuffsize = newsize;
  342. }
  343. }
  344. static void do1gcTM (lua_State *L, Udata *udata) {
  345. const TObject *tm = fasttm(L, udata->uv.metatable, TM_GC);
  346. if (tm != NULL) {
  347. setobj(L->top, tm);
  348. setuvalue(L->top+1, udata);
  349. L->top += 2;
  350. luaD_call(L, L->top - 2, 0);
  351. }
  352. }
  353. static void callGCTM (lua_State *L) {
  354. int oldah = L->allowhooks;
  355. L->allowhooks = 0; /* stop debug hooks during GC tag methods */
  356. L->top++; /* reserve space to keep udata while runs its gc method */
  357. while (G(L)->tmudata != NULL) {
  358. Udata *udata = G(L)->tmudata;
  359. G(L)->tmudata = udata->uv.next; /* remove udata from `tmudata' */
  360. udata->uv.next = G(L)->rootudata; /* return it to `root' list */
  361. G(L)->rootudata = udata;
  362. setuvalue(L->top - 1, udata); /* keep a reference to it */
  363. unmarkud(udata);
  364. markfinalized(udata);
  365. do1gcTM(L, udata);
  366. }
  367. L->top--;
  368. L->allowhooks = oldah; /* restore hooks */
  369. }
  370. void luaC_callallgcTM (lua_State *L) {
  371. separateudata(L);
  372. callGCTM(L); /* call their GC tag methods */
  373. }
  374. void luaC_collect (lua_State *L, int all) {
  375. collectudata(L);
  376. collectstrings(L, all);
  377. collecttable(L);
  378. collectproto(L);
  379. collectupval(L);
  380. collectclosures(L);
  381. }
  382. void luaC_collectgarbage (lua_State *L) {
  383. GCState st;
  384. st.L = L;
  385. st.tmark = NULL;
  386. st.toclear = NULL;
  387. markstacks(&st); /* mark all stacks */
  388. propagatemarks(&st); /* mark all reachable objects */
  389. separateudata(L); /* separate userdata to be preserved */
  390. marktmu(&st); /* mark `preserved' userdata */
  391. propagatemarks(&st); /* remark */
  392. cleartables(st.toclear);
  393. luaC_collect(L, 0);
  394. checkMbuffer(L);
  395. G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */
  396. callGCTM(L);
  397. }