lgc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. ** $Id: lgc.c,v 1.61 2000/08/08 20:42:07 roberto Exp roberto $
  3. ** Garbage Collector
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include "lua.h"
  7. #include "ldo.h"
  8. #include "lfunc.h"
  9. #include "lgc.h"
  10. #include "lmem.h"
  11. #include "lobject.h"
  12. #include "lref.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. static int markobject (GCState *st, TObject *o);
  22. /* mark a string; marks larger than 1 cannot be changed */
  23. #define strmark(s) {if ((s)->marked == 0) (s)->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->nkstr; i++)
  30. strmark(f->kstr[i]);
  31. for (i=0; i<f->nkproto; i++)
  32. protomark(f->kproto[i]);
  33. for (i=0; f->locvars[i].pc != -1; i++) /* mark local-variable names */
  34. if (f->locvars[i].varname)
  35. strmark(f->locvars[i].varname);
  36. }
  37. }
  38. static void markstack (lua_State *L, GCState *st) {
  39. StkId o;
  40. for (o=L->stack; o<L->top; o++)
  41. markobject(st, o);
  42. }
  43. static void marklock (lua_State *L, GCState *st) {
  44. int i;
  45. for (i=0; i<L->refSize; i++) {
  46. if (L->refArray[i].st == LOCK)
  47. markobject(st, &L->refArray[i].o);
  48. }
  49. }
  50. static void marktagmethods (lua_State *L, GCState *st) {
  51. int e;
  52. for (e=0; e<IM_N; e++) {
  53. int t;
  54. for (t=0; t<=L->last_tag; t++)
  55. markobject(st, luaT_getim(L, t,e));
  56. }
  57. }
  58. static int markobject (GCState *st, TObject *o) {
  59. switch (ttype(o)) {
  60. case TAG_USERDATA: case TAG_STRING:
  61. strmark(tsvalue(o));
  62. break;
  63. case TAG_TABLE: {
  64. if (!ismarked(hvalue(o))) {
  65. hvalue(o)->mark = st->tmark; /* chain it in list of marked */
  66. st->tmark = hvalue(o);
  67. }
  68. break;
  69. }
  70. case TAG_LMARK: {
  71. Closure *cl = infovalue(o)->func;
  72. if (!ismarked(cl)) {
  73. protomark(cl->f.l);
  74. cl->mark = st->cmark; /* chain it for later traversal */
  75. st->cmark = cl;
  76. }
  77. break;
  78. }
  79. case TAG_LCLOSURE:
  80. protomark(clvalue(o)->f.l);
  81. /* go through */
  82. case TAG_CCLOSURE: case TAG_CMARK:
  83. if (!ismarked(clvalue(o))) {
  84. clvalue(o)->mark = st->cmark; /* chain it for later traversal */
  85. st->cmark = clvalue(o);
  86. }
  87. break;
  88. default: break; /* numbers, etc */
  89. }
  90. return 0;
  91. }
  92. static void markall (lua_State *L) {
  93. GCState st;
  94. st.cmark = NULL;
  95. st.tmark = L->gt; /* put table of globals in mark list */
  96. L->gt->mark = NULL;
  97. marktagmethods(L, &st); /* mark tag methods */
  98. markstack(L, &st); /* mark stack objects */
  99. marklock(L, &st); /* mark locked objects */
  100. for (;;) { /* mark tables and closures */
  101. if (st.cmark) {
  102. int i;
  103. Closure *f = st.cmark; /* get first closure from list */
  104. st.cmark = f->mark; /* remove it from list */
  105. for (i=0; i<f->nupvalues; i++) /* mark its upvalues */
  106. markobject(&st, &f->upvalue[i]);
  107. }
  108. else if (st.tmark) {
  109. int i;
  110. Hash *h = st.tmark; /* get first table from list */
  111. st.tmark = h->mark; /* remove it from list */
  112. for (i=0; i<h->size; i++) {
  113. Node *n = node(h, i);
  114. if (ttype(key(n)) != TAG_NIL) {
  115. if (ttype(val(n)) == TAG_NIL)
  116. luaH_remove(h, key(n)); /* dead element; try to remove it */
  117. markobject(&st, &n->key);
  118. markobject(&st, &n->val);
  119. }
  120. }
  121. }
  122. else break; /* nothing else to mark */
  123. }
  124. }
  125. static void collectproto (lua_State *L) {
  126. Proto **p = &L->rootproto;
  127. Proto *next;
  128. while ((next = *p) != NULL) {
  129. if (next->marked) {
  130. next->marked = 0;
  131. p = &next->next;
  132. }
  133. else {
  134. *p = next->next;
  135. luaF_freeproto(L, next);
  136. }
  137. }
  138. }
  139. static void collectclosure (lua_State *L) {
  140. Closure **p = &L->rootcl;
  141. Closure *next;
  142. while ((next = *p) != NULL) {
  143. if (ismarked(next)) {
  144. next->mark = next; /* unmark */
  145. p = &next->next;
  146. }
  147. else {
  148. *p = next->next;
  149. luaF_freeclosure(L, next);
  150. }
  151. }
  152. }
  153. static void collecttable (lua_State *L) {
  154. Hash **p = &L->roottable;
  155. Hash *next;
  156. while ((next = *p) != NULL) {
  157. if (ismarked(next)) {
  158. next->mark = next; /* unmark */
  159. p = &next->next;
  160. }
  161. else {
  162. *p = next->next;
  163. luaH_free(L, next);
  164. }
  165. }
  166. }
  167. static void checktab (lua_State *L, stringtable *tb) {
  168. if (tb->nuse < (lint32)(tb->size/4) && tb->size > 10)
  169. luaS_resize(L, tb, tb->size/2); /* table is too big */
  170. }
  171. /*
  172. ** collect all elements with `marked' <= `limit'.
  173. ** with limit=0, that means all unmarked elements;
  174. ** with limit=MAX_INT, that means all elements.
  175. */
  176. static void collectstringtab (lua_State *L, int limit) {
  177. int i;
  178. for (i=0; i<L->strt.size; i++) { /* for each list */
  179. TString **p = &L->strt.hash[i];
  180. TString *next;
  181. while ((next = *p) != NULL) {
  182. if (next->marked > limit) { /* preserve? */
  183. if (next->marked < FIXMARK) /* does not change FIXMARKs */
  184. next->marked = 0;
  185. p = &next->nexthash;
  186. }
  187. else { /* collect */
  188. *p = next->nexthash;
  189. L->strt.nuse--;
  190. L->nblocks -= gcsizestring(L, next->u.s.len);
  191. luaM_free(L, next);
  192. }
  193. }
  194. }
  195. checktab(L, &L->strt);
  196. }
  197. static void collectudatatab (lua_State *L, int all) {
  198. int i;
  199. for (i=0; i<L->udt.size; i++) { /* for each list */
  200. TString **p = &L->udt.hash[i];
  201. TString *next;
  202. while ((next = *p) != NULL) {
  203. LUA_ASSERT(next->marked <= 1, "udata cannot be fixed");
  204. if (next->marked > all) { /* preserve? */
  205. next->marked = 0;
  206. p = &next->nexthash;
  207. }
  208. else { /* collect */
  209. int tag = next->u.d.tag;
  210. if (tag > L->last_tag) tag = TAG_USERDATA;
  211. *p = next->nexthash;
  212. next->nexthash = L->IMtable[tag].collected; /* chain udata */
  213. L->IMtable[tag].collected = next;
  214. L->nblocks -= gcsizeudata;
  215. L->udt.nuse--;
  216. }
  217. }
  218. }
  219. checktab(L, &L->udt);
  220. }
  221. static void callgcTM (lua_State *L, const TObject *o) {
  222. const TObject *im = luaT_getimbyObj(L, o, IM_GC);
  223. if (ttype(im) != TAG_NIL) {
  224. luaD_checkstack(L, 2);
  225. *(L->top) = *im;
  226. *(L->top+1) = *o;
  227. L->top += 2;
  228. luaD_call(L, L->top-2, 0);
  229. }
  230. }
  231. static void callgcTMudata (lua_State *L) {
  232. int tag;
  233. TObject o;
  234. ttype(&o) = TAG_USERDATA;
  235. for (tag=L->last_tag; tag>=0; tag--) {
  236. TString *udata = L->IMtable[tag].collected;
  237. L->IMtable[tag].collected = NULL;
  238. while (udata) {
  239. TString *next = udata->nexthash;
  240. tsvalue(&o) = udata;
  241. callgcTM(L, &o);
  242. luaM_free(L, udata);
  243. udata = next;
  244. }
  245. }
  246. }
  247. void luaC_collect (lua_State *L, int all) {
  248. int oldah = L->allowhooks;
  249. L->allowhooks = 0; /* stop debug hooks during GC */
  250. L->GCthreshold *= 4; /* to avoid GC during GC */
  251. collectudatatab(L, all);
  252. callgcTMudata(L);
  253. collectstringtab(L, all?MAX_INT:0);
  254. collecttable(L);
  255. collectproto(L);
  256. collectclosure(L);
  257. L->allowhooks = oldah; /* restore hooks */
  258. }
  259. long lua_collectgarbage (lua_State *L, long limit) {
  260. unsigned long recovered = L->nblocks; /* to subtract `nblocks' after gc */
  261. markall(L);
  262. luaR_invalidaterefs(L);
  263. luaC_collect(L, 0);
  264. recovered = recovered - L->nblocks;
  265. L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
  266. if (L->Mbuffsize > L->Mbuffnext*4) { /* is buffer too big? */
  267. L->Mbuffsize /= 2; /* still larger than Mbuffnext*2 */
  268. luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
  269. }
  270. callgcTM(L, &luaO_nilobject);
  271. return recovered;
  272. }
  273. void luaC_checkGC (lua_State *L) {
  274. if (L->nblocks >= L->GCthreshold)
  275. lua_collectgarbage(L, 0);
  276. }