Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-07 10:37:56

0001 /*
0002 ** $Id: lgc.h $
0003 ** Garbage Collector
0004 ** See Copyright Notice in lua.h
0005 */
0006 
0007 #ifndef lgc_h
0008 #define lgc_h
0009 
0010 
0011 #include "lobject.h"
0012 #include "lstate.h"
0013 
0014 /*
0015 ** Collectable objects may have one of three colors: white, which means
0016 ** the object is not marked; gray, which means the object is marked, but
0017 ** its references may be not marked; and black, which means that the
0018 ** object and all its references are marked.  The main invariant of the
0019 ** garbage collector, while marking objects, is that a black object can
0020 ** never point to a white one. Moreover, any gray object must be in a
0021 ** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it
0022 ** can be visited again before finishing the collection cycle. (Open
0023 ** upvalues are an exception to this rule.)  These lists have no meaning
0024 ** when the invariant is not being enforced (e.g., sweep phase).
0025 */
0026 
0027 
0028 /*
0029 ** Possible states of the Garbage Collector
0030 */
0031 #define GCSpropagate    0
0032 #define GCSenteratomic  1
0033 #define GCSatomic   2
0034 #define GCSswpallgc 3
0035 #define GCSswpfinobj    4
0036 #define GCSswptobefnz   5
0037 #define GCSswpend   6
0038 #define GCScallfin  7
0039 #define GCSpause    8
0040 
0041 
0042 #define issweepphase(g)  \
0043     (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
0044 
0045 
0046 /*
0047 ** macro to tell when main invariant (white objects cannot point to black
0048 ** ones) must be kept. During a collection, the sweep
0049 ** phase may break the invariant, as objects turned white may point to
0050 ** still-black objects. The invariant is restored when sweep ends and
0051 ** all objects are white again.
0052 */
0053 
0054 #define keepinvariant(g)    ((g)->gcstate <= GCSatomic)
0055 
0056 
0057 /*
0058 ** some useful bit tricks
0059 */
0060 #define resetbits(x,m)      ((x) &= cast_byte(~(m)))
0061 #define setbits(x,m)        ((x) |= (m))
0062 #define testbits(x,m)       ((x) & (m))
0063 #define bitmask(b)      (1<<(b))
0064 #define bit2mask(b1,b2)     (bitmask(b1) | bitmask(b2))
0065 #define l_setbit(x,b)       setbits(x, bitmask(b))
0066 #define resetbit(x,b)       resetbits(x, bitmask(b))
0067 #define testbit(x,b)        testbits(x, bitmask(b))
0068 
0069 
0070 /*
0071 ** Layout for bit use in 'marked' field. First three bits are
0072 ** used for object "age" in generational mode. Last bit is used
0073 ** by tests.
0074 */
0075 #define WHITE0BIT   3  /* object is white (type 0) */
0076 #define WHITE1BIT   4  /* object is white (type 1) */
0077 #define BLACKBIT    5  /* object is black */
0078 #define FINALIZEDBIT    6  /* object has been marked for finalization */
0079 
0080 #define TESTBIT     7
0081 
0082 
0083 
0084 #define WHITEBITS   bit2mask(WHITE0BIT, WHITE1BIT)
0085 
0086 
0087 #define iswhite(x)      testbits((x)->marked, WHITEBITS)
0088 #define isblack(x)      testbit((x)->marked, BLACKBIT)
0089 #define isgray(x)  /* neither white nor black */  \
0090     (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))
0091 
0092 #define tofinalize(x)   testbit((x)->marked, FINALIZEDBIT)
0093 
0094 #define otherwhite(g)   ((g)->currentwhite ^ WHITEBITS)
0095 #define isdeadm(ow,m)   ((m) & (ow))
0096 #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
0097 
0098 #define changewhite(x)  ((x)->marked ^= WHITEBITS)
0099 #define nw2black(x)  \
0100     check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT))
0101 
0102 #define luaC_white(g)   cast_byte((g)->currentwhite & WHITEBITS)
0103 
0104 
0105 /* object age in generational mode */
0106 #define G_NEW       0   /* created in current cycle */
0107 #define G_SURVIVAL  1   /* created in previous cycle */
0108 #define G_OLD0      2   /* marked old by frw. barrier in this cycle */
0109 #define G_OLD1      3   /* first full cycle as old */
0110 #define G_OLD       4   /* really old object (not to be visited) */
0111 #define G_TOUCHED1  5   /* old object touched this cycle */
0112 #define G_TOUCHED2  6   /* old object touched in previous cycle */
0113 
0114 #define AGEBITS     7  /* all age bits (111) */
0115 
0116 #define getage(o)   ((o)->marked & AGEBITS)
0117 #define setage(o,a)  ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a))
0118 #define isold(o)    (getage(o) > G_SURVIVAL)
0119 
0120 #define changeage(o,f,t)  \
0121     check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t)))
0122 
0123 
0124 /* Default Values for GC parameters */
0125 #define LUAI_GENMAJORMUL         100
0126 #define LUAI_GENMINORMUL         20
0127 
0128 /* wait memory to double before starting new cycle */
0129 #define LUAI_GCPAUSE    200
0130 
0131 /*
0132 ** some gc parameters are stored divided by 4 to allow a maximum value
0133 ** up to 1023 in a 'lu_byte'.
0134 */
0135 #define getgcparam(p)   ((p) * 4)
0136 #define setgcparam(p,v) ((p) = (v) / 4)
0137 
0138 #define LUAI_GCMUL      100
0139 
0140 /* how much to allocate before next GC step (log2) */
0141 #define LUAI_GCSTEPSIZE 13      /* 8 KB */
0142 
0143 
0144 /*
0145 ** Check whether the declared GC mode is generational. While in
0146 ** generational mode, the collector can go temporarily to incremental
0147 ** mode to improve performance. This is signaled by 'g->lastatomic != 0'.
0148 */
0149 #define isdecGCmodegen(g)   (g->gckind == KGC_GEN || g->lastatomic != 0)
0150 
0151 
0152 /*
0153 ** Control when GC is running:
0154 */
0155 #define GCSTPUSR    1  /* bit true when GC stopped by user */
0156 #define GCSTPGC     2  /* bit true when GC stopped by itself */
0157 #define GCSTPCLS    4  /* bit true when closing Lua state */
0158 #define gcrunning(g)    ((g)->gcstp == 0)
0159 
0160 
0161 /*
0162 ** Does one step of collection when debt becomes positive. 'pre'/'pos'
0163 ** allows some adjustments to be done only when needed. macro
0164 ** 'condchangemem' is used only for heavy tests (forcing a full
0165 ** GC cycle on every opportunity)
0166 */
0167 #define luaC_condGC(L,pre,pos) \
0168     { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
0169       condchangemem(L,pre,pos); }
0170 
0171 /* more often than not, 'pre'/'pos' are empty */
0172 #define luaC_checkGC(L)     luaC_condGC(L,(void)0,(void)0)
0173 
0174 
0175 #define luaC_objbarrier(L,p,o) (  \
0176     (isblack(p) && iswhite(o)) ? \
0177     luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
0178 
0179 #define luaC_barrier(L,p,v) (  \
0180     iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0))
0181 
0182 #define luaC_objbarrierback(L,p,o) (  \
0183     (isblack(p) && iswhite(o)) ? luaC_barrierback_(L,p) : cast_void(0))
0184 
0185 #define luaC_barrierback(L,p,v) (  \
0186     iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0))
0187 
0188 LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
0189 LUAI_FUNC void luaC_freeallobjects (lua_State *L);
0190 LUAI_FUNC void luaC_step (lua_State *L);
0191 LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
0192 LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
0193 LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
0194 LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz,
0195                                                  size_t offset);
0196 LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
0197 LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
0198 LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
0199 LUAI_FUNC void luaC_changemode (lua_State *L, int newmode);
0200 
0201 
0202 #endif