lgc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. ** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $
  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 GCSsweepstring 2
  15. #define GCSsweep 3
  16. #define GCSfinalize 4
  17. /*
  18. ** some userful bit tricks
  19. */
  20. #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
  21. #define setbits(x,m) ((x) |= (m))
  22. #define testbits(x,m) ((x) & (m))
  23. #define bitmask(b) (1<<(b))
  24. #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
  25. #define l_setbit(x,b) setbits(x, bitmask(b))
  26. #define resetbit(x,b) resetbits(x, bitmask(b))
  27. #define testbit(x,b) testbits(x, bitmask(b))
  28. #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2)))
  29. #define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2)))
  30. #define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2)))
  31. /*
  32. ** Possible Garbage Collector flags.
  33. ** Layout for bit use in 'gsflags' field in global_State structure.
  34. ** bit 0 - Protect GC from recursive calls.
  35. ** bit 1 - Don't try to shrink string table if EGC was called during a string table resize.
  36. */
  37. #define GCFlagsNone 0
  38. #define GCBlockGCBit 0
  39. #define GCResizingStringsBit 1
  40. #define is_block_gc(L) testbit(G(L)->gcflags, GCBlockGCBit)
  41. #define set_block_gc(L) l_setbit(G(L)->gcflags, GCBlockGCBit)
  42. #define unset_block_gc(L) resetbit(G(L)->gcflags, GCBlockGCBit)
  43. #define is_resizing_strings_gc(L) testbit(G(L)->gcflags, GCResizingStringsBit)
  44. #define set_resizing_strings_gc(L) l_setbit(G(L)->gcflags, GCResizingStringsBit)
  45. #define unset_resizing_strings_gc(L) resetbit(G(L)->gcflags, GCResizingStringsBit)
  46. /*
  47. ** Layout for bit use in `marked' field:
  48. ** bit 0 - object is white (type 0)
  49. ** bit 1 - object is white (type 1)
  50. ** bit 2 - object is black
  51. ** bit 3 - for thread: Don't resize thread's stack
  52. ** bit 3 - for userdata: has been finalized
  53. ** bit 3 - for tables: has weak keys
  54. ** bit 4 - for tables: has weak values
  55. ** bit 5 - object is fixed (should not be collected)
  56. ** bit 6 - object is "super" fixed (only the main thread)
  57. ** bit 7 - object is (partially) stored in read-only memory
  58. */
  59. #define WHITE0BIT 0
  60. #define WHITE1BIT 1
  61. #define BLACKBIT 2
  62. #define FIXEDSTACKBIT 3
  63. #define FINALIZEDBIT 3
  64. #define KEYWEAKBIT 3
  65. #define VALUEWEAKBIT 4
  66. #define FIXEDBIT 5
  67. #define SFIXEDBIT 6
  68. #define READONLYBIT 7
  69. #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
  70. #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
  71. #define isblack(x) testbit((x)->gch.marked, BLACKBIT)
  72. #define isgray(x) (!isblack(x) && !iswhite(x))
  73. #define otherwhite(g) (g->currentwhite ^ WHITEBITS)
  74. #define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS)
  75. #define changewhite(x) ((x)->gch.marked ^= WHITEBITS)
  76. #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
  77. #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
  78. #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
  79. #define isfixedstack(x) testbit((x)->marked, FIXEDSTACKBIT)
  80. #define fixedstack(x) l_setbit((x)->marked, FIXEDSTACKBIT)
  81. #define unfixedstack(x) resetbit((x)->marked, FIXEDSTACKBIT)
  82. #define luaC_checkGC(L) { \
  83. condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \
  84. if (G(L)->totalbytes >= G(L)->GCthreshold) \
  85. luaC_step(L); }
  86. #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
  87. luaC_barrierf(L,obj2gco(p),gcvalue(v)); }
  88. #define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \
  89. luaC_barrierback(L,t); }
  90. #define luaC_objbarrier(L,p,o) \
  91. { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
  92. luaC_barrierf(L,obj2gco(p),obj2gco(o)); }
  93. #define luaC_objbarriert(L,t,o) \
  94. { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); }
  95. LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all);
  96. LUAI_FUNC void luaC_callGCTM (lua_State *L);
  97. LUAI_FUNC void luaC_freeall (lua_State *L);
  98. LUAI_FUNC void luaC_step (lua_State *L);
  99. LUAI_FUNC void luaC_fullgc (lua_State *L);
  100. LUAI_FUNC int luaC_sweepstrgc (lua_State *L);
  101. LUAI_FUNC void luaC_marknew (lua_State *L, GCObject *o);
  102. LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt);
  103. LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv);
  104. LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v);
  105. LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t);
  106. #endif