annotate src/lua/lgc.c @ 190:9a7a46c4aa1b

extending functionality of support namespaces in prepreation of give-pokemon
author Robert McIntyre <rlm@mit.edu>
date Thu, 22 Mar 2012 16:34:10 -0500
parents 27763b933818
children
rev   line source
rlm@1 1 /*
rlm@1 2 ** $Id: lgc.c,v 2.38.1.1 2007/12/27 13:02:25 roberto Exp $
rlm@1 3 ** Garbage Collector
rlm@1 4 ** See Copyright Notice in lua.h
rlm@1 5 */
rlm@1 6
rlm@1 7 #include <string.h>
rlm@1 8
rlm@1 9 #define lgc_c
rlm@1 10 #define LUA_CORE
rlm@1 11
rlm@1 12 #include "lua.h"
rlm@1 13
rlm@1 14 #include "ldebug.h"
rlm@1 15 #include "ldo.h"
rlm@1 16 #include "lfunc.h"
rlm@1 17 #include "lgc.h"
rlm@1 18 #include "lmem.h"
rlm@1 19 #include "lobject.h"
rlm@1 20 #include "lstate.h"
rlm@1 21 #include "lstring.h"
rlm@1 22 #include "ltable.h"
rlm@1 23 #include "ltm.h"
rlm@1 24
rlm@1 25
rlm@1 26 #define GCSTEPSIZE 1024u
rlm@1 27 #define GCSWEEPMAX 40
rlm@1 28 #define GCSWEEPCOST 10
rlm@1 29 #define GCFINALIZECOST 100
rlm@1 30
rlm@1 31
rlm@1 32 #define maskmarks cast_byte(~(bitmask(BLACKBIT)|WHITEBITS))
rlm@1 33
rlm@1 34 #define makewhite(g,x) \
rlm@1 35 ((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g)))
rlm@1 36
rlm@1 37 #define white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
rlm@1 38 #define black2gray(x) resetbit((x)->gch.marked, BLACKBIT)
rlm@1 39
rlm@1 40 #define stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT)
rlm@1 41
rlm@1 42
rlm@1 43 #define isfinalized(u) testbit((u)->marked, FINALIZEDBIT)
rlm@1 44 #define markfinalized(u) l_setbit((u)->marked, FINALIZEDBIT)
rlm@1 45
rlm@1 46
rlm@1 47 #define KEYWEAK bitmask(KEYWEAKBIT)
rlm@1 48 #define VALUEWEAK bitmask(VALUEWEAKBIT)
rlm@1 49
rlm@1 50
rlm@1 51
rlm@1 52 #define markvalue(g,o) { checkconsistency(o); \
rlm@1 53 if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); }
rlm@1 54
rlm@1 55 #define markobject(g,t) { if (iswhite(obj2gco(t))) \
rlm@1 56 reallymarkobject(g, obj2gco(t)); }
rlm@1 57
rlm@1 58
rlm@1 59 #define setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpause)
rlm@1 60
rlm@1 61
rlm@1 62 static void removeentry (Node *n) {
rlm@1 63 lua_assert(ttisnil(gval(n)));
rlm@1 64 if (iscollectable(gkey(n)))
rlm@1 65 setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */
rlm@1 66 }
rlm@1 67
rlm@1 68
rlm@1 69 static void reallymarkobject (global_State *g, GCObject *o) {
rlm@1 70 lua_assert(iswhite(o) && !isdead(g, o));
rlm@1 71 white2gray(o);
rlm@1 72 switch (o->gch.tt) {
rlm@1 73 case LUA_TSTRING: {
rlm@1 74 return;
rlm@1 75 }
rlm@1 76 case LUA_TUSERDATA: {
rlm@1 77 Table *mt = gco2u(o)->metatable;
rlm@1 78 gray2black(o); /* udata are never gray */
rlm@1 79 if (mt) markobject(g, mt);
rlm@1 80 markobject(g, gco2u(o)->env);
rlm@1 81 return;
rlm@1 82 }
rlm@1 83 case LUA_TUPVAL: {
rlm@1 84 UpVal *uv = gco2uv(o);
rlm@1 85 markvalue(g, uv->v);
rlm@1 86 if (uv->v == &uv->u.value) /* closed? */
rlm@1 87 gray2black(o); /* open upvalues are never black */
rlm@1 88 return;
rlm@1 89 }
rlm@1 90 case LUA_TFUNCTION: {
rlm@1 91 gco2cl(o)->c.gclist = g->gray;
rlm@1 92 g->gray = o;
rlm@1 93 break;
rlm@1 94 }
rlm@1 95 case LUA_TTABLE: {
rlm@1 96 gco2h(o)->gclist = g->gray;
rlm@1 97 g->gray = o;
rlm@1 98 break;
rlm@1 99 }
rlm@1 100 case LUA_TTHREAD: {
rlm@1 101 gco2th(o)->gclist = g->gray;
rlm@1 102 g->gray = o;
rlm@1 103 break;
rlm@1 104 }
rlm@1 105 case LUA_TPROTO: {
rlm@1 106 gco2p(o)->gclist = g->gray;
rlm@1 107 g->gray = o;
rlm@1 108 break;
rlm@1 109 }
rlm@1 110 default: lua_assert(0);
rlm@1 111 }
rlm@1 112 }
rlm@1 113
rlm@1 114
rlm@1 115 static void marktmu (global_State *g) {
rlm@1 116 GCObject *u = g->tmudata;
rlm@1 117 if (u) {
rlm@1 118 do {
rlm@1 119 u = u->gch.next;
rlm@1 120 makewhite(g, u); /* may be marked, if left from previous GC */
rlm@1 121 reallymarkobject(g, u);
rlm@1 122 } while (u != g->tmudata);
rlm@1 123 }
rlm@1 124 }
rlm@1 125
rlm@1 126
rlm@1 127 /* move `dead' udata that need finalization to list `tmudata' */
rlm@1 128 size_t luaC_separateudata (lua_State *L, int all) {
rlm@1 129 global_State *g = G(L);
rlm@1 130 size_t deadmem = 0;
rlm@1 131 GCObject **p = &g->mainthread->next;
rlm@1 132 GCObject *curr;
rlm@1 133 while ((curr = *p) != NULL) {
rlm@1 134 if (!(iswhite(curr) || all) || isfinalized(gco2u(curr)))
rlm@1 135 p = &curr->gch.next; /* don't bother with them */
rlm@1 136 else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) {
rlm@1 137 markfinalized(gco2u(curr)); /* don't need finalization */
rlm@1 138 p = &curr->gch.next;
rlm@1 139 }
rlm@1 140 else { /* must call its gc method */
rlm@1 141 deadmem += sizeudata(gco2u(curr));
rlm@1 142 markfinalized(gco2u(curr));
rlm@1 143 *p = curr->gch.next;
rlm@1 144 /* link `curr' at the end of `tmudata' list */
rlm@1 145 if (g->tmudata == NULL) /* list is empty? */
rlm@1 146 g->tmudata = curr->gch.next = curr; /* creates a circular list */
rlm@1 147 else {
rlm@1 148 curr->gch.next = g->tmudata->gch.next;
rlm@1 149 g->tmudata->gch.next = curr;
rlm@1 150 g->tmudata = curr;
rlm@1 151 }
rlm@1 152 }
rlm@1 153 }
rlm@1 154 return deadmem;
rlm@1 155 }
rlm@1 156
rlm@1 157
rlm@1 158 static int traversetable (global_State *g, Table *h) {
rlm@1 159 int i;
rlm@1 160 int weakkey = 0;
rlm@1 161 int weakvalue = 0;
rlm@1 162 const TValue *mode;
rlm@1 163 if (h->metatable)
rlm@1 164 markobject(g, h->metatable);
rlm@1 165 mode = gfasttm(g, h->metatable, TM_MODE);
rlm@1 166 if (mode && ttisstring(mode)) { /* is there a weak mode? */
rlm@1 167 weakkey = (strchr(svalue(mode), 'k') != NULL);
rlm@1 168 weakvalue = (strchr(svalue(mode), 'v') != NULL);
rlm@1 169 if (weakkey || weakvalue) { /* is really weak? */
rlm@1 170 h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */
rlm@1 171 h->marked |= cast_byte((weakkey << KEYWEAKBIT) |
rlm@1 172 (weakvalue << VALUEWEAKBIT));
rlm@1 173 h->gclist = g->weak; /* must be cleared after GC, ... */
rlm@1 174 g->weak = obj2gco(h); /* ... so put in the appropriate list */
rlm@1 175 }
rlm@1 176 }
rlm@1 177 if (weakkey && weakvalue) return 1;
rlm@1 178 if (!weakvalue) {
rlm@1 179 i = h->sizearray;
rlm@1 180 while (i--)
rlm@1 181 markvalue(g, &h->array[i]);
rlm@1 182 }
rlm@1 183 i = sizenode(h);
rlm@1 184 while (i--) {
rlm@1 185 Node *n = gnode(h, i);
rlm@1 186 lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n)));
rlm@1 187 if (ttisnil(gval(n)))
rlm@1 188 removeentry(n); /* remove empty entries */
rlm@1 189 else {
rlm@1 190 lua_assert(!ttisnil(gkey(n)));
rlm@1 191 if (!weakkey) markvalue(g, gkey(n));
rlm@1 192 if (!weakvalue) markvalue(g, gval(n));
rlm@1 193 }
rlm@1 194 }
rlm@1 195 return weakkey || weakvalue;
rlm@1 196 }
rlm@1 197
rlm@1 198
rlm@1 199 /*
rlm@1 200 ** All marks are conditional because a GC may happen while the
rlm@1 201 ** prototype is still being created
rlm@1 202 */
rlm@1 203 static void traverseproto (global_State *g, Proto *f) {
rlm@1 204 int i;
rlm@1 205 if (f->source) stringmark(f->source);
rlm@1 206 for (i=0; i<f->sizek; i++) /* mark literals */
rlm@1 207 markvalue(g, &f->k[i]);
rlm@1 208 for (i=0; i<f->sizeupvalues; i++) { /* mark upvalue names */
rlm@1 209 if (f->upvalues[i])
rlm@1 210 stringmark(f->upvalues[i]);
rlm@1 211 }
rlm@1 212 for (i=0; i<f->sizep; i++) { /* mark nested protos */
rlm@1 213 if (f->p[i])
rlm@1 214 markobject(g, f->p[i]);
rlm@1 215 }
rlm@1 216 for (i=0; i<f->sizelocvars; i++) { /* mark local-variable names */
rlm@1 217 if (f->locvars[i].varname)
rlm@1 218 stringmark(f->locvars[i].varname);
rlm@1 219 }
rlm@1 220 }
rlm@1 221
rlm@1 222
rlm@1 223
rlm@1 224 static void traverseclosure (global_State *g, Closure *cl) {
rlm@1 225 markobject(g, cl->c.env);
rlm@1 226 if (cl->c.isC) {
rlm@1 227 int i;
rlm@1 228 for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
rlm@1 229 markvalue(g, &cl->c.upvalue[i]);
rlm@1 230 }
rlm@1 231 else {
rlm@1 232 int i;
rlm@1 233 lua_assert(cl->l.nupvalues == cl->l.p->nups);
rlm@1 234 markobject(g, cl->l.p);
rlm@1 235 for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */
rlm@1 236 markobject(g, cl->l.upvals[i]);
rlm@1 237 }
rlm@1 238 }
rlm@1 239
rlm@1 240
rlm@1 241 static void checkstacksizes (lua_State *L, StkId max) {
rlm@1 242 int ci_used = cast_int(L->ci - L->base_ci); /* number of `ci' in use */
rlm@1 243 int s_used = cast_int(max - L->stack); /* part of stack in use */
rlm@1 244 if (L->size_ci > LUAI_MAXCALLS) /* handling overflow? */
rlm@1 245 return; /* do not touch the stacks */
rlm@1 246 if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
rlm@1 247 luaD_reallocCI(L, L->size_ci/2); /* still big enough... */
rlm@1 248 condhardstacktests(luaD_reallocCI(L, ci_used + 1));
rlm@1 249 if (4*s_used < L->stacksize &&
rlm@1 250 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize)
rlm@1 251 luaD_reallocstack(L, L->stacksize/2); /* still big enough... */
rlm@1 252 condhardstacktests(luaD_reallocstack(L, s_used));
rlm@1 253 }
rlm@1 254
rlm@1 255
rlm@1 256 static void traversestack (global_State *g, lua_State *l) {
rlm@1 257 StkId o, lim;
rlm@1 258 CallInfo *ci;
rlm@1 259 markvalue(g, gt(l));
rlm@1 260 lim = l->top;
rlm@1 261 for (ci = l->base_ci; ci <= l->ci; ci++) {
rlm@1 262 lua_assert(ci->top <= l->stack_last);
rlm@1 263 if (lim < ci->top) lim = ci->top;
rlm@1 264 }
rlm@1 265 for (o = l->stack; o < l->top; o++)
rlm@1 266 markvalue(g, o);
rlm@1 267 for (; o <= lim; o++)
rlm@1 268 setnilvalue(o);
rlm@1 269 checkstacksizes(l, lim);
rlm@1 270 }
rlm@1 271
rlm@1 272
rlm@1 273 /*
rlm@1 274 ** traverse one gray object, turning it to black.
rlm@1 275 ** Returns `quantity' traversed.
rlm@1 276 */
rlm@1 277 static l_mem propagatemark (global_State *g) {
rlm@1 278 GCObject *o = g->gray;
rlm@1 279 lua_assert(isgray(o));
rlm@1 280 gray2black(o);
rlm@1 281 switch (o->gch.tt) {
rlm@1 282 case LUA_TTABLE: {
rlm@1 283 Table *h = gco2h(o);
rlm@1 284 g->gray = h->gclist;
rlm@1 285 if (traversetable(g, h)) /* table is weak? */
rlm@1 286 black2gray(o); /* keep it gray */
rlm@1 287 return sizeof(Table) + sizeof(TValue) * h->sizearray +
rlm@1 288 sizeof(Node) * sizenode(h);
rlm@1 289 }
rlm@1 290 case LUA_TFUNCTION: {
rlm@1 291 Closure *cl = gco2cl(o);
rlm@1 292 g->gray = cl->c.gclist;
rlm@1 293 traverseclosure(g, cl);
rlm@1 294 return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) :
rlm@1 295 sizeLclosure(cl->l.nupvalues);
rlm@1 296 }
rlm@1 297 case LUA_TTHREAD: {
rlm@1 298 lua_State *th = gco2th(o);
rlm@1 299 g->gray = th->gclist;
rlm@1 300 th->gclist = g->grayagain;
rlm@1 301 g->grayagain = o;
rlm@1 302 black2gray(o);
rlm@1 303 traversestack(g, th);
rlm@1 304 return sizeof(lua_State) + sizeof(TValue) * th->stacksize +
rlm@1 305 sizeof(CallInfo) * th->size_ci;
rlm@1 306 }
rlm@1 307 case LUA_TPROTO: {
rlm@1 308 Proto *p = gco2p(o);
rlm@1 309 g->gray = p->gclist;
rlm@1 310 traverseproto(g, p);
rlm@1 311 return sizeof(Proto) + sizeof(Instruction) * p->sizecode +
rlm@1 312 sizeof(Proto *) * p->sizep +
rlm@1 313 sizeof(TValue) * p->sizek +
rlm@1 314 sizeof(int) * p->sizelineinfo +
rlm@1 315 sizeof(LocVar) * p->sizelocvars +
rlm@1 316 sizeof(TString *) * p->sizeupvalues;
rlm@1 317 }
rlm@1 318 default: lua_assert(0); return 0;
rlm@1 319 }
rlm@1 320 }
rlm@1 321
rlm@1 322
rlm@1 323 static size_t propagateall (global_State *g) {
rlm@1 324 size_t m = 0;
rlm@1 325 while (g->gray) m += propagatemark(g);
rlm@1 326 return m;
rlm@1 327 }
rlm@1 328
rlm@1 329
rlm@1 330 /*
rlm@1 331 ** The next function tells whether a key or value can be cleared from
rlm@1 332 ** a weak table. Non-collectable objects are never removed from weak
rlm@1 333 ** tables. Strings behave as `values', so are never removed too. for
rlm@1 334 ** other objects: if really collected, cannot keep them; for userdata
rlm@1 335 ** being finalized, keep them in keys, but not in values
rlm@1 336 */
rlm@1 337 static int iscleared (const TValue *o, int iskey) {
rlm@1 338 if (!iscollectable(o)) return 0;
rlm@1 339 if (ttisstring(o)) {
rlm@1 340 stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */
rlm@1 341 return 0;
rlm@1 342 }
rlm@1 343 return iswhite(gcvalue(o)) ||
rlm@1 344 (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o))));
rlm@1 345 }
rlm@1 346
rlm@1 347
rlm@1 348 /*
rlm@1 349 ** clear collected entries from weaktables
rlm@1 350 */
rlm@1 351 static void cleartable (GCObject *l) {
rlm@1 352 while (l) {
rlm@1 353 Table *h = gco2h(l);
rlm@1 354 int i = h->sizearray;
rlm@1 355 lua_assert(testbit(h->marked, VALUEWEAKBIT) ||
rlm@1 356 testbit(h->marked, KEYWEAKBIT));
rlm@1 357 if (testbit(h->marked, VALUEWEAKBIT)) {
rlm@1 358 while (i--) {
rlm@1 359 TValue *o = &h->array[i];
rlm@1 360 if (iscleared(o, 0)) /* value was collected? */
rlm@1 361 setnilvalue(o); /* remove value */
rlm@1 362 }
rlm@1 363 }
rlm@1 364 i = sizenode(h);
rlm@1 365 while (i--) {
rlm@1 366 Node *n = gnode(h, i);
rlm@1 367 if (!ttisnil(gval(n)) && /* non-empty entry? */
rlm@1 368 (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) {
rlm@1 369 setnilvalue(gval(n)); /* remove value ... */
rlm@1 370 removeentry(n); /* remove entry from table */
rlm@1 371 }
rlm@1 372 }
rlm@1 373 l = h->gclist;
rlm@1 374 }
rlm@1 375 }
rlm@1 376
rlm@1 377
rlm@1 378 static void freeobj (lua_State *L, GCObject *o) {
rlm@1 379 switch (o->gch.tt) {
rlm@1 380 case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
rlm@1 381 case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break;
rlm@1 382 case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break;
rlm@1 383 case LUA_TTABLE: luaH_free(L, gco2h(o)); break;
rlm@1 384 case LUA_TTHREAD: {
rlm@1 385 lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread);
rlm@1 386 luaE_freethread(L, gco2th(o));
rlm@1 387 break;
rlm@1 388 }
rlm@1 389 case LUA_TSTRING: {
rlm@1 390 G(L)->strt.nuse--;
rlm@1 391 luaM_freemem(L, o, sizestring(gco2ts(o)));
rlm@1 392 break;
rlm@1 393 }
rlm@1 394 case LUA_TUSERDATA: {
rlm@1 395 luaM_freemem(L, o, sizeudata(gco2u(o)));
rlm@1 396 break;
rlm@1 397 }
rlm@1 398 default: lua_assert(0);
rlm@1 399 }
rlm@1 400 }
rlm@1 401
rlm@1 402
rlm@1 403
rlm@1 404 #define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM)
rlm@1 405
rlm@1 406
rlm@1 407 static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
rlm@1 408 GCObject *curr;
rlm@1 409 global_State *g = G(L);
rlm@1 410 int deadmask = otherwhite(g);
rlm@1 411 while ((curr = *p) != NULL && count-- > 0) {
rlm@1 412 if (curr->gch.tt == LUA_TTHREAD) /* sweep open upvalues of each thread */
rlm@1 413 sweepwholelist(L, &gco2th(curr)->openupval);
rlm@1 414 if ((curr->gch.marked ^ WHITEBITS) & deadmask) { /* not dead? */
rlm@1 415 lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT));
rlm@1 416 makewhite(g, curr); /* make it white (for next cycle) */
rlm@1 417 p = &curr->gch.next;
rlm@1 418 }
rlm@1 419 else { /* must erase `curr' */
rlm@1 420 lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT));
rlm@1 421 *p = curr->gch.next;
rlm@1 422 if (curr == g->rootgc) /* is the first element of the list? */
rlm@1 423 g->rootgc = curr->gch.next; /* adjust first */
rlm@1 424 freeobj(L, curr);
rlm@1 425 }
rlm@1 426 }
rlm@1 427 return p;
rlm@1 428 }
rlm@1 429
rlm@1 430
rlm@1 431 static void checkSizes (lua_State *L) {
rlm@1 432 global_State *g = G(L);
rlm@1 433 /* check size of string hash */
rlm@1 434 if (g->strt.nuse < cast(lu_int32, g->strt.size/4) &&
rlm@1 435 g->strt.size > MINSTRTABSIZE*2)
rlm@1 436 luaS_resize(L, g->strt.size/2); /* table is too big */
rlm@1 437 /* check size of buffer */
rlm@1 438 if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) { /* buffer too big? */
rlm@1 439 size_t newsize = luaZ_sizebuffer(&g->buff) / 2;
rlm@1 440 luaZ_resizebuffer(L, &g->buff, newsize);
rlm@1 441 }
rlm@1 442 }
rlm@1 443
rlm@1 444
rlm@1 445 static void GCTM (lua_State *L) {
rlm@1 446 global_State *g = G(L);
rlm@1 447 GCObject *o = g->tmudata->gch.next; /* get first element */
rlm@1 448 Udata *udata = rawgco2u(o);
rlm@1 449 const TValue *tm;
rlm@1 450 /* remove udata from `tmudata' */
rlm@1 451 if (o == g->tmudata) /* last element? */
rlm@1 452 g->tmudata = NULL;
rlm@1 453 else
rlm@1 454 g->tmudata->gch.next = udata->uv.next;
rlm@1 455 udata->uv.next = g->mainthread->next; /* return it to `root' list */
rlm@1 456 g->mainthread->next = o;
rlm@1 457 makewhite(g, o);
rlm@1 458 tm = fasttm(L, udata->uv.metatable, TM_GC);
rlm@1 459 if (tm != NULL) {
rlm@1 460 lu_byte oldah = L->allowhook;
rlm@1 461 lu_mem oldt = g->GCthreshold;
rlm@1 462 L->allowhook = 0; /* stop debug hooks during GC tag method */
rlm@1 463 g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */
rlm@1 464 setobj2s(L, L->top, tm);
rlm@1 465 setuvalue(L, L->top+1, udata);
rlm@1 466 L->top += 2;
rlm@1 467 luaD_call(L, L->top - 2, 0);
rlm@1 468 L->allowhook = oldah; /* restore hooks */
rlm@1 469 g->GCthreshold = oldt; /* restore threshold */
rlm@1 470 }
rlm@1 471 }
rlm@1 472
rlm@1 473
rlm@1 474 /*
rlm@1 475 ** Call all GC tag methods
rlm@1 476 */
rlm@1 477 void luaC_callGCTM (lua_State *L) {
rlm@1 478 while (G(L)->tmudata)
rlm@1 479 GCTM(L);
rlm@1 480 }
rlm@1 481
rlm@1 482
rlm@1 483 void luaC_freeall (lua_State *L) {
rlm@1 484 global_State *g = G(L);
rlm@1 485 int i;
rlm@1 486 g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT); /* mask to collect all elements */
rlm@1 487 sweepwholelist(L, &g->rootgc);
rlm@1 488 for (i = 0; i < g->strt.size; i++) /* free all string lists */
rlm@1 489 sweepwholelist(L, &g->strt.hash[i]);
rlm@1 490 }
rlm@1 491
rlm@1 492
rlm@1 493 static void markmt (global_State *g) {
rlm@1 494 int i;
rlm@1 495 for (i=0; i<NUM_TAGS; i++)
rlm@1 496 if (g->mt[i]) markobject(g, g->mt[i]);
rlm@1 497 }
rlm@1 498
rlm@1 499
rlm@1 500 /* mark root set */
rlm@1 501 static void markroot (lua_State *L) {
rlm@1 502 global_State *g = G(L);
rlm@1 503 g->gray = NULL;
rlm@1 504 g->grayagain = NULL;
rlm@1 505 g->weak = NULL;
rlm@1 506 markobject(g, g->mainthread);
rlm@1 507 /* make global table be traversed before main stack */
rlm@1 508 markvalue(g, gt(g->mainthread));
rlm@1 509 markvalue(g, registry(L));
rlm@1 510 markmt(g);
rlm@1 511 g->gcstate = GCSpropagate;
rlm@1 512 }
rlm@1 513
rlm@1 514
rlm@1 515 static void remarkupvals (global_State *g) {
rlm@1 516 UpVal *uv;
rlm@1 517 for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
rlm@1 518 lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
rlm@1 519 if (isgray(obj2gco(uv)))
rlm@1 520 markvalue(g, uv->v);
rlm@1 521 }
rlm@1 522 }
rlm@1 523
rlm@1 524
rlm@1 525 static void atomic (lua_State *L) {
rlm@1 526 global_State *g = G(L);
rlm@1 527 size_t udsize; /* total size of userdata to be finalized */
rlm@1 528 /* remark occasional upvalues of (maybe) dead threads */
rlm@1 529 remarkupvals(g);
rlm@1 530 /* traverse objects cautch by write barrier and by 'remarkupvals' */
rlm@1 531 propagateall(g);
rlm@1 532 /* remark weak tables */
rlm@1 533 g->gray = g->weak;
rlm@1 534 g->weak = NULL;
rlm@1 535 lua_assert(!iswhite(obj2gco(g->mainthread)));
rlm@1 536 markobject(g, L); /* mark running thread */
rlm@1 537 markmt(g); /* mark basic metatables (again) */
rlm@1 538 propagateall(g);
rlm@1 539 /* remark gray again */
rlm@1 540 g->gray = g->grayagain;
rlm@1 541 g->grayagain = NULL;
rlm@1 542 propagateall(g);
rlm@1 543 udsize = luaC_separateudata(L, 0); /* separate userdata to be finalized */
rlm@1 544 marktmu(g); /* mark `preserved' userdata */
rlm@1 545 udsize += propagateall(g); /* remark, to propagate `preserveness' */
rlm@1 546 cleartable(g->weak); /* remove collected objects from weak tables */
rlm@1 547 /* flip current white */
rlm@1 548 g->currentwhite = cast_byte(otherwhite(g));
rlm@1 549 g->sweepstrgc = 0;
rlm@1 550 g->sweepgc = &g->rootgc;
rlm@1 551 g->gcstate = GCSsweepstring;
rlm@1 552 g->estimate = g->totalbytes - udsize; /* first estimate */
rlm@1 553 }
rlm@1 554
rlm@1 555
rlm@1 556 static l_mem singlestep (lua_State *L) {
rlm@1 557 global_State *g = G(L);
rlm@1 558 /*lua_checkmemory(L);*/
rlm@1 559 switch (g->gcstate) {
rlm@1 560 case GCSpause: {
rlm@1 561 markroot(L); /* start a new collection */
rlm@1 562 return 0;
rlm@1 563 }
rlm@1 564 case GCSpropagate: {
rlm@1 565 if (g->gray)
rlm@1 566 return propagatemark(g);
rlm@1 567 else { /* no more `gray' objects */
rlm@1 568 atomic(L); /* finish mark phase */
rlm@1 569 return 0;
rlm@1 570 }
rlm@1 571 }
rlm@1 572 case GCSsweepstring: {
rlm@1 573 lu_mem old = g->totalbytes;
rlm@1 574 sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]);
rlm@1 575 if (g->sweepstrgc >= g->strt.size) /* nothing more to sweep? */
rlm@1 576 g->gcstate = GCSsweep; /* end sweep-string phase */
rlm@1 577 lua_assert(old >= g->totalbytes);
rlm@1 578 g->estimate -= old - g->totalbytes;
rlm@1 579 return GCSWEEPCOST;
rlm@1 580 }
rlm@1 581 case GCSsweep: {
rlm@1 582 lu_mem old = g->totalbytes;
rlm@1 583 g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
rlm@1 584 if (*g->sweepgc == NULL) { /* nothing more to sweep? */
rlm@1 585 checkSizes(L);
rlm@1 586 g->gcstate = GCSfinalize; /* end sweep phase */
rlm@1 587 }
rlm@1 588 lua_assert(old >= g->totalbytes);
rlm@1 589 g->estimate -= old - g->totalbytes;
rlm@1 590 return GCSWEEPMAX*GCSWEEPCOST;
rlm@1 591 }
rlm@1 592 case GCSfinalize: {
rlm@1 593 if (g->tmudata) {
rlm@1 594 GCTM(L);
rlm@1 595 if (g->estimate > GCFINALIZECOST)
rlm@1 596 g->estimate -= GCFINALIZECOST;
rlm@1 597 return GCFINALIZECOST;
rlm@1 598 }
rlm@1 599 else {
rlm@1 600 g->gcstate = GCSpause; /* end collection */
rlm@1 601 g->gcdept = 0;
rlm@1 602 return 0;
rlm@1 603 }
rlm@1 604 }
rlm@1 605 default: lua_assert(0); return 0;
rlm@1 606 }
rlm@1 607 }
rlm@1 608
rlm@1 609
rlm@1 610 void luaC_step (lua_State *L) {
rlm@1 611 global_State *g = G(L);
rlm@1 612 l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul;
rlm@1 613 if (lim == 0)
rlm@1 614 lim = (MAX_LUMEM-1)/2; /* no limit */
rlm@1 615 g->gcdept += g->totalbytes - g->GCthreshold;
rlm@1 616 do {
rlm@1 617 lim -= singlestep(L);
rlm@1 618 if (g->gcstate == GCSpause)
rlm@1 619 break;
rlm@1 620 } while (lim > 0);
rlm@1 621 if (g->gcstate != GCSpause) {
rlm@1 622 if (g->gcdept < GCSTEPSIZE)
rlm@1 623 g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/g->gcstepmul;*/
rlm@1 624 else {
rlm@1 625 g->gcdept -= GCSTEPSIZE;
rlm@1 626 g->GCthreshold = g->totalbytes;
rlm@1 627 }
rlm@1 628 }
rlm@1 629 else {
rlm@1 630 lua_assert(g->totalbytes >= g->estimate);
rlm@1 631 setthreshold(g);
rlm@1 632 }
rlm@1 633 }
rlm@1 634
rlm@1 635
rlm@1 636 void luaC_fullgc (lua_State *L) {
rlm@1 637 global_State *g = G(L);
rlm@1 638 if (g->gcstate <= GCSpropagate) {
rlm@1 639 /* reset sweep marks to sweep all elements (returning them to white) */
rlm@1 640 g->sweepstrgc = 0;
rlm@1 641 g->sweepgc = &g->rootgc;
rlm@1 642 /* reset other collector lists */
rlm@1 643 g->gray = NULL;
rlm@1 644 g->grayagain = NULL;
rlm@1 645 g->weak = NULL;
rlm@1 646 g->gcstate = GCSsweepstring;
rlm@1 647 }
rlm@1 648 lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate);
rlm@1 649 /* finish any pending sweep phase */
rlm@1 650 while (g->gcstate != GCSfinalize) {
rlm@1 651 lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep);
rlm@1 652 singlestep(L);
rlm@1 653 }
rlm@1 654 markroot(L);
rlm@1 655 while (g->gcstate != GCSpause) {
rlm@1 656 singlestep(L);
rlm@1 657 }
rlm@1 658 setthreshold(g);
rlm@1 659 }
rlm@1 660
rlm@1 661
rlm@1 662 void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) {
rlm@1 663 global_State *g = G(L);
rlm@1 664 lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
rlm@1 665 lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
rlm@1 666 lua_assert(ttype(&o->gch) != LUA_TTABLE);
rlm@1 667 /* must keep invariant? */
rlm@1 668 if (g->gcstate == GCSpropagate)
rlm@1 669 reallymarkobject(g, v); /* restore invariant */
rlm@1 670 else /* don't mind */
rlm@1 671 makewhite(g, o); /* mark as white just to avoid other barriers */
rlm@1 672 }
rlm@1 673
rlm@1 674
rlm@1 675 void luaC_barrierback (lua_State *L, Table *t) {
rlm@1 676 global_State *g = G(L);
rlm@1 677 GCObject *o = obj2gco(t);
rlm@1 678 lua_assert(isblack(o) && !isdead(g, o));
rlm@1 679 lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
rlm@1 680 black2gray(o); /* make table gray (again) */
rlm@1 681 t->gclist = g->grayagain;
rlm@1 682 g->grayagain = o;
rlm@1 683 }
rlm@1 684
rlm@1 685
rlm@1 686 void luaC_link (lua_State *L, GCObject *o, lu_byte tt) {
rlm@1 687 global_State *g = G(L);
rlm@1 688 o->gch.next = g->rootgc;
rlm@1 689 g->rootgc = o;
rlm@1 690 o->gch.marked = luaC_white(g);
rlm@1 691 o->gch.tt = tt;
rlm@1 692 }
rlm@1 693
rlm@1 694
rlm@1 695 void luaC_linkupval (lua_State *L, UpVal *uv) {
rlm@1 696 global_State *g = G(L);
rlm@1 697 GCObject *o = obj2gco(uv);
rlm@1 698 o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */
rlm@1 699 g->rootgc = o;
rlm@1 700 if (isgray(o)) {
rlm@1 701 if (g->gcstate == GCSpropagate) {
rlm@1 702 gray2black(o); /* closed upvalues need barrier */
rlm@1 703 luaC_barrier(L, uv, uv->v);
rlm@1 704 }
rlm@1 705 else { /* sweep phase: sweep it (turning it into white) */
rlm@1 706 makewhite(g, o);
rlm@1 707 lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
rlm@1 708 }
rlm@1 709 }
rlm@1 710 }
rlm@1 711