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 | 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 |
74 | SCM |
75 | scm_i_allocate_weak_vector (scm_t_bits type, SCM size, SCM fill, const char* caller) | |
592996c9 DH |
76 | #define FUNC_NAME caller |
77 | { | |
e11e83f3 MV |
78 | size_t c_size; |
79 | SCM v; | |
80 | ||
81 | c_size = scm_to_unsigned_integer (size, 0, SCM_VECTOR_MAX_LENGTH); | |
82 | ||
83 | if (c_size > 0) | |
592996c9 | 84 | { |
e11e83f3 MV |
85 | scm_t_bits *base; |
86 | size_t j; | |
87 | ||
88 | if (SCM_UNBNDP (fill)) | |
89 | fill = SCM_UNSPECIFIED; | |
90 | ||
91 | base = scm_gc_malloc (c_size * sizeof (scm_t_bits), "weak vector"); | |
92 | for (j = 0; j != c_size; ++j) | |
93 | base[j] = SCM_UNPACK (fill); | |
94 | v = scm_double_cell (SCM_MAKE_VECTOR_TAG (c_size, scm_tc7_wvect), | |
95 | (scm_t_bits) base, | |
96 | type, | |
97 | SCM_UNPACK (SCM_EOL)); | |
98 | scm_remember_upto_here_1 (fill); | |
592996c9 | 99 | } |
592996c9 | 100 | else |
e11e83f3 MV |
101 | { |
102 | v = scm_double_cell (SCM_MAKE_VECTOR_TAG (0, scm_tc7_wvect), | |
103 | (scm_t_bits) NULL, | |
104 | type, | |
105 | SCM_UNPACK (SCM_EOL)); | |
106 | } | |
107 | ||
108 | return v; | |
592996c9 DH |
109 | } |
110 | #undef FUNC_NAME | |
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 | { |
c35738c1 | 120 | return scm_i_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); |
e11e83f3 | 144 | res = scm_make_weak_vector (scm_from_int (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 | { |
7888309b | 167 | return scm_from_bool (SCM_WVECTP (obj) && !SCM_IS_WHVEC (obj)); |
0f2d19dd | 168 | } |
1bbd0b84 | 169 | #undef FUNC_NAME |
0f2d19dd | 170 | |
0f2d19dd JB |
171 | \f |
172 | ||
c35738c1 | 173 | SCM_DEFINE (scm_make_weak_key_alist_vector, "make-weak-key-alist-vector", 0, 1, 0, |
1e6808ea | 174 | (SCM size), |
c35738c1 MD |
175 | "@deffnx {Scheme Procedure} make-weak-value-alist-vector size\n" |
176 | "@deffnx {Scheme Procedure} make-doubly-weak-alist-vector 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})") | |
c35738c1 | 183 | #define FUNC_NAME s_scm_make_weak_key_alist_vector |
0f2d19dd | 184 | { |
c35738c1 | 185 | return scm_i_allocate_weak_vector |
e11e83f3 | 186 | (1, SCM_UNBNDP (size) ? scm_from_int (31) : size, SCM_EOL, FUNC_NAME); |
0f2d19dd | 187 | } |
1bbd0b84 | 188 | #undef FUNC_NAME |
0f2d19dd JB |
189 | |
190 | ||
c35738c1 | 191 | SCM_DEFINE (scm_make_weak_value_alist_vector, "make-weak-value-alist-vector", 0, 1, 0, |
1e6808ea | 192 | (SCM size), |
e3239868 DH |
193 | "Return a hash table with weak values with @var{size} buckets.\n" |
194 | "(@pxref{Hash Tables})") | |
c35738c1 | 195 | #define FUNC_NAME s_scm_make_weak_value_alist_vector |
0f2d19dd | 196 | { |
c35738c1 | 197 | return scm_i_allocate_weak_vector |
e11e83f3 | 198 | (2, SCM_UNBNDP (size) ? scm_from_int (31) : size, SCM_EOL, FUNC_NAME); |
0f2d19dd | 199 | } |
1bbd0b84 | 200 | #undef FUNC_NAME |
0f2d19dd JB |
201 | |
202 | ||
c35738c1 | 203 | SCM_DEFINE (scm_make_doubly_weak_alist_vector, "make-doubly-weak-alist-vector", 1, 0, 0, |
1e6808ea | 204 | (SCM size), |
e3239868 DH |
205 | "Return a hash table with weak keys and values with @var{size}\n" |
206 | "buckets. (@pxref{Hash Tables})") | |
c35738c1 | 207 | #define FUNC_NAME s_scm_make_doubly_weak_alist_vector |
0f2d19dd | 208 | { |
c35738c1 | 209 | return scm_i_allocate_weak_vector |
e11e83f3 | 210 | (3, SCM_UNBNDP (size) ? scm_from_int (31) : size, SCM_EOL, FUNC_NAME); |
0f2d19dd | 211 | } |
1bbd0b84 | 212 | #undef FUNC_NAME |
0f2d19dd | 213 | |
592996c9 | 214 | |
c35738c1 | 215 | SCM_DEFINE (scm_weak_key_alist_vector_p, "weak-key-alist-vector?", 1, 0, 0, |
1e6808ea | 216 | (SCM obj), |
c35738c1 MD |
217 | "@deffnx {Scheme Procedure} weak-value-alist-vector? obj\n" |
218 | "@deffnx {Scheme Procedure} doubly-weak-alist-vector? obj\n" | |
5352393c MG |
219 | "Return @code{#t} if @var{obj} is the specified weak hash\n" |
220 | "table. Note that a doubly weak hash table is neither a weak key\n" | |
221 | "nor a weak value hash table.") | |
c35738c1 | 222 | #define FUNC_NAME s_scm_weak_key_alist_vector_p |
0f2d19dd | 223 | { |
7888309b | 224 | return scm_from_bool (SCM_WVECTP (obj) && SCM_IS_WHVEC (obj)); |
0f2d19dd | 225 | } |
1bbd0b84 | 226 | #undef FUNC_NAME |
0f2d19dd JB |
227 | |
228 | ||
c35738c1 | 229 | SCM_DEFINE (scm_weak_value_alist_vector_p, "weak-value-alist-vector?", 1, 0, 0, |
1e6808ea MG |
230 | (SCM obj), |
231 | "Return @code{#t} if @var{obj} is a weak value hash table.") | |
c35738c1 | 232 | #define FUNC_NAME s_scm_weak_value_alist_vector_p |
0f2d19dd | 233 | { |
7888309b | 234 | return scm_from_bool (SCM_WVECTP (obj) && SCM_IS_WHVEC_V (obj)); |
0f2d19dd | 235 | } |
1bbd0b84 | 236 | #undef FUNC_NAME |
0f2d19dd JB |
237 | |
238 | ||
c35738c1 | 239 | SCM_DEFINE (scm_doubly_weak_alist_vector_p, "doubly-weak-alist-vector?", 1, 0, 0, |
1e6808ea MG |
240 | (SCM obj), |
241 | "Return @code{#t} if @var{obj} is a doubly weak hash table.") | |
c35738c1 | 242 | #define FUNC_NAME s_scm_doubly_weak_alist_vector_p |
0f2d19dd | 243 | { |
7888309b | 244 | return scm_from_bool (SCM_WVECTP (obj) && SCM_IS_WHVEC_B (obj)); |
0f2d19dd | 245 | } |
1bbd0b84 | 246 | #undef FUNC_NAME |
0f2d19dd | 247 | |
592996c9 | 248 | |
d662820a | 249 | static void * |
e81d98ec DH |
250 | scm_weak_vector_gc_init (void *dummy1 SCM_UNUSED, |
251 | void *dummy2 SCM_UNUSED, | |
252 | void *dummy3 SCM_UNUSED) | |
d662820a MD |
253 | { |
254 | scm_weak_vectors = SCM_EOL; | |
255 | ||
256 | return 0; | |
257 | } | |
258 | ||
592996c9 | 259 | |
d662820a | 260 | static void * |
e81d98ec DH |
261 | scm_mark_weak_vector_spines (void *dummy1 SCM_UNUSED, |
262 | void *dummy2 SCM_UNUSED, | |
263 | void *dummy3 SCM_UNUSED) | |
d662820a MD |
264 | { |
265 | SCM w; | |
266 | ||
d2e53ed6 | 267 | for (w = scm_weak_vectors; !scm_is_null (w); w = SCM_WVECT_GC_CHAIN (w)) |
d662820a MD |
268 | { |
269 | if (SCM_IS_WHVEC_ANY (w)) | |
270 | { | |
34d19ef6 | 271 | SCM const *ptr; |
d662820a | 272 | SCM obj; |
c014a02e ML |
273 | long j; |
274 | long n; | |
d662820a MD |
275 | |
276 | obj = w; | |
277 | ptr = SCM_VELTS (w); | |
bfa974f0 | 278 | n = SCM_VECTOR_LENGTH (w); |
d662820a MD |
279 | for (j = 0; j < n; ++j) |
280 | { | |
281 | SCM alist; | |
282 | ||
283 | alist = ptr[j]; | |
d2e53ed6 | 284 | while ( scm_is_pair (alist) |
c8a1bdc4 | 285 | && !SCM_GC_MARK_P (alist) |
d2e53ed6 | 286 | && scm_is_pair (SCM_CAR (alist))) |
d662820a | 287 | { |
c8a1bdc4 HWN |
288 | SCM_SET_GC_MARK (alist); |
289 | SCM_SET_GC_MARK (SCM_CAR (alist)); | |
fd336365 | 290 | alist = SCM_CDR (alist); |
d662820a MD |
291 | } |
292 | } | |
293 | } | |
294 | } | |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
c8a1bdc4 | 299 | #define UNMARKED_CELL_P(x) (SCM_NIMP(x) && !SCM_GC_MARK_P (x)) |
592996c9 | 300 | |
d662820a | 301 | static void * |
e81d98ec DH |
302 | scm_scan_weak_vectors (void *dummy1 SCM_UNUSED, |
303 | void *dummy2 SCM_UNUSED, | |
304 | void *dummy3 SCM_UNUSED) | |
d662820a MD |
305 | { |
306 | SCM *ptr, w; | |
d2e53ed6 | 307 | for (w = scm_weak_vectors; !scm_is_null (w); w = SCM_WVECT_GC_CHAIN (w)) |
d662820a MD |
308 | { |
309 | if (!SCM_IS_WHVEC_ANY (w)) | |
310 | { | |
311 | register long j, n; | |
312 | ||
34d19ef6 | 313 | ptr = SCM_GC_WRITABLE_VELTS (w); |
bfa974f0 | 314 | n = SCM_VECTOR_LENGTH (w); |
d662820a | 315 | for (j = 0; j < n; ++j) |
c8a1bdc4 | 316 | if (UNMARKED_CELL_P (ptr[j])) |
d662820a MD |
317 | ptr[j] = SCM_BOOL_F; |
318 | } | |
c35738c1 MD |
319 | /* check if we should scan the alist vector here (hashtables |
320 | have their own scan function in hashtab.c). */ | |
321 | else if (!SCM_WVECT_NOSCAN_P (w)) | |
d662820a MD |
322 | { |
323 | SCM obj = w; | |
c014a02e ML |
324 | register long n = SCM_VECTOR_LENGTH (w); |
325 | register long j; | |
d9dcd933 ML |
326 | int weak_keys = SCM_IS_WHVEC (obj) || SCM_IS_WHVEC_B (obj); |
327 | int weak_values = SCM_IS_WHVEC_V (obj) || SCM_IS_WHVEC_B (obj); | |
d662820a | 328 | |
34d19ef6 | 329 | ptr = SCM_GC_WRITABLE_VELTS (w); |
d662820a MD |
330 | |
331 | for (j = 0; j < n; ++j) | |
332 | { | |
333 | SCM * fixup; | |
334 | SCM alist; | |
d662820a MD |
335 | |
336 | fixup = ptr + j; | |
337 | alist = *fixup; | |
338 | ||
d2e53ed6 MV |
339 | while (scm_is_pair (alist) |
340 | && scm_is_pair (SCM_CAR (alist))) | |
d662820a MD |
341 | { |
342 | SCM key; | |
343 | SCM value; | |
344 | ||
345 | key = SCM_CAAR (alist); | |
346 | value = SCM_CDAR (alist); | |
c8a1bdc4 HWN |
347 | if ( (weak_keys && UNMARKED_CELL_P (key)) |
348 | || (weak_values && UNMARKED_CELL_P (value))) | |
d662820a MD |
349 | { |
350 | *fixup = SCM_CDR (alist); | |
351 | } | |
352 | else | |
353 | fixup = SCM_CDRLOC (alist); | |
354 | alist = SCM_CDR (alist); | |
355 | } | |
356 | } | |
357 | } | |
358 | } | |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
0f2d19dd JB |
363 | \f |
364 | ||
d662820a MD |
365 | void |
366 | scm_weaks_prehistory () | |
367 | { | |
368 | scm_c_hook_add (&scm_before_mark_c_hook, scm_weak_vector_gc_init, 0, 0); | |
369 | scm_c_hook_add (&scm_before_sweep_c_hook, scm_mark_weak_vector_spines, 0, 0); | |
370 | scm_c_hook_add (&scm_after_sweep_c_hook, scm_scan_weak_vectors, 0, 0); | |
371 | } | |
372 | ||
592996c9 | 373 | |
c35738c1 MD |
374 | SCM |
375 | scm_init_weaks_builtins () | |
376 | { | |
377 | #include "libguile/weaks.x" | |
378 | return SCM_UNSPECIFIED; | |
379 | } | |
380 | ||
0f2d19dd JB |
381 | void |
382 | scm_init_weaks () | |
0f2d19dd | 383 | { |
c35738c1 MD |
384 | scm_c_define_gsubr ("%init-weaks-builtins", 0, 0, 0, |
385 | scm_init_weaks_builtins); | |
0f2d19dd JB |
386 | } |
387 | ||
89e00824 ML |
388 | |
389 | /* | |
390 | Local Variables: | |
391 | c-file-style: "gnu" | |
392 | End: | |
393 | */ |