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