lgc.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. ** $Id: lgc.h,v 2.21 2009/06/08 19:35:59 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. /*
  10. ** Possible states of the Garbage Collector
  11. */
  12. #define GCSpause 0
  13. #define GCSpropagate 1
  14. #define GCSatomic 2
  15. #define GCSsweepstring 3
  16. #define GCSsweep 4
  17. #define GCSfinalize 5
  18. #define issweep(g) (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep)
  19. /*
  20. ** some userful bit tricks
  21. */
  22. #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
  23. #define setbits(x,m) ((x) |= (m))
  24. #define testbits(x,m) ((x) & (m))
  25. #define bitmask(b) (1<<(b))
  26. #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
  27. #define l_setbit(x,b) setbits(x, bitmask(b))
  28. #define resetbit(x,b) resetbits(x, bitmask(b))
  29. #define testbit(x,b) testbits(x, bitmask(b))
  30. #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2)))
  31. #define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2)))
  32. /*
  33. ** Layout for bit use in `marked' field:
  34. ** bit 0 - object is white (type 0)
  35. ** bit 1 - object is white (type 1)
  36. ** bit 2 - object is black
  37. ** bit 3 - for userdata: has been finalized
  38. ** bit 4 - for userdata: it's in 2nd part of rootgc list or in tobefnz
  39. ** bit 5 - object is fixed (should not be collected)
  40. ** bit 6 - object is "super" fixed (only the main thread)
  41. */
  42. #define WHITE0BIT 0
  43. #define WHITE1BIT 1
  44. #define BLACKBIT 2
  45. #define FINALIZEDBIT 3
  46. #define SEPARATED 4
  47. #define FIXEDBIT 5
  48. #define SFIXEDBIT 6
  49. #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
  50. #define iswhite(x) testbits((x)->gch.marked, WHITEBITS)
  51. #define isblack(x) testbit((x)->gch.marked, BLACKBIT)
  52. #define isgray(x) (!isblack(x) && !iswhite(x))
  53. #define otherwhite(g) (g->currentwhite ^ WHITEBITS)
  54. #define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS)
  55. #define changewhite(x) ((x)->gch.marked ^= WHITEBITS)
  56. #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
  57. #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
  58. #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
  59. #define luaC_checkGC(L) \
  60. {condchangemem(L); if (G(L)->totalbytes >= G(L)->GCthreshold) luaC_step(L);}
  61. #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
  62. luaC_barrierf(L,obj2gco(p),gcvalue(v)); }
  63. #define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \
  64. luaC_barrierback(L,t); }
  65. #define luaC_objbarrier(L,p,o) \
  66. { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
  67. luaC_barrierf(L,obj2gco(p),obj2gco(o)); }
  68. #define luaC_objbarriert(L,t,o) \
  69. { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); }
  70. LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all);
  71. LUAI_FUNC void luaC_callAllGCTM (lua_State *L);
  72. LUAI_FUNC void luaC_freeall (lua_State *L);
  73. LUAI_FUNC void luaC_step (lua_State *L);
  74. LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
  75. LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt);
  76. LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv);
  77. LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v);
  78. LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t);
  79. LUAI_FUNC void luaC_checkfinalizer (lua_State *L, Udata *u);
  80. #endif