lgc.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <stddef.h>
  9. #include "lobject.h"
  10. #include "lstate.h"
  11. /*
  12. ** Collectable objects may have one of three colors: white, which means
  13. ** the object is not marked; gray, which means the object is marked, but
  14. ** its references may be not marked; and black, which means that the
  15. ** object and all its references are marked. The main invariant of the
  16. ** garbage collector, while marking objects, is that a black object can
  17. ** never point to a white one. Moreover, any gray object must be in a
  18. ** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it
  19. ** can be visited again before finishing the collection cycle. (Open
  20. ** upvalues are an exception to this rule, as they are attached to
  21. ** a corresponding thread.) These lists have no meaning when the
  22. ** invariant is not being enforced (e.g., sweep phase).
  23. */
  24. /*
  25. ** Possible states of the Garbage Collector
  26. */
  27. #define GCSpropagate 0
  28. #define GCSenteratomic 1
  29. #define GCSatomic 2
  30. #define GCSswpallgc 3
  31. #define GCSswpfinobj 4
  32. #define GCSswptobefnz 5
  33. #define GCSswpend 6
  34. #define GCScallfin 7
  35. #define GCSpause 8
  36. #define issweepphase(g) \
  37. (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
  38. /*
  39. ** macro to tell when main invariant (white objects cannot point to black
  40. ** ones) must be kept. During a collection, the sweep phase may break
  41. ** the invariant, as objects turned white may point to still-black
  42. ** objects. The invariant is restored when sweep ends and all objects
  43. ** are white again.
  44. */
  45. #define keepinvariant(g) ((g)->gcstate <= GCSatomic)
  46. /*
  47. ** some useful bit tricks
  48. */
  49. #define resetbits(x,m) ((x) &= cast_byte(~(m)))
  50. #define setbits(x,m) ((x) |= (m))
  51. #define testbits(x,m) ((x) & (m))
  52. #define bitmask(b) (1<<(b))
  53. #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
  54. #define l_setbit(x,b) setbits(x, bitmask(b))
  55. #define resetbit(x,b) resetbits(x, bitmask(b))
  56. #define testbit(x,b) testbits(x, bitmask(b))
  57. /*
  58. ** Layout for bit use in 'marked' field. First three bits are
  59. ** used for object "age" in generational mode. Last bit is used
  60. ** by tests.
  61. */
  62. #define WHITE0BIT 3 /* object is white (type 0) */
  63. #define WHITE1BIT 4 /* object is white (type 1) */
  64. #define BLACKBIT 5 /* object is black */
  65. #define FINALIZEDBIT 6 /* object has been marked for finalization */
  66. #define TESTBIT 7
  67. #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
  68. #define iswhite(x) testbits((x)->marked, WHITEBITS)
  69. #define isblack(x) testbit((x)->marked, BLACKBIT)
  70. #define isgray(x) /* neither white nor black */ \
  71. (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))
  72. #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
  73. #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
  74. #define isdeadm(ow,m) ((m) & (ow))
  75. #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
  76. #define changewhite(x) ((x)->marked ^= WHITEBITS)
  77. #define nw2black(x) \
  78. check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT))
  79. #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS)
  80. /* object age in generational mode */
  81. #define G_NEW 0 /* created in current cycle */
  82. #define G_SURVIVAL 1 /* created in previous cycle */
  83. #define G_OLD0 2 /* marked old by frw. barrier in this cycle */
  84. #define G_OLD1 3 /* first full cycle as old */
  85. #define G_OLD 4 /* really old object (not to be visited) */
  86. #define G_TOUCHED1 5 /* old object touched this cycle */
  87. #define G_TOUCHED2 6 /* old object touched in previous cycle */
  88. #define AGEBITS 7 /* all age bits (111) */
  89. #define getage(o) ((o)->marked & AGEBITS)
  90. #define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a))
  91. #define isold(o) (getage(o) > G_SURVIVAL)
  92. /*
  93. ** In generational mode, objects are created 'new'. After surviving one
  94. ** cycle, they become 'survival'. Both 'new' and 'survival' can point
  95. ** to any other object, as they are traversed at the end of the cycle.
  96. ** We call them both 'young' objects.
  97. ** If a survival object survives another cycle, it becomes 'old1'.
  98. ** 'old1' objects can still point to survival objects (but not to
  99. ** new objects), so they still must be traversed. After another cycle
  100. ** (that, being old, 'old1' objects will "survive" no matter what)
  101. ** finally the 'old1' object becomes really 'old', and then they
  102. ** are no more traversed.
  103. **
  104. ** To keep its invariants, the generational mode uses the same barriers
  105. ** also used by the incremental mode. If a young object is caught in a
  106. ** forward barrier, it cannot become old immediately, because it can
  107. ** still point to other young objects. Instead, it becomes 'old0',
  108. ** which in the next cycle becomes 'old1'. So, 'old0' objects is
  109. ** old but can point to new and survival objects; 'old1' is old
  110. ** but cannot point to new objects; and 'old' cannot point to any
  111. ** young object.
  112. **
  113. ** If any old object ('old0', 'old1', 'old') is caught in a back
  114. ** barrier, it becomes 'touched1' and goes into a gray list, to be
  115. ** visited at the end of the cycle. There it evolves to 'touched2',
  116. ** which can point to survivals but not to new objects. In yet another
  117. ** cycle then it becomes 'old' again.
  118. **
  119. ** The generational mode must also control the colors of objects,
  120. ** because of the barriers. While the mutator is running, young objects
  121. ** are kept white. 'old', 'old1', and 'touched2' objects are kept black,
  122. ** as they cannot point to new objects; exceptions are threads and open
  123. ** upvalues, which age to 'old1' and 'old' but are kept gray. 'old0'
  124. ** objects may be gray or black, as in the incremental mode. 'touched1'
  125. ** objects are kept gray, as they must be visited again at the end of
  126. ** the cycle.
  127. */
  128. /*
  129. ** {======================================================
  130. ** Default Values for GC parameters
  131. ** =======================================================
  132. */
  133. /*
  134. ** Minor collections will shift to major ones after LUAI_MINORMAJOR%
  135. ** bytes become old.
  136. */
  137. #define LUAI_MINORMAJOR 70
  138. /*
  139. ** Major collections will shift to minor ones after a collection
  140. ** collects at least LUAI_MAJORMINOR% of the new bytes.
  141. */
  142. #define LUAI_MAJORMINOR 50
  143. /*
  144. ** A young (minor) collection will run after creating LUAI_GENMINORMUL%
  145. ** new bytes.
  146. */
  147. #define LUAI_GENMINORMUL 20
  148. /* incremental */
  149. /* Number of bytes must be LUAI_GCPAUSE% before starting new cycle */
  150. #define LUAI_GCPAUSE 250
  151. /*
  152. ** Step multiplier: The collector handles LUAI_GCMUL% work units for
  153. ** each new allocated word. (Each "work unit" corresponds roughly to
  154. ** sweeping one object or traversing one slot.)
  155. */
  156. #define LUAI_GCMUL 200
  157. /* How many bytes to allocate before next GC step */
  158. #define LUAI_GCSTEPSIZE (200 * sizeof(Table))
  159. #define setgcparam(g,p,v) (g->gcparams[LUA_GCP##p] = luaO_codeparam(v))
  160. #define applygcparam(g,p,x) luaO_applyparam(g->gcparams[LUA_GCP##p], x)
  161. /* }====================================================== */
  162. /*
  163. ** Control when GC is running:
  164. */
  165. #define GCSTPUSR 1 /* bit true when GC stopped by user */
  166. #define GCSTPGC 2 /* bit true when GC stopped by itself */
  167. #define GCSTPCLS 4 /* bit true when closing Lua state */
  168. #define gcrunning(g) ((g)->gcstp == 0)
  169. /*
  170. ** Does one step of collection when debt becomes zero. 'pre'/'pos'
  171. ** allows some adjustments to be done only when needed. macro
  172. ** 'condchangemem' is used only for heavy tests (forcing a full
  173. ** GC cycle on every opportunity)
  174. */
  175. #if !defined(HARDMEMTESTS)
  176. #define condchangemem(L,pre,pos,emg) ((void)0)
  177. #else
  178. #define condchangemem(L,pre,pos,emg) \
  179. { if (gcrunning(G(L))) { pre; luaC_fullgc(L, emg); pos; } }
  180. #endif
  181. #define luaC_condGC(L,pre,pos) \
  182. { if (G(L)->GCdebt <= 0) { pre; luaC_step(L); pos;}; \
  183. condchangemem(L,pre,pos,0); }
  184. /* more often than not, 'pre'/'pos' are empty */
  185. #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
  186. #define luaC_objbarrier(L,p,o) ( \
  187. (isblack(p) && iswhite(o)) ? \
  188. luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
  189. #define luaC_barrier(L,p,v) ( \
  190. iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0))
  191. #define luaC_objbarrierback(L,p,o) ( \
  192. (isblack(p) && iswhite(o)) ? luaC_barrierback_(L,p) : cast_void(0))
  193. #define luaC_barrierback(L,p,v) ( \
  194. iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0))
  195. LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
  196. LUAI_FUNC void luaC_freeallobjects (lua_State *L);
  197. LUAI_FUNC void luaC_step (lua_State *L);
  198. LUAI_FUNC void luaC_runtilstate (lua_State *L, int state, int fast);
  199. LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
  200. LUAI_FUNC GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz);
  201. LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, lu_byte tt, size_t sz,
  202. size_t offset);
  203. LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
  204. LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
  205. LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
  206. LUAI_FUNC void luaC_changemode (lua_State *L, int newmode);
  207. #endif