Coverage Report

Created: 2025-10-27 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/testdir/build/lua-master/source/lstring.c
Line
Count
Source
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
41.5k
#define MAXSTRTB  cast_int(luaM_limitN(INT_MAX, TString*))
29
30
/*
31
** Initial size for the string table (must be power of 2).
32
** The Lua core alone registers ~50 strings (reserved words +
33
** metaevent keys + a few others). Libraries would typically add
34
** a few dozens more.
35
*/
36
#if !defined(MINSTRTABSIZE)
37
125k
#define MINSTRTABSIZE   128
38
#endif
39
40
41
/*
42
** generic equality for strings
43
*/
44
12.1M
int luaS_eqstr (TString *a, TString *b) {
45
12.1M
  size_t len1, len2;
46
12.1M
  const char *s1 = getlstr(a, len1);
47
12.1M
  const char *s2 = getlstr(b, len2);
48
12.1M
  return ((len1 == len2) &&  /* equal length and ... */
49
6.74M
          (memcmp(s1, s2, len1) == 0));  /* equal contents */
50
12.1M
}
51
52
53
298M
static unsigned luaS_hash (const char *str, size_t l, unsigned seed) {
54
298M
  unsigned int h = seed ^ cast_uint(l);
55
4.38G
  for (; l > 0; l--)
56
4.08G
    h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
57
298M
  return h;
58
298M
}
59
60
61
19.7M
unsigned luaS_hashlongstr (TString *ts) {
62
19.7M
  lua_assert(ts->tt == LUA_VLNGSTR);
63
19.7M
  if (ts->extra == 0) {  /* no hash? */
64
2.91M
    size_t len = ts->u.lnglen;
65
2.91M
    ts->hash = luaS_hash(getlngstr(ts), len, ts->hash);
66
2.91M
    ts->extra = 1;  /* now it has its hash */
67
2.91M
  }
68
19.7M
  return ts->hash;
69
19.7M
}
70
71
72
109k
static void tablerehash (TString **vect, int osize, int nsize) {
73
109k
  int i;
74
25.1M
  for (i = osize; i < nsize; i++)  /* clear new elements */
75
25.0M
    vect[i] = NULL;
76
26.7M
  for (i = 0; i < osize; i++) {  /* rehash old part of the array */
77
26.6M
    TString *p = vect[i];
78
26.6M
    vect[i] = NULL;
79
45.3M
    while (p) {  /* for each string in the list */
80
18.6M
      TString *hnext = p->u.hnext;  /* save next */
81
18.6M
      unsigned int h = lmod(p->hash, nsize);  /* new position */
82
18.6M
      p->u.hnext = vect[h];  /* chain it into array */
83
18.6M
      vect[h] = p;
84
18.6M
      p = hnext;
85
18.6M
    }
86
26.6M
  }
87
109k
}
88
89
90
/*
91
** Resize the string table. If allocation fails, keep the current size.
92
** (This can degrade performance, but any non-zero size should work
93
** correctly.)
94
*/
95
46.5k
void luaS_resize (lua_State *L, int nsize) {
96
46.5k
  stringtable *tb = &G(L)->strt;
97
46.5k
  int osize = tb->size;
98
46.5k
  TString **newvect;
99
46.5k
  if (nsize < osize)  /* shrinking table? */
100
5.00k
    tablerehash(tb->hash, osize, nsize);  /* depopulate shrinking part */
101
46.5k
  newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
102
46.5k
  if (l_unlikely(newvect == NULL)) {  /* reallocation failed? */
103
0
    if (nsize < osize)  /* was it shrinking table? */
104
0
      tablerehash(tb->hash, nsize, osize);  /* restore to original size */
105
    /* leave table as it was */
106
0
  }
107
46.5k
  else {  /* allocation succeeded */
108
46.5k
    tb->hash = newvect;
109
46.5k
    tb->size = nsize;
110
46.5k
    if (nsize > osize)
111
41.5k
      tablerehash(newvect, osize, nsize);  /* rehash for new size */
112
46.5k
  }
113
46.5k
}
114
115
116
/*
117
** Clear API string cache. (Entries cannot be empty, so fill them with
118
** a non-collectable string.)
119
*/
120
1.54M
void luaS_clearcache (global_State *g) {
121
1.54M
  int i, j;
122
83.3M
  for (i = 0; i < STRCACHE_N; i++)
123
245M
    for (j = 0; j < STRCACHE_M; j++) {
124
163M
      if (iswhite(g->strcache[i][j]))  /* will entry be collected? */
125
5.15M
        g->strcache[i][j] = g->memerrmsg;  /* replace it with something fixed */
126
163M
    }
127
1.54M
}
128
129
130
/*
131
** Initialize the string table and the string cache
132
*/
133
62.8k
void luaS_init (lua_State *L) {
134
62.8k
  global_State *g = G(L);
135
62.8k
  int i, j;
136
62.8k
  stringtable *tb = &G(L)->strt;
137
62.8k
  tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*);
138
62.8k
  tablerehash(tb->hash, 0, MINSTRTABSIZE);  /* clear array */
139
62.8k
  tb->size = MINSTRTABSIZE;
140
  /* pre-create memory-error message */
141
62.8k
  g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
142
62.8k
  luaC_fix(L, obj2gco(g->memerrmsg));  /* it should never be collected */
143
3.39M
  for (i = 0; i < STRCACHE_N; i++)  /* fill cache with valid strings */
144
9.98M
    for (j = 0; j < STRCACHE_M; j++)
145
6.65M
      g->strcache[i][j] = g->memerrmsg;
146
62.8k
}
147
148
149
100M
size_t luaS_sizelngstr (size_t len, int kind) {
150
100M
  switch (kind) {
151
95.1M
    case LSTRREG:  /* regular long string */
152
      /* don't need 'falloc'/'ud', but need space for content */
153
95.1M
      return offsetof(TString, falloc) + (len + 1) * sizeof(char);
154
1.32M
    case LSTRFIX:  /* fixed external long string */
155
      /* don't need 'falloc'/'ud' */
156
1.32M
      return offsetof(TString, falloc);
157
4.33M
    default:  /* external long string with deallocation */
158
4.33M
      lua_assert(kind == LSTRMEM);
159
4.33M
      return sizeof(TString);
160
100M
  }
161
100M
}
162
163
164
/*
165
** creates a new string object
166
*/
167
static TString *createstrobj (lua_State *L, size_t totalsize, lu_byte tag,
168
54.9M
                              unsigned h) {
169
54.9M
  TString *ts;
170
54.9M
  GCObject *o;
171
54.9M
  o = luaC_newobj(L, tag, totalsize);
172
54.9M
  ts = gco2ts(o);
173
54.9M
  ts->hash = h;
174
54.9M
  ts->extra = 0;
175
54.9M
  return ts;
176
54.9M
}
177
178
179
23.2M
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
180
23.2M
  size_t totalsize = luaS_sizelngstr(l, LSTRREG);
181
23.2M
  TString *ts = createstrobj(L, totalsize, LUA_VLNGSTR, G(L)->seed);
182
23.2M
  ts->u.lnglen = l;
183
23.2M
  ts->shrlen = LSTRREG;  /* signals that it is a regular long string */
184
23.2M
  ts->contents = cast_charp(ts) + offsetof(TString, falloc);
185
23.2M
  ts->contents[l] = '\0';  /* ending 0 */
186
23.2M
  return ts;
187
23.2M
}
188
189
190
30.5M
void luaS_remove (lua_State *L, TString *ts) {
191
30.5M
  stringtable *tb = &G(L)->strt;
192
30.5M
  TString **p = &tb->hash[lmod(ts->hash, tb->size)];
193
33.8M
  while (*p != ts)  /* find previous element */
194
3.26M
    p = &(*p)->u.hnext;
195
30.5M
  *p = (*p)->u.hnext;  /* remove element from its list */
196
30.5M
  tb->nuse--;
197
30.5M
}
198
199
200
41.5k
static void growstrtab (lua_State *L, stringtable *tb) {
201
41.5k
  if (l_unlikely(tb->nuse == INT_MAX)) {  /* too many strings? */
202
0
    luaC_fullgc(L, 1);  /* try to free some... */
203
0
    if (tb->nuse == INT_MAX)  /* still too many? */
204
0
      luaM_error(L);  /* cannot even create a message... */
205
0
  }
206
41.5k
  if (tb->size <= MAXSTRTB / 2)  /* can grow string table? */
207
41.5k
    luaS_resize(L, tb->size * 2);
208
41.5k
}
209
210
211
/*
212
** Checks whether short string exists and reuses it or creates a new one.
213
*/
214
295M
static TString *internshrstr (lua_State *L, const char *str, size_t l) {
215
295M
  TString *ts;
216
295M
  global_State *g = G(L);
217
295M
  stringtable *tb = &g->strt;
218
295M
  unsigned int h = luaS_hash(str, l, g->seed);
219
295M
  TString **list = &tb->hash[lmod(h, tb->size)];
220
295M
  lua_assert(str != NULL);  /* otherwise 'memcmp'/'memcpy' are undefined */
221
371M
  for (ts = *list; ts != NULL; ts = ts->u.hnext) {
222
340M
    if (l == cast_uint(ts->shrlen) &&
223
273M
        (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) {
224
      /* found! */
225
265M
      if (isdead(g, ts))  /* dead (but not collected yet)? */
226
2.21M
        changewhite(ts);  /* resurrect it */
227
265M
      return ts;
228
265M
    }
229
340M
  }
230
  /* else must create a new string */
231
30.5M
  if (tb->nuse >= tb->size) {  /* need to grow string table? */
232
41.5k
    growstrtab(L, tb);
233
41.5k
    list = &tb->hash[lmod(h, tb->size)];  /* rehash with new size */
234
41.5k
  }
235
30.5M
  ts = createstrobj(L, sizestrshr(l), LUA_VSHRSTR, h);
236
30.5M
  ts->shrlen = cast(ls_byte, l);
237
30.5M
  getshrstr(ts)[l] = '\0';  /* ending 0 */
238
30.5M
  memcpy(getshrstr(ts), str, l * sizeof(char));
239
30.5M
  ts->u.hnext = *list;
240
30.5M
  *list = ts;
241
30.5M
  tb->nuse++;
242
30.5M
  return ts;
243
30.5M
}
244
245
246
/*
247
** new string (with explicit length)
248
*/
249
315M
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
250
315M
  if (l <= LUAI_MAXSHORTLEN)  /* short string? */
251
295M
    return internshrstr(L, str, l);
252
19.8M
  else {
253
19.8M
    TString *ts;
254
19.8M
    if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString))))
255
0
      luaM_toobig(L);
256
19.8M
    ts = luaS_createlngstrobj(L, l);
257
19.8M
    memcpy(getlngstr(ts), str, l * sizeof(char));
258
19.8M
    return ts;
259
19.8M
  }
260
315M
}
261
262
263
/*
264
** Create or reuse a zero-terminated string, first checking in the
265
** cache (using the string address as a key). The cache can contain
266
** only zero-terminated strings, so it is safe to use 'strcmp' to
267
** check hits.
268
*/
269
285M
TString *luaS_new (lua_State *L, const char *str) {
270
285M
  unsigned int i = point2uint(str) % STRCACHE_N;  /* hash */
271
285M
  int j;
272
285M
  TString **p = G(L)->strcache[i];
273
375M
  for (j = 0; j < STRCACHE_M; j++) {
274
358M
    if (strcmp(str, getstr(p[j])) == 0)  /* hit? */
275
268M
      return p[j];  /* that is it */
276
358M
  }
277
  /* normal route */
278
33.1M
  for (j = STRCACHE_M - 1; j > 0; j--)
279
16.5M
    p[j] = p[j - 1];  /* move out last element */
280
  /* new element is first in the list */
281
16.5M
  p[0] = luaS_newlstr(L, str, strlen(str));
282
16.5M
  return p[0];
283
285M
}
284
285
286
2.65M
Udata *luaS_newudata (lua_State *L, size_t s, unsigned short nuvalue) {
287
2.65M
  Udata *u;
288
2.65M
  int i;
289
2.65M
  GCObject *o;
290
2.65M
  if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
291
0
    luaM_toobig(L);
292
2.65M
  o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
293
2.65M
  u = gco2u(o);
294
2.65M
  u->len = s;
295
2.65M
  u->nuvalue = nuvalue;
296
2.65M
  u->metatable = NULL;
297
2.65M
  for (i = 0; i < nuvalue; i++)
298
2.65M
    setnilvalue(&u->uv[i].uv);
299
2.65M
  return u;
300
2.65M
}
301
302
303
struct NewExt {
304
  ls_byte kind;
305
  const char *s;
306
   size_t len;
307
  TString *ts;  /* output */
308
};
309
310
311
1.13M
static void f_newext (lua_State *L, void *ud) {
312
1.13M
  struct NewExt *ne = cast(struct NewExt *, ud);
313
1.13M
  size_t size = luaS_sizelngstr(0, ne->kind);
314
1.13M
  ne->ts = createstrobj(L, size, LUA_VLNGSTR, G(L)->seed);
315
1.13M
}
316
317
318
TString *luaS_newextlstr (lua_State *L,
319
1.13M
            const char *s, size_t len, lua_Alloc falloc, void *ud) {
320
1.13M
  struct NewExt ne;
321
1.13M
  if (!falloc) {
322
59.3k
    ne.kind = LSTRFIX;
323
59.3k
    f_newext(L, &ne);  /* just create header */
324
59.3k
  }
325
1.07M
  else {
326
1.07M
    ne.kind = LSTRMEM;
327
1.07M
    if (luaD_rawrunprotected(L, f_newext, &ne) != LUA_OK) {  /* mem. error? */
328
0
      (*falloc)(ud, cast_voidp(s), len + 1, 0);  /* free external string */
329
0
      luaM_error(L);  /* re-raise memory error */
330
0
    }
331
1.07M
    ne.ts->falloc = falloc;
332
1.07M
    ne.ts->ud = ud;
333
1.07M
  }
334
1.13M
  ne.ts->shrlen = ne.kind;
335
1.13M
  ne.ts->u.lnglen = len;
336
1.13M
  ne.ts->contents = cast_charp(s);
337
1.13M
  return ne.ts;
338
1.13M
}
339
340
341
/*
342
** Normalize an external string: If it is short, internalize it.
343
*/
344
27
TString *luaS_normstr (lua_State *L, TString *ts) {
345
27
  size_t len = ts->u.lnglen;
346
27
  if (len > LUAI_MAXSHORTLEN)
347
27
    return ts;  /* long string; keep the original */
348
0
  else {
349
0
    const char *str = getlngstr(ts);
350
0
    return internshrstr(L, str, len);
351
0
  }
352
27
}
353