1 /* Copyright (C) 1998 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. */
43 /* This is an implementation of guardians as described in
44 * R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) "Guardians in
45 * a Generation-Based Garbage Collector" ACM SIGPLAN Conference on
46 * Programming Language Design and Implementation, June 1993
47 * ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/pubs/guardians.ps.gz
49 * Author: Michael N. Livshin
50 * Modified by: Mikael Djurfeldt
61 #include "guardians.h"
63 static long scm_tc16_guardian
;
65 /* The live and zombies FIFOs are implemented as tconcs as described
66 in Dybvig's paper. This decouples addition and removal of elements
67 so that no synchronization between these needs to take place.
69 #define TCONC_IN(tc, obj, pair) \
71 SCM_SETCAR ((tc).tail, obj); \
72 SCM_SETCAR (pair, SCM_BOOL_F); \
73 SCM_SETCDR (pair, SCM_BOOL_F); \
74 SCM_SETCDR ((tc).tail, pair); \
78 #define TCONC_OUT(tc, res) \
80 (res) = SCM_CAR ((tc).head); \
81 (tc).head = SCM_CDR ((tc).head); \
84 #define TCONC_EMPTYP(tc) ((tc).head == (tc).tail)
86 typedef struct tconc_t
92 typedef struct guardian_t
98 #define GUARDIAN(x) ((guardian_t *) SCM_CDR (x))
99 #define GUARDIAN_LIVE(x) (GUARDIAN (x)->live)
100 #define GUARDIAN_ZOMBIES(x) (GUARDIAN (x)->zombies)
102 static SCM
*guardians
= NULL
;
103 static scm_sizet guardians_size
= 0;
104 static scm_sizet n_guardians
;
109 if (n_guardians
>= guardians_size
)
111 SCM_SYSCALL (guardians
=
112 (SCM
*) realloc((char *) guardians
,
113 sizeof (SCM
) * (guardians_size
*= 2)));
116 scm_puts ("active guardian table", scm_cur_errp
);
117 scm_puts ("\nFATAL ERROR DURING CRITICAL SCM_CODE SECTION\n",
119 exit (SCM_EXIT_FAILURE
);
122 guardians
[n_guardians
++] = ptr
;
123 /* Can't mark zombies here since they can refer to objects which are
124 living dead, thereby preventing them to join the zombies. */
131 scm_must_free ((char *) GUARDIAN (ptr
));
132 return sizeof (guardian_t
);
136 g_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
139 sprintf (buf
, "#<guardian live objs: %lu zombies: %lu>",
140 scm_ilength (SCM_CDR (GUARDIAN_LIVE (exp
).head
)),
141 scm_ilength (SCM_CDR (GUARDIAN_ZOMBIES (exp
).head
)));
142 scm_puts (buf
, port
);
147 static scm_smobfuns g_smob
= {
154 #define CCLO_G(cclo) (SCM_VELTS (cclo)[1])
157 guard (SCM cclo
, SCM arg
)
159 if (!SCM_UNBNDP (arg
))
161 scm_guard (cclo
, arg
);
162 return SCM_UNSPECIFIED
;
165 return scm_get_one_zombie (cclo
);
170 SCM_PROC (s_make_guardian
, "make-guardian", 0, 0, 0, scm_make_guardian
);
174 SCM cclo
= scm_makcclo (guard1
, 2L);
175 guardian_t
*g
= (guardian_t
*) scm_must_malloc (sizeof (guardian_t
),
177 SCM z1
= scm_cons (SCM_BOOL_F
, SCM_BOOL_F
);
178 SCM z2
= scm_cons (SCM_BOOL_F
, SCM_BOOL_F
);
183 /* A tconc starts out with one tail pair. */
184 g
->live
.head
= g
->live
.tail
= z1
;
185 g
->zombies
.head
= g
->zombies
.tail
= z2
;
187 SCM_SETCAR (z
, scm_tc16_guardian
);
196 scm_guardian_gc_init()
202 scm_guardian_zombify ()
205 for (i
= 0; i
< n_guardians
; ++i
)
207 SCM g
= guardians
[i
];
208 /* Loop through the live list and
209 1. move unmarked objects to the zombies tconc
210 2. mark the live tconc.
212 SCM tconc_tail
= GUARDIAN_LIVE (g
).tail
;
213 SCM prev_pair
= SCM_BOOL_F
;
214 SCM pair
= GUARDIAN_LIVE (g
).head
;
215 while (pair
!= tconc_tail
)
217 SCM next_pair
= SCM_CDR (pair
);
219 if (SCM_NMARKEDP (SCM_CAR (pair
)))
221 /* got you, zombie! */
223 /* out of the live list! */
224 if (SCM_FALSEP (prev_pair
))
225 GUARDIAN_LIVE (g
).head
= next_pair
;
227 /* mark previous pair */
228 SCM_SETCDR (prev_pair
, next_pair
| 1);
230 /* to the zombie list! */
231 TCONC_IN (GUARDIAN_ZOMBIES (g
), SCM_CAR (pair
), pair
);
235 if (SCM_NFALSEP (prev_pair
))
236 /* mark previous pair */
237 SCM_SETCDR (prev_pair
, pair
| 1);
243 if (SCM_NFALSEP (prev_pair
))
244 /* mark previous pair */
245 SCM_SETCDR (prev_pair
, pair
| 1);
246 /* mark live list tail */
247 SCM_SETOR_CDR (tconc_tail
, 1);
249 scm_gc_mark (GUARDIAN_ZOMBIES (g
).head
);
254 scm_guard (SCM guardian
, SCM obj
)
256 SCM g
= CCLO_G (guardian
);
264 /* This critical section barrier will be replaced by a mutex. */
266 TCONC_IN (GUARDIAN_LIVE (g
), obj
, z
);
272 scm_get_one_zombie (SCM guardian
)
274 SCM g
= CCLO_G (guardian
);
275 SCM res
= SCM_BOOL_F
;
277 /* This critical section barrier will be replaced by a mutex. */
279 if (!TCONC_EMPTYP (GUARDIAN_ZOMBIES (g
)))
280 TCONC_OUT (GUARDIAN_ZOMBIES (g
), res
);
289 scm_tc16_guardian
= scm_newsmob (&g_smob
);
290 if (!(guardians
= (SCM
*) malloc ((guardians_size
= 32) * sizeof (SCM
))))
292 fprintf (stderr
, "trouble!\n");
293 exit (SCM_EXIT_FAILURE
);
295 guard1
= scm_make_subr_opt ("guardian", scm_tc7_subr_2o
, guard
, 0);
297 #include "guardians.x"