lgc.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. ** $Id: lgc.h $
  3. ** Garbage Collector
  4. ** See Copyright Notice in lua.h
  5. */
  6. #ifndef lgc_h
  7. #define lgc_h
  8. #include "lobject.h"
  9. #include "lstate.h"
  10. /*
  11. ** Collectable objects may have one of three colors: white, which means
  12. ** the object is not marked; gray, which means the object is marked, but
  13. ** its references may be not marked; and black, which means that the
  14. ** object and all its references are marked. The main invariant of the
  15. ** garbage collector, while marking objects, is that a black object can
  16. ** never point to a white one. Moreover, any gray object must be in a
  17. ** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it
  18. ** can be visited again before finishing the collection cycle. (Open
  19. ** upvalues are an exception to this rule.) These lists have no meaning
  20. ** when the invariant is not being enforced (e.g., sweep phase).
  21. */
  22. /*
  23. ** Possible states of the Garbage Collector
  24. */
  25. #define GCSpropagate 0
  26. #define GCSenteratomic 1
  27. #define GCSatomic 2
  28. #define GCSswpallgc 3
  29. #define GCSswpfinobj 4
  30. #define GCSswptobefnz 5
  31. #define GCSswpend 6
  32. #define GCScallfin 7
  33. #define GCSpause 8
  34. #define issweepphase(g) \
  35. (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
  36. /*
  37. ** macro to tell when main invariant (white objects cannot point to black
  38. ** ones) must be kept. During a collection, the sweep
  39. ** phase may break the invariant, as objects turned white may point to
  40. ** still-black objects. The invariant is restored when sweep ends and
  41. ** all objects are white again.
  42. */
  43. #define keepinvariant(g) ((g)->gcstate <= GCSatomic)
  44. /*
  45. ** some useful bit tricks
  46. */
  47. #define resetbits(x,m) ((x) &= cast_byte(~(m)))
  48. #define setbits(x,m) ((x) |= (m))
  49. #define testbits(x,m) ((x) & (m))
  50. #define bitmask(b) (1<<(b))
  51. #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
  52. #define l_setbit(x,b) setbits(x, bitmask(b))
  53. #define resetbit(x,b) resetbits(x, bitmask(b))
  54. #define testbit(x,b) testbits(x, bitmask(b))
  55. /*
  56. ** Layout for bit use in 'marked' field. First three bits are
  57. ** used for object "age" in generational mode. Last bit is used
  58. ** by tests.
  59. */
  60. #define WHITE0BIT 3 /* object is white (type 0) */
  61. #define WHITE1BIT 4 /* object is white (type 1) */
  62. #define BLACKBIT 5 /* object is black */
  63. #define FINALIZEDBIT 6 /* object has been marked for finalization */
  64. #define TESTBIT 7
  65. #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
  66. #define iswhite(x) testbits((x)->marked, WHITEBITS)
  67. #define isblack(x) testbit((x)->marked, BLACKBIT)
  68. #define isgray(x) /* neither white nor black */ \
  69. (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))
  70. #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
  71. #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
  72. #define isdeadm(ow,m) ((m) & (ow))
  73. #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
  74. #define changewhite(x) ((x)->marked ^= WHITEBITS)
  75. #define nw2black(x) \
  76. check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT))
  77. #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS)
  78. /* object age in generational mode */
  79. #define G_NEW 0 /* created in current cycle */
  80. #define G_SURVIVAL 1 /* created in previous cycle */
  81. #define G_OLD0 2 /* marked old by frw. barrier in this cycle */
  82. #define G_OLD1 3 /* first full cycle as old */
  83. #define G_OLD 4 /* really old object (not to be visited) */
  84. #define G_TOUCHED1 5 /* old object touched this cycle */
  85. #define G_TOUCHED2 6 /* old object touched in previous cycle */
  86. #define G_SHARED 7 /* object is shared */
  87. #define AGEBITS 7 /* all age bits (111) */
  88. #define getage(o) ((o)->marked & AGEBITS)
  89. #define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a))
  90. #define isold(o) (getage(o) > G_SURVIVAL)
  91. #define isshared(x) (getage(x) == G_SHARED)
  92. #define makeshared(x) setage(x, G_SHARED)
  93. #define ispurewhite(x) (iswhite(x) && !isshared(x))
  94. #define changeage(o,f,t) \
  95. check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t)))
  96. /* Default Values for GC parameters */
  97. #define LUAI_GENMAJORMUL 100
  98. #define LUAI_GENMINORMUL 20
  99. /* wait memory to double before starting new cycle */
  100. #define LUAI_GCPAUSE 200
  101. /*
  102. ** some gc parameters are stored divided by 4 to allow a maximum value
  103. ** up to 1023 in a 'lu_byte'.
  104. */
  105. #define getgcparam(p) ((p) * 4)
  106. #define setgcparam(p,v) ((p) = (v) / 4)
  107. #define LUAI_GCMUL 100
  108. /* how much to allocate before next GC step (log2) */
  109. #define LUAI_GCSTEPSIZE 13 /* 8 KB */
  110. /*
  111. ** Check whether the declared GC mode is generational. While in
  112. ** generational mode, the collector can go temporarily to incremental
  113. ** mode to improve performance. This is signaled by 'g->lastatomic != 0'.
  114. */
  115. #define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0)
  116. /*
  117. ** Control when GC is running:
  118. */
  119. #define GCSTPUSR 1 /* bit true when GC stopped by user */
  120. #define GCSTPGC 2 /* bit true when GC stopped by itself */
  121. #define GCSTPCLS 4 /* bit true when closing Lua state */
  122. #define gcrunning(g) ((g)->gcstp == 0)
  123. /*
  124. ** Does one step of collection when debt becomes positive. 'pre'/'pos'
  125. ** allows some adjustments to be done only when needed. macro
  126. ** 'condchangemem' is used only for heavy tests (forcing a full
  127. ** GC cycle on every opportunity)
  128. */
  129. #define luaC_condGC(L,pre,pos) \
  130. { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
  131. condchangemem(L,pre,pos); }
  132. /* more often than not, 'pre'/'pos' are empty */
  133. #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
  134. #define luaC_objbarrier(L,p,o) ( \
  135. (isblack(p) && ispurewhite(o)) ? \
  136. luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
  137. #define luaC_barrier(L,p,v) ( \
  138. iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0))
  139. #define luaC_objbarrierback(L,p,o) ( \
  140. (isblack(p) && ispurewhite(o)) ? luaC_barrierback_(L,p) : cast_void(0))
  141. #define luaC_barrierback(L,p,v) ( \
  142. iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0))
  143. LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
  144. LUAI_FUNC void luaC_freeallobjects (lua_State *L);
  145. LUAI_FUNC void luaC_step (lua_State *L);
  146. LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
  147. LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
  148. LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
  149. LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz,
  150. size_t offset);
  151. LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
  152. LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
  153. LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
  154. LUAI_FUNC void luaC_changemode (lua_State *L, int newmode);
  155. #endif