Commit | Line | Data |
---|---|---|
e46f3fa6 | 1 | /* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc. |
e1f2bf99 MD |
2 | * |
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) | |
6 | * any later version. | |
7 | * | |
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. | |
12 | * | |
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 | |
17 | * | |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
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. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
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. | |
37 | * | |
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. */ | |
1bbd0b84 GB |
41 | |
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
e1f2bf99 MD |
45 | \f |
46 | ||
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 | |
52 | * | |
53 | * Author: Michael N. Livshin | |
54 | * Modified by: Mikael Djurfeldt | |
55 | */ | |
56 | ||
57 | #include <stdio.h> | |
58 | #include <assert.h> | |
59 | ||
a0599745 MD |
60 | #include "libguile/_scm.h" |
61 | #include "libguile/ports.h" | |
62 | #include "libguile/print.h" | |
63 | #include "libguile/smob.h" | |
64 | #include "libguile/vectors.h" | |
e1f2bf99 | 65 | |
a0599745 MD |
66 | #include "libguile/validate.h" |
67 | #include "libguile/guardians.h" | |
e1f2bf99 MD |
68 | |
69 | static long scm_tc16_guardian; | |
70 | ||
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. | |
74 | */ | |
75 | #define TCONC_IN(tc, obj, pair) \ | |
d3a6bc94 | 76 | do { \ |
e1f2bf99 MD |
77 | SCM_SETCAR ((tc).tail, obj); \ |
78 | SCM_SETCAR (pair, SCM_BOOL_F); \ | |
e46f3fa6 | 79 | SCM_SETCDR (pair, SCM_EOL); \ |
e1f2bf99 MD |
80 | SCM_SETCDR ((tc).tail, pair); \ |
81 | (tc).tail = pair; \ | |
d3a6bc94 | 82 | } while (0) |
e1f2bf99 MD |
83 | |
84 | #define TCONC_OUT(tc, res) \ | |
d3a6bc94 | 85 | do { \ |
e1f2bf99 MD |
86 | (res) = SCM_CAR ((tc).head); \ |
87 | (tc).head = SCM_CDR ((tc).head); \ | |
d3a6bc94 | 88 | } while (0) |
e1f2bf99 | 89 | |
7fa93bf8 | 90 | #define TCONC_EMPTYP(tc) (SCM_EQ_P ((tc).head, (tc).tail)) |
e1f2bf99 MD |
91 | |
92 | typedef struct tconc_t | |
93 | { | |
94 | SCM head; | |
95 | SCM tail; | |
96 | } tconc_t; | |
97 | ||
98 | typedef struct guardian_t | |
99 | { | |
100 | tconc_t live; | |
101 | tconc_t zombies; | |
01a119ac | 102 | struct guardian_t *next; |
e1f2bf99 MD |
103 | } guardian_t; |
104 | ||
7fa93bf8 | 105 | #define GUARDIAN(x) ((guardian_t *) SCM_CELL_WORD_1 (x)) |
e1f2bf99 MD |
106 | #define GUARDIAN_LIVE(x) (GUARDIAN (x)->live) |
107 | #define GUARDIAN_ZOMBIES(x) (GUARDIAN (x)->zombies) | |
01a119ac | 108 | #define GUARDIAN_NEXT(x) (GUARDIAN (x)->next) |
e1f2bf99 | 109 | |
e1f2bf99 MD |
110 | #define CCLO_G(cclo) (SCM_VELTS (cclo)[1]) |
111 | ||
e46f3fa6 GH |
112 | /* subr constructed from guard below. */ |
113 | static SCM guard1; | |
114 | ||
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. */ | |
e1f2bf99 MD |
119 | static SCM |
120 | guard (SCM cclo, SCM arg) | |
121 | { | |
122 | if (!SCM_UNBNDP (arg)) | |
123 | { | |
124 | scm_guard (cclo, arg); | |
125 | return SCM_UNSPECIFIED; | |
126 | } | |
127 | else | |
128 | return scm_get_one_zombie (cclo); | |
129 | } | |
130 | ||
e46f3fa6 GH |
131 | void |
132 | scm_guard (SCM guardian, SCM obj) | |
133 | { | |
134 | SCM g = CCLO_G (guardian); | |
135 | ||
136 | if (SCM_NIMP (obj)) | |
137 | { | |
138 | SCM z; | |
139 | ||
140 | SCM_NEWCELL (z); | |
141 | ||
142 | /* This critical section barrier will be replaced by a mutex. */ | |
143 | SCM_DEFER_INTS; | |
144 | TCONC_IN (GUARDIAN_LIVE (g), obj, z); | |
145 | SCM_ALLOW_INTS; | |
146 | } | |
147 | } | |
148 | ||
149 | SCM | |
150 | scm_get_one_zombie (SCM guardian) | |
151 | { | |
152 | SCM g = CCLO_G (guardian); | |
153 | SCM res = SCM_BOOL_F; | |
154 | ||
155 | /* This critical section barrier will be replaced by a mutex. */ | |
156 | SCM_DEFER_INTS; | |
157 | if (!TCONC_EMPTYP (GUARDIAN_ZOMBIES (g))) | |
158 | TCONC_OUT (GUARDIAN_ZOMBIES (g), res); | |
159 | SCM_ALLOW_INTS; | |
160 | return res; | |
161 | } | |
e1f2bf99 | 162 | |
a1ec6916 | 163 | SCM_DEFINE (scm_make_guardian, "make-guardian", 0, 0, 0, |
1bbd0b84 | 164 | (), |
e46f3fa6 GH |
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" | |
168 | ||
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" | |
176 | "the guardian.\n\n". | |
177 | ||
da4a1dba GB |
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" | |
e46f3fa6 | 181 | "and Implementation, June 1993.") |
1bbd0b84 | 182 | #define FUNC_NAME s_scm_make_guardian |
e1f2bf99 MD |
183 | { |
184 | SCM cclo = scm_makcclo (guard1, 2L); | |
1bbd0b84 | 185 | guardian_t *g = SCM_MUST_MALLOC_TYPE(guardian_t); |
e46f3fa6 GH |
186 | SCM z1 = scm_cons (SCM_BOOL_F, SCM_EOL); |
187 | SCM z2 = scm_cons (SCM_BOOL_F, SCM_EOL); | |
e1f2bf99 | 188 | SCM z; |
e46f3fa6 | 189 | |
e1f2bf99 MD |
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; | |
23a62151 MD |
193 | |
194 | SCM_NEWSMOB (z, scm_tc16_guardian, g); | |
e1f2bf99 MD |
195 | |
196 | CCLO_G (cclo) = z; | |
197 | ||
198 | return cclo; | |
199 | } | |
1bbd0b84 | 200 | #undef FUNC_NAME |
e1f2bf99 | 201 | |
e46f3fa6 GH |
202 | /* during the gc mark phase, live guardians are linked into a list |
203 | here. */ | |
204 | static guardian_t *first_live_guardian = NULL; | |
205 | static guardian_t **current_link_field = NULL; | |
206 | ||
207 | /* called before gc mark phase begins to initialise the live guardian | |
208 | list. */ | |
e1f2bf99 MD |
209 | void |
210 | scm_guardian_gc_init() | |
211 | { | |
01a119ac JB |
212 | current_link_field = &first_live_guardian; |
213 | first_live_guardian = NULL; | |
e1f2bf99 MD |
214 | } |
215 | ||
e46f3fa6 GH |
216 | /* mark a guardian by adding it to the live guardian list. */ |
217 | static SCM | |
218 | g_mark (SCM ptr) | |
219 | { | |
220 | *current_link_field = GUARDIAN (ptr); | |
221 | current_link_field = &GUARDIAN_NEXT (ptr); | |
222 | GUARDIAN_NEXT (ptr) = NULL; | |
223 | ||
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. */ | |
227 | return SCM_BOOL_F; | |
228 | } | |
229 | ||
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) | |
e1f2bf99 | 234 | { |
50fecba9 ML |
235 | guardian_t *first_guardian; |
236 | guardian_t **link_field = &first_live_guardian; | |
55b7e0bd JB |
237 | |
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. */ | |
e46f3fa6 | 243 | |
50fecba9 ML |
244 | do { |
245 | guardian_t *g; | |
246 | ||
247 | first_guardian = *link_field; | |
248 | link_field = current_link_field; | |
249 | ||
250 | /* first, scan all the guardians that are currently known to be live | |
251 | and move their unmarked objects to zombie lists. */ | |
252 | ||
253 | for (g = first_guardian; g; g = g->next) | |
254 | { | |
255 | SCM tconc_tail = g->live.tail; | |
256 | SCM *prev_ptr = &g->live.head; | |
257 | SCM pair = g->live.head; | |
258 | ||
259 | while (! SCM_EQ_P (pair, tconc_tail)) | |
260 | { | |
261 | SCM next_pair = SCM_CDR (pair); | |
262 | ||
263 | if (SCM_NMARKEDP (SCM_CAR (pair))) | |
264 | { | |
265 | /* got you, zombie! */ | |
266 | ||
267 | /* out of the live list! */ | |
268 | *prev_ptr = next_pair; | |
269 | ||
270 | /* into the zombie list! */ | |
271 | TCONC_IN (g->zombies, SCM_CAR (pair), pair); | |
272 | } | |
273 | else | |
274 | prev_ptr = SCM_CDRLOC (pair); | |
275 | ||
276 | pair = next_pair; | |
277 | } | |
278 | ||
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); | |
284 | } | |
285 | ||
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 | |
293 | impression. | |
294 | ||
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. */ | |
298 | ||
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. */ | |
302 | ||
303 | for (g = first_guardian; g && (!*link_field || g != *link_field); g = g->next) | |
01a119ac | 304 | scm_gc_mark (g->zombies.head); |
50fecba9 ML |
305 | |
306 | } while (current_link_field != link_field); | |
e1f2bf99 MD |
307 | } |
308 | ||
e46f3fa6 GH |
309 | /* not generally used, since guardian smob is wrapped in a closure. |
310 | maybe useful for debugging. */ | |
311 | static int | |
312 | g_print (SCM exp, SCM port, scm_print_state *pstate) | |
e1f2bf99 | 313 | { |
e46f3fa6 GH |
314 | char buf[256]; |
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); | |
e1f2bf99 | 319 | |
e46f3fa6 | 320 | return 1; |
e1f2bf99 MD |
321 | } |
322 | ||
323 | void | |
324 | scm_init_guardian() | |
325 | { | |
23a62151 | 326 | scm_tc16_guardian = scm_make_smob_type_mfpe ("guardian", sizeof (guardian_t), |
e46f3fa6 | 327 | g_mark, NULL, g_print, NULL); |
e1f2bf99 MD |
328 | guard1 = scm_make_subr_opt ("guardian", scm_tc7_subr_2o, guard, 0); |
329 | ||
a0599745 | 330 | #include "libguile/guardians.x" |
e1f2bf99 | 331 | } |
89e00824 ML |
332 | |
333 | /* | |
334 | Local Variables: | |
335 | c-file-style: "gnu" | |
336 | End: | |
337 | */ |