diff options
Diffstat (limited to 'lstring.c')
| -rw-r--r-- | lstring.c | 140 |
1 files changed, 88 insertions, 52 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstring.c,v 2.27 2013/06/19 14:27:00 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 2.28 2013/08/05 16:58:28 roberto Exp roberto $ |
| 3 | ** String table (keeps all strings handled by Lua) | 3 | ** String table (keeps all strings handled by Lua) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -12,12 +12,21 @@ | |||
| 12 | 12 | ||
| 13 | #include "lua.h" | 13 | #include "lua.h" |
| 14 | 14 | ||
| 15 | #include "ldebug.h" | ||
| 15 | #include "lmem.h" | 16 | #include "lmem.h" |
| 16 | #include "lobject.h" | 17 | #include "lobject.h" |
| 17 | #include "lstate.h" | 18 | #include "lstate.h" |
| 18 | #include "lstring.h" | 19 | #include "lstring.h" |
| 19 | 20 | ||
| 20 | 21 | ||
| 22 | /* mark for vacant places in hash table */ | ||
| 23 | #define VACANTK cast(TString *, cast(size_t, -1)) | ||
| 24 | |||
| 25 | |||
| 26 | /* second hash (for double hash) */ | ||
| 27 | #define h2(h1,hash,size) lmod(h1 + ((hash % 31) | 1), size) | ||
| 28 | |||
| 29 | |||
| 21 | /* | 30 | /* |
| 22 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to | 31 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to |
| 23 | ** compute its hash | 32 | ** compute its hash |
| @@ -64,30 +73,27 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { | |||
| 64 | void luaS_resize (lua_State *L, int newsize) { | 73 | void luaS_resize (lua_State *L, int newsize) { |
| 65 | int i; | 74 | int i; |
| 66 | stringtable *tb = &G(L)->strt; | 75 | stringtable *tb = &G(L)->strt; |
| 67 | /* cannot resize while GC is traversing strings */ | 76 | TString **oldhash = tb->hash; |
| 68 | luaC_runtilstate(L, ~bitmask(GCSsweepstring)); | 77 | int oldsize = tb->size; |
| 69 | if (newsize > tb->size) { | 78 | tb->hash = luaM_newvector(L, newsize, TString *); |
| 70 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); | 79 | tb->size = newsize; |
| 71 | for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL; | 80 | /* keep load factor below 75% */ |
| 72 | } | 81 | tb->empty = newsize/2 + newsize/4 - tb->nuse; |
| 82 | for (i = 0; i < newsize; i++) tb->hash[i] = NULL; | ||
| 83 | tb->nuse = 0; | ||
| 73 | /* rehash */ | 84 | /* rehash */ |
| 74 | for (i=0; i<tb->size; i++) { | 85 | for (i = 0; i < oldsize; i++) { |
| 75 | GCObject *p = tb->hash[i]; | 86 | TString *ts = oldhash[i]; |
| 76 | tb->hash[i] = NULL; | 87 | if (ts != NULL && ts != VACANTK) { |
| 77 | while (p) { /* for each node in the list */ | 88 | unsigned int hash = ts->tsv.hash; |
| 78 | GCObject *next = gch(p)->next; /* save next */ | 89 | int h1 = lmod(hash, tb->size); |
| 79 | unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */ | 90 | while (tb->hash[h1] != NULL) |
| 80 | gch(p)->next = tb->hash[h]; /* chain it */ | 91 | h1 = h2(h1, hash, tb->size); |
| 81 | tb->hash[h] = p; | 92 | tb->hash[h1] = ts; |
| 82 | p = next; | 93 | tb->nuse++; |
| 83 | } | 94 | } |
| 84 | } | 95 | } |
| 85 | if (newsize < tb->size) { | 96 | luaM_freearray(L, oldhash, oldsize); |
| 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 | } | 97 | } |
| 92 | 98 | ||
| 93 | 99 | ||
| @@ -95,11 +101,11 @@ void luaS_resize (lua_State *L, int newsize) { | |||
| 95 | ** creates a new string object | 101 | ** creates a new string object |
| 96 | */ | 102 | */ |
| 97 | static TString *createstrobj (lua_State *L, const char *str, size_t l, | 103 | static TString *createstrobj (lua_State *L, const char *str, size_t l, |
| 98 | int tag, unsigned int h, GCObject **list) { | 104 | int tag, unsigned int h) { |
| 99 | TString *ts; | 105 | TString *ts; |
| 100 | size_t totalsize; /* total size of TString object */ | 106 | size_t totalsize; /* total size of TString object */ |
| 101 | totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); | 107 | totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); |
| 102 | ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts; | 108 | ts = &luaC_newobj(L, tag, totalsize, NULL, 0)->ts; |
| 103 | ts->tsv.len = l; | 109 | ts->tsv.len = l; |
| 104 | ts->tsv.hash = h; | 110 | ts->tsv.hash = h; |
| 105 | ts->tsv.extra = 0; | 111 | ts->tsv.extra = 0; |
| @@ -109,20 +115,33 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l, | |||
| 109 | } | 115 | } |
| 110 | 116 | ||
| 111 | 117 | ||
| 112 | /* | 118 | static void rehash (lua_State *L, stringtable *tb) { |
| 113 | ** creates a new short string, inserting it into string table | 119 | int newsize; |
| 114 | */ | 120 | if (tb->nuse < tb->size / 2) { /* using less than half the size? */ |
| 115 | static TString *newshrstr (lua_State *L, const char *str, size_t l, | 121 | if (tb->nuse < tb->size / 4) /* using less than half of that? */ |
| 116 | unsigned int h) { | 122 | newsize = tb->size / 2; /* shrink table */ |
| 117 | GCObject **list; /* (pointer to) list where it will be inserted */ | 123 | else |
| 124 | newsize = tb->size; /* keep size (but reorganize table) */ | ||
| 125 | } | ||
| 126 | else { /* table must grow */ | ||
| 127 | if (tb->size >= MAX_INT/2) | ||
| 128 | luaG_runerror(L, "string-table overflow: too many strings"); | ||
| 129 | newsize = tb->size * 2; | ||
| 130 | } | ||
| 131 | luaS_resize(L, newsize); | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts) { | ||
| 118 | stringtable *tb = &G(L)->strt; | 136 | stringtable *tb = &G(L)->strt; |
| 119 | TString *s; | 137 | unsigned int hash = ts->tsv.hash; |
| 120 | if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) | 138 | int h1 = lmod(hash, tb->size); |
| 121 | luaS_resize(L, tb->size*2); /* too crowded */ | 139 | while (tb->hash[h1] != ts) { |
| 122 | list = &tb->hash[lmod(h, tb->size)]; | 140 | lua_assert(tb->hash[h1] != NULL); |
| 123 | s = createstrobj(L, str, l, LUA_TSHRSTR, h, list); | 141 | h1 = h2(h1, hash, tb->size); |
| 124 | tb->nuse++; | 142 | } |
| 125 | return s; | 143 | tb->hash[h1] = VACANTK; |
| 144 | tb->nuse--; | ||
| 126 | } | 145 | } |
| 127 | 146 | ||
| 128 | 147 | ||
| @@ -130,22 +149,39 @@ static TString *newshrstr (lua_State *L, const char *str, size_t l, | |||
| 130 | ** checks whether short string exists and reuses it or creates a new one | 149 | ** checks whether short string exists and reuses it or creates a new one |
| 131 | */ | 150 | */ |
| 132 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { | 151 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { |
| 133 | GCObject *o; | 152 | TString *ts; |
| 134 | global_State *g = G(L); | 153 | unsigned int hash = luaS_hash(str, l, G(L)->seed); |
| 135 | unsigned int h = luaS_hash(str, l, g->seed); | 154 | stringtable *tb = &G(L)->strt; |
| 136 | for (o = g->strt.hash[lmod(h, g->strt.size)]; | 155 | int vacant = -1; |
| 137 | o != NULL; | 156 | int h1; |
| 138 | o = gch(o)->next) { | 157 | if (tb->empty <= 0) |
| 139 | TString *ts = rawgco2ts(o); | 158 | rehash(L, tb); |
| 140 | if (h == ts->tsv.hash && | 159 | h1 = lmod(hash, tb->size); /* previous call can changed 'size' */ |
| 141 | l == ts->tsv.len && | 160 | while ((ts = tb->hash[h1]) != NULL) { /* search the string in hash table */ |
| 142 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { | 161 | if (ts == VACANTK) { |
| 143 | if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ | 162 | if (vacant < 0) vacant = h1; /* keep track of a vacant place */ |
| 144 | changewhite(o); /* resurrect it */ | 163 | } |
| 145 | return ts; | 164 | else if (l == ts->tsv.len && |
| 165 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { | ||
| 166 | /* found! */ | ||
| 167 | if (isdead(G(L), obj2gco(ts))) /* dead (but was not collected yet)? */ | ||
| 168 | changewhite(obj2gco(ts)); /* resurrect it */ | ||
| 169 | if (vacant >= 0) { /* is there a better place for this string? */ | ||
| 170 | tb->hash[vacant] = ts; /* move it up the line */ | ||
| 171 | tb->hash[h1] = VACANTK; | ||
| 172 | } | ||
| 173 | return ts; /* found */ | ||
| 146 | } | 174 | } |
| 175 | h1 = h2(h1, hash, tb->size); | ||
| 147 | } | 176 | } |
| 148 | return newshrstr(L, str, l, h); /* not found; create a new string */ | 177 | ts = createstrobj(L, str, l, LUA_TSHRSTR, hash); |
| 178 | tb->nuse++; | ||
| 179 | if (vacant < 0) /* found no vacant place? */ | ||
| 180 | tb->empty--; /* will have to use the empty place */ | ||
| 181 | else | ||
| 182 | h1 = vacant; /* insert string into vacant place */ | ||
| 183 | tb->hash[h1] = ts; | ||
| 184 | return ts; | ||
| 149 | } | 185 | } |
| 150 | 186 | ||
| 151 | 187 | ||
| @@ -158,7 +194,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | |||
| 158 | else { | 194 | else { |
| 159 | if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char)) | 195 | if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char)) |
| 160 | luaM_toobig(L); | 196 | luaM_toobig(L); |
| 161 | return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL); | 197 | return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed); |
| 162 | } | 198 | } |
| 163 | } | 199 | } |
| 164 | 200 | ||
