lgc.c 8.6 KB

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