lgc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. ** $Id: lgc.c,v 1.109 2001/06/28 14:57:17 roberto Exp $
  3. ** Garbage Collector
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define LUA_PRIVATE
  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. Hash *tmark; /* list of marked tables to be visited */
  20. Closure *cmark; /* list of marked closures to be visited */
  21. } GCState;
  22. /* mark a string; marks larger than 1 cannot be changed */
  23. #define strmark(s) {if ((s)->tsv.marked == 0) (s)->tsv.marked = 1;}
  24. static void protomark (Proto *f) {
  25. if (!f->marked) {
  26. int i;
  27. f->marked = 1;
  28. strmark(f->source);
  29. for (i=0; i<f->sizek; i++) {
  30. if (ttype(f->k+i) == LUA_TSTRING)
  31. strmark(tsvalue(f->k+i));
  32. }
  33. for (i=0; i<f->sizep; i++)
  34. protomark(f->p[i]);
  35. for (i=0; i<f->sizelocvars; i++) /* mark local-variable names */
  36. strmark(f->locvars[i].varname);
  37. }
  38. lua_assert(luaG_checkcode(f));
  39. }
  40. static void markclosure (GCState *st, Closure *cl) {
  41. if (!ismarked(cl)) {
  42. cl->mark = st->cmark; /* chain it for later traversal */
  43. st->cmark = cl;
  44. }
  45. }
  46. static void marktable (GCState *st, Hash *h) {
  47. if (!ismarked(h)) {
  48. h->mark = st->tmark; /* chain it for later traversal */
  49. st->tmark = h;
  50. }
  51. }
  52. static void markobject (GCState *st, TObject *o) {
  53. switch (ttype(o)) {
  54. case LUA_TSTRING:
  55. strmark(tsvalue(o));
  56. break;
  57. case LUA_TUSERDATA:
  58. if (!ismarkedudata(uvalue(o)))
  59. switchudatamark(uvalue(o));
  60. break;
  61. case LUA_TFUNCTION:
  62. markclosure(st, clvalue(o));
  63. break;
  64. case LUA_TTABLE: {
  65. marktable(st, hvalue(o));
  66. break;
  67. }
  68. default: {
  69. lua_assert(0 <= ttype(o) && ttype(o) <= LUA_TUPVAL);
  70. break;
  71. }
  72. }
  73. }
  74. static void markstacks (lua_State *L, GCState *st) {
  75. lua_State *L1 = L;
  76. do { /* for each thread */
  77. StkId o, lim;
  78. marktable(st, L1->gt); /* mark table of globals */
  79. for (o=L1->stack; o<L1->top; o++)
  80. markobject(st, o);
  81. lim = (L1->stack_last - L1->ci->base > MAXSTACK) ? L1->ci->base+MAXSTACK
  82. : L1->stack_last;
  83. for (; o<=lim; o++) setnilvalue(o);
  84. lua_assert(L1->previous->next == L1 && L1->next->previous == L1);
  85. L1 = L1->next;
  86. } while (L1 != L);
  87. }
  88. static void marktagmethods (global_State *G, GCState *st) {
  89. int t;
  90. for (t=0; t<G->ntag; t++) {
  91. struct TM *tm = &G->TMtable[t];
  92. int e;
  93. if (tm->name) strmark(tm->name);
  94. for (e=0; e<TM_N; e++) {
  95. Closure *cl = tm->method[e];
  96. if (cl) markclosure(st, cl);
  97. }
  98. }
  99. }
  100. static void traverseclosure (GCState *st, Closure *cl) {
  101. if (cl->isC) {
  102. int i;
  103. for (i=0; i<cl->nupvalues; i++) /* mark its upvalues */
  104. markobject(st, &cl->u.c.upvalue[i]);
  105. }
  106. else {
  107. int i;
  108. lua_assert(cl->nupvalues == cl->u.l.p->nupvalues);
  109. protomark(cl->u.l.p);
  110. for (i=0; i<cl->nupvalues; i++) { /* mark its upvalues */
  111. if (luaF_isclosed(cl, i)) {
  112. UpVal *u = cast(UpVal *, cl->u.l.upvals[i]);
  113. if (!u->marked) {
  114. u->marked = 1;
  115. markobject(st, &u->val);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. static void removekey (Node *n) {
  122. lua_assert(ttype(val(n)) == LUA_TNIL);
  123. if (ttype(key(n)) != LUA_TNIL && ttype(key(n)) != LUA_TNUMBER)
  124. setttype(key(n), LUA_TNONE); /* dead key; remove it */
  125. }
  126. static void traversetable (GCState *st, Hash *h) {
  127. int i;
  128. int mode = h->weakmode;
  129. if (mode == (LUA_WEAK_KEY | LUA_WEAK_VALUE))
  130. return; /* avoid traversing if both keys and values are weak */
  131. for (i=0; i<h->size; i++) {
  132. Node *n = node(h, i);
  133. if (ttype(val(n)) == LUA_TNIL)
  134. removekey(n);
  135. else {
  136. lua_assert(ttype(key(n)) != LUA_TNIL);
  137. if (ttype(key(n)) != LUA_TNUMBER && !(mode & LUA_WEAK_KEY))
  138. markobject(st, key(n));
  139. if (!(mode & LUA_WEAK_VALUE))
  140. markobject(st, val(n));
  141. }
  142. }
  143. }
  144. static void markall (lua_State *L) {
  145. GCState st;
  146. st.cmark = NULL;
  147. st.tmark = NULL;
  148. marktagmethods(G(L), &st); /* mark tag methods */
  149. markstacks(L, &st); /* mark all stacks */
  150. marktable(&st, G(L)->type2tag);
  151. marktable(&st, G(L)->registry);
  152. marktable(&st, G(L)->weakregistry);
  153. for (;;) { /* mark tables and closures */
  154. if (st.cmark) {
  155. Closure *cl = st.cmark; /* get first closure from list */
  156. st.cmark = cl->mark; /* remove it from list */
  157. traverseclosure(&st, cl);
  158. }
  159. else if (st.tmark) {
  160. Hash *h = st.tmark; /* get first table from list */
  161. st.tmark = h->mark; /* remove it from list */
  162. traversetable(&st, h);
  163. }
  164. else break; /* nothing else to mark */
  165. }
  166. }
  167. static int hasmark (const TObject *o) {
  168. switch (ttype(o)) {
  169. case LUA_TSTRING:
  170. return tsvalue(o)->tsv.marked;
  171. case LUA_TUSERDATA:
  172. return ismarkedudata(uvalue(o));
  173. case LUA_TTABLE:
  174. return ismarked(hvalue(o));
  175. case LUA_TFUNCTION:
  176. return ismarked(clvalue(o));
  177. default: /* number, nil */
  178. return 1;
  179. }
  180. }
  181. static void cleardeadnodes (Hash *h) {
  182. int i;
  183. for (i=0; i<h->size; i++) {
  184. Node *n = node(h, i);
  185. if (ttype(val(n)) == LUA_TNIL) continue; /* empty node */
  186. if (!hasmark(val(n)) || !hasmark(key(n))) {
  187. setnilvalue(val(n)); /* remove value */
  188. removekey(n);
  189. }
  190. }
  191. }
  192. static void cleartables (global_State *G) {
  193. Hash *h;
  194. for (h = G->roottable; h; h = h->next) {
  195. if (h->weakmode && ismarked(h))
  196. cleardeadnodes(h);
  197. }
  198. }
  199. static void collectproto (lua_State *L) {
  200. Proto **p = &G(L)->rootproto;
  201. Proto *curr;
  202. while ((curr = *p) != NULL) {
  203. if (curr->marked) {
  204. curr->marked = 0;
  205. p = &curr->next;
  206. }
  207. else {
  208. *p = curr->next;
  209. luaF_freeproto(L, curr);
  210. }
  211. }
  212. }
  213. static void collectclosure (lua_State *L, Closure **p) {
  214. Closure *curr;
  215. while ((curr = *p) != NULL) {
  216. if (ismarked(curr)) {
  217. curr->mark = curr; /* unmark */
  218. p = &curr->next;
  219. }
  220. else {
  221. *p = curr->next;
  222. luaF_freeclosure(L, curr);
  223. }
  224. }
  225. }
  226. static void collectclosures (lua_State *L) {
  227. lua_State *L1 = L;
  228. do { /* for each thread */
  229. collectclosure(L1, &L1->opencl);
  230. L1 = L1->next;
  231. } while (L1 != L);
  232. collectclosure(L, &G(L)->rootcl);
  233. }
  234. static void collecttable (lua_State *L) {
  235. Hash **p = &G(L)->roottable;
  236. Hash *curr;
  237. while ((curr = *p) != NULL) {
  238. if (ismarked(curr)) {
  239. curr->mark = curr; /* unmark */
  240. p = &curr->next;
  241. }
  242. else {
  243. *p = curr->next;
  244. luaH_free(L, curr);
  245. }
  246. }
  247. }
  248. static void collectupval (lua_State *L) {
  249. UpVal **v = &G(L)->rootupval;
  250. UpVal *curr;
  251. while ((curr = *v) != NULL) {
  252. if (curr->marked) {
  253. curr->marked = 0;
  254. v = &curr->next;
  255. }
  256. else {
  257. *v = curr->next;
  258. luaM_freelem(L, curr);
  259. }
  260. }
  261. }
  262. static void collectudata (lua_State *L, int keep) {
  263. Udata **p = &G(L)->rootudata;
  264. Udata *curr;
  265. while ((curr = *p) != NULL) {
  266. if (ismarkedudata(curr)) {
  267. switchudatamark(curr); /* unmark */
  268. p = &curr->uv.next;
  269. }
  270. else { /* collect */
  271. int tag = curr->uv.tag;
  272. *p = curr->uv.next;
  273. if (keep || /* must keep all of them (to close state)? */
  274. luaT_gettm(G(L), tag, TM_GC)) { /* or is there a GC tag method? */
  275. curr->uv.next = G(L)->TMtable[tag].collected; /* chain udata ... */
  276. G(L)->TMtable[tag].collected = curr; /* ... to call its TM later */
  277. }
  278. else /* no tag method; delete udata */
  279. luaM_free(L, curr, sizeudata(curr->uv.len));
  280. }
  281. }
  282. }
  283. static void collectstrings (lua_State *L, int all) {
  284. int i;
  285. for (i=0; i<G(L)->strt.size; i++) { /* for each list */
  286. TString **p = &G(L)->strt.hash[i];
  287. TString *curr;
  288. while ((curr = *p) != NULL) {
  289. if (curr->tsv.marked && !all) { /* preserve? */
  290. if (curr->tsv.marked < FIXMARK) /* does not change FIXMARKs */
  291. curr->tsv.marked = 0;
  292. p = &curr->tsv.nexthash;
  293. }
  294. else { /* collect */
  295. *p = curr->tsv.nexthash;
  296. G(L)->strt.nuse--;
  297. luaM_free(L, curr, sizestring(curr->tsv.len));
  298. }
  299. }
  300. }
  301. if (G(L)->strt.nuse < cast(ls_nstr, G(L)->strt.size/4) &&
  302. G(L)->strt.size > MINPOWER2)
  303. luaS_resize(L, G(L)->strt.size/2); /* table is too big */
  304. }
  305. #define MINBUFFER 256
  306. static void checkMbuffer (lua_State *L) {
  307. if (G(L)->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
  308. size_t newsize = G(L)->Mbuffsize/2; /* still larger than MINBUFFER */
  309. luaM_reallocvector(L, G(L)->Mbuffer, G(L)->Mbuffsize, newsize, l_char);
  310. G(L)->Mbuffsize = newsize;
  311. }
  312. }
  313. static void callgcTM (lua_State *L, const TObject *obj) {
  314. Closure *tm = luaT_gettmbyObj(G(L), obj, TM_GC);
  315. if (tm != NULL) {
  316. int oldah = L->allowhooks;
  317. StkId top = L->top;
  318. L->allowhooks = 0; /* stop debug hooks during GC tag methods */
  319. setclvalue(top, tm);
  320. setobj(top+1, obj);
  321. L->top += 2;
  322. luaD_call(L, top);
  323. L->top = top; /* restore top */
  324. L->allowhooks = oldah; /* restore hooks */
  325. }
  326. }
  327. static void callgcTMudata (lua_State *L) {
  328. int tag;
  329. luaD_checkstack(L, 3);
  330. for (tag=G(L)->ntag-1; tag>=0; tag--) { /* for each tag (in reverse order) */
  331. Udata *udata;
  332. while ((udata = G(L)->TMtable[tag].collected) != NULL) {
  333. G(L)->TMtable[tag].collected = udata->uv.next; /* remove it from list */
  334. udata->uv.next = G(L)->rootudata; /* resurect it */
  335. G(L)->rootudata = udata;
  336. setuvalue(L->top, udata);
  337. L->top++; /* keep it in stack to avoid being (recursively) collected */
  338. callgcTM(L, L->top-1);
  339. uvalue(L->top-1)->uv.tag = 0; /* default tag (udata is `finalized') */
  340. L->top--;
  341. }
  342. }
  343. }
  344. void luaC_callallgcTM (lua_State *L) {
  345. if (G(L)->rootudata) { /* avoid problems with incomplete states */
  346. collectudata(L, 1); /* collect all udata into tag lists */
  347. callgcTMudata(L); /* call their GC tag methods */
  348. }
  349. }
  350. void luaC_collect (lua_State *L, int all) {
  351. collectudata(L, 0);
  352. collectstrings(L, all);
  353. collecttable(L);
  354. collectproto(L);
  355. collectupval(L);
  356. collectclosures(L);
  357. }
  358. void luaC_collectgarbage (lua_State *L) {
  359. markall(L);
  360. cleartables(G(L));
  361. luaC_collect(L, 0);
  362. checkMbuffer(L);
  363. G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */
  364. callgcTMudata(L);
  365. callgcTM(L, &luaO_nilobject);
  366. }