The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lgc.h
Go to the documentation of this file.
1 /*
2 ** Garbage Collector
3 ** See Copyright Notice in lua.h
4 */
5 
6 #ifndef lgc_h
7 #define lgc_h
8 
9 
10 #include "lobject.h"
11 #include "lstate.h"
12 
13 /*
14 ** Collectable objects may have one of three colors: white, which
15 ** means the object is not marked; gray, which means the
16 ** object is marked, but its references may be not marked; and
17 ** black, which means that the object and all its references are marked.
18 ** The main invariant of the garbage collector, while marking objects,
19 ** is that a black object can never point to a white one. Moreover,
20 ** any gray object must be in a "gray list" (gray, grayagain, weak,
21 ** allweak, ephemeron) so that it can be visited again before finishing
22 ** the collection cycle. These lists have no meaning when the invariant
23 ** is not being enforced (e.g., sweep phase).
24 */
25 
26 
27 
28 /* how much to allocate before next GC step */
29 #if !defined(GCSTEPSIZE)
30 /* ~100 small strings */
31 #define GCSTEPSIZE (cast_int(100 * sizeof(TString)))
32 #endif
33 
34 
35 /*
36 ** Possible states of the Garbage Collector
37 */
38 #define GCSpropagate 0
39 #define GCSatomic 1
40 #define GCSsweepstring 2
41 #define GCSsweepudata 3
42 #define GCSsweep 4
43 #define GCSpause 5
44 
45 
46 #define issweepphase(g) \
47  (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep)
48 
49 #define isgenerational(g) ((g)->gckind == KGC_GEN)
50 
51 /*
52 ** macros to tell when main invariant (white objects cannot point to black
53 ** ones) must be kept. During a non-generational collection, the sweep
54 ** phase may break the invariant, as objects turned white may point to
55 ** still-black objects. The invariant is restored when sweep ends and
56 ** all objects are white again. During a generational collection, the
57 ** invariant must be kept all times.
58 */
59 
60 #define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic)
61 
62 
63 /*
64 ** Outside the collector, the state in generational mode is kept in
65 ** 'propagate', so 'keepinvariant' is always true.
66 */
67 #define keepinvariantout(g) \
68  check_exp(g->gcstate == GCSpropagate || !isgenerational(g), \
69  g->gcstate <= GCSatomic)
70 
71 
72 /*
73 ** some useful bit tricks
74 */
75 #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
76 #define setbits(x,m) ((x) |= (m))
77 #define testbits(x,m) ((x) & (m))
78 #define bitmask(b) (1<<(b))
79 #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
80 #define l_setbit(x,b) setbits(x, bitmask(b))
81 #define resetbit(x,b) resetbits(x, bitmask(b))
82 #define testbit(x,b) testbits(x, bitmask(b))
83 
84 
85 /* Layout for bit use in `marked' field: */
86 #define WHITE0BIT 0 /* object is white (type 0) */
87 #define WHITE1BIT 1 /* object is white (type 1) */
88 #define BLACKBIT 2 /* object is black */
89 #define FINALIZEDBIT 3 /* object has been separated for finalization */
90 #define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */
91 #define FIXEDBIT 5 /* object is fixed (should not be collected) */
92 #define OLDBIT 6 /* object is old (only in generational mode) */
93 /* bit 7 is currently used by tests (luaL_checkmemory) */
94 
95 #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
96 
97 
98 #define iswhite(x) testbits((x)->gch.marked, WHITEBITS)
99 #define isblack(x) testbit((x)->gch.marked, BLACKBIT)
100 #define isgray(x) /* neither white nor black */ \
101  (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT)))
102 
103 #define isold(x) testbit((x)->gch.marked, OLDBIT)
104 
105 /* MOVE OLD rule: whenever an object is moved to the beginning of
106  a GC list, its old bit must be cleared */
107 #define resetoldbit(o) resetbit((o)->gch.marked, OLDBIT)
108 
109 #define otherwhite(g) (g->currentwhite ^ WHITEBITS)
110 #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow)))
111 #define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked)
112 
113 #define changewhite(x) ((x)->gch.marked ^= WHITEBITS)
114 #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
115 
116 #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
117 
118 #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
119 
120 
121 #define luaC_condGC(L,c) \
122  {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);}
123 #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);)
124 
125 
126 #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
127  luaC_barrier_(L,obj2gco(p),gcvalue(v)); }
128 
129 #define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
130  luaC_barrierback_(L,p); }
131 
132 #define luaC_objbarrier(L,p,o) \
133  { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
134  luaC_barrier_(L,obj2gco(p),obj2gco(o)); }
135 
136 #define luaC_objbarrierback(L,p,o) \
137  { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); }
138 
139 #define luaC_barrierproto(L,p,c) \
140  { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); }
141 
143 LUAI_FUNC void luaC_step (lua_State *L);
145 LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
146 LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
147 LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz,
148  GCObject **list, int offset);
154 LUAI_FUNC void luaC_changemode (lua_State *L, int mode);
155 
156 #endif
Definition: lobject.h:559
Definition: lobject.h:466
const GLfloat * c
Definition: glew.h:12741
#define LUAI_FUNC
Definition: luaconf.h:187
LUAI_FUNC void luaC_runtilstate(lua_State *L, int statesmask)
Definition: lgc.cpp:1109
GLboolean GLboolean g
Definition: glew.h:7319
LUAI_FUNC void luaC_step(lua_State *L)
Definition: lgc.cpp:1176
GLenum mode
Definition: glew.h:2390
LUAI_FUNC void luaC_checkfinalizer(lua_State *L, GCObject *o, Table *mt)
Definition: lgc.cpp:872
GLintptr offset
Definition: glew.h:1650
LUAI_FUNC GCObject * luaC_newobj(lua_State *L, int tt, size_t sz, GCObject **list, int offset)
Definition: lgc.cpp:211
LUAI_FUNC void luaC_checkupvalcolor(global_State *g, UpVal *uv)
Definition: lgc.cpp:189
Definition: lobject.h:495
const GLdouble * v
Definition: glew.h:1359
LUAI_FUNC void luaC_freeallobjects(lua_State *L)
Definition: lgc.cpp:982
LUAI_FUNC void luaC_fullgc(lua_State *L, int isemergency)
Definition: lgc.cpp:1188
GLfloat GLfloat p
Definition: glew.h:12766
LUAI_FUNC void luaC_barrierproto_(lua_State *L, Proto *p, Closure *c)
Definition: lgc.cpp:171
LUAI_FUNC void luaC_barrierback_(lua_State *L, GCObject *o)
Definition: lgc.cpp:154
LUAI_FUNC void luaC_forcestep(lua_State *L)
Definition: lgc.cpp:1162
LUAI_FUNC void luaC_changemode(lua_State *L, int mode)
Definition: lgc.cpp:951
LUAI_FUNC void luaC_barrier_(lua_State *L, GCObject *o, GCObject *v)
Definition: lgc.cpp:134