First stab at the guardian implementation. Works fine at first glance!
[bpt/guile.git] / libguile / guardians.c
1 /* Copyright (C) 1998,1999,2000,2001, 2006 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library 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 GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 \f
19 /* This is an implementation of guardians as described in
20 * R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) "Guardians in
21 * a Generation-Based Garbage Collector" ACM SIGPLAN Conference on
22 * Programming Language Design and Implementation, June 1993
23 * ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/pubs/guardians.ps.gz
24 *
25 * Original design: Mikael Djurfeldt
26 * Original implementation: Michael Livshin
27 * Hacked on since by: everybody
28 *
29 * By this point, the semantics are actually quite different from
30 * those described in the abovementioned paper. The semantic changes
31 * are there to improve safety and intuitiveness. The interface is
32 * still (mostly) the one described by the paper, however.
33 *
34 * Boiled down again: Marius Vollmer
35 *
36 * Now they should again behave like those described in the paper.
37 * Scheme guardians should be simple and friendly, not like the greedy
38 * monsters we had...
39 *
40 * Rewritten for the Boehm-Wiser GC by Ludovic Courtès.
41 * FIXME: This is currently not thread-safe.
42 */
43
44
45 #include "libguile/_scm.h"
46 #include "libguile/async.h"
47 #include "libguile/ports.h"
48 #include "libguile/print.h"
49 #include "libguile/smob.h"
50 #include "libguile/validate.h"
51 #include "libguile/root.h"
52 #include "libguile/hashtab.h"
53 #include "libguile/weaks.h"
54 #include "libguile/deprecation.h"
55 #include "libguile/eval.h"
56
57 #include "libguile/guardians.h"
58 #include <gc/gc.h>
59
60
61
62
63 static scm_t_bits tc16_guardian;
64
65 typedef struct t_guardian
66 {
67 unsigned long live;
68 SCM zombies;
69 struct t_guardian *next;
70 } t_guardian;
71
72 #define GUARDIAN_P(x) SCM_SMOB_PREDICATE(tc16_guardian, x)
73 #define GUARDIAN_DATA(x) ((t_guardian *) SCM_CELL_WORD_1 (x))
74
75
76
77
78 static int
79 guardian_print (SCM guardian, SCM port, scm_print_state *pstate SCM_UNUSED)
80 {
81 t_guardian *g = GUARDIAN_DATA (guardian);
82
83 scm_puts ("#<guardian ", port);
84 scm_uintprint ((scm_t_bits) g, 16, port);
85
86 scm_puts (" (reachable: ", port);
87 scm_display (scm_from_uint (g->live), port);
88 scm_puts (" unreachable: ", port);
89 scm_display (scm_length (g->zombies), port);
90 scm_puts (")", port);
91
92 scm_puts (">", port);
93
94 return 1;
95 }
96
97 /* Handle finalization of OBJ which is guarded by the guardians listed in
98 GUARDIAN_LIST. */
99 static void
100 finalize_guarded (GC_PTR obj, GC_PTR guardian_list)
101 {
102 SCM cell_pool;
103
104 #if 0
105 printf ("finalizing guarded %p (%u guardians)\n",
106 obj, scm_to_uint (scm_length (guardian_list)));
107 scm_write (guardian_list, scm_current_output_port ());
108 #endif
109
110 /* Preallocate a bunch of cells so that we can make sure that no garbage
111 collection (and, thus, nested calls to `finalize_guarded ()') occurs
112 while executing the following loop. This is quite inefficient (call to
113 `scm_length ()') but that shouldn't be a problem in most cases. */
114 cell_pool = scm_make_list (scm_length (guardian_list), SCM_UNSPECIFIED);
115
116 /* Tell each guardian interested in OBJ that OBJ is no longer
117 reachable. */
118 for (;
119 guardian_list != SCM_EOL;
120 guardian_list = SCM_CDR (guardian_list))
121 {
122 SCM zombies;
123 t_guardian *g = GUARDIAN_DATA (SCM_CAR (guardian_list));
124
125 if (g->live == 0)
126 abort ();
127
128 /* Get a fresh cell from CELL_POOL. */
129 zombies = cell_pool;
130 cell_pool = SCM_CDR (cell_pool);
131
132 /* Compute and update G's zombie list. */
133 SCM_SETCAR (zombies, SCM_PACK (obj));
134 SCM_SETCDR (zombies, g->zombies);
135 g->zombies = zombies;
136
137 g->live--;
138 g->zombies = zombies;
139 }
140
141 #if 0
142 printf ("end of finalize (%p)\n", obj);
143 #endif
144 }
145
146 /* Add OBJ as a guarded object of GUARDIAN. */
147 static void
148 scm_i_guard (SCM guardian, SCM obj)
149 {
150 t_guardian *g = GUARDIAN_DATA (guardian);
151
152 if (!SCM_IMP (obj))
153 {
154 /* Register a finalizer and pass a list of guardians interested in OBJ
155 as the ``client data'' argument. */
156 GC_finalization_proc prev_finalizer;
157 GC_PTR prev_data;
158 SCM guardians_for_obj;
159
160 g->live++;
161 guardians_for_obj = scm_cons (guardian, SCM_EOL);
162
163 GC_REGISTER_FINALIZER_NO_ORDER ((GC_PTR)obj, finalize_guarded,
164 (GC_PTR)guardians_for_obj,
165 &prev_finalizer, &prev_data);
166
167 if ((prev_finalizer == finalize_guarded) && (prev_data != NULL))
168 {
169 /* OBJ is already guarded by another guardian: add GUARDIAN to its
170 list of guardians. */
171 SCM prev_guardian_list = SCM_PACK (prev_data);
172
173 if (!scm_is_pair (prev_guardian_list))
174 abort ();
175
176 SCM_SETCDR (guardians_for_obj, prev_guardian_list);
177 }
178 }
179 }
180
181 static SCM
182 scm_i_get_one_zombie (SCM guardian)
183 {
184 t_guardian *g = GUARDIAN_DATA (guardian);
185 SCM res = SCM_BOOL_F;
186
187 if (g->zombies != SCM_EOL)
188 {
189 /* Note: We return zombies in reverse order. */
190 res = SCM_CAR (g->zombies);
191 g->zombies = SCM_CDR (g->zombies);
192 }
193
194 return res;
195 }
196
197 /* This is the Scheme entry point for each guardian: If OBJ is an
198 * object, it's added to the guardian's live list. If OBJ is unbound,
199 * the next available unreachable object (or #f if none) is returned.
200 *
201 * If the second optional argument THROW_P is true (the default), then
202 * an error is raised if GUARDIAN is greedy and OBJ is already greedily
203 * guarded. If THROW_P is false, #f is returned instead of raising the
204 * error, and #t is returned if everything is fine.
205 */
206 static SCM
207 guardian_apply (SCM guardian, SCM obj, SCM throw_p)
208 {
209 #if ENABLE_DEPRECATED
210 if (!SCM_UNBNDP (throw_p))
211 scm_c_issue_deprecation_warning
212 ("Using the 'throw?' argument of a guardian is deprecated "
213 "and ineffective.");
214 #endif
215
216 if (!SCM_UNBNDP (obj))
217 {
218 scm_i_guard (guardian, obj);
219 return SCM_UNSPECIFIED;
220 }
221 else
222 return scm_i_get_one_zombie (guardian);
223 }
224
225 SCM_DEFINE (scm_make_guardian, "make-guardian", 0, 0, 0,
226 (),
227 "Create a new guardian. A guardian protects a set of objects from\n"
228 "garbage collection, allowing a program to apply cleanup or other\n"
229 "actions.\n"
230 "\n"
231 "@code{make-guardian} returns a procedure representing the guardian.\n"
232 "Calling the guardian procedure with an argument adds the argument to\n"
233 "the guardian's set of protected objects. Calling the guardian\n"
234 "procedure without an argument returns one of the protected objects\n"
235 "which are ready for garbage collection, or @code{#f} if no such object\n"
236 "is available. Objects which are returned in this way are removed from\n"
237 "the guardian.\n"
238 "\n"
239 "You can put a single object into a guardian more than once and you can\n"
240 "put a single object into more than one guardian. The object will then\n"
241 "be returned multiple times by the guardian procedures.\n"
242 "\n"
243 "An object is eligible to be returned from a guardian when it is no\n"
244 "longer referenced from outside any guardian.\n"
245 "\n"
246 "There is no guarantee about the order in which objects are returned\n"
247 "from a guardian. If you want to impose an order on finalization\n"
248 "actions, for example, you can do that by keeping objects alive in some\n"
249 "global data structure until they are no longer needed for finalizing\n"
250 "other objects.\n"
251 "\n"
252 "Being an element in a weak vector, a key in a hash table with weak\n"
253 "keys, or a value in a hash table with weak value does not prevent an\n"
254 "object from being returned by a guardian. But as long as an object\n"
255 "can be returned from a guardian it will not be removed from such a\n"
256 "weak vector or hash table. In other words, a weak link does not\n"
257 "prevent an object from being considered collectable, but being inside\n"
258 "a guardian prevents a weak link from being broken.\n"
259 "\n"
260 "A key in a weak key hash table can be though of as having a strong\n"
261 "reference to its associated value as long as the key is accessible.\n"
262 "Consequently, when the key only accessible from within a guardian, the\n"
263 "reference from the key to the value is also considered to be coming\n"
264 "from within a guardian. Thus, if there is no other reference to the\n"
265 "value, it is eligible to be returned from a guardian.\n")
266 #define FUNC_NAME s_scm_make_guardian
267 {
268 t_guardian *g = scm_gc_malloc (sizeof (t_guardian), "guardian");
269 SCM z;
270
271 /* A tconc starts out with one tail pair. */
272 g->live = 0;
273 g->zombies = SCM_EOL;
274
275 g->next = NULL;
276
277 SCM_NEWSMOB (z, tc16_guardian, g);
278
279 return z;
280 }
281 #undef FUNC_NAME
282
283 void
284 scm_init_guardians ()
285 {
286 /* We use unordered finalization `a la Java. */
287 GC_java_finalization = 1;
288
289 tc16_guardian = scm_make_smob_type ("guardian", 0);
290
291 scm_set_smob_print (tc16_guardian, guardian_print);
292 #if ENABLE_DEPRECATED
293 scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 2, 0);
294 #else
295 scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 1, 0);
296 #endif
297
298 #include "libguile/guardians.x"
299 }
300
301 /*
302 Local Variables:
303 c-file-style: "gnu"
304 End:
305 */