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