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