Remove docs `procedure-environment', which no longer exists
[bpt/guile.git] / doc / ref / api-memory.texi
CommitLineData
07d83abe
MV
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
7545ddd4 3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2009, 2010
07d83abe
MV
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
07d83abe
MV
7@node Memory Management
8@section Memory Management and Garbage Collection
9
10Guile uses a @emph{garbage collector} to manage most of its objects.
11While the garbage collector is designed to be mostly invisible, you
877f06c3 12sometimes need to interact with it explicitly.
07d83abe
MV
13
14See @ref{Garbage Collection} for a general discussion of how garbage
15collection relates to using Guile from C.
16
17@menu
18* Garbage Collection Functions::
19* Memory Blocks::
20* Weak References::
21* Guardians::
22@end menu
23
24
25@node Garbage Collection Functions
26@subsection Function related to Garbage Collection
27
28@deffn {Scheme Procedure} gc
29@deffnx {C Function} scm_gc ()
30Scans all of SCM objects and reclaims for further use those that are
31no longer accessible. You normally don't need to call this function
32explicitly. It is called automatically when appropriate.
33@end deffn
34
35@deftypefn {C Function} SCM scm_gc_protect_object (SCM @var{obj})
36Protects @var{obj} from being freed by the garbage collector, when it
37otherwise might be. When you are done with the object, call
38@code{scm_gc_unprotect_object} on the object. Calls to
39@code{scm_gc_protect}/@code{scm_gc_unprotect_object} can be nested, and
40the object remains protected until it has been unprotected as many times
41as it was protected. It is an error to unprotect an object more times
42than it has been protected. Returns the SCM object it was passed.
f07c349e
LC
43
44Note that storing @var{obj} in a C global variable has the same
45effect@footnote{In Guile up to version 1.8, C global variables were not
46scanned by the garbage collector; hence, @code{scm_gc_protect_object}
47was the only way in C to prevent a Scheme object from being freed.}.
07d83abe
MV
48@end deftypefn
49
50@deftypefn {C Function} SCM scm_gc_unprotect_object (SCM @var{obj})
51
52Unprotects an object from the garbage collector which was protected by
53@code{scm_gc_unprotect_object}. Returns the SCM object it was passed.
54@end deftypefn
55
56@deftypefn {C Function} SCM scm_permanent_object (SCM @var{obj})
57
58Similar to @code{scm_gc_protect_object} in that it causes the
59collector to always mark the object, except that it should not be
60nested (only call @code{scm_permanent_object} on an object once), and
61it has no corresponding unpermanent function. Once an object is
62declared permanent, it will never be freed. Returns the SCM object it
63was passed.
64@end deftypefn
65
66@c NOTE: The varargs scm_remember_upto_here is deliberately not
67@c documented, because we don't think it can be implemented as a nice
68@c inline compiler directive or asm block. New _3, _4 or whatever
69@c forms could certainly be added though, if needed.
70
71@deftypefn {C Macro} void scm_remember_upto_here_1 (SCM obj)
72@deftypefnx {C Macro} void scm_remember_upto_here_2 (SCM obj1, SCM obj2)
73Create a reference to the given object or objects, so they're certain
74to be present on the stack or in a register and hence will not be
75freed by the garbage collector before this point.
76
77Note that these functions can only be applied to ordinary C local
78variables (ie.@: ``automatics''). Objects held in global or static
79variables or some malloced block or the like cannot be protected with
80this mechanism.
81@end deftypefn
82
83@deffn {Scheme Procedure} gc-stats
84@deffnx {C Function} scm_gc_stats ()
85Return an association list of statistics about Guile's current
86use of storage.
c93557e7 87@end deffn
07d83abe 88
673ba2da
MV
89@deffn {Scheme Procedure} gc-live-object-stats
90@deffnx {C Function} scm_gc_live_object_stats ()
91Return an alist of statistics of the current live objects.
92@end deffn
93
07d83abe
MV
94@deftypefun void scm_gc_mark (SCM @var{x})
95Mark the object @var{x}, and recurse on any objects @var{x} refers to.
96If @var{x}'s mark bit is already set, return immediately. This function
97must only be called during the mark-phase of garbage collection,
98typically from a smob @emph{mark} function.
99@end deftypefun
100
101
07d83abe
MV
102@node Memory Blocks
103@subsection Memory Blocks
104
56273dea
LC
105@cindex automatically-managed memory
106@cindex GC-managed memory
107@cindex conservative garbage collection
108
07d83abe
MV
109In C programs, dynamic management of memory blocks is normally done
110with the functions malloc, realloc, and free. Guile has additional
111functions for dynamic memory allocation that are integrated into the
112garbage collector and the error reporting system.
113
114Memory blocks that are associated with Scheme objects (for example a
c5923112 115smob) should be allocated with @code{scm_gc_malloc} or
56273dea
LC
116@code{scm_gc_malloc_pointerless}. These two functions will either
117return a valid pointer or signal an error. Memory blocks allocated this
118way can be freed with @code{scm_gc_free}; however, this is not strictly
119needed: memory allocated with @code{scm_gc_malloc} or
120@code{scm_gc_malloc_pointerless} is automatically reclaimed when the
121garbage collector no longer sees any live reference to it@footnote{In
122Guile up to version 1.8, memory allocated with @code{scm_gc_malloc}
123@emph{had} to be freed with @code{scm_gc_free}.}.
124
125Memory allocated with @code{scm_gc_malloc} is scanned for live pointers.
126This means that if @code{scm_gc_malloc}-allocated memory contains a
127pointer to some other part of the memory, the garbage collector notices
128it and prevents it from being reclaimed@footnote{In Guile up to 1.8,
129memory allocated with @code{scm_gc_malloc} was @emph{not} scanned.
130Consequently, the GC had to be told explicitly about pointers to live
131objects contained in the memory block, e.g., @i{via} SMOB mark functions
132(@pxref{Smobs, @code{scm_set_smob_mark}})}. Conversely, memory
133allocated with @code{scm_gc_malloc_pointerless} is assumed to be
134``pointer-less'' and is not scanned.
07d83abe
MV
135
136For memory that is not associated with a Scheme object, you can use
137@code{scm_malloc} instead of @code{malloc}. Like
138@code{scm_gc_malloc}, it will either return a valid pointer or signal
139an error. However, it will not assume that the new memory block can
56273dea
LC
140be freed by a garbage collection. The memory must be explicitly freed
141with @code{free}.
07d83abe
MV
142
143There is also @code{scm_gc_realloc} and @code{scm_realloc}, to be used
ad0c2091
MV
144in place of @code{realloc} when appropriate, and @code{scm_gc_calloc}
145and @code{scm_calloc}, to be used in place of @code{calloc} when
07d83abe
MV
146appropriate.
147
56273dea
LC
148The function @code{scm_dynwind_free} can be useful when memory should be
149freed with libc's @code{free} when leaving a dynwind context,
150@xref{Dynamic Wind}.
07d83abe
MV
151
152@deftypefn {C Function} {void *} scm_malloc (size_t @var{size})
153@deftypefnx {C Function} {void *} scm_calloc (size_t @var{size})
154Allocate @var{size} bytes of memory and return a pointer to it. When
155@var{size} is 0, return @code{NULL}. When not enough memory is
156available, signal an error. This function runs the GC to free up some
157memory when it deems it appropriate.
158
159The memory is allocated by the libc @code{malloc} function and can be
160freed with @code{free}. There is no @code{scm_free} function to go
161with @code{scm_malloc} to make it easier to pass memory back and forth
a90968fa 162between different modules.
07d83abe
MV
163
164The function @code{scm_calloc} is similar to @code{scm_malloc}, but
165initializes the block of memory to zero as well.
166@end deftypefn
167
168@deftypefn {C Function} {void *} scm_realloc (void *@var{mem}, size_t @var{new_size})
169Change the size of the memory block at @var{mem} to @var{new_size} and
170return its new location. When @var{new_size} is 0, this is the same
171as calling @code{free} on @var{mem} and @code{NULL} is returned. When
172@var{mem} is @code{NULL}, this function behaves like @code{scm_malloc}
173and allocates a new block of size @var{new_size}.
174
175When not enough memory is available, signal an error. This function
176runs the GC to free up some memory when it deems it appropriate.
177@end deftypefn
178
179
180
181
56273dea
LC
182@deftypefn {C Function} {void *} scm_gc_malloc (size_t @var{size}, const char *@var{what})
183@deftypefnx {C Function} {void *} scm_gc_malloc_pointerless (size_t @var{size}, const char *@var{what})
184@deftypefnx {C Function} {void *} scm_gc_realloc (void *@var{mem}, size_t @var{old_size}, size_t @var{new_size}, const char *@var{what});
185@deftypefnx {C Function} {void *} scm_gc_calloc (size_t @var{size}, const char *@var{what})
186Allocate @var{size} bytes of automatically-managed memory. The memory
187is automatically freed when no longer referenced from any live memory
188block.
189
190Memory allocated with @code{scm_gc_malloc} or @code{scm_gc_calloc} is
191scanned for pointers. Memory allocated by
192@code{scm_gc_malloc_pointerless} is not scanned.
193
194The @code{scm_gc_realloc} call preserves the ``pointerlessness'' of the
195memory area pointed to by @var{mem}. Note that you need to pass the old
196size of a reallocated memory block as well. See below for a motivation.
197@end deftypefn
198
199
200@deftypefn {C Function} void scm_gc_free (void *@var{mem}, size_t @var{size}, const char *@var{what})
201Explicitly free the memory block pointed to by @var{mem}, which was
202previously allocated by one of the above @code{scm_gc} functions.
203
204Note that you need to explicitly pass the @var{size} parameter. This
205is done since it should normally be easy to provide this parameter
206(for memory that is associated with GC controlled objects) and help keep
207the memory management overhead very low. However, in Guile 2.x,
208@var{size} is always ignored.
209@end deftypefn
210
211
07d83abe
MV
212@deftypefn {C Function} void scm_gc_register_collectable_memory (void *@var{mem}, size_t @var{size}, const char *@var{what})
213Informs the GC that the memory at @var{mem} of size @var{size} can
214potentially be freed during a GC. That is, announce that @var{mem} is
215part of a GC controlled object and when the GC happens to free that
216object, @var{size} bytes will be freed along with it. The GC will
217@strong{not} free the memory itself, it will just know that so-and-so
218much bytes of memory are associated with GC controlled objects and the
219memory system figures this into its decisions when to run a GC.
220
07d83abe
MV
221The @var{what} argument is used for statistical purposes. It should
222describe the type of object that the memory will be used for so that
223users can identify just what strange objects are eating up their
224memory.
56273dea
LC
225
226In Guile 2.x, this function has no effect.
07d83abe
MV
227@end deftypefn
228
229@deftypefn {C Function} void scm_gc_unregister_collectable_memory (void *@var{mem}, size_t @var{size})
230Informs the GC that the memory at @var{mem} of size @var{size} is no
231longer associated with a GC controlled object. You must take care to
232match up every call to @code{scm_gc_register_collectable_memory} with
233a call to @code{scm_gc_unregister_collectable_memory}. If you don't do
234this, the GC might have a wrong impression of what is going on and run
235much less efficiently than it could.
07d83abe 236
56273dea 237In Guile 2.x, this function has no effect.
07d83abe
MV
238@end deftypefn
239
240
a90968fa
MV
241@deftypefn {C Function} void scm_frame_free (void *mem)
242Equivalent to @code{scm_frame_unwind_handler (free, @var{mem},
243SCM_F_WIND_EXPLICITLY)}. That is, the memory block at @var{mem} will
244be freed when the current frame is left.
245@end deftypefn
246
07d83abe
MV
247@deffn {Scheme Procedure} malloc-stats
248Return an alist ((@var{what} . @var{n}) ...) describing number
249of malloced objects.
250@var{what} is the second argument to @code{scm_gc_malloc},
251@var{n} is the number of objects of that type currently
252allocated.
56273dea
LC
253
254This function is only available if the @code{GUILE_DEBUG_MALLOC}
255preprocessor macro was defined when Guile was compiled.
07d83abe
MV
256@end deffn
257
258
259@subsubsection Upgrading from scm_must_malloc et al.
260
261Version 1.6 of Guile and earlier did not have the functions from the
262previous section. In their place, it had the functions
263@code{scm_must_malloc}, @code{scm_must_realloc} and
264@code{scm_must_free}. This section explains why we want you to stop
265using them, and how to do this.
266
267@findex scm_must_malloc
268@findex scm_must_realloc
269@findex scm_must_calloc
270@findex scm_must_free
271The functions @code{scm_must_malloc} and @code{scm_must_realloc}
272behaved like @code{scm_gc_malloc} and @code{scm_gc_realloc} do now,
273respectively. They would inform the GC about the newly allocated
274memory via the internal equivalent of
275@code{scm_gc_register_collectable_memory}. However,
276@code{scm_must_free} did not unregister the memory it was about to
277free. The usual way to unregister memory was to return its size from
278a smob free function.
279
280This disconnectedness of the actual freeing of memory and reporting
281this to the GC proved to be bad in practice. It was easy to make
282mistakes and report the wrong size because allocating and freeing was
283not done with symmetric code, and because it is cumbersome to compute
284the total size of nested data structures that were freed with multiple
285calls to @code{scm_must_free}. Additionally, there was no equivalent
286to @code{scm_malloc}, and it was tempting to just use
287@code{scm_must_malloc} and never to tell the GC that the memory has
288been freed.
289
290The effect was that the internal statistics kept by the GC drifted out
291of sync with reality and could even overflow in long running programs.
292When this happened, the result was a dramatic increase in (senseless)
293GC activity which would effectively stop the program dead.
294
295@findex scm_done_malloc
296@findex scm_done_free
297The functions @code{scm_done_malloc} and @code{scm_done_free} were
298introduced to help restore balance to the force, but existing bugs did
299not magically disappear, of course.
300
301Therefore we decided to force everybody to review their code by
302deprecating the existing functions and introducing new ones in their
303place that are hopefully easier to use correctly.
304
305For every use of @code{scm_must_malloc} you need to decide whether to
306use @code{scm_malloc} or @code{scm_gc_malloc} in its place. When the
307memory block is not part of a smob or some other Scheme object whose
308lifetime is ultimately managed by the garbage collector, use
309@code{scm_malloc} and @code{free}. When it is part of a smob, use
310@code{scm_gc_malloc} and change the smob free function to use
311@code{scm_gc_free} instead of @code{scm_must_free} or @code{free} and
312make it return zero.
313
314The important thing is to always pair @code{scm_malloc} with
315@code{free}; and to always pair @code{scm_gc_malloc} with
316@code{scm_gc_free}.
317
318The same reasoning applies to @code{scm_must_realloc} and
319@code{scm_realloc} versus @code{scm_gc_realloc}.
320
321
322@node Weak References
323@subsection Weak References
324
325[FIXME: This chapter is based on Mikael Djurfeldt's answer to a
326question by Michael Livshin. Any mistakes are not theirs, of course. ]
327
328Weak references let you attach bookkeeping information to data so that
329the additional information automatically disappears when the original
330data is no longer in use and gets garbage collected. In a weak key hash,
331the hash entry for that key disappears as soon as the key is no longer
332referenced from anywhere else. For weak value hashes, the same happens
333as soon as the value is no longer in use. Entries in a doubly weak hash
334disappear when either the key or the value are not used anywhere else
335anymore.
336
337Object properties offer the same kind of functionality as weak key
338hashes in many situations. (@pxref{Object Properties})
339
340Here's an example (a little bit strained perhaps, but one of the
341examples is actually used in Guile):
342
343Assume that you're implementing a debugging system where you want to
344associate information about filename and position of source code
345expressions with the expressions themselves.
346
347Hashtables can be used for that, but if you use ordinary hash tables
348it will be impossible for the scheme interpreter to "forget" old
349source when, for example, a file is reloaded.
350
351To implement the mapping from source code expressions to positional
352information it is necessary to use weak-key tables since we don't want
353the expressions to be remembered just because they are in our table.
354
355To implement a mapping from source file line numbers to source code
356expressions you would use a weak-value table.
357
358To implement a mapping from source code expressions to the procedures
359they constitute a doubly-weak table has to be used.
360
361@menu
cdf1ad3b 362* Weak hash tables::
07d83abe
MV
363* Weak vectors::
364@end menu
365
366
cdf1ad3b
MV
367@node Weak hash tables
368@subsubsection Weak hash tables
07d83abe
MV
369
370@deffn {Scheme Procedure} make-weak-key-hash-table size
371@deffnx {Scheme Procedure} make-weak-value-hash-table size
372@deffnx {Scheme Procedure} make-doubly-weak-hash-table size
373@deffnx {C Function} scm_make_weak_key_hash_table (size)
374@deffnx {C Function} scm_make_weak_value_hash_table (size)
375@deffnx {C Function} scm_make_doubly_weak_hash_table (size)
376Return a weak hash table with @var{size} buckets. As with any
377hash table, choosing a good size for the table requires some
378caution.
379
380You can modify weak hash tables in exactly the same way you
381would modify regular hash tables. (@pxref{Hash Tables})
382@end deffn
383
384@deffn {Scheme Procedure} weak-key-hash-table? obj
385@deffnx {Scheme Procedure} weak-value-hash-table? obj
386@deffnx {Scheme Procedure} doubly-weak-hash-table? obj
387@deffnx {C Function} scm_weak_key_hash_table_p (obj)
388@deffnx {C Function} scm_weak_value_hash_table_p (obj)
389@deffnx {C Function} scm_doubly_weak_hash_table_p (obj)
390Return @code{#t} if @var{obj} is the specified weak hash
391table. Note that a doubly weak hash table is neither a weak key
392nor a weak value hash table.
393@end deffn
394
07d83abe
MV
395@node Weak vectors
396@subsubsection Weak vectors
397
398Weak vectors are mainly useful in Guile's implementation of weak hash
399tables.
400
401@deffn {Scheme Procedure} make-weak-vector size [fill]
402@deffnx {C Function} scm_make_weak_vector (size, fill)
403Return a weak vector with @var{size} elements. If the optional
404argument @var{fill} is given, all entries in the vector will be
405set to @var{fill}. The default value for @var{fill} is the
406empty list.
407@end deffn
408
409@deffn {Scheme Procedure} weak-vector . l
410@deffnx {Scheme Procedure} list->weak-vector l
411@deffnx {C Function} scm_weak_vector (l)
412Construct a weak vector from a list: @code{weak-vector} uses
413the list of its arguments while @code{list->weak-vector} uses
414its only argument @var{l} (a list) to construct a weak vector
415the same way @code{list->vector} would.
416@end deffn
417
418@deffn {Scheme Procedure} weak-vector? obj
419@deffnx {C Function} scm_weak_vector_p (obj)
420Return @code{#t} if @var{obj} is a weak vector. Note that all
421weak hashes are also weak vectors.
422@end deffn
423
424
425@node Guardians
426@subsection Guardians
427
930888e8
MV
428Guardians provide a way to be notified about objects that would
429otherwise be collected as garbage. Guarding them prevents the objects
430from being collected and cleanup actions can be performed on them, for
431example.
07d83abe 432
930888e8
MV
433See R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) "Guardians in
434a Generation-Based Garbage Collector". ACM SIGPLAN Conference on
435Programming Language Design and Implementation, June 1993.
07d83abe 436
930888e8
MV
437@deffn {Scheme Procedure} make-guardian
438@deffnx {C Function} scm_make_guardian ()
439Create a new guardian. A guardian protects a set of objects from
440garbage collection, allowing a program to apply cleanup or other
441actions.
07d83abe 442
930888e8
MV
443@code{make-guardian} returns a procedure representing the guardian.
444Calling the guardian procedure with an argument adds the argument to
445the guardian's set of protected objects. Calling the guardian
446procedure without an argument returns one of the protected objects
447which are ready for garbage collection, or @code{#f} if no such object
448is available. Objects which are returned in this way are removed from
449the guardian.
07d83abe 450
930888e8
MV
451You can put a single object into a guardian more than once and you can
452put a single object into more than one guardian. The object will then
453be returned multiple times by the guardian procedures.
454
455An object is eligible to be returned from a guardian when it is no
456longer referenced from outside any guardian.
457
458There is no guarantee about the order in which objects are returned
459from a guardian. If you want to impose an order on finalization
460actions, for example, you can do that by keeping objects alive in some
461global data structure until they are no longer needed for finalizing
462other objects.
463
464Being an element in a weak vector, a key in a hash table with weak
e5547d5f 465keys, or a value in a hash table with weak values does not prevent an
930888e8
MV
466object from being returned by a guardian. But as long as an object
467can be returned from a guardian it will not be removed from such a
468weak vector or hash table. In other words, a weak link does not
469prevent an object from being considered collectable, but being inside
470a guardian prevents a weak link from being broken.
471
e5547d5f 472A key in a weak key hash table can be thought of as having a strong
930888e8 473reference to its associated value as long as the key is accessible.
e5547d5f
MV
474Consequently, when the key is only accessible from within a guardian,
475the reference from the key to the value is also considered to be
476coming from within a guardian. Thus, if there is no other reference
477to the value, it is eligible to be returned from a guardian.
07d83abe
MV
478@end deffn
479
480
07d83abe
MV
481@c Local Variables:
482@c TeX-master: "guile.texi"
483@c End: