History

Lua 5.3.0-work2

Lua Team committed 12 years ago

Commit ad386d91 parent b83a40a60 files · +3280 −2672Browse files

Showing the changes to src/lstring.c (1 of 60 files). Show all

Unified · Split

src/lstring.c+46−55
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstring.c,v 2.27 2013/06/19 14:27:00 roberto Exp $
2+** $Id: lstring.c,v 2.38 2014/03/19 18:51:42 roberto Exp $
33 ** String table (keeps all strings handled by Lua)
44 ** See Copyright Notice in lua.h
55 */
@@ -12,12 +12,15 @@ · 6 unmodified lines
1212
1313 #include "lua.h"
1414
15+#include "ldebug.h"
16+#include "ldo.h"
1517 #include "lmem.h"
1618 #include "lobject.h"
1719 #include "lstate.h"
1820 #include "lstring.h"
1921
2022
23+
2124 /*
2225 ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
2326 ** compute its hash
@@ -39,15 +42,6 @@ int luaS_eqlngstr (TString *a, TString *b) { · 15 unmodified lines
3942 }
4043
4144
42−/*
43−** equality for strings
44−*/
45−int luaS_eqstr (TString *a, TString *b) {
46− return (a->tsv.tt == b->tsv.tt) &&
47− (a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b));
48−}
49−
50−
5145 unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
5246 unsigned int h = seed ^ cast(unsigned int, l);
5347 size_t l1;
@@ -64,43 +58,41 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { · 10 unmodified lines
6458 void luaS_resize (lua_State *L, int newsize) {
6559 int i;
6660 stringtable *tb = &G(L)->strt;
67− /* cannot resize while GC is traversing strings */
68− luaC_runtilstate(L, ~bitmask(GCSsweepstring));
69− if (newsize > tb->size) {
70− luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
71− for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL;
61+ if (newsize > tb->size) { /* grow table if needed */
62+ luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
63+ for (i = tb->size; i < newsize; i++)
64+ tb->hash[i] = NULL;
7265 }
73− /* rehash */
74− for (i=0; i<tb->size; i++) {
75− GCObject *p = tb->hash[i];
66+ for (i = 0; i < tb->size; i++) { /* rehash */
67+ TString *p = tb->hash[i];
7668 tb->hash[i] = NULL;
7769 while (p) { /* for each node in the list */
78− GCObject *next = gch(p)->next; /* save next */
79− unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */
80− gch(p)->next = tb->hash[h]; /* chain it */
70+ TString *hnext = p->tsv.hnext; /* save next */
71+ unsigned int h = lmod(p->tsv.hash, newsize); /* new position */
72+ p->tsv.hnext = tb->hash[h]; /* chain it */
8173 tb->hash[h] = p;
82− resetoldbit(p); /* see MOVE OLD rule */
83− p = next;
74+ p = hnext;
8475 }
8576 }
86− if (newsize < tb->size) {
87− /* shrinking slice must be empty */
77+ if (newsize < tb->size) { /* shrink table if needed */
78+ /* vanishing slice should be empty */
8879 lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
89− luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
80+ luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
9081 }
9182 tb->size = newsize;
9283 }
9384
9485
86+
9587 /*
9688 ** creates a new string object
9789 */
9890 static TString *createstrobj (lua_State *L, const char *str, size_t l,
99− int tag, unsigned int h, GCObject **list) {
91+ int tag, unsigned int h) {
10092 TString *ts;
10193 size_t totalsize; /* total size of TString object */
10294 totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
103− ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts;
95+ ts = &luaC_newobj(L, tag, totalsize)->ts;
10496 ts->tsv.len = l;
10597 ts->tsv.hash = h;
10698 ts->tsv.extra = 0;
@@ -110,20 +102,13 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l, · 3 unmodified lines
110102 }
111103
112104
113−/*
114−** creates a new short string, inserting it into string table
115−*/
116−static TString *newshrstr (lua_State *L, const char *str, size_t l,
117− unsigned int h) {
118− GCObject **list; /* (pointer to) list where it will be inserted */
105+LUAI_FUNC void luaS_remove (lua_State *L, TString *ts) {
119106 stringtable *tb = &G(L)->strt;
120− TString *s;
121− if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
122− luaS_resize(L, tb->size*2); /* too crowded */
123− list = &tb->hash[lmod(h, tb->size)];
124− s = createstrobj(L, str, l, LUA_TSHRSTR, h, list);
125− tb->nuse++;
126− return s;
107+ TString **p = &tb->hash[lmod(ts->tsv.hash, tb->size)];
108+ while (*p != ts) /* find previous element */
109+ p = &(*p)->tsv.hnext;
110+ *p = (*p)->tsv.hnext; /* remove element from its list */
111+ tb->nuse--;
127112 }
128113
129114
@@ -131,22 +116,28 @@ static TString *newshrstr (lua_State *L, const char *str, size_t l, · 1 unmodified line
131116 ** checks whether short string exists and reuses it or creates a new one
132117 */
133118 static TString *internshrstr (lua_State *L, const char *str, size_t l) {
134− GCObject *o;
119+ TString *ts;
135120 global_State *g = G(L);
136121 unsigned int h = luaS_hash(str, l, g->seed);
137− for (o = g->strt.hash[lmod(h, g->strt.size)];
138− o != NULL;
139− o = gch(o)->next) {
140− TString *ts = rawgco2ts(o);
141− if (h == ts->tsv.hash &&
142− l == ts->tsv.len &&
122+ TString **list = &g->strt.hash[lmod(h, g->strt.size)];
123+ for (ts = *list; ts != NULL; ts = ts->tsv.hnext) {
124+ if (l == ts->tsv.len &&
143125 (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
144− if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */
145− changewhite(o); /* resurrect it */
126+ /* found! */
127+ if (isdead(g, obj2gco(ts))) /* dead (but not collected yet)? */
128+ changewhite(obj2gco(ts)); /* resurrect it */
146129 return ts;
147130 }
148131 }
149− return newshrstr(L, str, l, h); /* not found; create a new string */
132+ if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
133+ luaS_resize(L, g->strt.size * 2);
134+ list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
135+ }
136+ ts = createstrobj(L, str, l, LUA_TSHRSTR, h);
137+ ts->tsv.hnext = *list;
138+ *list = ts;
139+ g->strt.nuse++;
140+ return ts;
150141 }
151142
152143
@@ -159,7 +150,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { · 6 unmodified lines
159150 else {
160151 if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char))
161152 luaM_toobig(L);
162− return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL);
153+ return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed);
163154 }
164155 }
165156
@@ -172,14 +163,14 @@ TString *luaS_new (lua_State *L, const char *str) { · 6 unmodified lines
172163 }
173164
174165
175−Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
166+Udata *luaS_newudata (lua_State *L, size_t s) {
176167 Udata *u;
177168 if (s > MAX_SIZE - sizeof(Udata))
178169 luaM_toobig(L);
179− u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u;
170+ u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s)->u;
180171 u->uv.len = s;
181172 u->uv.metatable = NULL;
182− u->uv.env = e;
173+ setuservalue(L, u, luaO_nilobject);
183174 return u;
184175 }
185176