Commit | Line | Data |
---|---|---|
dbb605f5 | 1 | /* Copyright (C) 1998,1999,2000,2001, 2006, 2008 Free Software Foundation, Inc. |
e1f2bf99 | 2 | * |
73be1d9e MV |
3 | * This library is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
e1f2bf99 | 7 | * |
73be1d9e MV |
8 | * This library 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 GNU | |
11 | * Lesser General Public License for more details. | |
e1f2bf99 | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
92205699 | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
73be1d9e | 16 | */ |
1bbd0b84 | 17 | |
e1f2bf99 MD |
18 | \f |
19 | ||
20 | /* This is an implementation of guardians as described in | |
21 | * R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) "Guardians in | |
22 | * a Generation-Based Garbage Collector" ACM SIGPLAN Conference on | |
23 | * Programming Language Design and Implementation, June 1993 | |
24 | * ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/pubs/guardians.ps.gz | |
25 | * | |
06c1d900 MV |
26 | * Original design: Mikael Djurfeldt |
27 | * Original implementation: Michael Livshin | |
28 | * Hacked on since by: everybody | |
29 | * | |
56495472 ML |
30 | * By this point, the semantics are actually quite different from |
31 | * those described in the abovementioned paper. The semantic changes | |
32 | * are there to improve safety and intuitiveness. The interface is | |
33 | * still (mostly) the one described by the paper, however. | |
34 | * | |
06c1d900 MV |
35 | * Boiled down again: Marius Vollmer |
36 | * | |
37 | * Now they should again behave like those described in the paper. | |
38 | * Scheme guardians should be simple and friendly, not like the greedy | |
39 | * monsters we had... | |
e1f2bf99 MD |
40 | */ |
41 | ||
dbb605f5 LC |
42 | #ifdef HAVE_CONFIG_H |
43 | # include <config.h> | |
44 | #endif | |
e1f2bf99 | 45 | |
a0599745 | 46 | #include "libguile/_scm.h" |
4e047c3e | 47 | #include "libguile/async.h" |
a0599745 MD |
48 | #include "libguile/ports.h" |
49 | #include "libguile/print.h" | |
50 | #include "libguile/smob.h" | |
a0599745 | 51 | #include "libguile/validate.h" |
56495472 | 52 | #include "libguile/root.h" |
d9dcd933 ML |
53 | #include "libguile/hashtab.h" |
54 | #include "libguile/weaks.h" | |
06c1d900 MV |
55 | #include "libguile/deprecation.h" |
56 | #include "libguile/eval.h" | |
56495472 | 57 | |
a0599745 | 58 | #include "libguile/guardians.h" |
e1f2bf99 | 59 | |
e1f2bf99 MD |
60 | |
61 | /* The live and zombies FIFOs are implemented as tconcs as described | |
62 | in Dybvig's paper. This decouples addition and removal of elements | |
63 | so that no synchronization between these needs to take place. | |
64 | */ | |
01449aa5 | 65 | |
455c0ac8 | 66 | typedef struct t_tconc |
01449aa5 DH |
67 | { |
68 | SCM head; | |
69 | SCM tail; | |
455c0ac8 | 70 | } t_tconc; |
01449aa5 | 71 | |
bc36d050 | 72 | #define TCONC_EMPTYP(tc) (scm_is_eq ((tc).head, (tc).tail)) |
01449aa5 | 73 | |
e1f2bf99 | 74 | #define TCONC_IN(tc, obj, pair) \ |
d3a6bc94 | 75 | do { \ |
e1f2bf99 | 76 | SCM_SETCAR ((tc).tail, obj); \ |
2ff08405 DH |
77 | SCM_SET_CELL_OBJECT_1 (pair, SCM_EOL); \ |
78 | SCM_SET_CELL_OBJECT_0 (pair, SCM_BOOL_F); \ | |
e1f2bf99 MD |
79 | SCM_SETCDR ((tc).tail, pair); \ |
80 | (tc).tail = pair; \ | |
d3a6bc94 | 81 | } while (0) |
e1f2bf99 MD |
82 | |
83 | #define TCONC_OUT(tc, res) \ | |
d3a6bc94 | 84 | do { \ |
e1f2bf99 MD |
85 | (res) = SCM_CAR ((tc).head); \ |
86 | (tc).head = SCM_CDR ((tc).head); \ | |
d3a6bc94 | 87 | } while (0) |
e1f2bf99 | 88 | |
e1f2bf99 | 89 | |
92c2555f | 90 | static scm_t_bits tc16_guardian; |
e1f2bf99 | 91 | |
455c0ac8 | 92 | typedef struct t_guardian |
e1f2bf99 | 93 | { |
455c0ac8 DH |
94 | t_tconc live; |
95 | t_tconc zombies; | |
96 | struct t_guardian *next; | |
455c0ac8 | 97 | } t_guardian; |
e1f2bf99 | 98 | |
06c1d900 | 99 | #define GUARDIAN_P(x) SCM_SMOB_PREDICATE(tc16_guardian, x) |
455c0ac8 | 100 | #define GUARDIAN_DATA(x) ((t_guardian *) SCM_CELL_WORD_1 (x)) |
56495472 | 101 | |
06c1d900 | 102 | static t_guardian *guardians; |
c0a5d888 | 103 | |
06c1d900 MV |
104 | void |
105 | scm_i_init_guardians_for_gc () | |
106 | { | |
107 | guardians = NULL; | |
108 | } | |
56495472 | 109 | |
06c1d900 MV |
110 | /* mark a guardian by adding it to the live guardian list. */ |
111 | static SCM | |
112 | guardian_mark (SCM ptr) | |
113 | { | |
114 | t_guardian *g = GUARDIAN_DATA (ptr); | |
115 | g->next = guardians; | |
116 | guardians = g; | |
56495472 | 117 | |
06c1d900 MV |
118 | return SCM_BOOL_F; |
119 | } | |
56495472 | 120 | |
06c1d900 MV |
121 | /* Identify inaccessible objects and move them from the live list to |
122 | the zombie list. An object is inaccessible when it is unmarked at | |
123 | this point. Therefore, the inaccessible objects are not marked yet | |
124 | since that would prevent them from being recognized as | |
125 | inaccessible. | |
e1f2bf99 | 126 | |
06c1d900 MV |
127 | The pairs that form the life list itself are marked, tho. |
128 | */ | |
129 | void | |
130 | scm_i_identify_inaccessible_guardeds () | |
56495472 | 131 | { |
06c1d900 | 132 | t_guardian *g; |
e1f2bf99 | 133 | |
06c1d900 | 134 | for (g = guardians; g; g = g->next) |
56495472 | 135 | { |
06c1d900 MV |
136 | SCM pair, next_pair; |
137 | SCM *prev_ptr; | |
138 | ||
139 | for (pair = g->live.head, prev_ptr = &g->live.head; | |
140 | !scm_is_eq (pair, g->live.tail); | |
141 | pair = next_pair) | |
142 | { | |
143 | SCM obj = SCM_CAR (pair); | |
144 | next_pair = SCM_CDR (pair); | |
145 | if (!SCM_GC_MARK_P (obj)) | |
146 | { | |
147 | /* Unmarked, move to 'inaccessible' list. | |
148 | */ | |
149 | *prev_ptr = next_pair; | |
150 | TCONC_IN (g->zombies, obj, pair); | |
151 | } | |
152 | else | |
153 | { | |
154 | SCM_SET_GC_MARK (pair); | |
155 | prev_ptr = SCM_CDRLOC (pair); | |
156 | } | |
157 | } | |
158 | SCM_SET_GC_MARK (pair); | |
56495472 | 159 | } |
56495472 | 160 | } |
e46f3fa6 | 161 | |
06c1d900 MV |
162 | int |
163 | scm_i_mark_inaccessible_guardeds () | |
01449aa5 | 164 | { |
06c1d900 MV |
165 | t_guardian *g; |
166 | int again = 0; | |
01449aa5 | 167 | |
06c1d900 MV |
168 | /* We never need to see the guardians again that are processed here, |
169 | so we clear the list. Calling scm_gc_mark below might find new | |
170 | guardians, however (and other things), and we inform the GC about | |
171 | this by returning non-zero. See scm_mark_all in gc-mark.c | |
172 | */ | |
01449aa5 | 173 | |
06c1d900 MV |
174 | g = guardians; |
175 | guardians = NULL; | |
176 | ||
177 | for (; g; g = g->next) | |
178 | { | |
179 | SCM pair; | |
180 | ||
181 | for (pair = g->zombies.head; | |
182 | !scm_is_eq (pair, g->zombies.tail); | |
183 | pair = SCM_CDR (pair)) | |
184 | { | |
185 | if (!SCM_GC_MARK_P (pair)) | |
186 | { | |
187 | scm_gc_mark (SCM_CAR (pair)); | |
188 | SCM_SET_GC_MARK (pair); | |
189 | again = 1; | |
190 | } | |
191 | } | |
192 | SCM_SET_GC_MARK (pair); | |
193 | } | |
194 | return again; | |
195 | } | |
01449aa5 | 196 | |
1be6b49c | 197 | static size_t |
01449aa5 DH |
198 | guardian_free (SCM ptr) |
199 | { | |
4c9419ac MV |
200 | scm_gc_free (GUARDIAN_DATA (ptr), sizeof (t_guardian), "guardian"); |
201 | return 0; | |
01449aa5 DH |
202 | } |
203 | ||
01449aa5 | 204 | static int |
e81d98ec | 205 | guardian_print (SCM guardian, SCM port, scm_print_state *pstate SCM_UNUSED) |
01449aa5 | 206 | { |
455c0ac8 | 207 | t_guardian *g = GUARDIAN_DATA (guardian); |
c0a5d888 | 208 | |
06c1d900 | 209 | scm_puts ("#<guardian ", port); |
0345e278 | 210 | scm_uintprint ((scm_t_bits) g, 16, port); |
c0a5d888 | 211 | |
06c1d900 MV |
212 | scm_puts (" (reachable: ", port); |
213 | scm_display (scm_length (SCM_CDR (g->live.head)), port); | |
214 | scm_puts (" unreachable: ", port); | |
215 | scm_display (scm_length (SCM_CDR (g->zombies.head)), port); | |
216 | scm_puts (")", port); | |
c0a5d888 ML |
217 | |
218 | scm_puts (">", port); | |
01449aa5 DH |
219 | |
220 | return 1; | |
221 | } | |
222 | ||
06c1d900 MV |
223 | static void |
224 | scm_i_guard (SCM guardian, SCM obj) | |
e46f3fa6 | 225 | { |
455c0ac8 | 226 | t_guardian *g = GUARDIAN_DATA (guardian); |
c0a5d888 | 227 | |
01449aa5 | 228 | if (!SCM_IMP (obj)) |
e46f3fa6 GH |
229 | { |
230 | SCM z; | |
16d4699b | 231 | z = scm_cons (SCM_BOOL_F, SCM_BOOL_F); |
c0a5d888 | 232 | TCONC_IN (g->live, obj, z); |
e46f3fa6 GH |
233 | } |
234 | } | |
235 | ||
06c1d900 MV |
236 | static SCM |
237 | scm_i_get_one_zombie (SCM guardian) | |
e46f3fa6 | 238 | { |
455c0ac8 | 239 | t_guardian *g = GUARDIAN_DATA (guardian); |
e46f3fa6 GH |
240 | SCM res = SCM_BOOL_F; |
241 | ||
c0a5d888 ML |
242 | if (!TCONC_EMPTYP (g->zombies)) |
243 | TCONC_OUT (g->zombies, res); | |
56495472 | 244 | |
e46f3fa6 GH |
245 | return res; |
246 | } | |
e1f2bf99 | 247 | |
06c1d900 MV |
248 | /* This is the Scheme entry point for each guardian: If OBJ is an |
249 | * object, it's added to the guardian's live list. If OBJ is unbound, | |
250 | * the next available unreachable object (or #f if none) is returned. | |
251 | * | |
252 | * If the second optional argument THROW_P is true (the default), then | |
253 | * an error is raised if GUARDIAN is greedy and OBJ is already greedily | |
254 | * guarded. If THROW_P is false, #f is returned instead of raising the | |
255 | * error, and #t is returned if everything is fine. | |
256 | */ | |
257 | static SCM | |
258 | guardian_apply (SCM guardian, SCM obj, SCM throw_p) | |
259 | { | |
260 | #if ENABLE_DEPRECATED | |
261 | if (!SCM_UNBNDP (throw_p)) | |
262 | scm_c_issue_deprecation_warning | |
263 | ("Using the 'throw?' argument of a guardian is deprecated " | |
264 | "and ineffective."); | |
265 | #endif | |
01449aa5 | 266 | |
06c1d900 MV |
267 | if (!SCM_UNBNDP (obj)) |
268 | { | |
269 | scm_i_guard (guardian, obj); | |
270 | return SCM_UNSPECIFIED; | |
271 | } | |
272 | else | |
273 | return scm_i_get_one_zombie (guardian); | |
274 | } | |
275 | ||
276 | SCM_DEFINE (scm_make_guardian, "make-guardian", 0, 0, 0, | |
277 | (), | |
278 | "Create a new guardian. A guardian protects a set of objects from\n" | |
279 | "garbage collection, allowing a program to apply cleanup or other\n" | |
280 | "actions.\n" | |
281 | "\n" | |
282 | "@code{make-guardian} returns a procedure representing the guardian.\n" | |
283 | "Calling the guardian procedure with an argument adds the argument to\n" | |
284 | "the guardian's set of protected objects. Calling the guardian\n" | |
285 | "procedure without an argument returns one of the protected objects\n" | |
286 | "which are ready for garbage collection, or @code{#f} if no such object\n" | |
287 | "is available. Objects which are returned in this way are removed from\n" | |
288 | "the guardian.\n" | |
289 | "\n" | |
290 | "You can put a single object into a guardian more than once and you can\n" | |
291 | "put a single object into more than one guardian. The object will then\n" | |
292 | "be returned multiple times by the guardian procedures.\n" | |
293 | "\n" | |
294 | "An object is eligible to be returned from a guardian when it is no\n" | |
295 | "longer referenced from outside any guardian.\n" | |
296 | "\n" | |
297 | "There is no guarantee about the order in which objects are returned\n" | |
298 | "from a guardian. If you want to impose an order on finalization\n" | |
299 | "actions, for example, you can do that by keeping objects alive in some\n" | |
300 | "global data structure until they are no longer needed for finalizing\n" | |
301 | "other objects.\n" | |
302 | "\n" | |
303 | "Being an element in a weak vector, a key in a hash table with weak\n" | |
304 | "keys, or a value in a hash table with weak value does not prevent an\n" | |
305 | "object from being returned by a guardian. But as long as an object\n" | |
306 | "can be returned from a guardian it will not be removed from such a\n" | |
307 | "weak vector or hash table. In other words, a weak link does not\n" | |
308 | "prevent an object from being considered collectable, but being inside\n" | |
309 | "a guardian prevents a weak link from being broken.\n" | |
310 | "\n" | |
311 | "A key in a weak key hash table can be though of as having a strong\n" | |
312 | "reference to its associated value as long as the key is accessible.\n" | |
313 | "Consequently, when the key only accessible from within a guardian, the\n" | |
314 | "reference from the key to the value is also considered to be coming\n" | |
315 | "from within a guardian. Thus, if there is no other reference to the\n" | |
316 | "value, it is eligible to be returned from a guardian.\n") | |
1bbd0b84 | 317 | #define FUNC_NAME s_scm_make_guardian |
e1f2bf99 | 318 | { |
4c9419ac | 319 | t_guardian *g = scm_gc_malloc (sizeof (t_guardian), "guardian"); |
e46f3fa6 GH |
320 | SCM z1 = scm_cons (SCM_BOOL_F, SCM_EOL); |
321 | SCM z2 = scm_cons (SCM_BOOL_F, SCM_EOL); | |
e1f2bf99 | 322 | SCM z; |
e46f3fa6 | 323 | |
e1f2bf99 MD |
324 | /* A tconc starts out with one tail pair. */ |
325 | g->live.head = g->live.tail = z1; | |
326 | g->zombies.head = g->zombies.tail = z2; | |
56495472 | 327 | |
c0a5d888 | 328 | g->next = NULL; |
23a62151 | 329 | |
01449aa5 | 330 | SCM_NEWSMOB (z, tc16_guardian, g); |
e1f2bf99 | 331 | |
01449aa5 | 332 | return z; |
e1f2bf99 | 333 | } |
1bbd0b84 | 334 | #undef FUNC_NAME |
e1f2bf99 | 335 | |
e1f2bf99 | 336 | void |
56495472 | 337 | scm_init_guardians () |
e1f2bf99 | 338 | { |
01449aa5 DH |
339 | tc16_guardian = scm_make_smob_type ("guardian", 0); |
340 | scm_set_smob_mark (tc16_guardian, guardian_mark); | |
341 | scm_set_smob_free (tc16_guardian, guardian_free); | |
342 | scm_set_smob_print (tc16_guardian, guardian_print); | |
06c1d900 | 343 | #if ENABLE_DEPRECATED |
c0a5d888 | 344 | scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 2, 0); |
06c1d900 MV |
345 | #else |
346 | scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 1, 0); | |
347 | #endif | |
d9dcd933 | 348 | |
a0599745 | 349 | #include "libguile/guardians.x" |
e1f2bf99 | 350 | } |
89e00824 ML |
351 | |
352 | /* | |
353 | Local Variables: | |
354 | c-file-style: "gnu" | |
355 | End: | |
356 | */ |