18e3e3a7aSWarner Losh /* 20495ed39SKyle Evans ** $Id: lgc.h $ 38e3e3a7aSWarner Losh ** Garbage Collector 48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h 58e3e3a7aSWarner Losh */ 68e3e3a7aSWarner Losh 78e3e3a7aSWarner Losh #ifndef lgc_h 88e3e3a7aSWarner Losh #define lgc_h 98e3e3a7aSWarner Losh 108e3e3a7aSWarner Losh 118e3e3a7aSWarner Losh #include "lobject.h" 128e3e3a7aSWarner Losh #include "lstate.h" 138e3e3a7aSWarner Losh 148e3e3a7aSWarner Losh /* 150495ed39SKyle Evans ** Collectable objects may have one of three colors: white, which means 160495ed39SKyle Evans ** the object is not marked; gray, which means the object is marked, but 170495ed39SKyle Evans ** its references may be not marked; and black, which means that the 180495ed39SKyle Evans ** object and all its references are marked. The main invariant of the 190495ed39SKyle Evans ** garbage collector, while marking objects, is that a black object can 200495ed39SKyle Evans ** never point to a white one. Moreover, any gray object must be in a 210495ed39SKyle Evans ** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it 220495ed39SKyle Evans ** can be visited again before finishing the collection cycle. (Open 230495ed39SKyle Evans ** upvalues are an exception to this rule.) These lists have no meaning 240495ed39SKyle Evans ** when the invariant is not being enforced (e.g., sweep phase). 258e3e3a7aSWarner Losh */ 268e3e3a7aSWarner Losh 278e3e3a7aSWarner Losh 288e3e3a7aSWarner Losh /* 298e3e3a7aSWarner Losh ** Possible states of the Garbage Collector 308e3e3a7aSWarner Losh */ 318e3e3a7aSWarner Losh #define GCSpropagate 0 320495ed39SKyle Evans #define GCSenteratomic 1 330495ed39SKyle Evans #define GCSatomic 2 340495ed39SKyle Evans #define GCSswpallgc 3 350495ed39SKyle Evans #define GCSswpfinobj 4 360495ed39SKyle Evans #define GCSswptobefnz 5 370495ed39SKyle Evans #define GCSswpend 6 380495ed39SKyle Evans #define GCScallfin 7 390495ed39SKyle Evans #define GCSpause 8 408e3e3a7aSWarner Losh 418e3e3a7aSWarner Losh 428e3e3a7aSWarner Losh #define issweepphase(g) \ 438e3e3a7aSWarner Losh (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) 448e3e3a7aSWarner Losh 458e3e3a7aSWarner Losh 468e3e3a7aSWarner Losh /* 478e3e3a7aSWarner Losh ** macro to tell when main invariant (white objects cannot point to black 488e3e3a7aSWarner Losh ** ones) must be kept. During a collection, the sweep 498e3e3a7aSWarner Losh ** phase may break the invariant, as objects turned white may point to 508e3e3a7aSWarner Losh ** still-black objects. The invariant is restored when sweep ends and 518e3e3a7aSWarner Losh ** all objects are white again. 528e3e3a7aSWarner Losh */ 538e3e3a7aSWarner Losh 548e3e3a7aSWarner Losh #define keepinvariant(g) ((g)->gcstate <= GCSatomic) 558e3e3a7aSWarner Losh 568e3e3a7aSWarner Losh 578e3e3a7aSWarner Losh /* 588e3e3a7aSWarner Losh ** some useful bit tricks 598e3e3a7aSWarner Losh */ 600495ed39SKyle Evans #define resetbits(x,m) ((x) &= cast_byte(~(m))) 618e3e3a7aSWarner Losh #define setbits(x,m) ((x) |= (m)) 628e3e3a7aSWarner Losh #define testbits(x,m) ((x) & (m)) 638e3e3a7aSWarner Losh #define bitmask(b) (1<<(b)) 648e3e3a7aSWarner Losh #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 658e3e3a7aSWarner Losh #define l_setbit(x,b) setbits(x, bitmask(b)) 668e3e3a7aSWarner Losh #define resetbit(x,b) resetbits(x, bitmask(b)) 678e3e3a7aSWarner Losh #define testbit(x,b) testbits(x, bitmask(b)) 688e3e3a7aSWarner Losh 698e3e3a7aSWarner Losh 700495ed39SKyle Evans /* 710495ed39SKyle Evans ** Layout for bit use in 'marked' field. First three bits are 720495ed39SKyle Evans ** used for object "age" in generational mode. Last bit is used 730495ed39SKyle Evans ** by tests. 740495ed39SKyle Evans */ 750495ed39SKyle Evans #define WHITE0BIT 3 /* object is white (type 0) */ 760495ed39SKyle Evans #define WHITE1BIT 4 /* object is white (type 1) */ 770495ed39SKyle Evans #define BLACKBIT 5 /* object is black */ 780495ed39SKyle Evans #define FINALIZEDBIT 6 /* object has been marked for finalization */ 790495ed39SKyle Evans 800495ed39SKyle Evans #define TESTBIT 7 810495ed39SKyle Evans 820495ed39SKyle Evans 838e3e3a7aSWarner Losh 848e3e3a7aSWarner Losh #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 858e3e3a7aSWarner Losh 868e3e3a7aSWarner Losh 878e3e3a7aSWarner Losh #define iswhite(x) testbits((x)->marked, WHITEBITS) 888e3e3a7aSWarner Losh #define isblack(x) testbit((x)->marked, BLACKBIT) 898e3e3a7aSWarner Losh #define isgray(x) /* neither white nor black */ \ 908e3e3a7aSWarner Losh (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) 918e3e3a7aSWarner Losh 928e3e3a7aSWarner Losh #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) 938e3e3a7aSWarner Losh 948e3e3a7aSWarner Losh #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) 950495ed39SKyle Evans #define isdeadm(ow,m) ((m) & (ow)) 968e3e3a7aSWarner Losh #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) 978e3e3a7aSWarner Losh 988e3e3a7aSWarner Losh #define changewhite(x) ((x)->marked ^= WHITEBITS) 990495ed39SKyle Evans #define nw2black(x) \ 1000495ed39SKyle Evans check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT)) 1018e3e3a7aSWarner Losh 1020495ed39SKyle Evans #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS) 1038e3e3a7aSWarner Losh 1048e3e3a7aSWarner Losh 1050495ed39SKyle Evans /* object age in generational mode */ 1060495ed39SKyle Evans #define G_NEW 0 /* created in current cycle */ 1070495ed39SKyle Evans #define G_SURVIVAL 1 /* created in previous cycle */ 1080495ed39SKyle Evans #define G_OLD0 2 /* marked old by frw. barrier in this cycle */ 1090495ed39SKyle Evans #define G_OLD1 3 /* first full cycle as old */ 1100495ed39SKyle Evans #define G_OLD 4 /* really old object (not to be visited) */ 1110495ed39SKyle Evans #define G_TOUCHED1 5 /* old object touched this cycle */ 1120495ed39SKyle Evans #define G_TOUCHED2 6 /* old object touched in previous cycle */ 1130495ed39SKyle Evans 1140495ed39SKyle Evans #define AGEBITS 7 /* all age bits (111) */ 1150495ed39SKyle Evans 1160495ed39SKyle Evans #define getage(o) ((o)->marked & AGEBITS) 1170495ed39SKyle Evans #define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a)) 1180495ed39SKyle Evans #define isold(o) (getage(o) > G_SURVIVAL) 1190495ed39SKyle Evans 1200495ed39SKyle Evans #define changeage(o,f,t) \ 1210495ed39SKyle Evans check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) 1220495ed39SKyle Evans 1230495ed39SKyle Evans 1240495ed39SKyle Evans /* Default Values for GC parameters */ 1250495ed39SKyle Evans #define LUAI_GENMAJORMUL 100 1260495ed39SKyle Evans #define LUAI_GENMINORMUL 20 1270495ed39SKyle Evans 1280495ed39SKyle Evans /* wait memory to double before starting new cycle */ 1290495ed39SKyle Evans #define LUAI_GCPAUSE 200 1300495ed39SKyle Evans 1310495ed39SKyle Evans /* 1320495ed39SKyle Evans ** some gc parameters are stored divided by 4 to allow a maximum value 1330495ed39SKyle Evans ** up to 1023 in a 'lu_byte'. 1340495ed39SKyle Evans */ 1350495ed39SKyle Evans #define getgcparam(p) ((p) * 4) 1360495ed39SKyle Evans #define setgcparam(p,v) ((p) = (v) / 4) 1370495ed39SKyle Evans 1380495ed39SKyle Evans #define LUAI_GCMUL 100 1390495ed39SKyle Evans 1400495ed39SKyle Evans /* how much to allocate before next GC step (log2) */ 1410495ed39SKyle Evans #define LUAI_GCSTEPSIZE 13 /* 8 KB */ 1420495ed39SKyle Evans 1430495ed39SKyle Evans 1440495ed39SKyle Evans /* 1450495ed39SKyle Evans ** Check whether the declared GC mode is generational. While in 1460495ed39SKyle Evans ** generational mode, the collector can go temporarily to incremental 1470495ed39SKyle Evans ** mode to improve performance. This is signaled by 'g->lastatomic != 0'. 1480495ed39SKyle Evans */ 1490495ed39SKyle Evans #define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0) 1500495ed39SKyle Evans 1518c784bb8SWarner Losh 1528c784bb8SWarner Losh /* 1538c784bb8SWarner Losh ** Control when GC is running: 1548c784bb8SWarner Losh */ 1558c784bb8SWarner Losh #define GCSTPUSR 1 /* bit true when GC stopped by user */ 1568c784bb8SWarner Losh #define GCSTPGC 2 /* bit true when GC stopped by itself */ 1578c784bb8SWarner Losh #define GCSTPCLS 4 /* bit true when closing Lua state */ 1588c784bb8SWarner Losh #define gcrunning(g) ((g)->gcstp == 0) 1598c784bb8SWarner Losh 1608c784bb8SWarner Losh 1618e3e3a7aSWarner Losh /* 1628e3e3a7aSWarner Losh ** Does one step of collection when debt becomes positive. 'pre'/'pos' 1638e3e3a7aSWarner Losh ** allows some adjustments to be done only when needed. macro 1648e3e3a7aSWarner Losh ** 'condchangemem' is used only for heavy tests (forcing a full 1658e3e3a7aSWarner Losh ** GC cycle on every opportunity) 1668e3e3a7aSWarner Losh */ 1678e3e3a7aSWarner Losh #define luaC_condGC(L,pre,pos) \ 1688e3e3a7aSWarner Losh { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ 1698e3e3a7aSWarner Losh condchangemem(L,pre,pos); } 1708e3e3a7aSWarner Losh 1718e3e3a7aSWarner Losh /* more often than not, 'pre'/'pos' are empty */ 1728e3e3a7aSWarner Losh #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) 1738e3e3a7aSWarner Losh 1748e3e3a7aSWarner Losh 1758e3e3a7aSWarner Losh #define luaC_objbarrier(L,p,o) ( \ 1768e3e3a7aSWarner Losh (isblack(p) && iswhite(o)) ? \ 1778e3e3a7aSWarner Losh luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) 1788e3e3a7aSWarner Losh 179*a9490b81SWarner Losh #define luaC_barrier(L,p,v) ( \ 180*a9490b81SWarner Losh iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0)) 181*a9490b81SWarner Losh 182*a9490b81SWarner Losh #define luaC_objbarrierback(L,p,o) ( \ 183*a9490b81SWarner Losh (isblack(p) && iswhite(o)) ? luaC_barrierback_(L,p) : cast_void(0)) 184*a9490b81SWarner Losh 185*a9490b81SWarner Losh #define luaC_barrierback(L,p,v) ( \ 186*a9490b81SWarner Losh iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0)) 187*a9490b81SWarner Losh 1888e3e3a7aSWarner Losh LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); 1898e3e3a7aSWarner Losh LUAI_FUNC void luaC_freeallobjects (lua_State *L); 1908e3e3a7aSWarner Losh LUAI_FUNC void luaC_step (lua_State *L); 1918e3e3a7aSWarner Losh LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 1928e3e3a7aSWarner Losh LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 1938e3e3a7aSWarner Losh LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); 194*a9490b81SWarner Losh LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, 195*a9490b81SWarner Losh size_t offset); 1968e3e3a7aSWarner Losh LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 1970495ed39SKyle Evans LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); 1988e3e3a7aSWarner Losh LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 1990495ed39SKyle Evans LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); 2008e3e3a7aSWarner Losh 2018e3e3a7aSWarner Losh 2028e3e3a7aSWarner Losh #endif 203