lua/lgc.h

Go to the documentation of this file.
00001 /*
00002 ** $Id: lgc.h,v 2.52 2011/10/03 17:54:25 roberto Exp $
00003 ** Garbage Collector
00004 ** See Copyright Notice in lua.h
00005 */
00006 
00007 #ifndef lgc_h
00008 #define lgc_h
00009 
00010 
00011 #include "lobject.h"
00012 #include "lstate.h"
00013 
00014 /*
00015 ** Collectable objects may have one of three colors: white, which
00016 ** means the object is not marked; gray, which means the
00017 ** object is marked, but its references may be not marked; and
00018 ** black, which means that the object and all its references are marked.
00019 ** The main invariant of the garbage collector, while marking objects,
00020 ** is that a black object can never point to a white one. Moreover,
00021 ** any gray object must be in a "gray list" (gray, grayagain, weak,
00022 ** allweak, ephemeron) so that it can be visited again before finishing
00023 ** the collection cycle. These lists have no meaning when the invariant
00024 ** is not being enforced (e.g., sweep phase).
00025 */
00026 
00027 
00028 /*
00029 ** Possible states of the Garbage Collector
00030 */
00031 #define GCSpropagate    0
00032 #define GCSatomic   1
00033 #define GCSsweepstring  2
00034 #define GCSsweepudata   3
00035 #define GCSsweep    4
00036 #define GCSpause    5
00037 
00038 
00039 #define issweepphase(g)  \
00040     (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep)
00041 
00042 #define isgenerational(g)   ((g)->gckind == KGC_GEN)
00043 
00044 /*
00045 ** macro to tell when main invariant (white objects cannot point to black
00046 ** ones) must be kept. During a non-generational collection, the sweep
00047 ** phase may break the invariant, as objects turned white may point to
00048 ** still-black objects. The invariant is restored when sweep ends and
00049 ** all objects are white again. During a generational collection, the
00050 ** invariant must be kept all times.
00051 */
00052 #define keepinvariant(g)  (isgenerational(g) || g->gcstate <= GCSatomic)
00053 
00054 
00055 /*
00056 ** some useful bit tricks
00057 */
00058 #define resetbits(x,m)      ((x) &= cast(lu_byte, ~(m)))
00059 #define setbits(x,m)        ((x) |= (m))
00060 #define testbits(x,m)       ((x) & (m))
00061 #define bitmask(b)      (1<<(b))
00062 #define bit2mask(b1,b2)     (bitmask(b1) | bitmask(b2))
00063 #define l_setbit(x,b)       setbits(x, bitmask(b))
00064 #define resetbit(x,b)       resetbits(x, bitmask(b))
00065 #define testbit(x,b)        testbits(x, bitmask(b))
00066 
00067 
00068 /* Layout for bit use in `marked' field: */
00069 #define WHITE0BIT   0  /* object is white (type 0) */
00070 #define WHITE1BIT   1  /* object is white (type 1) */
00071 #define BLACKBIT    2  /* object is black */
00072 #define FINALIZEDBIT    3  /* object has been separated for finalization */
00073 #define SEPARATED   4  /* object is in 'finobj' list or in 'tobefnz' */
00074 #define FIXEDBIT    5  /* object is fixed (should not be collected) */
00075 #define OLDBIT      6  /* object is old (only in generational mode) */
00076 /* bit 7 is currently used by tests (luaL_checkmemory) */
00077 
00078 #define WHITEBITS   bit2mask(WHITE0BIT, WHITE1BIT)
00079 
00080 
00081 #define iswhite(x)      testbits((x)->gch.marked, WHITEBITS)
00082 #define isblack(x)      testbit((x)->gch.marked, BLACKBIT)
00083 #define isgray(x)  /* neither white nor black */  \
00084     (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT)))
00085 
00086 #define isold(x)    testbit((x)->gch.marked, OLDBIT)
00087 
00088 /* MOVE OLD rule: whenever an object is moved to the beginning of
00089    a GC list, its old bit must be cleared */
00090 #define resetoldbit(o)  resetbit((o)->gch.marked, OLDBIT)
00091 
00092 #define otherwhite(g)   (g->currentwhite ^ WHITEBITS)
00093 #define isdeadm(ow,m)   (!(((m) ^ WHITEBITS) & (ow)))
00094 #define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked)
00095 
00096 #define changewhite(x)  ((x)->gch.marked ^= WHITEBITS)
00097 #define gray2black(x)   l_setbit((x)->gch.marked, BLACKBIT)
00098 
00099 #define valiswhite(x)   (iscollectable(x) && iswhite(gcvalue(x)))
00100 
00101 #define luaC_white(g)   cast(lu_byte, (g)->currentwhite & WHITEBITS)
00102 
00103 
00104 #define luaC_condGC(L,c) \
00105     {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);}
00106 #define luaC_checkGC(L)     luaC_condGC(L, luaC_step(L);)
00107 
00108 
00109 #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p)))  \
00110     luaC_barrier_(L,obj2gco(p),gcvalue(v)); }
00111 
00112 #define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p)))  \
00113     luaC_barrierback_(L,p); }
00114 
00115 #define luaC_objbarrier(L,p,o)  \
00116     { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
00117         luaC_barrier_(L,obj2gco(p),obj2gco(o)); }
00118 
00119 #define luaC_objbarrierback(L,p,o)  \
00120    { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); }
00121 
00122 #define luaC_barrierproto(L,p,c) \
00123    { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); }
00124 
00125 LUAI_FUNC void luaC_freeallobjects (lua_State *L);
00126 LUAI_FUNC void luaC_step (lua_State *L);
00127 LUAI_FUNC void luaC_forcestep (lua_State *L);
00128 LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
00129 LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
00130 LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz,
00131                                  GCObject **list, int offset);
00132 LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
00133 LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
00134 LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c);
00135 LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
00136 LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv);
00137 LUAI_FUNC void luaC_changemode (lua_State *L, int mode);
00138 
00139 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:03 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs