Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lstring.c File Reference
#include "lprefix.h"
#include <string.h>
#include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "lstring.h"

Go to the source code of this file.

Macros

#define lstring_c
 
#define LUA_CORE
 
#define MAXSTRTB   cast_int(luaM_limitN(MAX_INT, TString*))
 

Functions

int luaS_eqlngstr (TString *a, TString *b)
 
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed)
 
unsigned int luaS_hashlongstr (TString *ts)
 
static void tablerehash (TString **vect, int osize, int nsize)
 
void luaS_resize (lua_State *L, int nsize)
 
void luaS_clearcache (global_State *g)
 
void luaS_init (lua_State *L)
 
static TStringcreatestrobj (lua_State *L, size_t l, int tag, unsigned int h)
 
TStringluaS_createlngstrobj (lua_State *L, size_t l)
 
void luaS_remove (lua_State *L, TString *ts)
 
static void growstrtab (lua_State *L, stringtable *tb)
 
static TStringinternshrstr (lua_State *L, const char *str, size_t l)
 
TStringluaS_newlstr (lua_State *L, const char *str, size_t l)
 
TStringluaS_new (lua_State *L, const char *str)
 
UdataluaS_newudata (lua_State *L, size_t s, int nuvalue)
 

Macro Definition Documentation

◆ lstring_c

#define lstring_c

Definition at line 7 of file lua-5.4.3/src/lstring.c.

◆ LUA_CORE

#define LUA_CORE

Definition at line 8 of file lua-5.4.3/src/lstring.c.

◆ MAXSTRTB

#define MAXSTRTB   cast_int(luaM_limitN(MAX_INT, TString*))

Definition at line 28 of file lua-5.4.3/src/lstring.c.

Referenced by growstrtab().

Function Documentation

◆ createstrobj()

static TString * createstrobj ( lua_State * L,
size_t l,
int tag,
unsigned int h )
static

Definition at line 143 of file lua-5.4.3/src/lstring.c.

143 {
144 TString *ts;
145 GCObject *o;
146 size_t totalsize; /* total size of TString object */
147 totalsize = sizelstring(l);
148 o = luaC_newobj(L, tag, totalsize);
149 ts = gco2ts(o);
150 ts->hash = h;
151 ts->extra = 0;
152 getstr(ts)[l] = '\0'; /* ending 0 */
153 return ts;
154}
#define getstr(ts)
#define gco2ts(o)
GCObject * luaC_newobj(lua_State *L, int tt, size_t sz, GCObject **list, int offset)
#define sizelstring(l)
unsigned int hash

References TString::extra, gco2ts, getstr, TString::hash, luaC_newobj(), and sizelstring.

Referenced by internshrstr(), and luaS_createlngstrobj().

◆ growstrtab()

static void growstrtab ( lua_State * L,
stringtable * tb )
static

Definition at line 174 of file lua-5.4.3/src/lstring.c.

174 {
175 if (l_unlikely(tb->nuse == MAX_INT)) { /* too many strings? */
176 luaC_fullgc(L, 1); /* try to free some... */
177 if (tb->nuse == MAX_INT) /* still too many? */
178 luaM_error(L); /* cannot even create a message... */
179 }
180 if (tb->size <= MAXSTRTB / 2) /* can grow string table? */
181 luaS_resize(L, tb->size * 2);
182}
void luaC_fullgc(lua_State *L)
#define MAX_INT
#define luaM_error(L)
#define MAXSTRTB
void luaS_resize(lua_State *L, int nsize)

References luaC_fullgc(), luaM_error, luaS_resize(), MAX_INT, MAXSTRTB, stringtable::nuse, and stringtable::size.

Referenced by internshrstr().

◆ internshrstr()

static TString * internshrstr ( lua_State * L,
const char * str,
size_t l )
static

Definition at line 188 of file lua-5.4.3/src/lstring.c.

188 {
189 TString *ts;
190 global_State *g = G(L);
191 stringtable *tb = &g->strt;
192 unsigned int h = luaS_hash(str, l, g->seed);
193 TString **list = &tb->hash[lmod(h, tb->size)];
194 lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
195 for (ts = *list; ts != NULL; ts = ts->u.hnext) {
196 if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
197 /* found! */
198 if (isdead(g, ts)) /* dead (but not collected yet)? */
199 changewhite(ts); /* resurrect it */
200 return ts;
201 }
202 }
203 /* else must create a new string */
204 if (tb->nuse >= tb->size) { /* need to grow string table? */
205 growstrtab(L, tb);
206 list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */
207 }
208 ts = createstrobj(L, l, LUA_VSHRSTR, h);
209 memcpy(getstr(ts), str, l * sizeof(char));
210 ts->shrlen = cast_byte(l);
211 ts->u.hnext = *list;
212 *list = ts;
213 tb->nuse++;
214 return ts;
215}
#define NULL
Definition gmacros.h:924
#define changewhite(x)
#define isdead(g, v)
#define cast_byte(i)
#define lua_assert(c)
#define lmod(s, size)
#define G(L)
#define LUA_VSHRSTR
static TString * createstrobj(lua_State *L, size_t l, int tag, unsigned int h)
unsigned int luaS_hash(const char *str, size_t l, unsigned int seed)
static void growstrtab(lua_State *L, stringtable *tb)
union TString::@70 u
struct TString * hnext

References cast_byte, changewhite, createstrobj(), G, getstr, growstrtab(), stringtable::hash, TString::hnext, isdead, lmod, lua_assert, LUA_VSHRSTR, luaS_hash(), NULL, stringtable::nuse, global_State::seed, TString::shrlen, stringtable::size, global_State::strt, and TString::u.

Referenced by luaS_newlstr().

◆ luaS_clearcache()

void luaS_clearcache ( global_State * g)

Definition at line 110 of file lua-5.4.3/src/lstring.c.

110 {
111 int i, j;
112 for (i = 0; i < STRCACHE_N; i++)
113 for (j = 0; j < STRCACHE_M; j++) {
114 if (iswhite(g->strcache[i][j])) /* will entry be collected? */
115 g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
116 }
117}
#define iswhite(x)
#define STRCACHE_M
#define STRCACHE_N
TString * strcache[STRCACHE_N][STRCACHE_M]

References iswhite, global_State::memerrmsg, global_State::strcache, STRCACHE_M, and STRCACHE_N.

◆ luaS_createlngstrobj()

TString * luaS_createlngstrobj ( lua_State * L,
size_t l )

Definition at line 157 of file lua-5.4.3/src/lstring.c.

157 {
158 TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed);
159 ts->u.lnglen = l;
160 return ts;
161}
#define LUA_VLNGSTR

References createstrobj(), G, TString::lnglen, LUA_VLNGSTR, and TString::u.

Referenced by luaS_newlstr().

◆ luaS_eqlngstr()

int luaS_eqlngstr ( TString * a,
TString * b )

Definition at line 34 of file lua-5.4.3/src/lstring.c.

34 {
35 size_t len = a->u.lnglen;
36 lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR);
37 return (a == b) || /* same instance or... */
38 ((len == b->u.lnglen) && /* equal length and ... */
39 (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
40}

References getstr, TString::lnglen, lua_assert, LUA_VLNGSTR, and TString::u.

◆ luaS_hash()

unsigned int luaS_hash ( const char * str,
size_t l,
unsigned int seed )

Definition at line 43 of file lua-5.4.3/src/lstring.c.

43 {
44 unsigned int h = seed ^ cast_uint(l);
45 for (; l > 0; l--)
46 h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
47 return h;
48}
#define cast_uint(i)

References cast_byte, and cast_uint.

Referenced by internshrstr(), and luaS_hashlongstr().

◆ luaS_hashlongstr()

unsigned int luaS_hashlongstr ( TString * ts)

Definition at line 51 of file lua-5.4.3/src/lstring.c.

51 {
52 lua_assert(ts->tt == LUA_VLNGSTR);
53 if (ts->extra == 0) { /* no hash? */
54 size_t len = ts->u.lnglen;
55 ts->hash = luaS_hash(getstr(ts), len, ts->hash);
56 ts->extra = 1; /* now it has its hash */
57 }
58 return ts->hash;
59}

References TString::extra, getstr, TString::hash, TString::lnglen, lua_assert, LUA_VLNGSTR, luaS_hash(), and TString::u.

◆ luaS_init()

void luaS_init ( lua_State * L)

Definition at line 123 of file lua-5.4.3/src/lstring.c.

123 {
124 global_State *g = G(L);
125 int i, j;
126 stringtable *tb = &G(L)->strt;
128 tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */
129 tb->size = MINSTRTABSIZE;
130 /* pre-create memory-error message */
132 luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
133 for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
134 for (j = 0; j < STRCACHE_M; j++)
135 g->strcache[i][j] = g->memerrmsg;
136}
#define MINSTRTABSIZE
#define MEMERRMSG
#define luaM_newvector(L, n, t)
#define obj2gco(v)
#define luaS_newliteral(L, s)
void luaC_fix(lua_State *L, GCObject *o)
static void tablerehash(TString **vect, int osize, int nsize)

References G, stringtable::hash, luaC_fix(), luaM_newvector, luaS_newliteral, MEMERRMSG, global_State::memerrmsg, MINSTRTABSIZE, obj2gco, stringtable::size, global_State::strcache, STRCACHE_M, STRCACHE_N, and tablerehash().

◆ luaS_new()

TString * luaS_new ( lua_State * L,
const char * str )

Definition at line 241 of file lua-5.4.3/src/lstring.c.

241 {
242 unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
243 int j;
244 TString **p = G(L)->strcache[i];
245 for (j = 0; j < STRCACHE_M; j++) {
246 if (strcmp(str, getstr(p[j])) == 0) /* hit? */
247 return p[j]; /* that is it */
248 }
249 /* normal route */
250 for (j = STRCACHE_M - 1; j > 0; j--)
251 p[j] = p[j - 1]; /* move out last element */
252 /* new element is first in the list */
253 p[0] = luaS_newlstr(L, str, strlen(str));
254 return p[0];
255}
#define point2uint(p)
TString * luaS_newlstr(lua_State *L, const char *str, size_t l)

References G, getstr, luaS_newlstr(), point2uint, STRCACHE_M, and STRCACHE_N.

◆ luaS_newlstr()

TString * luaS_newlstr ( lua_State * L,
const char * str,
size_t l )

Definition at line 221 of file lua-5.4.3/src/lstring.c.

221 {
222 if (l <= LUAI_MAXSHORTLEN) /* short string? */
223 return internshrstr(L, str, l);
224 else {
225 TString *ts;
226 if (l_unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char)))
227 luaM_toobig(L);
228 ts = luaS_createlngstrobj(L, l);
229 memcpy(getstr(ts), str, l * sizeof(char));
230 return ts;
231 }
232}
void * luaM_toobig(lua_State *L)
#define LUAI_MAXSHORTLEN
#define MAX_SIZE
static TString * internshrstr(lua_State *L, const char *str, size_t l)
TString * luaS_createlngstrobj(lua_State *L, size_t l)

References getstr, internshrstr(), LUAI_MAXSHORTLEN, luaM_toobig(), luaS_createlngstrobj(), and MAX_SIZE.

Referenced by luaS_new().

◆ luaS_newudata()

Udata * luaS_newudata ( lua_State * L,
size_t s,
int nuvalue )

Definition at line 258 of file lua-5.4.3/src/lstring.c.

258 {
259 Udata *u;
260 int i;
261 GCObject *o;
262 if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
263 luaM_toobig(L);
264 o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
265 u = gco2u(o);
266 u->len = s;
267 u->nuvalue = nuvalue;
268 u->metatable = NULL;
269 for (i = 0; i < nuvalue; i++)
270 setnilvalue(&u->uv[i].uv);
271 return u;
272}
#define setnilvalue(obj)
#define gco2u(o)
#define sizeudata(u)
#define udatamemoffset(nuv)
#define LUA_VUSERDATA
CURL_EXTERN CURLMcode curl_socket_t s
Definition multi.h:318
struct Udata::@47 uv
struct Table * metatable
unsigned short nuvalue

References gco2u, Udata::len, LUA_VUSERDATA, luaC_newobj(), luaM_toobig(), MAX_SIZE, Udata::metatable, NULL, Udata::nuvalue, s, setnilvalue, sizeudata, udatamemoffset, and Udata::uv.

◆ luaS_remove()

void luaS_remove ( lua_State * L,
TString * ts )

Definition at line 164 of file lua-5.4.3/src/lstring.c.

164 {
165 stringtable *tb = &G(L)->strt;
166 TString **p = &tb->hash[lmod(ts->hash, tb->size)];
167 while (*p != ts) /* find previous element */
168 p = &(*p)->u.hnext;
169 *p = (*p)->u.hnext; /* remove element from its list */
170 tb->nuse--;
171}

References G, TString::hash, stringtable::hash, TString::hnext, lmod, stringtable::nuse, stringtable::size, and TString::u.

◆ luaS_resize()

void luaS_resize ( lua_State * L,
int nsize )

Definition at line 85 of file lua-5.4.3/src/lstring.c.

85 {
86 stringtable *tb = &G(L)->strt;
87 int osize = tb->size;
88 TString **newvect;
89 if (nsize < osize) /* shrinking table? */
90 tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */
91 newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
92 if (l_unlikely(newvect == NULL)) { /* reallocation failed? */
93 if (nsize < osize) /* was it shrinking table? */
94 tablerehash(tb->hash, nsize, osize); /* restore to original size */
95 /* leave table as it was */
96 }
97 else { /* allocation succeeded */
98 tb->hash = newvect;
99 tb->size = nsize;
100 if (nsize > osize)
101 tablerehash(newvect, osize, nsize); /* rehash for new size */
102 }
103}
#define luaM_reallocvector(L, v, oldn, n, t)

References G, stringtable::hash, luaM_reallocvector, NULL, stringtable::size, and tablerehash().

Referenced by growstrtab().

◆ tablerehash()

static void tablerehash ( TString ** vect,
int osize,
int nsize )
static

Definition at line 62 of file lua-5.4.3/src/lstring.c.

62 {
63 int i;
64 for (i = osize; i < nsize; i++) /* clear new elements */
65 vect[i] = NULL;
66 for (i = 0; i < osize; i++) { /* rehash old part of the array */
67 TString *p = vect[i];
68 vect[i] = NULL;
69 while (p) { /* for each string in the list */
70 TString *hnext = p->u.hnext; /* save next */
71 unsigned int h = lmod(p->hash, nsize); /* new position */
72 p->u.hnext = vect[h]; /* chain it into array */
73 vect[h] = p;
74 p = hnext;
75 }
76 }
77}

References TString::hash, TString::hnext, lmod, NULL, and TString::u.

Referenced by luaS_init(), and luaS_resize().