2
0

lgc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. ** $Id: lgc.c,v 1.87 2001/02/02 16:32:00 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. /*
  17. ** optional "lock" for GC
  18. ** (when Lua calls GC tag methods it unlocks the regular lock)
  19. */
  20. #ifndef LUA_LOCKGC
  21. #define LUA_LOCKGC(L) {
  22. #endif
  23. #ifndef LUA_UNLOCKGC
  24. #define LUA_UNLOCKGC(L) }
  25. #endif
  26. typedef struct GCState {
  27. Hash *tmark; /* list of marked tables to be visited */
  28. Closure *cmark; /* list of marked closures to be visited */
  29. } GCState;
  30. static void markobject (GCState *st, TObject *o);
  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->sizekstr; i++)
  39. strmark(f->kstr[i]);
  40. for (i=0; i<f->sizekproto; i++)
  41. protomark(f->kproto[i]);
  42. for (i=0; i<f->sizelocvars; i++) /* mark local-variable names */
  43. strmark(f->locvars[i].varname);
  44. }
  45. }
  46. static void markclosure (GCState *st, Closure *cl) {
  47. if (!ismarked(cl)) {
  48. if (!cl->isC)
  49. protomark(cl->f.l);
  50. cl->mark = st->cmark; /* chain it for later traversal */
  51. st->cmark = cl;
  52. }
  53. }
  54. static void marktable (GCState *st, Hash *h) {
  55. if (!ismarked(h)) {
  56. h->mark = st->tmark; /* chain it in list of marked */
  57. st->tmark = h;
  58. }
  59. }
  60. static void markobject (GCState *st, TObject *o) {
  61. switch (ttype(o)) {
  62. case LUA_TUSERDATA: case LUA_TSTRING:
  63. strmark(tsvalue(o));
  64. break;
  65. case LUA_TMARK:
  66. markclosure(st, infovalue(o)->func);
  67. break;
  68. case LUA_TFUNCTION:
  69. markclosure(st, clvalue(o));
  70. break;
  71. case LUA_TTABLE: {
  72. marktable(st, hvalue(o));
  73. break;
  74. }
  75. default: break; /* numbers, etc */
  76. }
  77. }
  78. static void markstacks (lua_State *L, GCState *st) {
  79. lua_State *L1 = L;
  80. do { /* for each thread */
  81. StkId o, lim;
  82. marktable(st, L1->gt); /* mark table of globals */
  83. for (o=L1->stack; o<L1->top; o++)
  84. markobject(st, o);
  85. lim = (L1->stack_last - L1->top > MAXSTACK) ? L1->top+MAXSTACK
  86. : L1->stack_last;
  87. for (; o<=lim; o++) setnilvalue(o);
  88. lua_assert(L1->previous->next == L1 && L1->next->previous == L1);
  89. L1 = L1->next;
  90. } while (L1 != L);
  91. }
  92. static void marklock (global_State *G, GCState *st) {
  93. int i;
  94. for (i=0; i<G->nref; i++) {
  95. if (G->refArray[i].st == LOCK)
  96. markobject(st, &G->refArray[i].o);
  97. }
  98. }
  99. static void marktagmethods (global_State *G, GCState *st) {
  100. int t;
  101. for (t=0; t<G->ntag; t++) {
  102. struct TM *tm = &G->TMtable[t];
  103. int e;
  104. if (tm->name) strmark(tm->name);
  105. for (e=0; e<TM_N; e++) {
  106. Closure *cl = tm->method[e];
  107. if (cl) markclosure(st, cl);
  108. }
  109. }
  110. }
  111. static void traverseclosure (GCState *st, Closure *f) {
  112. int i;
  113. for (i=0; i<f->nupvalues; i++) /* mark its upvalues */
  114. markobject(st, &f->upvalue[i]);
  115. }
  116. static void traversetable (GCState *st, Hash *h) {
  117. int i;
  118. for (i=0; i<h->size; i++) {
  119. Node *n = node(h, i);
  120. if (ttype(val(n)) == LUA_TNIL) {
  121. if (ttype_key(n) != LUA_TNIL)
  122. n->key_value.ts = NULL; /* dead key; remove it */
  123. }
  124. else {
  125. lua_assert(ttype_key(n) != LUA_TNIL);
  126. if (ttype_key(n) != LUA_TNUMBER) {
  127. TObject o;
  128. setkey2obj(&o, n);
  129. markobject(st, &o);
  130. }
  131. markobject(st, &n->val);
  132. }
  133. }
  134. }
  135. static void markall (lua_State *L) {
  136. GCState st;
  137. st.cmark = NULL;
  138. st.tmark = NULL;
  139. marktagmethods(G(L), &st); /* mark tag methods */
  140. markstacks(L, &st); /* mark all stacks */
  141. marklock(G(L), &st); /* mark locked objects */
  142. marktable(&st, G(L)->type2tag);
  143. for (;;) { /* mark tables and closures */
  144. if (st.cmark) {
  145. Closure *f = st.cmark; /* get first closure from list */
  146. st.cmark = f->mark; /* remove it from list */
  147. traverseclosure(&st, f);
  148. }
  149. else if (st.tmark) {
  150. Hash *h = st.tmark; /* get first table from list */
  151. st.tmark = h->mark; /* remove it from list */
  152. traversetable(&st, h);
  153. }
  154. else break; /* nothing else to mark */
  155. }
  156. }
  157. static int hasmark (const TObject *o) {
  158. /* valid only for locked objects */
  159. switch (ttype(o)) {
  160. case LUA_TSTRING: case LUA_TUSERDATA:
  161. return tsvalue(o)->marked;
  162. case LUA_TTABLE:
  163. return ismarked(hvalue(o));
  164. case LUA_TFUNCTION:
  165. return ismarked(clvalue(o));
  166. default: /* number */
  167. return 1;
  168. }
  169. }
  170. /* macro for internal debugging; check if a link of free refs is valid */
  171. #define VALIDLINK(L, st,n) (NONEXT <= (st) && (st) < (n))
  172. static void invalidaterefs (global_State *G) {
  173. int n = G->nref;
  174. int i;
  175. for (i=0; i<n; i++) {
  176. struct Ref *r = &G->refArray[i];
  177. if (r->st == HOLD && !hasmark(&r->o))
  178. r->st = COLLECTED;
  179. lua_assert((r->st == LOCK && hasmark(&r->o)) ||
  180. (r->st == HOLD && hasmark(&r->o)) ||
  181. r->st == COLLECTED ||
  182. r->st == NONEXT ||
  183. (r->st < n && VALIDLINK(L, G->refArray[r->st].st, n)));
  184. }
  185. lua_assert(VALIDLINK(L, G->refFree, n));
  186. }
  187. static void collectproto (lua_State *L) {
  188. Proto **p = &G(L)->rootproto;
  189. Proto *next;
  190. while ((next = *p) != NULL) {
  191. if (next->marked) {
  192. next->marked = 0;
  193. p = &next->next;
  194. }
  195. else {
  196. *p = next->next;
  197. luaF_freeproto(L, next);
  198. }
  199. }
  200. }
  201. static void collectclosure (lua_State *L) {
  202. Closure **p = &G(L)->rootcl;
  203. Closure *next;
  204. while ((next = *p) != NULL) {
  205. if (ismarked(next)) {
  206. next->mark = next; /* unmark */
  207. p = &next->next;
  208. }
  209. else {
  210. *p = next->next;
  211. luaF_freeclosure(L, next);
  212. }
  213. }
  214. }
  215. static void collecttable (lua_State *L) {
  216. Hash **p = &G(L)->roottable;
  217. Hash *next;
  218. while ((next = *p) != NULL) {
  219. if (ismarked(next)) {
  220. next->mark = next; /* unmark */
  221. p = &next->next;
  222. }
  223. else {
  224. *p = next->next;
  225. luaH_free(L, next);
  226. }
  227. }
  228. }
  229. static void checktab (lua_State *L, stringtable *tb) {
  230. if (tb->nuse < (luint32)(tb->size/4) && tb->size > MINPOWER2)
  231. luaS_resize(L, tb, tb->size/2); /* table is too big */
  232. }
  233. static void collectstrings (lua_State *L, int all) {
  234. int i;
  235. for (i=0; i<G(L)->strt.size; i++) { /* for each list */
  236. TString **p = &G(L)->strt.hash[i];
  237. TString *next;
  238. while ((next = *p) != NULL) {
  239. if (next->marked && !all) { /* preserve? */
  240. if (next->marked < FIXMARK) /* does not change FIXMARKs */
  241. next->marked = 0;
  242. p = &next->nexthash;
  243. }
  244. else { /* collect */
  245. *p = next->nexthash;
  246. G(L)->strt.nuse--;
  247. luaM_free(L, next, sizestring(next->len));
  248. }
  249. }
  250. }
  251. checktab(L, &G(L)->strt);
  252. }
  253. static void collectudata (lua_State *L, int all) {
  254. int i;
  255. for (i=0; i<G(L)->udt.size; i++) { /* for each list */
  256. TString **p = &G(L)->udt.hash[i];
  257. TString *next;
  258. while ((next = *p) != NULL) {
  259. lua_assert(next->marked <= 1);
  260. if (next->marked && !all) { /* preserve? */
  261. next->marked = 0;
  262. p = &next->nexthash;
  263. }
  264. else { /* collect */
  265. int tag = next->u.d.tag;
  266. *p = next->nexthash;
  267. next->nexthash = G(L)->TMtable[tag].collected; /* chain udata */
  268. G(L)->TMtable[tag].collected = next;
  269. G(L)->udt.nuse--;
  270. }
  271. }
  272. }
  273. checktab(L, &G(L)->udt);
  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, 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. TString *udata;
  301. while ((udata = G(L)->TMtable[tag].collected) != NULL) {
  302. TObject obj;
  303. G(L)->TMtable[tag].collected = udata->nexthash; /* 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, all);
  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. invalidaterefs(G(L)); /* check unlocked references */
  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. }
  328. void luaC_checkGC (lua_State *L) {
  329. if (G(L)->nblocks >= G(L)->GCthreshold)
  330. luaC_collectgarbage(L);
  331. }