lgc.c 6.2 KB

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