lgc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. ** $Id: lgc.h,v 2.85 2014/07/19 15:14:46 roberto Exp roberto $
  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. #define luaC_condGC(L,c) \
  78. {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);}
  79. #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);)
  80. #define luaC_barrier(L,p,v) { \
  81. if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \
  82. luaC_barrier_(L,obj2gco(p),gcvalue(v)); }
  83. #define luaC_barrierback(L,p,v) { \
  84. if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \
  85. luaC_barrierback_(L,p); }
  86. #define luaC_objbarrier(L,p,o) { \
  87. if (isblack(p) && iswhite(o)) \
  88. luaC_barrier_(L,obj2gco(p),obj2gco(o)); }
  89. #define luaC_upvalbarrier(L,uv) \
  90. { if (iscollectable((uv)->v) && !upisopen(uv)) \
  91. luaC_upvalbarrier_(L,uv); }
  92. LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
  93. LUAI_FUNC void luaC_freeallobjects (lua_State *L);
  94. LUAI_FUNC void luaC_step (lua_State *L);
  95. LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
  96. LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
  97. LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
  98. LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
  99. LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o);
  100. LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv);
  101. LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
  102. LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv);
  103. #endif