Showing the changes to src/lstring.c (1 of 60 files). Show all
Unified · Split
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | /* |
| 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 $ | |
| 3 | 3 | ** String table (keeps all strings handled by Lua) |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -12,12 +12,15 @@ · 6 unmodified lines | ||
| 12 | 12 | |
| 13 | 13 | #include "lua.h" |
| 14 | 14 | |
| 15 | +#include "ldebug.h" | |
| 16 | +#include "ldo.h" | |
| 15 | 17 | #include "lmem.h" |
| 16 | 18 | #include "lobject.h" |
| 17 | 19 | #include "lstate.h" |
| 18 | 20 | #include "lstring.h" |
| 19 | 21 | |
| 20 | 22 | |
| 23 | + | |
| 21 | 24 | /* |
| 22 | 25 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to |
| 23 | 26 | ** compute its hash |
| @@ -39,15 +42,6 @@ int luaS_eqlngstr (TString *a, TString *b) { · 15 unmodified lines | ||
| 39 | 42 | } |
| 40 | 43 | |
| 41 | 44 | |
| 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 | − | |
| 51 | 45 | unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { |
| 52 | 46 | unsigned int h = seed ^ cast(unsigned int, l); |
| 53 | 47 | size_t l1; |
| @@ -64,43 +58,41 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { · 10 unmodified lines | ||
| 64 | 58 | void luaS_resize (lua_State *L, int newsize) { |
| 65 | 59 | int i; |
| 66 | 60 | 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; | |
| 72 | 65 | } |
| 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]; | |
| 76 | 68 | tb->hash[i] = NULL; |
| 77 | 69 | 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 */ | |
| 81 | 73 | tb->hash[h] = p; |
| 82 | − resetoldbit(p); /* see MOVE OLD rule */ | |
| 83 | − p = next; | |
| 74 | + p = hnext; | |
| 84 | 75 | } |
| 85 | 76 | } |
| 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 */ | |
| 88 | 79 | 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 *); | |
| 90 | 81 | } |
| 91 | 82 | tb->size = newsize; |
| 92 | 83 | } |
| 93 | 84 | |
| 94 | 85 | |
| 86 | + | |
| 95 | 87 | /* |
| 96 | 88 | ** creates a new string object |
| 97 | 89 | */ |
| 98 | 90 | 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) { | |
| 100 | 92 | TString *ts; |
| 101 | 93 | size_t totalsize; /* total size of TString object */ |
| 102 | 94 | 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; | |
| 104 | 96 | ts->tsv.len = l; |
| 105 | 97 | ts->tsv.hash = h; |
| 106 | 98 | ts->tsv.extra = 0; |
| @@ -110,20 +102,13 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l, · 3 unmodified lines | ||
| 110 | 102 | } |
| 111 | 103 | |
| 112 | 104 | |
| 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) { | |
| 119 | 106 | 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--; | |
| 127 | 112 | } |
| 128 | 113 | |
| 129 | 114 | |
| @@ -131,22 +116,28 @@ static TString *newshrstr (lua_State *L, const char *str, size_t l, · 1 unmodified line | ||
| 131 | 116 | ** checks whether short string exists and reuses it or creates a new one |
| 132 | 117 | */ |
| 133 | 118 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { |
| 134 | − GCObject *o; | |
| 119 | + TString *ts; | |
| 135 | 120 | global_State *g = G(L); |
| 136 | 121 | 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 && | |
| 143 | 125 | (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 */ | |
| 146 | 129 | return ts; |
| 147 | 130 | } |
| 148 | 131 | } |
| 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; | |
| 150 | 141 | } |
| 151 | 142 | |
| 152 | 143 | |
| @@ -159,7 +150,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { · 6 unmodified lines | ||
| 159 | 150 | else { |
| 160 | 151 | if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char)) |
| 161 | 152 | 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); | |
| 163 | 154 | } |
| 164 | 155 | } |
| 165 | 156 | |
| @@ -172,14 +163,14 @@ TString *luaS_new (lua_State *L, const char *str) { · 6 unmodified lines | ||
| 172 | 163 | } |
| 173 | 164 | |
| 174 | 165 | |
| 175 | −Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { | |
| 166 | +Udata *luaS_newudata (lua_State *L, size_t s) { | |
| 176 | 167 | Udata *u; |
| 177 | 168 | if (s > MAX_SIZE - sizeof(Udata)) |
| 178 | 169 | 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; | |
| 180 | 171 | u->uv.len = s; |
| 181 | 172 | u->uv.metatable = NULL; |
| 182 | − u->uv.env = e; | |
| 173 | + setuservalue(L, u, luaO_nilobject); | |
| 183 | 174 | return u; |
| 184 | 175 | } |
| 185 | 176 | |