lgc.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. ** $Id: lgc.h,v 2.91 2015/12/21 13:02:14 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 GCSswpallgc 2
  33. #define GCSswpfinobj 3
  34. #define GCSswptobefnz 4
  35. #define GCSswpend 5
  36. #define GCScallfin 6
  37. #define GCSpause 7
  38. #define issweepphase(g) \
  39. (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
  40. /*
  41. ** macro to tell when main invariant (white objects cannot point to black
  42. ** ones) must be kept. During a collection, the sweep
  43. ** phase may break the invariant, as objects turned white may point to
  44. ** still-black objects. The invariant is restored when sweep ends and
  45. ** all objects are white again.
  46. */
  47. #define keepinvariant(g) ((g)->gcstate <= GCSatomic)
  48. /*
  49. ** some useful bit tricks
  50. */
  51. #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
  52. #define setbits(x,m) ((x) |= (m))
  53. #define testbits(x,m) ((x) & (m))
  54. #define bitmask(b) (1<<(b))
  55. #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
  56. #define l_setbit(x,b) setbits(x, bitmask(b))
  57. #define resetbit(x,b) resetbits(x, bitmask(b))
  58. #define testbit(x,b) testbits(x, bitmask(b))
  59. /* Layout for bit use in 'marked' field: */
  60. #define WHITE0BIT 0 /* object is white (type 0) */
  61. #define WHITE1BIT 1 /* object is white (type 1) */
  62. #define BLACKBIT 2 /* object is black */
  63. #define FINALIZEDBIT 3 /* object has been marked for finalization */
  64. /* bit 7 is currently used by tests (luaL_checkmemory) */
  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) ^ WHITEBITS) & (ow)))
  73. #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
  74. #define changewhite(x) ((x)->marked ^= WHITEBITS)
  75. #define gray2black(x) l_setbit((x)->marked, BLACKBIT)
  76. #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
  77. /*
  78. ** Does one step of collection when debt becomes positive. 'pre'/'pos'
  79. ** allows some adjustments to be done only when needed. macro
  80. ** 'condchangemem' is used only for heavy tests (forcing a full
  81. ** GC cycle on every opportunity)
  82. */
  83. #define luaC_condGC(L,pre,pos) \
  84. { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
  85. condchangemem(L,pre,pos); }
  86. /* more often than not, 'pre'/'pos' are empty */
  87. #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
  88. #define luaC_barrier(L,p,v) ( \
  89. (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
  90. luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0))
  91. #define luaC_barrierback(L,p,v) ( \
  92. (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
  93. luaC_barrierback_(L,p) : cast_void(0))
  94. #define luaC_objbarrier(L,p,o) ( \
  95. (isblack(p) && iswhite(o)) ? \
  96. luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
  97. #define luaC_upvalbarrier(L,uv) ( \
  98. (iscollectable((uv)->v) && !upisopen(uv)) ? \
  99. luaC_upvalbarrier_(L,uv) : cast_void(0))
  100. LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
  101. LUAI_FUNC void luaC_freeallobjects (lua_State *L);
  102. LUAI_FUNC void luaC_step (lua_State *L);
  103. LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
  104. LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
  105. LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
  106. LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
  107. LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o);
  108. LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv);
  109. LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
  110. LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv);
  111. #endif