From ba11831d357889ee090ce92ff508957c6c023c42 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 7 Jun 2001 12:01:21 -0300 Subject: smaller structs for udata and for strings --- lcode.c | 4 ++-- lgc.c | 11 ++++++----- llex.c | 4 ++-- lobject.h | 13 ++++++++----- lstring.c | 42 +++++++++++++++++++++++------------------- 5 files changed, 41 insertions(+), 33 deletions(-) diff --git a/lcode.c b/lcode.c index 8c43084e..29a6bc89 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 1.69 2001/06/05 18:17:01 roberto Exp roberto $ +** $Id: lcode.c,v 1.70 2001/06/06 18:00:19 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -231,7 +231,7 @@ int luaK_stringk (FuncState *fs, TString *s) { TObject o; setsvalue(&o, s); c = addk(fs, &o); - s->constindex = c; /* hint for next time */ + s->constindex = (unsigned short)c; /* hint for next time */ } return c; } diff --git a/lgc.c b/lgc.c index 86b7d092..fcd22a86 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.99 2001/06/05 19:27:32 roberto Exp roberto $ +** $Id: lgc.c,v 1.100 2001/06/06 18:00:19 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -86,7 +86,8 @@ static void markobject (GCState *st, TObject *o) { strmark(tsvalue(o)); break; case LUA_TUSERDATA: - uvalue(o)->marked = 1; + if (!ismarkedudata(uvalue(o))) + switchudatamark(uvalue(o)); break; case LUA_TFUNCTION: markclosure(st, clvalue(o)); @@ -196,7 +197,7 @@ static int hasmark (const TObject *o) { case LUA_TSTRING: return tsvalue(o)->marked; case LUA_TUSERDATA: - return uvalue(o)->marked; + return ismarkedudata(uvalue(o)); case LUA_TTABLE: return ismarked(hvalue(o)); case LUA_TFUNCTION: @@ -284,8 +285,8 @@ static void collectudata (lua_State *L) { Udata **p = &G(L)->rootudata; Udata *next; while ((next = *p) != NULL) { - if (next->marked) { - next->marked = 0; /* unmark */ + if (ismarkedudata(next)) { + switchudatamark(next); /* unmark */ p = &next->next; } else { /* collect */ diff --git a/llex.c b/llex.c index dfe7dbac..74b1982a 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 1.83 2001/03/07 12:49:37 roberto Exp roberto $ +** $Id: llex.c,v 1.84 2001/03/26 14:31:49 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -40,7 +40,7 @@ void luaX_init (lua_State *L) { for (i=0; imarked = RESERVEDMARK+i; /* reserved word */ + ts->marked = (unsigned short)RESERVEDMARK+i; /* reserved word */ } } diff --git a/lobject.h b/lobject.h index ebcdbbb7..53df58cc 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 1.103 2001/06/05 18:17:01 roberto Exp roberto $ +** $Id: lobject.h,v 1.104 2001/06/06 18:00:19 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -82,9 +82,9 @@ typedef TObject *StkId; /* index to stack elements */ */ typedef struct TString { lu_hash hash; - int constindex; /* hint to reuse constants */ size_t len; - int marked; + unsigned short constindex; /* hint to reuse constants */ + short marked; struct TString *nexthash; /* chain for hash table */ } TString; @@ -105,14 +105,17 @@ union L_UTString { typedef struct Udata { - int tag; + int tag; /* negative means `marked' (only during GC) */ void *value; size_t len; - int marked; struct Udata *next; /* chain for list of all udata */ } Udata; +#define switchudatamark(u) ((u)->tag = (-((u)->tag+1))) +#define ismarkedudata(u) ((u)->tag < 0) + + /* ** Function Prototypes diff --git a/lstring.c b/lstring.c index e9d165cc..21cad45b 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 1.62 2001/03/26 14:31:49 roberto Exp roberto $ +** $Id: lstring.c,v 1.63 2001/06/06 18:00:19 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -47,6 +47,27 @@ void luaS_resize (lua_State *L, int newsize) { } +static TString *newlstr (lua_State *L, const l_char *str, size_t l, lu_hash h) { + TString *ts = (TString *)luaM_malloc(L, sizestring(l)); + stringtable *tb; + ts->nexthash = NULL; + ts->len = l; + ts->hash = h; + ts->marked = 0; + ts->constindex = 0; + memcpy(getstr(ts), str, l*sizeof(l_char)); + getstr(ts)[l] = l_c('\0'); /* ending 0 */ + tb = &G(L)->strt; + h = lmod(h, tb->size); + ts->nexthash = tb->hash[h]; /* chain new entry */ + tb->hash[h] = ts; + tb->nuse++; + if (tb->nuse > (ls_nstr)tb->size && tb->size <= MAX_INT/2) + luaS_resize(L, tb->size*2); /* too crowded */ + return ts; +} + + TString *luaS_newlstr (lua_State *L, const l_char *str, size_t l) { TString *ts; lu_hash h = l; /* seed */ @@ -58,29 +79,12 @@ TString *luaS_newlstr (lua_State *L, const l_char *str, size_t l) { if (ts->len == l && (memcmp(str, getstr(ts), l) == 0)) return ts; } - /* not found */ - ts = (TString *)luaM_malloc(L, sizestring(l)); - ts->marked = 0; - ts->nexthash = NULL; - ts->len = l; - ts->hash = h; - ts->constindex = 0; - memcpy(getstr(ts), str, l*sizeof(l_char)); - getstr(ts)[l] = 0; /* ending 0 */ - h = lmod(h, G(L)->strt.size); - ts->nexthash = G(L)->strt.hash[h]; /* chain new entry */ - G(L)->strt.hash[h] = ts; - G(L)->strt.nuse++; - if (G(L)->strt.nuse > (ls_nstr)G(L)->strt.size && - G(L)->strt.size <= MAX_INT/2) - luaS_resize(L, G(L)->strt.size*2); /* too crowded */ - return ts; + return newlstr(L, str, l, h); /* not found */ } Udata *luaS_newudata (lua_State *L, size_t s) { Udata *u = (Udata *)luaM_malloc(L, sizeudata(s)); - u->marked = 0; u->len = s; u->tag = 0; u->value = ((union L_UUdata *)(u) + 1); -- cgit v1.2.3-55-g6feb