diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-09 14:28:14 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-09 14:28:14 -0300 |
commit | dd22ea4da550c3a158e0f11b928c349336184f85 (patch) | |
tree | b73e2ae0498c4efa30dee04f710fb1a349f164ed /tree.c | |
parent | 5fdcfeb353d726a9bcd6d4db5dd0b62b9b8e4b02 (diff) | |
download | lua-dd22ea4da550c3a158e0f11b928c349336184f85.tar.gz lua-dd22ea4da550c3a158e0f11b928c349336184f85.tar.bz2 lua-dd22ea4da550c3a158e0f11b928c349336184f85.zip |
new implementation for udata (again they are just void *);
new implementation for the API: most operations now do not disturb
structures lua2C and C2lua.
Diffstat (limited to 'tree.c')
-rw-r--r-- | tree.c | 61 |
1 files changed, 41 insertions, 20 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_tree="$Id: tree.c,v 1.25 1997/05/05 20:21:23 roberto Exp roberto $"; | 6 | char *rcs_tree="$Id: tree.c,v 1.26 1997/05/14 18:38:29 roberto Exp roberto $"; |
7 | 7 | ||
8 | 8 | ||
9 | #include <string.h> | 9 | #include <string.h> |
@@ -29,15 +29,21 @@ static int initialized = 0; | |||
29 | 29 | ||
30 | static stringtable string_root[NUM_HASHS]; | 30 | static stringtable string_root[NUM_HASHS]; |
31 | 31 | ||
32 | static TaggedString EMPTY = {LUA_T_STRING, NULL, 0, NOT_USED, NOT_USED, | 32 | static TaggedString EMPTY = {LUA_T_STRING, NULL, {{NOT_USED, NOT_USED}}, |
33 | 0, 2, {0}}; | 33 | 0, 2, {0}}; |
34 | 34 | ||
35 | 35 | ||
36 | static unsigned long hash (char *buff, long size) | 36 | static unsigned long hash (char *s, int tag) |
37 | { | 37 | { |
38 | unsigned long h = 0; | 38 | unsigned long h; |
39 | while (size--) | 39 | if (tag != LUA_T_STRING) |
40 | h = ((h<<5)-h)^(unsigned char)*(buff++); | 40 | h = (unsigned long)s; |
41 | else { | ||
42 | long size = strlen(s); | ||
43 | h = 0; | ||
44 | while (size--) | ||
45 | h = ((h<<5)-h)^(unsigned char)*(s++); | ||
46 | } | ||
41 | return h; | 47 | return h; |
42 | } | 48 | } |
43 | 49 | ||
@@ -86,10 +92,30 @@ static void grow (stringtable *tb) | |||
86 | tb->hash = newhash; | 92 | tb->hash = newhash; |
87 | } | 93 | } |
88 | 94 | ||
89 | static TaggedString *insert (char *buff, long size, int tag, stringtable *tb) | 95 | |
96 | static TaggedString *newone(char *buff, int tag, unsigned long h) | ||
90 | { | 97 | { |
91 | TaggedString *ts; | 98 | TaggedString *ts; |
92 | unsigned long h = hash(buff, size); | 99 | if (tag == LUA_T_STRING) { |
100 | ts = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(buff)); | ||
101 | strcpy(ts->str, buff); | ||
102 | ts->u.s.varindex = ts->u.s.constindex = NOT_USED; | ||
103 | ts->tag = LUA_T_STRING; | ||
104 | } | ||
105 | else { | ||
106 | ts = (TaggedString *)luaI_malloc(sizeof(TaggedString)); | ||
107 | ts->u.v = buff; | ||
108 | ts->tag = tag == LUA_ANYTAG ? 0 : tag; | ||
109 | } | ||
110 | ts->marked = 0; | ||
111 | ts->hash = h; | ||
112 | return ts; | ||
113 | } | ||
114 | |||
115 | static TaggedString *insert (char *buff, int tag, stringtable *tb) | ||
116 | { | ||
117 | TaggedString *ts; | ||
118 | unsigned long h = hash(buff, tag); | ||
93 | int i; | 119 | int i; |
94 | int j = -1; | 120 | int j = -1; |
95 | if ((Long)tb->nuse*3 >= (Long)tb->size*2) | 121 | if ((Long)tb->nuse*3 >= (Long)tb->size*2) |
@@ -103,8 +129,9 @@ static TaggedString *insert (char *buff, long size, int tag, stringtable *tb) | |||
103 | { | 129 | { |
104 | if (ts == &EMPTY) | 130 | if (ts == &EMPTY) |
105 | j = i; | 131 | j = i; |
106 | else if (ts->size == size && ts->tag == tag && | 132 | else if ((ts->tag == LUA_T_STRING) ? |
107 | memcmp(buff, ts->str, size) == 0) | 133 | (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) : |
134 | ((tag == ts->tag || tag == LUA_ANYTAG) && buff == ts->u.v)) | ||
108 | return ts; | 135 | return ts; |
109 | i = (i+1)%tb->size; | 136 | i = (i+1)%tb->size; |
110 | } | 137 | } |
@@ -114,24 +141,18 @@ static TaggedString *insert (char *buff, long size, int tag, stringtable *tb) | |||
114 | i = j; | 141 | i = j; |
115 | else | 142 | else |
116 | tb->nuse++; | 143 | tb->nuse++; |
117 | ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+size-1); | 144 | ts = tb->hash[i] = newone(buff, tag, h); |
118 | memcpy(ts->str, buff, size); | ||
119 | ts->tag = tag; | ||
120 | ts->size = size; | ||
121 | ts->marked = 0; | ||
122 | ts->hash = h; | ||
123 | ts->varindex = ts->constindex = NOT_USED; | ||
124 | return ts; | 145 | return ts; |
125 | } | 146 | } |
126 | 147 | ||
127 | TaggedString *luaI_createuserdata (char *buff, long size, int tag) | 148 | TaggedString *luaI_createudata (void *udata, int tag) |
128 | { | 149 | { |
129 | return insert(buff, size, tag, &string_root[(unsigned)buff[0]%NUM_HASHS]); | 150 | return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]); |
130 | } | 151 | } |
131 | 152 | ||
132 | TaggedString *lua_createstring (char *str) | 153 | TaggedString *lua_createstring (char *str) |
133 | { | 154 | { |
134 | return luaI_createuserdata(str, strlen(str)+1, LUA_T_STRING); | 155 | return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]); |
135 | } | 156 | } |
136 | 157 | ||
137 | 158 | ||