* ports.c (scm_getc): minor tweak.
[bpt/guile.git] / libguile / weaks.c
CommitLineData
f59a096e 1/* Copyright (C) 1995,1996,1998,2000,2001, 2003 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"
f59a096e 48#include "libguile/hashtab.h"
0f2d19dd 49
a0599745
MD
50#include "libguile/validate.h"
51#include "libguile/weaks.h"
0f2d19dd 52
592996c9 53\f
0f2d19dd 54
c35738c1
MD
55/* 1. The current hash table implementation in hashtab.c uses weak alist
56 * vectors (formerly called weak hash tables) internally.
57 *
58 * 2. All hash table operations still work on alist vectors.
59 *
60 * 3. The weak vector and alist vector Scheme API is accessed through
61 * the module (ice-9 weak-vector).
62 */
63
64
0f2d19dd
JB
65/* {Weak Vectors}
66 */
67
68
592996c9
DH
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.
73 */
c35738c1
MD
74SCM
75scm_i_allocate_weak_vector (scm_t_bits type, SCM size, SCM fill, const char* caller)
592996c9
DH
76#define FUNC_NAME caller
77{
78 if (SCM_INUMP (size))
79 {
80 size_t c_size;
81 SCM v;
82
83 SCM_ASSERT_RANGE (1, size, SCM_INUM (size) >= 0);
84 c_size = SCM_INUM (size);
85
592996c9
DH
86 if (c_size > 0)
87 {
88 scm_t_bits *base;
89 size_t j;
90
91 if (SCM_UNBNDP (fill))
92 fill = SCM_UNSPECIFIED;
93
94 SCM_ASSERT_RANGE (1, size, c_size <= SCM_VECTOR_MAX_LENGTH);
4c9419ac 95 base = scm_gc_malloc (c_size * sizeof (scm_t_bits), "weak vector");
592996c9
DH
96 for (j = 0; j != c_size; ++j)
97 base[j] = SCM_UNPACK (fill);
228a24ef
DH
98 v = scm_double_cell (SCM_MAKE_VECTOR_TAG (c_size, scm_tc7_wvect),
99 (scm_t_bits) base,
100 type,
101 SCM_UNPACK (SCM_EOL));
592996c9
DH
102 scm_remember_upto_here_1 (fill);
103 }
104 else
105 {
228a24ef
DH
106 v = scm_double_cell (SCM_MAKE_VECTOR_TAG (0, scm_tc7_wvect),
107 (scm_t_bits) NULL,
108 type,
109 SCM_UNPACK (SCM_EOL));
592996c9
DH
110 }
111
112 return v;
113 }
114 else if (SCM_BIGP (size))
115 SCM_OUT_OF_RANGE (1, size);
116 else
117 SCM_WRONG_TYPE_ARG (1, size);
118}
119#undef FUNC_NAME
120
3b3b36dd 121SCM_DEFINE (scm_make_weak_vector, "make-weak-vector", 1, 1, 0,
1e6808ea 122 (SCM size, SCM fill),
b380b885 123 "Return a weak vector with @var{size} elements. If the optional\n"
1e6808ea
MG
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"
126 "empty list.")
1bbd0b84 127#define FUNC_NAME s_scm_make_weak_vector
0f2d19dd 128{
c35738c1 129 return scm_i_allocate_weak_vector (0, size, fill, FUNC_NAME);
0f2d19dd 130}
1bbd0b84 131#undef FUNC_NAME
0f2d19dd
JB
132
133
1bbd0b84 134SCM_REGISTER_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector);
1cc91f1b 135
3b3b36dd 136SCM_DEFINE (scm_weak_vector, "weak-vector", 0, 0, 1,
1bbd0b84 137 (SCM l),
8f85c0c6 138 "@deffnx {Scheme Procedure} list->weak-vector l\n"
1e6808ea
MG
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.")
1bbd0b84 143#define FUNC_NAME s_scm_weak_vector
0f2d19dd
JB
144{
145 SCM res;
22a52da1 146 SCM *data;
c014a02e 147 long i;
0f2d19dd 148
22a52da1
DH
149 /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted
150 while the vector is being created. */
0f2d19dd 151 i = scm_ilength (l);
1bbd0b84 152 SCM_ASSERT (i >= 0, l, SCM_ARG1, FUNC_NAME);
0f2d19dd 153 res = scm_make_weak_vector (SCM_MAKINUM (i), SCM_UNSPECIFIED);
22a52da1 154
34d19ef6
HWN
155 /*
156 no alloc, so this loop is safe.
157 */
158 data = SCM_WRITABLE_VELTS (res);
c96d76b8 159 while (!SCM_NULL_OR_NIL_P (l))
22a52da1
DH
160 {
161 *data++ = SCM_CAR (l);
162 l = SCM_CDR (l);
163 }
164
0f2d19dd
JB
165 return res;
166}
1bbd0b84 167#undef FUNC_NAME
0f2d19dd
JB
168
169
3b3b36dd 170SCM_DEFINE (scm_weak_vector_p, "weak-vector?", 1, 0, 0,
1e6808ea 171 (SCM obj),
5352393c
MG
172 "Return @code{#t} if @var{obj} is a weak vector. Note that all\n"
173 "weak hashes are also weak vectors.")
1bbd0b84 174#define FUNC_NAME s_scm_weak_vector_p
0f2d19dd 175{
592996c9 176 return SCM_BOOL (SCM_WVECTP (obj) && !SCM_IS_WHVEC (obj));
0f2d19dd 177}
1bbd0b84 178#undef FUNC_NAME
0f2d19dd 179
0f2d19dd
JB
180\f
181
c35738c1 182SCM_DEFINE (scm_make_weak_key_alist_vector, "make-weak-key-alist-vector", 0, 1, 0,
1e6808ea 183 (SCM size),
c35738c1
MD
184 "@deffnx {Scheme Procedure} make-weak-value-alist-vector size\n"
185 "@deffnx {Scheme Procedure} make-doubly-weak-alist-vector size\n"
1e6808ea
MG
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"
188 "caution.\n"
189 "\n"
190 "You can modify weak hash tables in exactly the same way you\n"
191 "would modify regular hash tables. (@pxref{Hash Tables})")
c35738c1 192#define FUNC_NAME s_scm_make_weak_key_alist_vector
0f2d19dd 193{
c35738c1
MD
194 return scm_i_allocate_weak_vector
195 (1, SCM_UNBNDP (size) ? SCM_MAKINUM (31) : size, SCM_EOL, FUNC_NAME);
0f2d19dd 196}
1bbd0b84 197#undef FUNC_NAME
0f2d19dd
JB
198
199
c35738c1 200SCM_DEFINE (scm_make_weak_value_alist_vector, "make-weak-value-alist-vector", 0, 1, 0,
1e6808ea 201 (SCM size),
e3239868
DH
202 "Return a hash table with weak values with @var{size} buckets.\n"
203 "(@pxref{Hash Tables})")
c35738c1 204#define FUNC_NAME s_scm_make_weak_value_alist_vector
0f2d19dd 205{
c35738c1
MD
206 return scm_i_allocate_weak_vector
207 (2, SCM_UNBNDP (size) ? SCM_MAKINUM (31) : size, SCM_EOL, FUNC_NAME);
0f2d19dd 208}
1bbd0b84 209#undef FUNC_NAME
0f2d19dd
JB
210
211
c35738c1 212SCM_DEFINE (scm_make_doubly_weak_alist_vector, "make-doubly-weak-alist-vector", 1, 0, 0,
1e6808ea 213 (SCM size),
e3239868
DH
214 "Return a hash table with weak keys and values with @var{size}\n"
215 "buckets. (@pxref{Hash Tables})")
c35738c1 216#define FUNC_NAME s_scm_make_doubly_weak_alist_vector
0f2d19dd 217{
c35738c1
MD
218 return scm_i_allocate_weak_vector
219 (3, SCM_UNBNDP (size) ? SCM_MAKINUM (31) : size, SCM_EOL, FUNC_NAME);
0f2d19dd 220}
1bbd0b84 221#undef FUNC_NAME
0f2d19dd 222
592996c9 223
c35738c1 224SCM_DEFINE (scm_weak_key_alist_vector_p, "weak-key-alist-vector?", 1, 0, 0,
1e6808ea 225 (SCM obj),
c35738c1
MD
226 "@deffnx {Scheme Procedure} weak-value-alist-vector? obj\n"
227 "@deffnx {Scheme Procedure} doubly-weak-alist-vector? obj\n"
5352393c
MG
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.")
c35738c1 231#define FUNC_NAME s_scm_weak_key_alist_vector_p
0f2d19dd 232{
592996c9 233 return SCM_BOOL (SCM_WVECTP (obj) && SCM_IS_WHVEC (obj));
0f2d19dd 234}
1bbd0b84 235#undef FUNC_NAME
0f2d19dd
JB
236
237
c35738c1 238SCM_DEFINE (scm_weak_value_alist_vector_p, "weak-value-alist-vector?", 1, 0, 0,
1e6808ea
MG
239 (SCM obj),
240 "Return @code{#t} if @var{obj} is a weak value hash table.")
c35738c1 241#define FUNC_NAME s_scm_weak_value_alist_vector_p
0f2d19dd 242{
592996c9 243 return SCM_BOOL (SCM_WVECTP (obj) && SCM_IS_WHVEC_V (obj));
0f2d19dd 244}
1bbd0b84 245#undef FUNC_NAME
0f2d19dd
JB
246
247
c35738c1 248SCM_DEFINE (scm_doubly_weak_alist_vector_p, "doubly-weak-alist-vector?", 1, 0, 0,
1e6808ea
MG
249 (SCM obj),
250 "Return @code{#t} if @var{obj} is a doubly weak hash table.")
c35738c1 251#define FUNC_NAME s_scm_doubly_weak_alist_vector_p
0f2d19dd 252{
592996c9 253 return SCM_BOOL (SCM_WVECTP (obj) && SCM_IS_WHVEC_B (obj));
0f2d19dd 254}
1bbd0b84 255#undef FUNC_NAME
0f2d19dd 256
592996c9 257
d662820a 258static void *
e81d98ec
DH
259scm_weak_vector_gc_init (void *dummy1 SCM_UNUSED,
260 void *dummy2 SCM_UNUSED,
261 void *dummy3 SCM_UNUSED)
d662820a
MD
262{
263 scm_weak_vectors = SCM_EOL;
264
265 return 0;
266}
267
592996c9 268
d662820a 269static void *
e81d98ec
DH
270scm_mark_weak_vector_spines (void *dummy1 SCM_UNUSED,
271 void *dummy2 SCM_UNUSED,
272 void *dummy3 SCM_UNUSED)
d662820a
MD
273{
274 SCM w;
275
276 for (w = scm_weak_vectors; !SCM_NULLP (w); w = SCM_WVECT_GC_CHAIN (w))
277 {
278 if (SCM_IS_WHVEC_ANY (w))
279 {
34d19ef6 280 SCM const *ptr;
d662820a 281 SCM obj;
c014a02e
ML
282 long j;
283 long n;
d662820a
MD
284
285 obj = w;
286 ptr = SCM_VELTS (w);
bfa974f0 287 n = SCM_VECTOR_LENGTH (w);
d662820a
MD
288 for (j = 0; j < n; ++j)
289 {
290 SCM alist;
291
292 alist = ptr[j];
293 while ( SCM_CONSP (alist)
c8a1bdc4 294 && !SCM_GC_MARK_P (alist)
d662820a
MD
295 && SCM_CONSP (SCM_CAR (alist)))
296 {
c8a1bdc4
HWN
297 SCM_SET_GC_MARK (alist);
298 SCM_SET_GC_MARK (SCM_CAR (alist));
fd336365 299 alist = SCM_CDR (alist);
d662820a
MD
300 }
301 }
302 }
303 }
304
305 return 0;
306}
307
c8a1bdc4 308#define UNMARKED_CELL_P(x) (SCM_NIMP(x) && !SCM_GC_MARK_P (x))
592996c9 309
d662820a 310static void *
e81d98ec
DH
311scm_scan_weak_vectors (void *dummy1 SCM_UNUSED,
312 void *dummy2 SCM_UNUSED,
313 void *dummy3 SCM_UNUSED)
d662820a
MD
314{
315 SCM *ptr, w;
316 for (w = scm_weak_vectors; !SCM_NULLP (w); w = SCM_WVECT_GC_CHAIN (w))
317 {
318 if (!SCM_IS_WHVEC_ANY (w))
319 {
320 register long j, n;
321
34d19ef6 322 ptr = SCM_GC_WRITABLE_VELTS (w);
bfa974f0 323 n = SCM_VECTOR_LENGTH (w);
d662820a 324 for (j = 0; j < n; ++j)
c8a1bdc4 325 if (UNMARKED_CELL_P (ptr[j]))
d662820a
MD
326 ptr[j] = SCM_BOOL_F;
327 }
c35738c1
MD
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))
d662820a
MD
331 {
332 SCM obj = w;
c014a02e
ML
333 register long n = SCM_VECTOR_LENGTH (w);
334 register long j;
d9dcd933
ML
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);
d662820a 337
34d19ef6 338 ptr = SCM_GC_WRITABLE_VELTS (w);
d662820a
MD
339
340 for (j = 0; j < n; ++j)
341 {
342 SCM * fixup;
343 SCM alist;
d662820a
MD
344
345 fixup = ptr + j;
346 alist = *fixup;
347
c8a1bdc4
HWN
348 while (SCM_CONSP (alist)
349 && SCM_CONSP (SCM_CAR (alist)))
d662820a
MD
350 {
351 SCM key;
352 SCM value;
353
354 key = SCM_CAAR (alist);
355 value = SCM_CDAR (alist);
c8a1bdc4
HWN
356 if ( (weak_keys && UNMARKED_CELL_P (key))
357 || (weak_values && UNMARKED_CELL_P (value)))
d662820a
MD
358 {
359 *fixup = SCM_CDR (alist);
360 }
361 else
362 fixup = SCM_CDRLOC (alist);
363 alist = SCM_CDR (alist);
364 }
365 }
366 }
367 }
368
369 return 0;
370}
371
0f2d19dd
JB
372\f
373
d662820a
MD
374void
375scm_weaks_prehistory ()
376{
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);
380}
381
592996c9 382
c35738c1
MD
383SCM
384scm_init_weaks_builtins ()
385{
386#include "libguile/weaks.x"
387 return SCM_UNSPECIFIED;
388}
389
0f2d19dd
JB
390void
391scm_init_weaks ()
0f2d19dd 392{
c35738c1
MD
393 scm_c_define_gsubr ("%init-weaks-builtins", 0, 0, 0,
394 scm_init_weaks_builtins);
0f2d19dd
JB
395}
396
89e00824
ML
397
398/*
399 Local Variables:
400 c-file-style: "gnu"
401 End:
402*/