lgc.c 8.8 KB

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