api-memory.texi 20 KB

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