Commit | Line | Data |
---|---|---|
07d83abe MV |
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 | |
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 explicitely. | |
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 | ||
84 | @deftypefun void scm_gc_mark (SCM @var{x}) | |
85 | Mark the object @var{x}, and recurse on any objects @var{x} refers to. | |
86 | If @var{x}'s mark bit is already set, return immediately. This function | |
87 | must only be called during the mark-phase of garbage collection, | |
88 | typically from a smob @emph{mark} function. | |
89 | @end deftypefun | |
90 | ||
91 | ||
92 | @end deffn | |
93 | ||
94 | ||
95 | @node Memory Blocks | |
96 | @subsection Memory Blocks | |
97 | ||
98 | In C programs, dynamic management of memory blocks is normally done | |
99 | with the functions malloc, realloc, and free. Guile has additional | |
100 | functions for dynamic memory allocation that are integrated into the | |
101 | garbage collector and the error reporting system. | |
102 | ||
103 | Memory blocks that are associated with Scheme objects (for example a | |
104 | smob) should be allocated and freed with @code{scm_gc_malloc} and | |
105 | @code{scm_gc_free}. The function @code{scm_gc_malloc} will either | |
106 | return a valid pointer or signal an error. It will also assume that | |
107 | the new memory can be freed by a garbage collection. The garbage | |
108 | collector uses this information to decide when to try to actually | |
109 | collect some garbage. Memory blocks allocated with | |
110 | @code{scm_gc_malloc} must be freed with @code{scm_gc_free}. | |
111 | ||
112 | For memory that is not associated with a Scheme object, you can use | |
113 | @code{scm_malloc} instead of @code{malloc}. Like | |
114 | @code{scm_gc_malloc}, it will either return a valid pointer or signal | |
115 | an error. However, it will not assume that the new memory block can | |
116 | be freed by a garbage collection. The memory can be freed with | |
117 | @code{free}. | |
118 | ||
119 | There is also @code{scm_gc_realloc} and @code{scm_realloc}, to be used | |
120 | in place of @code{realloc} when appropriate, @code{scm_gc_calloc} and | |
121 | @code{scm_calloc}, to be used in place of @code{calloc} when | |
122 | appropriate. | |
123 | ||
124 | For really specialized needs, take at look at | |
125 | @code{scm_gc_register_collectable_memory} and | |
126 | @code{scm_gc_unregister_collectable_memory}. | |
127 | ||
128 | @deftypefn {C Function} {void *} scm_malloc (size_t @var{size}) | |
129 | @deftypefnx {C Function} {void *} scm_calloc (size_t @var{size}) | |
130 | Allocate @var{size} bytes of memory and return a pointer to it. When | |
131 | @var{size} is 0, return @code{NULL}. When not enough memory is | |
132 | available, signal an error. This function runs the GC to free up some | |
133 | memory when it deems it appropriate. | |
134 | ||
135 | The memory is allocated by the libc @code{malloc} function and can be | |
136 | freed with @code{free}. There is no @code{scm_free} function to go | |
137 | with @code{scm_malloc} to make it easier to pass memory back and forth | |
138 | between different modules. | |
139 | ||
140 | The function @code{scm_calloc} is similar to @code{scm_malloc}, but | |
141 | initializes the block of memory to zero as well. | |
142 | @end deftypefn | |
143 | ||
144 | @deftypefn {C Function} {void *} scm_realloc (void *@var{mem}, size_t @var{new_size}) | |
145 | Change the size of the memory block at @var{mem} to @var{new_size} and | |
146 | return its new location. When @var{new_size} is 0, this is the same | |
147 | as calling @code{free} on @var{mem} and @code{NULL} is returned. When | |
148 | @var{mem} is @code{NULL}, this function behaves like @code{scm_malloc} | |
149 | and allocates a new block of size @var{new_size}. | |
150 | ||
151 | When not enough memory is available, signal an error. This function | |
152 | runs the GC to free up some memory when it deems it appropriate. | |
153 | @end deftypefn | |
154 | ||
155 | ||
156 | ||
157 | ||
158 | @deftypefn {C Function} void scm_gc_register_collectable_memory (void *@var{mem}, size_t @var{size}, const char *@var{what}) | |
159 | Informs the GC that the memory at @var{mem} of size @var{size} can | |
160 | potentially be freed during a GC. That is, announce that @var{mem} is | |
161 | part of a GC controlled object and when the GC happens to free that | |
162 | object, @var{size} bytes will be freed along with it. The GC will | |
163 | @strong{not} free the memory itself, it will just know that so-and-so | |
164 | much bytes of memory are associated with GC controlled objects and the | |
165 | memory system figures this into its decisions when to run a GC. | |
166 | ||
167 | @var{mem} does not need to come from @code{scm_malloc}. You can only | |
168 | call this function once for every memory block. | |
169 | ||
170 | The @var{what} argument is used for statistical purposes. It should | |
171 | describe the type of object that the memory will be used for so that | |
172 | users can identify just what strange objects are eating up their | |
173 | memory. | |
174 | @end deftypefn | |
175 | ||
176 | @deftypefn {C Function} void scm_gc_unregister_collectable_memory (void *@var{mem}, size_t @var{size}) | |
177 | Informs the GC that the memory at @var{mem} of size @var{size} is no | |
178 | longer associated with a GC controlled object. You must take care to | |
179 | match up every call to @code{scm_gc_register_collectable_memory} with | |
180 | a call to @code{scm_gc_unregister_collectable_memory}. If you don't do | |
181 | this, the GC might have a wrong impression of what is going on and run | |
182 | much less efficiently than it could. | |
183 | @end deftypefn | |
184 | ||
185 | @deftypefn {C Function} {void *} scm_gc_malloc (size_t @var{size}, const char *@var{what}) | |
186 | @deftypefnx {C Function} {void *} scm_gc_realloc (void *@var{mem}, size_t @var{old_size}, size_t @var{new_size}, const char *@var{what}); | |
187 | @deftypefnx {C Function} {void *} scm_gc_calloc (size_t @var{size}, const char *@var{what}) | |
188 | Like @code{scm_malloc}, @code{scm_realloc} or @code{scm_calloc}, but | |
189 | also call @code{scm_gc_register_collectable_memory}. Note that you | |
190 | need to pass the old size of a reallocated memory block as well. See | |
191 | below for a motivation. | |
192 | @end deftypefn | |
193 | ||
194 | ||
195 | @deftypefn {C Function} void scm_gc_free (void *@var{mem}, size_t @var{size}, const char *@var{what}) | |
196 | Like @code{free}, but also call @code{scm_gc_unregister_collectable_memory}. | |
197 | ||
198 | Note that you need to explicitely pass the @var{size} parameter. This | |
199 | is done since it should normally be easy to provide this parameter | |
200 | (for memory that is associated with GC controlled objects) and this | |
201 | frees us from tracking this value in the GC itself, which will keep | |
202 | the memory management overhead very low. | |
203 | @end deftypefn | |
204 | ||
205 | @deffn {Scheme Procedure} malloc-stats | |
206 | Return an alist ((@var{what} . @var{n}) ...) describing number | |
207 | of malloced objects. | |
208 | @var{what} is the second argument to @code{scm_gc_malloc}, | |
209 | @var{n} is the number of objects of that type currently | |
210 | allocated. | |
211 | @end deffn | |
212 | ||
213 | ||
214 | @subsubsection Upgrading from scm_must_malloc et al. | |
215 | ||
216 | Version 1.6 of Guile and earlier did not have the functions from the | |
217 | previous section. In their place, it had the functions | |
218 | @code{scm_must_malloc}, @code{scm_must_realloc} and | |
219 | @code{scm_must_free}. This section explains why we want you to stop | |
220 | using them, and how to do this. | |
221 | ||
222 | @findex scm_must_malloc | |
223 | @findex scm_must_realloc | |
224 | @findex scm_must_calloc | |
225 | @findex scm_must_free | |
226 | The functions @code{scm_must_malloc} and @code{scm_must_realloc} | |
227 | behaved like @code{scm_gc_malloc} and @code{scm_gc_realloc} do now, | |
228 | respectively. They would inform the GC about the newly allocated | |
229 | memory via the internal equivalent of | |
230 | @code{scm_gc_register_collectable_memory}. However, | |
231 | @code{scm_must_free} did not unregister the memory it was about to | |
232 | free. The usual way to unregister memory was to return its size from | |
233 | a smob free function. | |
234 | ||
235 | This disconnectedness of the actual freeing of memory and reporting | |
236 | this to the GC proved to be bad in practice. It was easy to make | |
237 | mistakes and report the wrong size because allocating and freeing was | |
238 | not done with symmetric code, and because it is cumbersome to compute | |
239 | the total size of nested data structures that were freed with multiple | |
240 | calls to @code{scm_must_free}. Additionally, there was no equivalent | |
241 | to @code{scm_malloc}, and it was tempting to just use | |
242 | @code{scm_must_malloc} and never to tell the GC that the memory has | |
243 | been freed. | |
244 | ||
245 | The effect was that the internal statistics kept by the GC drifted out | |
246 | of sync with reality and could even overflow in long running programs. | |
247 | When this happened, the result was a dramatic increase in (senseless) | |
248 | GC activity which would effectively stop the program dead. | |
249 | ||
250 | @findex scm_done_malloc | |
251 | @findex scm_done_free | |
252 | The functions @code{scm_done_malloc} and @code{scm_done_free} were | |
253 | introduced to help restore balance to the force, but existing bugs did | |
254 | not magically disappear, of course. | |
255 | ||
256 | Therefore we decided to force everybody to review their code by | |
257 | deprecating the existing functions and introducing new ones in their | |
258 | place that are hopefully easier to use correctly. | |
259 | ||
260 | For every use of @code{scm_must_malloc} you need to decide whether to | |
261 | use @code{scm_malloc} or @code{scm_gc_malloc} in its place. When the | |
262 | memory block is not part of a smob or some other Scheme object whose | |
263 | lifetime is ultimately managed by the garbage collector, use | |
264 | @code{scm_malloc} and @code{free}. When it is part of a smob, use | |
265 | @code{scm_gc_malloc} and change the smob free function to use | |
266 | @code{scm_gc_free} instead of @code{scm_must_free} or @code{free} and | |
267 | make it return zero. | |
268 | ||
269 | The important thing is to always pair @code{scm_malloc} with | |
270 | @code{free}; and to always pair @code{scm_gc_malloc} with | |
271 | @code{scm_gc_free}. | |
272 | ||
273 | The same reasoning applies to @code{scm_must_realloc} and | |
274 | @code{scm_realloc} versus @code{scm_gc_realloc}. | |
275 | ||
276 | ||
277 | @node Weak References | |
278 | @subsection Weak References | |
279 | ||
280 | [FIXME: This chapter is based on Mikael Djurfeldt's answer to a | |
281 | question by Michael Livshin. Any mistakes are not theirs, of course. ] | |
282 | ||
283 | Weak references let you attach bookkeeping information to data so that | |
284 | the additional information automatically disappears when the original | |
285 | data is no longer in use and gets garbage collected. In a weak key hash, | |
286 | the hash entry for that key disappears as soon as the key is no longer | |
287 | referenced from anywhere else. For weak value hashes, the same happens | |
288 | as soon as the value is no longer in use. Entries in a doubly weak hash | |
289 | disappear when either the key or the value are not used anywhere else | |
290 | anymore. | |
291 | ||
292 | Object properties offer the same kind of functionality as weak key | |
293 | hashes in many situations. (@pxref{Object Properties}) | |
294 | ||
295 | Here's an example (a little bit strained perhaps, but one of the | |
296 | examples is actually used in Guile): | |
297 | ||
298 | Assume that you're implementing a debugging system where you want to | |
299 | associate information about filename and position of source code | |
300 | expressions with the expressions themselves. | |
301 | ||
302 | Hashtables can be used for that, but if you use ordinary hash tables | |
303 | it will be impossible for the scheme interpreter to "forget" old | |
304 | source when, for example, a file is reloaded. | |
305 | ||
306 | To implement the mapping from source code expressions to positional | |
307 | information it is necessary to use weak-key tables since we don't want | |
308 | the expressions to be remembered just because they are in our table. | |
309 | ||
310 | To implement a mapping from source file line numbers to source code | |
311 | expressions you would use a weak-value table. | |
312 | ||
313 | To implement a mapping from source code expressions to the procedures | |
314 | they constitute a doubly-weak table has to be used. | |
315 | ||
316 | @menu | |
317 | * Weak key hashes:: | |
318 | * Weak vectors:: | |
319 | @end menu | |
320 | ||
321 | ||
322 | @node Weak key hashes | |
323 | @subsubsection Weak key hashes | |
324 | ||
325 | @deffn {Scheme Procedure} make-weak-key-hash-table size | |
326 | @deffnx {Scheme Procedure} make-weak-value-hash-table size | |
327 | @deffnx {Scheme Procedure} make-doubly-weak-hash-table size | |
328 | @deffnx {C Function} scm_make_weak_key_hash_table (size) | |
329 | @deffnx {C Function} scm_make_weak_value_hash_table (size) | |
330 | @deffnx {C Function} scm_make_doubly_weak_hash_table (size) | |
331 | Return a weak hash table with @var{size} buckets. As with any | |
332 | hash table, choosing a good size for the table requires some | |
333 | caution. | |
334 | ||
335 | You can modify weak hash tables in exactly the same way you | |
336 | would modify regular hash tables. (@pxref{Hash Tables}) | |
337 | @end deffn | |
338 | ||
339 | @deffn {Scheme Procedure} weak-key-hash-table? obj | |
340 | @deffnx {Scheme Procedure} weak-value-hash-table? obj | |
341 | @deffnx {Scheme Procedure} doubly-weak-hash-table? obj | |
342 | @deffnx {C Function} scm_weak_key_hash_table_p (obj) | |
343 | @deffnx {C Function} scm_weak_value_hash_table_p (obj) | |
344 | @deffnx {C Function} scm_doubly_weak_hash_table_p (obj) | |
345 | Return @code{#t} if @var{obj} is the specified weak hash | |
346 | table. Note that a doubly weak hash table is neither a weak key | |
347 | nor a weak value hash table. | |
348 | @end deffn | |
349 | ||
350 | @deffn {Scheme Procedure} make-weak-value-hash-table k | |
351 | @end deffn | |
352 | ||
353 | @deffn {Scheme Procedure} weak-value-hash-table? x | |
354 | @end deffn | |
355 | ||
356 | @deffn {Scheme Procedure} make-doubly-weak-hash-table k | |
357 | @end deffn | |
358 | ||
359 | @deffn {Scheme Procedure} doubly-weak-hash-table? x | |
360 | @end deffn | |
361 | ||
362 | ||
363 | @node Weak vectors | |
364 | @subsubsection Weak vectors | |
365 | ||
366 | Weak vectors are mainly useful in Guile's implementation of weak hash | |
367 | tables. | |
368 | ||
369 | @deffn {Scheme Procedure} make-weak-vector size [fill] | |
370 | @deffnx {C Function} scm_make_weak_vector (size, fill) | |
371 | Return a weak vector with @var{size} elements. If the optional | |
372 | argument @var{fill} is given, all entries in the vector will be | |
373 | set to @var{fill}. The default value for @var{fill} is the | |
374 | empty list. | |
375 | @end deffn | |
376 | ||
377 | @deffn {Scheme Procedure} weak-vector . l | |
378 | @deffnx {Scheme Procedure} list->weak-vector l | |
379 | @deffnx {C Function} scm_weak_vector (l) | |
380 | Construct a weak vector from a list: @code{weak-vector} uses | |
381 | the list of its arguments while @code{list->weak-vector} uses | |
382 | its only argument @var{l} (a list) to construct a weak vector | |
383 | the same way @code{list->vector} would. | |
384 | @end deffn | |
385 | ||
386 | @deffn {Scheme Procedure} weak-vector? obj | |
387 | @deffnx {C Function} scm_weak_vector_p (obj) | |
388 | Return @code{#t} if @var{obj} is a weak vector. Note that all | |
389 | weak hashes are also weak vectors. | |
390 | @end deffn | |
391 | ||
392 | ||
393 | @node Guardians | |
394 | @subsection Guardians | |
395 | ||
396 | @deffn {Scheme Procedure} make-guardian [greedy?] | |
397 | @deffnx {C Function} scm_make_guardian (greedy_p) | |
398 | Create a new guardian. | |
399 | A guardian protects a set of objects from garbage collection, | |
400 | allowing a program to apply cleanup or other actions. | |
401 | ||
402 | @code{make-guardian} returns a procedure representing the guardian. | |
403 | Calling the guardian procedure with an argument adds the | |
404 | argument to the guardian's set of protected objects. | |
405 | Calling the guardian procedure without an argument returns | |
406 | one of the protected objects which are ready for garbage | |
407 | collection, or @code{#f} if no such object is available. | |
408 | Objects which are returned in this way are removed from | |
409 | the guardian. | |
410 | ||
411 | @code{make-guardian} takes one optional argument that says whether the | |
412 | new guardian should be greedy or sharing. If there is any chance | |
413 | that any object protected by the guardian may be resurrected, | |
414 | then you should make the guardian greedy (this is the default). | |
415 | ||
416 | See R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) | |
417 | "Guardians in a Generation-Based Garbage Collector". | |
418 | ACM SIGPLAN Conference on Programming Language Design | |
419 | and Implementation, June 1993. | |
420 | ||
421 | (the semantics are slightly different at this point, but the | |
422 | paper still (mostly) accurately describes the interface). | |
423 | @end deffn | |
424 | ||
425 | @deffn {Scheme Procedure} destroy-guardian! guardian | |
426 | @deffnx {C Function} scm_destroy_guardian_x (guardian) | |
427 | Destroys @var{guardian}, by making it impossible to put any more | |
428 | objects in it or get any objects from it. It also unguards any | |
429 | objects guarded by @var{guardian}. | |
430 | @end deffn | |
431 | ||
432 | @deffn {Scheme Procedure} guardian-greedy? guardian | |
433 | @deffnx {C Function} scm_guardian_greedy_p (guardian) | |
434 | Return @code{#t} if @var{guardian} is a greedy guardian, otherwise @code{#f}. | |
435 | @end deffn | |
436 | ||
437 | @deffn {Scheme Procedure} guardian-destroyed? guardian | |
438 | @deffnx {C Function} scm_guardian_destroyed_p (guardian) | |
439 | Return @code{#t} if @var{guardian} has been destroyed, otherwise @code{#f}. | |
440 | @end deffn | |
441 | ||
442 | ||
443 | @page | |
444 | @node Objects | |
445 | @section Objects | |
446 | ||
447 | @deffn {Scheme Procedure} entity? obj | |
448 | @deffnx {C Function} scm_entity_p (obj) | |
449 | Return @code{#t} if @var{obj} is an entity. | |
450 | @end deffn | |
451 | ||
452 | @deffn {Scheme Procedure} operator? obj | |
453 | @deffnx {C Function} scm_operator_p (obj) | |
454 | Return @code{#t} if @var{obj} is an operator. | |
455 | @end deffn | |
456 | ||
457 | @deffn {Scheme Procedure} set-object-procedure! obj proc | |
458 | @deffnx {C Function} scm_set_object_procedure_x (obj, proc) | |
459 | Set the object procedure of @var{obj} to @var{proc}. | |
460 | @var{obj} must be either an entity or an operator. | |
461 | @end deffn | |
462 | ||
463 | @deffn {Scheme Procedure} make-class-object metaclass layout | |
464 | @deffnx {C Function} scm_make_class_object (metaclass, layout) | |
465 | Create a new class object of class @var{metaclass}, with the | |
466 | slot layout specified by @var{layout}. | |
467 | @end deffn | |
468 | ||
469 | @deffn {Scheme Procedure} make-subclass-object class layout | |
470 | @deffnx {C Function} scm_make_subclass_object (class, layout) | |
471 | Create a subclass object of @var{class}, with the slot layout | |
472 | specified by @var{layout}. | |
473 | @end deffn | |
474 | ||
475 | ||
476 | @c Local Variables: | |
477 | @c TeX-master: "guile.texi" | |
478 | @c End: |