Fixed use of finalizers for guardians and SMOBs (undoes patches 23-24).
[bpt/guile.git] / libguile / weaks.c
CommitLineData
2b829bbb 1/* Copyright (C) 1995,1996,1998,2000,2001, 2003, 2006 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
92205699
MV
15 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301 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
06c1d900
MV
45#include <stdio.h>
46
a0599745
MD
47#include "libguile/_scm.h"
48#include "libguile/vectors.h"
c96d76b8 49#include "libguile/lang.h"
f59a096e 50#include "libguile/hashtab.h"
0f2d19dd 51
a0599745
MD
52#include "libguile/validate.h"
53#include "libguile/weaks.h"
0f2d19dd 54
592996c9 55\f
0f2d19dd 56
c35738c1
MD
57/* 1. The current hash table implementation in hashtab.c uses weak alist
58 * vectors (formerly called weak hash tables) internally.
59 *
60 * 2. All hash table operations still work on alist vectors.
61 *
62 * 3. The weak vector and alist vector Scheme API is accessed through
63 * the module (ice-9 weak-vector).
64 */
65
66
0f2d19dd
JB
67/* {Weak Vectors}
68 */
69
70
3b3b36dd 71SCM_DEFINE (scm_make_weak_vector, "make-weak-vector", 1, 1, 0,
1e6808ea 72 (SCM size, SCM fill),
b380b885 73 "Return a weak vector with @var{size} elements. If the optional\n"
1e6808ea
MG
74 "argument @var{fill} is given, all entries in the vector will be\n"
75 "set to @var{fill}. The default value for @var{fill} is the\n"
76 "empty list.")
1bbd0b84 77#define FUNC_NAME s_scm_make_weak_vector
0f2d19dd 78{
d525e4f9 79 return scm_i_make_weak_vector (0, size, fill);
0f2d19dd 80}
1bbd0b84 81#undef FUNC_NAME
0f2d19dd
JB
82
83
1bbd0b84 84SCM_REGISTER_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector);
1cc91f1b 85
3b3b36dd 86SCM_DEFINE (scm_weak_vector, "weak-vector", 0, 0, 1,
1bbd0b84 87 (SCM l),
8f85c0c6 88 "@deffnx {Scheme Procedure} list->weak-vector l\n"
1e6808ea
MG
89 "Construct a weak vector from a list: @code{weak-vector} uses\n"
90 "the list of its arguments while @code{list->weak-vector} uses\n"
91 "its only argument @var{l} (a list) to construct a weak vector\n"
92 "the same way @code{list->vector} would.")
1bbd0b84 93#define FUNC_NAME s_scm_weak_vector
0f2d19dd 94{
d525e4f9 95 return scm_i_make_weak_vector_from_list (0, l);
0f2d19dd 96}
1bbd0b84 97#undef FUNC_NAME
0f2d19dd
JB
98
99
3b3b36dd 100SCM_DEFINE (scm_weak_vector_p, "weak-vector?", 1, 0, 0,
1e6808ea 101 (SCM obj),
5352393c
MG
102 "Return @code{#t} if @var{obj} is a weak vector. Note that all\n"
103 "weak hashes are also weak vectors.")
1bbd0b84 104#define FUNC_NAME s_scm_weak_vector_p
0f2d19dd 105{
6e708ef2 106 return scm_from_bool (SCM_I_WVECTP (obj) && !SCM_IS_WHVEC (obj));
0f2d19dd 107}
1bbd0b84 108#undef FUNC_NAME
0f2d19dd 109
0f2d19dd 110\f
3a2de079
LC
111/* Weak alist vectors, i.e., vectors of alists.
112
113 The alist vector themselves are _not_ weak. The `car' (or `cdr', or both)
114 of the pairs within it are weak. See `hashtab.c' for details. */
0f2d19dd 115
4650cdd2
LC
116
117/* FIXME: We used to have two implementations of weak hash tables: the one in
118 here and the one in `hashtab.c'. The difference is that weak alist
119 vectors could be used as vectors while (weak) hash tables can't. We need
120 to unify that. */
121
c35738c1 122SCM_DEFINE (scm_make_weak_key_alist_vector, "make-weak-key-alist-vector", 0, 1, 0,
1e6808ea 123 (SCM size),
c35738c1
MD
124 "@deffnx {Scheme Procedure} make-weak-value-alist-vector size\n"
125 "@deffnx {Scheme Procedure} make-doubly-weak-alist-vector size\n"
1e6808ea
MG
126 "Return a weak hash table with @var{size} buckets. As with any\n"
127 "hash table, choosing a good size for the table requires some\n"
128 "caution.\n"
129 "\n"
130 "You can modify weak hash tables in exactly the same way you\n"
131 "would modify regular hash tables. (@pxref{Hash Tables})")
c35738c1 132#define FUNC_NAME s_scm_make_weak_key_alist_vector
0f2d19dd 133{
4650cdd2 134 return scm_make_weak_key_hash_table (size);
0f2d19dd 135}
1bbd0b84 136#undef FUNC_NAME
0f2d19dd
JB
137
138
c35738c1 139SCM_DEFINE (scm_make_weak_value_alist_vector, "make-weak-value-alist-vector", 0, 1, 0,
1e6808ea 140 (SCM size),
e3239868
DH
141 "Return a hash table with weak values with @var{size} buckets.\n"
142 "(@pxref{Hash Tables})")
c35738c1 143#define FUNC_NAME s_scm_make_weak_value_alist_vector
0f2d19dd 144{
4650cdd2 145 return scm_make_weak_value_hash_table (size);
0f2d19dd 146}
1bbd0b84 147#undef FUNC_NAME
0f2d19dd
JB
148
149
c35738c1 150SCM_DEFINE (scm_make_doubly_weak_alist_vector, "make-doubly-weak-alist-vector", 1, 0, 0,
1e6808ea 151 (SCM size),
e3239868
DH
152 "Return a hash table with weak keys and values with @var{size}\n"
153 "buckets. (@pxref{Hash Tables})")
c35738c1 154#define FUNC_NAME s_scm_make_doubly_weak_alist_vector
0f2d19dd 155{
b6ed39c4 156 return scm_make_doubly_weak_hash_table (size);
0f2d19dd 157}
1bbd0b84 158#undef FUNC_NAME
0f2d19dd 159
592996c9 160
c35738c1 161SCM_DEFINE (scm_weak_key_alist_vector_p, "weak-key-alist-vector?", 1, 0, 0,
1e6808ea 162 (SCM obj),
c35738c1
MD
163 "@deffnx {Scheme Procedure} weak-value-alist-vector? obj\n"
164 "@deffnx {Scheme Procedure} doubly-weak-alist-vector? obj\n"
5352393c
MG
165 "Return @code{#t} if @var{obj} is the specified weak hash\n"
166 "table. Note that a doubly weak hash table is neither a weak key\n"
167 "nor a weak value hash table.")
c35738c1 168#define FUNC_NAME s_scm_weak_key_alist_vector_p
0f2d19dd 169{
6e708ef2 170 return scm_from_bool (SCM_I_WVECTP (obj) && SCM_IS_WHVEC (obj));
0f2d19dd 171}
1bbd0b84 172#undef FUNC_NAME
0f2d19dd
JB
173
174
c35738c1 175SCM_DEFINE (scm_weak_value_alist_vector_p, "weak-value-alist-vector?", 1, 0, 0,
1e6808ea
MG
176 (SCM obj),
177 "Return @code{#t} if @var{obj} is a weak value hash table.")
c35738c1 178#define FUNC_NAME s_scm_weak_value_alist_vector_p
0f2d19dd 179{
6e708ef2 180 return scm_from_bool (SCM_I_WVECTP (obj) && SCM_IS_WHVEC_V (obj));
0f2d19dd 181}
1bbd0b84 182#undef FUNC_NAME
0f2d19dd
JB
183
184
c35738c1 185SCM_DEFINE (scm_doubly_weak_alist_vector_p, "doubly-weak-alist-vector?", 1, 0, 0,
1e6808ea
MG
186 (SCM obj),
187 "Return @code{#t} if @var{obj} is a doubly weak hash table.")
c35738c1 188#define FUNC_NAME s_scm_doubly_weak_alist_vector_p
0f2d19dd 189{
6e708ef2 190 return scm_from_bool (SCM_I_WVECTP (obj) && SCM_IS_WHVEC_B (obj));
0f2d19dd 191}
1bbd0b84 192#undef FUNC_NAME
0f2d19dd 193
592996c9 194
592996c9 195
06c1d900 196\f
c35738c1
MD
197SCM
198scm_init_weaks_builtins ()
199{
200#include "libguile/weaks.x"
201 return SCM_UNSPECIFIED;
202}
203
0f2d19dd
JB
204void
205scm_init_weaks ()
0f2d19dd 206{
c35738c1
MD
207 scm_c_define_gsubr ("%init-weaks-builtins", 0, 0, 0,
208 scm_init_weaks_builtins);
0f2d19dd
JB
209}
210
89e00824
ML
211
212/*
213 Local Variables:
214 c-file-style: "gnu"
215 End:
216*/