diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-26 12:02:26 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-26 12:02:26 -0300 |
commit | a580480b07cdf7201306b246deeb2fe84f2c25a9 (patch) | |
tree | 30e9d4798228156eea5be2589834f1ff2db4355e /lstring.c | |
parent | 0dd6d1080e7f58eb17cb8a2ad3fc5801ed7c0532 (diff) | |
download | lua-a580480b07cdf7201306b246deeb2fe84f2c25a9.tar.gz lua-a580480b07cdf7201306b246deeb2fe84f2c25a9.tar.bz2 lua-a580480b07cdf7201306b246deeb2fe84f2c25a9.zip |
new implementation for globals: Global value is stored in TaggedString
Diffstat (limited to 'lstring.c')
-rw-r--r-- | lstring.c | 84 |
1 files changed, 65 insertions, 19 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 1.1 1997/08/14 20:23:30 roberto Exp $ | 2 | ** $Id: lstring.c,v 1.1 1997/09/16 19:25:59 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 | */ |
@@ -15,6 +15,10 @@ | |||
15 | 15 | ||
16 | #define NUM_HASHS 61 | 16 | #define NUM_HASHS 61 |
17 | 17 | ||
18 | |||
19 | GCnode luaS_root = {NULL, 0}; /* list of global variables */ | ||
20 | |||
21 | |||
18 | typedef struct { | 22 | typedef struct { |
19 | int size; | 23 | int size; |
20 | int nuse; /* number of elements (including EMPTYs) */ | 24 | int nuse; /* number of elements (including EMPTYs) */ |
@@ -39,7 +43,7 @@ static stringtable string_root[NUM_HASHS] = { | |||
39 | }; | 43 | }; |
40 | 44 | ||
41 | 45 | ||
42 | static TaggedString EMPTY = {LUA_T_STRING, {0}, {{NOT_USED, NOT_USED}}, 2, {0}}; | 46 | static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}}; |
43 | 47 | ||
44 | 48 | ||
45 | 49 | ||
@@ -68,7 +72,7 @@ static void grow (stringtable *tb) | |||
68 | tb->nuse = 0; | 72 | tb->nuse = 0; |
69 | for (i=0; i<tb->size; i++) { | 73 | for (i=0; i<tb->size; i++) { |
70 | if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) { | 74 | if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) { |
71 | int h = tb->hash[i]->uu.hash%newsize; | 75 | int h = tb->hash[i]->hash%newsize; |
72 | while (newhash[h]) | 76 | while (newhash[h]) |
73 | h = (h+1)%newsize; | 77 | h = (h+1)%newsize; |
74 | newhash[h] = tb->hash[i]; | 78 | newhash[h] = tb->hash[i]; |
@@ -87,16 +91,18 @@ static TaggedString *newone(char *buff, int tag, unsigned long h) | |||
87 | if (tag == LUA_T_STRING) { | 91 | if (tag == LUA_T_STRING) { |
88 | ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+strlen(buff)); | 92 | ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+strlen(buff)); |
89 | strcpy(ts->str, buff); | 93 | strcpy(ts->str, buff); |
90 | ts->u.s.varindex = ts->u.s.constindex = NOT_USED; | 94 | ts->u.globalval.ttype = LUA_T_NIL; /* initialize global value */ |
91 | ts->tag = LUA_T_STRING; | 95 | ts->constindex = 0; |
92 | } | 96 | } |
93 | else { | 97 | else { |
94 | ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)); | 98 | ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)); |
95 | ts->u.v = buff; | 99 | ts->u.d.v = buff; |
96 | ts->tag = tag == LUA_ANYTAG ? 0 : tag; | 100 | ts->u.d.tag = tag == LUA_ANYTAG ? 0 : tag; |
101 | ts->constindex = -1; /* tag -> this is a userdata */ | ||
97 | } | 102 | } |
98 | ts->marked = 0; | 103 | ts->head.marked = 0; |
99 | ts->uu.hash = h; | 104 | ts->head.next = (GCnode *)ts; /* signal it is in no list */ |
105 | ts->hash = h; | ||
100 | return ts; | 106 | return ts; |
101 | } | 107 | } |
102 | 108 | ||
@@ -113,9 +119,9 @@ static TaggedString *insert (char *buff, int tag, stringtable *tb) | |||
113 | { | 119 | { |
114 | if (ts == &EMPTY) | 120 | if (ts == &EMPTY) |
115 | j = i; | 121 | j = i; |
116 | else if ((ts->tag == LUA_T_STRING) ? | 122 | else if ((ts->constindex >= 0) ? /* is a string? */ |
117 | (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) : | 123 | (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) : |
118 | ((tag == ts->tag || tag == LUA_ANYTAG) && buff == ts->u.v)) | 124 | ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v)) |
119 | return ts; | 125 | return ts; |
120 | i = (i+1)%tb->size; | 126 | i = (i+1)%tb->size; |
121 | } | 127 | } |
@@ -142,8 +148,8 @@ TaggedString *luaS_new (char *str) | |||
142 | TaggedString *luaS_newfixedstring (char *str) | 148 | TaggedString *luaS_newfixedstring (char *str) |
143 | { | 149 | { |
144 | TaggedString *ts = luaS_new(str); | 150 | TaggedString *ts = luaS_new(str); |
145 | if (ts->marked == 0) | 151 | if (ts->head.marked == 0) |
146 | ts->marked = 2; /* avoid GC */ | 152 | ts->head.marked = 2; /* avoid GC */ |
147 | return ts; | 153 | return ts; |
148 | } | 154 | } |
149 | 155 | ||
@@ -151,7 +157,7 @@ TaggedString *luaS_newfixedstring (char *str) | |||
151 | void luaS_free (TaggedString *l) | 157 | void luaS_free (TaggedString *l) |
152 | { | 158 | { |
153 | while (l) { | 159 | while (l) { |
154 | TaggedString *next = l->uu.next; | 160 | TaggedString *next = (TaggedString *)l->head.next; |
155 | luaM_free(l); | 161 | luaM_free(l); |
156 | l = next; | 162 | l = next; |
157 | } | 163 | } |
@@ -159,22 +165,35 @@ void luaS_free (TaggedString *l) | |||
159 | 165 | ||
160 | 166 | ||
161 | /* | 167 | /* |
162 | ** Garbage collection function. | 168 | ** Garbage collection functions. |
163 | */ | 169 | */ |
170 | |||
171 | static void remove_from_list (GCnode *l) | ||
172 | { | ||
173 | while (l) { | ||
174 | GCnode *next = l->next; | ||
175 | while (next && !next->marked) | ||
176 | next = l->next = next->next; | ||
177 | l = next; | ||
178 | } | ||
179 | } | ||
180 | |||
181 | |||
164 | TaggedString *luaS_collector (void) | 182 | TaggedString *luaS_collector (void) |
165 | { | 183 | { |
166 | TaggedString *frees = NULL; | 184 | TaggedString *frees = NULL; |
167 | int i; | 185 | int i; |
186 | remove_from_list(&luaS_root); | ||
168 | for (i=0; i<NUM_HASHS; i++) { | 187 | for (i=0; i<NUM_HASHS; i++) { |
169 | stringtable *tb = &string_root[i]; | 188 | stringtable *tb = &string_root[i]; |
170 | int j; | 189 | int j; |
171 | for (j=0; j<tb->size; j++) { | 190 | for (j=0; j<tb->size; j++) { |
172 | TaggedString *t = tb->hash[j]; | 191 | TaggedString *t = tb->hash[j]; |
173 | if (t == NULL) continue; | 192 | if (t == NULL) continue; |
174 | if (t->marked == 1) | 193 | if (t->head.marked == 1) |
175 | t->marked = 0; | 194 | t->head.marked = 0; |
176 | else if (!t->marked) { | 195 | else if (!t->head.marked) { |
177 | t->uu.next = frees; | 196 | t->head.next = (GCnode *)frees; |
178 | frees = t; | 197 | frees = t; |
179 | tb->hash[j] = &EMPTY; | 198 | tb->hash[j] = &EMPTY; |
180 | --luaO_nentities; | 199 | --luaO_nentities; |
@@ -184,3 +203,30 @@ TaggedString *luaS_collector (void) | |||
184 | return frees; | 203 | return frees; |
185 | } | 204 | } |
186 | 205 | ||
206 | |||
207 | void luaS_rawsetglobal (TaggedString *ts, TObject *newval) | ||
208 | { | ||
209 | ts->u.globalval = *newval; | ||
210 | if (ts->head.next == (GCnode *)ts) { /* is not in list? */ | ||
211 | ts->head.next = luaS_root.next; | ||
212 | luaS_root.next = (GCnode *)ts; | ||
213 | } | ||
214 | } | ||
215 | |||
216 | |||
217 | char *luaS_travsymbol (int (*fn)(TObject *)) | ||
218 | { | ||
219 | TaggedString *g; | ||
220 | for (g=(TaggedString *)luaS_root.next; g; g=(TaggedString *)g->head.next) | ||
221 | if (fn(&g->u.globalval)) | ||
222 | return g->str; | ||
223 | return NULL; | ||
224 | } | ||
225 | |||
226 | |||
227 | int luaS_globaldefined (char *name) | ||
228 | { | ||
229 | TaggedString *ts = luaS_new(name); | ||
230 | return ts->u.globalval.ttype != LUA_T_NIL; | ||
231 | } | ||
232 | |||