lgc.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.) These lists have no meaning
  21. ** when the invariant is not being enforced (e.g., sweep phase).
  22. */
  23. /*
  24. ** Possible states of the Garbage Collector
  25. */
  26. #define GCSpropagate 0
  27. #define GCSenteratomic 1
  28. #define GCSatomic 2
  29. #define GCSswpallgc 3
  30. #define GCSswpfinobj 4
  31. #define GCSswptobefnz 5
  32. #define GCSswpend 6
  33. #define GCScallfin 7
  34. #define GCSpause 8
  35. #define issweepphase(g) \
  36. (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
  37. /*
  38. ** macro to tell when main invariant (white objects cannot point to black
  39. ** ones) must be kept. During a collection, the sweep
  40. ** phase may break the invariant, as objects turned white may point to
  41. ** still-black objects. The invariant is restored when sweep ends and
  42. ** all objects are white again.
  43. */
  44. #define keepinvariant(g) ((g)->gcstate <= GCSatomic)
  45. /*
  46. ** some useful bit tricks
  47. */
  48. #define resetbits(x,m) ((x) &= cast_byte(~(m)))
  49. #define setbits(x,m) ((x) |= (m))
  50. #define testbits(x,m) ((x) & (m))
  51. #define bitmask(b) (1<<(b))
  52. #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
  53. #define l_setbit(x,b) setbits(x, bitmask(b))
  54. #define resetbit(x,b) resetbits(x, bitmask(b))
  55. #define testbit(x,b) testbits(x, bitmask(b))
  56. /*
  57. ** Layout for bit use in 'marked' field. First three bits are
  58. ** used for object "age" in generational mode. Last bit is used
  59. ** by tests.
  60. */
  61. #define WHITE0BIT 3 /* object is white (type 0) */
  62. #define WHITE1BIT 4 /* object is white (type 1) */
  63. #define BLACKBIT 5 /* object is black */
  64. #define FINALIZEDBIT 6 /* object has been marked for finalization */
  65. #define TESTBIT 7
  66. #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
  67. #define iswhite(x) testbits((x)->marked, WHITEBITS)
  68. #define isblack(x) testbit((x)->marked, BLACKBIT)
  69. #define isgray(x) /* neither white nor black */ \
  70. (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))
  71. #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
  72. #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
  73. #define isdeadm(ow,m) ((m) & (ow))
  74. #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
  75. #define changewhite(x) ((x)->marked ^= WHITEBITS)
  76. #define nw2black(x) \
  77. check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT))
  78. #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS)
  79. /* object age in generational mode */
  80. #define G_NEW 0 /* created in current cycle */
  81. #define G_SURVIVAL 1 /* created in previous cycle */
  82. #define G_OLD0 2 /* marked old by frw. barrier in this cycle */
  83. #define G_OLD1 3 /* first full cycle as old */
  84. #define G_OLD 4 /* really old object (not to be visited) */
  85. #define G_TOUCHED1 5 /* old object touched this cycle */
  86. #define G_TOUCHED2 6 /* old object touched in previous cycle */
  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 changeage(o,f,t) \
  92. check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t)))
  93. /* Default Values for GC parameters */
  94. /* generational */
  95. #define LUAI_GENMAJORMUL 100 /* major multiplier */
  96. #define LUAI_GENMINORMUL 20 /* minor multiplier */
  97. /* incremental */
  98. /* wait memory to double before starting new cycle */
  99. #define LUAI_GCPAUSE 200
  100. #define LUAI_GCMUL 300 /* step multiplier */
  101. /* how many objects to allocate before next GC step (log2) */
  102. #define LUAI_GCSTEPSIZE 8 /* 256 objects */
  103. /*
  104. ** Control when GC is running:
  105. */
  106. #define GCSTPUSR 1 /* bit true when GC stopped by user */
  107. #define GCSTPGC 2 /* bit true when GC stopped by itself */
  108. #define GCSTPCLS 4 /* bit true when closing Lua state */
  109. #define gcrunning(g) ((g)->gcstp == 0)
  110. /*
  111. ** Macros to set and apply GC parameters. GC parameters are given in
  112. ** percentage points, but are stored as lu_byte. To reduce their
  113. ** values and avoid repeated divisions by 100, these macros store
  114. ** the original parameter multiplied by 2^n and divided by 100.
  115. ** To apply them, the value is divided by 2^n (a shift) and then
  116. ** multiplied by the stored parameter, yielding
  117. ** value / 2^n * (original parameter * 2^n / 100), or approximately
  118. ** (value * original parameter / 100).
  119. **
  120. ** For most parameters, which are typically larger than 100%, 2^n is
  121. ** 16 (2^4), allowing maximum values up to 1599. For the minor
  122. ** multiplier, which is typically smaller, 2^n is 64 (2^6) to allow more
  123. ** precision.
  124. */
  125. #define gcparamshift(p) \
  126. (offsetof(global_State, p) == offsetof(global_State, genminormul) ? 6 : 4)
  127. #define setgcparam(g,p,v) \
  128. (g->p = (cast_uint(v) << gcparamshift(p)) / 100u)
  129. #define applygcparam(g,p,v) (((v) >> gcparamshift(p)) * g->p)
  130. /*
  131. ** Does one step of collection when debt becomes zero. 'pre'/'pos'
  132. ** allows some adjustments to be done only when needed. macro
  133. ** 'condchangemem' is used only for heavy tests (forcing a full
  134. ** GC cycle on every opportunity)
  135. */
  136. #define luaC_condGC(L,pre,pos) \
  137. { if (G(L)->GCdebt <= 0) { pre; luaC_step(L); pos;}; \
  138. condchangemem(L,pre,pos); }
  139. /* more often than not, 'pre'/'pos' are empty */
  140. #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
  141. #define luaC_barrier(L,p,v) ( \
  142. (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
  143. luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0))
  144. #define luaC_barrierback(L,p,v) ( \
  145. (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
  146. luaC_barrierback_(L,p) : cast_void(0))
  147. #define luaC_objbarrier(L,p,o) ( \
  148. (isblack(p) && iswhite(o)) ? \
  149. luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
  150. LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
  151. LUAI_FUNC void luaC_freeallobjects (lua_State *L);
  152. LUAI_FUNC void luaC_step (lua_State *L);
  153. LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
  154. LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
  155. LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
  156. LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz,
  157. size_t offset);
  158. LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
  159. LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
  160. LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
  161. LUAI_FUNC void luaC_changemode (lua_State *L, int newmode);
  162. #endif