native Lua  0.5.0-devel
Lua on the platform you use with the compiler you choose
lgc.h
Go to the documentation of this file.
1 /*
2 ** $Id: lgc.h $
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 #include "lstate.h"
13 
14 #include "_native_lua_config.h" /* native Lua */
15 
16 
17 /*
18 ** Collectable objects may have one of three colors: white, which
19 ** means the object is not marked; gray, which means the
20 ** object is marked, but its references may be not marked; and
21 ** black, which means that the object and all its references are marked.
22 ** The main invariant of the garbage collector, while marking objects,
23 ** is that a black object can never point to a white one. Moreover,
24 ** any gray object must be in a "gray list" (gray, grayagain, weak,
25 ** allweak, ephemeron) so that it can be visited again before finishing
26 ** the collection cycle. These lists have no meaning when the invariant
27 ** is not being enforced (e.g., sweep phase).
28 */
29 
30 
31 /*
32 ** Possible states of the Garbage Collector
33 */
34 #define GCSpropagate 0
35 #define GCSenteratomic 1
36 #define GCSatomic 2
37 #define GCSswpallgc 3
38 #define GCSswpfinobj 4
39 #define GCSswptobefnz 5
40 #define GCSswpend 6
41 #define GCScallfin 7
42 #define GCSpause 8
43 
44 
45 #define issweepphase(g) \
46  (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
47 
48 
49 /*
50 ** macro to tell when main invariant (white objects cannot point to black
51 ** ones) must be kept. During a collection, the sweep
52 ** phase may break the invariant, as objects turned white may point to
53 ** still-black objects. The invariant is restored when sweep ends and
54 ** all objects are white again.
55 */
56 
57 #define keepinvariant(g) ((g)->gcstate <= GCSatomic)
58 
59 
60 /*
61 ** some useful bit tricks
62 */
63 #define resetbits(x,m) ((x) &= cast_byte(~(m)))
64 #define setbits(x,m) ((x) |= (m))
65 #define testbits(x,m) ((x) & (m))
66 #define bitmask(b) (1<<(b))
67 #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
68 #define l_setbit(x,b) setbits(x, bitmask(b))
69 #define resetbit(x,b) resetbits(x, bitmask(b))
70 #define testbit(x,b) testbits(x, bitmask(b))
71 
72 
73 /*
74 ** Layout for bit use in 'marked' field. First three bits are
75 ** used for object "age" in generational mode. Last bit is free
76 ** to be used by respective objects.
77 */
78 #define WHITE0BIT 3 /* object is white (type 0) */
79 #define WHITE1BIT 4 /* object is white (type 1) */
80 #define BLACKBIT 5 /* object is black */
81 #define FINALIZEDBIT 6 /* object has been marked for finalization */
82 
83 
84 
85 #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
86 
87 
88 #define iswhite(x) testbits((x)->marked, WHITEBITS)
89 #define isblack(x) testbit((x)->marked, BLACKBIT)
90 #define isgray(x) /* neither white nor black */ \
91  (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))
92 
93 #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
94 
95 #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
96 #define isdeadm(ow,m) ((m) & (ow))
97 #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
98 
99 #define changewhite(x) ((x)->marked ^= WHITEBITS)
100 #define gray2black(x) l_setbit((x)->marked, BLACKBIT)
101 
102 #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS)
103 
104 
105 /* object age in generational mode */
106 #define G_NEW 0 /* created in current cycle */
107 #define G_SURVIVAL 1 /* created in previous cycle */
108 #define G_OLD0 2 /* marked old by frw. barrier in this cycle */
109 #define G_OLD1 3 /* first full cycle as old */
110 #define G_OLD 4 /* really old object (not to be visited) */
111 #define G_TOUCHED1 5 /* old object touched this cycle */
112 #define G_TOUCHED2 6 /* old object touched in previous cycle */
113 
114 #define AGEBITS 7 /* all age bits (111) */
115 
116 #define getage(o) ((o)->marked & AGEBITS)
117 #define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a))
118 #define isold(o) (getage(o) > G_SURVIVAL)
119 
120 #define changeage(o,f,t) \
121  check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t)))
122 
123 
124 /* Default Values for GC parameters */
125 #define LUAI_GENMAJORMUL 100
126 #define LUAI_GENMINORMUL 20
127 
128 /* wait memory to double before starting new cycle */
129 #define LUAI_GCPAUSE 200
130 
131 /*
132 ** some gc parameters are stored divided by 4 to allow a maximum value
133 ** up to 1023 in a 'lu_byte'.
134 */
135 #define getgcparam(p) ((p) * 4)
136 #define setgcparam(p,v) ((p) = (v) / 4)
137 
138 #define LUAI_GCMUL 100
139 
140 /* how much to allocate before next GC step (log2) */
141 #define LUAI_GCSTEPSIZE 13 /* 8 KB */
142 
143 
144 /*
145 ** Check whether the declared GC mode is generational. While in
146 ** generational mode, the collector can go temporarily to incremental
147 ** mode to improve performance. This is signaled by 'g->lastatomic != 0'.
148 */
149 #define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0)
150 
151 /*
152 ** Does one step of collection when debt becomes positive. 'pre'/'pos'
153 ** allows some adjustments to be done only when needed. macro
154 ** 'condchangemem' is used only for heavy tests (forcing a full
155 ** GC cycle on every opportunity)
156 */
157 #define luaC_condGC(L,pre,pos) \
158  { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
159  condchangemem(L,pre,pos); }
160 
161 /* more often than not, 'pre'/'pos' are empty */
162 #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
163 
164 
165 #define luaC_barrier(L,p,v) ( \
166  (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
167  luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0))
168 
169 #define luaC_barrierback(L,p,v) ( \
170  (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
171  luaC_barrierback_(L,p) : cast_void(0))
172 
173 #define luaC_objbarrier(L,p,o) ( \
174  (isblack(p) && iswhite(o)) ? \
175  luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
176 
177 LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
179 LUAI_FUNC void luaC_step (lua_State *L);
180 LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
181 LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
182 LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
186 LUAI_FUNC void luaC_changemode (lua_State *L, int newmode);
187 
188 
189 #endif
LUAI_FUNC
#define LUAI_FUNC
Definition: luaconf.h:324
lstate.h
_native_lua_config.h
native Lua configuration file
luaC_changemode
LUAI_FUNC void luaC_changemode(lua_State *L, int newmode)
Definition: lgc.c:1223
luaC_runtilstate
LUAI_FUNC void luaC_runtilstate(lua_State *L, int statesmask)
Definition: lgc.c:1531
luaC_barrier_
LUAI_FUNC void luaC_barrier_(lua_State *L, GCObject *o, GCObject *v)
Definition: lgc.c:193
luaC_freeallobjects
LUAI_FUNC void luaC_freeallobjects(lua_State *L)
Definition: lgc.c:1403
luaC_newobj
LUAI_FUNC GCObject * luaC_newobj(lua_State *L, int tt, size_t sz)
Definition: lgc.c:240
Table
Definition: lobject.h:713
luaC_fix
LUAI_FUNC void luaC_fix(lua_State *L, GCObject *o)
Definition: lgc.c:225
lobject.h
lua_State
Definition: lstate.h:283
luaC_checkfinalizer
LUAI_FUNC void luaC_checkfinalizer(lua_State *L, GCObject *o, Table *mt)
Definition: lgc.c:927
GCObject
Definition: lobject.h:269
luaC_barrierback_
LUAI_FUNC void luaC_barrierback_(lua_State *L, GCObject *o)
Definition: lgc.c:214
luaC_step
LUAI_FUNC void luaC_step(lua_State *L)
Definition: lgc.c:1566
luaC_fullgc
LUAI_FUNC void luaC_fullgc(lua_State *L, int isemergency)
Definition: lgc.c:1603