lstring.c
Go to the documentation of this file.
1 /*
2 ** $Id: lstring.c $
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 ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a long string to
27 ** compute its hash
28 */
29 #if !defined(LUAI_HASHLIMIT)
30 #define LUAI_HASHLIMIT 5
31 #endif
32 
33 
34 
35 /*
36 ** Maximum size for string table.
37 */
38 #define MAXSTRTB cast_int(luaM_limitN(MAX_INT, TString*))
39 
40 
41 /*
42 ** equality for long strings
43 */
45  size_t len = a->u.lnglen;
46  lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR);
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  size_t step) {
55  unsigned int h = seed ^ cast_uint(l);
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_VLNGSTR);
64  if (ts->extra == 0) { /* no hash? */
65  size_t len = ts->u.lnglen;
66  size_t step = (len >> LUAI_HASHLIMIT) + 1;
67  ts->hash = luaS_hash(getstr(ts), len, ts->hash, step);
68  ts->extra = 1; /* now it has its hash */
69  }
70  return ts->hash;
71 }
72 
73 
74 static void tablerehash (TString **vect, int osize, int nsize) {
75  int i;
76  for (i = osize; i < nsize; i++) /* clear new elements */
77  vect[i] = NULL;
78  for (i = 0; i < osize; i++) { /* rehash old part of the array */
79  TString *p = vect[i];
80  vect[i] = NULL;
81  while (p) { /* for each string in the list */
82  TString *hnext = p->u.hnext; /* save next */
83  unsigned int h = lmod(p->hash, nsize); /* new position */
84  p->u.hnext = vect[h]; /* chain it into array */
85  vect[h] = p;
86  p = hnext;
87  }
88  }
89 }
90 
91 
92 /*
93 ** Resize the string table. If allocation fails, keep the current size.
94 ** (This can degrade performance, but any non-zero size should work
95 ** correctly.)
96 */
97 void luaS_resize (lua_State *L, int nsize) {
98  stringtable *tb = &G(L)->strt;
99  int osize = tb->size;
100  TString **newvect;
101  if (nsize < osize) /* shrinking table? */
102  tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */
103  newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
104  if (unlikely(newvect == NULL)) { /* reallocation failed? */
105  if (nsize < osize) /* was it shrinking table? */
106  tablerehash(tb->hash, nsize, osize); /* restore to original size */
107  /* leave table as it was */
108  }
109  else { /* allocation succeeded */
110  tb->hash = newvect;
111  tb->size = nsize;
112  if (nsize > osize)
113  tablerehash(newvect, osize, nsize); /* rehash for new size */
114  }
115 }
116 
117 
118 /*
119 ** Clear API string cache. (Entries cannot be empty, so fill them with
120 ** a non-collectable string.)
121 */
123  int i, j;
124  for (i = 0; i < STRCACHE_N; i++)
125  for (j = 0; j < STRCACHE_M; j++) {
126  if (iswhite(g->strcache[i][j])) /* will entry be collected? */
127  g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
128  }
129 }
130 
131 
132 /*
133 ** Initialize the string table and the string cache
134 */
135 void luaS_init (lua_State *L) {
136  global_State *g = G(L);
137  int i, j;
138  stringtable *tb = &G(L)->strt;
140  tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */
141  tb->size = MINSTRTABSIZE;
142  /* pre-create memory-error message */
144  luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
145  for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
146  for (j = 0; j < STRCACHE_M; j++)
147  g->strcache[i][j] = g->memerrmsg;
148 }
149 
150 
151 
152 /*
153 ** creates a new string object
154 */
155 static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
156  TString *ts;
157  GCObject *o;
158  size_t totalsize; /* total size of TString object */
159  totalsize = sizelstring(l);
160  o = luaC_newobj(L, tag, totalsize);
161  ts = gco2ts(o);
162  ts->hash = h;
163  ts->extra = 0;
164  getstr(ts)[l] = '\0'; /* ending 0 */
165  return ts;
166 }
167 
168 
170  TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed);
171  ts->u.lnglen = l;
172  return ts;
173 }
174 
175 
176 void luaS_remove (lua_State *L, TString *ts) {
177  stringtable *tb = &G(L)->strt;
178  TString **p = &tb->hash[lmod(ts->hash, tb->size)];
179  while (*p != ts) /* find previous element */
180  p = &(*p)->u.hnext;
181  *p = (*p)->u.hnext; /* remove element from its list */
182  tb->nuse--;
183 }
184 
185 
186 static void growstrtab (lua_State *L, stringtable *tb) {
187  if (unlikely(tb->nuse == MAX_INT)) { /* too many strings? */
188  luaC_fullgc(L, 1); /* try to free some... */
189  if (tb->nuse == MAX_INT) /* still too many? */
190  luaM_error(L); /* cannot even create a message... */
191  }
192  if (tb->size <= MAXSTRTB / 2) /* can grow string table? */
193  luaS_resize(L, tb->size * 2);
194 }
195 
196 
197 /*
198 ** Checks whether short string exists and reuses it or creates a new one.
199 */
200 static TString *internshrstr (lua_State *L, const char *str, size_t l) {
201  TString *ts;
202  global_State *g = G(L);
203  stringtable *tb = &g->strt;
204  unsigned int h = luaS_hash(str, l, g->seed, 1);
205  TString **list = &tb->hash[lmod(h, tb->size)];
206  lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
207  for (ts = *list; ts != NULL; ts = ts->u.hnext) {
208  if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
209  /* found! */
210  if (isdead(g, ts)) /* dead (but not collected yet)? */
211  changewhite(ts); /* resurrect it */
212  return ts;
213  }
214  }
215  /* else must create a new string */
216  if (tb->nuse >= tb->size) { /* need to grow string table? */
217  growstrtab(L, tb);
218  list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */
219  }
220  ts = createstrobj(L, l, LUA_VSHRSTR, h);
221  memcpy(getstr(ts), str, l * sizeof(char));
222  ts->shrlen = cast_byte(l);
223  ts->u.hnext = *list;
224  *list = ts;
225  tb->nuse++;
226  return ts;
227 }
228 
229 
230 /*
231 ** new string (with explicit length)
232 */
233 TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
234  if (l <= LUAI_MAXSHORTLEN) /* short string? */
235  return internshrstr(L, str, l);
236  else {
237  TString *ts;
238  if (unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char)))
239  luaM_toobig(L);
240  ts = luaS_createlngstrobj(L, l);
241  memcpy(getstr(ts), str, l * sizeof(char));
242  return ts;
243  }
244 }
245 
246 
247 /*
248 ** Create or reuse a zero-terminated string, first checking in the
249 ** cache (using the string address as a key). The cache can contain
250 ** only zero-terminated strings, so it is safe to use 'strcmp' to
251 ** check hits.
252 */
253 TString *luaS_new (lua_State *L, const char *str) {
254  unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
255  int j;
256  TString **p = G(L)->strcache[i];
257  for (j = 0; j < STRCACHE_M; j++) {
258  if (strcmp(str, getstr(p[j])) == 0) /* hit? */
259  return p[j]; /* that is it */
260  }
261  /* normal route */
262  for (j = STRCACHE_M - 1; j > 0; j--)
263  p[j] = p[j - 1]; /* move out last element */
264  /* new element is first in the list */
265  p[0] = luaS_newlstr(L, str, strlen(str));
266  return p[0];
267 }
268 
269 
270 Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) {
271  Udata *u;
272  int i;
273  GCObject *o;
274  if (unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
275  luaM_toobig(L);
276  o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
277  u = gco2u(o);
278  u->len = s;
279  u->nuvalue = nuvalue;
280  u->metatable = NULL;
281  for (i = 0; i < nuvalue; i++)
282  setnilvalue(&u->uv[i].uv);
283  return u;
284 }
285 
#define luaM_error(L)
Definition: lmem.h:17
#define changewhite(x)
Definition: lgc.h:98
void luaS_remove(lua_State *L, TString *ts)
Definition: lstring.c:176
#define STRCACHE_M
Definition: llimits.h:227
#define LUAI_MAXSHORTLEN
Definition: llimits.h:205
#define luaM_newvector(L, n, t)
Definition: lmem.h:60
static TString * createstrobj(lua_State *L, size_t l, int tag, unsigned int h)
Definition: lstring.c:155
#define obj2gco(v)
Definition: lstate.h:381
l_noret luaM_toobig(lua_State *L)
Definition: lmem.c:124
unsigned short nuvalue
Definition: lobject.h:439
#define iswhite(x)
Definition: lgc.h:87
unsigned int luaS_hashlongstr(TString *ts)
Definition: lstring.c:62
#define LUAI_HASHLIMIT
Definition: lstring.c:30
Udata * luaS_newudata(lua_State *L, size_t s, int nuvalue)
Definition: lstring.c:270
#define cast_uint(i)
Definition: llimits.h:129
lu_byte extra
Definition: lobject.h:364
#define cast_byte(i)
Definition: llimits.h:130
#define setnilvalue(obj)
Definition: lobject.h:176
#define MINSTRTABSIZE
Definition: llimits.h:216
void luaS_clearcache(global_State *g)
Definition: lstring.c:122
#define G(L)
Definition: lstate.h:332
size_t len
Definition: lobject.h:440
TString * luaS_newlstr(lua_State *L, const char *str, size_t l)
Definition: lstring.c:233
void luaC_fix(lua_State *L, GCObject *o)
Definition: lgc.c:244
#define unlikely(x)
Definition: llimits.h:162
size_t lnglen
Definition: lobject.h:368
static TString * internshrstr(lua_State *L, const char *str, size_t l)
Definition: lstring.c:200
struct Table * metatable
Definition: lobject.h:441
TString * luaS_createlngstrobj(lua_State *L, size_t l)
Definition: lstring.c:169
#define luaM_reallocvector(L, v, oldn, n, t)
Definition: lmem.h:70
#define luaS_newliteral(L, s)
Definition: lstring.h:28
unsigned int hash
Definition: lobject.h:366
#define getstr(ts)
Definition: lobject.h:379
#define gco2u(o)
Definition: lstate.h:366
#define STRCACHE_N
Definition: llimits.h:226
int nuse
Definition: lstate.h:185
TValue uv
Definition: lobject.h:428
#define LUA_VSHRSTR
Definition: lobject.h:336
#define udatamemoffset(nuv)
Definition: lobject.h:466
lu_byte shrlen
Definition: lobject.h:365
UValue uv[1]
Definition: lobject.h:443
#define LUA_VLNGSTR
Definition: lobject.h:337
#define isdead(g, v)
Definition: lgc.h:96
TString * memerrmsg
Definition: lstate.h:293
union UpVal::@22 u
void luaS_resize(lua_State *L, int nsize)
Definition: lstring.c:97
#define point2uint(p)
Definition: llimits.h:78
#define lua_assert(c)
Definition: llimits.h:101
TString * strcache[STRCACHE_N][STRCACHE_M]
Definition: lstate.h:296
void luaS_init(lua_State *L)
Definition: lstring.c:135
#define sizeudata(nuv, nb)
Definition: lobject.h:474
int size
Definition: lstate.h:186
TString ** hash
Definition: lstate.h:184
TString * luaS_new(lua_State *L, const char *str)
Definition: lstring.c:253
#define gco2ts(o)
Definition: lstate.h:364
#define lmod(s, size)
Definition: lobject.h:761
#define MAX_INT
Definition: llimits.h:53
GCObject * luaC_newobj(lua_State *L, int tt, size_t sz)
Definition: lgc.c:259
unsigned int luaS_hash(const char *str, size_t l, unsigned int seed, size_t step)
Definition: lstring.c:53
unsigned int seed
Definition: lstate.h:261
#define MAXSTRTB
Definition: lstring.c:38
Definition: lobject.h:437
stringtable strt
Definition: lstate.h:258
struct TString * hnext
Definition: lobject.h:369
union TString::@20 u
static void growstrtab(lua_State *L, stringtable *tb)
Definition: lstring.c:186
int luaS_eqlngstr(TString *a, TString *b)
Definition: lstring.c:44
void luaC_fullgc(lua_State *L, int isemergency)
Definition: lgc.c:1697
static void tablerehash(TString **vect, int osize, int nsize)
Definition: lstring.c:74
#define MAX_SIZE
Definition: llimits.h:44
#define LUA_VUSERDATA
Definition: lobject.h:407
#define sizelstring(l)
Definition: lstring.h:26
#define MEMERRMSG
Definition: lstring.h:19
int len
Definition: utf-8.c:46


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:09