diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-10-11 14:13:42 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-10-11 14:13:42 -0200 |
| commit | c5fee7615e979e3a39af44614f82938519dedb68 (patch) | |
| tree | 831ad8d88aba1d15e6337838d11e0234bd2e96d6 | |
| parent | cca78b5c71f4def3d3d80c71f690f8380b3cb35e (diff) | |
| download | lua-c5fee7615e979e3a39af44614f82938519dedb68.tar.gz lua-c5fee7615e979e3a39af44614f82938519dedb68.tar.bz2 lua-c5fee7615e979e3a39af44614f82938519dedb68.zip | |
new implementation for string hashing, with chaining.
| -rw-r--r-- | lapi.c | 10 | ||||
| -rw-r--r-- | lbuiltin.c | 21 | ||||
| -rw-r--r-- | lgc.c | 38 | ||||
| -rw-r--r-- | llex.c | 8 | ||||
| -rw-r--r-- | lobject.h | 21 | ||||
| -rw-r--r-- | lstring.c | 132 | ||||
| -rw-r--r-- | lstring.h | 14 |
7 files changed, 117 insertions, 127 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.51 1999/10/04 17:51:04 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.52 1999/10/07 19:04:30 roberto Exp roberto $ |
| 3 | ** Lua API | 3 | ** Lua API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -393,11 +393,11 @@ TaggedString *luaA_nextvar (TaggedString *g) { | |||
| 393 | g = L->rootglobal; /* first variable */ | 393 | g = L->rootglobal; /* first variable */ |
| 394 | else { | 394 | else { |
| 395 | /* check whether name is in global var list */ | 395 | /* check whether name is in global var list */ |
| 396 | luaL_arg_check(g != g->next, 1, "variable name expected"); | 396 | luaL_arg_check(g != g->nextglobal, 1, "variable name expected"); |
| 397 | g = g->next; /* get next */ | 397 | g = g->nextglobal; /* get next */ |
| 398 | } | 398 | } |
| 399 | while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */ | 399 | while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */ |
| 400 | g = g->next; | 400 | g = g->nextglobal; |
| 401 | if (g) { | 401 | if (g) { |
| 402 | ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = g; | 402 | ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = g; |
| 403 | incr_top; | 403 | incr_top; |
| @@ -579,7 +579,7 @@ const char *lua_getobjname (lua_Object o, const char **name) { | |||
| 579 | /* try to find a name for given function */ | 579 | /* try to find a name for given function */ |
| 580 | TaggedString *g; | 580 | TaggedString *g; |
| 581 | set_normalized(L->stack.top, Address(o)); /* to be accessed by "checkfunc" */ | 581 | set_normalized(L->stack.top, Address(o)); /* to be accessed by "checkfunc" */ |
| 582 | for (g=L->rootglobal; g; g=g->next) { | 582 | for (g=L->rootglobal; g; g=g->nextglobal) { |
| 583 | if (checkfunc(&g->u.s.globalval)) { | 583 | if (checkfunc(&g->u.s.globalval)) { |
| 584 | *name = g->str; | 584 | *name = g->str; |
| 585 | return "global"; | 585 | return "global"; |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbuiltin.c,v 1.64 1999/10/04 17:51:04 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.65 1999/10/07 19:04:30 roberto Exp roberto $ |
| 3 | ** Built-in functions | 3 | ** Built-in functions |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -447,7 +447,7 @@ static void luaB_foreachvar (void) { | |||
| 447 | TObject f; /* see comment in 'foreachi' */ | 447 | TObject f; /* see comment in 'foreachi' */ |
| 448 | f = *luaA_Address(luaL_functionarg(1)); | 448 | f = *luaA_Address(luaL_functionarg(1)); |
| 449 | luaD_checkstack(4); /* for extra var name, f, var name, and globalval */ | 449 | luaD_checkstack(4); /* for extra var name, f, var name, and globalval */ |
| 450 | for (s = L->rootglobal; s; s = s->next) { | 450 | for (s = L->rootglobal; s; s = s->nextglobal) { |
| 451 | if (s->u.s.globalval.ttype != LUA_T_NIL) { | 451 | if (s->u.s.globalval.ttype != LUA_T_NIL) { |
| 452 | pushtagstring(s); /* keep (extra) s on stack to avoid GC */ | 452 | pushtagstring(s); /* keep (extra) s on stack to avoid GC */ |
| 453 | *(L->stack.top++) = f; | 453 | *(L->stack.top++) = f; |
| @@ -606,6 +606,12 @@ static void mem_query (void) { | |||
| 606 | } | 606 | } |
| 607 | 607 | ||
| 608 | 608 | ||
| 609 | static void hash_query (void) { | ||
| 610 | const TObject *o = luaA_Address(luaL_nonnullarg(1)); | ||
| 611 | lua_pushnumber(luaH_hashindex(o)); | ||
| 612 | } | ||
| 613 | |||
| 614 | |||
| 609 | static void query_strings (void) { | 615 | static void query_strings (void) { |
| 610 | int h = luaL_check_int(1) - 1; | 616 | int h = luaL_check_int(1) - 1; |
| 611 | int s = luaL_opt_int(2, 0) - 1; | 617 | int s = luaL_opt_int(2, 0) - 1; |
| @@ -617,10 +623,10 @@ static void query_strings (void) { | |||
| 617 | } | 623 | } |
| 618 | else { | 624 | else { |
| 619 | TaggedString *ts = L->string_root[h].hash[s]; | 625 | TaggedString *ts = L->string_root[h].hash[s]; |
| 620 | if (ts == NULL) lua_pushstring("<NIL>"); | 626 | for (ts = L->string_root[h].hash[s]; ts; ts = ts->nexthash) { |
| 621 | else if (ts == &luaS_EMPTY) lua_pushstring("<EMPTY>"); | 627 | if (ts->constindex == -1) lua_pushstring("<USERDATA>"); |
| 622 | else if (ts->constindex == -1) lua_pushstring("<USERDATA>"); | 628 | else lua_pushstring(ts->str); |
| 623 | else lua_pushstring(ts->str); | 629 | } |
| 624 | } | 630 | } |
| 625 | } | 631 | } |
| 626 | 632 | ||
| @@ -707,9 +713,10 @@ static void testC (void) { | |||
| 707 | static const struct luaL_reg builtin_funcs[] = { | 713 | static const struct luaL_reg builtin_funcs[] = { |
| 708 | #ifdef DEBUG | 714 | #ifdef DEBUG |
| 709 | {"extra", extra_services}, | 715 | {"extra", extra_services}, |
| 716 | {"hash", hash_query}, | ||
| 717 | {"querystr", query_strings}, | ||
| 710 | {"testC", testC}, | 718 | {"testC", testC}, |
| 711 | {"totalmem", mem_query}, | 719 | {"totalmem", mem_query}, |
| 712 | {"querystr", query_strings}, | ||
| 713 | #endif | 720 | #endif |
| 714 | {"_ALERT", luaB_alert}, | 721 | {"_ALERT", luaB_alert}, |
| 715 | {"_ERRORMESSAGE", error_message}, | 722 | {"_ERRORMESSAGE", error_message}, |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lgc.c,v 1.26 1999/09/27 18:00:25 roberto Exp roberto $ | 2 | ** $Id: lgc.c,v 1.27 1999/10/04 17:51:04 roberto Exp roberto $ |
| 3 | ** Garbage Collector | 3 | ** Garbage Collector |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -64,7 +64,7 @@ static void hashmark (Hash *h) { | |||
| 64 | 64 | ||
| 65 | static void globalmark (void) { | 65 | static void globalmark (void) { |
| 66 | TaggedString *g; | 66 | TaggedString *g; |
| 67 | for (g=L->rootglobal; g; g=g->next) { | 67 | for (g=L->rootglobal; g; g=g->nextglobal) { |
| 68 | LUA_ASSERT(g->constindex >= 0, "userdata in global list"); | 68 | LUA_ASSERT(g->constindex >= 0, "userdata in global list"); |
| 69 | if (g->u.s.globalval.ttype != LUA_T_NIL) { | 69 | if (g->u.s.globalval.ttype != LUA_T_NIL) { |
| 70 | markobject(&g->u.s.globalval); | 70 | markobject(&g->u.s.globalval); |
| @@ -161,8 +161,8 @@ static void clear_global_list (void) { | |||
| 161 | TaggedString **p = &L->rootglobal; | 161 | TaggedString **p = &L->rootglobal; |
| 162 | TaggedString *next; | 162 | TaggedString *next; |
| 163 | while ((next = *p) != NULL) { | 163 | while ((next = *p) != NULL) { |
| 164 | if (next->marked) p = &next->next; | 164 | if (next->marked) p = &next->nextglobal; |
| 165 | else *p = next->next; | 165 | else *p = next->nextglobal; |
| 166 | } | 166 | } |
| 167 | } | 167 | } |
| 168 | 168 | ||
| @@ -177,22 +177,28 @@ static void collectstring (int limit) { | |||
| 177 | int i; | 177 | int i; |
| 178 | ttype(&o) = LUA_T_USERDATA; | 178 | ttype(&o) = LUA_T_USERDATA; |
| 179 | clear_global_list(); | 179 | clear_global_list(); |
| 180 | for (i=0; i<NUM_HASHS; i++) { | 180 | for (i=0; i<NUM_HASHS; i++) { /* for each hash table */ |
| 181 | stringtable *tb = &L->string_root[i]; | 181 | stringtable *tb = &L->string_root[i]; |
| 182 | int j; | 182 | int j; |
| 183 | for (j=0; j<tb->size; j++) { | 183 | for (j=0; j<tb->size; j++) { /* for each list */ |
| 184 | TaggedString *t = tb->hash[j]; | 184 | TaggedString **p = &tb->hash[j]; |
| 185 | if (t == NULL) continue; | 185 | TaggedString *next; |
| 186 | if (t->marked < limit) { | 186 | while ((next = *p) != NULL) { |
| 187 | if (t->constindex == -1) { /* is userdata? */ | 187 | if (next->marked >= limit) { |
| 188 | tsvalue(&o) = t; | 188 | if (next->marked < FIXMARK) /* does not change FIXMARKs */ |
| 189 | luaD_gcIM(&o); | 189 | next->marked = 0; |
| 190 | p = &next->nexthash; | ||
| 191 | } | ||
| 192 | else { /* collect */ | ||
| 193 | if (next->constindex == -1) { /* is userdata? */ | ||
| 194 | tsvalue(&o) = next; | ||
| 195 | luaD_gcIM(&o); | ||
| 196 | } | ||
| 197 | *p = next->nexthash; | ||
| 198 | luaS_free(next); | ||
| 199 | tb->nuse--; | ||
| 190 | } | 200 | } |
| 191 | luaS_free(t); | ||
| 192 | tb->hash[j] = &luaS_EMPTY; | ||
| 193 | } | 201 | } |
| 194 | else if (t->marked == 1) | ||
| 195 | t->marked = 0; | ||
| 196 | } | 202 | } |
| 197 | } | 203 | } |
| 198 | } | 204 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: llex.c,v 1.39 1999/09/06 13:55:09 roberto Exp roberto $ | 2 | ** $Id: llex.c,v 1.40 1999/10/04 17:51:04 roberto Exp roberto $ |
| 3 | ** Lexical Analyzer | 3 | ** Lexical Analyzer |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -37,7 +37,7 @@ void luaX_init (void) { | |||
| 37 | int i; | 37 | int i; |
| 38 | for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) { | 38 | for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) { |
| 39 | TaggedString *ts = luaS_new(reserved[i]); | 39 | TaggedString *ts = luaS_new(reserved[i]); |
| 40 | ts->marked = FIRST_RESERVED+i; /* reserved word (always > 255) */ | 40 | ts->marked = RESERVEDMARK+i; /* reserved word */ |
| 41 | } | 41 | } |
| 42 | } | 42 | } |
| 43 | 43 | ||
| @@ -426,8 +426,8 @@ int luaX_lex (LexState *LS) { | |||
| 426 | } while (isalnum(LS->current) || LS->current == '_'); | 426 | } while (isalnum(LS->current) || LS->current == '_'); |
| 427 | save('\0'); | 427 | save('\0'); |
| 428 | ts = luaS_new(L->Mbuffer+L->Mbuffbase); | 428 | ts = luaS_new(L->Mbuffer+L->Mbuffbase); |
| 429 | if (ts->marked >= FIRST_RESERVED) | 429 | if (ts->marked >= RESERVEDMARK) /* reserved word? */ |
| 430 | return ts->marked; /* reserved word */ | 430 | return ts->marked-RESERVEDMARK+FIRST_RESERVED; |
| 431 | LS->seminfo.ts = ts; | 431 | LS->seminfo.ts = ts; |
| 432 | return NAME; | 432 | return NAME; |
| 433 | } | 433 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.h,v 1.30 1999/09/06 20:34:18 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 1.31 1999/10/04 17:51:04 roberto Exp roberto $ |
| 3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -14,13 +14,15 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | #ifdef DEBUG | 16 | #ifdef DEBUG |
| 17 | #include "lauxlib.h" | 17 | #ifdef NDEBUG |
| 18 | #define LUA_INTERNALERROR(s) \ | 18 | #undef NDEBUG |
| 19 | luaL_verror("INTERNAL ERROR - %s [%s:%d]",(s),__FILE__,__LINE__) | 19 | #endif |
| 20 | #define LUA_ASSERT(c,s) { if (!(c)) LUA_INTERNALERROR(s); } | 20 | #include <assert.h> |
| 21 | #define LUA_INTERNALERROR(s) assert(0) | ||
| 22 | #define LUA_ASSERT(c,s) assert(c) | ||
| 21 | #else | 23 | #else |
| 22 | #define LUA_INTERNALERROR(s) /* empty */ | 24 | #define LUA_INTERNALERROR(s) /* empty */ |
| 23 | #define LUA_ASSERT(c,s) /* empty */ | 25 | #define LUA_ASSERT(c,s) /* empty */ |
| 24 | #endif | 26 | #endif |
| 25 | 27 | ||
| 26 | 28 | ||
| @@ -90,8 +92,8 @@ typedef struct TObject { | |||
| 90 | */ | 92 | */ |
| 91 | 93 | ||
| 92 | typedef struct TaggedString { | 94 | typedef struct TaggedString { |
| 93 | struct TaggedString *next; | 95 | struct TaggedString *nexthash; /* chain hash table */ |
| 94 | int marked; | 96 | struct TaggedString *nextglobal; /* chain global variables */ |
| 95 | unsigned long hash; | 97 | unsigned long hash; |
| 96 | int constindex; /* hint to reuse constants (= -1 if this is a userdata) */ | 98 | int constindex; /* hint to reuse constants (= -1 if this is a userdata) */ |
| 97 | union { | 99 | union { |
| @@ -104,6 +106,7 @@ typedef struct TaggedString { | |||
| 104 | void *v; /* if this is a userdata, here is its value */ | 106 | void *v; /* if this is a userdata, here is its value */ |
| 105 | } d; | 107 | } d; |
| 106 | } u; | 108 | } u; |
| 109 | unsigned char marked; | ||
| 107 | char str[1]; /* \0 byte already reserved */ | 110 | char str[1]; /* \0 byte already reserved */ |
| 108 | } TaggedString; | 111 | } TaggedString; |
| 109 | 112 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstring.c,v 1.21 1999/09/28 12:27:06 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 1.22 1999/10/04 17:51:04 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 | */ |
| @@ -19,11 +19,6 @@ | |||
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | TaggedString luaS_EMPTY = {NULL, MAX_INT, 0L, 0, | ||
| 23 | {{{LUA_T_NIL, {NULL}}, 0L}}, {0}}; | ||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | /* | 22 | /* |
| 28 | ** to avoid hash tables with size = 0 (cannot hash with size=0), all | 23 | ** to avoid hash tables with size = 0 (cannot hash with size=0), all |
| 29 | ** hash tables are initialized with this `array'. Elements are never | 24 | ** hash tables are initialized with this `array'. Elements are never |
| @@ -48,6 +43,7 @@ void luaS_init (void) { | |||
| 48 | void luaS_freeall (void) { | 43 | void luaS_freeall (void) { |
| 49 | int i; | 44 | int i; |
| 50 | for (i=0; i<NUM_HASHS; i++) { | 45 | for (i=0; i<NUM_HASHS; i++) { |
| 46 | LUA_ASSERT(L->string_root[i].nuse==0, "non-empty string table"); | ||
| 51 | if (L->string_root[i].hash != init_hash) | 47 | if (L->string_root[i].hash != init_hash) |
| 52 | luaM_free(L->string_root[i].hash); | 48 | luaM_free(L->string_root[i].hash); |
| 53 | } | 49 | } |
| @@ -56,43 +52,27 @@ void luaS_freeall (void) { | |||
| 56 | 52 | ||
| 57 | 53 | ||
| 58 | static unsigned long hash_s (const char *s, long l) { | 54 | static unsigned long hash_s (const char *s, long l) { |
| 59 | unsigned long h = 0; /* seed */ | 55 | unsigned long h = l; /* seed */ |
| 60 | while (l--) | 56 | while (l--) |
| 61 | h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++)); | 57 | h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++)); |
| 62 | return h; | 58 | return h; |
| 63 | } | 59 | } |
| 64 | 60 | ||
| 65 | 61 | ||
| 66 | static int newsize (const stringtable *tb) { | ||
| 67 | int realuse = 0; | ||
| 68 | int i; | ||
| 69 | /* count how many entries are really in use */ | ||
| 70 | for (i=0; i<tb->size; i++) { | ||
| 71 | if (tb->hash[i] != NULL && tb->hash[i] != &luaS_EMPTY) | ||
| 72 | realuse++; | ||
| 73 | } | ||
| 74 | return luaO_redimension(realuse*2); | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | static void grow (stringtable *tb) { | 62 | static void grow (stringtable *tb) { |
| 79 | int ns = newsize(tb); | 63 | int ns = luaO_redimension(tb->size*2); /* new size */ |
| 80 | TaggedString **newhash = luaM_newvector(ns, TaggedString *); | 64 | TaggedString **newhash = luaM_newvector(ns, TaggedString *); |
| 81 | int i; | 65 | int i; |
| 82 | for (i=0; i<ns; i++) | 66 | for (i=0; i<ns; i++) newhash[i] = NULL; |
| 83 | newhash[i] = NULL; | ||
| 84 | /* rehash */ | 67 | /* rehash */ |
| 85 | tb->nuse = 0; | ||
| 86 | for (i=0; i<tb->size; i++) { | 68 | for (i=0; i<tb->size; i++) { |
| 87 | if (tb->hash[i] != NULL && tb->hash[i] != &luaS_EMPTY) { | 69 | TaggedString *p = tb->hash[i]; |
| 88 | unsigned long h = tb->hash[i]->hash; | 70 | while (p) { /* for each node in the list */ |
| 89 | int h1 = h%ns; | 71 | TaggedString *next = p->nexthash; /* save next */ |
| 90 | while (newhash[h1]) { | 72 | int h = p->hash%ns; /* new position */ |
| 91 | h1 += (h&(ns-2)) + 1; /* double hashing */ | 73 | p->nexthash = newhash[h]; /* chain it in new position */ |
| 92 | if (h1 >= ns) h1 -= ns; | 74 | newhash[h] = p; |
| 93 | } | 75 | p = next; |
| 94 | newhash[h1] = tb->hash[i]; | ||
| 95 | tb->nuse++; | ||
| 96 | } | 76 | } |
| 97 | } | 77 | } |
| 98 | luaM_free(tb->hash); | 78 | luaM_free(tb->hash); |
| @@ -101,91 +81,79 @@ static void grow (stringtable *tb) { | |||
| 101 | } | 81 | } |
| 102 | 82 | ||
| 103 | 83 | ||
| 84 | static TaggedString *newone (long l, unsigned long h) { | ||
| 85 | TaggedString *ts = (TaggedString *)luaM_malloc( | ||
| 86 | sizeof(TaggedString)+l*sizeof(char)); | ||
| 87 | ts->marked = 0; | ||
| 88 | ts->nexthash = NULL; | ||
| 89 | ts->nextglobal = ts; /* signal it is not in global list */ | ||
| 90 | ts->hash = h; | ||
| 91 | return ts; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 104 | static TaggedString *newone_s (const char *str, long l, unsigned long h) { | 95 | static TaggedString *newone_s (const char *str, long l, unsigned long h) { |
| 105 | TaggedString *ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+l); | 96 | TaggedString *ts = newone(l, h); |
| 106 | memcpy(ts->str, str, l); | 97 | memcpy(ts->str, str, l); |
| 107 | ts->str[l] = 0; /* ending 0 */ | 98 | ts->str[l] = 0; /* ending 0 */ |
| 108 | ts->u.s.globalval.ttype = LUA_T_NIL; /* initialize global value */ | 99 | ts->u.s.globalval.ttype = LUA_T_NIL; /* initialize global value */ |
| 109 | ts->u.s.len = l; | 100 | ts->u.s.len = l; |
| 110 | ts->constindex = 0; | 101 | ts->constindex = 0; |
| 111 | L->nblocks += gcsizestring(l); | 102 | L->nblocks += gcsizestring(l); |
| 112 | ts->marked = 0; | ||
| 113 | ts->next = ts; /* signal it is in no list */ | ||
| 114 | ts->hash = h; | ||
| 115 | return ts; | 103 | return ts; |
| 116 | } | 104 | } |
| 117 | 105 | ||
| 106 | |||
| 118 | static TaggedString *newone_u (void *buff, int tag, unsigned long h) { | 107 | static TaggedString *newone_u (void *buff, int tag, unsigned long h) { |
| 119 | TaggedString *ts = luaM_new(TaggedString); | 108 | TaggedString *ts = newone(0, h); |
| 120 | ts->u.d.v = buff; | 109 | ts->u.d.v = buff; |
| 121 | ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag; | 110 | ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag; |
| 122 | ts->constindex = -1; /* tag -> this is a userdata */ | 111 | ts->constindex = -1; /* tag -> this is a userdata */ |
| 123 | L->nblocks++; | 112 | L->nblocks++; |
| 124 | ts->marked = 0; | ||
| 125 | ts->next = ts; /* signal it is in no list */ | ||
| 126 | ts->hash = h; | ||
| 127 | return ts; | 113 | return ts; |
| 128 | } | 114 | } |
| 129 | 115 | ||
| 130 | 116 | ||
| 131 | static void newentry (stringtable *tb, TaggedString *ts, int h1) { | 117 | static void newentry (stringtable *tb, TaggedString *ts, int h) { |
| 132 | tb->nuse++; | 118 | tb->nuse++; |
| 133 | if ((long)tb->nuse*3 < (long)tb->size*2) /* still have room? */ | 119 | if (tb->nuse >= tb->size) { /* no more room? */ |
| 134 | tb->hash[h1] = ts; | ||
| 135 | else { /* must grow */ | ||
| 136 | if (tb->hash == init_hash) { /* cannot change init_hash */ | 120 | if (tb->hash == init_hash) { /* cannot change init_hash */ |
| 137 | LUA_ASSERT(h1==0, "`init_hash' has size 1"); | 121 | LUA_ASSERT(h==0, "`init_hash' has size 1"); |
| 138 | tb->hash = luaM_newvector(1, TaggedString *); /* so, `clone' it */ | 122 | tb->hash = luaM_newvector(1, TaggedString *); /* so, `clone' it */ |
| 123 | tb->hash[0] = NULL; | ||
| 139 | } | 124 | } |
| 140 | tb->hash[h1] = ts; | ||
| 141 | grow(tb); | 125 | grow(tb); |
| 126 | h = ts->hash%tb->size; /* new hash position */ | ||
| 142 | } | 127 | } |
| 128 | ts->nexthash = tb->hash[h]; /* chain new entry */ | ||
| 129 | tb->hash[h] = ts; | ||
| 143 | } | 130 | } |
| 144 | 131 | ||
| 145 | 132 | ||
| 146 | static TaggedString *insert_s (const char *str, long l, stringtable *tb) { | 133 | static TaggedString *insert_s (const char *str, long l, |
| 134 | stringtable *tb, unsigned long h) { | ||
| 135 | int h1 = h%tb->size; | ||
| 147 | TaggedString *ts; | 136 | TaggedString *ts; |
| 148 | unsigned long h = hash_s(str, l); | 137 | for (ts = tb->hash[h1]; ts; ts = ts->nexthash) |
| 149 | int size = tb->size; | 138 | if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0)) |
| 150 | int j = -1; /* last empty place found (or -1) */ | ||
| 151 | int h1 = h%size; | ||
| 152 | while ((ts = tb->hash[h1]) != NULL) { | ||
| 153 | if (ts == &luaS_EMPTY) | ||
| 154 | j = h1; | ||
| 155 | else if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0)) | ||
| 156 | return ts; | 139 | return ts; |
| 157 | h1 += (h&(size-2)) + 1; /* double hashing */ | ||
| 158 | if (h1 >= size) h1 -= size; | ||
| 159 | } | ||
| 160 | /* not found */ | 140 | /* not found */ |
| 161 | ts = newone_s(str, l, h); /* create new entry */ | 141 | ts = newone_s(str, l, h); /* create new entry */ |
| 162 | if (j != -1) /* is there an EMPTY space? */ | 142 | newentry(tb, ts, h1); /* insert it on table */ |
| 163 | tb->hash[j] = ts; /* use it for new element */ | ||
| 164 | else | ||
| 165 | newentry(tb, ts, h1); /* no EMPTY places; must use a virgin one */ | ||
| 166 | return ts; | 143 | return ts; |
| 167 | } | 144 | } |
| 168 | 145 | ||
| 169 | 146 | ||
| 170 | static TaggedString *insert_u (void *buff, int tag, stringtable *tb) { | 147 | static TaggedString *insert_u (void *buff, int tag, stringtable *tb) { |
| 171 | TaggedString *ts; | ||
| 172 | unsigned long h = (unsigned long)buff; | 148 | unsigned long h = (unsigned long)buff; |
| 173 | int size = tb->size; | 149 | int h1 = h%tb->size; |
| 174 | int j = -1; | 150 | TaggedString *ts; |
| 175 | int h1 = h%size; | 151 | for (ts = tb->hash[h1]; ts; ts = ts->nexthash) |
| 176 | while ((ts = tb->hash[h1]) != NULL) { | 152 | if ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v) |
| 177 | if (ts == &luaS_EMPTY) | ||
| 178 | j = h1; | ||
| 179 | else if ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v) | ||
| 180 | return ts; | 153 | return ts; |
| 181 | h1 += (h&(size-2)) + 1; | 154 | /* not found */ |
| 182 | if (h1 >= size) h1 -= size; | ||
| 183 | } | ||
| 184 | ts = newone_u(buff, tag, h); | 155 | ts = newone_u(buff, tag, h); |
| 185 | if (j != -1) | 156 | newentry(tb, ts, h1); |
| 186 | tb->hash[j] = ts; | ||
| 187 | else | ||
| 188 | newentry(tb, ts, h1); | ||
| 189 | return ts; | 157 | return ts; |
| 190 | } | 158 | } |
| 191 | 159 | ||
| @@ -196,8 +164,8 @@ TaggedString *luaS_createudata (void *udata, int tag) { | |||
| 196 | } | 164 | } |
| 197 | 165 | ||
| 198 | TaggedString *luaS_newlstr (const char *str, long l) { | 166 | TaggedString *luaS_newlstr (const char *str, long l) { |
| 199 | int t = (l==0) ? 0 : ((int)((unsigned char)str[0]+l))%NUM_HASHSTR; | 167 | unsigned long h = hash_s(str, l); |
| 200 | return insert_s(str, l, &L->string_root[t]); | 168 | return insert_s(str, l, &L->string_root[h%NUM_HASHSTR], h); |
| 201 | } | 169 | } |
| 202 | 170 | ||
| 203 | TaggedString *luaS_new (const char *str) { | 171 | TaggedString *luaS_new (const char *str) { |
| @@ -206,7 +174,7 @@ TaggedString *luaS_new (const char *str) { | |||
| 206 | 174 | ||
| 207 | TaggedString *luaS_newfixedstring (const char *str) { | 175 | TaggedString *luaS_newfixedstring (const char *str) { |
| 208 | TaggedString *ts = luaS_new(str); | 176 | TaggedString *ts = luaS_new(str); |
| 209 | if (ts->marked == 0) ts->marked = 2; /* avoid GC */ | 177 | if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */ |
| 210 | return ts; | 178 | return ts; |
| 211 | } | 179 | } |
| 212 | 180 | ||
| @@ -219,8 +187,8 @@ void luaS_free (TaggedString *t) { | |||
| 219 | 187 | ||
| 220 | void luaS_rawsetglobal (TaggedString *ts, const TObject *newval) { | 188 | void luaS_rawsetglobal (TaggedString *ts, const TObject *newval) { |
| 221 | ts->u.s.globalval = *newval; | 189 | ts->u.s.globalval = *newval; |
| 222 | if (ts->next == ts) { /* is not in list? */ | 190 | if (ts->nextglobal == ts) { /* is not in list? */ |
| 223 | ts->next = L->rootglobal; | 191 | ts->nextglobal = L->rootglobal; |
| 224 | L->rootglobal = ts; | 192 | L->rootglobal = ts; |
| 225 | } | 193 | } |
| 226 | } | 194 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstring.h,v 1.8 1999/08/16 20:52:00 roberto Exp roberto $ | 2 | ** $Id: lstring.h,v 1.9 1999/10/04 17:51:04 roberto Exp roberto $ |
| 3 | ** String table (keep all strings handled by Lua) | 3 | ** String table (keep all strings handled by Lua) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -11,12 +11,18 @@ | |||
| 11 | #include "lobject.h" | 11 | #include "lobject.h" |
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | #define NUM_HASHSTR 31 | 14 | #define NUM_HASHSTR 31 /* a prime not in array `dimensions' */ |
| 15 | #define NUM_HASHUDATA 31 | 15 | #define NUM_HASHUDATA 31 /* idem */ |
| 16 | #define NUM_HASHS (NUM_HASHSTR+NUM_HASHUDATA) | 16 | #define NUM_HASHS (NUM_HASHSTR+NUM_HASHUDATA) |
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | extern TaggedString luaS_EMPTY; | 19 | /* |
| 20 | ** any taggedstring with mark>=FIXMARK is never collected. | ||
| 21 | ** Marks>=RESERVEDMARK are used to identify reserved words. | ||
| 22 | */ | ||
| 23 | #define FIXMARK 2 | ||
| 24 | #define RESERVEDMARK 3 | ||
| 25 | |||
| 20 | 26 | ||
| 21 | void luaS_init (void); | 27 | void luaS_init (void); |
| 22 | TaggedString *luaS_createudata (void *udata, int tag); | 28 | TaggedString *luaS_createudata (void *udata, int tag); |
