diff --git a/lgc.c b/lgc.c index 9a00e1d0..baaca9a7 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.103 2001/06/12 18:43:13 roberto Exp roberto $ +** $Id: lgc.c,v 1.104 2001/06/13 18:51:20 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -62,7 +62,7 @@ static void markclosure (GCState *st, Closure *cl) { static void marktable (GCState *st, Hash *h) { if (!ismarked(h)) { - h->mark = st->tmark; /* chain it in list of marked */ + h->mark = st->tmark; /* chain it for later traversal */ st->tmark = h; } } @@ -127,8 +127,9 @@ static void traverseclosure (GCState *st, Closure *f) { static void removekey (Node *n) { + lua_assert(ttype(val(n)) == LUA_TNIL); if (ttype_key(n) != LUA_TNIL && ttype_key(n) != LUA_TNUMBER) - n->key_value.ts = NULL; /* dead key; remove it */ + ttype_key(n) = LUA_TNONE; /* dead key; remove it */ } @@ -180,30 +181,29 @@ static void markall (lua_State *L) { } -static int hasmark (const TObject *o) { - switch (ttype(o)) { +static int hasmark (int tt, Value *v) { + switch (tt) { case LUA_TSTRING: - return tsvalue(o)->marked; + return v->ts->marked; case LUA_TUSERDATA: - return ismarkedudata(uvalue(o)); + return ismarkedudata(v->u); case LUA_TTABLE: - return ismarked(hvalue(o)); + return ismarked(v->h); case LUA_TFUNCTION: - return ismarked(clvalue(o)); + return ismarked(v->cl); default: /* number, nil */ return 1; } } -static void invalidatetable (Hash *h) { +static void cleardeadnodes (Hash *h) { int i; for (i=0; isize; i++) { Node *n = node(h, i); - TObject k; if (ttype(val(n)) == LUA_TNIL) continue; /* empty node */ - setkey2obj(&k, n); - if (!hasmark(val(n)) || !hasmark(&k)) { + if (!hasmark(ttype(val(n)), &(val(n)->value)) || + !hasmark(ttype_key(n), &n->key_value)) { setnilvalue(val(n)); /* remove value */ removekey(n); } @@ -211,27 +211,26 @@ static void invalidatetable (Hash *h) { } -static void invalidatetables (global_State *G) { +static void cleartables (global_State *G) { Hash *h; for (h = G->roottable; h; h = h->next) { - if (ismarked(h) && h->weakmode) - invalidatetable(h); + if (h->weakmode && ismarked(h)) + cleardeadnodes(h); } } - static void collectproto (lua_State *L) { Proto **p = &G(L)->rootproto; - Proto *next; - while ((next = *p) != NULL) { - if (next->marked) { - next->marked = 0; - p = &next->next; + Proto *curr; + while ((curr = *p) != NULL) { + if (curr->marked) { + curr->marked = 0; + p = &curr->next; } else { - *p = next->next; - luaF_freeproto(L, next); + *p = curr->next; + luaF_freeproto(L, curr); } } } @@ -239,15 +238,15 @@ static void collectproto (lua_State *L) { static void collectclosure (lua_State *L) { Closure **p = &G(L)->rootcl; - Closure *next; - while ((next = *p) != NULL) { - if (ismarked(next)) { - next->mark = next; /* unmark */ - p = &next->next; + Closure *curr; + while ((curr = *p) != NULL) { + if (ismarked(curr)) { + curr->mark = curr; /* unmark */ + p = &curr->next; } else { - *p = next->next; - luaF_freeclosure(L, next); + *p = curr->next; + luaF_freeclosure(L, curr); } } } @@ -255,15 +254,15 @@ static void collectclosure (lua_State *L) { static void collecttable (lua_State *L) { Hash **p = &G(L)->roottable; - Hash *next; - while ((next = *p) != NULL) { - if (ismarked(next)) { - next->mark = next; /* unmark */ - p = &next->next; + Hash *curr; + while ((curr = *p) != NULL) { + if (ismarked(curr)) { + curr->mark = curr; /* unmark */ + p = &curr->next; } else { - *p = next->next; - luaH_free(L, next); + *p = curr->next; + luaH_free(L, curr); } } } @@ -271,17 +270,17 @@ static void collecttable (lua_State *L) { void luaC_collectudata (lua_State *L) { Udata **p = &G(L)->rootudata; - Udata *next; - while ((next = *p) != NULL) { - if (ismarkedudata(next)) { - switchudatamark(next); /* unmark */ - p = &next->next; + Udata *curr; + while ((curr = *p) != NULL) { + if (ismarkedudata(curr)) { + switchudatamark(curr); /* unmark */ + p = &curr->next; } else { /* collect */ - int tag = next->tag; - *p = next->next; - next->next = G(L)->TMtable[tag].collected; /* chain udata */ - G(L)->TMtable[tag].collected = next; + int tag = curr->tag; + *p = curr->next; + curr->next = G(L)->TMtable[tag].collected; /* chain udata */ + G(L)->TMtable[tag].collected = curr; } } } @@ -291,17 +290,17 @@ static void collectstrings (lua_State *L, int all) { int i; for (i=0; istrt.size; i++) { /* for each list */ TString **p = &G(L)->strt.hash[i]; - TString *next; - while ((next = *p) != NULL) { - if (next->marked && !all) { /* preserve? */ - if (next->marked < FIXMARK) /* does not change FIXMARKs */ - next->marked = 0; - p = &next->nexthash; + TString *curr; + while ((curr = *p) != NULL) { + if (curr->marked && !all) { /* preserve? */ + if (curr->marked < FIXMARK) /* does not change FIXMARKs */ + curr->marked = 0; + p = &curr->nexthash; } else { /* collect */ - *p = next->nexthash; + *p = curr->nexthash; G(L)->strt.nuse--; - luaM_free(L, next, sizestring(next->len)); + luaM_free(L, curr, sizestring(curr->len)); } } } @@ -365,11 +364,12 @@ void luaC_collect (lua_State *L, int all) { void luaC_collectgarbage (lua_State *L) { markall(L); - invalidatetables(G(L)); + cleartables(G(L)); luaC_collect(L, 0); checkMbuffer(L); - G(L)->GCthreshold = 2*G(L)->nblocks; /* set new threshold */ + G(L)->GCthreshold = 2*G(L)->nblocks; /* temporary threshold (for TM) */ luaC_callgcTMudata(L); + G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */ callgcTM(L, &luaO_nilobject); } diff --git a/lvm.c b/lvm.c index 5cadb252..3b17a0ad 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.183 2001/06/08 19:20:02 roberto Exp roberto $ +** $Id: lvm.c,v 1.184 2001/06/11 14:56:42 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -106,17 +106,17 @@ void luaV_Lclosure (lua_State *L, Proto *l, int nelems) { } -static void callTM (lua_State *L, Closure *f, const l_char *fmt, ...) { +/* maximum stack used by a call to a tag method (func + args) */ +#define MAXSTACK_TM 4 + +static StkId callTM (lua_State *L, Closure *f, const l_char *fmt, ...) { va_list argp; StkId base = L->top; - StkId result = NULL; /* result position */ + lua_assert(strlen(fmt)+1 <= MAXSTACK_TM); + luaD_checkstack(L, MAXSTACK_TM); va_start(argp, fmt); setclvalue(L->top, f); /* push function */ - incr_top; - if (*fmt == l_c('r')) { - fmt++; - result = va_arg(argp, TObject *); /* result position */ - } + L->top++; while (*fmt) { if (*fmt++ == l_c('o')) { setobj(L->top, va_arg(argp, TObject *)); @@ -125,17 +125,24 @@ static void callTM (lua_State *L, Closure *f, const l_char *fmt, ...) { lua_assert(*(fmt-1) == l_c('s')); setsvalue(L->top, va_arg(argp, TString *)); } - incr_top; + L->top++; } luaD_call(L, base); - if (result) { /* need result? */ - if (L->top == base) /* are there valid results? */ - setnilvalue(result); /* function had no results */ - else - setobj(result, base); /* get first result */ + va_end(argp); + return base; +} + + +#define setTM(L, base) (L->top = (base)) + +static void setTMresult (lua_State *L, TObject *result, StkId base) { + if (L->top == base) { /* are there valid results? */ + setnilvalue(result); /* function had no results */ + } + else { + setobj(result, base); /* get first result */ } L->top = base; /* restore top */ - va_end(argp); } @@ -164,7 +171,7 @@ void luaV_gettable (lua_State *L, StkId t, TObject *key, StkId res) { if (tm == NULL) /* no tag method? */ luaG_typeerror(L, t, l_s("index")); } - callTM(L, tm, l_s("roo"), res, t, key); + setTMresult(L, res, callTM(L, tm, l_s("oo"), t, key)); } @@ -187,7 +194,7 @@ void luaV_settable (lua_State *L, StkId t, TObject *key, StkId val) { if (tm == NULL) /* no tag method? */ luaG_typeerror(L, t, l_s("index")); } - callTM(L, tm, l_s("ooo"), t, key, val); + setTM(L, callTM(L, tm, l_s("ooo"), t, key, val)); } @@ -197,8 +204,9 @@ void luaV_getglobal (lua_State *L, TString *name, StkId res) { if (!HAS_TM_GETGLOBAL(L, ttype(value)) || /* is there a tag method? */ (tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL) { setobj(res, value); /* default behavior */ - } else - callTM(L, tm, l_s("rso"), res, name, value); + } + else + setTMresult(L, res, callTM(L, tm, l_s("so"), name, value)); } @@ -208,8 +216,9 @@ void luaV_setglobal (lua_State *L, TString *name, StkId val) { if (!HAS_TM_SETGLOBAL(L, ttype(oldvalue)) || /* no tag methods? */ (tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) { setobj(oldvalue, val); /* raw set */ - } else - callTM(L, tm, l_s("soo"), name, oldvalue, val); + } + else + setTM(L, callTM(L, tm, l_s("soo"), name, oldvalue, val)); } @@ -226,7 +235,7 @@ static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2, } } opname = luaS_new(L, luaT_eventname[event]); - callTM(L, tm, l_s("roos"), res, p1, p2, opname); + setTMresult(L, res, callTM(L, tm, l_s("oos"), p1, p2, opname)); return 1; } @@ -566,15 +575,16 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { return ra; } case OP_FORPREP: { - int jmp = GETARG_sBc(i); if (luaV_tonumber(ra, ra) == NULL) luaD_error(L, l_s("`for' initial value must be a number")); if (luaV_tonumber(ra+1, ra+1) == NULL) luaD_error(L, l_s("`for' limit must be a number")); if (luaV_tonumber(ra+2, ra+2) == NULL) luaD_error(L, l_s("`for' step must be a number")); - pc += -jmp; /* `jump' to loop end (delta is negated here) */ nvalue(ra) -= nvalue(ra+2);/* decrement index (to be incremented) */ + pc += -GETARG_sBc(i); /* `jump' to loop end (delta is negated here) */ + /* store in `ra+1' total number of repetitions */ + nvalue(ra+1) = ((nvalue(ra+1)-nvalue(ra))/nvalue(ra+2)); /* go through */ } case OP_FORLOOP: { @@ -582,28 +592,26 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { ttype(ra+2) == LUA_TNUMBER); if (ttype(ra) != LUA_TNUMBER) luaD_error(L, l_s("`for' index must be a number")); - nvalue(ra) += nvalue(ra+2); /* increment index */ - if (nvalue(ra+2) > 0 ? - nvalue(ra) <= nvalue(ra+1) : - nvalue(ra) >= nvalue(ra+1)) + if (--nvalue(ra+1) >= 0) { + nvalue(ra) += nvalue(ra+2); /* increment index */ dojump(pc, i); /* repeat loop */ + } break; } case OP_TFORPREP: { - int jmp = GETARG_sBc(i); if (ttype(ra) != LUA_TTABLE) luaD_error(L, l_s("`for' table must be a table")); setnvalue(ra+1, -1); /* initial index */ setnilvalue(ra+2); setnilvalue(ra+3); - pc += -jmp; /* `jump' to loop end (delta is negated here) */ + pc += -GETARG_sBc(i); /* `jump' to loop end (delta is negated here) */ /* go through */ } case OP_TFORLOOP: { Hash *t; int n; - runtime_check(L, ttype(ra) == LUA_TTABLE); - runtime_check(L, ttype(ra+1) == LUA_TNUMBER); + runtime_check(L, ttype(ra) == LUA_TTABLE && + ttype(ra+1) == LUA_TNUMBER); t = hvalue(ra); n = (int)nvalue(ra+1); n = luaH_nexti(t, n);