api-memory.texi 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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, 2013
  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. @node Weak References
  198. @subsection Weak References
  199. [FIXME: This chapter is based on Mikael Djurfeldt's answer to a
  200. question by Michael Livshin. Any mistakes are not theirs, of course. ]
  201. Weak references let you attach bookkeeping information to data so that
  202. the additional information automatically disappears when the original
  203. data is no longer in use and gets garbage collected. In a weak key hash,
  204. the hash entry for that key disappears as soon as the key is no longer
  205. referenced from anywhere else. For weak value hashes, the same happens
  206. as soon as the value is no longer in use. Entries in a doubly weak hash
  207. disappear when either the key or the value are not used anywhere else
  208. anymore.
  209. Object properties offer the same kind of functionality as weak key
  210. hashes in many situations. (@pxref{Object Properties})
  211. Here's an example (a little bit strained perhaps, but one of the
  212. examples is actually used in Guile):
  213. Assume that you're implementing a debugging system where you want to
  214. associate information about filename and position of source code
  215. expressions with the expressions themselves.
  216. Hashtables can be used for that, but if you use ordinary hash tables
  217. it will be impossible for the scheme interpreter to "forget" old
  218. source when, for example, a file is reloaded.
  219. To implement the mapping from source code expressions to positional
  220. information it is necessary to use weak-key tables since we don't want
  221. the expressions to be remembered just because they are in our table.
  222. To implement a mapping from source file line numbers to source code
  223. expressions you would use a weak-value table.
  224. To implement a mapping from source code expressions to the procedures
  225. they constitute a doubly-weak table has to be used.
  226. @menu
  227. * Weak hash tables::
  228. * Weak vectors::
  229. @end menu
  230. @node Weak hash tables
  231. @subsubsection Weak hash tables
  232. @deffn {Scheme Procedure} make-weak-key-hash-table size
  233. @deffnx {Scheme Procedure} make-weak-value-hash-table size
  234. @deffnx {Scheme Procedure} make-doubly-weak-hash-table size
  235. @deffnx {C Function} scm_make_weak_key_hash_table (size)
  236. @deffnx {C Function} scm_make_weak_value_hash_table (size)
  237. @deffnx {C Function} scm_make_doubly_weak_hash_table (size)
  238. Return a weak hash table with @var{size} buckets. As with any
  239. hash table, choosing a good size for the table requires some
  240. caution.
  241. You can modify weak hash tables in exactly the same way you
  242. would modify regular hash tables. (@pxref{Hash Tables})
  243. @end deffn
  244. @deffn {Scheme Procedure} weak-key-hash-table? obj
  245. @deffnx {Scheme Procedure} weak-value-hash-table? obj
  246. @deffnx {Scheme Procedure} doubly-weak-hash-table? obj
  247. @deffnx {C Function} scm_weak_key_hash_table_p (obj)
  248. @deffnx {C Function} scm_weak_value_hash_table_p (obj)
  249. @deffnx {C Function} scm_doubly_weak_hash_table_p (obj)
  250. Return @code{#t} if @var{obj} is the specified weak hash
  251. table. Note that a doubly weak hash table is neither a weak key
  252. nor a weak value hash table.
  253. @end deffn
  254. @node Weak vectors
  255. @subsubsection Weak vectors
  256. Weak vectors are mainly useful in Guile's implementation of weak hash
  257. tables.
  258. @deffn {Scheme Procedure} make-weak-vector size [fill]
  259. @deffnx {C Function} scm_make_weak_vector (size, fill)
  260. Return a weak vector with @var{size} elements. If the optional
  261. argument @var{fill} is given, all entries in the vector will be
  262. set to @var{fill}. The default value for @var{fill} is the
  263. empty list.
  264. @end deffn
  265. @deffn {Scheme Procedure} weak-vector elem @dots{}
  266. @deffnx {Scheme Procedure} list->weak-vector l
  267. @deffnx {C Function} scm_weak_vector (l)
  268. Construct a weak vector from a list: @code{weak-vector} uses
  269. the list of its arguments while @code{list->weak-vector} uses
  270. its only argument @var{l} (a list) to construct a weak vector
  271. the same way @code{list->vector} would.
  272. @end deffn
  273. @deffn {Scheme Procedure} weak-vector? obj
  274. @deffnx {C Function} scm_weak_vector_p (obj)
  275. Return @code{#t} if @var{obj} is a weak vector. Note that all
  276. weak hashes are also weak vectors.
  277. @end deffn
  278. @node Guardians
  279. @subsection Guardians
  280. Guardians provide a way to be notified about objects that would
  281. otherwise be collected as garbage. Guarding them prevents the objects
  282. from being collected and cleanup actions can be performed on them, for
  283. example.
  284. See R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) "Guardians in
  285. a Generation-Based Garbage Collector". ACM SIGPLAN Conference on
  286. Programming Language Design and Implementation, June 1993.
  287. @deffn {Scheme Procedure} make-guardian
  288. @deffnx {C Function} scm_make_guardian ()
  289. Create a new guardian. A guardian protects a set of objects from
  290. garbage collection, allowing a program to apply cleanup or other
  291. actions.
  292. @code{make-guardian} returns a procedure representing the guardian.
  293. Calling the guardian procedure with an argument adds the argument to
  294. the guardian's set of protected objects. Calling the guardian
  295. procedure without an argument returns one of the protected objects
  296. which are ready for garbage collection, or @code{#f} if no such object
  297. is available. Objects which are returned in this way are removed from
  298. the guardian.
  299. You can put a single object into a guardian more than once and you can
  300. put a single object into more than one guardian. The object will then
  301. be returned multiple times by the guardian procedures.
  302. An object is eligible to be returned from a guardian when it is no
  303. longer referenced from outside any guardian.
  304. There is no guarantee about the order in which objects are returned
  305. from a guardian. If you want to impose an order on finalization
  306. actions, for example, you can do that by keeping objects alive in some
  307. global data structure until they are no longer needed for finalizing
  308. other objects.
  309. Being an element in a weak vector, a key in a hash table with weak
  310. keys, or a value in a hash table with weak values does not prevent an
  311. object from being returned by a guardian. But as long as an object
  312. can be returned from a guardian it will not be removed from such a
  313. weak vector or hash table. In other words, a weak link does not
  314. prevent an object from being considered collectable, but being inside
  315. a guardian prevents a weak link from being broken.
  316. A key in a weak key hash table can be thought of as having a strong
  317. reference to its associated value as long as the key is accessible.
  318. Consequently, when the key is only accessible from within a guardian,
  319. the reference from the key to the value is also considered to be
  320. coming from within a guardian. Thus, if there is no other reference
  321. to the value, it is eligible to be returned from a guardian.
  322. @end deffn
  323. @c Local Variables:
  324. @c TeX-master: "guile.texi"
  325. @c End: