The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lstring.cpp
Go to the documentation of this file.
1 /*
2 ** String table (keeps all strings handled by Lua)
3 ** See Copyright Notice in lua.h
4 */
5 
6 
7 #include <string.h>
8 
9 #define lstring_c
10 #define LUA_CORE
11 
12 #include "lua.h"
13 
14 #include "lmem.h"
15 #include "lobject.h"
16 #include "lstate.h"
17 #include "lstring.h"
18 
19 
20 /*
21 ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
22 ** compute its hash
23 */
24 #if !defined(LUAI_HASHLIMIT)
25 #define LUAI_HASHLIMIT 5
26 #endif
27 
28 
29 /*
30 ** equality for long strings
31 */
33  size_t len = a->tsv.len;
34  lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR);
35  return (a == b) || /* same instance or... */
36  ((len == b->tsv.len) && /* equal length and ... */
37  (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
38 }
39 
40 
41 /*
42 ** equality for strings
43 */
45  return (a->tsv.tt == b->tsv.tt) &&
46  (a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b));
47 }
48 
49 
50 unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
51  unsigned int h = seed ^ cast(unsigned int, l);
52  size_t l1;
53  size_t step = (l >> LUAI_HASHLIMIT) + 1;
54  for (l1 = l; l1 >= step; l1 -= step)
55  h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1]));
56  return h;
57 }
58 
59 
60 /*
61 ** resizes the string table
62 */
63 void luaS_resize (lua_State *L, int newsize) {
64  int i;
65  stringtable *tb = &G(L)->strt;
66  /* cannot resize while GC is traversing strings */
68  if (newsize > tb->size) {
69  luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
70  for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL;
71  }
72  /* rehash */
73  for (i=0; i<tb->size; i++) {
74  GCObject *p = tb->hash[i];
75  tb->hash[i] = NULL;
76  while (p) { /* for each node in the list */
77  GCObject *next = gch(p)->next; /* save next */
78  unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */
79  gch(p)->next = tb->hash[h]; /* chain it */
80  tb->hash[h] = p;
81  resetoldbit(p); /* see MOVE OLD rule */
82  p = next;
83  }
84  }
85  if (newsize < tb->size) {
86  /* shrinking slice must be empty */
87  lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
88  luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
89  }
90  tb->size = newsize;
91 }
92 
93 
94 /*
95 ** creates a new string object
96 */
97 static TString *createstrobj (lua_State *L, const char *str, size_t l,
98  int tag, unsigned int h, GCObject **list) {
99  TString *ts;
100  size_t totalsize; /* total size of TString object */
101  totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
102  ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts;
103  ts->tsv.len = l;
104  ts->tsv.hash = h;
105  ts->tsv.extra = 0;
106  memcpy(ts+1, str, l*sizeof(char));
107  ((char *)(ts+1))[l] = '\0'; /* ending 0 */
108  return ts;
109 }
110 
111 
112 /*
113 ** creates a new short string, inserting it into string table
114 */
115 static TString *newshrstr (lua_State *L, const char *str, size_t l,
116  unsigned int h) {
117  GCObject **list; /* (pointer to) list where it will be inserted */
118  stringtable *tb = &G(L)->strt;
119  TString *s;
120  if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
121  luaS_resize(L, tb->size*2); /* too crowded */
122  list = &tb->hash[lmod(h, tb->size)];
123  s = createstrobj(L, str, l, LUA_TSHRSTR, h, list);
124  tb->nuse++;
125  return s;
126 }
127 
128 
129 /*
130 ** checks whether short string exists and reuses it or creates a new one
131 */
132 static TString *internshrstr (lua_State *L, const char *str, size_t l) {
133  GCObject *o;
134  global_State *g = G(L);
135  unsigned int h = luaS_hash(str, l, g->seed);
136  for (o = g->strt.hash[lmod(h, g->strt.size)];
137  o != NULL;
138  o = gch(o)->next) {
139  TString *ts = rawgco2ts(o);
140  if (h == ts->tsv.hash &&
141  l == ts->tsv.len &&
142  (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
143  if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */
144  changewhite(o); /* resurrect it */
145  return ts;
146  }
147  }
148  return newshrstr(L, str, l, h); /* not found; create a new string */
149 }
150 
151 
152 /*
153 ** new string (with explicit length)
154 */
155 TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
156  if (l <= LUAI_MAXSHORTLEN) /* short string? */
157  return internshrstr(L, str, l);
158  else {
159  if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
160  luaM_toobig(L);
161  return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL);
162  }
163 }
164 
165 
166 /*
167 ** new zero-terminated string
168 */
169 TString *luaS_new (lua_State *L, const char *str) {
170  return luaS_newlstr(L, str, strlen(str));
171 }
172 
173 
175  Udata *u;
176  if (s > MAX_SIZET - sizeof(Udata))
177  luaM_toobig(L);
178  u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u;
179  u->uv.len = s;
180  u->uv.metatable = NULL;
181  u->uv.env = e;
182  return u;
183 }
184 
unsigned int hash
Definition: lobject.h:414
#define rawgco2ts(o)
Definition: lstate.h:199
#define MAX_SIZET
Definition: llimits.h:29
#define changewhite(x)
Definition: lgc.h:113
union Udata u
Definition: lstate.h:187
Definition: lobject.h:559
unsigned int luaS_hash(const char *str, size_t l, unsigned int seed)
Definition: lstring.cpp:50
#define LUA_TUSERDATA
Definition: lua.h:84
#define GCSsweepstring
Definition: lgc.h:40
Definition: lobject.h:430
GCObject ** hash
Definition: lstate.h:59
#define LUAI_MAXSHORTLEN
Definition: luaconf.h:242
GCObject * luaC_newobj(lua_State *L, int tt, size_t sz, GCObject **list, int offset)
Definition: lgc.cpp:211
#define LUAI_HASHLIMIT
Definition: lstring.cpp:25
#define cast(t, exp)
Definition: llimits.h:92
#define cast_byte(i)
Definition: llimits.h:94
unsigned LUA_INT32 lu_int32
Definition: llimits.h:17
#define G(L)
Definition: lstate.h:178
#define h
GLboolean GLboolean g
Definition: glew.h:7319
TString * luaS_new(lua_State *L, const char *str)
Definition: lstring.cpp:169
GLdouble l
Definition: glew.h:6966
void luaC_runtilstate(lua_State *L, int statesmask)
Definition: lgc.cpp:1109
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
#define luaM_reallocvector(L, v, oldn, n, t)
Definition: lmem.h:43
#define getstr(ts)
Definition: lobject.h:421
lu_byte extra
Definition: lobject.h:413
static TString * createstrobj(lua_State *L, const char *str, size_t l, int tag, unsigned int h, GCObject **list)
Definition: lstring.cpp:97
Udata * luaS_newudata(lua_State *L, size_t s, Table *e)
Definition: lstring.cpp:174
union TString ts
Definition: lstate.h:186
union TString TString
GLenum GLsizei len
Definition: glew.h:5662
size_t len
Definition: lobject.h:436
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
#define isdead(g, v)
Definition: lgc.h:111
static TString * newshrstr(lua_State *L, const char *str, size_t l, unsigned int h)
Definition: lstring.cpp:115
GLfloat GLfloat p
Definition: glew.h:12766
int luaS_eqstr(TString *a, TString *b)
Definition: lstring.cpp:44
struct Table * env
Definition: lobject.h:435
#define lua_assert(c)
Definition: llimits.h:65
#define gch(o)
Definition: lstate.h:196
#define eqshrstr(a, b)
Definition: lstring.h:33
#define LUA_TLNGSTR
Definition: lobject.h:56
int size
Definition: lstate.h:61
#define gco2ts(o)
Definition: lstate.h:201
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
size_t i
Definition: function.cpp:1057
struct Table * metatable
Definition: lobject.h:434
#define resetoldbit(o)
Definition: lgc.h:107
struct TString::@7 tsv
#define lmod(s, size)
Definition: lobject.h:576
#define MAX_INT
Definition: llimits.h:36
struct Udata::@8 uv
#define next(ls)
Definition: llex.cpp:27
GLsizeiptr size
Definition: glew.h:1649
#define LUA_TSHRSTR
Definition: lobject.h:55
unsigned int seed
Definition: lstate.h:120
lu_int32 nuse
Definition: lstate.h:60
void luaS_resize(lua_State *L, int newsize)
Definition: lstring.cpp:63
#define bitmask(b)
Definition: lgc.h:78
static TString * internshrstr(lua_State *L, const char *str, size_t l)
Definition: lstring.cpp:132
stringtable strt
Definition: lstate.h:118
#define e
TString * luaS_newlstr(lua_State *L, const char *str, size_t l)
Definition: lstring.cpp:155
size_t len
Definition: lobject.h:415
GLdouble s
Definition: glew.h:1358
l_noret luaM_toobig(lua_State *L)
Definition: lmem.cpp:65
int luaS_eqlngstr(TString *a, TString *b)
Definition: lstring.cpp:32