1 /*
2 ** $Id: lstring.c,v 2.45 2014/11/02 19:19:04 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
26 /*
27 ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
28 ** compute its hash
29 */
30 #if !defined(LUAI_HASHLIMIT)
31 #define LUAI_HASHLIMIT 5
32 #endif
33
34
35 /*
36 ** equality for long strings
37 */
luaS_eqlngstr(TString * a,TString * b)38 int luaS_eqlngstr (TString *a, TString *b) {
39 size_t len = a->len;
40 lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
41 return (a == b) || /* same instance or... */
42 ((len == b->len) && /* equal length and ... */
43 (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
44 }
45
46
luaS_hash(const char * str,size_t l,unsigned int seed)47 unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
48 unsigned int h = seed ^ cast(unsigned int, l);
49 size_t l1;
50 size_t step = (l >> LUAI_HASHLIMIT) + 1;
51 for (l1 = l; l1 >= step; l1 -= step)
52 h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1]));
53 return h;
54 }
55
56
57 /*
58 ** resizes the string table
59 */
luaS_resize(lua_State * L,int newsize)60 void luaS_resize (lua_State *L, int newsize) {
61 int i;
62 stringtable *tb = &G(L)->strt;
63 if (newsize > tb->size) { /* grow table if needed */
64 luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
65 for (i = tb->size; i < newsize; i++)
66 tb->hash[i] = NULL;
67 }
68 for (i = 0; i < tb->size; i++) { /* rehash */
69 TString *p = tb->hash[i];
70 tb->hash[i] = NULL;
71 while (p) { /* for each node in the list */
72 TString *hnext = p->hnext; /* save next */
73 unsigned int h = lmod(p->hash, newsize); /* new position */
74 p->hnext = tb->hash[h]; /* chain it */
75 tb->hash[h] = p;
76 p = hnext;
77 }
78 }
79 if (newsize < tb->size) { /* shrink table if needed */
80 /* vanishing slice should be empty */
81 lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
82 luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
83 }
84 tb->size = newsize;
85 }
86
87
88
89 /*
90 ** creates a new string object
91 */
createstrobj(lua_State * L,const char * str,size_t l,int tag,unsigned int h)92 static TString *createstrobj (lua_State *L, const char *str, size_t l,
93 int tag, unsigned int h) {
94 TString *ts;
95 GCObject *o;
96 size_t totalsize; /* total size of TString object */
97 totalsize = sizelstring(l);
98 o = luaC_newobj(L, tag, totalsize);
99 ts = gco2ts(o);
100 ts->len = l;
101 ts->hash = h;
102 ts->extra = 0;
103 memcpy(getaddrstr(ts), str, l * sizeof(char));
104 getaddrstr(ts)[l] = '\0'; /* ending 0 */
105 return ts;
106 }
107
108
luaS_remove(lua_State * L,TString * ts)109 void luaS_remove (lua_State *L, TString *ts) {
110 stringtable *tb = &G(L)->strt;
111 TString **p = &tb->hash[lmod(ts->hash, tb->size)];
112 while (*p != ts) /* find previous element */
113 p = &(*p)->hnext;
114 *p = (*p)->hnext; /* remove element from its list */
115 tb->nuse--;
116 }
117
118
119 /*
120 ** checks whether short string exists and reuses it or creates a new one
121 */
internshrstr(lua_State * L,const char * str,size_t l)122 static TString *internshrstr (lua_State *L, const char *str, size_t l) {
123 TString *ts;
124 global_State *g = G(L);
125 unsigned int h = luaS_hash(str, l, g->seed);
126 TString **list = &g->strt.hash[lmod(h, g->strt.size)];
127 for (ts = *list; ts != NULL; ts = ts->hnext) {
128 if (l == ts->len &&
129 (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
130 /* found! */
131 if (isdead(g, ts)) /* dead (but not collected yet)? */
132 changewhite(ts); /* resurrect it */
133 return ts;
134 }
135 }
136 if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
137 luaS_resize(L, g->strt.size * 2);
138 list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
139 }
140 ts = createstrobj(L, str, l, LUA_TSHRSTR, h);
141 ts->hnext = *list;
142 *list = ts;
143 g->strt.nuse++;
144 return ts;
145 }
146
147
148 /*
149 ** new string (with explicit length)
150 */
luaS_newlstr(lua_State * L,const char * str,size_t l)151 TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
152 if (l <= LUAI_MAXSHORTLEN) /* short string? */
153 return internshrstr(L, str, l);
154 else {
155 if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char))
156 luaM_toobig(L);
157 return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed);
158 }
159 }
160
161
162 /*
163 ** new zero-terminated string
164 */
luaS_new(lua_State * L,const char * str)165 TString *luaS_new (lua_State *L, const char *str) {
166 return luaS_newlstr(L, str, strlen(str));
167 }
168
169
luaS_newudata(lua_State * L,size_t s)170 Udata *luaS_newudata (lua_State *L, size_t s) {
171 Udata *u;
172 GCObject *o;
173 if (s > MAX_SIZE - sizeof(Udata))
174 luaM_toobig(L);
175 o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s));
176 u = gco2u(o);
177 u->len = s;
178 u->metatable = NULL;
179 setuservalue(L, u, luaO_nilobject);
180 return u;
181 }
182
183