Coverage Report

Created: 2023-09-30 06:14

/src/testdir/build/lua-master/source/lstring.c
Line
Count
Source (jump to first uncovered line)
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
** Maximum size for string table.
27
*/
28
4.09k
#define MAXSTRTB  cast_int(luaM_limitN(MAX_INT, TString*))
29
30
31
/*
32
** equality for long strings
33
*/
34
809k
int luaS_eqlngstr (TString *a, TString *b) {
35
809k
  size_t len = a->u.lnglen;
36
809k
  lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR);
37
809k
  return (a == b) ||  /* same instance or... */
38
809k
    ((len == b->u.lnglen) &&  /* equal length and ... */
39
326k
     (memcmp(getlngstr(a), getlngstr(b), len) == 0));  /* equal contents */
40
809k
}
41
42
43
28.3M
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
44
28.3M
  unsigned int h = seed ^ cast_uint(l);
45
765M
  for (; l > 0; l--)
46
737M
    h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
47
28.3M
  return h;
48
28.3M
}
49
50
51
1.43M
unsigned int luaS_hashlongstr (TString *ts) {
52
1.43M
  lua_assert(ts->tt == LUA_VLNGSTR);
53
1.43M
  if (ts->extra == 0) {  /* no hash? */
54
276k
    size_t len = ts->u.lnglen;
55
276k
    ts->hash = luaS_hash(getlngstr(ts), len, ts->hash);
56
0
    ts->extra = 1;  /* now it has its hash */
57
276k
  }
58
1.43M
  return ts->hash;
59
1.43M
}
60
61
62
24.9k
static void tablerehash (TString **vect, int osize, int nsize) {
63
24.9k
  int i;
64
3.33M
  for (i = osize; i < nsize; i++)  /* clear new elements */
65
3.31M
    vect[i] = NULL;
66
824k
  for (i = 0; i < osize; i++) {  /* rehash old part of the array */
67
799k
    TString *p = vect[i];
68
799k
    vect[i] = NULL;
69
1.48M
    while (p) {  /* for each string in the list */
70
689k
      TString *hnext = p->u.hnext;  /* save next */
71
689k
      unsigned int h = lmod(p->hash, nsize);  /* new position */
72
0
      p->u.hnext = vect[h];  /* chain it into array */
73
689k
      vect[h] = p;
74
689k
      p = hnext;
75
689k
    }
76
799k
  }
77
24.9k
}
78
79
80
/*
81
** Resize the string table. If allocation fails, keep the current size.
82
** (This can degrade performance, but any non-zero size should work
83
** correctly.)
84
*/
85
4.27k
void luaS_resize (lua_State *L, int nsize) {
86
4.27k
  stringtable *tb = &G(L)->strt;
87
4.27k
  int osize = tb->size;
88
4.27k
  TString **newvect;
89
4.27k
  if (nsize < osize)  /* shrinking table? */
90
174
    tablerehash(tb->hash, osize, nsize);  /* depopulate shrinking part */
91
4.27k
  newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
92
4.27k
  if (l_unlikely(newvect == NULL)) {  /* reallocation failed? */
93
0
    if (nsize < osize)  /* was it shrinking table? */
94
0
      tablerehash(tb->hash, nsize, osize);  /* restore to original size */
95
    /* leave table as it was */
96
0
  }
97
4.27k
  else {  /* allocation succeeded */
98
4.27k
    tb->hash = newvect;
99
4.27k
    tb->size = nsize;
100
4.27k
    if (nsize > osize)
101
4.09k
      tablerehash(newvect, osize, nsize);  /* rehash for new size */
102
4.27k
  }
103
4.27k
}
104
105
106
/*
107
** Clear API string cache. (Entries cannot be empty, so fill them with
108
** a non-collectable string.)
109
*/
110
140k
void luaS_clearcache (global_State *g) {
111
140k
  int i, j;
112
7.59M
  for (i = 0; i < STRCACHE_N; i++)
113
22.3M
    for (j = 0; j < STRCACHE_M; j++) {
114
14.9M
      if (iswhite(g->strcache[i][j]))  /* will entry be collected? */
115
79.0k
        g->strcache[i][j] = g->memerrmsg;  /* replace it with something fixed */
116
14.9M
    }
117
140k
}
118
119
120
/*
121
** Initialize the string table and the string cache
122
*/
123
20.6k
void luaS_init (lua_State *L) {
124
20.6k
  global_State *g = G(L);
125
20.6k
  int i, j;
126
20.6k
  stringtable *tb = &G(L)->strt;
127
20.6k
  tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*);
128
20.6k
  tablerehash(tb->hash, 0, MINSTRTABSIZE);  /* clear array */
129
20.6k
  tb->size = MINSTRTABSIZE;
130
  /* pre-create memory-error message */
131
20.6k
  g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
132
20.6k
  luaC_fix(L, obj2gco(g->memerrmsg));  /* it should never be collected */
133
1.11M
  for (i = 0; i < STRCACHE_N; i++)  /* fill cache with valid strings */
134
3.28M
    for (j = 0; j < STRCACHE_M; j++)
135
2.18M
      g->strcache[i][j] = g->memerrmsg;
136
20.6k
}
137
138
139
140
/*
141
** creates a new string object
142
*/
143
2.76M
static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
144
2.76M
  TString *ts;
145
2.76M
  GCObject *o;
146
2.76M
  size_t totalsize;  /* total size of TString object */
147
2.76M
  totalsize = sizelstring(l);
148
2.76M
  o = luaC_newobj(L, tag, totalsize);
149
2.76M
  ts = gco2ts(o);
150
0
  ts->hash = h;
151
2.76M
  ts->extra = 0;
152
2.76M
  getstr(ts)[l] = '\0';  /* ending 0 */
153
2.76M
  return ts;
154
2.76M
}
155
156
157
692k
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
158
692k
  TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed);
159
692k
  ts->u.lnglen = l;
160
692k
  ts->shrlen = 0xFF;  /* signals that it is a long string */
161
692k
  return ts;
162
692k
}
163
164
165
2.07M
void luaS_remove (lua_State *L, TString *ts) {
166
2.07M
  stringtable *tb = &G(L)->strt;
167
2.07M
  TString **p = &tb->hash[lmod(ts->hash, tb->size)];
168
2.20M
  while (*p != ts)  /* find previous element */
169
128k
    p = &(*p)->u.hnext;
170
2.07M
  *p = (*p)->u.hnext;  /* remove element from its list */
171
2.07M
  tb->nuse--;
172
2.07M
}
173
174
175
4.09k
static void growstrtab (lua_State *L, stringtable *tb) {
176
4.09k
  if (l_unlikely(tb->nuse == MAX_INT)) {  /* too many strings? */
177
0
    luaC_fullgc(L, 1);  /* try to free some... */
178
0
    if (tb->nuse == MAX_INT)  /* still too many? */
179
0
      luaM_error(L);  /* cannot even create a message... */
180
0
  }
181
4.09k
  if (tb->size <= MAXSTRTB / 2)  /* can grow string table? */
182
4.09k
    luaS_resize(L, tb->size * 2);
183
4.09k
}
184
185
186
/*
187
** Checks whether short string exists and reuses it or creates a new one.
188
*/
189
28.0M
static TString *internshrstr (lua_State *L, const char *str, size_t l) {
190
28.0M
  TString *ts;
191
28.0M
  global_State *g = G(L);
192
28.0M
  stringtable *tb = &g->strt;
193
28.0M
  unsigned int h = luaS_hash(str, l, g->seed);
194
28.0M
  TString **list = &tb->hash[lmod(h, tb->size)];
195
28.0M
  lua_assert(str != NULL);  /* otherwise 'memcmp'/'memcpy' are undefined */
196
34.0M
  for (ts = *list; ts != NULL; ts = ts->u.hnext) {
197
31.9M
    if (l == ts->shrlen && (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) {
198
      /* found! */
199
25.9M
      if (isdead(g, ts))  /* dead (but not collected yet)? */
200
1.24k
        changewhite(ts);  /* resurrect it */
201
25.9M
      return ts;
202
25.9M
    }
203
31.9M
  }
204
  /* else must create a new string */
205
2.07M
  if (tb->nuse >= tb->size) {  /* need to grow string table? */
206
4.09k
    growstrtab(L, tb);
207
4.09k
    list = &tb->hash[lmod(h, tb->size)];  /* rehash with new size */
208
4.09k
  }
209
2.07M
  ts = createstrobj(L, l, LUA_VSHRSTR, h);
210
2.07M
  ts->shrlen = cast_byte(l);
211
2.07M
  memcpy(getshrstr(ts), str, l * sizeof(char));
212
0
  ts->u.hnext = *list;
213
2.07M
  *list = ts;
214
2.07M
  tb->nuse++;
215
2.07M
  return ts;
216
2.07M
}
217
218
219
/*
220
** new string (with explicit length)
221
*/
222
28.7M
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
223
28.7M
  if (l <= LUAI_MAXSHORTLEN)  /* short string? */
224
28.0M
    return internshrstr(L, str, l);
225
673k
  else {
226
673k
    TString *ts;
227
673k
    if (l_unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char)))
228
0
      luaM_toobig(L);
229
673k
    ts = luaS_createlngstrobj(L, l);
230
673k
    memcpy(getlngstr(ts), str, l * sizeof(char));
231
0
    return ts;
232
673k
  }
233
28.7M
}
234
235
236
/*
237
** Create or reuse a zero-terminated string, first checking in the
238
** cache (using the string address as a key). The cache can contain
239
** only zero-terminated strings, so it is safe to use 'strcmp' to
240
** check hits.
241
*/
242
2.53M
TString *luaS_new (lua_State *L, const char *str) {
243
2.53M
  unsigned int i = point2uint(str) % STRCACHE_N;  /* hash */
244
2.53M
  int j;
245
2.53M
  TString **p = G(L)->strcache[i];
246
5.78M
  for (j = 0; j < STRCACHE_M; j++) {
247
4.20M
    if (strcmp(str, getstr(p[j])) == 0)  /* hit? */
248
950k
      return p[j];  /* that is it */
249
4.20M
  }
250
  /* normal route */
251
3.16M
  for (j = STRCACHE_M - 1; j > 0; j--)
252
1.58M
    p[j] = p[j - 1];  /* move out last element */
253
  /* new element is first in the list */
254
1.58M
  p[0] = luaS_newlstr(L, str, strlen(str));
255
1.58M
  return p[0];
256
2.53M
}
257
258
259
57.0k
Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) {
260
57.0k
  Udata *u;
261
57.0k
  int i;
262
57.0k
  GCObject *o;
263
57.0k
  if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
264
0
    luaM_toobig(L);
265
57.0k
  o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
266
57.0k
  u = gco2u(o);
267
0
  u->len = s;
268
57.0k
  u->nuvalue = nuvalue;
269
57.0k
  u->metatable = NULL;
270
57.0k
  for (i = 0; i < nuvalue; i++)
271
57.0k
    setnilvalue(&u->uv[i].uv);
272
57.0k
  return u;
273
57.0k
}
274