1 /* 2 ** $Id$ 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 13 14 /* 15 ** Possible states of the Garbage Collector 16 */ 17 #define GCSpause 0 18 #define GCSpropagate 1 19 #define GCSsweepstring 2 20 #define GCSsweep 3 21 #define GCSfinalize 4 22 23 24 /* 25 ** some userful bit tricks 26 */ 27 #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 28 #define setbits(x,m) ((x) |= (m)) 29 #define testbits(x,m) ((x) & (m)) 30 #define bitmask(b) (1<<(b)) 31 #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 32 #define l_setbit(x,b) setbits(x, bitmask(b)) 33 #define resetbit(x,b) resetbits(x, bitmask(b)) 34 #define testbit(x,b) testbits(x, bitmask(b)) 35 #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) 36 #define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2))) 37 #define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2))) 38 39 40 41 /* 42 ** Layout for bit use in `marked' field: 43 ** bit 0 - object is white (type 0) 44 ** bit 1 - object is white (type 1) 45 ** bit 2 - object is black 46 ** bit 3 - for userdata: has been finalized 47 ** bit 3 - for tables: has weak keys 48 ** bit 4 - for tables: has weak values 49 ** bit 5 - object is fixed (should not be collected) 50 ** bit 6 - object is "super" fixed (only the main thread) 51 */ 52 53 54 #define WHITE0BIT 0 55 #define WHITE1BIT 1 56 #define BLACKBIT 2 57 #define FINALIZEDBIT 3 58 #define KEYWEAKBIT 3 59 #define VALUEWEAKBIT 4 60 #define FIXEDBIT 5 61 #define SFIXEDBIT 6 62 #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 63 64 65 #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) 66 #define isblack(x) testbit((x)->gch.marked, BLACKBIT) 67 #define isgray(x) (!isblack(x) && !iswhite(x)) 68 69 #define otherwhite(g) (g->currentwhite ^ WHITEBITS) 70 #define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) 71 72 #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 73 #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 74 75 #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 76 77 #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 78 79 80 #define luaC_checkGC(L) { \ 81 condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \ 82 if (G(L)->totalbytes >= G(L)->GCthreshold) \ 83 luaC_step(L); } 84 85 86 #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 87 luaC_barrierf(L,obj2gco(p),gcvalue(v)); } 88 89 #define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ 90 luaC_barrierback(L,t); } 91 92 #define luaC_objbarrier(L,p,o) \ 93 { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 94 luaC_barrierf(L,obj2gco(p),obj2gco(o)); } 95 96 #define luaC_objbarriert(L,t,o) \ 97 { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); } 98 99 LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all); 100 LUAI_FUNC void luaC_callGCTM (lua_State *L); 101 LUAI_FUNC void luaC_freeall (lua_State *L); 102 LUAI_FUNC void luaC_step (lua_State *L); 103 LUAI_FUNC void luaC_fullgc (lua_State *L); 104 LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt); 105 LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); 106 LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); 107 LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); 108 109 110 #endif 111