diff options
Diffstat (limited to 'lstring.c')
-rw-r--r-- | lstring.c | 29 |
1 files changed, 14 insertions, 15 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 1.49 2001/01/10 17:41:50 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 1.50 2001/01/11 18:59:20 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,16 +19,16 @@ | |||
19 | 19 | ||
20 | 20 | ||
21 | void luaS_init (lua_State *L) { | 21 | void luaS_init (lua_State *L) { |
22 | luaS_resize(L, &L->strt, MINPOWER2); | 22 | luaS_resize(L, &G(L)->strt, MINPOWER2); |
23 | luaS_resize(L, &L->udt, MINPOWER2); | 23 | luaS_resize(L, &G(L)->udt, MINPOWER2); |
24 | } | 24 | } |
25 | 25 | ||
26 | 26 | ||
27 | void luaS_freeall (lua_State *L) { | 27 | void luaS_freeall (lua_State *L) { |
28 | LUA_ASSERT(L->strt.nuse==0, "non-empty string table"); | 28 | lua_assert(G(L)->strt.nuse==0); |
29 | luaM_freearray(L, L->strt.hash, L->strt.size, TString *); | 29 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); |
30 | LUA_ASSERT(L->udt.nuse==0, "non-empty udata table"); | 30 | lua_assert(G(L)->udt.nuse==0); |
31 | luaM_freearray(L, L->udt.hash, L->udt.size, TString *); | 31 | luaM_freearray(L, G(L)->udt.hash, G(L)->udt.size, TString *); |
32 | } | 32 | } |
33 | 33 | ||
34 | 34 | ||
@@ -41,10 +41,9 @@ void luaS_resize (lua_State *L, stringtable *tb, int newsize) { | |||
41 | TString *p = tb->hash[i]; | 41 | TString *p = tb->hash[i]; |
42 | while (p) { /* for each node in the list */ | 42 | while (p) { /* for each node in the list */ |
43 | TString *next = p->nexthash; /* save next */ | 43 | TString *next = p->nexthash; /* save next */ |
44 | luint32 h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value); | 44 | luint32 h = (tb == &G(L)->strt) ? p->u.s.hash : IntPoint(p->u.d.value); |
45 | int h1 = lmod(h, newsize); /* new position */ | 45 | int h1 = lmod(h, newsize); /* new position */ |
46 | LUA_ASSERT((int)(h%newsize) == lmod(h, newsize), | 46 | lua_assert((int)(h%newsize) == lmod(h, newsize)); |
47 | "a&(x-1) == a%x, for x power of 2"); | ||
48 | p->nexthash = newhash[h1]; /* chain it in new position */ | 47 | p->nexthash = newhash[h1]; /* chain it in new position */ |
49 | newhash[h1] = p; | 48 | newhash[h1] = p; |
50 | p = next; | 49 | p = next; |
@@ -73,7 +72,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | |||
73 | size_t l1; | 72 | size_t l1; |
74 | for (l1=l; l1>=step; l1-=step) /* compute hash */ | 73 | for (l1=l; l1>=step; l1-=step) /* compute hash */ |
75 | h = h ^ ((h<<5)+(h>>2)+(unsigned char)str[l1-1]); | 74 | h = h ^ ((h<<5)+(h>>2)+(unsigned char)str[l1-1]); |
76 | for (ts = L->strt.hash[lmod(h, L->strt.size)]; ts; ts = ts->nexthash) { | 75 | for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; ts; ts = ts->nexthash) { |
77 | if (ts->len == l && (memcmp(str, ts->str, l) == 0)) | 76 | if (ts->len == l && (memcmp(str, ts->str, l) == 0)) |
78 | return ts; | 77 | return ts; |
79 | } | 78 | } |
@@ -86,7 +85,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | |||
86 | ts->u.s.constindex = 0; | 85 | ts->u.s.constindex = 0; |
87 | memcpy(ts->str, str, l); | 86 | memcpy(ts->str, str, l); |
88 | ts->str[l] = 0; /* ending 0 */ | 87 | ts->str[l] = 0; /* ending 0 */ |
89 | newentry(L, &L->strt, ts, lmod(h, L->strt.size)); /* insert it on table */ | 88 | newentry(L, &G(L)->strt, ts, lmod(h, G(L)->strt.size)); /* insert it */ |
90 | return ts; | 89 | return ts; |
91 | } | 90 | } |
92 | 91 | ||
@@ -100,15 +99,15 @@ TString *luaS_newudata (lua_State *L, size_t s, void *udata) { | |||
100 | ts->u.d.tag = 0; | 99 | ts->u.d.tag = 0; |
101 | ts->u.d.value = (udata == NULL) ? uts+1 : udata; | 100 | ts->u.d.value = (udata == NULL) ? uts+1 : udata; |
102 | /* insert it on table */ | 101 | /* insert it on table */ |
103 | newentry(L, &L->udt, ts, lmod(IntPoint(ts->u.d.value), L->udt.size)); | 102 | newentry(L, &G(L)->udt, ts, lmod(IntPoint(ts->u.d.value), G(L)->udt.size)); |
104 | return ts; | 103 | return ts; |
105 | } | 104 | } |
106 | 105 | ||
107 | 106 | ||
108 | TString *luaS_createudata (lua_State *L, void *udata, int tag) { | 107 | TString *luaS_createudata (lua_State *L, void *udata, int tag) { |
109 | int h1 = lmod(IntPoint(udata), L->udt.size); | 108 | int h1 = lmod(IntPoint(udata), G(L)->udt.size); |
110 | TString *ts; | 109 | TString *ts; |
111 | for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) { | 110 | for (ts = G(L)->udt.hash[h1]; ts; ts = ts->nexthash) { |
112 | if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG)) | 111 | if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG)) |
113 | return ts; | 112 | return ts; |
114 | } | 113 | } |