]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | ** $Id: lgc.h,v 2.86 2014/10/25 11:50:46 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 | #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);) | |
107 | ||
108 | ||
109 | #define luaC_barrier(L,p,v) { \ | |
110 | if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ | |
111 | luaC_barrier_(L,obj2gco(p),gcvalue(v)); } | |
112 | ||
113 | #define luaC_barrierback(L,p,v) { \ | |
114 | if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ | |
115 | luaC_barrierback_(L,p); } | |
116 | ||
117 | #define luaC_objbarrier(L,p,o) { \ | |
118 | if (isblack(p) && iswhite(o)) \ | |
119 | luaC_barrier_(L,obj2gco(p),obj2gco(o)); } | |
120 | ||
121 | #define luaC_upvalbarrier(L,uv) \ | |
122 | { if (iscollectable((uv)->v) && !upisopen(uv)) \ | |
123 | luaC_upvalbarrier_(L,uv); } | |
124 | ||
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); | |
136 | ||
137 | ||
138 | #endif |