250a4cf7d5271b89a912c0604d774396b8ec51d8
[bpt/guile.git] / libguile / weaks.c
1 /* Copyright (C) 1995,1996,1998,2000,2001, 2003 Free Software Foundation, Inc.
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
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
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.
40 * If you do not wish that, delete this exception notice. */
41
42
43 \f
44
45 #include "libguile/_scm.h"
46 #include "libguile/vectors.h"
47 #include "libguile/lang.h"
48 #include "libguile/hashtab.h"
49
50 #include "libguile/validate.h"
51 #include "libguile/weaks.h"
52
53 \f
54
55 /* 1. The current hash table implementation in hashtab.c uses weak alist
56 * vectors (formerly called weak hash tables) internally.
57 *
58 * 2. All hash table operations still work on alist vectors.
59 *
60 * 3. The weak vector and alist vector Scheme API is accessed through
61 * the module (ice-9 weak-vector).
62 */
63
64
65 /* {Weak Vectors}
66 */
67
68
69 /* Allocate memory for a weak vector on behalf of the caller. The allocated
70 * vector will be of the given weak vector subtype. It will contain size
71 * elements which are initialized with the 'fill' object, or, if 'fill' is
72 * undefined, with an unspecified object.
73 */
74 SCM
75 scm_i_allocate_weak_vector (scm_t_bits type, SCM size, SCM fill, const char* caller)
76 #define FUNC_NAME caller
77 {
78 size_t c_size;
79 SCM v;
80
81 c_size = scm_to_unsigned_integer (size, 0, SCM_VECTOR_MAX_LENGTH);
82
83 if (c_size > 0)
84 {
85 scm_t_bits *base;
86 size_t j;
87
88 if (SCM_UNBNDP (fill))
89 fill = SCM_UNSPECIFIED;
90
91 base = scm_gc_malloc (c_size * sizeof (scm_t_bits), "weak vector");
92 for (j = 0; j != c_size; ++j)
93 base[j] = SCM_UNPACK (fill);
94 v = scm_double_cell (SCM_MAKE_VECTOR_TAG (c_size, scm_tc7_wvect),
95 (scm_t_bits) base,
96 type,
97 SCM_UNPACK (SCM_EOL));
98 scm_remember_upto_here_1 (fill);
99 }
100 else
101 {
102 v = scm_double_cell (SCM_MAKE_VECTOR_TAG (0, scm_tc7_wvect),
103 (scm_t_bits) NULL,
104 type,
105 SCM_UNPACK (SCM_EOL));
106 }
107
108 return v;
109 }
110 #undef FUNC_NAME
111
112 SCM_DEFINE (scm_make_weak_vector, "make-weak-vector", 1, 1, 0,
113 (SCM size, SCM fill),
114 "Return a weak vector with @var{size} elements. If the optional\n"
115 "argument @var{fill} is given, all entries in the vector will be\n"
116 "set to @var{fill}. The default value for @var{fill} is the\n"
117 "empty list.")
118 #define FUNC_NAME s_scm_make_weak_vector
119 {
120 return scm_i_allocate_weak_vector (0, size, fill, FUNC_NAME);
121 }
122 #undef FUNC_NAME
123
124
125 SCM_REGISTER_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector);
126
127 SCM_DEFINE (scm_weak_vector, "weak-vector", 0, 0, 1,
128 (SCM l),
129 "@deffnx {Scheme Procedure} list->weak-vector l\n"
130 "Construct a weak vector from a list: @code{weak-vector} uses\n"
131 "the list of its arguments while @code{list->weak-vector} uses\n"
132 "its only argument @var{l} (a list) to construct a weak vector\n"
133 "the same way @code{list->vector} would.")
134 #define FUNC_NAME s_scm_weak_vector
135 {
136 SCM res;
137 SCM *data;
138 long i;
139
140 /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted
141 while the vector is being created. */
142 i = scm_ilength (l);
143 SCM_ASSERT (i >= 0, l, SCM_ARG1, FUNC_NAME);
144 res = scm_make_weak_vector (scm_from_int (i), SCM_UNSPECIFIED);
145
146 /*
147 no alloc, so this loop is safe.
148 */
149 data = SCM_WRITABLE_VELTS (res);
150 while (!SCM_NULL_OR_NIL_P (l))
151 {
152 *data++ = SCM_CAR (l);
153 l = SCM_CDR (l);
154 }
155
156 return res;
157 }
158 #undef FUNC_NAME
159
160
161 SCM_DEFINE (scm_weak_vector_p, "weak-vector?", 1, 0, 0,
162 (SCM obj),
163 "Return @code{#t} if @var{obj} is a weak vector. Note that all\n"
164 "weak hashes are also weak vectors.")
165 #define FUNC_NAME s_scm_weak_vector_p
166 {
167 return scm_from_bool (SCM_WVECTP (obj) && !SCM_IS_WHVEC (obj));
168 }
169 #undef FUNC_NAME
170
171 \f
172
173 SCM_DEFINE (scm_make_weak_key_alist_vector, "make-weak-key-alist-vector", 0, 1, 0,
174 (SCM size),
175 "@deffnx {Scheme Procedure} make-weak-value-alist-vector size\n"
176 "@deffnx {Scheme Procedure} make-doubly-weak-alist-vector size\n"
177 "Return a weak hash table with @var{size} buckets. As with any\n"
178 "hash table, choosing a good size for the table requires some\n"
179 "caution.\n"
180 "\n"
181 "You can modify weak hash tables in exactly the same way you\n"
182 "would modify regular hash tables. (@pxref{Hash Tables})")
183 #define FUNC_NAME s_scm_make_weak_key_alist_vector
184 {
185 return scm_i_allocate_weak_vector
186 (1, SCM_UNBNDP (size) ? scm_from_int (31) : size, SCM_EOL, FUNC_NAME);
187 }
188 #undef FUNC_NAME
189
190
191 SCM_DEFINE (scm_make_weak_value_alist_vector, "make-weak-value-alist-vector", 0, 1, 0,
192 (SCM size),
193 "Return a hash table with weak values with @var{size} buckets.\n"
194 "(@pxref{Hash Tables})")
195 #define FUNC_NAME s_scm_make_weak_value_alist_vector
196 {
197 return scm_i_allocate_weak_vector
198 (2, SCM_UNBNDP (size) ? scm_from_int (31) : size, SCM_EOL, FUNC_NAME);
199 }
200 #undef FUNC_NAME
201
202
203 SCM_DEFINE (scm_make_doubly_weak_alist_vector, "make-doubly-weak-alist-vector", 1, 0, 0,
204 (SCM size),
205 "Return a hash table with weak keys and values with @var{size}\n"
206 "buckets. (@pxref{Hash Tables})")
207 #define FUNC_NAME s_scm_make_doubly_weak_alist_vector
208 {
209 return scm_i_allocate_weak_vector
210 (3, SCM_UNBNDP (size) ? scm_from_int (31) : size, SCM_EOL, FUNC_NAME);
211 }
212 #undef FUNC_NAME
213
214
215 SCM_DEFINE (scm_weak_key_alist_vector_p, "weak-key-alist-vector?", 1, 0, 0,
216 (SCM obj),
217 "@deffnx {Scheme Procedure} weak-value-alist-vector? obj\n"
218 "@deffnx {Scheme Procedure} doubly-weak-alist-vector? obj\n"
219 "Return @code{#t} if @var{obj} is the specified weak hash\n"
220 "table. Note that a doubly weak hash table is neither a weak key\n"
221 "nor a weak value hash table.")
222 #define FUNC_NAME s_scm_weak_key_alist_vector_p
223 {
224 return scm_from_bool (SCM_WVECTP (obj) && SCM_IS_WHVEC (obj));
225 }
226 #undef FUNC_NAME
227
228
229 SCM_DEFINE (scm_weak_value_alist_vector_p, "weak-value-alist-vector?", 1, 0, 0,
230 (SCM obj),
231 "Return @code{#t} if @var{obj} is a weak value hash table.")
232 #define FUNC_NAME s_scm_weak_value_alist_vector_p
233 {
234 return scm_from_bool (SCM_WVECTP (obj) && SCM_IS_WHVEC_V (obj));
235 }
236 #undef FUNC_NAME
237
238
239 SCM_DEFINE (scm_doubly_weak_alist_vector_p, "doubly-weak-alist-vector?", 1, 0, 0,
240 (SCM obj),
241 "Return @code{#t} if @var{obj} is a doubly weak hash table.")
242 #define FUNC_NAME s_scm_doubly_weak_alist_vector_p
243 {
244 return scm_from_bool (SCM_WVECTP (obj) && SCM_IS_WHVEC_B (obj));
245 }
246 #undef FUNC_NAME
247
248
249 static void *
250 scm_weak_vector_gc_init (void *dummy1 SCM_UNUSED,
251 void *dummy2 SCM_UNUSED,
252 void *dummy3 SCM_UNUSED)
253 {
254 scm_weak_vectors = SCM_EOL;
255
256 return 0;
257 }
258
259
260 static void *
261 scm_mark_weak_vector_spines (void *dummy1 SCM_UNUSED,
262 void *dummy2 SCM_UNUSED,
263 void *dummy3 SCM_UNUSED)
264 {
265 SCM w;
266
267 for (w = scm_weak_vectors; !scm_is_null (w); w = SCM_WVECT_GC_CHAIN (w))
268 {
269 if (SCM_IS_WHVEC_ANY (w))
270 {
271 SCM const *ptr;
272 SCM obj;
273 long j;
274 long n;
275
276 obj = w;
277 ptr = SCM_VELTS (w);
278 n = SCM_VECTOR_LENGTH (w);
279 for (j = 0; j < n; ++j)
280 {
281 SCM alist;
282
283 alist = ptr[j];
284 while ( scm_is_pair (alist)
285 && !SCM_GC_MARK_P (alist)
286 && scm_is_pair (SCM_CAR (alist)))
287 {
288 SCM_SET_GC_MARK (alist);
289 SCM_SET_GC_MARK (SCM_CAR (alist));
290 alist = SCM_CDR (alist);
291 }
292 }
293 }
294 }
295
296 return 0;
297 }
298
299 #define UNMARKED_CELL_P(x) (SCM_NIMP(x) && !SCM_GC_MARK_P (x))
300
301 static void *
302 scm_scan_weak_vectors (void *dummy1 SCM_UNUSED,
303 void *dummy2 SCM_UNUSED,
304 void *dummy3 SCM_UNUSED)
305 {
306 SCM *ptr, w;
307 for (w = scm_weak_vectors; !scm_is_null (w); w = SCM_WVECT_GC_CHAIN (w))
308 {
309 if (!SCM_IS_WHVEC_ANY (w))
310 {
311 register long j, n;
312
313 ptr = SCM_GC_WRITABLE_VELTS (w);
314 n = SCM_VECTOR_LENGTH (w);
315 for (j = 0; j < n; ++j)
316 if (UNMARKED_CELL_P (ptr[j]))
317 ptr[j] = SCM_BOOL_F;
318 }
319 /* check if we should scan the alist vector here (hashtables
320 have their own scan function in hashtab.c). */
321 else if (!SCM_WVECT_NOSCAN_P (w))
322 {
323 SCM obj = w;
324 register long n = SCM_VECTOR_LENGTH (w);
325 register long j;
326 int weak_keys = SCM_IS_WHVEC (obj) || SCM_IS_WHVEC_B (obj);
327 int weak_values = SCM_IS_WHVEC_V (obj) || SCM_IS_WHVEC_B (obj);
328
329 ptr = SCM_GC_WRITABLE_VELTS (w);
330
331 for (j = 0; j < n; ++j)
332 {
333 SCM * fixup;
334 SCM alist;
335
336 fixup = ptr + j;
337 alist = *fixup;
338
339 while (scm_is_pair (alist)
340 && scm_is_pair (SCM_CAR (alist)))
341 {
342 SCM key;
343 SCM value;
344
345 key = SCM_CAAR (alist);
346 value = SCM_CDAR (alist);
347 if ( (weak_keys && UNMARKED_CELL_P (key))
348 || (weak_values && UNMARKED_CELL_P (value)))
349 {
350 *fixup = SCM_CDR (alist);
351 }
352 else
353 fixup = SCM_CDRLOC (alist);
354 alist = SCM_CDR (alist);
355 }
356 }
357 }
358 }
359
360 return 0;
361 }
362
363 \f
364
365 void
366 scm_weaks_prehistory ()
367 {
368 scm_c_hook_add (&scm_before_mark_c_hook, scm_weak_vector_gc_init, 0, 0);
369 scm_c_hook_add (&scm_before_sweep_c_hook, scm_mark_weak_vector_spines, 0, 0);
370 scm_c_hook_add (&scm_after_sweep_c_hook, scm_scan_weak_vectors, 0, 0);
371 }
372
373
374 SCM
375 scm_init_weaks_builtins ()
376 {
377 #include "libguile/weaks.x"
378 return SCM_UNSPECIFIED;
379 }
380
381 void
382 scm_init_weaks ()
383 {
384 scm_c_define_gsubr ("%init-weaks-builtins", 0, 0, 0,
385 scm_init_weaks_builtins);
386 }
387
388
389 /*
390 Local Variables:
391 c-file-style: "gnu"
392 End:
393 */