rlm@1: /* rlm@1: ** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $ rlm@1: ** Garbage Collector rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: #ifndef lgc_h rlm@1: #define lgc_h rlm@1: rlm@1: rlm@1: #include "lobject.h" rlm@1: rlm@1: rlm@1: /* rlm@1: ** Possible states of the Garbage Collector rlm@1: */ rlm@1: #define GCSpause 0 rlm@1: #define GCSpropagate 1 rlm@1: #define GCSsweepstring 2 rlm@1: #define GCSsweep 3 rlm@1: #define GCSfinalize 4 rlm@1: rlm@1: rlm@1: /* rlm@1: ** some userful bit tricks rlm@1: */ rlm@1: #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) rlm@1: #define setbits(x,m) ((x) |= (m)) rlm@1: #define testbits(x,m) ((x) & (m)) rlm@1: #define bitmask(b) (1<<(b)) rlm@1: #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) rlm@1: #define l_setbit(x,b) setbits(x, bitmask(b)) rlm@1: #define resetbit(x,b) resetbits(x, bitmask(b)) rlm@1: #define testbit(x,b) testbits(x, bitmask(b)) rlm@1: #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) rlm@1: #define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2))) rlm@1: #define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2))) rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** Layout for bit use in `marked' field: rlm@1: ** bit 0 - object is white (type 0) rlm@1: ** bit 1 - object is white (type 1) rlm@1: ** bit 2 - object is black rlm@1: ** bit 3 - for userdata: has been finalized rlm@1: ** bit 3 - for tables: has weak keys rlm@1: ** bit 4 - for tables: has weak values rlm@1: ** bit 5 - object is fixed (should not be collected) rlm@1: ** bit 6 - object is "super" fixed (only the main thread) rlm@1: */ rlm@1: rlm@1: rlm@1: #define WHITE0BIT 0 rlm@1: #define WHITE1BIT 1 rlm@1: #define BLACKBIT 2 rlm@1: #define FINALIZEDBIT 3 rlm@1: #define KEYWEAKBIT 3 rlm@1: #define VALUEWEAKBIT 4 rlm@1: #define FIXEDBIT 5 rlm@1: #define SFIXEDBIT 6 rlm@1: #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) rlm@1: rlm@1: rlm@1: #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) rlm@1: #define isblack(x) testbit((x)->gch.marked, BLACKBIT) rlm@1: #define isgray(x) (!isblack(x) && !iswhite(x)) rlm@1: rlm@1: #define otherwhite(g) (g->currentwhite ^ WHITEBITS) rlm@1: #define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) rlm@1: rlm@1: #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) rlm@1: #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) rlm@1: rlm@1: #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) rlm@1: rlm@1: #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) rlm@1: rlm@1: rlm@1: #define luaC_checkGC(L) { \ rlm@1: condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \ rlm@1: if (G(L)->totalbytes >= G(L)->GCthreshold) \ rlm@1: luaC_step(L); } rlm@1: rlm@1: rlm@1: #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ rlm@1: luaC_barrierf(L,obj2gco(p),gcvalue(v)); } rlm@1: rlm@1: #define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ rlm@1: luaC_barrierback(L,t); } rlm@1: rlm@1: #define luaC_objbarrier(L,p,o) \ rlm@1: { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ rlm@1: luaC_barrierf(L,obj2gco(p),obj2gco(o)); } rlm@1: rlm@1: #define luaC_objbarriert(L,t,o) \ rlm@1: { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); } rlm@1: rlm@1: LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all); rlm@1: LUAI_FUNC void luaC_callGCTM (lua_State *L); rlm@1: LUAI_FUNC void luaC_freeall (lua_State *L); rlm@1: LUAI_FUNC void luaC_step (lua_State *L); rlm@1: LUAI_FUNC void luaC_fullgc (lua_State *L); rlm@1: LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt); rlm@1: LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); rlm@1: LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); rlm@1: LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); rlm@1: rlm@1: rlm@1: #endif