lgc.c 5.5 KB

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