1 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
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)
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.
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
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE 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 the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, 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 them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
47 /* This is an implementation of guardians as described in
48 * R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) "Guardians in
49 * a Generation-Based Garbage Collector" ACM SIGPLAN Conference on
50 * Programming Language Design and Implementation, June 1993
51 * ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/pubs/guardians.ps.gz
53 * By this point, the semantics are actually quite different from
54 * those described in the abovementioned paper. The semantic changes
55 * are there to improve safety and intuitiveness. The interface is
56 * still (mostly) the one described by the paper, however.
58 * Original design: Mikael Djurfeldt
59 * Original implementation: Michael Livshin
60 * Hacked on since by: everybody
64 #include "libguile/_scm.h"
65 #include "libguile/ports.h"
66 #include "libguile/print.h"
67 #include "libguile/smob.h"
68 #include "libguile/validate.h"
69 #include "libguile/properties.h"
70 #include "libguile/root.h"
72 #include "libguile/guardians.h"
75 /* The live and zombies FIFOs are implemented as tconcs as described
76 in Dybvig's paper. This decouples addition and removal of elements
77 so that no synchronization between these needs to take place.
80 typedef struct tconc_t
86 #define TCONC_EMPTYP(tc) (SCM_EQ_P ((tc).head, (tc).tail))
88 #define TCONC_IN(tc, obj, pair) \
90 SCM_SETCAR ((tc).tail, obj); \
91 SCM_SETCAR (pair, SCM_BOOL_F); \
92 SCM_SETCDR (pair, SCM_EOL); \
93 SCM_SETCDR ((tc).tail, pair); \
97 #define TCONC_OUT(tc, res) \
99 (res) = SCM_CAR ((tc).head); \
100 (tc).head = SCM_CDR ((tc).head); \
104 static scm_bits_t tc16_guardian
;
106 typedef struct guardian_t
110 struct guardian_t
*next
;
115 #define GUARDIAN_P(x) SCM_SMOB_PREDICATE(tc16_guardian, x)
116 #define GUARDIAN(x) ((guardian_t *) SCM_CELL_WORD_1 (x))
117 #define GUARDIAN_LIVE(x) (GUARDIAN (x)->live)
118 #define GUARDIAN_ZOMBIES(x) (GUARDIAN (x)->zombies)
119 #define GUARDIAN_NEXT(x) (GUARDIAN (x)->next)
120 #define GUARDIAN_GREEDY_P(x) (GUARDIAN (x)->greedy_p)
121 #define GUARDIAN_LISTED_P(x) (GUARDIAN (x)->listed_p)
124 /* during the gc mark phase, live guardians are linked into the lists
126 static guardian_t
*greedy_guardians
= NULL
;
127 static guardian_t
*sharing_guardians
= NULL
;
129 /* greedily guarded objects have this property set, so that we can
130 catch any attempt to greedily guard them again */
131 static SCM greedily_guarded_prop
= SCM_EOL
;
133 /* this is the list of guarded objects that are parts of cycles. we
134 don't know in which order to return them from guardians, so we just
135 unguard them and whine about it in after-gc-hook */
136 static SCM self_centered_zombies
= SCM_EOL
;
140 add_to_live_list (SCM g
)
142 if (GUARDIAN_LISTED_P (g
))
145 if (GUARDIAN_GREEDY_P (g
))
147 GUARDIAN_NEXT (g
) = greedy_guardians
;
148 greedy_guardians
= GUARDIAN (g
);
152 GUARDIAN_NEXT (g
) = sharing_guardians
;
153 sharing_guardians
= GUARDIAN (g
);
156 GUARDIAN_LISTED_P (g
) = 1;
159 /* mark a guardian by adding it to the live guardian list. */
161 guardian_mark (SCM ptr
)
163 add_to_live_list (ptr
);
165 /* the objects protected by the guardian are not marked here: that
166 would prevent them from ever getting collected. instead marking
167 is done at the end of the mark phase by guardian_zombify. */
173 guardian_free (SCM ptr
)
175 scm_must_free (GUARDIAN (ptr
));
176 return sizeof (guardian_t
);
181 guardian_print (SCM g
, SCM port
, scm_print_state
*pstate
)
183 scm_puts ("#<", port
);
184 if (GUARDIAN_GREEDY_P (g
))
185 scm_puts ("greedy ", port
);
186 scm_puts ("guardian (reachable: ", port
);
187 scm_display (scm_length (SCM_CDR (GUARDIAN_LIVE (g
).head
)), port
);
188 scm_puts (" unreachable: ", port
);
189 scm_display (scm_length (SCM_CDR (GUARDIAN_ZOMBIES (g
).head
)), port
);
190 scm_puts (")>", port
);
196 /* This is the Scheme entry point for each guardian: If arg is an object, it's
197 * added to the guardian's live list. If arg is unbound, the next available
198 * zombified object (or #f if none) is returned.
201 guardian_apply (SCM guardian
, SCM arg
)
203 if (!SCM_UNBNDP (arg
))
205 scm_guard (guardian
, arg
);
206 return SCM_UNSPECIFIED
;
209 return scm_get_one_zombie (guardian
);
214 scm_guard (SCM guardian
, SCM obj
)
220 if (GUARDIAN_GREEDY_P (guardian
))
222 if (SCM_NFALSEP (scm_primitive_property_ref
223 (greedily_guarded_prop
, obj
)))
224 scm_misc_error ("guard",
225 "object is already greedily guarded", obj
);
227 scm_primitive_property_set_x (greedily_guarded_prop
,
233 /* This critical section barrier will be replaced by a mutex. */
235 TCONC_IN (GUARDIAN_LIVE (guardian
), obj
, z
);
242 scm_get_one_zombie (SCM guardian
)
244 SCM res
= SCM_BOOL_F
;
246 /* This critical section barrier will be replaced by a mutex. */
248 if (!TCONC_EMPTYP (GUARDIAN_ZOMBIES (guardian
)))
249 TCONC_OUT (GUARDIAN_ZOMBIES (guardian
), res
);
252 if (SCM_NFALSEP (res
)
253 && GUARDIAN_GREEDY_P (guardian
)
254 && SCM_NFALSEP (scm_primitive_property_ref
255 (greedily_guarded_prop
, res
)))
256 scm_primitive_property_del_x (greedily_guarded_prop
, res
);
262 SCM_DEFINE (scm_make_guardian
, "make-guardian", 0, 1, 0,
264 "Create a new guardian.\n"
265 "A guardian protects a set of objects from garbage collection,\n"
266 "allowing a program to apply cleanup or other actions.\n\n"
268 "make-guardian returns a procedure representing the guardian.\n"
269 "Calling the guardian procedure with an argument adds the\n"
270 "argument to the guardian's set of protected objects.\n"
271 "Calling the guardian procedure without an argument returns\n"
272 "one of the protected objects which are ready for garbage\n"
273 "collection or @code{#f} if no such object is available.\n"
274 "Objects which are returned in this way are removed from\n"
277 "make-guardian takes one optional argument that says whether the\n"
278 "new guardian should be greedy or not. if there is any chance\n"
279 "that any object protected by the guardian may be resurrected,\n"
280 "then make the guardian greedy (this is the default).\n\n"
282 "See R. Kent Dybvig, Carl Bruggeman, and David Eby (1993)\n"
283 "\"Guardians in a Generation-Based Garbage Collector\".\n"
284 "ACM SIGPLAN Conference on Programming Language Design\n"
285 "and Implementation, June 1993.\n\n"
287 "(the semantics are slightly different at this point, but the\n"
288 "paper still (mostly) accurately describes the interface).")
289 #define FUNC_NAME s_scm_make_guardian
291 guardian_t
*g
= SCM_MUST_MALLOC_TYPE (guardian_t
);
292 SCM z1
= scm_cons (SCM_BOOL_F
, SCM_EOL
);
293 SCM z2
= scm_cons (SCM_BOOL_F
, SCM_EOL
);
296 /* A tconc starts out with one tail pair. */
297 g
->live
.head
= g
->live
.tail
= z1
;
298 g
->zombies
.head
= g
->zombies
.tail
= z2
;
301 if (SCM_UNBNDP (greedy_p
))
304 g
->greedy_p
= SCM_NFALSEP (greedy_p
);
306 SCM_NEWSMOB (z
, tc16_guardian
, g
);
313 /* called before gc mark phase begins to initialise the live guardian list. */
315 guardian_gc_init (void *dummy1
, void *dummy2
, void *dummy3
)
317 greedy_guardians
= sharing_guardians
= NULL
;
323 mark_dependencies_in_tconc (tconc_t
*tc
)
328 /* scan the list for unmarked objects, and mark their
330 for (pair
= tc
->head
, prev_ptr
= &tc
->head
;
331 ! SCM_EQ_P (pair
, tc
->tail
);
334 SCM obj
= SCM_CAR (pair
);
335 next_pair
= SCM_CDR (pair
);
337 if (! SCM_MARKEDP (obj
))
339 /* a candidate for finalizing */
340 scm_gc_mark_dependencies (obj
);
342 if (SCM_MARKEDP (obj
))
344 /* uh oh. a cycle. transfer this object (the
345 spine cell, to be exact) to
346 self_centered_zombies, so we'll be able to
347 complain about it later. */
348 *prev_ptr
= next_pair
;
349 SCM_SETGCMARK (pair
);
350 SCM_SETCDR (pair
, SCM_CDR (self_centered_zombies
));
351 SCM_SETCDR (self_centered_zombies
, pair
);
355 /* see if this is a guardian. if yes, list it (but don't
357 if (GUARDIAN_P (obj
))
358 add_to_live_list (obj
);
360 prev_ptr
= SCM_CDRLOC (pair
);
367 mark_dependencies (guardian_t
*g
)
369 mark_dependencies_in_tconc (&g
->zombies
);
370 mark_dependencies_in_tconc (&g
->live
);
374 mark_and_zombify (guardian_t
*g
)
376 SCM tconc_tail
= g
->live
.tail
;
377 SCM
*prev_ptr
= &g
->live
.head
;
378 SCM pair
= g
->live
.head
;
380 while (! SCM_EQ_P (pair
, tconc_tail
))
382 SCM next_pair
= SCM_CDR (pair
);
384 if (SCM_NMARKEDP (SCM_CAR (pair
)))
386 /* got you, zombie! */
388 /* out of the live list! */
389 *prev_ptr
= next_pair
;
392 /* if the guardian is greedy, mark this zombie now. this
393 way it won't be zombified again this time around. */
394 SCM_SETGCMARK (SCM_CAR (pair
));
396 /* into the zombie list! */
397 TCONC_IN (g
->zombies
, SCM_CAR (pair
), pair
);
400 prev_ptr
= SCM_CDRLOC (pair
);
405 /* Mark the cells of the live list (yes, the cells in the list, we
406 don't care about objects pointed to by the list cars, since we
407 know they are already marked). */
408 for (pair
= g
->live
.head
; !SCM_NULLP (pair
); pair
= SCM_CDR (pair
))
409 SCM_SETGCMARK (pair
);
413 /* this is called by the garbage collector between the mark and sweep
414 phases. for each marked guardian, it moves any unmarked object in
415 its live list (tconc) to its zombie list (tconc). */
417 guardian_zombify (void *dummy1
, void *dummy2
, void *dummy3
)
419 guardian_t
*last_greedy_guardian
= NULL
;
420 guardian_t
*last_sharing_guardian
= NULL
;
421 guardian_t
*first_greedy_guardian
= NULL
;
422 guardian_t
*first_sharing_guardian
= NULL
;
425 /* First, find all newly unreachable objects and mark their
428 Note that new guardians may be stuck on the end of the live
429 guardian lists as we run this loop, since guardians might be
430 guarded too. When we mark a guarded guardian, its mark function
431 sticks in the appropriate live guardian list. The loop
432 terminates when no new guardians are found. */
435 first_greedy_guardian
= greedy_guardians
;
436 first_sharing_guardian
= sharing_guardians
;
438 for (g
= greedy_guardians
; g
!= last_greedy_guardian
;
440 mark_dependencies (g
);
441 for (g
= sharing_guardians
; g
!= last_sharing_guardian
;
443 mark_dependencies (g
);
445 last_greedy_guardian
= first_greedy_guardian
;
446 last_sharing_guardian
= first_sharing_guardian
;
447 } while (first_greedy_guardian
!= greedy_guardians
448 || first_sharing_guardian
!= sharing_guardians
);
450 /* now, scan all the guardians that are currently known to be live
451 and move their unmarked objects to zombie lists. */
453 for (g
= greedy_guardians
; g
; g
= g
->next
)
455 mark_and_zombify (g
);
458 for (g
= sharing_guardians
; g
; g
= g
->next
)
460 mark_and_zombify (g
);
464 /* Preserve the zombies in their undead state, by marking to prevent
466 for (g
= greedy_guardians
; g
; g
= g
->next
)
467 scm_gc_mark (g
->zombies
.head
);
468 for (g
= sharing_guardians
; g
; g
= g
->next
)
469 scm_gc_mark (g
->zombies
.head
);
475 whine_about_self_centered_zombies (void *dummy1
, void *dummy2
, void *dummy3
)
477 if (! SCM_NULLP (SCM_CDR (self_centered_zombies
)))
481 scm_puts ("** WARNING: the following guarded objects were unguarded due to cycles:",
483 scm_newline (scm_cur_errp
);
484 for (pair
= SCM_CDR (self_centered_zombies
);
485 ! SCM_NULLP (pair
); pair
= SCM_CDR (pair
))
487 scm_display (SCM_CAR (pair
), scm_cur_errp
);
488 scm_newline (scm_cur_errp
);
491 SCM_SETCDR (self_centered_zombies
, SCM_EOL
);
498 scm_init_guardians ()
500 tc16_guardian
= scm_make_smob_type ("guardian", 0);
501 scm_set_smob_mark (tc16_guardian
, guardian_mark
);
502 scm_set_smob_free (tc16_guardian
, guardian_free
);
503 scm_set_smob_print (tc16_guardian
, guardian_print
);
504 scm_set_smob_apply (tc16_guardian
, guardian_apply
, 0, 1, 0);
506 scm_c_hook_add (&scm_before_mark_c_hook
, guardian_gc_init
, 0, 0);
507 scm_c_hook_add (&scm_before_sweep_c_hook
, guardian_zombify
, 0, 0);
509 greedily_guarded_prop
=
510 scm_permanent_object (scm_primitive_make_property (SCM_BOOL_F
));
512 self_centered_zombies
=
513 scm_permanent_object (scm_cons (SCM_UNDEFINED
, SCM_EOL
));
514 scm_c_hook_add (&scm_after_gc_c_hook
,
515 whine_about_self_centered_zombies
, 0, 0);
517 #ifndef SCM_MAGIC_SNARFER
518 #include "libguile/guardians.x"