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 MEMERRMSG   "not enough memory"
 
#define LUAI_HASHLIMIT   5
 

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)
 
void luaS_resize (lua_State *L, int newsize)
 
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 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)
 

Macro Definition Documentation

◆ lstring_c

#define lstring_c

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

◆ LUA_CORE

#define LUA_CORE

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

◆ LUAI_HASHLIMIT

#define LUAI_HASHLIMIT   5

Definition at line 33 of file lua-5.3.6/src/lstring.c.

Referenced by luaS_hash().

◆ MEMERRMSG

#define MEMERRMSG   "not enough memory"

Definition at line 25 of file lua-5.3.6/src/lstring.c.

Referenced by luaS_init().

Function Documentation

◆ createstrobj()

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

Definition at line 133 of file lua-5.3.6/src/lstring.c.

133 {
134 TString *ts;
135 GCObject *o;
136 size_t totalsize; /* total size of TString object */
137 totalsize = sizelstring(l);
138 o = luaC_newobj(L, tag, totalsize);
139 ts = gco2ts(o);
140 ts->hash = h;
141 ts->extra = 0;
142 getstr(ts)[l] = '\0'; /* ending 0 */
143 return ts;
144}
#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().

◆ internshrstr()

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

Definition at line 167 of file lua-5.3.6/src/lstring.c.

167 {
168 TString *ts;
169 global_State *g = G(L);
170 unsigned int h = luaS_hash(str, l, g->seed);
171 TString **list = &g->strt.hash[lmod(h, g->strt.size)];
172 lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
173 for (ts = *list; ts != NULL; ts = ts->u.hnext) {
174 if (l == ts->shrlen &&
175 (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
176 /* found! */
177 if (isdead(g, ts)) /* dead (but not collected yet)? */
178 changewhite(ts); /* resurrect it */
179 return ts;
180 }
181 }
182 if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
183 luaS_resize(L, g->strt.size * 2);
184 list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
185 }
186 ts = createstrobj(L, l, LUA_TSHRSTR, h);
187 memcpy(getstr(ts), str, l * sizeof(char));
188 ts->shrlen = cast_byte(l);
189 ts->u.hnext = *list;
190 *list = ts;
191 g->strt.nuse++;
192 return ts;
193}
#define NULL
Definition gmacros.h:924
#define changewhite(x)
#define isdead(g, v)
#define cast_byte(i)
#define lua_assert(c)
#define MAX_INT
#define lmod(s, size)
#define G(L)
#define LUA_TSHRSTR
static TString * createstrobj(lua_State *L, size_t l, int tag, unsigned int h)
void luaS_resize(lua_State *L, int newsize)
unsigned int luaS_hash(const char *str, size_t l, unsigned int seed)
union TString::@70 u
struct TString * hnext

References cast_byte, changewhite, createstrobj(), G, getstr, stringtable::hash, TString::hnext, isdead, lmod, lua_assert, LUA_TSHRSTR, luaS_hash(), luaS_resize(), MAX_INT, 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 103 of file lua-5.3.6/src/lstring.c.

103 {
104 int i, j;
105 for (i = 0; i < STRCACHE_N; i++)
106 for (j = 0; j < STRCACHE_M; j++) {
107 if (iswhite(g->strcache[i][j])) /* will entry be collected? */
108 g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
109 }
110}
#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.

Referenced by atomic(), and atomic().

◆ luaS_createlngstrobj()

TString * luaS_createlngstrobj ( lua_State * L,
size_t l )

Definition at line 147 of file lua-5.3.6/src/lstring.c.

147 {
148 TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed);
149 ts->u.lnglen = l;
150 return ts;
151}
#define LUA_TLNGSTR

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

Referenced by LoadString(), loadStringN(), luaS_newlstr(), and luaV_concat().

◆ luaS_eqlngstr()

int luaS_eqlngstr ( TString * a,
TString * b )

Definition at line 40 of file lua-5.3.6/src/lstring.c.

40 {
41 size_t len = a->u.lnglen;
42 lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
43 return (a == b) || /* same instance or... */
44 ((len == b->u.lnglen) && /* equal length and ... */
45 (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
46}

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

◆ luaS_hash()

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

Definition at line 49 of file lua-5.3.6/src/lstring.c.

49 {
50 unsigned int h = seed ^ cast(unsigned int, l);
51 size_t step = (l >> LUAI_HASHLIMIT) + 1;
52 for (; l >= step; l -= step)
53 h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
54 return h;
55}
#define cast(t, exp)
#define LUAI_HASHLIMIT

References cast, cast_byte, and LUAI_HASHLIMIT.

Referenced by internshrstr(), and luaS_hashlongstr().

◆ luaS_hashlongstr()

unsigned int luaS_hashlongstr ( TString * ts)

Definition at line 58 of file lua-5.3.6/src/lstring.c.

58 {
59 lua_assert(ts->tt == LUA_TLNGSTR);
60 if (ts->extra == 0) { /* no hash? */
61 ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash);
62 ts->extra = 1; /* now it has its hash */
63 }
64 return ts->hash;
65}

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

Referenced by mainposition(), and mainposition().

◆ luaS_init()

void luaS_init ( lua_State * L)

Definition at line 116 of file lua-5.3.6/src/lstring.c.

116 {
117 global_State *g = G(L);
118 int i, j;
119 luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
120 /* pre-create memory-error message */
122 luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
123 for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
124 for (j = 0; j < STRCACHE_M; j++)
125 g->strcache[i][j] = g->memerrmsg;
126}
#define MINSTRTABSIZE
#define obj2gco(v)
#define luaS_newliteral(L, s)
void luaC_fix(lua_State *L, GCObject *o)
#define MEMERRMSG

References G, luaC_fix(), luaS_newliteral, luaS_resize(), global_State::memerrmsg, MEMERRMSG, MINSTRTABSIZE, obj2gco, global_State::strcache, STRCACHE_M, and STRCACHE_N.

Referenced by f_luaopen(), and f_luaopen().

◆ luaS_new()

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

Definition at line 219 of file lua-5.3.6/src/lstring.c.

219 {
220 unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
221 int j;
222 TString **p = G(L)->strcache[i];
223 for (j = 0; j < STRCACHE_M; j++) {
224 if (strcmp(str, getstr(p[j])) == 0) /* hit? */
225 return p[j]; /* that is it */
226 }
227 /* normal route */
228 for (j = STRCACHE_M - 1; j > 0; j--)
229 p[j] = p[j - 1]; /* move out last element */
230 /* new element is first in the list */
231 p[0] = luaS_newlstr(L, str, strlen(str));
232 return p[0];
233}
#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 199 of file lua-5.3.6/src/lstring.c.

199 {
200 if (l <= LUAI_MAXSHORTLEN) /* short string? */
201 return internshrstr(L, str, l);
202 else {
203 TString *ts;
204 if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char))
205 luaM_toobig(L);
206 ts = luaS_createlngstrobj(L, l);
207 memcpy(getstr(ts), str, l * sizeof(char));
208 return ts;
209 }
210}
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 )

Definition at line 236 of file lua-5.3.6/src/lstring.c.

236 {
237 Udata *u;
238 GCObject *o;
239 if (s > MAX_SIZE - sizeof(Udata))
240 luaM_toobig(L);
242 u = gco2u(o);
243 u->len = s;
244 u->metatable = NULL;
246 return u;
247}
#define luaO_nilobject
#define gco2u(o)
#define LUA_TUSERDATA
#define setuservalue(L, u, o)
#define sizeludata(l)
CURL_EXTERN CURLMcode curl_socket_t s
Definition multi.h:318
struct Table * metatable

References gco2u, Udata::len, LUA_TUSERDATA, luaC_newobj(), luaM_toobig(), luaO_nilobject, MAX_SIZE, Udata::metatable, NULL, s, setuservalue, and sizeludata.

◆ luaS_remove()

void luaS_remove ( lua_State * L,
TString * ts )

Definition at line 154 of file lua-5.3.6/src/lstring.c.

154 {
155 stringtable *tb = &G(L)->strt;
156 TString **p = &tb->hash[lmod(ts->hash, tb->size)];
157 while (*p != ts) /* find previous element */
158 p = &(*p)->u.hnext;
159 *p = (*p)->u.hnext; /* remove element from its list */
160 tb->nuse--;
161}

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

Referenced by freeobj(), and freeobj().

◆ luaS_resize()

void luaS_resize ( lua_State * L,
int newsize )

Definition at line 71 of file lua-5.3.6/src/lstring.c.

71 {
72 int i;
73 stringtable *tb = &G(L)->strt;
74 if (newsize > tb->size) { /* grow table if needed */
75 luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
76 for (i = tb->size; i < newsize; i++)
77 tb->hash[i] = NULL;
78 }
79 for (i = 0; i < tb->size; i++) { /* rehash */
80 TString *p = tb->hash[i];
81 tb->hash[i] = NULL;
82 while (p) { /* for each node in the list */
83 TString *hnext = p->u.hnext; /* save next */
84 unsigned int h = lmod(p->hash, newsize); /* new position */
85 p->u.hnext = tb->hash[h]; /* chain it */
86 tb->hash[h] = p;
87 p = hnext;
88 }
89 }
90 if (newsize < tb->size) { /* shrink table if needed */
91 /* vanishing slice should be empty */
92 lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
93 luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
94 }
95 tb->size = newsize;
96}
#define luaM_reallocvector(L, v, oldn, n, t)

References G, TString::hash, stringtable::hash, TString::hnext, lmod, lua_assert, luaM_reallocvector, NULL, stringtable::size, and TString::u.

Referenced by internshrstr(), and luaS_init().