aboutsummaryrefslogtreecommitdiff
path: root/lstring.c
diff options
context:
space:
mode:
Diffstat (limited to 'lstring.c')
-rw-r--r--lstring.c66
1 files changed, 37 insertions, 29 deletions
diff --git a/lstring.c b/lstring.c
index 6aaf6b82..f71425e8 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 1.24 1999/10/14 19:13:31 roberto Exp roberto $ 2** $Id: lstring.c,v 1.25 1999/10/19 13:33:22 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*/
@@ -87,7 +87,6 @@ static TaggedString *newone (long l, unsigned long h) {
87 sizeof(TaggedString)+l*sizeof(char)); 87 sizeof(TaggedString)+l*sizeof(char));
88 ts->marked = 0; 88 ts->marked = 0;
89 ts->nexthash = NULL; 89 ts->nexthash = NULL;
90 ts->nextglobal = ts; /* signal it is not in global list */
91 ts->hash = h; 90 ts->hash = h;
92 return ts; 91 return ts;
93} 92}
@@ -97,7 +96,7 @@ static TaggedString *newone_s (const char *str, long l, unsigned long h) {
97 TaggedString *ts = newone(l, h); 96 TaggedString *ts = newone(l, h);
98 memcpy(ts->str, str, l); 97 memcpy(ts->str, str, l);
99 ts->str[l] = 0; /* ending 0 */ 98 ts->str[l] = 0; /* ending 0 */
100 ts->u.s.globalval.ttype = LUA_T_NIL; /* initialize global value */ 99 ts->u.s.gv = NULL; /* no global value */
101 ts->u.s.len = l; 100 ts->u.s.len = l;
102 ts->constindex = 0; 101 ts->constindex = 0;
103 L->nblocks += gcsizestring(l); 102 L->nblocks += gcsizestring(l);
@@ -107,7 +106,7 @@ static TaggedString *newone_s (const char *str, long l, unsigned long h) {
107 106
108static TaggedString *newone_u (void *buff, int tag, unsigned long h) { 107static TaggedString *newone_u (void *buff, int tag, unsigned long h) {
109 TaggedString *ts = newone(0, h); 108 TaggedString *ts = newone(0, h);
110 ts->u.d.v = buff; 109 ts->u.d.value = buff;
111 ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag; 110 ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
112 ts->constindex = -1; /* tag -> this is a userdata */ 111 ts->constindex = -1; /* tag -> this is a userdata */
113 L->nblocks++; 112 L->nblocks++;
@@ -131,13 +130,15 @@ static void newentry (stringtable *tb, TaggedString *ts, int h) {
131} 130}
132 131
133 132
134static TaggedString *insert_s (const char *str, long l, 133TaggedString *luaS_newlstr (const char *str, long l) {
135 stringtable *tb, unsigned long h) { 134 unsigned long h = hash_s(str, l);
135 stringtable *tb = &L->string_root[h%NUM_HASHSTR];
136 int h1 = h%tb->size; 136 int h1 = h%tb->size;
137 TaggedString *ts; 137 TaggedString *ts;
138 for (ts = tb->hash[h1]; ts; ts = ts->nexthash) 138 for (ts = tb->hash[h1]; ts; ts = ts->nexthash) {
139 if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0)) 139 if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
140 return ts; 140 return ts;
141 }
141 /* not found */ 142 /* not found */
142 ts = newone_s(str, l, h); /* create new entry */ 143 ts = newone_s(str, l, h); /* create new entry */
143 newentry(tb, ts, h1); /* insert it on table */ 144 newentry(tb, ts, h1); /* insert it on table */
@@ -145,30 +146,22 @@ static TaggedString *insert_s (const char *str, long l,
145} 146}
146 147
147 148
148static TaggedString *insert_u (void *buff, int tag, stringtable *tb) { 149TaggedString *luaS_createudata (void *udata, int tag) {
149 unsigned long h = (IntPoint)buff; 150 unsigned long h = (IntPoint)udata;
151 stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR];
150 int h1 = h%tb->size; 152 int h1 = h%tb->size;
151 TaggedString *ts; 153 TaggedString *ts;
152 for (ts = tb->hash[h1]; ts; ts = ts->nexthash) 154 for (ts = tb->hash[h1]; ts; ts = ts->nexthash) {
153 if ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v) 155 if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
154 return ts; 156 return ts;
157 }
155 /* not found */ 158 /* not found */
156 ts = newone_u(buff, tag, h); 159 ts = newone_u(udata, tag, h);
157 newentry(tb, ts, h1); 160 newentry(tb, ts, h1);
158 return ts; 161 return ts;
159} 162}
160 163
161 164
162TaggedString *luaS_createudata (void *udata, int tag) {
163 int t = ((IntPoint)udata%NUM_HASHUDATA)+NUM_HASHSTR;
164 return insert_u(udata, tag, &L->string_root[t]);
165}
166
167TaggedString *luaS_newlstr (const char *str, long l) {
168 unsigned long h = hash_s(str, l);
169 return insert_s(str, l, &L->string_root[h%NUM_HASHSTR], h);
170}
171
172TaggedString *luaS_new (const char *str) { 165TaggedString *luaS_new (const char *str) {
173 return luaS_newlstr(str, strlen(str)); 166 return luaS_newlstr(str, strlen(str));
174} 167}
@@ -181,23 +174,38 @@ TaggedString *luaS_newfixedstring (const char *str) {
181 174
182 175
183void luaS_free (TaggedString *t) { 176void luaS_free (TaggedString *t) {
184 L->nblocks -= (t->constindex == -1) ? 1 : gcsizestring(t->u.s.len); 177 if (t->constindex == -1) /* is userdata? */
178 L->nblocks--;
179 else { /* is string */
180 L->nblocks -= gcsizestring(t->u.s.len);
181 luaM_free(t->u.s.gv);
182 }
185 luaM_free(t); 183 luaM_free(t);
186} 184}
187 185
188 186
189void luaS_rawsetglobal (TaggedString *ts, const TObject *newval) { 187GlobalVar *luaS_assertglobal (TaggedString *ts) {
190 ts->u.s.globalval = *newval; 188 GlobalVar *gv = ts->u.s.gv;
191 if (ts->nextglobal == ts) { /* is not in list? */ 189 if (!gv) { /* no global value yet? */
192 ts->nextglobal = L->rootglobal; 190 gv = luaM_new(GlobalVar);
193 L->rootglobal = ts; 191 gv->value.ttype = LUA_T_NIL; /* initial value */
192 gv->name = ts;
193 gv->next = L->rootglobal; /* chain in global list */
194 L->rootglobal = gv;
195 ts->u.s.gv = gv;
194 } 196 }
197 return gv;
198}
199
200
201GlobalVar *luaS_assertglobalbyname (const char *name) {
202 return luaS_assertglobal(luaS_new(name));
195} 203}
196 204
197 205
198int luaS_globaldefined (const char *name) { 206int luaS_globaldefined (const char *name) {
199 TaggedString *ts = luaS_new(name); 207 TaggedString *ts = luaS_new(name);
200 return ts->u.s.globalval.ttype != LUA_T_NIL; 208 return ts->u.s.gv && ts->u.s.gv->value.ttype != LUA_T_NIL;
201} 209}
202 210
203 211