diff options
Diffstat (limited to 'lstring.c')
-rw-r--r-- | lstring.c | 19 |
1 files changed, 11 insertions, 8 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 1.44 2000/10/26 12:47:05 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 1.45 2000/10/30 17:49:19 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 | */ |
@@ -44,8 +44,8 @@ void luaS_freeall (lua_State *L) { | |||
44 | } | 44 | } |
45 | 45 | ||
46 | 46 | ||
47 | static unsigned long hash_s (const char *s, size_t l) { | 47 | static luint32 hash_s (const char *s, size_t l) { |
48 | unsigned long h = l; /* seed */ | 48 | luint32 h = l; /* seed */ |
49 | size_t step = (l>>5)|1; /* if string is too long, don't hash all its chars */ | 49 | size_t step = (l>>5)|1; /* if string is too long, don't hash all its chars */ |
50 | for (; l>=step; l-=step) | 50 | for (; l>=step; l-=step) |
51 | h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++)); | 51 | h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++)); |
@@ -62,7 +62,7 @@ void luaS_resize (lua_State *L, stringtable *tb, int newsize) { | |||
62 | TString *p = tb->hash[i]; | 62 | TString *p = tb->hash[i]; |
63 | while (p) { /* for each node in the list */ | 63 | while (p) { /* for each node in the list */ |
64 | TString *next = p->nexthash; /* save next */ | 64 | TString *next = p->nexthash; /* save next */ |
65 | unsigned long h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value); | 65 | luint32 h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value); |
66 | int h1 = h&(newsize-1); /* new position */ | 66 | int h1 = h&(newsize-1); /* new position */ |
67 | LUA_ASSERT(h%newsize == (h&(newsize-1)), | 67 | LUA_ASSERT(h%newsize == (h&(newsize-1)), |
68 | "a&(x-1) == a%x, for x power of 2"); | 68 | "a&(x-1) == a%x, for x power of 2"); |
@@ -72,7 +72,10 @@ void luaS_resize (lua_State *L, stringtable *tb, int newsize) { | |||
72 | } | 72 | } |
73 | } | 73 | } |
74 | luaM_free(L, tb->hash); | 74 | luaM_free(L, tb->hash); |
75 | L->nblocks += (newsize - tb->size)*sizeof(TString *); | 75 | if (newsize > tb->size) /* avoid "unsigned negative" values */ |
76 | L->nblocks += (newsize - tb->size)*sizeof(TString *); | ||
77 | else | ||
78 | L->nblocks -= (tb->size - newsize)*sizeof(TString *); | ||
76 | tb->size = newsize; | 79 | tb->size = newsize; |
77 | tb->hash = newhash; | 80 | tb->hash = newhash; |
78 | } | 81 | } |
@@ -82,14 +85,14 @@ static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) { | |||
82 | ts->nexthash = tb->hash[h]; /* chain new entry */ | 85 | ts->nexthash = tb->hash[h]; /* chain new entry */ |
83 | tb->hash[h] = ts; | 86 | tb->hash[h] = ts; |
84 | tb->nuse++; | 87 | tb->nuse++; |
85 | if (tb->nuse > (lint32)tb->size && tb->size < MAX_INT/2) /* too crowded? */ | 88 | if (tb->nuse > (luint32)tb->size && tb->size < MAX_INT/2) /* too crowded? */ |
86 | luaS_resize(L, tb, tb->size*2); | 89 | luaS_resize(L, tb, tb->size*2); |
87 | } | 90 | } |
88 | 91 | ||
89 | 92 | ||
90 | 93 | ||
91 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | 94 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { |
92 | unsigned long h = hash_s(str, l); | 95 | luint32 h = hash_s(str, l); |
93 | int h1 = h & (L->strt.size-1); | 96 | int h1 = h & (L->strt.size-1); |
94 | TString *ts; | 97 | TString *ts; |
95 | for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) { | 98 | for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) { |
@@ -113,7 +116,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | |||
113 | 116 | ||
114 | TString *luaS_newudata (lua_State *L, size_t s, void *udata) { | 117 | TString *luaS_newudata (lua_State *L, size_t s, void *udata) { |
115 | union L_UTString *uts = (union L_UTString *)luaM_malloc(L, | 118 | union L_UTString *uts = (union L_UTString *)luaM_malloc(L, |
116 | (lint32)sizeof(union L_UTString)+s); | 119 | (luint32)sizeof(union L_UTString)+s); |
117 | TString *ts = &uts->ts; | 120 | TString *ts = &uts->ts; |
118 | ts->marked = 0; | 121 | ts->marked = 0; |
119 | ts->nexthash = NULL; | 122 | ts->nexthash = NULL; |