lgc.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. ** $Id: lgc.h,v 2.49 2010/12/29 18:00:23 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. /*
  23. ** Possible states of the Garbage Collector
  24. */
  25. #define GCSpropagate 0
  26. #define GCSatomic 1
  27. #define GCSsweepstring 2
  28. #define GCSsweepudata 3
  29. #define GCSsweep 4
  30. #define GCSpause 5
  31. #define issweepphase(g) \
  32. (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep)
  33. #define isgenerational(g) ((g)->gckind == KGC_GEN)
  34. /*
  35. ** macro to tell when main invariant (white objects cannot point to black
  36. ** ones) must be kept. During a non-generational collection, the sweep
  37. ** phase may break the invariant, as objects turned white may point to
  38. ** still-black objects. The invariant is restored when sweep ends and
  39. ** all objects are white again. During a generational collection, the
  40. ** invariant must be kept all times.
  41. */
  42. #define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic)
  43. /*
  44. ** some useful bit tricks
  45. */
  46. #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
  47. #define setbits(x,m) ((x) |= (m))
  48. #define testbits(x,m) ((x) & (m))
  49. #define bitmask(b) (1<<(b))
  50. #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
  51. #define l_setbit(x,b) setbits(x, bitmask(b))
  52. #define resetbit(x,b) resetbits(x, bitmask(b))
  53. #define testbit(x,b) testbits(x, bitmask(b))
  54. #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2)))
  55. #define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2)))
  56. /* Layout for bit use in `marked' field: */
  57. #define WHITE0BIT 0 /* object is white (type 0) */
  58. #define WHITE1BIT 1 /* object is white (type 1) */
  59. #define BLACKBIT 2 /* object is black */
  60. #define FINALIZEDBIT 3 /* object has been separated for finalization */
  61. #define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */
  62. #define FIXEDBIT 5 /* object is fixed (should not be collected) */
  63. #define OLDBIT 6 /* object is old (only in generational mode) */
  64. /* bit 7 is currently used by tests (luaL_checkmemory) */
  65. #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
  66. #define iswhite(x) testbits((x)->gch.marked, WHITEBITS)
  67. #define isblack(x) testbit((x)->gch.marked, BLACKBIT)
  68. #define isgray(x) /* neither white nor black */ \
  69. (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT)))
  70. #define isold(x) testbit((x)->gch.marked, OLDBIT)
  71. /* MOVE OLD rule: whenever an object is moved to the beginning of
  72. a GC list, its old bit must be cleared */
  73. #define resetoldbit(o) resetbit((o)->gch.marked, OLDBIT)
  74. #define otherwhite(g) (g->currentwhite ^ WHITEBITS)
  75. #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow)))
  76. #define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked)
  77. #define changewhite(x) ((x)->gch.marked ^= WHITEBITS)
  78. #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
  79. #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
  80. #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
  81. #define luaC_condGC(L,c) \
  82. {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);}
  83. #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);)
  84. #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
  85. luaC_barrier_(L,obj2gco(p),gcvalue(v)); }
  86. #define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
  87. luaC_barrierback_(L,p); }
  88. #define luaC_objbarrier(L,p,o) \
  89. { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
  90. luaC_barrier_(L,obj2gco(p),obj2gco(o)); }
  91. #define luaC_objbarrierback(L,p,o) \
  92. { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); }
  93. #define luaC_barrierproto(L,p,c) \
  94. { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); }
  95. LUAI_FUNC void luaC_separateudata (lua_State *L, int all);
  96. LUAI_FUNC void luaC_freeallobjects (lua_State *L);
  97. LUAI_FUNC void luaC_step (lua_State *L);
  98. LUAI_FUNC void luaC_forcestep (lua_State *L);
  99. LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
  100. LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
  101. LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz,
  102. GCObject **list, int offset);
  103. LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
  104. LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
  105. LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c);
  106. LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
  107. LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv);
  108. LUAI_FUNC void luaC_changemode (lua_State *L, int mode);
  109. #endif