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