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
25 #define MEMERRMSG "not enough memory"
29 ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
32 #if !defined(LUAI_HASHLIMIT)
33 #define LUAI_HASHLIMIT 5
38 ** equality for long strings
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 */
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]));
58 unsigned int luaS_hashlongstr (TString *ts) {
59 lua_assert(ts->tt == LUA_TLNGSTR);
60 if (ts->extra == 0) { /* no hash? */
61 ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash);
62 ts->extra = 1; /* now it has its hash */
69 ** resizes the string table
71 void luaS_resize (lua_State *L, int newsize) {
73 stringtable *tb = &G(L)->strt;
74 if (newsize > tb->size) { /* grow table if needed */
75 luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
76 for (i = tb->size; i < newsize; i++)
79 for (i = 0; i < tb->size; i++) { /* rehash */
80 TString *p = tb->hash[i];
82 while (p) { /* for each node in the list */
83 TString *hnext = p->u.hnext; /* save next */
84 unsigned int h = lmod(p->hash, newsize); /* new position */
85 p->u.hnext = tb->hash[h]; /* chain it */
90 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 luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
100 ** Clear API string cache. (Entries cannot be empty, so fill them with
101 ** a non-collectable string.)
103 void luaS_clearcache (global_State *g) {
105 for (i = 0; i < STRCACHE_N; i++)
106 for (j = 0; j < STRCACHE_M; j++) {
107 if (iswhite(g->strcache[i][j])) /* will entry be collected? */
108 g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
114 ** Initialize the string table and the string cache
116 void luaS_init (lua_State *L) {
117 global_State *g = G(L);
119 luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
120 /* pre-create memory-error message */
121 g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
122 luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
123 for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
124 for (j = 0; j < STRCACHE_M; j++)
125 g->strcache[i][j] = g->memerrmsg;
131 ** creates a new string object
133 static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
136 size_t totalsize; /* total size of TString object */
137 totalsize = sizelstring(l);
138 o = luaC_newobj(L, tag, totalsize);
142 getstr(ts)[l] = '\0'; /* ending 0 */
147 TString *luaS_createlngstrobj (lua_State *L, size_t l) {
148 TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed);
154 void luaS_remove (lua_State *L, TString *ts) {
155 stringtable *tb = &G(L)->strt;
156 TString **p = &tb->hash[lmod(ts->hash, tb->size)];
157 while (*p != ts) /* find previous element */
159 *p = (*p)->u.hnext; /* remove element from its list */
165 ** checks whether short string exists and reuses it or creates a new one
167 static TString *internshrstr (lua_State *L, const char *str, size_t l) {
169 global_State *g = G(L);
170 unsigned int h = luaS_hash(str, l, g->seed);
171 TString **list = &g->strt.hash[lmod(h, g->strt.size)];
172 lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
173 for (ts = *list; ts != NULL; ts = ts->u.hnext) {
174 if (l == ts->shrlen &&
175 (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
177 if (isdead(g, ts)) /* dead (but not collected yet)? */
178 changewhite(ts); /* resurrect it */
182 if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
183 luaS_resize(L, g->strt.size * 2);
184 list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
186 ts = createstrobj(L, l, LUA_TSHRSTR, h);
187 memcpy(getstr(ts), str, l * sizeof(char));
188 ts->shrlen = cast_byte(l);
197 ** new string (with explicit length)
199 TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
200 if (l <= LUAI_MAXSHORTLEN) /* short string? */
201 return internshrstr(L, str, l);
204 if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char))
206 ts = luaS_createlngstrobj(L, l);
207 memcpy(getstr(ts), str, l * sizeof(char));
214 ** Create or reuse a zero-terminated string, first checking in the
215 ** cache (using the string address as a key). The cache can contain
216 ** only zero-terminated strings, so it is safe to use 'strcmp' to
219 TString *luaS_new (lua_State *L, const char *str) {
220 unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
222 TString **p = G(L)->strcache[i];
223 for (j = 0; j < STRCACHE_M; j++) {
224 if (strcmp(str, getstr(p[j])) == 0) /* hit? */
225 return p[j]; /* that is it */
228 for (j = STRCACHE_M - 1; j > 0; j--)
229 p[j] = p[j - 1]; /* move out last element */
230 /* new element is first in the list */
231 p[0] = luaS_newlstr(L, str, strlen(str));
236 Udata *luaS_newudata (lua_State *L, size_t s) {
239 if (s > MAX_SIZE - sizeof(Udata))
241 o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s));
245 setuservalue(L, u, luaO_nilobject);