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