2 @node Memory Management
3 @chapter Memory Management and Garbage Collection
12 @node Garbage Collection
13 @section Garbage Collection
15 [FIXME: this is pasted in from Tom Lord's original guile.texi and should
18 @deffn {Scheme Procedure} gc
19 @deffnx {C Function} scm_gc ()
20 Scans all of SCM objects and reclaims for further use those that are
24 @deffn {Scheme Procedure} gc-stats
25 @deffnx {C Function} scm_gc_stats ()
26 Return an association list of statistics about Guile's current
30 @deffn {Scheme Procedure} object-address obj
31 @deffnx {C Function} scm_object_address (obj)
32 Return an integer that for the lifetime of @var{obj} is uniquely
33 returned by this function for @var{obj}
38 @section Weak References
40 [FIXME: This chapter is based on Mikael Djurfeldt's answer to a question
41 by Michael Livshin. Any mistakes are not theirs, of course. ]
43 Weak references let you attach bookkeeping information to data so that
44 the additional information automatically disappears when the original
45 data is no longer in use and gets garbage collected. In a weak key hash,
46 the hash entry for that key disappears as soon as the key is no longer
47 referenced from anywhere else. For weak value hashes, the same happens
48 as soon as the value is no longer in use. Entries in a doubly weak hash
49 disappear when either the key or the value are not used anywhere else
52 Property lists offer the same kind of functionality as weak key hashes
53 in many situations. (@pxref{Property Lists})
55 Here's an example (a little bit strained perhaps, but one of the
56 examples is actually used in Guile):
58 Assume that you're implementing a debugging system where you want to
59 associate information about filename and position of source code
60 expressions with the expressions themselves.
62 Hashtables can be used for that, but if you use ordinary hash tables
63 it will be impossible for the scheme interpreter to "forget" old
64 source when, for example, a file is reloaded.
66 To implement the mapping from source code expressions to positional
67 information it is necessary to use weak-key tables since we don't want
68 the expressions to be remembered just because they are in our table.
70 To implement a mapping from source file line numbers to source code
71 expressions you would use a weak-value table.
73 To implement a mapping from source code expressions to the procedures
74 they constitute a doubly-weak table has to be used.
83 @subsection Weak key hashes
85 @deffn {Scheme Procedure} make-weak-key-hash-table size
86 @deffnx {Scheme Procedure} make-weak-value-hash-table size
87 @deffnx {Scheme Procedure} make-doubly-weak-hash-table size
88 @deffnx {C Function} scm_make_weak_key_hash_table (size)
89 @deffnx {C Function} scm_make_weak_value_hash_table (size)
90 @deffnx {C Function} scm_make_doubly_weak_hash_table (size)
91 Return a weak hash table with @var{size} buckets. As with any
92 hash table, choosing a good size for the table requires some
95 You can modify weak hash tables in exactly the same way you
96 would modify regular hash tables. (@pxref{Hash Tables})
99 @deffn {Scheme Procedure} weak-key-hash-table? obj
100 @deffnx {Scheme Procedure} weak-value-hash-table? obj
101 @deffnx {Scheme Procedure} doubly-weak-hash-table? obj
102 @deffnx {C Function} scm_weak_key_hash_table_p (obj)
103 @deffnx {C Function} scm_weak_value_hash_table_p (obj)
104 @deffnx {C Function} scm_doubly_weak_hash_table_p (obj)
105 Return @code{#t} if @var{obj} is the specified weak hash
106 table. Note that a doubly weak hash table is neither a weak key
107 nor a weak value hash table.
110 @deffn {Scheme Procedure} make-weak-value-hash-table k
113 @deffn {Scheme Procedure} weak-value-hash-table? x
116 @deffn {Scheme Procedure} make-doubly-weak-hash-table k
119 @deffn {Scheme Procedure} doubly-weak-hash-table? x
124 @subsection Weak vectors
126 Weak vectors are mainly useful in Guile's implementation of weak hash
129 @deffn {Scheme Procedure} make-weak-vector size [fill]
130 @deffnx {C Function} scm_make_weak_vector (size, fill)
131 Return a weak vector with @var{size} elements. If the optional
132 argument @var{fill} is given, all entries in the vector will be
133 set to @var{fill}. The default value for @var{fill} is the
137 @deffn {Scheme Procedure} weak-vector . l
138 @deffnx {Scheme Procedure} list->weak-vector l
139 @deffnx {C Function} scm_weak_vector (l)
140 Construct a weak vector from a list: @code{weak-vector} uses
141 the list of its arguments while @code{list->weak-vector} uses
142 its only argument @var{l} (a list) to construct a weak vector
143 the same way @code{list->vector} would.
146 @deffn {Scheme Procedure} weak-vector? obj
147 @deffnx {C Function} scm_weak_vector_p (obj)
148 Return @code{#t} if @var{obj} is a weak vector. Note that all
149 weak hashes are also weak vectors.
156 @deffn {Scheme Procedure} make-guardian [greedy?]
157 @deffnx {C Function} scm_make_guardian (greedy_p)
158 Create a new guardian.
159 A guardian protects a set of objects from garbage collection,
160 allowing a program to apply cleanup or other actions.
162 @code{make-guardian} returns a procedure representing the guardian.
163 Calling the guardian procedure with an argument adds the
164 argument to the guardian's set of protected objects.
165 Calling the guardian procedure without an argument returns
166 one of the protected objects which are ready for garbage
167 collection, or @code{#f} if no such object is available.
168 Objects which are returned in this way are removed from
171 @code{make-guardian} takes one optional argument that says whether the
172 new guardian should be greedy or sharing. If there is any chance
173 that any object protected by the guardian may be resurrected,
174 then you should make the guardian greedy (this is the default).
176 See R. Kent Dybvig, Carl Bruggeman, and David Eby (1993)
177 "Guardians in a Generation-Based Garbage Collector".
178 ACM SIGPLAN Conference on Programming Language Design
179 and Implementation, June 1993.
181 (the semantics are slightly different at this point, but the
182 paper still (mostly) accurately describes the interface).
185 @deffn {Scheme Procedure} destroy-guardian! guardian
186 @deffnx {C Function} scm_destroy_guardian_x (guardian)
187 Destroys @var{guardian}, by making it impossible to put any more
188 objects in it or get any objects from it. It also unguards any
189 objects guarded by @var{guardian}.
192 @deffn {Scheme Procedure} guardian-greedy? guardian
193 @deffnx {C Function} scm_guardian_greedy_p (guardian)
194 Return @code{#t} if @var{guardian} is a greedy guardian, otherwise @code{#f}.
197 @deffn {Scheme Procedure} guardian-destroyed? guardian
198 @deffnx {C Function} scm_guardian_destroyed_p (guardian)
199 Return @code{#t} if @var{guardian} has been destroyed, otherwise @code{#f}.
207 @deffn {Scheme Procedure} entity? obj
208 @deffnx {C Function} scm_entity_p (obj)
209 Return @code{#t} if @var{obj} is an entity.
212 @deffn {Scheme Procedure} operator? obj
213 @deffnx {C Function} scm_operator_p (obj)
214 Return @code{#t} if @var{obj} is an operator.
217 @deffn {Scheme Procedure} set-object-procedure! obj proc
218 @deffnx {C Function} scm_set_object_procedure_x (obj, proc)
219 Set the object procedure of @var{obj} to @var{proc}.
220 @var{obj} must be either an entity or an operator.
223 @deffn {Scheme Procedure} make-class-object metaclass layout
224 @deffnx {C Function} scm_make_class_object (metaclass, layout)
225 Create a new class object of class @var{metaclass}, with the
226 slot layout specified by @var{layout}.
229 @deffn {Scheme Procedure} make-subclass-object class layout
230 @deffnx {C Function} scm_make_subclass_object (class, layout)
231 Create a subclass object of @var{class}, with the slot layout
232 specified by @var{layout}.
237 @c TeX-master: "guile.texi"