aed3e18a5f0431083831ab6be8d839d8a3d2f330
[ardour.git] / libs / lua / lua-5.3.3 / lgc.h
1 /*
2 ** $Id: lgc.h,v 2.91 2015/12/21 13:02:14 roberto Exp $
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 /*
15 ** Collectable objects may have one of three colors: white, which
16 ** means the object is not marked; gray, which means the
17 ** object is marked, but its references may be not marked; and
18 ** black, which means that the object and all its references are marked.
19 ** The main invariant of the garbage collector, while marking objects,
20 ** is that a black object can never point to a white one. Moreover,
21 ** any gray object must be in a "gray list" (gray, grayagain, weak,
22 ** allweak, ephemeron) so that it can be visited again before finishing
23 ** the collection cycle. These lists have no meaning when the invariant
24 ** is not being enforced (e.g., sweep phase).
25 */
26
27
28
29 /* how much to allocate before next GC step */
30 #if !defined(GCSTEPSIZE)
31 /* ~100 small strings */
32 #define GCSTEPSIZE      (cast_int(100 * sizeof(TString)))
33 #endif
34
35
36 /*
37 ** Possible states of the Garbage Collector
38 */
39 #define GCSpropagate    0
40 #define GCSatomic       1
41 #define GCSswpallgc     2
42 #define GCSswpfinobj    3
43 #define GCSswptobefnz   4
44 #define GCSswpend       5
45 #define GCScallfin      6
46 #define GCSpause        7
47
48
49 #define issweepphase(g)  \
50         (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
51
52
53 /*
54 ** macro to tell when main invariant (white objects cannot point to black
55 ** ones) must be kept. During a 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.
59 */
60
61 #define keepinvariant(g)        ((g)->gcstate <= GCSatomic)
62
63
64 /*
65 ** some useful bit tricks
66 */
67 #define resetbits(x,m)          ((x) &= cast(lu_byte, ~(m)))
68 #define setbits(x,m)            ((x) |= (m))
69 #define testbits(x,m)           ((x) & (m))
70 #define bitmask(b)              (1<<(b))
71 #define bit2mask(b1,b2)         (bitmask(b1) | bitmask(b2))
72 #define l_setbit(x,b)           setbits(x, bitmask(b))
73 #define resetbit(x,b)           resetbits(x, bitmask(b))
74 #define testbit(x,b)            testbits(x, bitmask(b))
75
76
77 /* Layout for bit use in 'marked' field: */
78 #define WHITE0BIT       0  /* object is white (type 0) */
79 #define WHITE1BIT       1  /* object is white (type 1) */
80 #define BLACKBIT        2  /* object is black */
81 #define FINALIZEDBIT    3  /* object has been marked for finalization */
82 /* bit 7 is currently used by tests (luaL_checkmemory) */
83
84 #define WHITEBITS       bit2mask(WHITE0BIT, WHITE1BIT)
85
86
87 #define iswhite(x)      testbits((x)->marked, WHITEBITS)
88 #define isblack(x)      testbit((x)->marked, BLACKBIT)
89 #define isgray(x)  /* neither white nor black */  \
90         (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))
91
92 #define tofinalize(x)   testbit((x)->marked, FINALIZEDBIT)
93
94 #define otherwhite(g)   ((g)->currentwhite ^ WHITEBITS)
95 #define isdeadm(ow,m)   (!(((m) ^ WHITEBITS) & (ow)))
96 #define isdead(g,v)     isdeadm(otherwhite(g), (v)->marked)
97
98 #define changewhite(x)  ((x)->marked ^= WHITEBITS)
99 #define gray2black(x)   l_setbit((x)->marked, BLACKBIT)
100
101 #define luaC_white(g)   cast(lu_byte, (g)->currentwhite & WHITEBITS)
102
103
104 /*
105 ** Does one step of collection when debt becomes positive. 'pre'/'pos'
106 ** allows some adjustments to be done only when needed. macro
107 ** 'condchangemem' is used only for heavy tests (forcing a full
108 ** GC cycle on every opportunity)
109 */
110 #define luaC_condGC(L,pre,pos) \
111         { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
112           condchangemem(L,pre,pos); }
113
114 /* more often than not, 'pre'/'pos' are empty */
115 #define luaC_checkGC(L)         luaC_condGC(L,(void)0,(void)0)
116
117
118 #define luaC_barrier(L,p,v) (  \
119         (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ?  \
120         luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0))
121
122 #define luaC_barrierback(L,p,v) (  \
123         (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
124         luaC_barrierback_(L,p) : cast_void(0))
125
126 #define luaC_objbarrier(L,p,o) (  \
127         (isblack(p) && iswhite(o)) ? \
128         luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
129
130 #define luaC_upvalbarrier(L,uv) ( \
131         (iscollectable((uv)->v) && !upisopen(uv)) ? \
132          luaC_upvalbarrier_(L,uv) : cast_void(0))
133
134 LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
135 LUAI_FUNC void luaC_freeallobjects (lua_State *L);
136 LUAI_FUNC void luaC_step (lua_State *L);
137 LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
138 LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
139 LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
140 LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
141 LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o);
142 LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv);
143 LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
144 LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv);
145
146
147 #endif