api-memory.texi 20 KB

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