diff options
Diffstat (limited to 'lstring.c')
| -rw-r--r-- | lstring.c | 66 |
1 files changed, 37 insertions, 29 deletions
| @@ -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 | ||
| 108 | static TaggedString *newone_u (void *buff, int tag, unsigned long h) { | 107 | static 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 | ||
| 134 | static TaggedString *insert_s (const char *str, long l, | 133 | TaggedString *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 | ||
| 148 | static TaggedString *insert_u (void *buff, int tag, stringtable *tb) { | 149 | TaggedString *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 | ||
| 162 | TaggedString *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 | |||
| 167 | TaggedString *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 | |||
| 172 | TaggedString *luaS_new (const char *str) { | 165 | TaggedString *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 | ||
| 183 | void luaS_free (TaggedString *t) { | 176 | void 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 | ||
| 189 | void luaS_rawsetglobal (TaggedString *ts, const TObject *newval) { | 187 | GlobalVar *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 | |||
| 201 | GlobalVar *luaS_assertglobalbyname (const char *name) { | ||
| 202 | return luaS_assertglobal(luaS_new(name)); | ||
| 195 | } | 203 | } |
| 196 | 204 | ||
| 197 | 205 | ||
| 198 | int luaS_globaldefined (const char *name) { | 206 | int 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 | ||
