49d7cbab4a17f30e2b469eae0e3be7bcc181e993
[bpt/guile.git] / libguile / guardians.c
1 /* Copyright (C) 1998,1999,2000,2001, 2006, 2008, 2009, 2011,
2 * 2012, 2013 Free Software Foundation, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19
20 \f
21 /* This is an implementation of guardians as described in
22 * R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) "Guardians in
23 * a Generation-Based Garbage Collector" ACM SIGPLAN Conference on
24 * Programming Language Design and Implementation, June 1993
25 * ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/pubs/guardians.ps.gz
26 *
27 * Original design: Mikael Djurfeldt
28 * Original implementation: Michael Livshin
29 * Hacked on since by: everybody
30 *
31 * By this point, the semantics are actually quite different from
32 * those described in the abovementioned paper. The semantic changes
33 * are there to improve safety and intuitiveness. The interface is
34 * still (mostly) the one described by the paper, however.
35 *
36 * Boiled down again: Marius Vollmer
37 *
38 * Now they should again behave like those described in the paper.
39 * Scheme guardians should be simple and friendly, not like the greedy
40 * monsters we had...
41 *
42 * Rewritten for the Boehm-Demers-Weiser GC by Ludovic Courtès.
43 * FIXME: This is currently not thread-safe.
44 */
45
46 /* Uncomment the following line to debug guardian finalization. */
47 /* #define DEBUG_GUARDIANS 1 */
48
49 #ifdef HAVE_CONFIG_H
50 # include <config.h>
51 #endif
52
53 #include "libguile/_scm.h"
54 #include "libguile/async.h"
55 #include "libguile/ports.h"
56 #include "libguile/print.h"
57 #include "libguile/smob.h"
58 #include "libguile/validate.h"
59 #include "libguile/root.h"
60 #include "libguile/hashtab.h"
61 #include "libguile/deprecation.h"
62 #include "libguile/eval.h"
63
64 #include "libguile/guardians.h"
65 #include "libguile/bdw-gc.h"
66
67
68
69
70 static scm_t_bits tc16_guardian;
71
72 typedef struct t_guardian
73 {
74 unsigned long live;
75 SCM zombies;
76 struct t_guardian *next;
77 } t_guardian;
78
79 #define GUARDIAN_P(x) SCM_SMOB_PREDICATE(tc16_guardian, x)
80 #define GUARDIAN_DATA(x) ((t_guardian *) SCM_SMOB_DATA_1 (x))
81
82
83
84
85 static int
86 guardian_print (SCM guardian, SCM port, scm_print_state *pstate SCM_UNUSED)
87 {
88 t_guardian *g = GUARDIAN_DATA (guardian);
89
90 scm_puts_unlocked ("#<guardian ", port);
91 scm_uintprint ((scm_t_bits) g, 16, port);
92
93 scm_puts_unlocked (" (reachable: ", port);
94 scm_display (scm_from_uint (g->live), port);
95 scm_puts_unlocked (" unreachable: ", port);
96 scm_display (scm_length (g->zombies), port);
97 scm_puts_unlocked (")", port);
98
99 scm_puts_unlocked (">", port);
100
101 return 1;
102 }
103
104 /* Handle finalization of OBJ which is guarded by the guardians listed in
105 GUARDIAN_LIST. */
106 static void
107 finalize_guarded (void *ptr, void *finalizer_data)
108 {
109 SCM cell_pool;
110 SCM obj, guardian_list, proxied_finalizer;
111
112 obj = SCM_PACK_POINTER (ptr);
113 guardian_list = SCM_CDR (SCM_PACK_POINTER (finalizer_data));
114 proxied_finalizer = SCM_CAR (SCM_PACK_POINTER (finalizer_data));
115
116 #ifdef DEBUG_GUARDIANS
117 printf ("finalizing guarded %p (%u guardians)\n",
118 ptr, scm_to_uint (scm_length (guardian_list)));
119 #endif
120
121 /* Preallocate a bunch of cells so that we can make sure that no garbage
122 collection (and, thus, nested calls to `finalize_guarded ()') occurs
123 while executing the following loop. This is quite inefficient (call to
124 `scm_length ()') but that shouldn't be a problem in most cases. */
125 cell_pool = scm_make_list (scm_length (guardian_list), SCM_UNSPECIFIED);
126
127 /* Tell each guardian interested in OBJ that OBJ is no longer
128 reachable. */
129 for (;
130 !scm_is_null (guardian_list);
131 guardian_list = SCM_CDR (guardian_list))
132 {
133 SCM zombies;
134 SCM guardian;
135 t_guardian *g;
136
137 guardian = scm_c_weak_vector_ref (scm_car (guardian_list), 0);
138
139 if (scm_is_false (guardian))
140 {
141 /* The guardian itself vanished in the meantime. */
142 #ifdef DEBUG_GUARDIANS
143 printf (" guardian for %p vanished\n", ptr);
144 #endif
145 continue;
146 }
147
148 g = GUARDIAN_DATA (guardian);
149 if (g->live == 0)
150 abort ();
151
152 /* Get a fresh cell from CELL_POOL. */
153 zombies = cell_pool;
154 cell_pool = SCM_CDR (cell_pool);
155
156 /* Compute and update G's zombie list. */
157 SCM_SETCAR (zombies, obj);
158 SCM_SETCDR (zombies, g->zombies);
159 g->zombies = zombies;
160
161 g->live--;
162 g->zombies = zombies;
163 }
164
165 if (scm_is_true (proxied_finalizer))
166 {
167 /* Re-register the finalizer that was in place before we installed this
168 one. */
169 GC_finalization_proc finalizer, prev_finalizer;
170 void *finalizer_data, *prev_finalizer_data;
171
172 finalizer = (GC_finalization_proc) SCM_UNPACK_POINTER (SCM_CAR (proxied_finalizer));
173 finalizer_data = SCM_UNPACK_POINTER (SCM_CDR (proxied_finalizer));
174
175 if (finalizer == NULL)
176 abort ();
177
178 GC_REGISTER_FINALIZER_NO_ORDER (ptr, finalizer, finalizer_data,
179 &prev_finalizer, &prev_finalizer_data);
180
181 #ifdef DEBUG_GUARDIANS
182 printf (" reinstalled proxied finalizer %p for %p\n", finalizer, ptr);
183 #endif
184 }
185
186 #ifdef DEBUG_GUARDIANS
187 printf ("end of finalize (%p)\n", ptr);
188 #endif
189 }
190
191 /* Add OBJ as a guarded object of GUARDIAN. */
192 static void
193 scm_i_guard (SCM guardian, SCM obj)
194 {
195 t_guardian *g = GUARDIAN_DATA (guardian);
196
197 if (SCM_HEAP_OBJECT_P (obj))
198 {
199 /* Register a finalizer and pass a pair as the ``client data''
200 argument. The pair contains in its car `#f' or a pair describing a
201 ``proxied'' finalizer (see below); its cdr contains a list of
202 guardians interested in OBJ.
203
204 A ``proxied'' finalizer is a finalizer that was registered for OBJ
205 before OBJ became guarded (e.g., a SMOB `free' function). We are
206 assuming here that finalizers are only used internally, either at
207 the very beginning of an object's lifetime (e.g., see `SCM_NEWSMOB')
208 or by this function. */
209 GC_finalization_proc prev_finalizer;
210 void *prev_data;
211 SCM guardians_for_obj, finalizer_data;
212
213 g->live++;
214
215 /* Note: GUARDIANS_FOR_OBJ holds weak references to guardians so
216 that a guardian can be collected before the objects it guards
217 (see `guardians.test'). */
218 guardians_for_obj = scm_cons (scm_make_weak_vector (SCM_INUM1, guardian),
219 SCM_EOL);
220 finalizer_data = scm_cons (SCM_BOOL_F, guardians_for_obj);
221
222 GC_REGISTER_FINALIZER_NO_ORDER (SCM_UNPACK_POINTER (obj), finalize_guarded,
223 SCM_UNPACK_POINTER (finalizer_data),
224 &prev_finalizer, &prev_data);
225
226 if (prev_finalizer == finalize_guarded)
227 {
228 /* OBJ is already guarded by another guardian: add GUARDIAN to its
229 list of guardians. */
230 SCM prev_guardian_list, prev_finalizer_data;
231
232 if (prev_data == NULL)
233 abort ();
234
235 prev_finalizer_data = SCM_PACK_POINTER (prev_data);
236 if (!scm_is_pair (prev_finalizer_data))
237 abort ();
238
239 prev_guardian_list = SCM_CDR (prev_finalizer_data);
240 SCM_SETCDR (guardians_for_obj, prev_guardian_list);
241
242 /* Also copy information about proxied finalizers. */
243 SCM_SETCAR (finalizer_data, SCM_CAR (prev_finalizer_data));
244 }
245 else if (prev_finalizer != NULL)
246 {
247 /* There was already a finalizer registered for OBJ so we will
248 ``proxy'' it, i.e., record it so that we can re-register it once
249 `finalize_guarded ()' has finished. */
250 SCM proxied_finalizer;
251
252 proxied_finalizer = scm_cons (SCM_PACK_POINTER (prev_finalizer),
253 SCM_PACK_POINTER (prev_data));
254 SCM_SETCAR (finalizer_data, proxied_finalizer);
255 }
256 }
257 }
258
259 static SCM
260 scm_i_get_one_zombie (SCM guardian)
261 {
262 t_guardian *g = GUARDIAN_DATA (guardian);
263 SCM res = SCM_BOOL_F;
264
265 if (!scm_is_null (g->zombies))
266 {
267 /* Note: We return zombies in reverse order. */
268 res = SCM_CAR (g->zombies);
269 g->zombies = SCM_CDR (g->zombies);
270 }
271
272 return res;
273 }
274
275 /* This is the Scheme entry point for each guardian: If OBJ is an
276 * object, it's added to the guardian's live list. If OBJ is unbound,
277 * the next available unreachable object (or #f if none) is returned.
278 *
279 * If the second optional argument THROW_P is true (the default), then
280 * an error is raised if GUARDIAN is greedy and OBJ is already greedily
281 * guarded. If THROW_P is false, #f is returned instead of raising the
282 * error, and #t is returned if everything is fine.
283 */
284 static SCM
285 guardian_apply (SCM guardian, SCM obj, SCM throw_p)
286 {
287 if (!SCM_UNBNDP (obj))
288 {
289 scm_i_guard (guardian, obj);
290 return SCM_UNSPECIFIED;
291 }
292 else
293 return scm_i_get_one_zombie (guardian);
294 }
295
296 SCM_DEFINE (scm_make_guardian, "make-guardian", 0, 0, 0,
297 (),
298 "Create a new guardian. A guardian protects a set of objects from\n"
299 "garbage collection, allowing a program to apply cleanup or other\n"
300 "actions.\n"
301 "\n"
302 "@code{make-guardian} returns a procedure representing the guardian.\n"
303 "Calling the guardian procedure with an argument adds the argument to\n"
304 "the guardian's set of protected objects. Calling the guardian\n"
305 "procedure without an argument returns one of the protected objects\n"
306 "which are ready for garbage collection, or @code{#f} if no such object\n"
307 "is available. Objects which are returned in this way are removed from\n"
308 "the guardian.\n"
309 "\n"
310 "You can put a single object into a guardian more than once and you can\n"
311 "put a single object into more than one guardian. The object will then\n"
312 "be returned multiple times by the guardian procedures.\n"
313 "\n"
314 "An object is eligible to be returned from a guardian when it is no\n"
315 "longer referenced from outside any guardian.\n"
316 "\n"
317 "There is no guarantee about the order in which objects are returned\n"
318 "from a guardian. If you want to impose an order on finalization\n"
319 "actions, for example, you can do that by keeping objects alive in some\n"
320 "global data structure until they are no longer needed for finalizing\n"
321 "other objects.\n"
322 "\n"
323 "Being an element in a weak vector, a key in a hash table with weak\n"
324 "keys, or a value in a hash table with weak value does not prevent an\n"
325 "object from being returned by a guardian. But as long as an object\n"
326 "can be returned from a guardian it will not be removed from such a\n"
327 "weak vector or hash table. In other words, a weak link does not\n"
328 "prevent an object from being considered collectable, but being inside\n"
329 "a guardian prevents a weak link from being broken.\n"
330 "\n"
331 "A key in a weak key hash table can be though of as having a strong\n"
332 "reference to its associated value as long as the key is accessible.\n"
333 "Consequently, when the key only accessible from within a guardian, the\n"
334 "reference from the key to the value is also considered to be coming\n"
335 "from within a guardian. Thus, if there is no other reference to the\n"
336 "value, it is eligible to be returned from a guardian.\n")
337 #define FUNC_NAME s_scm_make_guardian
338 {
339 t_guardian *g = scm_gc_malloc (sizeof (t_guardian), "guardian");
340 SCM z;
341
342 /* A tconc starts out with one tail pair. */
343 g->live = 0;
344 g->zombies = SCM_EOL;
345
346 g->next = NULL;
347
348 SCM_NEWSMOB (z, tc16_guardian, g);
349
350 return z;
351 }
352 #undef FUNC_NAME
353
354 void
355 scm_init_guardians ()
356 {
357 /* We use unordered finalization `a la Java. */
358 #ifdef HAVE_GC_SET_JAVA_FINALIZATION
359 /* This function was added in 7.2alpha2 (June 2009). */
360 GC_set_java_finalization (1);
361 #else
362 /* This symbol is deprecated as of 7.3. */
363 GC_java_finalization = 1;
364 #endif
365
366 tc16_guardian = scm_make_smob_type ("guardian", 0);
367
368 scm_set_smob_print (tc16_guardian, guardian_print);
369 scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 1, 0);
370
371 #include "libguile/guardians.x"
372 }
373
374 /*
375 Local Variables:
376 c-file-style: "gnu"
377 End:
378 */