aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-06-07 12:01:21 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-06-07 12:01:21 -0300
commitba11831d357889ee090ce92ff508957c6c023c42 (patch)
treed54958d74c7fc1d5b751bf4819ed7345e3b6e69b
parent190ddd431dd9f14148d232ed9a72db482a1df934 (diff)
downloadlua-ba11831d357889ee090ce92ff508957c6c023c42.tar.gz
lua-ba11831d357889ee090ce92ff508957c6c023c42.tar.bz2
lua-ba11831d357889ee090ce92ff508957c6c023c42.zip
smaller structs for udata and for strings
-rw-r--r--lcode.c4
-rw-r--r--lgc.c11
-rw-r--r--llex.c4
-rw-r--r--lobject.h13
-rw-r--r--lstring.c42
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 @@
1/* 1/*
2** $Id: lcode.c,v 1.69 2001/06/05 18:17:01 roberto Exp roberto $ 2** $Id: lcode.c,v 1.70 2001/06/06 18:00:19 roberto Exp roberto $
3** Code generator for Lua 3** Code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -231,7 +231,7 @@ int luaK_stringk (FuncState *fs, TString *s) {
231 TObject o; 231 TObject o;
232 setsvalue(&o, s); 232 setsvalue(&o, s);
233 c = addk(fs, &o); 233 c = addk(fs, &o);
234 s->constindex = c; /* hint for next time */ 234 s->constindex = (unsigned short)c; /* hint for next time */
235 } 235 }
236 return c; 236 return c;
237} 237}
diff --git a/lgc.c b/lgc.c
index 86b7d092..fcd22a86 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 1.99 2001/06/05 19:27:32 roberto Exp roberto $ 2** $Id: lgc.c,v 1.100 2001/06/06 18:00:19 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*/
@@ -86,7 +86,8 @@ static void markobject (GCState *st, TObject *o) {
86 strmark(tsvalue(o)); 86 strmark(tsvalue(o));
87 break; 87 break;
88 case LUA_TUSERDATA: 88 case LUA_TUSERDATA:
89 uvalue(o)->marked = 1; 89 if (!ismarkedudata(uvalue(o)))
90 switchudatamark(uvalue(o));
90 break; 91 break;
91 case LUA_TFUNCTION: 92 case LUA_TFUNCTION:
92 markclosure(st, clvalue(o)); 93 markclosure(st, clvalue(o));
@@ -196,7 +197,7 @@ static int hasmark (const TObject *o) {
196 case LUA_TSTRING: 197 case LUA_TSTRING:
197 return tsvalue(o)->marked; 198 return tsvalue(o)->marked;
198 case LUA_TUSERDATA: 199 case LUA_TUSERDATA:
199 return uvalue(o)->marked; 200 return ismarkedudata(uvalue(o));
200 case LUA_TTABLE: 201 case LUA_TTABLE:
201 return ismarked(hvalue(o)); 202 return ismarked(hvalue(o));
202 case LUA_TFUNCTION: 203 case LUA_TFUNCTION:
@@ -284,8 +285,8 @@ static void collectudata (lua_State *L) {
284 Udata **p = &G(L)->rootudata; 285 Udata **p = &G(L)->rootudata;
285 Udata *next; 286 Udata *next;
286 while ((next = *p) != NULL) { 287 while ((next = *p) != NULL) {
287 if (next->marked) { 288 if (ismarkedudata(next)) {
288 next->marked = 0; /* unmark */ 289 switchudatamark(next); /* unmark */
289 p = &next->next; 290 p = &next->next;
290 } 291 }
291 else { /* collect */ 292 else { /* collect */
diff --git a/llex.c b/llex.c
index dfe7dbac..74b1982a 100644
--- a/llex.c
+++ b/llex.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.c,v 1.83 2001/03/07 12:49:37 roberto Exp roberto $ 2** $Id: llex.c,v 1.84 2001/03/26 14:31:49 roberto Exp roberto $
3** Lexical Analyzer 3** Lexical Analyzer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -40,7 +40,7 @@ void luaX_init (lua_State *L) {
40 for (i=0; i<NUM_RESERVED; i++) { 40 for (i=0; i<NUM_RESERVED; i++) {
41 TString *ts = luaS_new(L, token2string[i]); 41 TString *ts = luaS_new(L, token2string[i]);
42 lua_assert(strlen(token2string[i])+1 <= TOKEN_LEN); 42 lua_assert(strlen(token2string[i])+1 <= TOKEN_LEN);
43 ts->marked = RESERVEDMARK+i; /* reserved word */ 43 ts->marked = (unsigned short)RESERVEDMARK+i; /* reserved word */
44 } 44 }
45} 45}
46 46
diff --git a/lobject.h b/lobject.h
index ebcdbbb7..53df58cc 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 1.103 2001/06/05 18:17:01 roberto Exp roberto $ 2** $Id: lobject.h,v 1.104 2001/06/06 18:00:19 roberto Exp roberto $
3** Type definitions for Lua objects 3** Type definitions for Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -82,9 +82,9 @@ typedef TObject *StkId; /* index to stack elements */
82*/ 82*/
83typedef struct TString { 83typedef struct TString {
84 lu_hash hash; 84 lu_hash hash;
85 int constindex; /* hint to reuse constants */
86 size_t len; 85 size_t len;
87 int marked; 86 unsigned short constindex; /* hint to reuse constants */
87 short marked;
88 struct TString *nexthash; /* chain for hash table */ 88 struct TString *nexthash; /* chain for hash table */
89} TString; 89} TString;
90 90
@@ -105,14 +105,17 @@ union L_UTString {
105 105
106 106
107typedef struct Udata { 107typedef struct Udata {
108 int tag; 108 int tag; /* negative means `marked' (only during GC) */
109 void *value; 109 void *value;
110 size_t len; 110 size_t len;
111 int marked;
112 struct Udata *next; /* chain for list of all udata */ 111 struct Udata *next; /* chain for list of all udata */
113} Udata; 112} Udata;
114 113
115 114
115#define switchudatamark(u) ((u)->tag = (-((u)->tag+1)))
116#define ismarkedudata(u) ((u)->tag < 0)
117
118
116 119
117/* 120/*
118** Function Prototypes 121** Function Prototypes
diff --git a/lstring.c b/lstring.c
index e9d165cc..21cad45b 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 1.62 2001/03/26 14:31:49 roberto Exp roberto $ 2** $Id: lstring.c,v 1.63 2001/06/06 18:00:19 roberto Exp roberto $
3** String table (keeps all strings handled by Lua) 3** String table (keeps all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -47,6 +47,27 @@ void luaS_resize (lua_State *L, int newsize) {
47} 47}
48 48
49 49
50static TString *newlstr (lua_State *L, const l_char *str, size_t l, lu_hash h) {
51 TString *ts = (TString *)luaM_malloc(L, sizestring(l));
52 stringtable *tb;
53 ts->nexthash = NULL;
54 ts->len = l;
55 ts->hash = h;
56 ts->marked = 0;
57 ts->constindex = 0;
58 memcpy(getstr(ts), str, l*sizeof(l_char));
59 getstr(ts)[l] = l_c('\0'); /* ending 0 */
60 tb = &G(L)->strt;
61 h = lmod(h, tb->size);
62 ts->nexthash = tb->hash[h]; /* chain new entry */
63 tb->hash[h] = ts;
64 tb->nuse++;
65 if (tb->nuse > (ls_nstr)tb->size && tb->size <= MAX_INT/2)
66 luaS_resize(L, tb->size*2); /* too crowded */
67 return ts;
68}
69
70
50TString *luaS_newlstr (lua_State *L, const l_char *str, size_t l) { 71TString *luaS_newlstr (lua_State *L, const l_char *str, size_t l) {
51 TString *ts; 72 TString *ts;
52 lu_hash h = l; /* seed */ 73 lu_hash h = l; /* seed */
@@ -58,29 +79,12 @@ TString *luaS_newlstr (lua_State *L, const l_char *str, size_t l) {
58 if (ts->len == l && (memcmp(str, getstr(ts), l) == 0)) 79 if (ts->len == l && (memcmp(str, getstr(ts), l) == 0))
59 return ts; 80 return ts;
60 } 81 }
61 /* not found */ 82 return newlstr(L, str, l, h); /* not found */
62 ts = (TString *)luaM_malloc(L, sizestring(l));
63 ts->marked = 0;
64 ts->nexthash = NULL;
65 ts->len = l;
66 ts->hash = h;
67 ts->constindex = 0;
68 memcpy(getstr(ts), str, l*sizeof(l_char));
69 getstr(ts)[l] = 0; /* ending 0 */
70 h = lmod(h, G(L)->strt.size);
71 ts->nexthash = G(L)->strt.hash[h]; /* chain new entry */
72 G(L)->strt.hash[h] = ts;
73 G(L)->strt.nuse++;
74 if (G(L)->strt.nuse > (ls_nstr)G(L)->strt.size &&
75 G(L)->strt.size <= MAX_INT/2)
76 luaS_resize(L, G(L)->strt.size*2); /* too crowded */
77 return ts;
78} 83}
79 84
80 85
81Udata *luaS_newudata (lua_State *L, size_t s) { 86Udata *luaS_newudata (lua_State *L, size_t s) {
82 Udata *u = (Udata *)luaM_malloc(L, sizeudata(s)); 87 Udata *u = (Udata *)luaM_malloc(L, sizeudata(s));
83 u->marked = 0;
84 u->len = s; 88 u->len = s;
85 u->tag = 0; 89 u->tag = 0;
86 u->value = ((union L_UUdata *)(u) + 1); 90 u->value = ((union L_UUdata *)(u) + 1);