1 /*
2 ** $Id: lstring.c $
3 ** String table (keeps all strings handled by Lua)
4 ** See Copyright Notice in lua.h
5 */
6
7 #define lstring_c
8 #define LUA_CORE
9
10 #include "lprefix.h"
11
12
13 #include <string.h>
14
15 #include "lua.h"
16
17 #include "ldebug.h"
18 #include "ldo.h"
19 #include "lmem.h"
20 #include "lobject.h"
21 #include "lstate.h"
22 #include "lstring.h"
23
24
25 /*
26 ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a long string to
27 ** compute its hash
28 */
29 #if !defined(LUAI_HASHLIMIT)
30 #define LUAI_HASHLIMIT 5
31 #endif
32
33
34
35 /*
36 ** Maximum size for string table.
37 */
38 #define MAXSTRTB cast_int(luaM_limitN(MAX_INT, TString*))
39
40
41 /*
42 ** equality for long strings
43 */
luaS_eqlngstr(TString * a,TString * b)44 int luaS_eqlngstr (TString *a, TString *b) {
45 size_t len = a->u.lnglen;
46 lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR);
47 return (a == b) || /* same instance or... */
48 ((len == b->u.lnglen) && /* equal length and ... */
49 (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
50 }
51
52
luaS_hash(const char * str,size_t l,unsigned int seed,size_t step)53 unsigned int luaS_hash (const char *str, size_t l, unsigned int seed,
54 size_t step) {
55 unsigned int h = seed ^ cast_uint(l);
56 for (; l >= step; l -= step)
57 h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
58 return h;
59 }
60
61
luaS_hashlongstr(TString * ts)62 unsigned int luaS_hashlongstr (TString *ts) {
63 lua_assert(ts->tt == LUA_VLNGSTR);
64 if (ts->extra == 0) { /* no hash? */
65 size_t len = ts->u.lnglen;
66 size_t step = (len >> LUAI_HASHLIMIT) + 1;
67 ts->hash = luaS_hash(getstr(ts), len, ts->hash, step);
68 ts->extra = 1; /* now it has its hash */
69 }
70 return ts->hash;
71 }
72
73
tablerehash(TString ** vect,int osize,int nsize)74 static void tablerehash (TString **vect, int osize, int nsize) {
75 int i;
76 for (i = osize; i < nsize; i++) /* clear new elements */
77 vect[i] = NULL;
78 for (i = 0; i < osize; i++) { /* rehash old part of the array */
79 TString *p = vect[i];
80 vect[i] = NULL;
81 while (p) { /* for each string in the list */
82 TString *hnext = p->u.hnext; /* save next */
83 unsigned int h = lmod(p->hash, nsize); /* new position */
84 p->u.hnext = vect[h]; /* chain it into array */
85 vect[h] = p;
86 p = hnext;
87 }
88 }
89 }
90
91
92 /*
93 ** Resize the string table. If allocation fails, keep the current size.
94 ** (This can degrade performance, but any non-zero size should work
95 ** correctly.)
96 */
luaS_resize(lua_State * L,int nsize)97 void luaS_resize (lua_State *L, int nsize) {
98 stringtable *tb = &G(L)->strt;
99 int osize = tb->size;
100 TString **newvect;
101 if (nsize < osize) /* shrinking table? */
102 tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */
103 newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
104 if (unlikely(newvect == NULL)) { /* reallocation failed? */
105 if (nsize < osize) /* was it shrinking table? */
106 tablerehash(tb->hash, nsize, osize); /* restore to original size */
107 /* leave table as it was */
108 }
109 else { /* allocation succeeded */
110 tb->hash = newvect;
111 tb->size = nsize;
112 if (nsize > osize)
113 tablerehash(newvect, osize, nsize); /* rehash for new size */
114 }
115 }
116
117
118 /*
119 ** Clear API string cache. (Entries cannot be empty, so fill them with
120 ** a non-collectable string.)
121 */
luaS_clearcache(global_State * g)122 void luaS_clearcache (global_State *g) {
123 int i, j;
124 for (i = 0; i < STRCACHE_N; i++)
125 for (j = 0; j < STRCACHE_M; j++) {
126 if (iswhite(g->strcache[i][j])) /* will entry be collected? */
127 g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
128 }
129 }
130
131
132 /*
133 ** Initialize the string table and the string cache
134 */
luaS_init(lua_State * L)135 void luaS_init (lua_State *L) {
136 global_State *g = G(L);
137 int i, j;
138 stringtable *tb = &G(L)->strt;
139 tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*);
140 tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */
141 tb->size = MINSTRTABSIZE;
142 /* pre-create memory-error message */
143 g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
144 luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
145 for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
146 for (j = 0; j < STRCACHE_M; j++)
147 g->strcache[i][j] = g->memerrmsg;
148 }
149
150
151
152 /*
153 ** creates a new string object
154 */
createstrobj(lua_State * L,size_t l,int tag,unsigned int h)155 static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
156 TString *ts;
157 GCObject *o;
158 size_t totalsize; /* total size of TString object */
159 totalsize = sizelstring(l);
160 o = luaC_newobj(L, tag, totalsize);
161 ts = gco2ts(o);
162 ts->hash = h;
163 ts->extra = 0;
164 getstr(ts)[l] = '\0'; /* ending 0 */
165 return ts;
166 }
167
168
luaS_createlngstrobj(lua_State * L,size_t l)169 TString *luaS_createlngstrobj (lua_State *L, size_t l) {
170 TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed);
171 ts->u.lnglen = l;
172 return ts;
173 }
174
175
luaS_remove(lua_State * L,TString * ts)176 void luaS_remove (lua_State *L, TString *ts) {
177 stringtable *tb = &G(L)->strt;
178 TString **p = &tb->hash[lmod(ts->hash, tb->size)];
179 while (*p != ts) /* find previous element */
180 p = &(*p)->u.hnext;
181 *p = (*p)->u.hnext; /* remove element from its list */
182 tb->nuse--;
183 }
184
185
growstrtab(lua_State * L,stringtable * tb)186 static void growstrtab (lua_State *L, stringtable *tb) {
187 if (unlikely(tb->nuse == MAX_INT)) { /* too many strings? */
188 luaC_fullgc(L, 1); /* try to free some... */
189 if (tb->nuse == MAX_INT) /* still too many? */
190 luaM_error(L); /* cannot even create a message... */
191 }
192 if (tb->size <= MAXSTRTB / 2) /* can grow string table? */
193 luaS_resize(L, tb->size * 2);
194 }
195
196
197 /*
198 ** Checks whether short string exists and reuses it or creates a new one.
199 */
internshrstr(lua_State * L,const char * str,size_t l)200 static TString *internshrstr (lua_State *L, const char *str, size_t l) {
201 TString *ts;
202 global_State *g = G(L);
203 stringtable *tb = &g->strt;
204 unsigned int h = luaS_hash(str, l, g->seed, 1);
205 TString **list = &tb->hash[lmod(h, tb->size)];
206 lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
207 for (ts = *list; ts != NULL; ts = ts->u.hnext) {
208 if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
209 /* found! */
210 if (isdead(g, ts)) /* dead (but not collected yet)? */
211 changewhite(ts); /* resurrect it */
212 return ts;
213 }
214 }
215 /* else must create a new string */
216 if (tb->nuse >= tb->size) { /* need to grow string table? */
217 growstrtab(L, tb);
218 list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */
219 }
220 ts = createstrobj(L, l, LUA_VSHRSTR, h);
221 memcpy(getstr(ts), str, l * sizeof(char));
222 ts->shrlen = cast_byte(l);
223 ts->u.hnext = *list;
224 *list = ts;
225 tb->nuse++;
226 return ts;
227 }
228
229
230 /*
231 ** new string (with explicit length)
232 */
luaS_newlstr(lua_State * L,const char * str,size_t l)233 TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
234 if (l <= LUAI_MAXSHORTLEN) /* short string? */
235 return internshrstr(L, str, l);
236 else {
237 TString *ts;
238 if (unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char)))
239 luaM_toobig(L);
240 ts = luaS_createlngstrobj(L, l);
241 memcpy(getstr(ts), str, l * sizeof(char));
242 return ts;
243 }
244 }
245
246
247 /*
248 ** Create or reuse a zero-terminated string, first checking in the
249 ** cache (using the string address as a key). The cache can contain
250 ** only zero-terminated strings, so it is safe to use 'strcmp' to
251 ** check hits.
252 */
luaS_new(lua_State * L,const char * str)253 TString *luaS_new (lua_State *L, const char *str) {
254 unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
255 int j;
256 TString **p = G(L)->strcache[i];
257 for (j = 0; j < STRCACHE_M; j++) {
258 if (strcmp(str, getstr(p[j])) == 0) /* hit? */
259 return p[j]; /* that is it */
260 }
261 /* normal route */
262 for (j = STRCACHE_M - 1; j > 0; j--)
263 p[j] = p[j - 1]; /* move out last element */
264 /* new element is first in the list */
265 p[0] = luaS_newlstr(L, str, strlen(str));
266 return p[0];
267 }
268
269
luaS_newudata(lua_State * L,size_t s,int nuvalue)270 Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) {
271 Udata *u;
272 int i;
273 GCObject *o;
274 if (unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
275 luaM_toobig(L);
276 o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
277 u = gco2u(o);
278 u->len = s;
279 u->nuvalue = nuvalue;
280 u->metatable = NULL;
281 for (i = 0; i < nuvalue; i++)
282 setnilvalue(&u->uv[i].uv);
283 return u;
284 }
285
286