etc/protocols - sync with NetBSD-8
[minix.git] / external / mit / lua / dist / src / lgc.h
blobea07a045b7e6374972559f5106401579a64fc7fd
1 /*
2 ** $Id: lgc.h,v 1.3 2015/02/02 14:03:05 lneto Exp $
3 ** Garbage Collector
4 ** See Copyright Notice in lua.h
5 */
7 #ifndef lgc_h
8 #define lgc_h
11 #include "lobject.h"
12 #include "lstate.h"
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).
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
37 ** Possible states of the Garbage Collector
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
49 #define issweepphase(g) \
50 (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
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.
61 #define keepinvariant(g) ((g)->gcstate <= GCSatomic)
65 ** some useful bit tricks
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))
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) */
84 #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
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)))
92 #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
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)
98 #define changewhite(x) ((x)->marked ^= WHITEBITS)
99 #define gray2black(x) l_setbit((x)->marked, BLACKBIT)
101 #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
104 #define luaC_condGC(L,c) \
105 {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);}
106 #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);)
109 #define luaC_barrier(L,p,v) { \
110 if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \
111 luaC_barrier_(L,obj2gco(p),gcvalue(v)); }
113 #define luaC_barrierback(L,p,v) { \
114 if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \
115 luaC_barrierback_(L,p); }
117 #define luaC_objbarrier(L,p,o) { \
118 if (isblack(p) && iswhite(o)) \
119 luaC_barrier_(L,obj2gco(p),obj2gco(o)); }
121 #define luaC_upvalbarrier(L,uv) \
122 { if (iscollectable((uv)->v) && !upisopen(uv)) \
123 luaC_upvalbarrier_(L,uv); }
125 LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
126 LUAI_FUNC void luaC_freeallobjects (lua_State *L);
127 LUAI_FUNC void luaC_step (lua_State *L);
128 LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
129 LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
130 LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
131 LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
132 LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o);
133 LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv);
134 LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
135 LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv);
138 #endif