diff options
| author | Li Jin <dragon-fly@qq.com> | 2020-06-22 16:50:40 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2020-06-22 16:50:40 +0800 |
| commit | cd2b60b101a398cb9356d746364e70eaed1860f1 (patch) | |
| tree | a1fe71b76faabc4883f16905a94164ce5c23e692 /src/lua/lgc.h | |
| parent | 88c1052e700f38cf3d8ad82d469da4c487760b7e (diff) | |
| download | yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.gz yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.bz2 yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.zip | |
add support for local variable declared with attribute 'close' and 'const' for Lua 5.4.
Diffstat (limited to 'src/lua/lgc.h')
| -rw-r--r-- | src/lua/lgc.h | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/src/lua/lgc.h b/src/lua/lgc.h new file mode 100644 index 0000000..b972472 --- /dev/null +++ b/src/lua/lgc.h | |||
| @@ -0,0 +1,186 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: lgc.h $ | ||
| 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 | ** Possible states of the Garbage Collector | ||
| 30 | */ | ||
| 31 | #define GCSpropagate 0 | ||
| 32 | #define GCSenteratomic 1 | ||
| 33 | #define GCSatomic 2 | ||
| 34 | #define GCSswpallgc 3 | ||
| 35 | #define GCSswpfinobj 4 | ||
| 36 | #define GCSswptobefnz 5 | ||
| 37 | #define GCSswpend 6 | ||
| 38 | #define GCScallfin 7 | ||
| 39 | #define GCSpause 8 | ||
| 40 | |||
| 41 | |||
| 42 | #define issweepphase(g) \ | ||
| 43 | (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) | ||
| 44 | |||
| 45 | |||
| 46 | /* | ||
| 47 | ** macro to tell when main invariant (white objects cannot point to black | ||
| 48 | ** ones) must be kept. During a collection, the sweep | ||
| 49 | ** phase may break the invariant, as objects turned white may point to | ||
| 50 | ** still-black objects. The invariant is restored when sweep ends and | ||
| 51 | ** all objects are white again. | ||
| 52 | */ | ||
| 53 | |||
| 54 | #define keepinvariant(g) ((g)->gcstate <= GCSatomic) | ||
| 55 | |||
| 56 | |||
| 57 | /* | ||
| 58 | ** some useful bit tricks | ||
| 59 | */ | ||
| 60 | #define resetbits(x,m) ((x) &= cast_byte(~(m))) | ||
| 61 | #define setbits(x,m) ((x) |= (m)) | ||
| 62 | #define testbits(x,m) ((x) & (m)) | ||
| 63 | #define bitmask(b) (1<<(b)) | ||
| 64 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) | ||
| 65 | #define l_setbit(x,b) setbits(x, bitmask(b)) | ||
| 66 | #define resetbit(x,b) resetbits(x, bitmask(b)) | ||
| 67 | #define testbit(x,b) testbits(x, bitmask(b)) | ||
| 68 | |||
| 69 | |||
| 70 | /* | ||
| 71 | ** Layout for bit use in 'marked' field. First three bits are | ||
| 72 | ** used for object "age" in generational mode. Last bit is free | ||
| 73 | ** to be used by respective objects. | ||
| 74 | */ | ||
| 75 | #define WHITE0BIT 3 /* object is white (type 0) */ | ||
| 76 | #define WHITE1BIT 4 /* object is white (type 1) */ | ||
| 77 | #define BLACKBIT 5 /* object is black */ | ||
| 78 | #define FINALIZEDBIT 6 /* object has been marked for finalization */ | ||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) | ||
| 83 | |||
| 84 | |||
| 85 | #define iswhite(x) testbits((x)->marked, WHITEBITS) | ||
| 86 | #define isblack(x) testbit((x)->marked, BLACKBIT) | ||
| 87 | #define isgray(x) /* neither white nor black */ \ | ||
| 88 | (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) | ||
| 89 | |||
| 90 | #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) | ||
| 91 | |||
| 92 | #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) | ||
| 93 | #define isdeadm(ow,m) ((m) & (ow)) | ||
| 94 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) | ||
| 95 | |||
| 96 | #define changewhite(x) ((x)->marked ^= WHITEBITS) | ||
| 97 | #define gray2black(x) l_setbit((x)->marked, BLACKBIT) | ||
| 98 | |||
| 99 | #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS) | ||
| 100 | |||
| 101 | |||
| 102 | /* object age in generational mode */ | ||
| 103 | #define G_NEW 0 /* created in current cycle */ | ||
| 104 | #define G_SURVIVAL 1 /* created in previous cycle */ | ||
| 105 | #define G_OLD0 2 /* marked old by frw. barrier in this cycle */ | ||
| 106 | #define G_OLD1 3 /* first full cycle as old */ | ||
| 107 | #define G_OLD 4 /* really old object (not to be visited) */ | ||
| 108 | #define G_TOUCHED1 5 /* old object touched this cycle */ | ||
| 109 | #define G_TOUCHED2 6 /* old object touched in previous cycle */ | ||
| 110 | |||
| 111 | #define AGEBITS 7 /* all age bits (111) */ | ||
| 112 | |||
| 113 | #define getage(o) ((o)->marked & AGEBITS) | ||
| 114 | #define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a)) | ||
| 115 | #define isold(o) (getage(o) > G_SURVIVAL) | ||
| 116 | |||
| 117 | #define changeage(o,f,t) \ | ||
| 118 | check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) | ||
| 119 | |||
| 120 | |||
| 121 | /* Default Values for GC parameters */ | ||
| 122 | #define LUAI_GENMAJORMUL 100 | ||
| 123 | #define LUAI_GENMINORMUL 20 | ||
| 124 | |||
| 125 | /* wait memory to double before starting new cycle */ | ||
| 126 | #define LUAI_GCPAUSE 200 | ||
| 127 | |||
| 128 | /* | ||
| 129 | ** some gc parameters are stored divided by 4 to allow a maximum value | ||
| 130 | ** up to 1023 in a 'lu_byte'. | ||
| 131 | */ | ||
| 132 | #define getgcparam(p) ((p) * 4) | ||
| 133 | #define setgcparam(p,v) ((p) = (v) / 4) | ||
| 134 | |||
| 135 | #define LUAI_GCMUL 100 | ||
| 136 | |||
| 137 | /* how much to allocate before next GC step (log2) */ | ||
| 138 | #define LUAI_GCSTEPSIZE 13 /* 8 KB */ | ||
| 139 | |||
| 140 | |||
| 141 | /* | ||
| 142 | ** Check whether the declared GC mode is generational. While in | ||
| 143 | ** generational mode, the collector can go temporarily to incremental | ||
| 144 | ** mode to improve performance. This is signaled by 'g->lastatomic != 0'. | ||
| 145 | */ | ||
| 146 | #define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0) | ||
| 147 | |||
| 148 | /* | ||
| 149 | ** Does one step of collection when debt becomes positive. 'pre'/'pos' | ||
| 150 | ** allows some adjustments to be done only when needed. macro | ||
| 151 | ** 'condchangemem' is used only for heavy tests (forcing a full | ||
| 152 | ** GC cycle on every opportunity) | ||
| 153 | */ | ||
| 154 | #define luaC_condGC(L,pre,pos) \ | ||
| 155 | { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ | ||
| 156 | condchangemem(L,pre,pos); } | ||
| 157 | |||
| 158 | /* more often than not, 'pre'/'pos' are empty */ | ||
| 159 | #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) | ||
| 160 | |||
| 161 | |||
| 162 | #define luaC_barrier(L,p,v) ( \ | ||
| 163 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ | ||
| 164 | luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) | ||
| 165 | |||
| 166 | #define luaC_barrierback(L,p,v) ( \ | ||
| 167 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ | ||
| 168 | luaC_barrierback_(L,p) : cast_void(0)) | ||
| 169 | |||
| 170 | #define luaC_objbarrier(L,p,o) ( \ | ||
| 171 | (isblack(p) && iswhite(o)) ? \ | ||
| 172 | luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) | ||
| 173 | |||
| 174 | LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); | ||
| 175 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); | ||
| 176 | LUAI_FUNC void luaC_step (lua_State *L); | ||
| 177 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); | ||
| 178 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); | ||
| 179 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); | ||
| 180 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); | ||
| 181 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); | ||
| 182 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); | ||
| 183 | LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); | ||
| 184 | |||
| 185 | |||
| 186 | #endif | ||
