lgc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. ** $Id: lgc.c,v 1.40 2000/01/25 13:57:18 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->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)) != LUA_T_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 != LUA_T_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 LUA_T_USERDATA: case LUA_T_STRING:
  78. strmark(L, tsvalue(o));
  79. break;
  80. case LUA_T_ARRAY:
  81. hashmark(L, avalue(o));
  82. break;
  83. case LUA_T_LCLOSURE: case LUA_T_LCLMARK:
  84. case LUA_T_CCLOSURE: case LUA_T_CCLMARK:
  85. closuremark(L, o->value.cl);
  86. break;
  87. case LUA_T_LPROTO: case LUA_T_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. TProtoFunc **p = &L->rootproto;
  96. TProtoFunc *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) = LUA_T_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. TaggedString **p = &tb->hash[j];
  163. TaggedString *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. #ifdef LUA_COMPAT_GC
  186. static void tableTM (lua_State *L) {
  187. Hash *p;
  188. TObject o;
  189. ttype(&o) = LUA_T_ARRAY;
  190. for (p = L->roottable; p; p = p->next) {
  191. if (!p->marked) {
  192. avalue(&o) = p;
  193. luaD_gcIM(L, &o);
  194. }
  195. }
  196. }
  197. #else
  198. #define tableTM(L) /* do nothing */
  199. #endif
  200. static void markall (lua_State *L) {
  201. travstack(L); /* mark stack objects */
  202. travglobal(L); /* mark global variable values and names */
  203. travlock(L); /* mark locked objects */
  204. luaT_travtagmethods(L, markobject); /* mark tag methods */
  205. }
  206. void luaC_collect (lua_State *L, int all) {
  207. int oldah = L->allowhooks;
  208. L->allowhooks = 0; /* stop debug hooks during GC */
  209. L->GCthreshold *= 4; /* to avoid GC during GC */
  210. tableTM(L); /* call TM for tables (if LUA_COMPAT_GC) */
  211. collecttable(L);
  212. collectstring(L, all?MAX_INT:1);
  213. collectproto(L);
  214. collectclosure(L);
  215. L->allowhooks = oldah; /* restore hooks */
  216. }
  217. long lua_collectgarbage (lua_State *L, long limit) {
  218. unsigned long recovered = L->nblocks; /* to subtract `nblocks' after gc */
  219. markall(L);
  220. luaR_invalidaterefs(L);
  221. luaC_collect(L, 0);
  222. luaD_gcIM(L, &luaO_nilobject); /* GC tag method for nil (signal end of GC) */
  223. recovered = recovered - L->nblocks;
  224. L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
  225. if (L->Mbuffsize > L->Mbuffnext*4) { /* is buffer too big? */
  226. L->Mbuffsize /= 2; /* still larger than Mbuffnext*2 */
  227. luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
  228. }
  229. return recovered;
  230. }
  231. void luaC_checkGC (lua_State *L) {
  232. if (L->nblocks >= L->GCthreshold)
  233. lua_collectgarbage(L, 0);
  234. }