* scheme-modules.texi: split "Scheme and modules" into
[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"
c96d76b8 47#include "libguile/lang.h"
0f2d19dd 48
a0599745
MD
49#include "libguile/validate.h"
50#include "libguile/weaks.h"
0f2d19dd 51
592996c9 52\f
0f2d19dd
JB
53
54/* {Weak Vectors}
55 */
56
57
592996c9
DH
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 */
63static SCM
64allocate_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
592996c9
DH
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);
4c9419ac 84 base = scm_gc_malloc (c_size * sizeof (scm_t_bits), "weak vector");
592996c9
DH
85 for (j = 0; j != c_size; ++j)
86 base[j] = SCM_UNPACK (fill);
228a24ef
DH
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));
592996c9
DH
91 scm_remember_upto_here_1 (fill);
92 }
93 else
94 {
228a24ef
DH
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));
592996c9
DH
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
3b3b36dd 111SCM_DEFINE (scm_make_weak_vector, "make-weak-vector", 1, 1, 0,
1e6808ea 112 (SCM size, SCM fill),
b380b885 113 "Return a weak vector with @var{size} elements. If the optional\n"
1e6808ea
MG
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.")
1bbd0b84 117#define FUNC_NAME s_scm_make_weak_vector
0f2d19dd 118{
592996c9 119 return allocate_weak_vector (0, size, fill, FUNC_NAME);
0f2d19dd 120}
1bbd0b84 121#undef FUNC_NAME
0f2d19dd
JB
122
123
1bbd0b84 124SCM_REGISTER_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector);
1cc91f1b 125
3b3b36dd 126SCM_DEFINE (scm_weak_vector, "weak-vector", 0, 0, 1,
1bbd0b84 127 (SCM l),
8f85c0c6 128 "@deffnx {Scheme Procedure} list->weak-vector l\n"
1e6808ea
MG
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.")
1bbd0b84 133#define FUNC_NAME s_scm_weak_vector
0f2d19dd
JB
134{
135 SCM res;
22a52da1 136 SCM *data;
c014a02e 137 long i;
0f2d19dd 138
22a52da1
DH
139 /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted
140 while the vector is being created. */
0f2d19dd 141 i = scm_ilength (l);
1bbd0b84 142 SCM_ASSERT (i >= 0, l, SCM_ARG1, FUNC_NAME);
0f2d19dd 143 res = scm_make_weak_vector (SCM_MAKINUM (i), SCM_UNSPECIFIED);
22a52da1 144
34d19ef6
HWN
145 /*
146 no alloc, so this loop is safe.
147 */
148 data = SCM_WRITABLE_VELTS (res);
c96d76b8 149 while (!SCM_NULL_OR_NIL_P (l))
22a52da1
DH
150 {
151 *data++ = SCM_CAR (l);
152 l = SCM_CDR (l);
153 }
154
0f2d19dd
JB
155 return res;
156}
1bbd0b84 157#undef FUNC_NAME
0f2d19dd
JB
158
159
3b3b36dd 160SCM_DEFINE (scm_weak_vector_p, "weak-vector?", 1, 0, 0,
1e6808ea 161 (SCM obj),
5352393c
MG
162 "Return @code{#t} if @var{obj} is a weak vector. Note that all\n"
163 "weak hashes are also weak vectors.")
1bbd0b84 164#define FUNC_NAME s_scm_weak_vector_p
0f2d19dd 165{
592996c9 166 return SCM_BOOL (SCM_WVECTP (obj) && !SCM_IS_WHVEC (obj));
0f2d19dd 167}
1bbd0b84 168#undef FUNC_NAME
0f2d19dd 169
0f2d19dd
JB
170\f
171
3b3b36dd 172SCM_DEFINE (scm_make_weak_key_hash_table, "make-weak-key-hash-table", 1, 0, 0,
1e6808ea 173 (SCM size),
8f85c0c6
NJ
174 "@deffnx {Scheme Procedure} make-weak-value-hash-table size\n"
175 "@deffnx {Scheme Procedure} make-doubly-weak-hash-table size\n"
1e6808ea
MG
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})")
1bbd0b84 182#define FUNC_NAME s_scm_make_weak_key_hash_table
0f2d19dd 183{
592996c9 184 return allocate_weak_vector (1, size, SCM_EOL, FUNC_NAME);
0f2d19dd 185}
1bbd0b84 186#undef FUNC_NAME
0f2d19dd
JB
187
188
a1ec6916 189SCM_DEFINE (scm_make_weak_value_hash_table, "make-weak-value-hash-table", 1, 0, 0,
1e6808ea 190 (SCM size),
e3239868
DH
191 "Return a hash table with weak values with @var{size} buckets.\n"
192 "(@pxref{Hash Tables})")
1bbd0b84 193#define FUNC_NAME s_scm_make_weak_value_hash_table
0f2d19dd 194{
592996c9 195 return allocate_weak_vector (2, size, SCM_EOL, FUNC_NAME);
0f2d19dd 196}
1bbd0b84 197#undef FUNC_NAME
0f2d19dd
JB
198
199
a1ec6916 200SCM_DEFINE (scm_make_doubly_weak_hash_table, "make-doubly-weak-hash-table", 1, 0, 0,
1e6808ea 201 (SCM size),
e3239868
DH
202 "Return a hash table with weak keys and values with @var{size}\n"
203 "buckets. (@pxref{Hash Tables})")
1bbd0b84 204#define FUNC_NAME s_scm_make_doubly_weak_hash_table
0f2d19dd 205{
592996c9 206 return allocate_weak_vector (3, size, SCM_EOL, FUNC_NAME);
0f2d19dd 207}
1bbd0b84 208#undef FUNC_NAME
0f2d19dd 209
592996c9 210
3b3b36dd 211SCM_DEFINE (scm_weak_key_hash_table_p, "weak-key-hash-table?", 1, 0, 0,
1e6808ea 212 (SCM obj),
8f85c0c6
NJ
213 "@deffnx {Scheme Procedure} weak-value-hash-table? obj\n"
214 "@deffnx {Scheme Procedure} doubly-weak-hash-table? obj\n"
5352393c
MG
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.")
1bbd0b84 218#define FUNC_NAME s_scm_weak_key_hash_table_p
0f2d19dd 219{
592996c9 220 return SCM_BOOL (SCM_WVECTP (obj) && SCM_IS_WHVEC (obj));
0f2d19dd 221}
1bbd0b84 222#undef FUNC_NAME
0f2d19dd
JB
223
224
a1ec6916 225SCM_DEFINE (scm_weak_value_hash_table_p, "weak-value-hash-table?", 1, 0, 0,
1e6808ea
MG
226 (SCM obj),
227 "Return @code{#t} if @var{obj} is a weak value hash table.")
1bbd0b84 228#define FUNC_NAME s_scm_weak_value_hash_table_p
0f2d19dd 229{
592996c9 230 return SCM_BOOL (SCM_WVECTP (obj) && SCM_IS_WHVEC_V (obj));
0f2d19dd 231}
1bbd0b84 232#undef FUNC_NAME
0f2d19dd
JB
233
234
a1ec6916 235SCM_DEFINE (scm_doubly_weak_hash_table_p, "doubly-weak-hash-table?", 1, 0, 0,
1e6808ea
MG
236 (SCM obj),
237 "Return @code{#t} if @var{obj} is a doubly weak hash table.")
1bbd0b84 238#define FUNC_NAME s_scm_doubly_weak_hash_table_p
0f2d19dd 239{
592996c9 240 return SCM_BOOL (SCM_WVECTP (obj) && SCM_IS_WHVEC_B (obj));
0f2d19dd 241}
1bbd0b84 242#undef FUNC_NAME
0f2d19dd 243
592996c9 244
d662820a 245static void *
e81d98ec
DH
246scm_weak_vector_gc_init (void *dummy1 SCM_UNUSED,
247 void *dummy2 SCM_UNUSED,
248 void *dummy3 SCM_UNUSED)
d662820a
MD
249{
250 scm_weak_vectors = SCM_EOL;
251
252 return 0;
253}
254
592996c9 255
d662820a 256static void *
e81d98ec
DH
257scm_mark_weak_vector_spines (void *dummy1 SCM_UNUSED,
258 void *dummy2 SCM_UNUSED,
259 void *dummy3 SCM_UNUSED)
d662820a
MD
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 {
34d19ef6 267 SCM const *ptr;
d662820a 268 SCM obj;
c014a02e
ML
269 long j;
270 long n;
d662820a
MD
271
272 obj = w;
273 ptr = SCM_VELTS (w);
bfa974f0 274 n = SCM_VECTOR_LENGTH (w);
d662820a
MD
275 for (j = 0; j < n; ++j)
276 {
277 SCM alist;
278
279 alist = ptr[j];
280 while ( SCM_CONSP (alist)
281 && !SCM_GCMARKP (alist)
282 && SCM_CONSP (SCM_CAR (alist)))
283 {
284 SCM_SETGCMARK (alist);
285 SCM_SETGCMARK (SCM_CAR (alist));
fd336365 286 alist = SCM_CDR (alist);
d662820a
MD
287 }
288 }
289 }
290 }
291
292 return 0;
293}
294
592996c9 295
d662820a 296static void *
e81d98ec
DH
297scm_scan_weak_vectors (void *dummy1 SCM_UNUSED,
298 void *dummy2 SCM_UNUSED,
299 void *dummy3 SCM_UNUSED)
d662820a
MD
300{
301 SCM *ptr, w;
302 for (w = scm_weak_vectors; !SCM_NULLP (w); w = SCM_WVECT_GC_CHAIN (w))
303 {
304 if (!SCM_IS_WHVEC_ANY (w))
305 {
306 register long j, n;
307
34d19ef6 308 ptr = SCM_GC_WRITABLE_VELTS (w);
bfa974f0 309 n = SCM_VECTOR_LENGTH (w);
d662820a 310 for (j = 0; j < n; ++j)
406c7d90 311 if (SCM_FREE_CELL_P (ptr[j]))
d662820a
MD
312 ptr[j] = SCM_BOOL_F;
313 }
314 else /* if (SCM_IS_WHVEC_ANY (scm_weak_vectors[i])) */
315 {
316 SCM obj = w;
c014a02e
ML
317 register long n = SCM_VECTOR_LENGTH (w);
318 register long j;
d9dcd933
ML
319 int weak_keys = SCM_IS_WHVEC (obj) || SCM_IS_WHVEC_B (obj);
320 int weak_values = SCM_IS_WHVEC_V (obj) || SCM_IS_WHVEC_B (obj);
d662820a 321
34d19ef6 322 ptr = SCM_GC_WRITABLE_VELTS (w);
d662820a
MD
323
324 for (j = 0; j < n; ++j)
325 {
326 SCM * fixup;
327 SCM alist;
d662820a
MD
328
329 fixup = ptr + j;
330 alist = *fixup;
331
332 while ( SCM_CONSP (alist)
333 && SCM_CONSP (SCM_CAR (alist)))
334 {
335 SCM key;
336 SCM value;
337
338 key = SCM_CAAR (alist);
339 value = SCM_CDAR (alist);
406c7d90
DH
340 if ( (weak_keys && SCM_FREE_CELL_P (key))
341 || (weak_values && SCM_FREE_CELL_P (value)))
d662820a
MD
342 {
343 *fixup = SCM_CDR (alist);
344 }
345 else
346 fixup = SCM_CDRLOC (alist);
347 alist = SCM_CDR (alist);
348 }
349 }
350 }
351 }
352
353 return 0;
354}
355
0f2d19dd
JB
356\f
357
d662820a
MD
358void
359scm_weaks_prehistory ()
360{
361 scm_c_hook_add (&scm_before_mark_c_hook, scm_weak_vector_gc_init, 0, 0);
362 scm_c_hook_add (&scm_before_sweep_c_hook, scm_mark_weak_vector_spines, 0, 0);
363 scm_c_hook_add (&scm_after_sweep_c_hook, scm_scan_weak_vectors, 0, 0);
364}
365
592996c9 366
0f2d19dd
JB
367void
368scm_init_weaks ()
0f2d19dd 369{
a0599745 370#include "libguile/weaks.x"
0f2d19dd
JB
371}
372
89e00824
ML
373
374/*
375 Local Variables:
376 c-file-style: "gnu"
377 End:
378*/