9
3

lgc.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 AGEBITS 7 /* all age bits (111) */
  87. #define getage(o) ((o)->marked & AGEBITS)
  88. #define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a))
  89. #define isold(o) (getage(o) > G_SURVIVAL)
  90. #define changeage(o,f,t) \
  91. check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t)))
  92. /* Default Values for GC parameters */
  93. #define LUAI_GENMAJORMUL 100
  94. #define LUAI_GENMINORMUL 20
  95. /* wait memory to double before starting new cycle */
  96. #define LUAI_GCPAUSE 200
  97. /*
  98. ** some gc parameters are stored divided by 4 to allow a maximum value
  99. ** up to 1023 in a 'lu_byte'.
  100. */
  101. #define getgcparam(p) ((p) * 4)
  102. #define setgcparam(p,v) ((p) = (v) / 4)
  103. #define LUAI_GCMUL 100
  104. /* how much to allocate before next GC step (log2) */
  105. #define LUAI_GCSTEPSIZE 13 /* 8 KB */
  106. /*
  107. ** Check whether the declared GC mode is generational. While in
  108. ** generational mode, the collector can go temporarily to incremental
  109. ** mode to improve performance. This is signaled by 'g->lastatomic != 0'.
  110. */
  111. #define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0)
  112. /*
  113. ** Does one step of collection when debt becomes positive. 'pre'/'pos'
  114. ** allows some adjustments to be done only when needed. macro
  115. ** 'condchangemem' is used only for heavy tests (forcing a full
  116. ** GC cycle on every opportunity)
  117. */
  118. #define luaC_condGC(L,pre,pos) \
  119. { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
  120. condchangemem(L,pre,pos); }
  121. /* more often than not, 'pre'/'pos' are empty */
  122. #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
  123. #define luaC_barrier(L,p,v) ( \
  124. (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
  125. luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0))
  126. #define luaC_barrierback(L,p,v) ( \
  127. (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
  128. luaC_barrierback_(L,p) : cast_void(0))
  129. #define luaC_objbarrier(L,p,o) ( \
  130. (isblack(p) && iswhite(o)) ? \
  131. luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
  132. LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
  133. LUAI_FUNC void luaC_freeallobjects (lua_State *L);
  134. LUAI_FUNC void luaC_step (lua_State *L);
  135. LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
  136. LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
  137. LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
  138. LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
  139. LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
  140. LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
  141. LUAI_FUNC void luaC_changemode (lua_State *L, int newmode);
  142. #endif