Line data Source code
1 : /*
2 : ** $Id: lstring.c,v 2.56.1.1 2017/04/19 17:20:42 roberto Exp $
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 : #define MEMERRMSG "not enough mem, increase SCR_HEAP_SIZE"
26 :
27 :
28 : /*
29 : ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
30 : ** compute its hash
31 : */
32 : #if !defined(LUAI_HASHLIMIT)
33 : #define LUAI_HASHLIMIT 5
34 : #endif
35 :
36 :
37 : /*
38 : ** equality for long strings
39 : */
40 273 : int luaS_eqlngstr (TString *a, TString *b) {
41 273 : size_t len = a->u.lnglen;
42 : lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
43 302 : return (a == b) || /* same instance or... */
44 29 : ((len == b->u.lnglen) && /* equal length and ... */
45 18 : (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
46 : }
47 :
48 :
49 155897 : unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
50 155897 : unsigned int h = seed ^ cast(unsigned int, l);
51 155897 : size_t step = (l >> LUAI_HASHLIMIT) + 1;
52 1313147 : for (; l >= step; l -= step)
53 1157250 : h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
54 155897 : return h;
55 : }
56 :
57 :
58 1323 : unsigned int luaS_hashlongstr (TString *ts) {
59 : lua_assert(ts->tt == LUA_TLNGSTR);
60 1323 : if (ts->extra == 0) { /* no hash? */
61 268 : ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash);
62 268 : ts->extra = 1; /* now it has its hash */
63 : }
64 1323 : return ts->hash;
65 : }
66 :
67 :
68 : /*
69 : ** resizes the string table
70 : */
71 244 : void luaS_resize (lua_State *L, int newsize) {
72 : int i;
73 244 : stringtable *tb = &G(L)->strt;
74 244 : if (newsize > tb->size) { /* grow table if needed */
75 243 : luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
76 44275 : for (i = tb->size; i < newsize; i++)
77 44032 : tb->hash[i] = NULL;
78 : }
79 32884 : for (i = 0; i < tb->size; i++) { /* rehash */
80 32640 : TString *p = tb->hash[i];
81 32640 : tb->hash[i] = NULL;
82 64410 : while (p) { /* for each node in the list */
83 31770 : TString *hnext = p->u.hnext; /* save next */
84 31770 : unsigned int h = lmod(p->hash, newsize); /* new position */
85 31770 : p->u.hnext = tb->hash[h]; /* chain it */
86 31770 : tb->hash[h] = p;
87 31770 : p = hnext;
88 : }
89 : }
90 244 : if (newsize < tb->size) { /* shrink table if needed */
91 : /* vanishing slice should be empty */
92 : lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
93 1 : luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
94 : }
95 244 : tb->size = newsize;
96 244 : }
97 :
98 :
99 : /*
100 : ** Clear API string cache. (Entries cannot be empty, so fill them with
101 : ** a non-collectable string.)
102 : */
103 90640 : void luaS_clearcache (global_State *g) {
104 : int i, j;
105 4894560 : for (i = 0; i < STRCACHE_N; i++)
106 14411760 : for (j = 0; j < STRCACHE_M; j++) {
107 9607840 : if (iswhite(g->strcache[i][j])) /* will entry be collected? */
108 654 : g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
109 : }
110 90640 : }
111 :
112 :
113 : /*
114 : ** Initialize the string table and the string cache
115 : */
116 97 : void luaS_init (lua_State *L) {
117 97 : global_State *g = G(L);
118 : int i, j;
119 97 : luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
120 : /* pre-create memory-error message */
121 97 : g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
122 97 : luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
123 5238 : for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
124 15423 : for (j = 0; j < STRCACHE_M; j++)
125 10282 : g->strcache[i][j] = g->memerrmsg;
126 97 : }
127 :
128 :
129 :
130 : /*
131 : ** creates a new string object
132 : */
133 54906 : static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
134 : TString *ts;
135 : GCObject *o;
136 : size_t totalsize; /* total size of TString object */
137 54906 : totalsize = sizelstring(l);
138 54906 : o = luaC_newobj(L, tag, totalsize);
139 54906 : ts = gco2ts(o);
140 54906 : ts->hash = h;
141 54906 : ts->extra = 0;
142 : #pragma GCC diagnostic push
143 : #if defined(__GNUC__) && __GNUC__ >= 10
144 : #pragma GCC diagnostic ignored "-Wstringop-overflow"
145 : #endif
146 54906 : getstr(ts)[l] = '\0'; /* ending 0 */
147 : #pragma GCC diagnostic pop
148 54906 : return ts;
149 : }
150 :
151 :
152 1648 : TString *luaS_createlngstrobj (lua_State *L, size_t l) {
153 1648 : TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed);
154 1648 : ts->u.lnglen = l;
155 1648 : return ts;
156 : }
157 :
158 :
159 41038 : void luaS_remove (lua_State *L, TString *ts) {
160 41038 : stringtable *tb = &G(L)->strt;
161 41038 : TString **p = &tb->hash[lmod(ts->hash, tb->size)];
162 43827 : while (*p != ts) /* find previous element */
163 2789 : p = &(*p)->u.hnext;
164 41038 : *p = (*p)->u.hnext; /* remove element from its list */
165 41038 : tb->nuse--;
166 41038 : }
167 :
168 :
169 : /*
170 : ** checks whether short string exists and reuses it or creates a new one
171 : */
172 155532 : static TString *internshrstr (lua_State *L, const char *str, size_t l) {
173 : TString *ts;
174 155532 : global_State *g = G(L);
175 155532 : unsigned int h = luaS_hash(str, l, g->seed);
176 155532 : TString **list = &g->strt.hash[lmod(h, g->strt.size)];
177 : lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
178 215715 : for (ts = *list; ts != NULL; ts = ts->u.hnext) {
179 162457 : if (l == ts->shrlen &&
180 107231 : (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
181 : /* found! */
182 102274 : if (isdead(g, ts)) /* dead (but not collected yet)? */
183 0 : changewhite(ts); /* resurrect it */
184 102274 : return ts;
185 : }
186 : }
187 53258 : if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
188 146 : luaS_resize(L, g->strt.size * 2);
189 146 : list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
190 : }
191 53258 : ts = createstrobj(L, l, LUA_TSHRSTR, h);
192 53258 : memcpy(getstr(ts), str, l * sizeof(char));
193 53258 : ts->shrlen = cast_byte(l);
194 53258 : ts->u.hnext = *list;
195 53258 : *list = ts;
196 53258 : g->strt.nuse++;
197 53258 : return ts;
198 : }
199 :
200 :
201 : /*
202 : ** new string (with explicit length)
203 : */
204 156937 : TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
205 156937 : if (l <= LUAI_MAXSHORTLEN) /* short string? */
206 155532 : return internshrstr(L, str, l);
207 : else {
208 : TString *ts;
209 1405 : if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char))
210 0 : luaM_toobig(L);
211 1405 : ts = luaS_createlngstrobj(L, l);
212 1405 : memcpy(getstr(ts), str, l * sizeof(char));
213 1405 : return ts;
214 : }
215 : }
216 :
217 :
218 : /*
219 : ** Create or reuse a zero-terminated string, first checking in the
220 : ** cache (using the string address as a key). The cache can contain
221 : ** only zero-terminated strings, so it is safe to use 'strcmp' to
222 : ** check hits.
223 : */
224 4122171 : TString *luaS_new (lua_State *L, const char *str) {
225 4122171 : unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
226 : int j;
227 4122171 : TString **p = G(L)->strcache[i];
228 4731349 : for (j = 0; j < STRCACHE_M; j++) {
229 4715902 : if (strcmp(str, getstr(p[j])) == 0) /* hit? */
230 4106724 : return p[j]; /* that is it */
231 : }
232 : /* normal route */
233 30894 : for (j = STRCACHE_M - 1; j > 0; j--)
234 15447 : p[j] = p[j - 1]; /* move out last element */
235 : /* new element is first in the list */
236 15447 : p[0] = luaS_newlstr(L, str, strlen(str));
237 15447 : return p[0];
238 : }
239 :
240 :
241 902229 : Udata *luaS_newudata (lua_State *L, size_t s) {
242 : Udata *u;
243 : GCObject *o;
244 902229 : if (s > MAX_SIZE - sizeof(Udata))
245 0 : luaM_toobig(L);
246 902229 : o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s));
247 902229 : u = gco2u(o);
248 902229 : u->len = s;
249 902229 : u->metatable = NULL;
250 902229 : setuservalue(L, u, luaO_nilobject);
251 902229 : return u;
252 : }
253 :
|