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