aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-11-22 16:24:50 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-11-22 16:24:50 -0200
commit514783de9d6cd538d1a51aa8979b7763132730ac (patch)
treef5d6a3637f950dade2418180c4ef08442fc474b5
parent7d1499ba88fbb9275b6e5dea6005a49ff15347dd (diff)
downloadlua-514783de9d6cd538d1a51aa8979b7763132730ac.tar.gz
lua-514783de9d6cd538d1a51aa8979b7763132730ac.tar.bz2
lua-514783de9d6cd538d1a51aa8979b7763132730ac.zip
simpler without `init_hash'
-rw-r--r--lstring.c40
1 files changed, 12 insertions, 28 deletions
diff --git a/lstring.c b/lstring.c
index e5edc12c..dcfbe718 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 1.27 1999/11/10 15:39:35 roberto Exp roberto $ 2** $Id: lstring.c,v 1.28 1999/11/22 13:12:07 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*/
@@ -22,23 +22,14 @@
22 22
23 23
24 24
25/*
26** to avoid hash tables with size = 0 (cannot hash with size=0), all
27** hash tables are initialized with this `array'. Elements are never
28** written here, because this table (with size=1) must grow to get an
29** element, and before it grows we replace it for a `real' table.
30**
31*/
32static TaggedString *init_hash[1] = {NULL};
33
34
35void luaS_init (lua_State *L) { 25void luaS_init (lua_State *L) {
36 int i; 26 int i;
37 L->string_root = luaM_newvector(L, NUM_HASHS, stringtable); 27 L->string_root = luaM_newvector(L, NUM_HASHS, stringtable);
38 for (i=0; i<NUM_HASHS; i++) { 28 for (i=0; i<NUM_HASHS; i++) {
39 L->string_root[i].size = 1; 29 L->string_root[i].size = 1;
40 L->string_root[i].nuse = 0; 30 L->string_root[i].nuse = 0;
41 L->string_root[i].hash = init_hash; 31 L->string_root[i].hash = luaM_newvector(L, 1, TaggedString *);;
32 L->string_root[i].hash[0] = NULL;
42 } 33 }
43} 34}
44 35
@@ -47,11 +38,9 @@ void luaS_freeall (lua_State *L) {
47 int i; 38 int i;
48 for (i=0; i<NUM_HASHS; i++) { 39 for (i=0; i<NUM_HASHS; i++) {
49 LUA_ASSERT(L, L->string_root[i].nuse==0, "non-empty string table"); 40 LUA_ASSERT(L, L->string_root[i].nuse==0, "non-empty string table");
50 if (L->string_root[i].hash != init_hash) 41 luaM_free(L, L->string_root[i].hash);
51 luaM_free(L, L->string_root[i].hash);
52 } 42 }
53 luaM_free(L, L->string_root); 43 luaM_free(L, L->string_root);
54 LUA_ASSERT(L, init_hash[0] == NULL, "init_hash corrupted");
55} 44}
56 45
57 46
@@ -64,7 +53,7 @@ static unsigned long hash_s (const char *s, long l) {
64 53
65 54
66void luaS_grow (lua_State *L, stringtable *tb) { 55void luaS_grow (lua_State *L, stringtable *tb) {
67 int ns = luaO_redimension(L, tb->nuse*2); /* new size */ 56 int ns = luaO_redimension(L, tb->nuse); /* new size */
68 TaggedString **newhash = luaM_newvector(L, ns, TaggedString *); 57 TaggedString **newhash = luaM_newvector(L, ns, TaggedString *);
69 int i; 58 int i;
70 for (i=0; i<ns; i++) newhash[i] = NULL; 59 for (i=0; i<ns; i++) newhash[i] = NULL;
@@ -95,7 +84,8 @@ static TaggedString *newone (lua_State *L, long l, unsigned long h) {
95} 84}
96 85
97 86
98static TaggedString *newone_s (lua_State *L, const char *str, long l, unsigned long h) { 87static TaggedString *newone_s (lua_State *L, const char *str,
88 long l, unsigned long h) {
99 TaggedString *ts = newone(L, l, h); 89 TaggedString *ts = newone(L, l, h);
100 memcpy(ts->str, str, l); 90 memcpy(ts->str, str, l);
101 ts->str[l] = 0; /* ending 0 */ 91 ts->str[l] = 0; /* ending 0 */
@@ -107,7 +97,8 @@ static TaggedString *newone_s (lua_State *L, const char *str, long l, unsigned l
107} 97}
108 98
109 99
110static TaggedString *newone_u (lua_State *L, void *buff, int tag, unsigned long h) { 100static TaggedString *newone_u (lua_State *L, void *buff,
101 int tag, unsigned long h) {
111 TaggedString *ts = newone(L, 0, h); 102 TaggedString *ts = newone(L, 0, h);
112 ts->u.d.value = buff; 103 ts->u.d.value = buff;
113 ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag; 104 ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
@@ -118,18 +109,11 @@ static TaggedString *newone_u (lua_State *L, void *buff, int tag, unsigned long
118 109
119 110
120static void newentry (lua_State *L, stringtable *tb, TaggedString *ts, int h) { 111static void newentry (lua_State *L, stringtable *tb, TaggedString *ts, int h) {
121 tb->nuse++;
122 if (tb->nuse >= tb->size) { /* no more room? */
123 if (tb->hash == init_hash) { /* cannot change init_hash */
124 LUA_ASSERT(L, h==0, "`init_hash' has size 1");
125 tb->hash = luaM_newvector(L, 1, TaggedString *); /* so, `clone' it */
126 tb->hash[0] = NULL;
127 }
128 luaS_grow(L, tb);
129 h = ts->hash%tb->size; /* new hash position */
130 }
131 ts->nexthash = tb->hash[h]; /* chain new entry */ 112 ts->nexthash = tb->hash[h]; /* chain new entry */
132 tb->hash[h] = ts; 113 tb->hash[h] = ts;
114 tb->nuse++;
115 if (tb->nuse > tb->size) /* too crowded? */
116 luaS_grow(L, tb);
133} 117}
134 118
135 119