lgc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. ** $Id: lgc.c,v 1.34 1999/11/26 18:59:20 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 bigger 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_CLOSURE: case LUA_T_CLMARK:
  82. closuremark(L, o->value.cl);
  83. break;
  84. case LUA_T_PROTO: case LUA_T_PMARK:
  85. protomark(L, o->value.tf);
  86. break;
  87. default: break; /* numbers, cprotos, etc */
  88. }
  89. return 0;
  90. }
  91. static void collectproto (lua_State *L) {
  92. TProtoFunc **p = &L->rootproto;
  93. TProtoFunc *next;
  94. while ((next = *p) != NULL) {
  95. if (next->marked) {
  96. next->marked = 0;
  97. p = &next->next;
  98. }
  99. else {
  100. *p = next->next;
  101. luaF_freeproto(L, next);
  102. }
  103. }
  104. }
  105. static void collectclosure (lua_State *L) {
  106. Closure **p = &L->rootcl;
  107. Closure *next;
  108. while ((next = *p) != NULL) {
  109. if (next->marked) {
  110. next->marked = 0;
  111. p = &next->next;
  112. }
  113. else {
  114. *p = next->next;
  115. luaF_freeclosure(L, next);
  116. }
  117. }
  118. }
  119. static void collecttable (lua_State *L) {
  120. Hash **p = &L->roottable;
  121. Hash *next;
  122. while ((next = *p) != NULL) {
  123. if (next->marked) {
  124. next->marked = 0;
  125. p = &next->next;
  126. }
  127. else {
  128. *p = next->next;
  129. luaH_free(L, next);
  130. }
  131. }
  132. }
  133. /*
  134. ** remove from the global list globals whose names will be collected
  135. ** (the global itself is freed when its name is freed)
  136. */
  137. static void clear_global_list (lua_State *L, int limit) {
  138. GlobalVar **p = &L->rootglobal;
  139. GlobalVar *next;
  140. while ((next = *p) != NULL) {
  141. if (next->name->marked >= limit) p = &next->next;
  142. else *p = next->next;
  143. }
  144. }
  145. /*
  146. ** collect all elements with `marked' < `limit'.
  147. ** with limit=1, that means all unmarked elements;
  148. ** with limit=MAX_INT, that means all elements.
  149. */
  150. static void collectstring (lua_State *L, int limit) {
  151. TObject o; /* to call userdata 'gc' tag method */
  152. int i;
  153. ttype(&o) = LUA_T_USERDATA;
  154. clear_global_list(L, limit);
  155. for (i=0; i<NUM_HASHS; i++) { /* for each hash table */
  156. stringtable *tb = &L->string_root[i];
  157. int j;
  158. for (j=0; j<tb->size; j++) { /* for each list */
  159. TaggedString **p = &tb->hash[j];
  160. TaggedString *next;
  161. while ((next = *p) != NULL) {
  162. if (next->marked >= limit) {
  163. if (next->marked < FIXMARK) /* does not change FIXMARKs */
  164. next->marked = 0;
  165. p = &next->nexthash;
  166. }
  167. else { /* collect */
  168. if (next->constindex == -1) { /* is userdata? */
  169. tsvalue(&o) = next;
  170. luaD_gcIM(L, &o);
  171. }
  172. *p = next->nexthash;
  173. luaS_free(L, next);
  174. tb->nuse--;
  175. }
  176. }
  177. }
  178. if ((tb->nuse+1)*6 < tb->size)
  179. luaS_resize(L, tb, tb->size/2); /* table is too big */
  180. }
  181. }
  182. #ifdef LUA_COMPAT_GC
  183. static void tableTM (lua_State *L) {
  184. Hash *p;
  185. TObject o;
  186. ttype(&o) = LUA_T_ARRAY;
  187. for (p = L->roottable; p; p = p->next) {
  188. if (!p->marked) {
  189. avalue(&o) = p;
  190. luaD_gcIM(L, &o);
  191. }
  192. }
  193. }
  194. #else
  195. #define tableTM(L) /* do nothing */
  196. #endif
  197. static void markall (lua_State *L) {
  198. travstack(L); /* mark stack objects */
  199. travglobal(L); /* mark global variable values and names */
  200. travlock(L); /* mark locked objects */
  201. luaT_travtagmethods(L, markobject); /* mark tag methods */
  202. }
  203. void luaC_collect (lua_State *L, int all) {
  204. L->GCthreshold *= 4; /* to avoid GC during GC */
  205. tableTM(L); /* call TM for tables (if LUA_COMPAT_GC) */
  206. collecttable(L);
  207. collectstring(L, all?MAX_INT:1);
  208. collectproto(L);
  209. collectclosure(L);
  210. }
  211. long lua_collectgarbage (lua_State *L, long limit) {
  212. unsigned long recovered = L->nblocks; /* to subtract nblocks after gc */
  213. markall(L);
  214. luaR_invalidaterefs(L);
  215. luaC_collect(L, 0);
  216. luaD_gcIM(L, &luaO_nilobject); /* GC tag method for nil (signal end of GC) */
  217. recovered = recovered - L->nblocks;
  218. L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
  219. if (L->Mbuffsize > L->Mbuffnext*4) { /* is buffer too big? */
  220. L->Mbuffsize /= 2; /* still larger than Mbuffnext*2 */
  221. luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
  222. }
  223. return recovered;
  224. }
  225. void luaC_checkGC (lua_State *L) {
  226. if (L->nblocks >= L->GCthreshold)
  227. lua_collectgarbage(L, 0);
  228. }