merge from 1.8 branch
[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
20 /* This is an implementation of guardians as described in
21 * R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) "Guardians in
22 * a Generation-Based Garbage Collector" ACM SIGPLAN Conference on
23 * Programming Language Design and Implementation, June 1993
24 * ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/pubs/guardians.ps.gz
25 *
26 * Original design: Mikael Djurfeldt
27 * Original implementation: Michael Livshin
28 * Hacked on since by: everybody
29 *
30 * By this point, the semantics are actually quite different from
31 * those described in the abovementioned paper. The semantic changes
32 * are there to improve safety and intuitiveness. The interface is
33 * still (mostly) the one described by the paper, however.
34 *
35 * Boiled down again: Marius Vollmer
36 *
37 * Now they should again behave like those described in the paper.
38 * Scheme guardians should be simple and friendly, not like the greedy
39 * monsters we had...
40 */
41
42
43 #include "libguile/_scm.h"
44 #include "libguile/async.h"
45 #include "libguile/ports.h"
46 #include "libguile/print.h"
47 #include "libguile/smob.h"
48 #include "libguile/validate.h"
49 #include "libguile/root.h"
50 #include "libguile/hashtab.h"
51 #include "libguile/weaks.h"
52 #include "libguile/deprecation.h"
53 #include "libguile/eval.h"
54
55 #include "libguile/guardians.h"
56
57
58 /* The live and zombies FIFOs are implemented as tconcs as described
59 in Dybvig's paper. This decouples addition and removal of elements
60 so that no synchronization between these needs to take place.
61 */
62
63 typedef struct t_tconc
64 {
65 SCM head;
66 SCM tail;
67 } t_tconc;
68
69 #define TCONC_EMPTYP(tc) (scm_is_eq ((tc).head, (tc).tail))
70
71 #define TCONC_IN(tc, obj, pair) \
72 do { \
73 SCM_SETCAR ((tc).tail, obj); \
74 SCM_SET_CELL_OBJECT_1 (pair, SCM_EOL); \
75 SCM_SET_CELL_OBJECT_0 (pair, SCM_BOOL_F); \
76 SCM_SETCDR ((tc).tail, pair); \
77 (tc).tail = pair; \
78 } while (0)
79
80 #define TCONC_OUT(tc, res) \
81 do { \
82 (res) = SCM_CAR ((tc).head); \
83 (tc).head = SCM_CDR ((tc).head); \
84 } while (0)
85
86
87 static scm_t_bits tc16_guardian;
88
89 typedef struct t_guardian
90 {
91 t_tconc live;
92 t_tconc zombies;
93 struct t_guardian *next;
94 } t_guardian;
95
96 #define GUARDIAN_P(x) SCM_SMOB_PREDICATE(tc16_guardian, x)
97 #define GUARDIAN_DATA(x) ((t_guardian *) SCM_CELL_WORD_1 (x))
98
99 static t_guardian *guardians;
100
101 void
102 scm_i_init_guardians_for_gc ()
103 {
104 guardians = NULL;
105 }
106
107 /* mark a guardian by adding it to the live guardian list. */
108 static SCM
109 guardian_mark (SCM ptr)
110 {
111 t_guardian *g = GUARDIAN_DATA (ptr);
112 g->next = guardians;
113 guardians = g;
114
115 return SCM_BOOL_F;
116 }
117
118 /* Identify inaccessible objects and move them from the live list to
119 the zombie list. An object is inaccessible when it is unmarked at
120 this point. Therefore, the inaccessible objects are not marked yet
121 since that would prevent them from being recognized as
122 inaccessible.
123
124 The pairs that form the life list itself are marked, tho.
125 */
126 void
127 scm_i_identify_inaccessible_guardeds ()
128 {
129 t_guardian *g;
130
131 for (g = guardians; g; g = g->next)
132 {
133 SCM pair, next_pair;
134 SCM *prev_ptr;
135
136 for (pair = g->live.head, prev_ptr = &g->live.head;
137 !scm_is_eq (pair, g->live.tail);
138 pair = next_pair)
139 {
140 SCM obj = SCM_CAR (pair);
141 next_pair = SCM_CDR (pair);
142 if (!SCM_GC_MARK_P (obj))
143 {
144 /* Unmarked, move to 'inaccessible' list.
145 */
146 *prev_ptr = next_pair;
147 TCONC_IN (g->zombies, obj, pair);
148 }
149 else
150 {
151 SCM_SET_GC_MARK (pair);
152 prev_ptr = SCM_CDRLOC (pair);
153 }
154 }
155 SCM_SET_GC_MARK (pair);
156 }
157 }
158
159 int
160 scm_i_mark_inaccessible_guardeds ()
161 {
162 t_guardian *g;
163 int again = 0;
164
165 /* We never need to see the guardians again that are processed here,
166 so we clear the list. Calling scm_gc_mark below might find new
167 guardians, however (and other things), and we inform the GC about
168 this by returning non-zero. See scm_mark_all in gc-mark.c
169 */
170
171 g = guardians;
172 guardians = NULL;
173
174 for (; g; g = g->next)
175 {
176 SCM pair;
177
178 for (pair = g->zombies.head;
179 !scm_is_eq (pair, g->zombies.tail);
180 pair = SCM_CDR (pair))
181 {
182 if (!SCM_GC_MARK_P (pair))
183 {
184 scm_gc_mark (SCM_CAR (pair));
185 SCM_SET_GC_MARK (pair);
186 again = 1;
187 }
188 }
189 SCM_SET_GC_MARK (pair);
190 }
191 return again;
192 }
193
194 static size_t
195 guardian_free (SCM ptr)
196 {
197 scm_gc_free (GUARDIAN_DATA (ptr), sizeof (t_guardian), "guardian");
198 return 0;
199 }
200
201 static int
202 guardian_print (SCM guardian, SCM port, scm_print_state *pstate SCM_UNUSED)
203 {
204 t_guardian *g = GUARDIAN_DATA (guardian);
205
206 scm_puts ("#<guardian ", port);
207 scm_uintprint ((scm_t_bits) g, 16, port);
208
209 scm_puts (" (reachable: ", port);
210 scm_display (scm_length (SCM_CDR (g->live.head)), port);
211 scm_puts (" unreachable: ", port);
212 scm_display (scm_length (SCM_CDR (g->zombies.head)), port);
213 scm_puts (")", port);
214
215 scm_puts (">", port);
216
217 return 1;
218 }
219
220 static void
221 scm_i_guard (SCM guardian, SCM obj)
222 {
223 t_guardian *g = GUARDIAN_DATA (guardian);
224
225 if (!SCM_IMP (obj))
226 {
227 SCM z;
228 z = scm_cons (SCM_BOOL_F, SCM_BOOL_F);
229 TCONC_IN (g->live, obj, z);
230 }
231 }
232
233 static SCM
234 scm_i_get_one_zombie (SCM guardian)
235 {
236 t_guardian *g = GUARDIAN_DATA (guardian);
237 SCM res = SCM_BOOL_F;
238
239 if (!TCONC_EMPTYP (g->zombies))
240 TCONC_OUT (g->zombies, res);
241
242 return res;
243 }
244
245 /* This is the Scheme entry point for each guardian: If OBJ is an
246 * object, it's added to the guardian's live list. If OBJ is unbound,
247 * the next available unreachable object (or #f if none) is returned.
248 *
249 * If the second optional argument THROW_P is true (the default), then
250 * an error is raised if GUARDIAN is greedy and OBJ is already greedily
251 * guarded. If THROW_P is false, #f is returned instead of raising the
252 * error, and #t is returned if everything is fine.
253 */
254 static SCM
255 guardian_apply (SCM guardian, SCM obj, SCM throw_p)
256 {
257 #if ENABLE_DEPRECATED
258 if (!SCM_UNBNDP (throw_p))
259 scm_c_issue_deprecation_warning
260 ("Using the 'throw?' argument of a guardian is deprecated "
261 "and ineffective.");
262 #endif
263
264 if (!SCM_UNBNDP (obj))
265 {
266 scm_i_guard (guardian, obj);
267 return SCM_UNSPECIFIED;
268 }
269 else
270 return scm_i_get_one_zombie (guardian);
271 }
272
273 SCM_DEFINE (scm_make_guardian, "make-guardian", 0, 0, 0,
274 (),
275 "Create a new guardian. A guardian protects a set of objects from\n"
276 "garbage collection, allowing a program to apply cleanup or other\n"
277 "actions.\n"
278 "\n"
279 "@code{make-guardian} returns a procedure representing the guardian.\n"
280 "Calling the guardian procedure with an argument adds the argument to\n"
281 "the guardian's set of protected objects. Calling the guardian\n"
282 "procedure without an argument returns one of the protected objects\n"
283 "which are ready for garbage collection, or @code{#f} if no such object\n"
284 "is available. Objects which are returned in this way are removed from\n"
285 "the guardian.\n"
286 "\n"
287 "You can put a single object into a guardian more than once and you can\n"
288 "put a single object into more than one guardian. The object will then\n"
289 "be returned multiple times by the guardian procedures.\n"
290 "\n"
291 "An object is eligible to be returned from a guardian when it is no\n"
292 "longer referenced from outside any guardian.\n"
293 "\n"
294 "There is no guarantee about the order in which objects are returned\n"
295 "from a guardian. If you want to impose an order on finalization\n"
296 "actions, for example, you can do that by keeping objects alive in some\n"
297 "global data structure until they are no longer needed for finalizing\n"
298 "other objects.\n"
299 "\n"
300 "Being an element in a weak vector, a key in a hash table with weak\n"
301 "keys, or a value in a hash table with weak value does not prevent an\n"
302 "object from being returned by a guardian. But as long as an object\n"
303 "can be returned from a guardian it will not be removed from such a\n"
304 "weak vector or hash table. In other words, a weak link does not\n"
305 "prevent an object from being considered collectable, but being inside\n"
306 "a guardian prevents a weak link from being broken.\n"
307 "\n"
308 "A key in a weak key hash table can be though of as having a strong\n"
309 "reference to its associated value as long as the key is accessible.\n"
310 "Consequently, when the key only accessible from within a guardian, the\n"
311 "reference from the key to the value is also considered to be coming\n"
312 "from within a guardian. Thus, if there is no other reference to the\n"
313 "value, it is eligible to be returned from a guardian.\n")
314 #define FUNC_NAME s_scm_make_guardian
315 {
316 t_guardian *g = scm_gc_malloc (sizeof (t_guardian), "guardian");
317 SCM z1 = scm_cons (SCM_BOOL_F, SCM_EOL);
318 SCM z2 = scm_cons (SCM_BOOL_F, SCM_EOL);
319 SCM z;
320
321 /* A tconc starts out with one tail pair. */
322 g->live.head = g->live.tail = z1;
323 g->zombies.head = g->zombies.tail = z2;
324
325 g->next = NULL;
326
327 SCM_NEWSMOB (z, tc16_guardian, g);
328
329 return z;
330 }
331 #undef FUNC_NAME
332
333 void
334 scm_init_guardians ()
335 {
336 tc16_guardian = scm_make_smob_type ("guardian", 0);
337 scm_set_smob_mark (tc16_guardian, guardian_mark);
338 scm_set_smob_free (tc16_guardian, guardian_free);
339 scm_set_smob_print (tc16_guardian, guardian_print);
340 #if ENABLE_DEPRECATED
341 scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 2, 0);
342 #else
343 scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 1, 0);
344 #endif
345
346 #include "libguile/guardians.x"
347 }
348
349 /*
350 Local Variables:
351 c-file-style: "gnu"
352 End:
353 */