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