Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2. ** $Id: lgc.h,v 2.44 2010/06/30 14:11:17 roberto Exp $
  3. ** Garbage Collector
  4. ** See Copyright Notice in lua.h
  5. */
  6.  
  7. #ifndef lgc_h
  8. #define lgc_h
  9.  
  10.  
  11. #include "lobject.h"
  12. #include "lstate.h"
  13.  
  14. /*
  15. ** Collectable objects may have one of three colors: white, which
  16. ** means the object is not marked; gray, which means the
  17. ** object is marked, but its references may be not marked; and
  18. ** black, which means that the object and all its references are marked.
  19. ** The main invariant of the garbage collector, while marking objects,
  20. ** is that a black object can never point to a white one. Moreover,
  21. ** any gray object must be in a "gray list" (gray, grayagain, weak,
  22. ** allweak, ephemeron) so that it can be visited again before finishing
  23. ** the collection cycle. These lists have no meaning when the invariant
  24. ** is not being enforced (e.g., sweep phase).
  25. */
  26.  
  27.  
  28. /*
  29. ** Possible states of the Garbage Collector
  30. */
  31. #define GCSpropagate    0
  32. #define GCSatomic       1
  33. #define GCSsweepstring  2
  34. #define GCSsweepudata   3
  35. #define GCSsweep        4
  36. #define GCSpause        5
  37.  
  38.  
  39. #define issweepphase(g)  \
  40.         (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep)
  41.  
  42. #define isgenerational(g)       ((g)->gckind == KGC_GEN)
  43.  
  44. /*
  45. ** macro to tell when main invariant (white objects cannot point to black
  46. ** ones) must be kept. During a non-generational collection, the sweep
  47. ** phase may break the invariant, as objects turned white may point to
  48. ** still-black objects. The invariant is restored when sweep ends and
  49. ** all objects are white again. During a generational collection, the
  50. ** invariant must be kept all times.
  51. */
  52. #define keepinvariant(g)  (isgenerational(g) || g->gcstate <= GCSatomic)
  53.  
  54.  
  55. #define gcstopped(g)    ((g)->GCdebt == MIN_LMEM)
  56. #define stopgc(g)       ((g)->GCdebt = MIN_LMEM)
  57.  
  58.  
  59. /*
  60. ** some useful bit tricks
  61. */
  62. #define resetbits(x,m)          ((x) &= cast(lu_byte, ~(m)))
  63. #define setbits(x,m)            ((x) |= (m))
  64. #define testbits(x,m)           ((x) & (m))
  65. #define bitmask(b)              (1<<(b))
  66. #define bit2mask(b1,b2)         (bitmask(b1) | bitmask(b2))
  67. #define l_setbit(x,b)           setbits(x, bitmask(b))
  68. #define resetbit(x,b)           resetbits(x, bitmask(b))
  69. #define testbit(x,b)            testbits(x, bitmask(b))
  70. #define set2bits(x,b1,b2)       setbits(x, (bit2mask(b1, b2)))
  71. #define reset2bits(x,b1,b2)     resetbits(x, (bit2mask(b1, b2)))
  72.  
  73.  
  74.  
  75. /* Layout for bit use in `marked' field: */
  76. #define WHITE0BIT       0  /* object is white (type 0) */
  77. #define WHITE1BIT       1  /* object is white (type 1) */
  78. #define BLACKBIT        2  /* object is black */
  79. #define FINALIZEDBIT    3  /* for userdata: has been finalized */
  80. #define SEPARATED       4  /*  "    ": it's in 'udgc' list or in 'tobefnz' */
  81. #define FIXEDBIT        5  /* object is fixed (should not be collected) */
  82. #define OLDBIT          6  /* object is old (only in generational mode) */
  83. /* bit 7 is currently used by tests (luaL_checkmemory) */
  84.  
  85. #define WHITEBITS       bit2mask(WHITE0BIT, WHITE1BIT)
  86.  
  87.  
  88. #define iswhite(x)      testbits((x)->gch.marked, WHITEBITS)
  89. #define isblack(x)      testbit((x)->gch.marked, BLACKBIT)
  90. #define isgray(x)  /* neither white nor black */  \
  91.         (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT)))
  92.  
  93. #define isold(x)        testbit((x)->gch.marked, OLDBIT)
  94.  
  95. /* MOVE OLD rule: whenever an object is moved to the beginning of
  96.    a GC list, its old bit must be cleared */
  97. #define resetoldbit(o)  resetbit((o)->gch.marked, OLDBIT)
  98.  
  99. #define otherwhite(g)   (g->currentwhite ^ WHITEBITS)
  100. #define isdeadm(ow,m)   (!(((m) ^ WHITEBITS) & (ow)))
  101. #define isdead(g,v)     isdeadm(otherwhite(g), (v)->gch.marked)
  102.  
  103. #define changewhite(x)  ((x)->gch.marked ^= WHITEBITS)
  104. #define gray2black(x)   l_setbit((x)->gch.marked, BLACKBIT)
  105.  
  106. #define valiswhite(x)   (iscollectable(x) && iswhite(gcvalue(x)))
  107.  
  108. #define luaC_white(g)   cast(lu_byte, (g)->currentwhite & WHITEBITS)
  109.  
  110.  
  111. #define luaC_checkGC(L) {condchangemem(L); if (G(L)->GCdebt > 0) luaC_step(L);}
  112.  
  113.  
  114. #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p)))  \
  115.         luaC_barrier_(L,obj2gco(p),gcvalue(v)); }
  116.  
  117. #define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p)))  \
  118.         luaC_barrierback_(L,p); }
  119.  
  120. #define luaC_objbarrier(L,p,o)  \
  121.         { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
  122.                 luaC_barrier_(L,obj2gco(p),obj2gco(o)); }
  123.  
  124. #define luaC_objbarrierback(L,p,o)  \
  125.    { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); }
  126.  
  127. #define luaC_barrierproto(L,p,c) \
  128.    { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); }
  129.  
  130. LUAI_FUNC void luaC_separateudata (lua_State *L, int all);
  131. LUAI_FUNC void luaC_freeallobjects (lua_State *L);
  132. LUAI_FUNC void luaC_step (lua_State *L);
  133. LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
  134. LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
  135. LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz,
  136.                                  GCObject **list, int offset);
  137. LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
  138. LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
  139. LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c);
  140. LUAI_FUNC void luaC_checkfinalizer (lua_State *L, Udata *u);
  141. LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv);
  142. LUAI_FUNC void luaC_changemode (lua_State *L, int mode);
  143.  
  144. #endif
  145.