2
0

lgc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. ** $Id: lgc.h,v 2.75 2013/09/11 14:47:08 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 GCSswplocalgc 2
  33. #define GCSswpallgc 4
  34. #define GCSswpthreads 3
  35. #define GCSswplocalfin 5
  36. #define GCSswpfinobj 6
  37. #define GCSswptobefnz 7
  38. #define GCSswpend 8
  39. #define GCSpause 9
  40. #define issweepphase(g) \
  41. (GCSswplocalgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
  42. /*
  43. ** macro to tell when main invariant (white objects cannot point to black
  44. ** ones) must be kept. During a collection, the sweep
  45. ** phase may break the invariant, as objects turned white may point to
  46. ** still-black objects. The invariant is restored when sweep ends and
  47. ** all objects are white again.
  48. */
  49. #define keepinvariant(g) ((g)->gcstate <= GCSatomic)
  50. /*
  51. ** some useful bit tricks
  52. */
  53. #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
  54. #define setbits(x,m) ((x) |= (m))
  55. #define testbits(x,m) ((x) & (m))
  56. #define bitmask(b) (1<<(b))
  57. #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
  58. #define l_setbit(x,b) setbits(x, bitmask(b))
  59. #define resetbit(x,b) resetbits(x, bitmask(b))
  60. #define testbit(x,b) testbits(x, bitmask(b))
  61. /* Layout for bit use in `marked' field: */
  62. #define WHITE0BIT 0 /* object is white (type 0) */
  63. #define WHITE1BIT 1 /* object is white (type 1) */
  64. #define BLACKBIT 2 /* object is black */
  65. #define FINALIZEDBIT 3 /* object has been marked for finalization */
  66. #define NOLOCALBIT 4 /* object is not local */
  67. #define LOCALMARK 5 /* object is 'locally marked' or out of local list */
  68. /* bit 7 is currently used by tests (luaL_checkmemory) */
  69. #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
  70. #define iswhite(x) testbits((x)->gch.marked, WHITEBITS)
  71. #define isblack(x) testbit((x)->gch.marked, BLACKBIT)
  72. #define isgray(x) /* neither white nor black */ \
  73. (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT)))
  74. #define islocal(x) (!testbit((x)->gch.marked, NOLOCALBIT))
  75. #define tofinalize(x) testbit((x)->gch.marked, FINALIZEDBIT)
  76. #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
  77. #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow)))
  78. #define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked)
  79. #define changewhite(x) ((x)->gch.marked ^= WHITEBITS)
  80. #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
  81. #define nolocal(x) l_setbit((x)->gch.marked, NOLOCALBIT)
  82. #define valnolocal(v) { if (iscollectable(v)) nolocal(gcvalue(v)); }
  83. #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
  84. #define luaC_condGC(L,c) \
  85. {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);}
  86. #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);)
  87. #define luaC_barrier(L,p,v) { \
  88. if (iscollectable(v) && \
  89. (nolocal(gcvalue(v)), isblack(obj2gco(p)) && iswhite(gcvalue(v)))) \
  90. luaC_barrier_(L,obj2gco(p),gcvalue(v)); }
  91. #define luaC_barrierback(L,p,v) { \
  92. if (iscollectable(v) && \
  93. (nolocal(gcvalue(v)), isblack(obj2gco(p)) && iswhite(gcvalue(v)))) \
  94. luaC_barrierback_(L,obj2gco(p)); }
  95. #define luaC_objbarrier(L,p,o) { \
  96. if (nolocal(obj2gco(o)), isblack(obj2gco(p)) && iswhite(obj2gco(o))) \
  97. luaC_barrier_(L,obj2gco(p),obj2gco(o)); }
  98. #define luaC_upvalbarrier(L,uv) \
  99. { if (iscollectable((uv)->v) && !upisopen(uv)) \
  100. luaC_upvalbarrier_(L,uv); }
  101. LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
  102. LUAI_FUNC void luaC_freeallobjects (lua_State *L);
  103. LUAI_FUNC void luaC_step (lua_State *L);
  104. LUAI_FUNC void luaC_forcestep (lua_State *L);
  105. LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
  106. LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
  107. LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
  108. LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
  109. LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
  110. LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c);
  111. LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv);
  112. LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
  113. LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv);
  114. #endif