lgc.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. ** $Id: lgc.h,v 2.58 2012/09/11 12:53:08 roberto Exp $
  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
  12. ** means the object is not marked; gray, which means the
  13. ** object is marked, but its references may be not marked; and
  14. ** black, which means that the object and all its references are marked.
  15. ** The main invariant of the garbage collector, while marking objects,
  16. ** is that a black object can never point to a white one. Moreover,
  17. ** any gray object must be in a "gray list" (gray, grayagain, weak,
  18. ** allweak, ephemeron) so that it can be visited again before finishing
  19. ** the collection cycle. These lists have no meaning when the invariant
  20. ** is not being enforced (e.g., sweep phase).
  21. */
  22. /* how much to allocate before next GC step */
  23. #if !defined(GCSTEPSIZE)
  24. /* ~100 small strings */
  25. #define GCSTEPSIZE (cast_int(100 * sizeof(TString)))
  26. #endif
  27. /*
  28. ** Possible states of the Garbage Collector
  29. */
  30. #define GCSpropagate 0
  31. #define GCSatomic 1
  32. #define GCSsweepstring 2
  33. #define GCSsweepudata 3
  34. #define GCSsweep 4
  35. #define GCSpause 5
  36. #define issweepphase(g) \
  37. (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep)
  38. #define isgenerational(g) ((g)->gckind == KGC_GEN)
  39. /*
  40. ** macros to tell when main invariant (white objects cannot point to black
  41. ** ones) must be kept. During a non-generational collection, the sweep
  42. ** phase may break the invariant, as objects turned white may point to
  43. ** still-black objects. The invariant is restored when sweep ends and
  44. ** all objects are white again. During a generational collection, the
  45. ** invariant must be kept all times.
  46. */
  47. #define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic)
  48. /*
  49. ** Outside the collector, the state in generational mode is kept in
  50. ** 'propagate', so 'keepinvariant' is always true.
  51. */
  52. #define keepinvariantout(g) \
  53. check_exp(g->gcstate == GCSpropagate || !isgenerational(g), \
  54. g->gcstate <= GCSatomic)
  55. /*
  56. ** some useful bit tricks
  57. */
  58. #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
  59. #define setbits(x,m) ((x) |= (m))
  60. #define testbits(x,m) ((x) & (m))
  61. #define bitmask(b) (1<<(b))
  62. #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
  63. #define l_setbit(x,b) setbits(x, bitmask(b))
  64. #define resetbit(x,b) resetbits(x, bitmask(b))
  65. #define testbit(x,b) testbits(x, bitmask(b))
  66. /* Layout for bit use in `marked' field: */
  67. #define WHITE0BIT 0 /* object is white (type 0) */
  68. #define WHITE1BIT 1 /* object is white (type 1) */
  69. #define BLACKBIT 2 /* object is black */
  70. #define FINALIZEDBIT 3 /* object has been separated for finalization */
  71. #define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */
  72. #define FIXEDBIT 5 /* object is fixed (should not be collected) */
  73. #define OLDBIT 6 /* object is old (only in generational mode) */
  74. /* bit 7 is currently used by tests (luaL_checkmemory) */
  75. #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
  76. #define iswhite(x) testbits((x)->gch.marked, WHITEBITS)
  77. #define isblack(x) testbit((x)->gch.marked, BLACKBIT)
  78. #define isgray(x) /* neither white nor black */ \
  79. (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT)))
  80. #define isold(x) testbit((x)->gch.marked, OLDBIT)
  81. /* MOVE OLD rule: whenever an object is moved to the beginning of
  82. a GC list, its old bit must be cleared */
  83. #define resetoldbit(o) resetbit((o)->gch.marked, OLDBIT)
  84. #define otherwhite(g) (g->currentwhite ^ WHITEBITS)
  85. #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow)))
  86. #define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked)
  87. #define changewhite(x) ((x)->gch.marked ^= WHITEBITS)
  88. #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
  89. #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
  90. #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
  91. #define luaC_condGC(L,c) \
  92. {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);}
  93. #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);)
  94. #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
  95. luaC_barrier_(L,obj2gco(p),gcvalue(v)); }
  96. #define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
  97. luaC_barrierback_(L,p); }
  98. #define luaC_objbarrier(L,p,o) \
  99. { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
  100. luaC_barrier_(L,obj2gco(p),obj2gco(o)); }
  101. #define luaC_objbarrierback(L,p,o) \
  102. { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); }
  103. #define luaC_barrierproto(L,p,c) \
  104. { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); }
  105. LUAI_FUNC void luaC_freeallobjects (lua_State *L);
  106. LUAI_FUNC void luaC_step (lua_State *L);
  107. LUAI_FUNC void luaC_forcestep (lua_State *L);
  108. LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
  109. LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
  110. LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz,
  111. GCObject **list, int offset);
  112. LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
  113. LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
  114. LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c);
  115. LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
  116. LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv);
  117. LUAI_FUNC void luaC_changemode (lua_State *L, int mode);
  118. #endif