* Added Jost Boekemeier's implementation of environments to guile.
[bpt/guile.git] / libguile / guardians.c
CommitLineData
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
69static 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 76do { \
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 85do { \
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
92typedef struct tconc_t
93{
94 SCM head;
95 SCM tail;
96} tconc_t;
97
98typedef 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. */
113static 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
119static SCM
120guard (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
131void
132scm_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
149SCM
150scm_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 163SCM_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. */
204static guardian_t *first_live_guardian = NULL;
205static guardian_t **current_link_field = NULL;
206
207/* called before gc mark phase begins to initialise the live guardian
208 list. */
5d2565a7
MD
209static void *
210scm_guardian_gc_init (void *dummy1, void *dummy2, void *dummy3)
e1f2bf99 211{
01a119ac
JB
212 current_link_field = &first_live_guardian;
213 first_live_guardian = NULL;
5d2565a7
MD
214
215 return 0;
e1f2bf99
MD
216}
217
e46f3fa6
GH
218/* mark a guardian by adding it to the live guardian list. */
219static SCM
220g_mark (SCM ptr)
221{
222 *current_link_field = GUARDIAN (ptr);
223 current_link_field = &GUARDIAN_NEXT (ptr);
224 GUARDIAN_NEXT (ptr) = NULL;
225
226 /* the objects protected by the guardian are not marked here: that
227 would prevent them from ever getting collected. instead marking
228 is done at the end of the mark phase by scm_guardian_zombify. */
229 return SCM_BOOL_F;
230}
231
232/* this is called by the garbage collector between the mark and sweep
233 phases. for each marked guardian, it moves any unmarked object in
234 its live list (tconc) to its zombie list (tconc). */
5d2565a7
MD
235static void *
236scm_guardian_zombify (void *dummy1, void *dummy2, void *dummy3)
e1f2bf99 237{
50fecba9
ML
238 guardian_t *first_guardian;
239 guardian_t **link_field = &first_live_guardian;
55b7e0bd
JB
240
241 /* Note that new guardians may be stuck on the end of the live
242 guardian list as we run this loop. As we move unmarked objects
243 to the zombie list and mark them, we may find some guarded
244 guardians. The guardian mark function will stick them on the end
245 of this list, so they'll be processed properly. */
e46f3fa6 246
50fecba9
ML
247 do {
248 guardian_t *g;
249
250 first_guardian = *link_field;
251 link_field = current_link_field;
252
253 /* first, scan all the guardians that are currently known to be live
254 and move their unmarked objects to zombie lists. */
255
256 for (g = first_guardian; g; g = g->next)
257 {
258 SCM tconc_tail = g->live.tail;
259 SCM *prev_ptr = &g->live.head;
260 SCM pair = g->live.head;
261
262 while (! SCM_EQ_P (pair, tconc_tail))
263 {
264 SCM next_pair = SCM_CDR (pair);
265
266 if (SCM_NMARKEDP (SCM_CAR (pair)))
267 {
268 /* got you, zombie! */
269
270 /* out of the live list! */
271 *prev_ptr = next_pair;
272
273 /* into the zombie list! */
274 TCONC_IN (g->zombies, SCM_CAR (pair), pair);
275 }
276 else
277 prev_ptr = SCM_CDRLOC (pair);
278
279 pair = next_pair;
280 }
281
282 /* Mark the cells of the live list (yes, the cells in the list,
283 even though we don't care about objects pointed to by the list
284 cars, since we know they are already marked). */
285 for (pair = g->live.head; SCM_NIMP (pair); pair = SCM_GCCDR (pair))
286 SCM_SETGCMARK (pair);
287 }
288
289 /* ghouston: Doesn't it seem a bit disturbing that if a zombie
290 is returned to full life after getting returned from the
291 guardian procedure, it may reference objects which are in a
292 guardian's zombie list? Is it not necessary to move such
293 zombies back to the live list, to avoid allowing the
294 guardian procedure to return an object which is referenced,
295 so not collectable? The paper doesn't give this
296 impression.
297
298 cmm: the paper does explicitly say that an object that is
299 guarded more than once should be returned more than once.
300 I believe this covers the above scenario. */
301
302 /* Preserve the zombies in their undead state, by marking to
303 prevent collection. Note that this may uncover zombified
304 guardians -- if so, they'll be processed in the next loop. */
305
306 for (g = first_guardian; g && (!*link_field || g != *link_field); g = g->next)
01a119ac 307 scm_gc_mark (g->zombies.head);
50fecba9
ML
308
309 } while (current_link_field != link_field);
5d2565a7
MD
310
311 return 0;
e1f2bf99
MD
312}
313
e46f3fa6
GH
314/* not generally used, since guardian smob is wrapped in a closure.
315 maybe useful for debugging. */
316static int
317g_print (SCM exp, SCM port, scm_print_state *pstate)
e1f2bf99 318{
e46f3fa6
GH
319 char buf[256];
320 sprintf (buf, "#<guardian live objs: %lu zombies: %lu>",
321 scm_ilength (SCM_CDR (GUARDIAN_LIVE (exp).head)),
322 scm_ilength (SCM_CDR (GUARDIAN_ZOMBIES (exp).head)));
323 scm_puts (buf, port);
e1f2bf99 324
e46f3fa6 325 return 1;
e1f2bf99
MD
326}
327
328void
329scm_init_guardian()
330{
23a62151 331 scm_tc16_guardian = scm_make_smob_type_mfpe ("guardian", sizeof (guardian_t),
e46f3fa6 332 g_mark, NULL, g_print, NULL);
e1f2bf99 333 guard1 = scm_make_subr_opt ("guardian", scm_tc7_subr_2o, guard, 0);
5d2565a7
MD
334 scm_c_hook_add (&scm_before_mark_c_hook, scm_guardian_gc_init, 0, 0);
335 scm_c_hook_add (&scm_before_sweep_c_hook, scm_guardian_zombify, 0, 0);
e1f2bf99 336
a0599745 337#include "libguile/guardians.x"
e1f2bf99 338}
89e00824
ML
339
340/*
341 Local Variables:
342 c-file-style: "gnu"
343 End:
344*/