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 * Author: Michael N. Livshin
54 * Modified by: Mikael Djurfeldt
67 #include "guardians.h"
69 static long scm_tc16_guardian
;
71 /* The live and zombies FIFOs are implemented as tconcs as described
72 in Dybvig's paper. This decouples addition and removal of elements
73 so that no synchronization between these needs to take place.
75 #define TCONC_IN(tc, obj, pair) \
77 SCM_SETCAR ((tc).tail, obj); \
78 SCM_SETCAR (pair, SCM_BOOL_F); \
79 SCM_SETCDR (pair, SCM_EOL); \
80 SCM_SETCDR ((tc).tail, pair); \
84 #define TCONC_OUT(tc, res) \
86 (res) = SCM_CAR ((tc).head); \
87 (tc).head = SCM_CDR ((tc).head); \
90 #define TCONC_EMPTYP(tc) (SCM_EQ_P ((tc).head, (tc).tail))
92 typedef struct tconc_t
98 typedef struct guardian_t
102 struct guardian_t
*next
;
105 #define GUARDIAN(x) ((guardian_t *) SCM_CELL_WORD_1 (x))
106 #define GUARDIAN_LIVE(x) (GUARDIAN (x)->live)
107 #define GUARDIAN_ZOMBIES(x) (GUARDIAN (x)->zombies)
108 #define GUARDIAN_NEXT(x) (GUARDIAN (x)->next)
110 #define CCLO_G(cclo) (SCM_VELTS (cclo)[1])
112 /* subr constructed from guard below. */
115 /* this is wrapped in a compiled closure and is the Scheme entry point
116 for each guardian: if arg is an object, it's added to the
117 guardian's live list. if arg is unbound, the next available
118 zombified object (or #f if none) is returned. */
120 guard (SCM cclo
, SCM arg
)
122 if (!SCM_UNBNDP (arg
))
124 scm_guard (cclo
, arg
);
125 return SCM_UNSPECIFIED
;
128 return scm_get_one_zombie (cclo
);
132 scm_guard (SCM guardian
, SCM obj
)
134 SCM g
= CCLO_G (guardian
);
142 /* This critical section barrier will be replaced by a mutex. */
144 TCONC_IN (GUARDIAN_LIVE (g
), obj
, z
);
150 scm_get_one_zombie (SCM guardian
)
152 SCM g
= CCLO_G (guardian
);
153 SCM res
= SCM_BOOL_F
;
155 /* This critical section barrier will be replaced by a mutex. */
157 if (!TCONC_EMPTYP (GUARDIAN_ZOMBIES (g
)))
158 TCONC_OUT (GUARDIAN_ZOMBIES (g
), res
);
163 SCM_DEFINE (scm_make_guardian
, "make-guardian", 0, 0, 0,
165 "Create a new guardian.\n"
166 "A guardian protects a set of objects from garbage collection,\n"
167 "allowing a program to apply cleanup or other actions.\n\n"
169 "make-guardian returns a procedure representing the guardian.\n"
170 "Calling the guardian procedure with an argument adds the\n"
171 "argument to the guardian's set of protected objects.\n"
172 "Calling the guardian procedure without an argument returns\n"
173 "one of the protected objects which are ready for garbage\n"
174 "collection or @code{#f} if no such object is available.\n"
175 "Objects which are returned in this way are removed from\n"
178 "See R. Kent Dybvig, Carl Bruggeman, and David Eby (1993)\n"
179 "\"Guardians in a Generation-Based Garbage Collector\".\n"
180 "ACM SIGPLAN Conference on Programming Language Design\n"
181 "and Implementation, June 1993.")
182 #define FUNC_NAME s_scm_make_guardian
184 SCM cclo
= scm_makcclo (guard1
, 2L);
185 guardian_t
*g
= SCM_MUST_MALLOC_TYPE(guardian_t
);
186 SCM z1
= scm_cons (SCM_BOOL_F
, SCM_EOL
);
187 SCM z2
= scm_cons (SCM_BOOL_F
, SCM_EOL
);
190 /* A tconc starts out with one tail pair. */
191 g
->live
.head
= g
->live
.tail
= z1
;
192 g
->zombies
.head
= g
->zombies
.tail
= z2
;
194 SCM_NEWSMOB (z
, scm_tc16_guardian
, g
);
202 /* during the gc mark phase, live guardians are linked into a list
204 static guardian_t
*first_live_guardian
= NULL
;
205 static guardian_t
**current_link_field
= NULL
;
207 /* called before gc mark phase begins to initialise the live guardian
210 scm_guardian_gc_init()
212 current_link_field
= &first_live_guardian
;
213 first_live_guardian
= NULL
;
216 /* mark a guardian by adding it to the live guardian list. */
220 *current_link_field
= GUARDIAN (ptr
);
221 current_link_field
= &GUARDIAN_NEXT (ptr
);
222 GUARDIAN_NEXT (ptr
) = NULL
;
224 /* the objects protected by the guardian are not marked here: that
225 would prevent them from ever getting collected. instead marking
226 is done at the end of the mark phase by scm_guardian_zombify. */
230 /* this is called by the garbage collector between the mark and sweep
231 phases. for each marked guardian, it moves any unmarked object in
232 its live list (tconc) to its zombie list (tconc). */
233 void scm_guardian_zombify (void)
235 guardian_t
*first_guardian
;
236 guardian_t
**link_field
= &first_live_guardian
;
238 /* Note that new guardians may be stuck on the end of the live
239 guardian list as we run this loop. As we move unmarked objects
240 to the zombie list and mark them, we may find some guarded
241 guardians. The guardian mark function will stick them on the end
242 of this list, so they'll be processed properly. */
247 first_guardian
= *link_field
;
248 link_field
= current_link_field
;
250 /* first, scan all the guardians that are currently known to be live
251 and move their unmarked objects to zombie lists. */
253 for (g
= first_guardian
; g
; g
= g
->next
)
255 SCM tconc_tail
= g
->live
.tail
;
256 SCM
*prev_ptr
= &g
->live
.head
;
257 SCM pair
= g
->live
.head
;
259 while (! SCM_EQ_P (pair
, tconc_tail
))
261 SCM next_pair
= SCM_CDR (pair
);
263 if (SCM_NMARKEDP (SCM_CAR (pair
)))
265 /* got you, zombie! */
267 /* out of the live list! */
268 *prev_ptr
= next_pair
;
270 /* into the zombie list! */
271 TCONC_IN (g
->zombies
, SCM_CAR (pair
), pair
);
274 prev_ptr
= SCM_CDRLOC (pair
);
279 /* Mark the cells of the live list (yes, the cells in the list,
280 even though we don't care about objects pointed to by the list
281 cars, since we know they are already marked). */
282 for (pair
= g
->live
.head
; SCM_NIMP (pair
); pair
= SCM_GCCDR (pair
))
283 SCM_SETGCMARK (pair
);
286 /* ghouston: Doesn't it seem a bit disturbing that if a zombie
287 is returned to full life after getting returned from the
288 guardian procedure, it may reference objects which are in a
289 guardian's zombie list? Is it not necessary to move such
290 zombies back to the live list, to avoid allowing the
291 guardian procedure to return an object which is referenced,
292 so not collectable? The paper doesn't give this
295 cmm: the paper does explicitly say that an object that is
296 guarded more than once should be returned more than once.
297 I believe this covers the above scenario. */
299 /* Preserve the zombies in their undead state, by marking to
300 prevent collection. Note that this may uncover zombified
301 guardians -- if so, they'll be processed in the next loop. */
303 for (g
= first_guardian
; g
&& (!*link_field
|| g
!= *link_field
); g
= g
->next
)
304 scm_gc_mark (g
->zombies
.head
);
306 } while (current_link_field
!= link_field
);
309 /* not generally used, since guardian smob is wrapped in a closure.
310 maybe useful for debugging. */
312 g_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
315 sprintf (buf
, "#<guardian live objs: %lu zombies: %lu>",
316 scm_ilength (SCM_CDR (GUARDIAN_LIVE (exp
).head
)),
317 scm_ilength (SCM_CDR (GUARDIAN_ZOMBIES (exp
).head
)));
318 scm_puts (buf
, port
);
326 scm_tc16_guardian
= scm_make_smob_type_mfpe ("guardian", sizeof (guardian_t
),
327 g_mark
, NULL
, g_print
, NULL
);
328 guard1
= scm_make_subr_opt ("guardian", scm_tc7_subr_2o
, guard
, 0);
330 #include "guardians.x"