1 /* Copyright (C) 1995,1996,1998,2000,2001, 2003 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of this library.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
45 #include "libguile/_scm.h"
46 #include "libguile/vectors.h"
47 #include "libguile/lang.h"
48 #include "libguile/hashtab.h"
50 #include "libguile/validate.h"
51 #include "libguile/weaks.h"
55 /* 1. The current hash table implementation in hashtab.c uses weak alist
56 * vectors (formerly called weak hash tables) internally.
58 * 2. All hash table operations still work on alist vectors.
60 * 3. The weak vector and alist vector Scheme API is accessed through
61 * the module (ice-9 weak-vector).
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.
75 scm_i_allocate_weak_vector (scm_t_bits type
, SCM size
, SCM fill
, const char* caller
)
76 #define FUNC_NAME caller
83 SCM_ASSERT_RANGE (1, size
, SCM_INUM (size
) >= 0);
84 c_size
= SCM_INUM (size
);
91 if (SCM_UNBNDP (fill
))
92 fill
= SCM_UNSPECIFIED
;
94 SCM_ASSERT_RANGE (1, size
, c_size
<= SCM_VECTOR_MAX_LENGTH
);
95 base
= scm_gc_malloc (c_size
* sizeof (scm_t_bits
), "weak vector");
96 for (j
= 0; j
!= c_size
; ++j
)
97 base
[j
] = SCM_UNPACK (fill
);
98 v
= scm_double_cell (SCM_MAKE_VECTOR_TAG (c_size
, scm_tc7_wvect
),
101 SCM_UNPACK (SCM_EOL
));
102 scm_remember_upto_here_1 (fill
);
106 v
= scm_double_cell (SCM_MAKE_VECTOR_TAG (0, scm_tc7_wvect
),
109 SCM_UNPACK (SCM_EOL
));
114 else if (SCM_BIGP (size
))
115 SCM_OUT_OF_RANGE (1, size
);
117 SCM_WRONG_TYPE_ARG (1, size
);
121 SCM_DEFINE (scm_make_weak_vector
, "make-weak-vector", 1, 1, 0,
122 (SCM size
, SCM fill
),
123 "Return a weak vector with @var{size} elements. If the optional\n"
124 "argument @var{fill} is given, all entries in the vector will be\n"
125 "set to @var{fill}. The default value for @var{fill} is the\n"
127 #define FUNC_NAME s_scm_make_weak_vector
129 return scm_i_allocate_weak_vector (0, size
, fill
, FUNC_NAME
);
134 SCM_REGISTER_PROC(s_list_to_weak_vector
, "list->weak-vector", 1, 0, 0, scm_weak_vector
);
136 SCM_DEFINE (scm_weak_vector
, "weak-vector", 0, 0, 1,
138 "@deffnx {Scheme Procedure} list->weak-vector l\n"
139 "Construct a weak vector from a list: @code{weak-vector} uses\n"
140 "the list of its arguments while @code{list->weak-vector} uses\n"
141 "its only argument @var{l} (a list) to construct a weak vector\n"
142 "the same way @code{list->vector} would.")
143 #define FUNC_NAME s_scm_weak_vector
149 /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted
150 while the vector is being created. */
152 SCM_ASSERT (i
>= 0, l
, SCM_ARG1
, FUNC_NAME
);
153 res
= scm_make_weak_vector (SCM_MAKINUM (i
), SCM_UNSPECIFIED
);
156 no alloc, so this loop is safe.
158 data
= SCM_WRITABLE_VELTS (res
);
159 while (!SCM_NULL_OR_NIL_P (l
))
161 *data
++ = SCM_CAR (l
);
170 SCM_DEFINE (scm_weak_vector_p
, "weak-vector?", 1, 0, 0,
172 "Return @code{#t} if @var{obj} is a weak vector. Note that all\n"
173 "weak hashes are also weak vectors.")
174 #define FUNC_NAME s_scm_weak_vector_p
176 return SCM_BOOL (SCM_WVECTP (obj
) && !SCM_IS_WHVEC (obj
));
182 SCM_DEFINE (scm_make_weak_key_alist_vector
, "make-weak-key-alist-vector", 0, 1, 0,
184 "@deffnx {Scheme Procedure} make-weak-value-alist-vector size\n"
185 "@deffnx {Scheme Procedure} make-doubly-weak-alist-vector size\n"
186 "Return a weak hash table with @var{size} buckets. As with any\n"
187 "hash table, choosing a good size for the table requires some\n"
190 "You can modify weak hash tables in exactly the same way you\n"
191 "would modify regular hash tables. (@pxref{Hash Tables})")
192 #define FUNC_NAME s_scm_make_weak_key_alist_vector
194 return scm_i_allocate_weak_vector
195 (1, SCM_UNBNDP (size
) ? SCM_MAKINUM (31) : size
, SCM_EOL
, FUNC_NAME
);
200 SCM_DEFINE (scm_make_weak_value_alist_vector
, "make-weak-value-alist-vector", 0, 1, 0,
202 "Return a hash table with weak values with @var{size} buckets.\n"
203 "(@pxref{Hash Tables})")
204 #define FUNC_NAME s_scm_make_weak_value_alist_vector
206 return scm_i_allocate_weak_vector
207 (2, SCM_UNBNDP (size
) ? SCM_MAKINUM (31) : size
, SCM_EOL
, FUNC_NAME
);
212 SCM_DEFINE (scm_make_doubly_weak_alist_vector
, "make-doubly-weak-alist-vector", 1, 0, 0,
214 "Return a hash table with weak keys and values with @var{size}\n"
215 "buckets. (@pxref{Hash Tables})")
216 #define FUNC_NAME s_scm_make_doubly_weak_alist_vector
218 return scm_i_allocate_weak_vector
219 (3, SCM_UNBNDP (size
) ? SCM_MAKINUM (31) : size
, SCM_EOL
, FUNC_NAME
);
224 SCM_DEFINE (scm_weak_key_alist_vector_p
, "weak-key-alist-vector?", 1, 0, 0,
226 "@deffnx {Scheme Procedure} weak-value-alist-vector? obj\n"
227 "@deffnx {Scheme Procedure} doubly-weak-alist-vector? obj\n"
228 "Return @code{#t} if @var{obj} is the specified weak hash\n"
229 "table. Note that a doubly weak hash table is neither a weak key\n"
230 "nor a weak value hash table.")
231 #define FUNC_NAME s_scm_weak_key_alist_vector_p
233 return SCM_BOOL (SCM_WVECTP (obj
) && SCM_IS_WHVEC (obj
));
238 SCM_DEFINE (scm_weak_value_alist_vector_p
, "weak-value-alist-vector?", 1, 0, 0,
240 "Return @code{#t} if @var{obj} is a weak value hash table.")
241 #define FUNC_NAME s_scm_weak_value_alist_vector_p
243 return SCM_BOOL (SCM_WVECTP (obj
) && SCM_IS_WHVEC_V (obj
));
248 SCM_DEFINE (scm_doubly_weak_alist_vector_p
, "doubly-weak-alist-vector?", 1, 0, 0,
250 "Return @code{#t} if @var{obj} is a doubly weak hash table.")
251 #define FUNC_NAME s_scm_doubly_weak_alist_vector_p
253 return SCM_BOOL (SCM_WVECTP (obj
) && SCM_IS_WHVEC_B (obj
));
259 scm_weak_vector_gc_init (void *dummy1 SCM_UNUSED
,
260 void *dummy2 SCM_UNUSED
,
261 void *dummy3 SCM_UNUSED
)
263 scm_weak_vectors
= SCM_EOL
;
270 scm_mark_weak_vector_spines (void *dummy1 SCM_UNUSED
,
271 void *dummy2 SCM_UNUSED
,
272 void *dummy3 SCM_UNUSED
)
276 for (w
= scm_weak_vectors
; !SCM_NULLP (w
); w
= SCM_WVECT_GC_CHAIN (w
))
278 if (SCM_IS_WHVEC_ANY (w
))
287 n
= SCM_VECTOR_LENGTH (w
);
288 for (j
= 0; j
< n
; ++j
)
293 while ( SCM_CONSP (alist
)
294 && !SCM_GC_MARK_P (alist
)
295 && SCM_CONSP (SCM_CAR (alist
)))
297 SCM_SET_GC_MARK (alist
);
298 SCM_SET_GC_MARK (SCM_CAR (alist
));
299 alist
= SCM_CDR (alist
);
308 #define UNMARKED_CELL_P(x) (SCM_NIMP(x) && !SCM_GC_MARK_P (x))
311 scm_scan_weak_vectors (void *dummy1 SCM_UNUSED
,
312 void *dummy2 SCM_UNUSED
,
313 void *dummy3 SCM_UNUSED
)
316 for (w
= scm_weak_vectors
; !SCM_NULLP (w
); w
= SCM_WVECT_GC_CHAIN (w
))
318 if (!SCM_IS_WHVEC_ANY (w
))
322 ptr
= SCM_GC_WRITABLE_VELTS (w
);
323 n
= SCM_VECTOR_LENGTH (w
);
324 for (j
= 0; j
< n
; ++j
)
325 if (UNMARKED_CELL_P (ptr
[j
]))
328 /* check if we should scan the alist vector here (hashtables
329 have their own scan function in hashtab.c). */
330 else if (!SCM_WVECT_NOSCAN_P (w
))
333 register long n
= SCM_VECTOR_LENGTH (w
);
335 int weak_keys
= SCM_IS_WHVEC (obj
) || SCM_IS_WHVEC_B (obj
);
336 int weak_values
= SCM_IS_WHVEC_V (obj
) || SCM_IS_WHVEC_B (obj
);
338 ptr
= SCM_GC_WRITABLE_VELTS (w
);
340 for (j
= 0; j
< n
; ++j
)
348 while (SCM_CONSP (alist
)
349 && SCM_CONSP (SCM_CAR (alist
)))
354 key
= SCM_CAAR (alist
);
355 value
= SCM_CDAR (alist
);
356 if ( (weak_keys
&& UNMARKED_CELL_P (key
))
357 || (weak_values
&& UNMARKED_CELL_P (value
)))
359 *fixup
= SCM_CDR (alist
);
362 fixup
= SCM_CDRLOC (alist
);
363 alist
= SCM_CDR (alist
);
375 scm_weaks_prehistory ()
377 scm_c_hook_add (&scm_before_mark_c_hook
, scm_weak_vector_gc_init
, 0, 0);
378 scm_c_hook_add (&scm_before_sweep_c_hook
, scm_mark_weak_vector_spines
, 0, 0);
379 scm_c_hook_add (&scm_after_sweep_c_hook
, scm_scan_weak_vectors
, 0, 0);
384 scm_init_weaks_builtins ()
386 #include "libguile/weaks.x"
387 return SCM_UNSPECIFIED
;
393 scm_c_define_gsubr ("%init-weaks-builtins", 0, 0, 0,
394 scm_init_weaks_builtins
);