lgc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. ** $Id: lgc.c,v 1.47 2000/04/14 18:12:35 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 void luaD_gcTM (lua_State *L, const TObject *o) {
  19. const TObject *im = luaT_getimbyObj(L, o, IM_GC);
  20. if (ttype(im) != TAG_NIL) {
  21. luaD_checkstack(L, 2);
  22. *(L->top++) = *im;
  23. *(L->top++) = *o;
  24. luaD_call(L, L->top-2, 0);
  25. }
  26. }
  27. static int markobject (lua_State *L, TObject *o);
  28. /* mark a string; marks larger than 1 cannot be changed */
  29. #define strmark(L, s) {if ((s)->marked == 0) (s)->marked = 1;}
  30. static void protomark (lua_State *L, Proto *f) {
  31. if (!f->marked) {
  32. int i;
  33. f->marked = 1;
  34. strmark(L, f->source);
  35. for (i=f->nkstr-1; i>=0; i--)
  36. strmark(L, f->kstr[i]);
  37. for (i=f->nkproto-1; i>=0; i--)
  38. protomark(L, f->kproto[i]);
  39. }
  40. }
  41. static void closuremark (lua_State *L, Closure *f) {
  42. if (!f->marked) {
  43. int i = f->nelems;
  44. f->marked = 1;
  45. while (i--)
  46. markobject(L, &f->consts[i]);
  47. }
  48. }
  49. static void hashmark (lua_State *L, Hash *h) {
  50. if (!h->marked) {
  51. int i;
  52. h->marked = 1;
  53. for (i=h->size-1; i>=0; i--) {
  54. Node *n = node(h,i);
  55. if (ttype(key(n)) != TAG_NIL) {
  56. markobject(L, &n->key);
  57. markobject(L, &n->val);
  58. }
  59. }
  60. }
  61. }
  62. static void travstack (lua_State *L) {
  63. int i;
  64. for (i = (L->top-1)-L->stack; i>=0; i--)
  65. markobject(L, L->stack+i);
  66. }
  67. static void travlock (lua_State *L) {
  68. int i;
  69. for (i=0; i<L->refSize; i++) {
  70. if (L->refArray[i].st == LOCK)
  71. markobject(L, &L->refArray[i].o);
  72. }
  73. }
  74. static int markobject (lua_State *L, TObject *o) {
  75. switch (ttype(o)) {
  76. case TAG_USERDATA: case TAG_STRING:
  77. strmark(L, tsvalue(o));
  78. break;
  79. case TAG_TABLE:
  80. hashmark(L, avalue(o));
  81. break;
  82. case TAG_LCLOSURE: case TAG_LMARK:
  83. protomark(L, clvalue(o)->f.l);
  84. /* go trhough */
  85. case TAG_CCLOSURE: case TAG_CMARK:
  86. closuremark(L, clvalue(o));
  87. break;
  88. default: break; /* numbers, etc */
  89. }
  90. return 0;
  91. }
  92. static void collectproto (lua_State *L) {
  93. Proto **p = &L->rootproto;
  94. Proto *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. ** collect all elements with `marked' < `limit'.
  136. ** with limit=1, that means all unmarked elements;
  137. ** with limit=MAX_INT, that means all elements.
  138. */
  139. static void collectstring (lua_State *L, int limit) {
  140. TObject o; /* to call userdata `gc' tag method */
  141. int i;
  142. ttype(&o) = TAG_USERDATA;
  143. for (i=0; i<NUM_HASHS; i++) { /* for each hash table */
  144. stringtable *tb = &L->string_root[i];
  145. int j;
  146. for (j=0; j<tb->size; j++) { /* for each list */
  147. TString **p = &tb->hash[j];
  148. TString *next;
  149. while ((next = *p) != NULL) {
  150. if (next->marked >= limit) {
  151. if (next->marked < FIXMARK) /* does not change FIXMARKs */
  152. next->marked = 0;
  153. p = &next->nexthash;
  154. }
  155. else { /* collect */
  156. if (next->constindex == -1) { /* is userdata? */
  157. tsvalue(&o) = next;
  158. luaD_gcTM(L, &o);
  159. }
  160. *p = next->nexthash;
  161. luaS_free(L, next);
  162. tb->nuse--;
  163. }
  164. }
  165. }
  166. if ((tb->nuse+1)*6 < tb->size)
  167. luaS_resize(L, tb, tb->size/2); /* table is too big */
  168. }
  169. }
  170. static void markall (lua_State *L) {
  171. travstack(L); /* mark stack objects */
  172. hashmark(L, L->gt); /* mark global variable values and names */
  173. travlock(L); /* mark locked objects */
  174. luaT_travtagmethods(L, markobject); /* mark tag methods */
  175. }
  176. void luaC_collect (lua_State *L, int all) {
  177. int oldah = L->allowhooks;
  178. L->allowhooks = 0; /* stop debug hooks during GC */
  179. L->GCthreshold *= 4; /* to avoid GC during GC */
  180. collecttable(L);
  181. collectstring(L, all?MAX_INT:1);
  182. collectproto(L);
  183. collectclosure(L);
  184. L->allowhooks = oldah; /* restore hooks */
  185. }
  186. long lua_collectgarbage (lua_State *L, long limit) {
  187. unsigned long recovered = L->nblocks; /* to subtract `nblocks' after gc */
  188. markall(L);
  189. luaR_invalidaterefs(L);
  190. luaC_collect(L, 0);
  191. recovered = recovered - L->nblocks;
  192. L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
  193. if (L->Mbuffsize > L->Mbuffnext*4) { /* is buffer too big? */
  194. L->Mbuffsize /= 2; /* still larger than Mbuffnext*2 */
  195. luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
  196. }
  197. luaD_gcTM(L, &luaO_nilobject);
  198. return recovered;
  199. }
  200. void luaC_checkGC (lua_State *L) {
  201. if (L->nblocks >= L->GCthreshold)
  202. lua_collectgarbage(L, 0);
  203. }