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
81 c_size
= scm_to_unsigned_integer (size
, 0, SCM_VECTOR_MAX_LENGTH
);
88 if (SCM_UNBNDP (fill
))
89 fill
= SCM_UNSPECIFIED
;
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
),
97 SCM_UNPACK (SCM_EOL
));
98 scm_remember_upto_here_1 (fill
);
102 v
= scm_double_cell (SCM_MAKE_VECTOR_TAG (0, scm_tc7_wvect
),
105 SCM_UNPACK (SCM_EOL
));
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"
118 #define FUNC_NAME s_scm_make_weak_vector
120 return scm_i_allocate_weak_vector (0, size
, fill
, FUNC_NAME
);
125 SCM_REGISTER_PROC(s_list_to_weak_vector
, "list->weak-vector", 1, 0, 0, scm_weak_vector
);
127 SCM_DEFINE (scm_weak_vector
, "weak-vector", 0, 0, 1,
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
140 /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted
141 while the vector is being created. */
143 SCM_ASSERT (i
>= 0, l
, SCM_ARG1
, FUNC_NAME
);
144 res
= scm_make_weak_vector (scm_from_int (i
), SCM_UNSPECIFIED
);
147 no alloc, so this loop is safe.
149 data
= SCM_WRITABLE_VELTS (res
);
150 while (!SCM_NULL_OR_NIL_P (l
))
152 *data
++ = SCM_CAR (l
);
161 SCM_DEFINE (scm_weak_vector_p
, "weak-vector?", 1, 0, 0,
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
167 return scm_from_bool (SCM_WVECTP (obj
) && !SCM_IS_WHVEC (obj
));
173 SCM_DEFINE (scm_make_weak_key_alist_vector
, "make-weak-key-alist-vector", 0, 1, 0,
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"
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
185 return scm_i_allocate_weak_vector
186 (1, SCM_UNBNDP (size
) ? scm_from_int (31) : size
, SCM_EOL
, FUNC_NAME
);
191 SCM_DEFINE (scm_make_weak_value_alist_vector
, "make-weak-value-alist-vector", 0, 1, 0,
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
197 return scm_i_allocate_weak_vector
198 (2, SCM_UNBNDP (size
) ? scm_from_int (31) : size
, SCM_EOL
, FUNC_NAME
);
203 SCM_DEFINE (scm_make_doubly_weak_alist_vector
, "make-doubly-weak-alist-vector", 1, 0, 0,
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
209 return scm_i_allocate_weak_vector
210 (3, SCM_UNBNDP (size
) ? scm_from_int (31) : size
, SCM_EOL
, FUNC_NAME
);
215 SCM_DEFINE (scm_weak_key_alist_vector_p
, "weak-key-alist-vector?", 1, 0, 0,
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
224 return scm_from_bool (SCM_WVECTP (obj
) && SCM_IS_WHVEC (obj
));
229 SCM_DEFINE (scm_weak_value_alist_vector_p
, "weak-value-alist-vector?", 1, 0, 0,
231 "Return @code{#t} if @var{obj} is a weak value hash table.")
232 #define FUNC_NAME s_scm_weak_value_alist_vector_p
234 return scm_from_bool (SCM_WVECTP (obj
) && SCM_IS_WHVEC_V (obj
));
239 SCM_DEFINE (scm_doubly_weak_alist_vector_p
, "doubly-weak-alist-vector?", 1, 0, 0,
241 "Return @code{#t} if @var{obj} is a doubly weak hash table.")
242 #define FUNC_NAME s_scm_doubly_weak_alist_vector_p
244 return scm_from_bool (SCM_WVECTP (obj
) && SCM_IS_WHVEC_B (obj
));
250 scm_weak_vector_gc_init (void *dummy1 SCM_UNUSED
,
251 void *dummy2 SCM_UNUSED
,
252 void *dummy3 SCM_UNUSED
)
254 scm_weak_vectors
= SCM_EOL
;
261 scm_mark_weak_vector_spines (void *dummy1 SCM_UNUSED
,
262 void *dummy2 SCM_UNUSED
,
263 void *dummy3 SCM_UNUSED
)
267 for (w
= scm_weak_vectors
; !scm_is_null (w
); w
= SCM_WVECT_GC_CHAIN (w
))
269 if (SCM_IS_WHVEC_ANY (w
))
278 n
= SCM_VECTOR_LENGTH (w
);
279 for (j
= 0; j
< n
; ++j
)
284 while ( scm_is_pair (alist
)
285 && !SCM_GC_MARK_P (alist
)
286 && scm_is_pair (SCM_CAR (alist
)))
288 SCM_SET_GC_MARK (alist
);
289 SCM_SET_GC_MARK (SCM_CAR (alist
));
290 alist
= SCM_CDR (alist
);
299 #define UNMARKED_CELL_P(x) (SCM_NIMP(x) && !SCM_GC_MARK_P (x))
302 scm_scan_weak_vectors (void *dummy1 SCM_UNUSED
,
303 void *dummy2 SCM_UNUSED
,
304 void *dummy3 SCM_UNUSED
)
307 for (w
= scm_weak_vectors
; !scm_is_null (w
); w
= SCM_WVECT_GC_CHAIN (w
))
309 if (!SCM_IS_WHVEC_ANY (w
))
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
]))
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
))
324 register long n
= SCM_VECTOR_LENGTH (w
);
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
);
329 ptr
= SCM_GC_WRITABLE_VELTS (w
);
331 for (j
= 0; j
< n
; ++j
)
339 while (scm_is_pair (alist
)
340 && scm_is_pair (SCM_CAR (alist
)))
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
)))
350 *fixup
= SCM_CDR (alist
);
353 fixup
= SCM_CDRLOC (alist
);
354 alist
= SCM_CDR (alist
);
366 scm_weaks_prehistory ()
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);
375 scm_init_weaks_builtins ()
377 #include "libguile/weaks.x"
378 return SCM_UNSPECIFIED
;
384 scm_c_define_gsubr ("%init-weaks-builtins", 0, 0, 0,
385 scm_init_weaks_builtins
);