diff options
| -rw-r--r-- | lgc.c | 278 |
1 files changed, 161 insertions, 117 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lgc.c,v 2.78 2010/04/12 16:07:06 roberto Exp roberto $ | 2 | ** $Id: lgc.c,v 2.79 2010/04/20 20:15:30 roberto Exp roberto $ |
| 3 | ** Garbage Collector | 3 | ** Garbage Collector |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -31,18 +31,22 @@ | |||
| 31 | #define GCATOMICCOST 1000 | 31 | #define GCATOMICCOST 1000 |
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | #define issweepphase(g) \ | ||
| 35 | (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep) | ||
| 36 | |||
| 37 | |||
| 34 | /* | 38 | /* |
| 35 | ** 'makewhite' erases all color bits plus the old bit and then | 39 | ** 'makewhite' erases all color bits plus the old bit and then |
| 36 | ** sets only the current white bit | 40 | ** sets only the current white bit |
| 37 | */ | 41 | */ |
| 38 | #define maskcolors (~(bit2mask(BLACKBIT, OLDBIT) | WHITEBITS)) | 42 | #define maskcolors (~(bit2mask(BLACKBIT, OLDBIT) | WHITEBITS)) |
| 39 | #define makewhite(w,x) \ | 43 | #define makewhite(g,x) \ |
| 40 | (gch(x)->marked = cast_byte((gch(x)->marked & maskcolors) | luaC_white(g))) | 44 | (gch(x)->marked = cast_byte((gch(x)->marked & maskcolors) | luaC_white(g))) |
| 41 | 45 | ||
| 42 | #define white2gray(x) resetbits(gch(x)->marked, WHITEBITS) | 46 | #define white2gray(x) resetbits(gch(x)->marked, WHITEBITS) |
| 43 | #define black2gray(x) resetbit(gch(x)->marked, BLACKBIT) | 47 | #define black2gray(x) resetbit(gch(x)->marked, BLACKBIT) |
| 44 | 48 | ||
| 45 | #define stringmark(s) resetbits((s)->tsv.marked, WHITEBITS) | 49 | #define stringmark(s) ((void)((s) && resetbits((s)->tsv.marked, WHITEBITS))) |
| 46 | 50 | ||
| 47 | 51 | ||
| 48 | #define isfinalized(u) testbit((u)->marked, FINALIZEDBIT) | 52 | #define isfinalized(u) testbit((u)->marked, FINALIZEDBIT) |
| @@ -57,6 +61,16 @@ | |||
| 57 | reallymarkobject(g, obj2gco(t)); } | 61 | reallymarkobject(g, obj2gco(t)); } |
| 58 | 62 | ||
| 59 | 63 | ||
| 64 | /* | ||
| 65 | ** macro to tell when main invariant (white objects cannot point to black | ||
| 66 | ** ones) must be kept. During a non-generational collection, the sweep | ||
| 67 | ** phase may brak the invariant, as objects turned white may point to | ||
| 68 | ** still-black objects. The invariant is restored when sweep ends and | ||
| 69 | ** all objects are white again. During a generational collection, the | ||
| 70 | ** invariant must be kept all times. | ||
| 71 | */ | ||
| 72 | #define keepinvariant(g) (g->gckind == KGC_GEN || g->gcstate == GCSpropagate) | ||
| 73 | |||
| 60 | static void reallymarkobject (global_State *g, GCObject *o); | 74 | static void reallymarkobject (global_State *g, GCObject *o); |
| 61 | 75 | ||
| 62 | 76 | ||
| @@ -66,12 +80,16 @@ static void reallymarkobject (global_State *g, GCObject *o); | |||
| 66 | ** ======================================================= | 80 | ** ======================================================= |
| 67 | */ | 81 | */ |
| 68 | 82 | ||
| 69 | static void linktable (Table *h, GCObject **p) { | 83 | |
| 70 | h->gclist = *p; | 84 | /* |
| 71 | *p = obj2gco(h); | 85 | ** link table 'h' into list pointed by 'p' |
| 72 | } | 86 | */ |
| 87 | #define linktable(h,p) ((h)->gclist = *(p), *(p) = obj2gco(h)) | ||
| 73 | 88 | ||
| 74 | 89 | ||
| 90 | /* | ||
| 91 | ** mark a table entry as dead (therefore removing it from the table) | ||
| 92 | */ | ||
| 75 | static void removeentry (Node *n) { | 93 | static void removeentry (Node *n) { |
| 76 | lua_assert(ttisnil(gval(n))); | 94 | lua_assert(ttisnil(gval(n))); |
| 77 | if (iscollectable(gkey(n))) | 95 | if (iscollectable(gkey(n))) |
| @@ -80,8 +98,8 @@ static void removeentry (Node *n) { | |||
| 80 | 98 | ||
| 81 | 99 | ||
| 82 | /* | 100 | /* |
| 83 | ** The next function tells whether a key or value can be cleared from | 101 | ** tells whether a key or value can be cleared from a weak |
| 84 | ** a weak table. Non-collectable objects are never removed from weak | 102 | ** table. Non-collectable objects are never removed from weak |
| 85 | ** tables. Strings behave as `values', so are never removed too. for | 103 | ** tables. Strings behave as `values', so are never removed too. for |
| 86 | ** other objects: if really collected, cannot keep them; for objects | 104 | ** other objects: if really collected, cannot keep them; for objects |
| 87 | ** being finalized, keep them in keys, but not in values | 105 | ** being finalized, keep them in keys, but not in values |
| @@ -97,25 +115,33 @@ static int iscleared (const TValue *o, int iskey) { | |||
| 97 | } | 115 | } |
| 98 | 116 | ||
| 99 | 117 | ||
| 118 | /* | ||
| 119 | ** barrier that moves collector forward, that is, mark the white object | ||
| 120 | ** being pointed by a black object. | ||
| 121 | */ | ||
| 100 | void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) { | 122 | void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) { |
| 101 | global_State *g = G(L); | 123 | global_State *g = G(L); |
| 102 | lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); | 124 | lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); |
| 103 | lua_assert(g->gckind == KGC_GEN || | 125 | lua_assert(g->gckind == KGC_GEN || |
| 104 | (g->gcstate != GCSfinalize && g->gcstate != GCSpause)); | 126 | (g->gcstate != GCSfinalize && g->gcstate != GCSpause)); |
| 105 | lua_assert(gch(o)->tt != LUA_TTABLE); | 127 | lua_assert(gch(o)->tt != LUA_TTABLE); |
| 106 | if (g->gcstate == GCSpropagate) /* must keep invariant? */ | 128 | if (keepinvariant(g)) /* must keep invariant? */ |
| 107 | reallymarkobject(g, v); /* restore invariant */ | 129 | reallymarkobject(g, v); /* restore invariant */ |
| 108 | else /* don't mind */ | 130 | else { /* sweep phase */ |
| 109 | makewhite(g, o); /* mark as white just to avoid other barriers */ | 131 | lua_assert(issweepphase(g)); |
| 132 | makewhite(g, o); /* mark main obj. as white to avoid other barriers */ | ||
| 133 | } | ||
| 110 | } | 134 | } |
| 111 | 135 | ||
| 112 | 136 | ||
| 137 | /* | ||
| 138 | ** barrier that moves collector backward, that is, mark the black object | ||
| 139 | ** pointing to a white object as gray again. | ||
| 140 | */ | ||
| 113 | void luaC_barrierback (lua_State *L, Table *t) { | 141 | void luaC_barrierback (lua_State *L, Table *t) { |
| 114 | global_State *g = G(L); | 142 | global_State *g = G(L); |
| 115 | GCObject *o = obj2gco(t); | 143 | GCObject *o = obj2gco(t); |
| 116 | lua_assert(isblack(o) && !isdead(g, o)); | 144 | lua_assert(isblack(o) && !isdead(g, o)); |
| 117 | lua_assert(g->gckind == KGC_GEN || | ||
| 118 | (g->gcstate != GCSfinalize && g->gcstate != GCSpause)); | ||
| 119 | black2gray(o); /* make table gray (again) */ | 145 | black2gray(o); /* make table gray (again) */ |
| 120 | t->gclist = g->grayagain; | 146 | t->gclist = g->grayagain; |
| 121 | g->grayagain = o; | 147 | g->grayagain = o; |
| @@ -123,7 +149,9 @@ void luaC_barrierback (lua_State *L, Table *t) { | |||
| 123 | 149 | ||
| 124 | 150 | ||
| 125 | /* | 151 | /* |
| 126 | ** create a new collectable object and link it to '*list' | 152 | ** create a new collectable object (with given type and size) and link |
| 153 | ** it to '*list'. 'offset' tells how many bytes to allocate before the | ||
| 154 | ** object itself (used only by states). | ||
| 127 | */ | 155 | */ |
| 128 | GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list, | 156 | GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list, |
| 129 | int offset) { | 157 | int offset) { |
| @@ -139,20 +167,19 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list, | |||
| 139 | } | 167 | } |
| 140 | 168 | ||
| 141 | 169 | ||
| 170 | /* | ||
| 171 | ** link an upvalue back to the main root list. (It was previously in the | ||
| 172 | ** list of open upvalues of some thread.) | ||
| 173 | */ | ||
| 142 | void luaC_linkupval (lua_State *L, UpVal *uv) { | 174 | void luaC_linkupval (lua_State *L, UpVal *uv) { |
| 143 | global_State *g = G(L); | 175 | global_State *g = G(L); |
| 144 | GCObject *o = obj2gco(uv); | 176 | GCObject *o = obj2gco(uv); |
| 145 | gch(o)->next = g->allgc; /* link upvalue into `allgc' list */ | 177 | gch(o)->next = g->allgc; /* link upvalue into `allgc' list */ |
| 146 | g->allgc = o; | 178 | g->allgc = o; |
| 147 | if (isgray(o)) { | 179 | lua_assert(!isblack(o)); /* open upvalues are never black */ |
| 148 | if (g->gcstate == GCSpropagate) { | 180 | if (isgray(o)) { /* is it marked? */ |
| 149 | gray2black(o); /* closed upvalues need barrier */ | 181 | gray2black(o); /* could not be black; now it can */ |
| 150 | luaC_barrier(L, uv, uv->v); | 182 | luaC_barrier(L, uv, uv->v); |
| 151 | } | ||
| 152 | else { /* sweep phase: sweep it (turning it into white) */ | ||
| 153 | makewhite(g, o); | ||
| 154 | lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); | ||
| 155 | } | ||
| 156 | } | 183 | } |
| 157 | } | 184 | } |
| 158 | 185 | ||
| @@ -166,25 +193,33 @@ void luaC_linkupval (lua_State *L, UpVal *uv) { | |||
| 166 | ** ======================================================= | 193 | ** ======================================================= |
| 167 | */ | 194 | */ |
| 168 | 195 | ||
| 196 | |||
| 197 | /* | ||
| 198 | ** mark an object. Userdata and closed upvalues are visited and turned | ||
| 199 | ** black here. Strings remain gray (it is the same as making them | ||
| 200 | ** black). Other objects are marked gray and added to appropriate list | ||
| 201 | ** to be visited (and turned black) later. (Open upvalues are already | ||
| 202 | ** linked in 'headuv' list.) | ||
| 203 | */ | ||
| 169 | static void reallymarkobject (global_State *g, GCObject *o) { | 204 | static void reallymarkobject (global_State *g, GCObject *o) { |
| 170 | lua_assert(iswhite(o) && !isdead(g, o)); | 205 | lua_assert(iswhite(o) && !isdead(g, o)); |
| 171 | white2gray(o); | 206 | white2gray(o); |
| 172 | switch (gch(o)->tt) { | 207 | switch (gch(o)->tt) { |
| 173 | case LUA_TSTRING: { | 208 | case LUA_TSTRING: { |
| 174 | return; | 209 | return; /* for strings, gray is as good as black */ |
| 175 | } | 210 | } |
| 176 | case LUA_TUSERDATA: { | 211 | case LUA_TUSERDATA: { |
| 177 | Table *mt = gco2u(o)->metatable; | 212 | Table *mt = gco2u(o)->metatable; |
| 178 | gray2black(o); /* udata are never gray */ | ||
| 179 | markobject(g, mt); | 213 | markobject(g, mt); |
| 180 | markobject(g, gco2u(o)->env); | 214 | markobject(g, gco2u(o)->env); |
| 215 | gray2black(o); /* all pointers marked */ | ||
| 181 | return; | 216 | return; |
| 182 | } | 217 | } |
| 183 | case LUA_TUPVAL: { | 218 | case LUA_TUPVAL: { |
| 184 | UpVal *uv = gco2uv(o); | 219 | UpVal *uv = gco2uv(o); |
| 185 | markvalue(g, uv->v); | 220 | markvalue(g, uv->v); |
| 186 | if (uv->v == &uv->u.value) /* closed? */ | 221 | if (uv->v == &uv->u.value) /* closed? (open upvalues remain gray) */ |
| 187 | gray2black(o); /* open upvalues are never black */ | 222 | gray2black(o); /* make it black */ |
| 188 | return; | 223 | return; |
| 189 | } | 224 | } |
| 190 | case LUA_TFUNCTION: { | 225 | case LUA_TFUNCTION: { |
| @@ -211,6 +246,9 @@ static void reallymarkobject (global_State *g, GCObject *o) { | |||
| 211 | } | 246 | } |
| 212 | 247 | ||
| 213 | 248 | ||
| 249 | /* | ||
| 250 | ** mark tag methods for basic types | ||
| 251 | */ | ||
| 214 | static void markmt (global_State *g) { | 252 | static void markmt (global_State *g) { |
| 215 | int i; | 253 | int i; |
| 216 | for (i=0; i < LUA_NUMTAGS; i++) | 254 | for (i=0; i < LUA_NUMTAGS; i++) |
| @@ -218,6 +256,9 @@ static void markmt (global_State *g) { | |||
| 218 | } | 256 | } |
| 219 | 257 | ||
| 220 | 258 | ||
| 259 | /* | ||
| 260 | ** mark all objects in list of being-finalized | ||
| 261 | */ | ||
| 221 | static void markbeingfnz (global_State *g) { | 262 | static void markbeingfnz (global_State *g) { |
| 222 | GCObject *o; | 263 | GCObject *o; |
| 223 | for (o = g->tobefnz; o != NULL; o = gch(o)->next) { | 264 | for (o = g->tobefnz; o != NULL; o = gch(o)->next) { |
| @@ -227,7 +268,23 @@ static void markbeingfnz (global_State *g) { | |||
| 227 | } | 268 | } |
| 228 | 269 | ||
| 229 | 270 | ||
| 230 | /* mark root set */ | 271 | /* |
| 272 | ** mark all values stored in marked open upvalues. (See comment in | ||
| 273 | ** 'lstate.h'.) | ||
| 274 | */ | ||
| 275 | static void remarkupvals (global_State *g) { | ||
| 276 | UpVal *uv; | ||
| 277 | for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) { | ||
| 278 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); | ||
| 279 | if (isgray(obj2gco(uv))) | ||
| 280 | markvalue(g, uv->v); | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | |||
| 285 | /* | ||
| 286 | ** mark root set | ||
| 287 | */ | ||
| 231 | static void markroot (lua_State *L) { | 288 | static void markroot (lua_State *L) { |
| 232 | global_State *g = G(L); | 289 | global_State *g = G(L); |
| 233 | g->gray = NULL; | 290 | g->gray = NULL; |
| @@ -239,16 +296,6 @@ static void markroot (lua_State *L) { | |||
| 239 | markbeingfnz(g); /* mark any finalizing object left from previous cycle */ | 296 | markbeingfnz(g); /* mark any finalizing object left from previous cycle */ |
| 240 | } | 297 | } |
| 241 | 298 | ||
| 242 | |||
| 243 | static void remarkupvals (global_State *g) { | ||
| 244 | UpVal *uv; | ||
| 245 | for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) { | ||
| 246 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); | ||
| 247 | if (isgray(obj2gco(uv))) | ||
| 248 | markvalue(g, uv->v); | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | /* }====================================================== */ | 299 | /* }====================================================== */ |
| 253 | 300 | ||
| 254 | 301 | ||
| @@ -259,49 +306,48 @@ static void remarkupvals (global_State *g) { | |||
| 259 | */ | 306 | */ |
| 260 | 307 | ||
| 261 | static void traverseweakvalue (global_State *g, Table *h) { | 308 | static void traverseweakvalue (global_State *g, Table *h) { |
| 262 | int i = sizenode(h); | 309 | Node *n, *limit = gnode(h, sizenode(h)); |
| 263 | while (i--) { | 310 | for (n = gnode(h, 0); n < limit; n++) { |
| 264 | Node *n = gnode(h, i); | ||
| 265 | checkdeadkey(n); | 311 | checkdeadkey(n); |
| 266 | if (ttisnil(gval(n))) | 312 | if (ttisnil(gval(n))) /* entry is empty? */ |
| 267 | removeentry(n); /* remove empty entries */ | 313 | removeentry(n); /* remove it */ |
| 268 | else { | 314 | else { |
| 269 | lua_assert(!ttisnil(gkey(n))); | 315 | lua_assert(!ttisnil(gkey(n))); |
| 270 | markvalue(g, gkey(n)); | 316 | markvalue(g, gkey(n)); /* mark key */ |
| 271 | } | 317 | } |
| 272 | } | 318 | } |
| 273 | linktable(h, &g->weak); | 319 | linktable(h, &g->weak); /* go to appropriate list */ |
| 274 | } | 320 | } |
| 275 | 321 | ||
| 276 | 322 | ||
| 277 | static int traverseephemeron (global_State *g, Table *h) { | 323 | static int traverseephemeron (global_State *g, Table *h) { |
| 278 | int marked = 0; | 324 | int marked = 0; /* true if an object is marked in this traversal */ |
| 279 | int hasclears = 0; | 325 | int hasclears = 0; /* true if table has unmarked pairs */ |
| 280 | int i = h->sizearray; | 326 | Node *n, *limit = gnode(h, sizenode(h)); |
| 281 | while (i--) { /* mark array part (numeric keys are 'strong') */ | 327 | int i; |
| 328 | /* traverse array part (numeric keys are 'strong') */ | ||
| 329 | for (i = 0; i < h->sizearray; i++) { | ||
| 282 | if (valiswhite(&h->array[i])) { | 330 | if (valiswhite(&h->array[i])) { |
| 283 | marked = 1; | 331 | marked = 1; |
| 284 | reallymarkobject(g, gcvalue(&h->array[i])); | 332 | reallymarkobject(g, gcvalue(&h->array[i])); |
| 285 | } | 333 | } |
| 286 | } | 334 | } |
| 287 | i = sizenode(h); | 335 | /* traverse hash part */ |
| 288 | while (i--) { | 336 | for (n = gnode(h, 0); n < limit; n++) { |
| 289 | Node *n = gnode(h, i); | ||
| 290 | checkdeadkey(n); | 337 | checkdeadkey(n); |
| 291 | if (ttisnil(gval(n))) /* entry is empty? */ | 338 | if (ttisnil(gval(n))) /* entry is empty? */ |
| 292 | removeentry(n); /* remove it */ | 339 | removeentry(n); /* remove it */ |
| 293 | else if (valiswhite(gval(n))) { | 340 | else if (valiswhite(gval(n))) { /* value not marked yet? */ |
| 294 | /* value is not marked yet */ | ||
| 295 | if (iscleared(key2tval(n), 1)) /* key is not marked (yet)? */ | 341 | if (iscleared(key2tval(n), 1)) /* key is not marked (yet)? */ |
| 296 | hasclears = 1; /* may have to propagate mark from key to value */ | 342 | hasclears = 1; /* may have to propagate mark from key to value */ |
| 297 | else { /* mark value only if key is marked */ | 343 | else { /* key is marked, so mark value */ |
| 298 | marked = 1; /* some mark changed status */ | 344 | marked = 1; /* value was not marked */ |
| 299 | reallymarkobject(g, gcvalue(gval(n))); | 345 | reallymarkobject(g, gcvalue(gval(n))); |
| 300 | } | 346 | } |
| 301 | } | 347 | } |
| 302 | } | 348 | } |
| 303 | if (hasclears) | 349 | if (hasclears) /* does table have unmarked pairs? */ |
| 304 | linktable(h, &g->ephemeron); | 350 | linktable(h, &g->ephemeron); /* will have to propagate again */ |
| 305 | else /* nothing to propagate */ | 351 | else /* nothing to propagate */ |
| 306 | linktable(h, &g->weak); /* avoid convergence phase */ | 352 | linktable(h, &g->weak); /* avoid convergence phase */ |
| 307 | return marked; | 353 | return marked; |
| @@ -309,20 +355,18 @@ static int traverseephemeron (global_State *g, Table *h) { | |||
| 309 | 355 | ||
| 310 | 356 | ||
| 311 | static void traversestrongtable (global_State *g, Table *h) { | 357 | static void traversestrongtable (global_State *g, Table *h) { |
| 358 | Node *n, *limit = gnode(h, sizenode(h)); | ||
| 312 | int i; | 359 | int i; |
| 313 | i = h->sizearray; | 360 | for (i = 0; i < h->sizearray; i++) /* traverse array part */ |
| 314 | while (i--) | ||
| 315 | markvalue(g, &h->array[i]); | 361 | markvalue(g, &h->array[i]); |
| 316 | i = sizenode(h); | 362 | for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ |
| 317 | while (i--) { | ||
| 318 | Node *n = gnode(h, i); | ||
| 319 | checkdeadkey(n); | 363 | checkdeadkey(n); |
| 320 | if (ttisnil(gval(n))) | 364 | if (ttisnil(gval(n))) /* entry is empty? */ |
| 321 | removeentry(n); /* remove empty entries */ | 365 | removeentry(n); /* remove it */ |
| 322 | else { | 366 | else { |
| 323 | lua_assert(!ttisnil(gkey(n))); | 367 | lua_assert(!ttisnil(gkey(n))); |
| 324 | markvalue(g, gkey(n)); | 368 | markvalue(g, gkey(n)); /* mark key */ |
| 325 | markvalue(g, gval(n)); | 369 | markvalue(g, gval(n)); /* mark value */ |
| 326 | } | 370 | } |
| 327 | } | 371 | } |
| 328 | } | 372 | } |
| @@ -349,40 +393,34 @@ static void traversetable (global_State *g, Table *h) { | |||
| 349 | } | 393 | } |
| 350 | 394 | ||
| 351 | 395 | ||
| 352 | /* | ||
| 353 | ** All marks are conditional because a GC may happen while the | ||
| 354 | ** prototype is still being created | ||
| 355 | */ | ||
| 356 | static void traverseproto (global_State *g, Proto *f) { | 396 | static void traverseproto (global_State *g, Proto *f) { |
| 357 | int i; | 397 | int i; |
| 358 | if (f->source) stringmark(f->source); | 398 | stringmark(f->source); |
| 359 | for (i=0; i<f->sizek; i++) /* mark literals */ | 399 | for (i = 0; i < f->sizek; i++) /* mark literals */ |
| 360 | markvalue(g, &f->k[i]); | 400 | markvalue(g, &f->k[i]); |
| 361 | for (i=0; i<f->sizeupvalues; i++) { /* mark upvalue names */ | 401 | for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ |
| 362 | if (f->upvalues[i].name) | 402 | stringmark(f->upvalues[i].name); |
| 363 | stringmark(f->upvalues[i].name); | 403 | for (i = 0; i < f->sizep; i++) /* mark nested protos */ |
| 364 | } | ||
| 365 | for (i=0; i<f->sizep; i++) /* mark nested protos */ | ||
| 366 | markobject(g, f->p[i]); | 404 | markobject(g, f->p[i]); |
| 367 | for (i=0; i<f->sizelocvars; i++) { /* mark local-variable names */ | 405 | for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ |
| 368 | if (f->locvars[i].varname) | 406 | stringmark(f->locvars[i].varname); |
| 369 | stringmark(f->locvars[i].varname); | ||
| 370 | } | ||
| 371 | } | 407 | } |
| 372 | 408 | ||
| 373 | 409 | ||
| 374 | static void traverseclosure (global_State *g, Closure *cl) { | 410 | static l_mem traverseclosure (global_State *g, Closure *cl) { |
| 375 | if (cl->c.isC) { | 411 | if (cl->c.isC) { |
| 376 | int i; | 412 | int i; |
| 377 | for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */ | 413 | for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */ |
| 378 | markvalue(g, &cl->c.upvalue[i]); | 414 | markvalue(g, &cl->c.upvalue[i]); |
| 415 | return sizeCclosure(cl->c.nupvalues); | ||
| 379 | } | 416 | } |
| 380 | else { | 417 | else { |
| 381 | int i; | 418 | int i; |
| 382 | lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues); | 419 | lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues); |
| 383 | markobject(g, cl->l.p); | 420 | markobject(g, cl->l.p); /* mark its prototype */ |
| 384 | for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */ | 421 | for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */ |
| 385 | markobject(g, cl->l.upvals[i]); | 422 | markobject(g, cl->l.upvals[i]); |
| 423 | return sizeLclosure(cl->l.nupvalues); | ||
| 386 | } | 424 | } |
| 387 | } | 425 | } |
| 388 | 426 | ||
| @@ -421,9 +459,7 @@ static l_mem propagatemark (global_State *g) { | |||
| 421 | case LUA_TFUNCTION: { | 459 | case LUA_TFUNCTION: { |
| 422 | Closure *cl = gco2cl(o); | 460 | Closure *cl = gco2cl(o); |
| 423 | g->gray = cl->c.gclist; | 461 | g->gray = cl->c.gclist; |
| 424 | traverseclosure(g, cl); | 462 | return traverseclosure(g, cl); |
| 425 | return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) : | ||
| 426 | sizeLclosure(cl->l.nupvalues); | ||
| 427 | } | 463 | } |
| 428 | case LUA_TTHREAD: { | 464 | case LUA_TTHREAD: { |
| 429 | lua_State *th = gco2th(o); | 465 | lua_State *th = gco2th(o); |
| @@ -467,14 +503,14 @@ static void convergeephemerons (global_State *g) { | |||
| 467 | int changed; | 503 | int changed; |
| 468 | do { | 504 | do { |
| 469 | GCObject *w; | 505 | GCObject *w; |
| 470 | GCObject *next = g->ephemeron; | 506 | GCObject *next = g->ephemeron; /* get ephemeron list */ |
| 471 | g->ephemeron = NULL; | 507 | g->ephemeron = NULL; /* tables will return to this list when traversed */ |
| 472 | changed = 0; | 508 | changed = 0; |
| 473 | while ((w = next) != NULL) { | 509 | while ((w = next) != NULL) { |
| 474 | next = gco2t(w)->gclist; | 510 | next = gco2t(w)->gclist; |
| 475 | if (traverseephemeron(g, gco2t(w))) { | 511 | if (traverseephemeron(g, gco2t(w))) { /* traverse marked some value? */ |
| 476 | changed = 1; | 512 | propagateall(g); /* propagate changes */ |
| 477 | propagateall(g); | 513 | changed = 1; /* will have to revisit all ephemeron tables */ |
| 478 | } | 514 | } |
| 479 | } | 515 | } |
| 480 | } while (changed); | 516 | } while (changed); |
| @@ -489,26 +525,26 @@ static void convergeephemerons (global_State *g) { | |||
| 489 | ** ======================================================= | 525 | ** ======================================================= |
| 490 | */ | 526 | */ |
| 491 | 527 | ||
| 492 | /* clear collected entries from weaktables */ | 528 | /* |
| 529 | ** clear collected entries from all weaktables in list 'l' | ||
| 530 | */ | ||
| 493 | static void cleartable (GCObject *l) { | 531 | static void cleartable (GCObject *l) { |
| 494 | while (l) { | 532 | for (; l != NULL; l = gco2t(l)->gclist) { |
| 495 | Table *h = gco2t(l); | 533 | Table *h = gco2t(l); |
| 496 | int i = h->sizearray; | 534 | Node *n, *limit = gnode(h, sizenode(h)); |
| 497 | while (i--) { | 535 | int i; |
| 536 | for (i = 0; i < h->sizearray; i++) { | ||
| 498 | TValue *o = &h->array[i]; | 537 | TValue *o = &h->array[i]; |
| 499 | if (iscleared(o, 0)) /* value was collected? */ | 538 | if (iscleared(o, 0)) /* value was collected? */ |
| 500 | setnilvalue(o); /* remove value */ | 539 | setnilvalue(o); /* remove value */ |
| 501 | } | 540 | } |
| 502 | i = sizenode(h); | 541 | for (n = gnode(h, 0); n < limit; n++) { |
| 503 | while (i--) { | ||
| 504 | Node *n = gnode(h, i); | ||
| 505 | if (!ttisnil(gval(n)) && /* non-empty entry? */ | 542 | if (!ttisnil(gval(n)) && /* non-empty entry? */ |
| 506 | (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) { | 543 | (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) { |
| 507 | setnilvalue(gval(n)); /* remove value ... */ | 544 | setnilvalue(gval(n)); /* remove value ... */ |
| 508 | removeentry(n); /* remove entry from table */ | 545 | removeentry(n); /* and remove entry from table */ |
| 509 | } | 546 | } |
| 510 | } | 547 | } |
| 511 | l = h->gclist; | ||
| 512 | } | 548 | } |
| 513 | } | 549 | } |
| 514 | 550 | ||
| @@ -535,6 +571,10 @@ static void freeobj (lua_State *L, GCObject *o) { | |||
| 535 | static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count); | 571 | static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count); |
| 536 | 572 | ||
| 537 | 573 | ||
| 574 | /* | ||
| 575 | ** sweep the (open) upvalues of a thread and resize its stack and | ||
| 576 | ** list of call-info structures. | ||
| 577 | */ | ||
| 538 | static void sweepthread (lua_State *L, lua_State *L1, int alive) { | 578 | static void sweepthread (lua_State *L, lua_State *L1, int alive) { |
| 539 | if (L1->stack == NULL) return; /* stack not completely built yet */ | 579 | if (L1->stack == NULL) return; /* stack not completely built yet */ |
| 540 | sweepwholelist(L, &L1->openupval); /* sweep open upvalues */ | 580 | sweepwholelist(L, &L1->openupval); /* sweep open upvalues */ |
| @@ -601,13 +641,11 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { | |||
| 601 | 641 | ||
| 602 | static void checkSizes (lua_State *L) { | 642 | static void checkSizes (lua_State *L) { |
| 603 | global_State *g = G(L); | 643 | global_State *g = G(L); |
| 604 | if (g->strt.nuse < cast(lu_int32, g->strt.size)) { | 644 | int hs = g->strt.size / 2; /* half the size of the string table */ |
| 605 | /* string-table size could be the smaller power of 2 larger than 'nuse' */ | 645 | if (g->strt.nuse < cast(lu_int32, hs)) { /* using less than half its size? */ |
| 606 | int size = 1 << luaO_ceillog2(g->strt.nuse); | 646 | luaS_resize(L, hs); /* halve its size */ |
| 607 | if (size < g->strt.size) /* current table too large? */ | ||
| 608 | luaS_resize(L, size); /* shrink it */ | ||
| 609 | } | 647 | } |
| 610 | luaZ_freebuffer(L, &g->buff); | 648 | luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */ |
| 611 | } | 649 | } |
| 612 | 650 | ||
| 613 | 651 | ||
| @@ -656,14 +694,18 @@ static void GCTM (lua_State *L, int propagateerrors) { | |||
| 656 | } | 694 | } |
| 657 | 695 | ||
| 658 | 696 | ||
| 659 | /* move 'dead' udata that need finalization to list 'tobefnz' */ | 697 | /* |
| 698 | ** move all unreachable udata that need finalization from list 'udgc' to | ||
| 699 | ** list 'tobefnz' | ||
| 700 | */ | ||
| 660 | void luaC_separateudata (lua_State *L, int all) { | 701 | void luaC_separateudata (lua_State *L, int all) { |
| 661 | global_State *g = G(L); | 702 | global_State *g = G(L); |
| 662 | GCObject **p = &g->udgc; | 703 | GCObject **p = &g->udgc; |
| 663 | GCObject *curr; | 704 | GCObject *curr; |
| 664 | GCObject **lastnext = &g->tobefnz; | 705 | GCObject **lastnext = &g->tobefnz; |
| 665 | /* find last 'next' field in 'tobefnz' list (to insert elements in its end) */ | 706 | /* find last 'next' field in 'tobefnz' list (to insert elements in its end) */ |
| 666 | while (*lastnext != NULL) lastnext = &gch(*lastnext)->next; | 707 | while (*lastnext != NULL) |
| 708 | lastnext = &gch(*lastnext)->next; | ||
| 667 | while ((curr = *p) != NULL) { /* traverse all finalizable objects */ | 709 | while ((curr = *p) != NULL) { /* traverse all finalizable objects */ |
| 668 | lua_assert(gch(curr)->tt == LUA_TUSERDATA && !isfinalized(gco2u(curr))); | 710 | lua_assert(gch(curr)->tt == LUA_TUSERDATA && !isfinalized(gco2u(curr))); |
| 669 | lua_assert(testbit(gch(curr)->marked, SEPARATED)); | 711 | lua_assert(testbit(gch(curr)->marked, SEPARATED)); |
| @@ -672,8 +714,7 @@ void luaC_separateudata (lua_State *L, int all) { | |||
| 672 | else { | 714 | else { |
| 673 | l_setbit(gch(curr)->marked, FINALIZEDBIT); /* won't be finalized again */ | 715 | l_setbit(gch(curr)->marked, FINALIZEDBIT); /* won't be finalized again */ |
| 674 | *p = gch(curr)->next; /* remove 'curr' from 'udgc' list */ | 716 | *p = gch(curr)->next; /* remove 'curr' from 'udgc' list */ |
| 675 | /* link 'curr' at the end of 'tobefnz' list */ | 717 | gch(curr)->next = *lastnext; /* link at the end of 'tobefnz' list */ |
| 676 | gch(curr)->next = *lastnext; | ||
| 677 | *lastnext = curr; | 718 | *lastnext = curr; |
| 678 | lastnext = &gch(curr)->next; | 719 | lastnext = &gch(curr)->next; |
| 679 | } | 720 | } |
| @@ -681,17 +722,21 @@ void luaC_separateudata (lua_State *L, int all) { | |||
| 681 | } | 722 | } |
| 682 | 723 | ||
| 683 | 724 | ||
| 725 | /* | ||
| 726 | ** if userdata 'u' has a finalizer, remove it from 'allgc' list (must | ||
| 727 | ** search the list to find it) and link it in 'udgc' list. | ||
| 728 | */ | ||
| 684 | void luaC_checkfinalizer (lua_State *L, Udata *u) { | 729 | void luaC_checkfinalizer (lua_State *L, Udata *u) { |
| 685 | global_State *g = G(L); | 730 | global_State *g = G(L); |
| 686 | if (testbit(u->uv.marked, SEPARATED) || /* userdata is already separated... */ | 731 | if (testbit(u->uv.marked, SEPARATED) || /* userdata is already separated... */ |
| 687 | isfinalized(&u->uv) || /* ... or is finalized... */ | 732 | isfinalized(&u->uv) || /* ... or is finalized... */ |
| 688 | gfasttm(g, u->uv.metatable, TM_GC) == NULL) /* or has no finalization? */ | 733 | gfasttm(g, u->uv.metatable, TM_GC) == NULL) /* or has no finalizer? */ |
| 689 | return; /* nothing to be done */ | 734 | return; /* nothing to be done */ |
| 690 | else { /* move 'u' to 'udgc' list */ | 735 | else { /* move 'u' to 'udgc' list */ |
| 691 | GCObject **p; | 736 | GCObject **p; |
| 692 | for (p = &g->allgc; *p != obj2gco(u); p = &gch(*p)->next) ; | 737 | for (p = &g->allgc; *p != obj2gco(u); p = &gch(*p)->next) ; |
| 693 | *p = u->uv.next; /* remove 'u' from root list */ | 738 | *p = u->uv.next; /* remove 'u' from root list */ |
| 694 | u->uv.next = g->udgc; /* re-link it in list */ | 739 | u->uv.next = g->udgc; /* link it in list 'udgc' */ |
| 695 | g->udgc = obj2gco(u); | 740 | g->udgc = obj2gco(u); |
| 696 | l_setbit(u->uv.marked, SEPARATED); /* mark it as such */ | 741 | l_setbit(u->uv.marked, SEPARATED); /* mark it as such */ |
| 697 | } | 742 | } |
| @@ -733,12 +778,11 @@ static void atomic (lua_State *L) { | |||
| 733 | remarkupvals(g); | 778 | remarkupvals(g); |
| 734 | /* traverse objects caught by write barrier and by 'remarkupvals' */ | 779 | /* traverse objects caught by write barrier and by 'remarkupvals' */ |
| 735 | propagateall(g); | 780 | propagateall(g); |
| 736 | /* at this point, all strongly accessible objects are marked. | ||
| 737 | Start marking weakly accessible objects. */ | ||
| 738 | traverselistofgrays(g, &g->weak); /* remark weak tables */ | 781 | traverselistofgrays(g, &g->weak); /* remark weak tables */ |
| 739 | traverselistofgrays(g, &g->ephemeron); /* remark ephemeron tables */ | 782 | traverselistofgrays(g, &g->ephemeron); /* remark ephemeron tables */ |
| 740 | traverselistofgrays(g, &g->grayagain); /* remark gray again */ | 783 | traverselistofgrays(g, &g->grayagain); /* remark gray again */ |
| 741 | convergeephemerons(g); | 784 | convergeephemerons(g); |
| 785 | /* at this point, all strongly accessible objects are marked. */ | ||
| 742 | luaC_separateudata(L, 0); /* separate userdata to be finalized */ | 786 | luaC_separateudata(L, 0); /* separate userdata to be finalized */ |
| 743 | markbeingfnz(g); /* mark userdata that will be finalized */ | 787 | markbeingfnz(g); /* mark userdata that will be finalized */ |
| 744 | propagateall(g); /* remark, to propagate `preserveness' */ | 788 | propagateall(g); /* remark, to propagate `preserveness' */ |
| @@ -748,7 +792,6 @@ static void atomic (lua_State *L) { | |||
| 748 | cleartable(g->ephemeron); | 792 | cleartable(g->ephemeron); |
| 749 | cleartable(g->allweak); | 793 | cleartable(g->allweak); |
| 750 | g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ | 794 | g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ |
| 751 | g->sweepstrgc = 0; /* prepare sweep phase */ | ||
| 752 | } | 795 | } |
| 753 | 796 | ||
| 754 | 797 | ||
| @@ -767,6 +810,7 @@ static l_mem singlestep (lua_State *L) { | |||
| 767 | else { /* no more `gray' objects */ | 810 | else { /* no more `gray' objects */ |
| 768 | g->gcstate = GCSatomic; /* finish mark phase */ | 811 | g->gcstate = GCSatomic; /* finish mark phase */ |
| 769 | atomic(L); | 812 | atomic(L); |
| 813 | g->sweepstrgc = 0; /* prepare to sweep strings */ | ||
| 770 | g->gcstate = GCSsweepstring; | 814 | g->gcstate = GCSsweepstring; |
| 771 | return GCATOMICCOST; | 815 | return GCATOMICCOST; |
| 772 | } | 816 | } |
