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/root.h"
70 #include "libguile/hashtab.h"
71 #include "libguile/weaks.h"
73 #include "libguile/guardians.h"
76 /* The live and zombies FIFOs are implemented as tconcs as described
77 in Dybvig's paper. This decouples addition and removal of elements
78 so that no synchronization between these needs to take place.
81 typedef struct tconc_t
87 #define TCONC_EMPTYP(tc) (SCM_EQ_P ((tc).head, (tc).tail))
89 #define TCONC_IN(tc, obj, pair) \
91 SCM_SETCAR ((tc).tail, obj); \
92 SCM_SETCAR (pair, SCM_BOOL_F); \
93 SCM_SETCDR (pair, SCM_EOL); \
94 SCM_SETCDR ((tc).tail, pair); \
98 #define TCONC_OUT(tc, res) \
100 (res) = SCM_CAR ((tc).head); \
101 (tc).head = SCM_CDR ((tc).head); \
105 static scm_bits_t tc16_guardian
;
107 typedef struct guardian_t
111 struct guardian_t
*next
;
116 #define GUARDIAN_P(x) SCM_SMOB_PREDICATE(tc16_guardian, x)
117 #define GUARDIAN(x) ((guardian_t *) SCM_CELL_WORD_1 (x))
118 #define GUARDIAN_LIVE(x) (GUARDIAN (x)->live)
119 #define GUARDIAN_ZOMBIES(x) (GUARDIAN (x)->zombies)
120 #define GUARDIAN_NEXT(x) (GUARDIAN (x)->next)
121 #define GUARDIAN_GREEDY_P(x) (GUARDIAN (x)->greedy_p)
122 #define GUARDIAN_LISTED_P(x) (GUARDIAN (x)->listed_p)
125 /* during the gc mark phase, live guardians are linked into the lists
127 static guardian_t
*greedy_guardians
= NULL
;
128 static guardian_t
*sharing_guardians
= NULL
;
130 static SCM greedily_guarded_whash
= SCM_EOL
;
132 /* this is the list of guarded objects that are parts of cycles. we
133 don't know in which order to return them from guardians, so we just
134 unguard them and whine about it in after-gc-hook */
135 static SCM self_centered_zombies
= SCM_EOL
;
139 add_to_live_list (SCM g
)
141 if (GUARDIAN_LISTED_P (g
))
144 if (GUARDIAN_GREEDY_P (g
))
146 GUARDIAN_NEXT (g
) = greedy_guardians
;
147 greedy_guardians
= GUARDIAN (g
);
151 GUARDIAN_NEXT (g
) = sharing_guardians
;
152 sharing_guardians
= GUARDIAN (g
);
155 GUARDIAN_LISTED_P (g
) = 1;
158 /* mark a guardian by adding it to the live guardian list. */
160 guardian_mark (SCM ptr
)
162 add_to_live_list (ptr
);
164 /* the objects protected by the guardian are not marked here: that
165 would prevent them from ever getting collected. instead marking
166 is done at the end of the mark phase by guardian_zombify. */
172 guardian_free (SCM ptr
)
174 scm_must_free (GUARDIAN (ptr
));
175 return sizeof (guardian_t
);
180 guardian_print (SCM g
, SCM port
, scm_print_state
*pstate
)
182 scm_puts ("#<", port
);
183 if (GUARDIAN_GREEDY_P (g
))
184 scm_puts ("greedy ", port
);
186 scm_puts ("sharing ", port
);
187 scm_puts ("guardian (reachable: ", port
);
188 scm_display (scm_length (SCM_CDR (GUARDIAN_LIVE (g
).head
)), port
);
189 scm_puts (" unreachable: ", port
);
190 scm_display (scm_length (SCM_CDR (GUARDIAN_ZOMBIES (g
).head
)), port
);
191 scm_puts (")>", port
);
197 /* This is the Scheme entry point for each guardian: If arg is an object, it's
198 * added to the guardian's live list. If arg is unbound, the next available
199 * zombified object (or #f if none) is returned.
202 guardian_apply (SCM guardian
, SCM arg
)
204 if (!SCM_UNBNDP (arg
))
206 scm_guard (guardian
, arg
);
207 return SCM_UNSPECIFIED
;
210 return scm_get_one_zombie (guardian
);
215 scm_guard (SCM guardian
, SCM obj
)
223 /* This critical section barrier will be replaced by a mutex. */
226 if (GUARDIAN_GREEDY_P (guardian
))
228 if (SCM_NFALSEP (scm_hashq_get_handle
229 (greedily_guarded_whash
, obj
)))
232 scm_misc_error ("guard",
233 "object is already greedily guarded", obj
);
236 scm_hashq_create_handle_x (greedily_guarded_whash
,
240 TCONC_IN (GUARDIAN_LIVE (guardian
), obj
, z
);
249 scm_get_one_zombie (SCM guardian
)
251 SCM res
= SCM_BOOL_F
;
253 /* This critical section barrier will be replaced by a mutex. */
256 if (!TCONC_EMPTYP (GUARDIAN_ZOMBIES (guardian
)))
257 TCONC_OUT (GUARDIAN_ZOMBIES (guardian
), res
);
259 if (SCM_NFALSEP (res
)
260 && GUARDIAN_GREEDY_P (guardian
)
261 && SCM_NFALSEP (scm_hashq_get_handle
262 (greedily_guarded_whash
, res
)))
263 scm_hashq_remove_x (greedily_guarded_whash
, res
);
271 SCM_DEFINE (scm_make_guardian
, "make-guardian", 0, 1, 0,
273 "Create a new guardian.\n"
274 "A guardian protects a set of objects from garbage collection,\n"
275 "allowing a program to apply cleanup or other actions.\n\n"
277 "make-guardian returns a procedure representing the guardian.\n"
278 "Calling the guardian procedure with an argument adds the\n"
279 "argument to the guardian's set of protected objects.\n"
280 "Calling the guardian procedure without an argument returns\n"
281 "one of the protected objects which are ready for garbage\n"
282 "collection or @code{#f} if no such object is available.\n"
283 "Objects which are returned in this way are removed from\n"
286 "make-guardian takes one optional argument that says whether the\n"
287 "new guardian should be greedy or not. if there is any chance\n"
288 "that any object protected by the guardian may be resurrected,\n"
289 "then make the guardian greedy (this is the default).\n\n"
291 "See R. Kent Dybvig, Carl Bruggeman, and David Eby (1993)\n"
292 "\"Guardians in a Generation-Based Garbage Collector\".\n"
293 "ACM SIGPLAN Conference on Programming Language Design\n"
294 "and Implementation, June 1993.\n\n"
296 "(the semantics are slightly different at this point, but the\n"
297 "paper still (mostly) accurately describes the interface).")
298 #define FUNC_NAME s_scm_make_guardian
300 guardian_t
*g
= SCM_MUST_MALLOC_TYPE (guardian_t
);
301 SCM z1
= scm_cons (SCM_BOOL_F
, SCM_EOL
);
302 SCM z2
= scm_cons (SCM_BOOL_F
, SCM_EOL
);
305 /* A tconc starts out with one tail pair. */
306 g
->live
.head
= g
->live
.tail
= z1
;
307 g
->zombies
.head
= g
->zombies
.tail
= z2
;
310 if (SCM_UNBNDP (greedy_p
))
313 g
->greedy_p
= SCM_NFALSEP (greedy_p
);
315 SCM_NEWSMOB (z
, tc16_guardian
, g
);
322 /* called before gc mark phase begins to initialise the live guardian list. */
324 guardian_gc_init (void *dummy1
, void *dummy2
, void *dummy3
)
326 greedy_guardians
= sharing_guardians
= NULL
;
332 mark_dependencies_in_tconc (tconc_t
*tc
)
337 /* scan the list for unmarked objects, and mark their
339 for (pair
= tc
->head
, prev_ptr
= &tc
->head
;
340 ! SCM_EQ_P (pair
, tc
->tail
);
343 SCM obj
= SCM_CAR (pair
);
344 next_pair
= SCM_CDR (pair
);
346 if (! SCM_MARKEDP (obj
))
348 /* a candidate for finalizing */
349 scm_gc_mark_dependencies (obj
);
351 if (SCM_MARKEDP (obj
))
353 /* uh oh. a cycle. transfer this object (the
354 spine cell, to be exact) to
355 self_centered_zombies, so we'll be able to
356 complain about it later. */
357 *prev_ptr
= next_pair
;
358 SCM_SETGCMARK (pair
);
359 SCM_SETCDR (pair
, SCM_CDR (self_centered_zombies
));
360 SCM_SETCDR (self_centered_zombies
, pair
);
364 /* see if this is a guardian. if yes, list it (but don't
366 if (GUARDIAN_P (obj
))
367 add_to_live_list (obj
);
369 prev_ptr
= SCM_CDRLOC (pair
);
376 mark_dependencies (guardian_t
*g
)
378 mark_dependencies_in_tconc (&g
->zombies
);
379 mark_dependencies_in_tconc (&g
->live
);
383 mark_and_zombify (guardian_t
*g
)
385 SCM tconc_tail
= g
->live
.tail
;
386 SCM
*prev_ptr
= &g
->live
.head
;
387 SCM pair
= g
->live
.head
;
389 while (! SCM_EQ_P (pair
, tconc_tail
))
391 SCM next_pair
= SCM_CDR (pair
);
393 if (SCM_NMARKEDP (SCM_CAR (pair
)))
395 /* got you, zombie! */
397 /* out of the live list! */
398 *prev_ptr
= next_pair
;
401 /* if the guardian is greedy, mark this zombie now. this
402 way it won't be zombified again this time around. */
403 SCM_SETGCMARK (SCM_CAR (pair
));
405 /* into the zombie list! */
406 TCONC_IN (g
->zombies
, SCM_CAR (pair
), pair
);
409 prev_ptr
= SCM_CDRLOC (pair
);
414 /* Mark the cells of the live list (yes, the cells in the list, we
415 don't care about objects pointed to by the list cars, since we
416 know they are already marked). */
417 for (pair
= g
->live
.head
; !SCM_NULLP (pair
); pair
= SCM_CDR (pair
))
418 SCM_SETGCMARK (pair
);
422 /* this is called by the garbage collector between the mark and sweep
423 phases. for each marked guardian, it moves any unmarked object in
424 its live list (tconc) to its zombie list (tconc). */
426 guardian_zombify (void *dummy1
, void *dummy2
, void *dummy3
)
428 guardian_t
*last_greedy_guardian
= NULL
;
429 guardian_t
*last_sharing_guardian
= NULL
;
430 guardian_t
*first_greedy_guardian
= NULL
;
431 guardian_t
*first_sharing_guardian
= NULL
;
434 /* First, find all newly unreachable objects and mark their
437 Note that new guardians may be stuck on the end of the live
438 guardian lists as we run this loop, since guardians might be
439 guarded too. When we mark a guarded guardian, its mark function
440 sticks in the appropriate live guardian list. The loop
441 terminates when no new guardians are found. */
444 first_greedy_guardian
= greedy_guardians
;
445 first_sharing_guardian
= sharing_guardians
;
447 for (g
= greedy_guardians
; g
!= last_greedy_guardian
;
449 mark_dependencies (g
);
450 for (g
= sharing_guardians
; g
!= last_sharing_guardian
;
452 mark_dependencies (g
);
454 last_greedy_guardian
= first_greedy_guardian
;
455 last_sharing_guardian
= first_sharing_guardian
;
456 } while (first_greedy_guardian
!= greedy_guardians
457 || first_sharing_guardian
!= sharing_guardians
);
459 /* now, scan all the guardians that are currently known to be live
460 and move their unmarked objects to zombie lists. */
462 for (g
= greedy_guardians
; g
; g
= g
->next
)
464 mark_and_zombify (g
);
467 for (g
= sharing_guardians
; g
; g
= g
->next
)
469 mark_and_zombify (g
);
473 /* Preserve the zombies in their undead state, by marking to prevent
475 for (g
= greedy_guardians
; g
; g
= g
->next
)
476 scm_gc_mark (g
->zombies
.head
);
477 for (g
= sharing_guardians
; g
; g
= g
->next
)
478 scm_gc_mark (g
->zombies
.head
);
484 whine_about_self_centered_zombies (void *dummy1
, void *dummy2
, void *dummy3
)
486 if (! SCM_NULLP (SCM_CDR (self_centered_zombies
)))
490 scm_puts ("** WARNING: the following guarded objects were unguarded due to cycles:",
492 scm_newline (scm_cur_errp
);
493 for (pair
= SCM_CDR (self_centered_zombies
);
494 ! SCM_NULLP (pair
); pair
= SCM_CDR (pair
))
496 scm_display (SCM_CAR (pair
), scm_cur_errp
);
497 scm_newline (scm_cur_errp
);
500 SCM_SETCDR (self_centered_zombies
, SCM_EOL
);
507 scm_init_guardians ()
509 tc16_guardian
= scm_make_smob_type ("guardian", 0);
510 scm_set_smob_mark (tc16_guardian
, guardian_mark
);
511 scm_set_smob_free (tc16_guardian
, guardian_free
);
512 scm_set_smob_print (tc16_guardian
, guardian_print
);
513 scm_set_smob_apply (tc16_guardian
, guardian_apply
, 0, 1, 0);
515 scm_c_hook_add (&scm_before_mark_c_hook
, guardian_gc_init
, 0, 0);
516 scm_c_hook_add (&scm_before_sweep_c_hook
, guardian_zombify
, 0, 0);
518 self_centered_zombies
=
519 scm_permanent_object (scm_cons (SCM_UNDEFINED
, SCM_EOL
));
520 scm_c_hook_add (&scm_after_gc_c_hook
,
521 whine_about_self_centered_zombies
, 0, 0);
523 greedily_guarded_whash
=
524 scm_permanent_object (scm_make_doubly_weak_hash_table (SCM_MAKINUM (31)));
526 #ifndef SCM_MAGIC_SNARFER
527 #include "libguile/guardians.x"