Commit | Line | Data |
---|---|---|
2b829bbb | 1 | /* Copyright (C) 1995,1996,1998,2000,2001, 2003, 2006 Free Software Foundation, Inc. |
0f2d19dd JB |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
92205699 MV |
15 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
16 | * Boston, MA 02110-1301 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of this library. | |
20 | * | |
21 | * The exception is that, if you link this library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking this library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by | |
31 | * Free Software Foundation as part of this library. If you copy | |
32 | * code from other releases distributed under the terms of the GPL into a copy of | |
33 | * this library, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from such code. | |
37 | * | |
38 | * If you write modifications of your own for this library, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 | 41 | |
1bbd0b84 | 42 | |
0f2d19dd | 43 | \f |
592996c9 | 44 | |
06c1d900 MV |
45 | #include <stdio.h> |
46 | ||
a0599745 MD |
47 | #include "libguile/_scm.h" |
48 | #include "libguile/vectors.h" | |
c96d76b8 | 49 | #include "libguile/lang.h" |
f59a096e | 50 | #include "libguile/hashtab.h" |
0f2d19dd | 51 | |
a0599745 MD |
52 | #include "libguile/validate.h" |
53 | #include "libguile/weaks.h" | |
0f2d19dd | 54 | |
592996c9 | 55 | \f |
0f2d19dd | 56 | |
c35738c1 MD |
57 | /* 1. The current hash table implementation in hashtab.c uses weak alist |
58 | * vectors (formerly called weak hash tables) internally. | |
59 | * | |
60 | * 2. All hash table operations still work on alist vectors. | |
61 | * | |
62 | * 3. The weak vector and alist vector Scheme API is accessed through | |
63 | * the module (ice-9 weak-vector). | |
64 | */ | |
65 | ||
66 | ||
0f2d19dd JB |
67 | /* {Weak Vectors} |
68 | */ | |
69 | ||
70 | ||
3b3b36dd | 71 | SCM_DEFINE (scm_make_weak_vector, "make-weak-vector", 1, 1, 0, |
1e6808ea | 72 | (SCM size, SCM fill), |
b380b885 | 73 | "Return a weak vector with @var{size} elements. If the optional\n" |
1e6808ea MG |
74 | "argument @var{fill} is given, all entries in the vector will be\n" |
75 | "set to @var{fill}. The default value for @var{fill} is the\n" | |
76 | "empty list.") | |
1bbd0b84 | 77 | #define FUNC_NAME s_scm_make_weak_vector |
0f2d19dd | 78 | { |
6e708ef2 | 79 | return scm_i_allocate_weak_vector (0, size, fill); |
0f2d19dd | 80 | } |
1bbd0b84 | 81 | #undef FUNC_NAME |
0f2d19dd JB |
82 | |
83 | ||
1bbd0b84 | 84 | SCM_REGISTER_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector); |
1cc91f1b | 85 | |
3b3b36dd | 86 | SCM_DEFINE (scm_weak_vector, "weak-vector", 0, 0, 1, |
1bbd0b84 | 87 | (SCM l), |
8f85c0c6 | 88 | "@deffnx {Scheme Procedure} list->weak-vector l\n" |
1e6808ea MG |
89 | "Construct a weak vector from a list: @code{weak-vector} uses\n" |
90 | "the list of its arguments while @code{list->weak-vector} uses\n" | |
91 | "its only argument @var{l} (a list) to construct a weak vector\n" | |
92 | "the same way @code{list->vector} would.") | |
1bbd0b84 | 93 | #define FUNC_NAME s_scm_weak_vector |
0f2d19dd | 94 | { |
6e708ef2 MV |
95 | scm_t_array_handle handle; |
96 | SCM res, *data; | |
c014a02e | 97 | long i; |
0f2d19dd JB |
98 | |
99 | i = scm_ilength (l); | |
1bbd0b84 | 100 | SCM_ASSERT (i >= 0, l, SCM_ARG1, FUNC_NAME); |
6e708ef2 | 101 | |
e11e83f3 | 102 | res = scm_make_weak_vector (scm_from_int (i), SCM_UNSPECIFIED); |
6e708ef2 | 103 | data = scm_vector_writable_elements (res, &handle, NULL, NULL); |
22a52da1 | 104 | |
6e708ef2 | 105 | while (scm_is_pair (l) && i > 0) |
22a52da1 DH |
106 | { |
107 | *data++ = SCM_CAR (l); | |
108 | l = SCM_CDR (l); | |
6e708ef2 | 109 | i--; |
22a52da1 DH |
110 | } |
111 | ||
c8857a4d MV |
112 | scm_array_handle_release (&handle); |
113 | ||
0f2d19dd JB |
114 | return res; |
115 | } | |
1bbd0b84 | 116 | #undef FUNC_NAME |
0f2d19dd JB |
117 | |
118 | ||
3b3b36dd | 119 | SCM_DEFINE (scm_weak_vector_p, "weak-vector?", 1, 0, 0, |
1e6808ea | 120 | (SCM obj), |
5352393c MG |
121 | "Return @code{#t} if @var{obj} is a weak vector. Note that all\n" |
122 | "weak hashes are also weak vectors.") | |
1bbd0b84 | 123 | #define FUNC_NAME s_scm_weak_vector_p |
0f2d19dd | 124 | { |
6e708ef2 | 125 | return scm_from_bool (SCM_I_WVECTP (obj) && !SCM_IS_WHVEC (obj)); |
0f2d19dd | 126 | } |
1bbd0b84 | 127 | #undef FUNC_NAME |
0f2d19dd | 128 | |
0f2d19dd JB |
129 | \f |
130 | ||
c35738c1 | 131 | SCM_DEFINE (scm_make_weak_key_alist_vector, "make-weak-key-alist-vector", 0, 1, 0, |
1e6808ea | 132 | (SCM size), |
c35738c1 MD |
133 | "@deffnx {Scheme Procedure} make-weak-value-alist-vector size\n" |
134 | "@deffnx {Scheme Procedure} make-doubly-weak-alist-vector size\n" | |
1e6808ea MG |
135 | "Return a weak hash table with @var{size} buckets. As with any\n" |
136 | "hash table, choosing a good size for the table requires some\n" | |
137 | "caution.\n" | |
138 | "\n" | |
139 | "You can modify weak hash tables in exactly the same way you\n" | |
140 | "would modify regular hash tables. (@pxref{Hash Tables})") | |
c35738c1 | 141 | #define FUNC_NAME s_scm_make_weak_key_alist_vector |
0f2d19dd | 142 | { |
c35738c1 | 143 | return scm_i_allocate_weak_vector |
6e708ef2 | 144 | (1, SCM_UNBNDP (size) ? scm_from_int (31) : size, SCM_EOL); |
0f2d19dd | 145 | } |
1bbd0b84 | 146 | #undef FUNC_NAME |
0f2d19dd JB |
147 | |
148 | ||
c35738c1 | 149 | SCM_DEFINE (scm_make_weak_value_alist_vector, "make-weak-value-alist-vector", 0, 1, 0, |
1e6808ea | 150 | (SCM size), |
e3239868 DH |
151 | "Return a hash table with weak values with @var{size} buckets.\n" |
152 | "(@pxref{Hash Tables})") | |
c35738c1 | 153 | #define FUNC_NAME s_scm_make_weak_value_alist_vector |
0f2d19dd | 154 | { |
c35738c1 | 155 | return scm_i_allocate_weak_vector |
6e708ef2 | 156 | (2, SCM_UNBNDP (size) ? scm_from_int (31) : size, SCM_EOL); |
0f2d19dd | 157 | } |
1bbd0b84 | 158 | #undef FUNC_NAME |
0f2d19dd JB |
159 | |
160 | ||
c35738c1 | 161 | SCM_DEFINE (scm_make_doubly_weak_alist_vector, "make-doubly-weak-alist-vector", 1, 0, 0, |
1e6808ea | 162 | (SCM size), |
e3239868 DH |
163 | "Return a hash table with weak keys and values with @var{size}\n" |
164 | "buckets. (@pxref{Hash Tables})") | |
c35738c1 | 165 | #define FUNC_NAME s_scm_make_doubly_weak_alist_vector |
0f2d19dd | 166 | { |
c35738c1 | 167 | return scm_i_allocate_weak_vector |
6e708ef2 | 168 | (3, SCM_UNBNDP (size) ? scm_from_int (31) : size, SCM_EOL); |
0f2d19dd | 169 | } |
1bbd0b84 | 170 | #undef FUNC_NAME |
0f2d19dd | 171 | |
592996c9 | 172 | |
c35738c1 | 173 | SCM_DEFINE (scm_weak_key_alist_vector_p, "weak-key-alist-vector?", 1, 0, 0, |
1e6808ea | 174 | (SCM obj), |
c35738c1 MD |
175 | "@deffnx {Scheme Procedure} weak-value-alist-vector? obj\n" |
176 | "@deffnx {Scheme Procedure} doubly-weak-alist-vector? obj\n" | |
5352393c MG |
177 | "Return @code{#t} if @var{obj} is the specified weak hash\n" |
178 | "table. Note that a doubly weak hash table is neither a weak key\n" | |
179 | "nor a weak value hash table.") | |
c35738c1 | 180 | #define FUNC_NAME s_scm_weak_key_alist_vector_p |
0f2d19dd | 181 | { |
6e708ef2 | 182 | return scm_from_bool (SCM_I_WVECTP (obj) && SCM_IS_WHVEC (obj)); |
0f2d19dd | 183 | } |
1bbd0b84 | 184 | #undef FUNC_NAME |
0f2d19dd JB |
185 | |
186 | ||
c35738c1 | 187 | SCM_DEFINE (scm_weak_value_alist_vector_p, "weak-value-alist-vector?", 1, 0, 0, |
1e6808ea MG |
188 | (SCM obj), |
189 | "Return @code{#t} if @var{obj} is a weak value hash table.") | |
c35738c1 | 190 | #define FUNC_NAME s_scm_weak_value_alist_vector_p |
0f2d19dd | 191 | { |
6e708ef2 | 192 | return scm_from_bool (SCM_I_WVECTP (obj) && SCM_IS_WHVEC_V (obj)); |
0f2d19dd | 193 | } |
1bbd0b84 | 194 | #undef FUNC_NAME |
0f2d19dd JB |
195 | |
196 | ||
c35738c1 | 197 | SCM_DEFINE (scm_doubly_weak_alist_vector_p, "doubly-weak-alist-vector?", 1, 0, 0, |
1e6808ea MG |
198 | (SCM obj), |
199 | "Return @code{#t} if @var{obj} is a doubly weak hash table.") | |
c35738c1 | 200 | #define FUNC_NAME s_scm_doubly_weak_alist_vector_p |
0f2d19dd | 201 | { |
6e708ef2 | 202 | return scm_from_bool (SCM_I_WVECTP (obj) && SCM_IS_WHVEC_B (obj)); |
0f2d19dd | 203 | } |
1bbd0b84 | 204 | #undef FUNC_NAME |
0f2d19dd | 205 | |
26224b3f LC |
206 | #define UNMARKED_CELL_P(x) 1 /* (SCM_NIMP(x) && !SCM_GC_MARK_P (x)) *//* |
207 | FIXME */ | |
592996c9 | 208 | |
06c1d900 | 209 | static SCM weak_vectors; |
d662820a | 210 | |
06c1d900 MV |
211 | void |
212 | scm_i_init_weak_vectors_for_gc () | |
213 | { | |
214 | weak_vectors = SCM_EOL; | |
d662820a MD |
215 | } |
216 | ||
06c1d900 MV |
217 | void |
218 | scm_i_mark_weak_vector (SCM w) | |
219 | { | |
220 | SCM_I_SET_WVECT_GC_CHAIN (w, weak_vectors); | |
221 | weak_vectors = w; | |
222 | } | |
592996c9 | 223 | |
06c1d900 MV |
224 | static int |
225 | scm_i_mark_weak_vector_non_weaks (SCM w) | |
d662820a | 226 | { |
06c1d900 | 227 | int again = 0; |
d662820a | 228 | |
06c1d900 | 229 | if (SCM_IS_WHVEC_ANY (w)) |
d662820a | 230 | { |
06c1d900 MV |
231 | SCM *ptr; |
232 | long n = SCM_I_WVECT_LENGTH (w); | |
233 | long j; | |
234 | int weak_keys = SCM_IS_WHVEC (w) || SCM_IS_WHVEC_B (w); | |
235 | int weak_values = SCM_IS_WHVEC_V (w) || SCM_IS_WHVEC_B (w); | |
236 | ||
237 | ptr = SCM_I_WVECT_GC_WVELTS (w); | |
238 | ||
239 | for (j = 0; j < n; ++j) | |
d662820a | 240 | { |
06c1d900 MV |
241 | SCM alist, slow_alist; |
242 | int slow_toggle = 0; | |
243 | ||
244 | /* We do not set the mark bits of the alist spine cells here | |
245 | since we do not want to ever create the situation where a | |
246 | marked cell references an unmarked cell (except in | |
247 | scm_gc_mark, where the referenced cells will be marked | |
248 | immediately). Thus, we can not use mark bits to stop us | |
249 | from looping indefinitely over a cyclic alist. Instead, | |
250 | we use the standard tortoise and hare trick to catch | |
251 | cycles. The fast walker does the work, and stops when it | |
252 | catches the slow walker to ensure that the whole cycle | |
253 | has been worked on. | |
254 | */ | |
255 | ||
256 | alist = slow_alist = ptr[j]; | |
257 | ||
258 | while (scm_is_pair (alist)) | |
d662820a | 259 | { |
06c1d900 | 260 | SCM elt = SCM_CAR (alist); |
d662820a | 261 | |
06c1d900 | 262 | if (UNMARKED_CELL_P (elt)) |
d662820a | 263 | { |
06c1d900 MV |
264 | if (scm_is_pair (elt)) |
265 | { | |
266 | SCM key = SCM_CAR (elt); | |
267 | SCM value = SCM_CDR (elt); | |
268 | ||
269 | if (!((weak_keys && UNMARKED_CELL_P (key)) | |
270 | || (weak_values && UNMARKED_CELL_P (value)))) | |
271 | { | |
272 | /* The item should be kept. We need to mark it | |
273 | recursively. | |
274 | */ | |
275 | scm_gc_mark (elt); | |
276 | again = 1; | |
277 | } | |
278 | } | |
279 | else | |
280 | { | |
281 | /* A non-pair cell element. This should not | |
282 | appear in a real alist, but when it does, we | |
283 | need to keep it. | |
284 | */ | |
285 | scm_gc_mark (elt); | |
286 | again = 1; | |
287 | } | |
288 | } | |
289 | ||
290 | alist = SCM_CDR (alist); | |
291 | ||
292 | if (slow_toggle && scm_is_pair (slow_alist)) | |
293 | { | |
294 | slow_alist = SCM_CDR (slow_alist); | |
295 | slow_toggle = !slow_toggle; | |
296 | if (scm_is_eq (slow_alist, alist)) | |
297 | break; | |
d662820a MD |
298 | } |
299 | } | |
06c1d900 MV |
300 | if (!scm_is_pair (alist)) |
301 | scm_gc_mark (alist); | |
d662820a MD |
302 | } |
303 | } | |
304 | ||
06c1d900 | 305 | return again; |
d662820a MD |
306 | } |
307 | ||
06c1d900 MV |
308 | int |
309 | scm_i_mark_weak_vectors_non_weaks () | |
310 | { | |
311 | int again = 0; | |
312 | SCM w = weak_vectors; | |
313 | while (!scm_is_null (w)) | |
314 | { | |
315 | if (scm_i_mark_weak_vector_non_weaks (w)) | |
316 | again = 1; | |
317 | w = SCM_I_WVECT_GC_CHAIN (w); | |
318 | } | |
319 | return again; | |
320 | } | |
592996c9 | 321 | |
06c1d900 MV |
322 | static void |
323 | scm_i_remove_weaks (SCM w) | |
d662820a | 324 | { |
26224b3f LC |
325 | return; /* FIXME */ |
326 | #if 0 | |
06c1d900 MV |
327 | SCM *ptr = SCM_I_WVECT_GC_WVELTS (w); |
328 | size_t n = SCM_I_WVECT_LENGTH (w); | |
329 | size_t i; | |
330 | ||
331 | if (!SCM_IS_WHVEC_ANY (w)) | |
d662820a | 332 | { |
06c1d900 MV |
333 | for (i = 0; i < n; ++i) |
334 | if (UNMARKED_CELL_P (ptr[i])) | |
335 | ptr[i] = SCM_BOOL_F; | |
336 | } | |
337 | else | |
338 | { | |
339 | size_t delta = 0; | |
d662820a | 340 | |
06c1d900 | 341 | for (i = 0; i < n; ++i) |
d662820a | 342 | { |
06c1d900 | 343 | SCM alist, *fixup; |
d662820a | 344 | |
06c1d900 MV |
345 | fixup = ptr + i; |
346 | alist = *fixup; | |
347 | while (scm_is_pair (alist) && !SCM_GC_MARK_P (alist)) | |
d662820a | 348 | { |
06c1d900 | 349 | if (UNMARKED_CELL_P (SCM_CAR (alist))) |
d662820a | 350 | { |
06c1d900 MV |
351 | *fixup = SCM_CDR (alist); |
352 | delta++; | |
353 | } | |
354 | else | |
355 | { | |
356 | SCM_SET_GC_MARK (alist); | |
357 | fixup = SCM_CDRLOC (alist); | |
d662820a | 358 | } |
06c1d900 | 359 | alist = *fixup; |
d662820a MD |
360 | } |
361 | } | |
06c1d900 MV |
362 | #if 0 |
363 | if (delta) | |
364 | fprintf (stderr, "vector %p, delta %d\n", w, delta); | |
365 | #endif | |
366 | SCM_I_SET_WVECT_DELTA (w, delta); | |
d662820a | 367 | } |
26224b3f | 368 | #endif |
d662820a MD |
369 | } |
370 | ||
d662820a | 371 | void |
06c1d900 | 372 | scm_i_remove_weaks_from_weak_vectors () |
d662820a | 373 | { |
06c1d900 MV |
374 | SCM w = weak_vectors; |
375 | while (!scm_is_null (w)) | |
376 | { | |
377 | scm_i_remove_weaks (w); | |
378 | w = SCM_I_WVECT_GC_CHAIN (w); | |
379 | } | |
d662820a MD |
380 | } |
381 | ||
06c1d900 | 382 | \f |
592996c9 | 383 | |
c35738c1 MD |
384 | SCM |
385 | scm_init_weaks_builtins () | |
386 | { | |
387 | #include "libguile/weaks.x" | |
388 | return SCM_UNSPECIFIED; | |
389 | } | |
390 | ||
0f2d19dd JB |
391 | void |
392 | scm_init_weaks () | |
0f2d19dd | 393 | { |
c35738c1 MD |
394 | scm_c_define_gsubr ("%init-weaks-builtins", 0, 0, 0, |
395 | scm_init_weaks_builtins); | |
0f2d19dd JB |
396 | } |
397 | ||
89e00824 ML |
398 | |
399 | /* | |
400 | Local Variables: | |
401 | c-file-style: "gnu" | |
402 | End: | |
403 | */ |