Commit | Line | Data |
---|---|---|
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 GB |
41 | |
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
0f2d19dd | 45 | \f |
a0599745 MD |
46 | #include "libguile/_scm.h" |
47 | #include "libguile/vectors.h" | |
0f2d19dd | 48 | |
a0599745 MD |
49 | #include "libguile/validate.h" |
50 | #include "libguile/weaks.h" | |
0f2d19dd JB |
51 | \f |
52 | ||
53 | ||
54 | /* {Weak Vectors} | |
55 | */ | |
56 | ||
57 | ||
3b3b36dd | 58 | SCM_DEFINE (scm_make_weak_vector, "make-weak-vector", 1, 1, 0, |
1bbd0b84 | 59 | (SCM k, SCM fill), |
b380b885 MD |
60 | "Return a weak vector with @var{size} elements. If the optional\n" |
61 | "argument @var{fill} is given, all entries in the vector will be set to\n" | |
62 | "@var{fill}. The default value for @var{fill} is the empty list.") | |
1bbd0b84 | 63 | #define FUNC_NAME s_scm_make_weak_vector |
0f2d19dd | 64 | { |
bc0eaf7b | 65 | /* Dirk:FIXME:: We should probably rather use a double cell for weak vectors. */ |
0f2d19dd | 66 | SCM v; |
250da369 | 67 | v = scm_make_vector (scm_sum (k, SCM_MAKINUM (2)), fill); |
0f2d19dd | 68 | SCM_DEFER_INTS; |
bc0eaf7b | 69 | SCM_SET_VECTOR_LENGTH (v, SCM_INUM (k), scm_tc7_wvect); |
250da369 | 70 | SCM_SETVELTS(v, SCM_VELTS(v) + 2); |
9e882eec DH |
71 | SCM_VELTS(v)[-2] = SCM_EOL; |
72 | SCM_UNPACK (SCM_VELTS (v)[-1]) = 0; | |
0f2d19dd JB |
73 | SCM_ALLOW_INTS; |
74 | return v; | |
75 | } | |
1bbd0b84 | 76 | #undef FUNC_NAME |
0f2d19dd JB |
77 | |
78 | ||
1bbd0b84 | 79 | SCM_REGISTER_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector); |
1cc91f1b | 80 | |
3b3b36dd | 81 | SCM_DEFINE (scm_weak_vector, "weak-vector", 0, 0, 1, |
1bbd0b84 | 82 | (SCM l), |
b380b885 MD |
83 | "@deffnx primitive list->weak-vector l\n" |
84 | "Construct a weak vector from a list: @code{weak-vector} uses the list of\n" | |
85 | "its arguments while @code{list->weak-vector} uses its only argument\n" | |
86 | "@var{l} (a list) to construct a weak vector the same way\n" | |
87 | "@code{vector->list} would.") | |
1bbd0b84 | 88 | #define FUNC_NAME s_scm_weak_vector |
0f2d19dd JB |
89 | { |
90 | SCM res; | |
22a52da1 | 91 | SCM *data; |
0f2d19dd JB |
92 | long i; |
93 | ||
22a52da1 DH |
94 | /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted |
95 | while the vector is being created. */ | |
0f2d19dd | 96 | i = scm_ilength (l); |
1bbd0b84 | 97 | SCM_ASSERT (i >= 0, l, SCM_ARG1, FUNC_NAME); |
0f2d19dd JB |
98 | res = scm_make_weak_vector (SCM_MAKINUM (i), SCM_UNSPECIFIED); |
99 | data = SCM_VELTS (res); | |
22a52da1 DH |
100 | |
101 | while (!SCM_NULLP (l)) | |
102 | { | |
103 | *data++ = SCM_CAR (l); | |
104 | l = SCM_CDR (l); | |
105 | } | |
106 | ||
0f2d19dd JB |
107 | return res; |
108 | } | |
1bbd0b84 | 109 | #undef FUNC_NAME |
0f2d19dd JB |
110 | |
111 | ||
3b3b36dd | 112 | SCM_DEFINE (scm_weak_vector_p, "weak-vector?", 1, 0, 0, |
1bbd0b84 | 113 | (SCM x), |
5352393c MG |
114 | "Return @code{#t} if @var{obj} is a weak vector. Note that all\n" |
115 | "weak hashes are also weak vectors.") | |
1bbd0b84 | 116 | #define FUNC_NAME s_scm_weak_vector_p |
0f2d19dd | 117 | { |
0c95b57d | 118 | return SCM_BOOL(SCM_WVECTP (x) && !SCM_IS_WHVEC (x)); |
0f2d19dd | 119 | } |
1bbd0b84 | 120 | #undef FUNC_NAME |
0f2d19dd JB |
121 | |
122 | ||
123 | ||
124 | \f | |
125 | ||
126 | ||
127 | ||
3b3b36dd | 128 | SCM_DEFINE (scm_make_weak_key_hash_table, "make-weak-key-hash-table", 1, 0, 0, |
1bbd0b84 | 129 | (SCM k), |
b380b885 MD |
130 | "@deffnx primitive make-weak-value-hash-table size\n" |
131 | "@deffnx primitive make-doubly-weak-hash-table size\n" | |
132 | "Return a weak hash table with @var{size} buckets. As with any hash\n" | |
133 | "table, choosing a good size for the table requires some caution.\n\n" | |
134 | "You can modify weak hash tables in exactly the same way you would modify\n" | |
135 | "regular hash tables. (@pxref{Hash Tables})") | |
1bbd0b84 | 136 | #define FUNC_NAME s_scm_make_weak_key_hash_table |
0f2d19dd JB |
137 | { |
138 | SCM v; | |
3b3b36dd | 139 | SCM_VALIDATE_INUM (1,k); |
0f2d19dd | 140 | v = scm_make_weak_vector (k, SCM_EOL); |
44d3cb0d | 141 | SCM_DEFER_INTS; |
f1267706 | 142 | SCM_UNPACK (SCM_VELTS (v)[-1]) = 1; |
0f2d19dd JB |
143 | SCM_ALLOW_INTS; |
144 | return v; | |
145 | } | |
1bbd0b84 | 146 | #undef FUNC_NAME |
0f2d19dd JB |
147 | |
148 | ||
a1ec6916 | 149 | SCM_DEFINE (scm_make_weak_value_hash_table, "make-weak-value-hash-table", 1, 0, 0, |
1bbd0b84 | 150 | (SCM k), |
e3239868 DH |
151 | "Return a hash table with weak values with @var{size} buckets.\n" |
152 | "(@pxref{Hash Tables})") | |
1bbd0b84 | 153 | #define FUNC_NAME s_scm_make_weak_value_hash_table |
0f2d19dd JB |
154 | { |
155 | SCM v; | |
3b3b36dd | 156 | SCM_VALIDATE_INUM (1,k); |
0f2d19dd | 157 | v = scm_make_weak_vector (k, SCM_EOL); |
44d3cb0d | 158 | SCM_DEFER_INTS; |
f1267706 | 159 | SCM_UNPACK (SCM_VELTS (v)[-1]) = 2; |
0f2d19dd JB |
160 | SCM_ALLOW_INTS; |
161 | return v; | |
162 | } | |
1bbd0b84 | 163 | #undef FUNC_NAME |
0f2d19dd JB |
164 | |
165 | ||
166 | ||
a1ec6916 | 167 | SCM_DEFINE (scm_make_doubly_weak_hash_table, "make-doubly-weak-hash-table", 1, 0, 0, |
1bbd0b84 | 168 | (SCM k), |
e3239868 DH |
169 | "Return a hash table with weak keys and values with @var{size}\n" |
170 | "buckets. (@pxref{Hash Tables})") | |
1bbd0b84 | 171 | #define FUNC_NAME s_scm_make_doubly_weak_hash_table |
0f2d19dd JB |
172 | { |
173 | SCM v; | |
3b3b36dd | 174 | SCM_VALIDATE_INUM (1,k); |
0f2d19dd | 175 | v = scm_make_weak_vector (k, SCM_EOL); |
44d3cb0d | 176 | SCM_DEFER_INTS; |
f1267706 | 177 | SCM_UNPACK (SCM_VELTS (v)[-1]) = 3; |
0f2d19dd JB |
178 | SCM_ALLOW_INTS; |
179 | return v; | |
180 | } | |
1bbd0b84 | 181 | #undef FUNC_NAME |
0f2d19dd | 182 | |
3b3b36dd | 183 | SCM_DEFINE (scm_weak_key_hash_table_p, "weak-key-hash-table?", 1, 0, 0, |
1bbd0b84 | 184 | (SCM x), |
b380b885 MD |
185 | "@deffnx primitive weak-value-hash-table? obj\n" |
186 | "@deffnx primitive doubly-weak-hash-table? obj\n" | |
5352393c MG |
187 | "Return @code{#t} if @var{obj} is the specified weak hash\n" |
188 | "table. Note that a doubly weak hash table is neither a weak key\n" | |
189 | "nor a weak value hash table.") | |
1bbd0b84 | 190 | #define FUNC_NAME s_scm_weak_key_hash_table_p |
0f2d19dd | 191 | { |
0c95b57d | 192 | return SCM_BOOL(SCM_WVECTP (x) && SCM_IS_WHVEC(x)); |
0f2d19dd | 193 | } |
1bbd0b84 | 194 | #undef FUNC_NAME |
0f2d19dd JB |
195 | |
196 | ||
a1ec6916 | 197 | SCM_DEFINE (scm_weak_value_hash_table_p, "weak-value-hash-table?", 1, 0, 0, |
1bbd0b84 | 198 | (SCM x), |
5352393c | 199 | "Return @code{#t} if @var{x} is a weak value hash table.") |
1bbd0b84 | 200 | #define FUNC_NAME s_scm_weak_value_hash_table_p |
0f2d19dd | 201 | { |
0c95b57d | 202 | return SCM_BOOL(SCM_WVECTP (x) && SCM_IS_WHVEC_V(x)); |
0f2d19dd | 203 | } |
1bbd0b84 | 204 | #undef FUNC_NAME |
0f2d19dd JB |
205 | |
206 | ||
a1ec6916 | 207 | SCM_DEFINE (scm_doubly_weak_hash_table_p, "doubly-weak-hash-table?", 1, 0, 0, |
1bbd0b84 | 208 | (SCM x), |
5352393c | 209 | "Return @code{#t} if @var{x} is a doubly weak hash table.") |
1bbd0b84 | 210 | #define FUNC_NAME s_scm_doubly_weak_hash_table_p |
0f2d19dd | 211 | { |
0c95b57d | 212 | return SCM_BOOL(SCM_WVECTP (x) && SCM_IS_WHVEC_B (x)); |
0f2d19dd | 213 | } |
1bbd0b84 | 214 | #undef FUNC_NAME |
0f2d19dd | 215 | |
d662820a MD |
216 | static void * |
217 | scm_weak_vector_gc_init (void *dummy1, void *dummy2, void *dummy3) | |
218 | { | |
219 | scm_weak_vectors = SCM_EOL; | |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
224 | static void * | |
225 | scm_mark_weak_vector_spines (void *dummy1, void *dummy2, void *dummy3) | |
226 | { | |
227 | SCM w; | |
228 | ||
229 | for (w = scm_weak_vectors; !SCM_NULLP (w); w = SCM_WVECT_GC_CHAIN (w)) | |
230 | { | |
231 | if (SCM_IS_WHVEC_ANY (w)) | |
232 | { | |
233 | SCM *ptr; | |
234 | SCM obj; | |
235 | int j; | |
236 | int n; | |
237 | ||
238 | obj = w; | |
239 | ptr = SCM_VELTS (w); | |
bfa974f0 | 240 | n = SCM_VECTOR_LENGTH (w); |
d662820a MD |
241 | for (j = 0; j < n; ++j) |
242 | { | |
243 | SCM alist; | |
244 | ||
245 | alist = ptr[j]; | |
246 | while ( SCM_CONSP (alist) | |
247 | && !SCM_GCMARKP (alist) | |
248 | && SCM_CONSP (SCM_CAR (alist))) | |
249 | { | |
250 | SCM_SETGCMARK (alist); | |
251 | SCM_SETGCMARK (SCM_CAR (alist)); | |
fd336365 | 252 | alist = SCM_CDR (alist); |
d662820a MD |
253 | } |
254 | } | |
255 | } | |
256 | } | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | static void * | |
262 | scm_scan_weak_vectors (void *dummy1, void *dummy2, void *dummy3) | |
263 | { | |
264 | SCM *ptr, w; | |
265 | for (w = scm_weak_vectors; !SCM_NULLP (w); w = SCM_WVECT_GC_CHAIN (w)) | |
266 | { | |
267 | if (!SCM_IS_WHVEC_ANY (w)) | |
268 | { | |
269 | register long j, n; | |
270 | ||
271 | ptr = SCM_VELTS (w); | |
bfa974f0 | 272 | n = SCM_VECTOR_LENGTH (w); |
d662820a | 273 | for (j = 0; j < n; ++j) |
406c7d90 | 274 | if (SCM_FREE_CELL_P (ptr[j])) |
d662820a MD |
275 | ptr[j] = SCM_BOOL_F; |
276 | } | |
277 | else /* if (SCM_IS_WHVEC_ANY (scm_weak_vectors[i])) */ | |
278 | { | |
279 | SCM obj = w; | |
bfa974f0 | 280 | register long n = SCM_VECTOR_LENGTH (w); |
d662820a | 281 | register long j; |
d9dcd933 ML |
282 | int weak_keys = SCM_IS_WHVEC (obj) || SCM_IS_WHVEC_B (obj); |
283 | int weak_values = SCM_IS_WHVEC_V (obj) || SCM_IS_WHVEC_B (obj); | |
d662820a MD |
284 | |
285 | ptr = SCM_VELTS (w); | |
286 | ||
287 | for (j = 0; j < n; ++j) | |
288 | { | |
289 | SCM * fixup; | |
290 | SCM alist; | |
d662820a MD |
291 | |
292 | fixup = ptr + j; | |
293 | alist = *fixup; | |
294 | ||
295 | while ( SCM_CONSP (alist) | |
296 | && SCM_CONSP (SCM_CAR (alist))) | |
297 | { | |
298 | SCM key; | |
299 | SCM value; | |
300 | ||
301 | key = SCM_CAAR (alist); | |
302 | value = SCM_CDAR (alist); | |
406c7d90 DH |
303 | if ( (weak_keys && SCM_FREE_CELL_P (key)) |
304 | || (weak_values && SCM_FREE_CELL_P (value))) | |
d662820a MD |
305 | { |
306 | *fixup = SCM_CDR (alist); | |
307 | } | |
308 | else | |
309 | fixup = SCM_CDRLOC (alist); | |
310 | alist = SCM_CDR (alist); | |
311 | } | |
312 | } | |
313 | } | |
314 | } | |
315 | ||
316 | return 0; | |
317 | } | |
318 | ||
0f2d19dd JB |
319 | |
320 | \f | |
321 | ||
1cc91f1b | 322 | |
d662820a MD |
323 | void |
324 | scm_weaks_prehistory () | |
325 | { | |
326 | scm_c_hook_add (&scm_before_mark_c_hook, scm_weak_vector_gc_init, 0, 0); | |
327 | scm_c_hook_add (&scm_before_sweep_c_hook, scm_mark_weak_vector_spines, 0, 0); | |
328 | scm_c_hook_add (&scm_after_sweep_c_hook, scm_scan_weak_vectors, 0, 0); | |
329 | } | |
330 | ||
0f2d19dd JB |
331 | void |
332 | scm_init_weaks () | |
0f2d19dd | 333 | { |
8dc9439f | 334 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 335 | #include "libguile/weaks.x" |
8dc9439f | 336 | #endif |
0f2d19dd JB |
337 | } |
338 | ||
89e00824 ML |
339 | |
340 | /* | |
341 | Local Variables: | |
342 | c-file-style: "gnu" | |
343 | End: | |
344 | */ |