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