Commit | Line | Data |
---|---|---|
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 JB |
54 | |
55 | /* {Weak Vectors} | |
56 | */ | |
57 | ||
58 | ||
592996c9 DH |
59 | /* Allocate memory for a weak vector on behalf of the caller. The allocated |
60 | * vector will be of the given weak vector subtype. It will contain size | |
61 | * elements which are initialized with the 'fill' object, or, if 'fill' is | |
62 | * undefined, with an unspecified object. | |
63 | */ | |
64 | static SCM | |
65 | allocate_weak_vector (scm_t_bits type, SCM size, SCM fill, const char* caller) | |
66 | #define FUNC_NAME caller | |
67 | { | |
68 | if (SCM_INUMP (size)) | |
69 | { | |
70 | size_t c_size; | |
71 | SCM v; | |
72 | ||
73 | SCM_ASSERT_RANGE (1, size, SCM_INUM (size) >= 0); | |
74 | c_size = SCM_INUM (size); | |
75 | ||
592996c9 DH |
76 | if (c_size > 0) |
77 | { | |
78 | scm_t_bits *base; | |
79 | size_t j; | |
80 | ||
81 | if (SCM_UNBNDP (fill)) | |
82 | fill = SCM_UNSPECIFIED; | |
83 | ||
84 | SCM_ASSERT_RANGE (1, size, c_size <= SCM_VECTOR_MAX_LENGTH); | |
4c9419ac | 85 | base = scm_gc_malloc (c_size * sizeof (scm_t_bits), "weak vector"); |
592996c9 DH |
86 | for (j = 0; j != c_size; ++j) |
87 | base[j] = SCM_UNPACK (fill); | |
228a24ef DH |
88 | v = scm_double_cell (SCM_MAKE_VECTOR_TAG (c_size, scm_tc7_wvect), |
89 | (scm_t_bits) base, | |
90 | type, | |
91 | SCM_UNPACK (SCM_EOL)); | |
592996c9 DH |
92 | scm_remember_upto_here_1 (fill); |
93 | } | |
94 | else | |
95 | { | |
228a24ef DH |
96 | v = scm_double_cell (SCM_MAKE_VECTOR_TAG (0, scm_tc7_wvect), |
97 | (scm_t_bits) NULL, | |
98 | type, | |
99 | SCM_UNPACK (SCM_EOL)); | |
592996c9 DH |
100 | } |
101 | ||
102 | return v; | |
103 | } | |
104 | else if (SCM_BIGP (size)) | |
105 | SCM_OUT_OF_RANGE (1, size); | |
106 | else | |
107 | SCM_WRONG_TYPE_ARG (1, size); | |
108 | } | |
109 | #undef FUNC_NAME | |
110 | ||
111 | ||
3b3b36dd | 112 | SCM_DEFINE (scm_make_weak_vector, "make-weak-vector", 1, 1, 0, |
1e6808ea | 113 | (SCM size, SCM fill), |
b380b885 | 114 | "Return a weak vector with @var{size} elements. If the optional\n" |
1e6808ea MG |
115 | "argument @var{fill} is given, all entries in the vector will be\n" |
116 | "set to @var{fill}. The default value for @var{fill} is the\n" | |
117 | "empty list.") | |
1bbd0b84 | 118 | #define FUNC_NAME s_scm_make_weak_vector |
0f2d19dd | 119 | { |
592996c9 | 120 | return allocate_weak_vector (0, size, fill, FUNC_NAME); |
0f2d19dd | 121 | } |
1bbd0b84 | 122 | #undef FUNC_NAME |
0f2d19dd JB |
123 | |
124 | ||
1bbd0b84 | 125 | SCM_REGISTER_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector); |
1cc91f1b | 126 | |
3b3b36dd | 127 | SCM_DEFINE (scm_weak_vector, "weak-vector", 0, 0, 1, |
1bbd0b84 | 128 | (SCM l), |
8f85c0c6 | 129 | "@deffnx {Scheme Procedure} list->weak-vector l\n" |
1e6808ea MG |
130 | "Construct a weak vector from a list: @code{weak-vector} uses\n" |
131 | "the list of its arguments while @code{list->weak-vector} uses\n" | |
132 | "its only argument @var{l} (a list) to construct a weak vector\n" | |
133 | "the same way @code{list->vector} would.") | |
1bbd0b84 | 134 | #define FUNC_NAME s_scm_weak_vector |
0f2d19dd JB |
135 | { |
136 | SCM res; | |
22a52da1 | 137 | SCM *data; |
c014a02e | 138 | long i; |
0f2d19dd | 139 | |
22a52da1 DH |
140 | /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted |
141 | while the vector is being created. */ | |
0f2d19dd | 142 | i = scm_ilength (l); |
1bbd0b84 | 143 | SCM_ASSERT (i >= 0, l, SCM_ARG1, FUNC_NAME); |
0f2d19dd | 144 | res = scm_make_weak_vector (SCM_MAKINUM (i), SCM_UNSPECIFIED); |
22a52da1 | 145 | |
34d19ef6 HWN |
146 | /* |
147 | no alloc, so this loop is safe. | |
148 | */ | |
149 | data = SCM_WRITABLE_VELTS (res); | |
c96d76b8 | 150 | while (!SCM_NULL_OR_NIL_P (l)) |
22a52da1 DH |
151 | { |
152 | *data++ = SCM_CAR (l); | |
153 | l = SCM_CDR (l); | |
154 | } | |
155 | ||
0f2d19dd JB |
156 | return res; |
157 | } | |
1bbd0b84 | 158 | #undef FUNC_NAME |
0f2d19dd JB |
159 | |
160 | ||
3b3b36dd | 161 | SCM_DEFINE (scm_weak_vector_p, "weak-vector?", 1, 0, 0, |
1e6808ea | 162 | (SCM obj), |
5352393c MG |
163 | "Return @code{#t} if @var{obj} is a weak vector. Note that all\n" |
164 | "weak hashes are also weak vectors.") | |
1bbd0b84 | 165 | #define FUNC_NAME s_scm_weak_vector_p |
0f2d19dd | 166 | { |
592996c9 | 167 | return SCM_BOOL (SCM_WVECTP (obj) && !SCM_IS_WHVEC (obj)); |
0f2d19dd | 168 | } |
1bbd0b84 | 169 | #undef FUNC_NAME |
0f2d19dd | 170 | |
0f2d19dd JB |
171 | \f |
172 | ||
f59a096e | 173 | SCM_DEFINE (scm_make_weak_key_hash_table, "make-weak-key-hash-table", 0, 1, 0, |
1e6808ea | 174 | (SCM size), |
8f85c0c6 NJ |
175 | "@deffnx {Scheme Procedure} make-weak-value-hash-table size\n" |
176 | "@deffnx {Scheme Procedure} make-doubly-weak-hash-table size\n" | |
1e6808ea MG |
177 | "Return a weak hash table with @var{size} buckets. As with any\n" |
178 | "hash table, choosing a good size for the table requires some\n" | |
179 | "caution.\n" | |
180 | "\n" | |
181 | "You can modify weak hash tables in exactly the same way you\n" | |
182 | "would modify regular hash tables. (@pxref{Hash Tables})") | |
1bbd0b84 | 183 | #define FUNC_NAME s_scm_make_weak_key_hash_table |
0f2d19dd | 184 | { |
f59a096e | 185 | if (SCM_UNBNDP (size)) |
0a4c1355 | 186 | return scm_vector_to_hash_table (allocate_weak_vector (1, SCM_MAKINUM (31), |
f59a096e MD |
187 | SCM_EOL, FUNC_NAME)); |
188 | else | |
189 | return allocate_weak_vector (1, size, SCM_EOL, FUNC_NAME); | |
0f2d19dd | 190 | } |
1bbd0b84 | 191 | #undef FUNC_NAME |
0f2d19dd JB |
192 | |
193 | ||
f59a096e | 194 | SCM_DEFINE (scm_make_weak_value_hash_table, "make-weak-value-hash-table", 0, 1, 0, |
1e6808ea | 195 | (SCM size), |
e3239868 DH |
196 | "Return a hash table with weak values with @var{size} buckets.\n" |
197 | "(@pxref{Hash Tables})") | |
1bbd0b84 | 198 | #define FUNC_NAME s_scm_make_weak_value_hash_table |
0f2d19dd | 199 | { |
f59a096e | 200 | if (SCM_UNBNDP (size)) |
0a4c1355 | 201 | return scm_vector_to_hash_table (allocate_weak_vector (2, SCM_MAKINUM (31), |
f59a096e MD |
202 | SCM_EOL, FUNC_NAME)); |
203 | else | |
204 | return allocate_weak_vector (2, size, SCM_EOL, FUNC_NAME); | |
0f2d19dd | 205 | } |
1bbd0b84 | 206 | #undef FUNC_NAME |
0f2d19dd JB |
207 | |
208 | ||
a1ec6916 | 209 | SCM_DEFINE (scm_make_doubly_weak_hash_table, "make-doubly-weak-hash-table", 1, 0, 0, |
1e6808ea | 210 | (SCM size), |
e3239868 DH |
211 | "Return a hash table with weak keys and values with @var{size}\n" |
212 | "buckets. (@pxref{Hash Tables})") | |
1bbd0b84 | 213 | #define FUNC_NAME s_scm_make_doubly_weak_hash_table |
0f2d19dd | 214 | { |
f59a096e | 215 | if (SCM_UNBNDP (size)) |
0a4c1355 | 216 | return scm_vector_to_hash_table (allocate_weak_vector (3, SCM_MAKINUM (31), |
f59a096e MD |
217 | SCM_EOL, FUNC_NAME)); |
218 | else | |
219 | return allocate_weak_vector (3, size, SCM_EOL, FUNC_NAME); | |
0f2d19dd | 220 | } |
1bbd0b84 | 221 | #undef FUNC_NAME |
0f2d19dd | 222 | |
592996c9 | 223 | |
3b3b36dd | 224 | SCM_DEFINE (scm_weak_key_hash_table_p, "weak-key-hash-table?", 1, 0, 0, |
1e6808ea | 225 | (SCM obj), |
8f85c0c6 NJ |
226 | "@deffnx {Scheme Procedure} weak-value-hash-table? obj\n" |
227 | "@deffnx {Scheme Procedure} doubly-weak-hash-table? 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.") | |
1bbd0b84 | 231 | #define FUNC_NAME s_scm_weak_key_hash_table_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 | ||
a1ec6916 | 238 | SCM_DEFINE (scm_weak_value_hash_table_p, "weak-value-hash-table?", 1, 0, 0, |
1e6808ea MG |
239 | (SCM obj), |
240 | "Return @code{#t} if @var{obj} is a weak value hash table.") | |
1bbd0b84 | 241 | #define FUNC_NAME s_scm_weak_value_hash_table_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 | ||
a1ec6916 | 248 | SCM_DEFINE (scm_doubly_weak_hash_table_p, "doubly-weak-hash-table?", 1, 0, 0, |
1e6808ea MG |
249 | (SCM obj), |
250 | "Return @code{#t} if @var{obj} is a doubly weak hash table.") | |
1bbd0b84 | 251 | #define FUNC_NAME s_scm_doubly_weak_hash_table_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 | 258 | static void * |
e81d98ec DH |
259 | scm_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 | 269 | static void * |
e81d98ec DH |
270 | scm_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 | 310 | static void * |
e81d98ec DH |
311 | scm_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 | } | |
328 | else /* if (SCM_IS_WHVEC_ANY (scm_weak_vectors[i])) */ | |
329 | { | |
330 | SCM obj = w; | |
c014a02e ML |
331 | register long n = SCM_VECTOR_LENGTH (w); |
332 | register long j; | |
d9dcd933 ML |
333 | int weak_keys = SCM_IS_WHVEC (obj) || SCM_IS_WHVEC_B (obj); |
334 | int weak_values = SCM_IS_WHVEC_V (obj) || SCM_IS_WHVEC_B (obj); | |
d662820a | 335 | |
34d19ef6 | 336 | ptr = SCM_GC_WRITABLE_VELTS (w); |
d662820a MD |
337 | |
338 | for (j = 0; j < n; ++j) | |
339 | { | |
340 | SCM * fixup; | |
341 | SCM alist; | |
d662820a MD |
342 | |
343 | fixup = ptr + j; | |
344 | alist = *fixup; | |
345 | ||
c8a1bdc4 HWN |
346 | while (SCM_CONSP (alist) |
347 | && SCM_CONSP (SCM_CAR (alist))) | |
d662820a MD |
348 | { |
349 | SCM key; | |
350 | SCM value; | |
351 | ||
352 | key = SCM_CAAR (alist); | |
353 | value = SCM_CDAR (alist); | |
c8a1bdc4 HWN |
354 | if ( (weak_keys && UNMARKED_CELL_P (key)) |
355 | || (weak_values && UNMARKED_CELL_P (value))) | |
d662820a MD |
356 | { |
357 | *fixup = SCM_CDR (alist); | |
358 | } | |
359 | else | |
360 | fixup = SCM_CDRLOC (alist); | |
361 | alist = SCM_CDR (alist); | |
362 | } | |
363 | } | |
364 | } | |
365 | } | |
366 | ||
367 | return 0; | |
368 | } | |
369 | ||
0f2d19dd JB |
370 | \f |
371 | ||
d662820a MD |
372 | void |
373 | scm_weaks_prehistory () | |
374 | { | |
375 | scm_c_hook_add (&scm_before_mark_c_hook, scm_weak_vector_gc_init, 0, 0); | |
376 | scm_c_hook_add (&scm_before_sweep_c_hook, scm_mark_weak_vector_spines, 0, 0); | |
377 | scm_c_hook_add (&scm_after_sweep_c_hook, scm_scan_weak_vectors, 0, 0); | |
378 | } | |
379 | ||
592996c9 | 380 | |
0f2d19dd JB |
381 | void |
382 | scm_init_weaks () | |
0f2d19dd | 383 | { |
a0599745 | 384 | #include "libguile/weaks.x" |
0f2d19dd JB |
385 | } |
386 | ||
89e00824 ML |
387 | |
388 | /* | |
389 | Local Variables: | |
390 | c-file-style: "gnu" | |
391 | End: | |
392 | */ |