lgc.c 9.3 KB

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