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