lgc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. ** $Id: lgc.c,v 1.42 2000/03/10 18:37:44 roberto Exp roberto $
  3. ** Garbage Collector
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define LUA_REENTRANT
  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. #include "lua.h"
  18. static int markobject (lua_State *L, TObject *o);
  19. /* mark a string; marks larger than 1 cannot be changed */
  20. #define strmark(L, s) {if ((s)->marked == 0) (s)->marked = 1;}
  21. static void protomark (lua_State *L, Proto *f) {
  22. if (!f->marked) {
  23. int i;
  24. f->marked = 1;
  25. strmark(L, f->source);
  26. for (i=f->nkstr-1; i>=0; i--)
  27. strmark(L, f->kstr[i]);
  28. for (i=f->nkproto-1; i>=0; i--)
  29. protomark(L, f->kproto[i]);
  30. }
  31. }
  32. static void closuremark (lua_State *L, Closure *f) {
  33. if (!f->marked) {
  34. int i;
  35. f->marked = 1;
  36. for (i=f->nelems; i>=0; i--)
  37. markobject(L, &f->consts[i]);
  38. }
  39. }
  40. static void hashmark (lua_State *L, Hash *h) {
  41. if (!h->marked) {
  42. int i;
  43. h->marked = 1;
  44. for (i=h->size-1; i>=0; i--) {
  45. Node *n = node(h,i);
  46. if (ttype(key(n)) != TAG_NIL) {
  47. markobject(L, &n->key);
  48. markobject(L, &n->val);
  49. }
  50. }
  51. }
  52. }
  53. static void travglobal (lua_State *L) {
  54. GlobalVar *gv;
  55. for (gv=L->rootglobal; gv; gv=gv->next) {
  56. LUA_ASSERT(L, gv->name->u.s.gv == gv, "inconsistent global name");
  57. if (gv->value.ttype != TAG_NIL) {
  58. strmark(L, gv->name); /* cannot collect non nil global variables */
  59. markobject(L, &gv->value);
  60. }
  61. }
  62. }
  63. static void travstack (lua_State *L) {
  64. int i;
  65. for (i = (L->top-1)-L->stack; i>=0; i--)
  66. markobject(L, L->stack+i);
  67. }
  68. static void travlock (lua_State *L) {
  69. int i;
  70. for (i=0; i<L->refSize; i++) {
  71. if (L->refArray[i].st == LOCK)
  72. markobject(L, &L->refArray[i].o);
  73. }
  74. }
  75. static int markobject (lua_State *L, TObject *o) {
  76. switch (ttype(o)) {
  77. case TAG_USERDATA: case TAG_STRING:
  78. strmark(L, tsvalue(o));
  79. break;
  80. case TAG_ARRAY:
  81. hashmark(L, avalue(o));
  82. break;
  83. case TAG_LCLOSURE: case TAG_LCLMARK:
  84. case TAG_CCLOSURE: case TAG_CCLMARK:
  85. closuremark(L, o->value.cl);
  86. break;
  87. case TAG_LPROTO: case TAG_LMARK:
  88. protomark(L, o->value.tf);
  89. break;
  90. default: break; /* numbers, cprotos, etc */
  91. }
  92. return 0;
  93. }
  94. static void collectproto (lua_State *L) {
  95. Proto **p = &L->rootproto;
  96. Proto *next;
  97. while ((next = *p) != NULL) {
  98. if (next->marked) {
  99. next->marked = 0;
  100. p = &next->next;
  101. }
  102. else {
  103. *p = next->next;
  104. luaF_freeproto(L, next);
  105. }
  106. }
  107. }
  108. static void collectclosure (lua_State *L) {
  109. Closure **p = &L->rootcl;
  110. Closure *next;
  111. while ((next = *p) != NULL) {
  112. if (next->marked) {
  113. next->marked = 0;
  114. p = &next->next;
  115. }
  116. else {
  117. *p = next->next;
  118. luaF_freeclosure(L, next);
  119. }
  120. }
  121. }
  122. static void collecttable (lua_State *L) {
  123. Hash **p = &L->roottable;
  124. Hash *next;
  125. while ((next = *p) != NULL) {
  126. if (next->marked) {
  127. next->marked = 0;
  128. p = &next->next;
  129. }
  130. else {
  131. *p = next->next;
  132. luaH_free(L, next);
  133. }
  134. }
  135. }
  136. /*
  137. ** remove from the global list globals whose names will be collected
  138. ** (the global itself is freed when its name is freed)
  139. */
  140. static void clear_global_list (lua_State *L, int limit) {
  141. GlobalVar **p = &L->rootglobal;
  142. GlobalVar *next;
  143. while ((next = *p) != NULL) {
  144. if (next->name->marked >= limit) p = &next->next;
  145. else *p = next->next;
  146. }
  147. }
  148. /*
  149. ** collect all elements with `marked' < `limit'.
  150. ** with limit=1, that means all unmarked elements;
  151. ** with limit=MAX_INT, that means all elements.
  152. */
  153. static void collectstring (lua_State *L, int limit) {
  154. TObject o; /* to call userdata `gc' tag method */
  155. int i;
  156. ttype(&o) = TAG_USERDATA;
  157. clear_global_list(L, limit);
  158. for (i=0; i<NUM_HASHS; i++) { /* for each hash table */
  159. stringtable *tb = &L->string_root[i];
  160. int j;
  161. for (j=0; j<tb->size; j++) { /* for each list */
  162. TString **p = &tb->hash[j];
  163. TString *next;
  164. while ((next = *p) != NULL) {
  165. if (next->marked >= limit) {
  166. if (next->marked < FIXMARK) /* does not change FIXMARKs */
  167. next->marked = 0;
  168. p = &next->nexthash;
  169. }
  170. else { /* collect */
  171. if (next->constindex == -1) { /* is userdata? */
  172. tsvalue(&o) = next;
  173. luaD_gcIM(L, &o);
  174. }
  175. *p = next->nexthash;
  176. luaS_free(L, next);
  177. tb->nuse--;
  178. }
  179. }
  180. }
  181. if ((tb->nuse+1)*6 < tb->size)
  182. luaS_resize(L, tb, tb->size/2); /* table is too big */
  183. }
  184. }
  185. static void markall (lua_State *L) {
  186. travstack(L); /* mark stack objects */
  187. travglobal(L); /* mark global variable values and names */
  188. travlock(L); /* mark locked objects */
  189. luaT_travtagmethods(L, markobject); /* mark tag methods */
  190. }
  191. void luaC_collect (lua_State *L, int all) {
  192. int oldah = L->allowhooks;
  193. L->allowhooks = 0; /* stop debug hooks during GC */
  194. L->GCthreshold *= 4; /* to avoid GC during GC */
  195. collecttable(L);
  196. collectstring(L, all?MAX_INT:1);
  197. collectproto(L);
  198. collectclosure(L);
  199. L->allowhooks = oldah; /* restore hooks */
  200. }
  201. long lua_collectgarbage (lua_State *L, long limit) {
  202. unsigned long recovered = L->nblocks; /* to subtract `nblocks' after gc */
  203. markall(L);
  204. luaR_invalidaterefs(L);
  205. luaC_collect(L, 0);
  206. luaD_gcIM(L, &luaO_nilobject); /* GC tag method for nil (signal end of GC) */
  207. recovered = recovered - L->nblocks;
  208. L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
  209. if (L->Mbuffsize > L->Mbuffnext*4) { /* is buffer too big? */
  210. L->Mbuffsize /= 2; /* still larger than Mbuffnext*2 */
  211. luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
  212. }
  213. return recovered;
  214. }
  215. void luaC_checkGC (lua_State *L) {
  216. if (L->nblocks >= L->GCthreshold)
  217. lua_collectgarbage(L, 0);
  218. }