lgc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. ** $Id: lgc.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  3. ** Garbage Collector
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #include "lua.h"
  8. #include "ldebug.h"
  9. #include "ldo.h"
  10. #include "lfunc.h"
  11. #include "lgc.h"
  12. #include "lmem.h"
  13. #include "lobject.h"
  14. #include "lstate.h"
  15. #include "lstring.h"
  16. #include "ltable.h"
  17. #include "ltm.h"
  18. typedef struct GCState {
  19. Table *tmark; /* list of marked tables to be visited */
  20. Table *toclear; /* list of visited weak tables (to be cleared after GC) */
  21. lua_State *L;
  22. } GCState;
  23. /* mark a string; marks larger than 1 cannot be changed */
  24. #define strmark(s) {if ((s)->tsv.marked == 0) (s)->tsv.marked = 1;}
  25. /* mark tricks for userdata */
  26. #define isudmarked(u) (u->uv.len & 1)
  27. #define markud(u) (u->uv.len |= 1)
  28. #define unmarkud(u) (u->uv.len--)
  29. /* mark tricks for upvalues (assume that open upvalues are always marked) */
  30. #define isupvalmarked(uv) ((uv)->v != &(uv)->value)
  31. static void markobject (GCState *st, TObject *o);
  32. static void protomark (Proto *f) {
  33. if (!f->marked) {
  34. int i;
  35. f->marked = 1;
  36. strmark(f->source);
  37. for (i=0; i<f->sizek; i++) {
  38. if (ttype(f->k+i) == LUA_TSTRING)
  39. strmark(tsvalue(f->k+i));
  40. }
  41. for (i=0; i<f->sizep; i++)
  42. protomark(f->p[i]);
  43. for (i=0; i<f->sizelocvars; i++) /* mark local-variable names */
  44. strmark(f->locvars[i].varname);
  45. }
  46. lua_assert(luaG_checkcode(f));
  47. }
  48. static void markclosure (GCState *st, Closure *cl) {
  49. if (!cl->c.marked) {
  50. cl->c.marked = 1;
  51. if (cl->c.isC) {
  52. int i;
  53. for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
  54. markobject(st, &cl->c.upvalue[i]);
  55. }
  56. else {
  57. int i;
  58. lua_assert(cl->l.nupvalues == cl->l.p->nupvalues);
  59. protomark(cl->l.p);
  60. for (i=0; i<cl->l.nupvalues; i++) { /* mark its upvalues */
  61. UpVal *u = cl->l.upvals[i];
  62. if (!isupvalmarked(u)) {
  63. markobject(st, &u->value);
  64. u->v = NULL; /* mark it! */
  65. }
  66. }
  67. }
  68. }
  69. }
  70. static void marktable (GCState *st, Table *h) {
  71. if (!ismarked(h)) {
  72. h->mark = st->tmark; /* chain it for later traversal */
  73. st->tmark = h;
  74. }
  75. }
  76. static void markobject (GCState *st, TObject *o) {
  77. switch (ttype(o)) {
  78. case LUA_TSTRING:
  79. strmark(tsvalue(o));
  80. break;
  81. case LUA_TUSERDATA:
  82. if (!isudmarked(uvalue(o)))
  83. markud(uvalue(o));
  84. break;
  85. case LUA_TFUNCTION:
  86. markclosure(st, clvalue(o));
  87. break;
  88. case LUA_TTABLE: {
  89. marktable(st, hvalue(o));
  90. break;
  91. }
  92. default: {
  93. lua_assert(ttype(o) == LUA_TNIL ||
  94. ttype(o) == LUA_TNUMBER ||
  95. ttype(o) == LUA_TBOOLEAN);
  96. break;
  97. }
  98. }
  99. }
  100. static void markstacks (GCState *st) {
  101. lua_State *L1 = st->L;
  102. do { /* for each thread */
  103. StkId o, lim;
  104. if (L1->base_ci == NULL) { /* incomplete state? */
  105. lua_assert(L1 != st->L);
  106. L1 = L1->next;
  107. luaE_closethread(st->L, L1->previous); /* collect it */
  108. continue;
  109. }
  110. for (o=L1->stack; o<L1->top; o++)
  111. markobject(st, o);
  112. lim = (L1->stack_last - L1->ci->base > MAXSTACK) ? L1->ci->base+MAXSTACK
  113. : L1->stack_last;
  114. for (; o<=lim; o++) setnilvalue(o);
  115. lua_assert(L1->previous->next == L1 && L1->next->previous == L1);
  116. L1 = L1->next;
  117. } while (L1 != st->L);
  118. }
  119. static void markudet (GCState *st) {
  120. Udata *u;
  121. for (u = G(st->L)->rootudata; u; u = u->uv.next)
  122. marktable(st, u->uv.eventtable);
  123. for (u = G(st->L)->tmudata; u; u = u->uv.next)
  124. marktable(st, u->uv.eventtable);
  125. }
  126. static void removekey (Node *n) {
  127. lua_assert(ttype(val(n)) == LUA_TNIL);
  128. if (ttype(key(n)) != LUA_TNIL && ttype(key(n)) != LUA_TNUMBER)
  129. setttype(key(n), LUA_TNONE); /* dead key; remove it */
  130. }
  131. static void traversetable (GCState *st, Table *h) {
  132. int i;
  133. const TObject *mode;
  134. int weakkey = 0;
  135. int weakvalue = 0;
  136. marktable(st, h->eventtable);
  137. mode = fasttm(st->L, h->eventtable, TM_WEAKMODE);
  138. if (mode) { /* weak table? must be cleared after GC... */
  139. h->mark = st->toclear; /* put in the appropriate list */
  140. st->toclear = h;
  141. if (ttype(mode) == LUA_TSTRING) {
  142. weakkey = (strchr(svalue(mode), 'k') != NULL);
  143. weakvalue = (strchr(svalue(mode), 'v') != NULL);
  144. }
  145. }
  146. if (!weakvalue) {
  147. i = sizearray(h);
  148. while (i--)
  149. markobject(st, &h->array[i]);
  150. }
  151. i = sizenode(h);
  152. while (i--) {
  153. Node *n = node(h, i);
  154. if (ttype(val(n)) != LUA_TNIL) {
  155. lua_assert(ttype(key(n)) != LUA_TNIL);
  156. if (!weakkey) markobject(st, key(n));
  157. if (!weakvalue) markobject(st, val(n));
  158. }
  159. }
  160. }
  161. static void markall (GCState *st) {
  162. lua_assert(hvalue(defaultet(st->L))->flags == cast(unsigned short, ~0));
  163. /* table is unchanged */
  164. markstacks(st); /* mark all stacks */
  165. markudet(st); /* mark userdata's event tables */
  166. while (st->tmark) { /* traverse marked tables */
  167. Table *h = st->tmark; /* get first table from list */
  168. st->tmark = h->mark; /* remove it from list */
  169. traversetable(st, h);
  170. }
  171. }
  172. static int hasmark (const TObject *o) {
  173. switch (ttype(o)) {
  174. case LUA_TSTRING:
  175. return tsvalue(o)->tsv.marked;
  176. case LUA_TUSERDATA:
  177. return isudmarked(uvalue(o));
  178. case LUA_TTABLE:
  179. return ismarked(hvalue(o));
  180. case LUA_TFUNCTION:
  181. return clvalue(o)->c.marked;
  182. default: /* number, nil, boolean */
  183. return 1;
  184. }
  185. }
  186. /*
  187. ** clear (set to nil) keys and values from weaktables that were collected
  188. */
  189. static void cleartables (Table *h) {
  190. for (; h; h = h->mark) {
  191. int i;
  192. i = sizearray(h);
  193. while (i--) {
  194. TObject *o = &h->array[i];
  195. if (!hasmark(o))
  196. setnilvalue(o); /* remove value */
  197. }
  198. i = sizenode(h);
  199. while (i--) {
  200. Node *n = node(h, i);
  201. if (!hasmark(val(n)) || !hasmark(key(n))) {
  202. setnilvalue(val(n)); /* remove value ... */
  203. removekey(n); /* ... and key */
  204. }
  205. }
  206. }
  207. }
  208. static void collectproto (lua_State *L) {
  209. Proto **p = &G(L)->rootproto;
  210. Proto *curr;
  211. while ((curr = *p) != NULL) {
  212. if (curr->marked) {
  213. curr->marked = 0;
  214. p = &curr->next;
  215. }
  216. else {
  217. *p = curr->next;
  218. luaF_freeproto(L, curr);
  219. }
  220. }
  221. }
  222. static void collectclosures (lua_State *L) {
  223. Closure **p = &G(L)->rootcl;
  224. Closure *curr;
  225. while ((curr = *p) != NULL) {
  226. if (curr->c.marked) {
  227. curr->c.marked = 0;
  228. p = &curr->c.next;
  229. }
  230. else {
  231. *p = curr->c.next;
  232. luaF_freeclosure(L, curr);
  233. }
  234. }
  235. }
  236. static void collectupval (lua_State *L) {
  237. UpVal **v = &G(L)->rootupval;
  238. UpVal *curr;
  239. while ((curr = *v) != NULL) {
  240. if (isupvalmarked(curr)) {
  241. lua_assert(curr->v == NULL);
  242. curr->v = &curr->value; /* unmark */
  243. v = &curr->next; /* next */
  244. }
  245. else {
  246. *v = curr->next; /* next */
  247. luaM_freelem(L, curr);
  248. }
  249. }
  250. }
  251. static void collecttable (lua_State *L) {
  252. Table **p = &G(L)->roottable;
  253. Table *curr;
  254. while ((curr = *p) != NULL) {
  255. if (ismarked(curr)) {
  256. curr->mark = curr; /* unmark */
  257. p = &curr->next;
  258. }
  259. else {
  260. *p = curr->next;
  261. luaH_free(L, curr);
  262. }
  263. }
  264. }
  265. static void collectudata (lua_State *L) {
  266. Udata **p = &G(L)->rootudata;
  267. Udata *curr;
  268. Udata *collected = NULL; /* to collect udata with gc event */
  269. Udata **lastcollected = &collected;
  270. while ((curr = *p) != NULL) {
  271. if (isudmarked(curr)) {
  272. unmarkud(curr);
  273. p = &curr->uv.next;
  274. }
  275. else {
  276. *p = curr->uv.next;
  277. if (fasttm(L, curr->uv.eventtable, TM_GC) != NULL) { /* gc event? */
  278. curr->uv.next = NULL; /* link `curr' at the end of `collected' list */
  279. *lastcollected = curr;
  280. lastcollected = &curr->uv.next;
  281. }
  282. else /* no gc event; delete udata */
  283. luaM_free(L, curr, sizeudata(curr->uv.len));
  284. }
  285. }
  286. /* insert collected udata with gc event into `tmudata' list */
  287. *lastcollected = G(L)->tmudata;
  288. G(L)->tmudata = collected;
  289. }
  290. static void collectstrings (lua_State *L, int all) {
  291. int i;
  292. for (i=0; i<G(L)->strt.size; i++) { /* for each list */
  293. TString **p = &G(L)->strt.hash[i];
  294. TString *curr;
  295. while ((curr = *p) != NULL) {
  296. if (curr->tsv.marked && !all) { /* preserve? */
  297. if (curr->tsv.marked < FIXMARK) /* does not change FIXMARKs */
  298. curr->tsv.marked = 0;
  299. p = &curr->tsv.nexthash;
  300. }
  301. else { /* collect */
  302. *p = curr->tsv.nexthash;
  303. G(L)->strt.nuse--;
  304. luaM_free(L, curr, sizestring(curr->tsv.len));
  305. }
  306. }
  307. }
  308. if (G(L)->strt.nuse < cast(ls_nstr, G(L)->strt.size/4) &&
  309. G(L)->strt.size > 4)
  310. luaS_resize(L, G(L)->strt.size/2); /* table is too big */
  311. }
  312. #define MINBUFFER 256
  313. static void checkMbuffer (lua_State *L) {
  314. if (G(L)->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
  315. size_t newsize = G(L)->Mbuffsize/2; /* still larger than MINBUFFER */
  316. luaM_reallocvector(L, G(L)->Mbuffer, G(L)->Mbuffsize, newsize, char);
  317. G(L)->Mbuffsize = newsize;
  318. }
  319. }
  320. static void do1gcTM (lua_State *L, Udata *udata) {
  321. const TObject *tm = fasttm(L, udata->uv.eventtable, TM_GC);
  322. if (tm != NULL) {
  323. StkId top = L->top;
  324. setobj(top, tm);
  325. setuvalue(top+1, udata);
  326. L->top += 2;
  327. luaD_call(L, top, 0);
  328. }
  329. }
  330. static void unprotectedcallGCTM (lua_State *L, void *pu) {
  331. luaD_checkstack(L, 3);
  332. L->top++; /* reserve space to keep udata while runs its gc method */
  333. while (G(L)->tmudata != NULL) {
  334. Udata *udata = G(L)->tmudata;
  335. G(L)->tmudata = udata->uv.next; /* remove udata from list */
  336. *(Udata **)pu = udata; /* keep a reference to it (in case of errors) */
  337. setuvalue(L->top - 1, udata); /* and on stack (in case of recursive GC) */
  338. udata->uv.next = G(L)->rootudata; /* resurect it */
  339. G(L)->rootudata = udata;
  340. do1gcTM(L, udata);
  341. /* mark udata as finalized (default event table) */
  342. uvalue(L->top-1)->uv.eventtable = hvalue(defaultet(L));
  343. }
  344. L->top--;
  345. }
  346. static void callGCTM (lua_State *L) {
  347. int oldah = L->allowhooks;
  348. L->allowhooks = 0; /* stop debug hooks during GC tag methods */
  349. while (G(L)->tmudata != NULL) {
  350. Udata *udata;
  351. if (luaD_runprotected(L, unprotectedcallGCTM, &udata) != 0) {
  352. /* `udata' generated an error during its gc */
  353. /* mark it as finalized (default event table) */
  354. udata->uv.eventtable = hvalue(defaultet(L));
  355. }
  356. }
  357. L->allowhooks = oldah; /* restore hooks */
  358. }
  359. void luaC_callallgcTM (lua_State *L) {
  360. lua_assert(G(L)->tmudata == NULL);
  361. G(L)->tmudata = G(L)->rootudata; /* all udata must be collected */
  362. G(L)->rootudata = NULL;
  363. callGCTM(L); /* call their GC tag methods */
  364. }
  365. void luaC_collect (lua_State *L, int all) {
  366. collectudata(L);
  367. collectstrings(L, all);
  368. collecttable(L);
  369. collectproto(L);
  370. collectupval(L);
  371. collectclosures(L);
  372. }
  373. void luaC_collectgarbage (lua_State *L) {
  374. GCState st;
  375. st.L = L;
  376. st.tmark = NULL;
  377. st.toclear = NULL;
  378. markall(&st);
  379. cleartables(st.toclear);
  380. luaC_collect(L, 0);
  381. checkMbuffer(L);
  382. G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */
  383. callGCTM(L);
  384. }