Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / guardians.c
1 /* Copyright (C) 1998,1999,2000,2001, 2006, 2008 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 #ifdef HAVE_CONFIG_H
45 # include <config.h>
46 #endif
47
48 #include "libguile/_scm.h"
49 #include "libguile/async.h"
50 #include "libguile/ports.h"
51 #include "libguile/print.h"
52 #include "libguile/smob.h"
53 #include "libguile/validate.h"
54 #include "libguile/root.h"
55 #include "libguile/hashtab.h"
56 #include "libguile/weaks.h"
57 #include "libguile/deprecation.h"
58 #include "libguile/eval.h"
59
60 #include "libguile/guardians.h"
61 #include "libguile/boehm-gc.h"
62
63
64
65
66 static scm_t_bits tc16_guardian;
67
68 typedef struct t_guardian
69 {
70 unsigned long live;
71 SCM zombies;
72 struct t_guardian *next;
73 } t_guardian;
74
75 #define GUARDIAN_P(x) SCM_SMOB_PREDICATE(tc16_guardian, x)
76 #define GUARDIAN_DATA(x) ((t_guardian *) SCM_CELL_WORD_1 (x))
77
78
79
80
81 static int
82 guardian_print (SCM guardian, SCM port, scm_print_state *pstate SCM_UNUSED)
83 {
84 t_guardian *g = GUARDIAN_DATA (guardian);
85
86 scm_puts ("#<guardian ", port);
87 scm_uintprint ((scm_t_bits) g, 16, port);
88
89 scm_puts (" (reachable: ", port);
90 scm_display (scm_from_uint (g->live), port);
91 scm_puts (" unreachable: ", port);
92 scm_display (scm_length (g->zombies), port);
93 scm_puts (")", port);
94
95 scm_puts (">", port);
96
97 return 1;
98 }
99
100 /* Handle finalization of OBJ which is guarded by the guardians listed in
101 GUARDIAN_LIST. */
102 static void
103 finalize_guarded (GC_PTR ptr, GC_PTR finalizer_data)
104 {
105 SCM cell_pool;
106 SCM obj, guardian_list, proxied_finalizer;
107
108 obj = PTR2SCM (ptr);
109 guardian_list = SCM_CDR (PTR2SCM (finalizer_data));
110 proxied_finalizer = SCM_CAR (PTR2SCM (finalizer_data));
111
112 #if 0
113 printf ("finalizing guarded %p (%u guardians)\n",
114 ptr, scm_to_uint (scm_length (guardian_list)));
115 #endif
116
117 /* Preallocate a bunch of cells so that we can make sure that no garbage
118 collection (and, thus, nested calls to `finalize_guarded ()') occurs
119 while executing the following loop. This is quite inefficient (call to
120 `scm_length ()') but that shouldn't be a problem in most cases. */
121 cell_pool = scm_make_list (scm_length (guardian_list), SCM_UNSPECIFIED);
122
123 /* Tell each guardian interested in OBJ that OBJ is no longer
124 reachable. */
125 for (;
126 guardian_list != SCM_EOL;
127 guardian_list = SCM_CDR (guardian_list))
128 {
129 SCM zombies;
130 t_guardian *g;
131
132 if (SCM_WEAK_PAIR_CAR_DELETED_P (guardian_list))
133 /* The guardian itself vanished in the meantime. */
134 continue;
135
136 g = GUARDIAN_DATA (SCM_CAR (guardian_list));
137 if (g->live == 0)
138 abort ();
139
140 /* Get a fresh cell from CELL_POOL. */
141 zombies = cell_pool;
142 cell_pool = SCM_CDR (cell_pool);
143
144 /* Compute and update G's zombie list. */
145 SCM_SETCAR (zombies, SCM_PACK (obj));
146 SCM_SETCDR (zombies, g->zombies);
147 g->zombies = zombies;
148
149 g->live--;
150 g->zombies = zombies;
151 }
152
153 if (proxied_finalizer != SCM_BOOL_F)
154 {
155 /* Re-register the finalizer that was in place before we installed this
156 one. */
157 GC_finalization_proc finalizer, prev_finalizer;
158 GC_PTR finalizer_data, prev_finalizer_data;
159
160 finalizer = (GC_finalization_proc) SCM2PTR (SCM_CAR (proxied_finalizer));
161 finalizer_data = SCM2PTR (SCM_CDR (proxied_finalizer));
162
163 if (finalizer == NULL)
164 abort ();
165
166 GC_REGISTER_FINALIZER_NO_ORDER (ptr, finalizer, finalizer_data,
167 &prev_finalizer, &prev_finalizer_data);
168
169 #if 0
170 printf (" reinstalled proxied finalizer %p for %p\n", finalizer, ptr);
171 #endif
172 }
173
174 #if 0
175 printf ("end of finalize (%p)\n", ptr);
176 #endif
177 }
178
179 /* Add OBJ as a guarded object of GUARDIAN. */
180 static void
181 scm_i_guard (SCM guardian, SCM obj)
182 {
183 t_guardian *g = GUARDIAN_DATA (guardian);
184
185 if (SCM_NIMP (obj))
186 {
187 /* Register a finalizer and pass a pair as the ``client data''
188 argument. The pair contains in its car `#f' or a pair describing a
189 ``proxied'' finalizer (see below); its cdr contains a list of
190 guardians interested in OBJ.
191
192 A ``proxied'' finalizer is a finalizer that was registered for OBJ
193 before OBJ became guarded (e.g., a SMOB `free' function). We are
194 assuming here that finalizers are only used internally, either at
195 the very beginning of an object's lifetime (e.g., see `SCM_NEWSMOB')
196 or by this function. */
197 GC_finalization_proc prev_finalizer;
198 GC_PTR prev_data;
199 SCM guardians_for_obj, finalizer_data;
200
201 g->live++;
202
203 /* Note: GUARDIANS_FOR_OBJ is a weak list so that a guardian can be
204 collected before the objects it guards (see `guardians.test'). */
205 guardians_for_obj = scm_weak_car_pair (guardian, SCM_EOL);
206 finalizer_data = scm_cons (SCM_BOOL_F, guardians_for_obj);
207
208 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (obj), finalize_guarded,
209 SCM2PTR (finalizer_data),
210 &prev_finalizer, &prev_data);
211
212 if (prev_finalizer == finalize_guarded)
213 {
214 /* OBJ is already guarded by another guardian: add GUARDIAN to its
215 list of guardians. */
216 SCM prev_guardian_list, prev_finalizer_data;
217
218 if (prev_data == NULL)
219 abort ();
220
221 prev_finalizer_data = PTR2SCM (prev_data);
222 if (!scm_is_pair (prev_finalizer_data))
223 abort ();
224
225 prev_guardian_list = SCM_CDR (prev_finalizer_data);
226 SCM_SETCDR (guardians_for_obj, prev_guardian_list);
227
228 /* Also copy information about proxied finalizers. */
229 SCM_SETCAR (finalizer_data, SCM_CAR (prev_finalizer_data));
230 }
231 else if (prev_finalizer != NULL)
232 {
233 /* There was already a finalizer registered for OBJ so we will
234 ``proxy'' it, i.e., record it so that we can re-register it once
235 `finalize_guarded ()' has finished. */
236 SCM proxied_finalizer;
237
238 proxied_finalizer = scm_cons (PTR2SCM (prev_finalizer),
239 PTR2SCM (prev_data));
240 SCM_SETCAR (finalizer_data, proxied_finalizer);
241 }
242 }
243 }
244
245 static SCM
246 scm_i_get_one_zombie (SCM guardian)
247 {
248 t_guardian *g = GUARDIAN_DATA (guardian);
249 SCM res = SCM_BOOL_F;
250
251 if (g->zombies != SCM_EOL)
252 {
253 /* Note: We return zombies in reverse order. */
254 res = SCM_CAR (g->zombies);
255 g->zombies = SCM_CDR (g->zombies);
256 }
257
258 return res;
259 }
260
261 /* This is the Scheme entry point for each guardian: If OBJ is an
262 * object, it's added to the guardian's live list. If OBJ is unbound,
263 * the next available unreachable object (or #f if none) is returned.
264 *
265 * If the second optional argument THROW_P is true (the default), then
266 * an error is raised if GUARDIAN is greedy and OBJ is already greedily
267 * guarded. If THROW_P is false, #f is returned instead of raising the
268 * error, and #t is returned if everything is fine.
269 */
270 static SCM
271 guardian_apply (SCM guardian, SCM obj, SCM throw_p)
272 {
273 #if ENABLE_DEPRECATED
274 if (!SCM_UNBNDP (throw_p))
275 scm_c_issue_deprecation_warning
276 ("Using the 'throw?' argument of a guardian is deprecated "
277 "and ineffective.");
278 #endif
279
280 if (!SCM_UNBNDP (obj))
281 {
282 scm_i_guard (guardian, obj);
283 return SCM_UNSPECIFIED;
284 }
285 else
286 return scm_i_get_one_zombie (guardian);
287 }
288
289 SCM_DEFINE (scm_make_guardian, "make-guardian", 0, 0, 0,
290 (),
291 "Create a new guardian. A guardian protects a set of objects from\n"
292 "garbage collection, allowing a program to apply cleanup or other\n"
293 "actions.\n"
294 "\n"
295 "@code{make-guardian} returns a procedure representing the guardian.\n"
296 "Calling the guardian procedure with an argument adds the argument to\n"
297 "the guardian's set of protected objects. Calling the guardian\n"
298 "procedure without an argument returns one of the protected objects\n"
299 "which are ready for garbage collection, or @code{#f} if no such object\n"
300 "is available. Objects which are returned in this way are removed from\n"
301 "the guardian.\n"
302 "\n"
303 "You can put a single object into a guardian more than once and you can\n"
304 "put a single object into more than one guardian. The object will then\n"
305 "be returned multiple times by the guardian procedures.\n"
306 "\n"
307 "An object is eligible to be returned from a guardian when it is no\n"
308 "longer referenced from outside any guardian.\n"
309 "\n"
310 "There is no guarantee about the order in which objects are returned\n"
311 "from a guardian. If you want to impose an order on finalization\n"
312 "actions, for example, you can do that by keeping objects alive in some\n"
313 "global data structure until they are no longer needed for finalizing\n"
314 "other objects.\n"
315 "\n"
316 "Being an element in a weak vector, a key in a hash table with weak\n"
317 "keys, or a value in a hash table with weak value does not prevent an\n"
318 "object from being returned by a guardian. But as long as an object\n"
319 "can be returned from a guardian it will not be removed from such a\n"
320 "weak vector or hash table. In other words, a weak link does not\n"
321 "prevent an object from being considered collectable, but being inside\n"
322 "a guardian prevents a weak link from being broken.\n"
323 "\n"
324 "A key in a weak key hash table can be though of as having a strong\n"
325 "reference to its associated value as long as the key is accessible.\n"
326 "Consequently, when the key only accessible from within a guardian, the\n"
327 "reference from the key to the value is also considered to be coming\n"
328 "from within a guardian. Thus, if there is no other reference to the\n"
329 "value, it is eligible to be returned from a guardian.\n")
330 #define FUNC_NAME s_scm_make_guardian
331 {
332 t_guardian *g = scm_gc_malloc (sizeof (t_guardian), "guardian");
333 SCM z;
334
335 /* A tconc starts out with one tail pair. */
336 g->live = 0;
337 g->zombies = SCM_EOL;
338
339 g->next = NULL;
340
341 SCM_NEWSMOB (z, tc16_guardian, g);
342
343 return z;
344 }
345 #undef FUNC_NAME
346
347 void
348 scm_init_guardians ()
349 {
350 /* We use unordered finalization `a la Java. */
351 GC_java_finalization = 1;
352
353 tc16_guardian = scm_make_smob_type ("guardian", 0);
354
355 scm_set_smob_print (tc16_guardian, guardian_print);
356 #if ENABLE_DEPRECATED
357 scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 2, 0);
358 #else
359 scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 1, 0);
360 #endif
361
362 #include "libguile/guardians.x"
363 }
364
365 /*
366 Local Variables:
367 c-file-style: "gnu"
368 End:
369 */