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