lgc.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. ** $Id: lgc.h,v 2.5 2004/03/15 21:04:33 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 GCSpropagate 0
  13. #define GCSsweepstring 1
  14. #define GCSsweep 2
  15. #define GCSfinalize 3
  16. /*
  17. ** some userful bit tricks
  18. */
  19. #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
  20. #define setbits(x,m) ((x) |= (m))
  21. #define testbits(x,m) ((x) & (m))
  22. #define bitmask(b) (1<<(b))
  23. #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
  24. #define setbit(x,b) setbits(x, bitmask(b))
  25. #define resetbit(x,b) resetbits(x, bitmask(b))
  26. #define testbit(x,b) testbits(x, bitmask(b))
  27. #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2)))
  28. #define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2)))
  29. #define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2)))
  30. /*
  31. ** Layout for bit use in `marked' field:
  32. ** bit 0 - object is gray
  33. ** bit 1 - object is black
  34. ** bit 2 - For userdata: is finalized;
  35. for tables: has weak keys
  36. ** bit 3 - for tables: has weak values
  37. ** bit 4 - object is fixed (should not be collected)
  38. */
  39. #define WHITE0BIT 0
  40. #define WHITE1BIT 1
  41. #define BLACKBIT 2
  42. #define FINALIZEDBIT 3
  43. #define KEYWEAKBIT 3
  44. #define VALUEWEAKBIT 4
  45. #define FIXEDBIT 5
  46. #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
  47. #define isblack(x) testbit((x)->gch.marked, BLACKBIT)
  48. #define isgray(x) (!isblack(x) && !iswhite(x))
  49. #define otherwhite(g) (g->currentwhite ^ bit2mask(WHITE0BIT, WHITE1BIT))
  50. #define isdead(g,v) ((v)->gch.marked & otherwhite(g))
  51. #define changewhite(x) ((x)->gch.marked ^= bit2mask(WHITE0BIT, WHITE1BIT))
  52. #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
  53. #define luaC_white(g) cast(lu_byte, (g)->currentwhite)
  54. #define luaC_checkGC(L) { if (G(L)->nblocks >= G(L)->GCthreshold) \
  55. luaC_step(L); }
  56. #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
  57. luaC_barrierf(L,obj2gco(p),gcvalue(v)); }
  58. #define luaC_barriert(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
  59. luaC_barrierback(L,obj2gco(p),gcvalue(v)); }
  60. #define luaC_objbarrier(L,p,o) \
  61. { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
  62. luaC_barrierf(L,obj2gco(p),obj2gco(o)); }
  63. size_t luaC_separateudata (lua_State *L, int all);
  64. void luaC_callGCTM (lua_State *L);
  65. void luaC_sweepall (lua_State *L);
  66. void luaC_step (lua_State *L);
  67. void luaC_fullgc (lua_State *L);
  68. void luaC_link (lua_State *L, GCObject *o, lu_byte tt);
  69. void luaC_linkupval (lua_State *L, UpVal *uv);
  70. void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v);
  71. void luaC_barrierback (lua_State *L, GCObject *o, GCObject *v);
  72. #endif