diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-10-04 15:51:04 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-10-04 15:51:04 -0200 |
commit | 4343420d4d559a7d4cdacdbc1fd61552dcf59f04 (patch) | |
tree | 57e0bdd41e2f3a4ab70d3150022569751e3d02ad /lstring.c | |
parent | 1f7103e05d01a6a4c300a73bcfc8d9b17b2c20a4 (diff) | |
download | lua-4343420d4d559a7d4cdacdbc1fd61552dcf59f04.tar.gz lua-4343420d4d559a7d4cdacdbc1fd61552dcf59f04.tar.bz2 lua-4343420d4d559a7d4cdacdbc1fd61552dcf59f04.zip |
simplified version of `gc' tag method (only for userdata now).
Diffstat (limited to 'lstring.c')
-rw-r--r-- | lstring.c | 142 |
1 files changed, 31 insertions, 111 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 1.20 1999/08/16 20:52:00 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 1.21 1999/09/28 12:27:06 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 | */ |
@@ -14,17 +14,13 @@ | |||
14 | #include "lua.h" | 14 | #include "lua.h" |
15 | 15 | ||
16 | 16 | ||
17 | #define NUM_HASHSTR 31 | ||
18 | #define NUM_HASHUDATA 31 | ||
19 | #define NUM_HASHS (NUM_HASHSTR+NUM_HASHUDATA) | ||
20 | |||
21 | 17 | ||
22 | #define gcsizestring(l) (1+(l/64)) /* "weight" for a string with length 'l' */ | 18 | #define gcsizestring(l) (1+(l/64)) /* "weight" for a string with length 'l' */ |
23 | 19 | ||
24 | 20 | ||
25 | 21 | ||
26 | static TaggedString EMPTY = {{NULL, 2}, 0L, 0, | 22 | TaggedString luaS_EMPTY = {NULL, MAX_INT, 0L, 0, |
27 | {{{LUA_T_NIL, {NULL}}, 0L}}, {0}}; | 23 | {{{LUA_T_NIL, {NULL}}, 0L}}, {0}}; |
28 | 24 | ||
29 | 25 | ||
30 | 26 | ||
@@ -49,6 +45,16 @@ void luaS_init (void) { | |||
49 | } | 45 | } |
50 | 46 | ||
51 | 47 | ||
48 | void luaS_freeall (void) { | ||
49 | int i; | ||
50 | for (i=0; i<NUM_HASHS; i++) { | ||
51 | if (L->string_root[i].hash != init_hash) | ||
52 | luaM_free(L->string_root[i].hash); | ||
53 | } | ||
54 | luaM_free(L->string_root); | ||
55 | } | ||
56 | |||
57 | |||
52 | static unsigned long hash_s (const char *s, long l) { | 58 | static unsigned long hash_s (const char *s, long l) { |
53 | unsigned long h = 0; /* seed */ | 59 | unsigned long h = 0; /* seed */ |
54 | while (l--) | 60 | while (l--) |
@@ -57,12 +63,12 @@ static unsigned long hash_s (const char *s, long l) { | |||
57 | } | 63 | } |
58 | 64 | ||
59 | 65 | ||
60 | static int newsize (stringtable *tb) { | 66 | static int newsize (const stringtable *tb) { |
61 | int realuse = 0; | 67 | int realuse = 0; |
62 | int i; | 68 | int i; |
63 | /* count how many entries are really in use */ | 69 | /* count how many entries are really in use */ |
64 | for (i=0; i<tb->size; i++) { | 70 | for (i=0; i<tb->size; i++) { |
65 | if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) | 71 | if (tb->hash[i] != NULL && tb->hash[i] != &luaS_EMPTY) |
66 | realuse++; | 72 | realuse++; |
67 | } | 73 | } |
68 | return luaO_redimension(realuse*2); | 74 | return luaO_redimension(realuse*2); |
@@ -78,7 +84,7 @@ static void grow (stringtable *tb) { | |||
78 | /* rehash */ | 84 | /* rehash */ |
79 | tb->nuse = 0; | 85 | tb->nuse = 0; |
80 | for (i=0; i<tb->size; i++) { | 86 | for (i=0; i<tb->size; i++) { |
81 | if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) { | 87 | if (tb->hash[i] != NULL && tb->hash[i] != &luaS_EMPTY) { |
82 | unsigned long h = tb->hash[i]->hash; | 88 | unsigned long h = tb->hash[i]->hash; |
83 | int h1 = h%ns; | 89 | int h1 = h%ns; |
84 | while (newhash[h1]) { | 90 | while (newhash[h1]) { |
@@ -103,8 +109,8 @@ static TaggedString *newone_s (const char *str, long l, unsigned long h) { | |||
103 | ts->u.s.len = l; | 109 | ts->u.s.len = l; |
104 | ts->constindex = 0; | 110 | ts->constindex = 0; |
105 | L->nblocks += gcsizestring(l); | 111 | L->nblocks += gcsizestring(l); |
106 | ts->head.marked = 0; | 112 | ts->marked = 0; |
107 | ts->head.next = (GCnode *)ts; /* signal it is in no list */ | 113 | ts->next = ts; /* signal it is in no list */ |
108 | ts->hash = h; | 114 | ts->hash = h; |
109 | return ts; | 115 | return ts; |
110 | } | 116 | } |
@@ -115,8 +121,8 @@ static TaggedString *newone_u (void *buff, int tag, unsigned long h) { | |||
115 | ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag; | 121 | ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag; |
116 | ts->constindex = -1; /* tag -> this is a userdata */ | 122 | ts->constindex = -1; /* tag -> this is a userdata */ |
117 | L->nblocks++; | 123 | L->nblocks++; |
118 | ts->head.marked = 0; | 124 | ts->marked = 0; |
119 | ts->head.next = (GCnode *)ts; /* signal it is in no list */ | 125 | ts->next = ts; /* signal it is in no list */ |
120 | ts->hash = h; | 126 | ts->hash = h; |
121 | return ts; | 127 | return ts; |
122 | } | 128 | } |
@@ -144,7 +150,7 @@ static TaggedString *insert_s (const char *str, long l, stringtable *tb) { | |||
144 | int j = -1; /* last empty place found (or -1) */ | 150 | int j = -1; /* last empty place found (or -1) */ |
145 | int h1 = h%size; | 151 | int h1 = h%size; |
146 | while ((ts = tb->hash[h1]) != NULL) { | 152 | while ((ts = tb->hash[h1]) != NULL) { |
147 | if (ts == &EMPTY) | 153 | if (ts == &luaS_EMPTY) |
148 | j = h1; | 154 | j = h1; |
149 | else if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0)) | 155 | else if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0)) |
150 | return ts; | 156 | return ts; |
@@ -168,7 +174,7 @@ static TaggedString *insert_u (void *buff, int tag, stringtable *tb) { | |||
168 | int j = -1; | 174 | int j = -1; |
169 | int h1 = h%size; | 175 | int h1 = h%size; |
170 | while ((ts = tb->hash[h1]) != NULL) { | 176 | while ((ts = tb->hash[h1]) != NULL) { |
171 | if (ts == &EMPTY) | 177 | if (ts == &luaS_EMPTY) |
172 | j = h1; | 178 | j = h1; |
173 | else if ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v) | 179 | else if ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v) |
174 | return ts; | 180 | return ts; |
@@ -200,115 +206,29 @@ TaggedString *luaS_new (const char *str) { | |||
200 | 206 | ||
201 | TaggedString *luaS_newfixedstring (const char *str) { | 207 | TaggedString *luaS_newfixedstring (const char *str) { |
202 | TaggedString *ts = luaS_new(str); | 208 | TaggedString *ts = luaS_new(str); |
203 | if (ts->head.marked == 0) | 209 | if (ts->marked == 0) ts->marked = 2; /* avoid GC */ |
204 | ts->head.marked = 2; /* avoid GC */ | ||
205 | return ts; | 210 | return ts; |
206 | } | 211 | } |
207 | 212 | ||
208 | 213 | ||
209 | void luaS_free (TaggedString *l) { | 214 | void luaS_free (TaggedString *t) { |
210 | while (l) { | 215 | L->nblocks -= (t->constindex == -1) ? 1 : gcsizestring(t->u.s.len); |
211 | TaggedString *next = (TaggedString *)l->head.next; | 216 | luaM_free(t); |
212 | L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(l->u.s.len); | ||
213 | luaM_free(l); | ||
214 | l = next; | ||
215 | } | ||
216 | } | ||
217 | |||
218 | |||
219 | /* | ||
220 | ** Garbage collection functions. | ||
221 | */ | ||
222 | |||
223 | static void remove_from_list (GCnode *l) { | ||
224 | while (l) { | ||
225 | GCnode *next = l->next; | ||
226 | while (next && !next->marked) | ||
227 | next = l->next = next->next; | ||
228 | l = next; | ||
229 | } | ||
230 | } | ||
231 | |||
232 | |||
233 | TaggedString *luaS_collector (void) { | ||
234 | TaggedString *frees = NULL; | ||
235 | int i; | ||
236 | remove_from_list(&(L->rootglobal)); | ||
237 | for (i=0; i<NUM_HASHS; i++) { | ||
238 | stringtable *tb = &L->string_root[i]; | ||
239 | int j; | ||
240 | for (j=0; j<tb->size; j++) { | ||
241 | TaggedString *t = tb->hash[j]; | ||
242 | if (t == NULL) continue; | ||
243 | if (t->head.marked == 1) | ||
244 | t->head.marked = 0; | ||
245 | else if (!t->head.marked) { | ||
246 | t->head.next = (GCnode *)frees; | ||
247 | frees = t; | ||
248 | tb->hash[j] = &EMPTY; | ||
249 | } | ||
250 | } | ||
251 | } | ||
252 | return frees; | ||
253 | } | ||
254 | |||
255 | |||
256 | TaggedString *luaS_collectudata (void) { | ||
257 | TaggedString *frees = NULL; | ||
258 | int i; | ||
259 | L->rootglobal.next = NULL; /* empty list of globals */ | ||
260 | for (i=NUM_HASHSTR; i<NUM_HASHS; i++) { | ||
261 | stringtable *tb = &L->string_root[i]; | ||
262 | int j; | ||
263 | for (j=0; j<tb->size; j++) { | ||
264 | TaggedString *t = tb->hash[j]; | ||
265 | if (t == NULL || t == &EMPTY) | ||
266 | continue; | ||
267 | LUA_ASSERT(t->constindex == -1, "must be userdata"); | ||
268 | t->head.next = (GCnode *)frees; | ||
269 | frees = t; | ||
270 | tb->hash[j] = &EMPTY; | ||
271 | } | ||
272 | } | ||
273 | return frees; | ||
274 | } | ||
275 | |||
276 | |||
277 | void luaS_freeall (void) { | ||
278 | int i; | ||
279 | for (i=0; i<NUM_HASHS; i++) { | ||
280 | stringtable *tb = &L->string_root[i]; | ||
281 | int j; | ||
282 | for (j=0; j<tb->size; j++) { | ||
283 | TaggedString *t = tb->hash[j]; | ||
284 | if (t != &EMPTY) luaM_free(t); | ||
285 | } | ||
286 | if (tb->hash != init_hash) luaM_free(tb->hash); | ||
287 | } | ||
288 | luaM_free(L->string_root); | ||
289 | } | 217 | } |
290 | 218 | ||
291 | 219 | ||
292 | void luaS_rawsetglobal (TaggedString *ts, TObject *newval) { | 220 | void luaS_rawsetglobal (TaggedString *ts, const TObject *newval) { |
293 | ts->u.s.globalval = *newval; | 221 | ts->u.s.globalval = *newval; |
294 | if (ts->head.next == (GCnode *)ts) { /* is not in list? */ | 222 | if (ts->next == ts) { /* is not in list? */ |
295 | ts->head.next = L->rootglobal.next; | 223 | ts->next = L->rootglobal; |
296 | L->rootglobal.next = (GCnode *)ts; | 224 | L->rootglobal = ts; |
297 | } | 225 | } |
298 | } | 226 | } |
299 | 227 | ||
300 | 228 | ||
301 | const char *luaS_travsymbol (int (*fn)(TObject *)) { | ||
302 | TaggedString *g; | ||
303 | for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next) | ||
304 | if (fn(&g->u.s.globalval)) | ||
305 | return g->str; | ||
306 | return NULL; | ||
307 | } | ||
308 | |||
309 | |||
310 | int luaS_globaldefined (const char *name) { | 229 | int luaS_globaldefined (const char *name) { |
311 | TaggedString *ts = luaS_new(name); | 230 | TaggedString *ts = luaS_new(name); |
312 | return ts->u.s.globalval.ttype != LUA_T_NIL; | 231 | return ts->u.s.globalval.ttype != LUA_T_NIL; |
313 | } | 232 | } |
314 | 233 | ||
234 | |||