smaller structs for udata and for strings

This commit is contained in:
Roberto Ierusalimschy 2001-06-07 12:01:21 -03:00
parent 190ddd431d
commit ba11831d35
5 changed files with 41 additions and 33 deletions

View File

@ -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;
}

11
lgc.c
View File

@ -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 */

4
llex.c
View File

@ -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; i<NUM_RESERVED; i++) {
TString *ts = luaS_new(L, token2string[i]);
lua_assert(strlen(token2string[i])+1 <= TOKEN_LEN);
ts->marked = RESERVEDMARK+i; /* reserved word */
ts->marked = (unsigned short)RESERVEDMARK+i; /* reserved word */
}
}

View File

@ -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

View File

@ -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);