README 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. Copyright (c) 1988, 1989 Hans-J. Boehm, Alan J. Demers
  2. Copyright (c) 1991-1996 by Xerox Corporation. All rights reserved.
  3. Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
  4. Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
  5. The file linux_threads.c is also
  6. Copyright (c) 1998 by Fergus Henderson. All rights reserved.
  7. The files Makefile.am, and configure.in are
  8. Copyright (c) 2001 by Red Hat Inc. All rights reserved.
  9. Several files supporting GNU-style builds are copyrighted by the Free
  10. Software Foundation, and carry a different license from that given
  11. below.
  12. THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  13. OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  14. Permission is hereby granted to use or copy this program
  15. for any purpose, provided the above notices are retained on all copies.
  16. Permission to modify the code and to distribute modified code is granted,
  17. provided the above notices are retained, and a notice that the code was
  18. modified is included with the above copyright notice.
  19. A few of the files needed to use the GNU-style build procedure come with
  20. slightly different licenses, though they are all similar in spirit. A few
  21. are GPL'ed, but with an exception that should cover all uses in the
  22. collector. (If you are concerned about such things, I recommend you look
  23. at the notice in config.guess or ltmain.sh.)
  24. This is version 6.6 of a conservative garbage collector for C and C++.
  25. You might find a more recent version of this at
  26. http://www.hpl.hp.com/personal/Hans_Boehm/gc
  27. OVERVIEW
  28. This is intended to be a general purpose, garbage collecting storage
  29. allocator. The algorithms used are described in:
  30. Boehm, H., and M. Weiser, "Garbage Collection in an Uncooperative Environment",
  31. Software Practice & Experience, September 1988, pp. 807-820.
  32. Boehm, H., A. Demers, and S. Shenker, "Mostly Parallel Garbage Collection",
  33. Proceedings of the ACM SIGPLAN '91 Conference on Programming Language Design
  34. and Implementation, SIGPLAN Notices 26, 6 (June 1991), pp. 157-164.
  35. Boehm, H., "Space Efficient Conservative Garbage Collection", Proceedings
  36. of the ACM SIGPLAN '91 Conference on Programming Language Design and
  37. Implementation, SIGPLAN Notices 28, 6 (June 1993), pp. 197-206.
  38. Boehm H., "Reducing Garbage Collector Cache Misses", Proceedings of the
  39. 2000 International Symposium on Memory Management.
  40. Possible interactions between the collector and optimizing compilers are
  41. discussed in
  42. Boehm, H., and D. Chase, "A Proposal for GC-safe C Compilation",
  43. The Journal of C Language Translation 4, 2 (December 1992).
  44. and
  45. Boehm H., "Simple GC-safe Compilation", Proceedings
  46. of the ACM SIGPLAN '96 Conference on Programming Language Design and
  47. Implementation.
  48. (Some of these are also available from
  49. http://www.hpl.hp.com/personal/Hans_Boehm/papers/, among other places.)
  50. Unlike the collector described in the second reference, this collector
  51. operates either with the mutator stopped during the entire collection
  52. (default) or incrementally during allocations. (The latter is supported
  53. on only a few machines.) On the most common platforms, it can be built
  54. with or without thread support. On a few platforms, it can take advantage
  55. of a multiprocessor to speed up garbage collection.
  56. Many of the ideas underlying the collector have previously been explored
  57. by others. Notably, some of the run-time systems developed at Xerox PARC
  58. in the early 1980s conservatively scanned thread stacks to locate possible
  59. pointers (cf. Paul Rovner, "On Adding Garbage Collection and Runtime Types
  60. to a Strongly-Typed Statically Checked, Concurrent Language" Xerox PARC
  61. CSL 84-7). Doug McIlroy wrote a simpler fully conservative collector that
  62. was part of version 8 UNIX (tm), but appears to not have received
  63. widespread use.
  64. Rudimentary tools for use of the collector as a leak detector are included
  65. (see http://www.hpl.hp.com/personal/Hans_Boehm/gc/leak.html),
  66. as is a fairly sophisticated string package "cord" that makes use of the
  67. collector. (See doc/README.cords and H.-J. Boehm, R. Atkinson, and M. Plass,
  68. "Ropes: An Alternative to Strings", Software Practice and Experience 25, 12
  69. (December 1995), pp. 1315-1330. This is very similar to the "rope" package
  70. in Xerox Cedar, or the "rope" package in the SGI STL or the g++ distribution.)
  71. Further collector documantation can be found at
  72. http://www.hpl.hp.com/personal/Hans_Boehm/gc
  73. GENERAL DESCRIPTION
  74. This is a garbage collecting storage allocator that is intended to be
  75. used as a plug-in replacement for C's malloc.
  76. Since the collector does not require pointers to be tagged, it does not
  77. attempt to ensure that all inaccessible storage is reclaimed. However,
  78. in our experience, it is typically more successful at reclaiming unused
  79. memory than most C programs using explicit deallocation. Unlike manually
  80. introduced leaks, the amount of unreclaimed memory typically stays
  81. bounded.
  82. In the following, an "object" is defined to be a region of memory allocated
  83. by the routines described below.
  84. Any objects not intended to be collected must be pointed to either
  85. from other such accessible objects, or from the registers,
  86. stack, data, or statically allocated bss segments. Pointers from
  87. the stack or registers may point to anywhere inside an object.
  88. The same is true for heap pointers if the collector is compiled with
  89. ALL_INTERIOR_POINTERS defined, as is now the default.
  90. Compiling without ALL_INTERIOR_POINTERS may reduce accidental retention
  91. of garbage objects, by requiring pointers from the heap to to the beginning
  92. of an object. But this no longer appears to be a significant
  93. issue for most programs.
  94. There are a number of routines which modify the pointer recognition
  95. algorithm. GC_register_displacement allows certain interior pointers
  96. to be recognized even if ALL_INTERIOR_POINTERS is nor defined.
  97. GC_malloc_ignore_off_page allows some pointers into the middle of large objects
  98. to be disregarded, greatly reducing the probablility of accidental
  99. retention of large objects. For most purposes it seems best to compile
  100. with ALL_INTERIOR_POINTERS and to use GC_malloc_ignore_off_page if
  101. you get collector warnings from allocations of very large objects.
  102. See README.debugging for details.
  103. WARNING: pointers inside memory allocated by the standard "malloc" are not
  104. seen by the garbage collector. Thus objects pointed to only from such a
  105. region may be prematurely deallocated. It is thus suggested that the
  106. standard "malloc" be used only for memory regions, such as I/O buffers, that
  107. are guaranteed not to contain pointers to garbage collectable memory.
  108. Pointers in C language automatic, static, or register variables,
  109. are correctly recognized. (Note that GC_malloc_uncollectable has semantics
  110. similar to standard malloc, but allocates objects that are traced by the
  111. collector.)
  112. WARNING: the collector does not always know how to find pointers in data
  113. areas that are associated with dynamic libraries. This is easy to
  114. remedy IF you know how to find those data areas on your operating
  115. system (see GC_add_roots). Code for doing this under SunOS, IRIX 5.X and 6.X,
  116. HP/UX, Alpha OSF/1, Linux, and win32 is included and used by default. (See
  117. README.win32 for win32 details.) On other systems pointers from dynamic
  118. library data areas may not be considered by the collector.
  119. If you're writing a program that depends on the collector scanning
  120. dynamic library data areas, it may be a good idea to include at least
  121. one call to GC_is_visible() to ensure that those areas are visible
  122. to the collector.
  123. Note that the garbage collector does not need to be informed of shared
  124. read-only data. However if the shared library mechanism can introduce
  125. discontiguous data areas that may contain pointers, then the collector does
  126. need to be informed.
  127. Signal processing for most signals may be deferred during collection,
  128. and during uninterruptible parts of the allocation process.
  129. Like standard ANSI C mallocs, by default it is unsafe to invoke
  130. malloc (and other GC routines) from a signal handler while another
  131. malloc call may be in progress. Removing -DNO_SIGNALS from Makefile
  132. attempts to remedy that. But that may not be reliable with a compiler that
  133. substantially reorders memory operations inside GC_malloc.
  134. The allocator/collector can also be configured for thread-safe operation.
  135. (Full signal safety can also be achieved, but only at the cost of two system
  136. calls per malloc, which is usually unacceptable.)
  137. WARNING: the collector does not guarantee to scan thread-local storage
  138. (e.g. of the kind accessed with pthread_getspecific()). The collector
  139. does scan thread stacks, though, so generally the best solution is to
  140. ensure that any pointers stored in thread-local storage are also
  141. stored on the thread's stack for the duration of their lifetime.
  142. (This is arguably a longstanding bug, but it hasn't been fixed yet.)
  143. INSTALLATION AND PORTABILITY
  144. As distributed, the macro SILENT is defined in Makefile.
  145. In the event of problems, this can be removed to obtain a moderate
  146. amount of descriptive output for each collection.
  147. (The given statistics exhibit a few peculiarities.
  148. Things don't appear to add up for a variety of reasons, most notably
  149. fragmentation losses. These are probably much more significant for the
  150. contrived program "test.c" than for your application.)
  151. Note that typing "make test" will automatically build the collector
  152. and then run setjmp_test and gctest. Setjmp_test will give you information
  153. about configuring the collector, which is useful primarily if you have
  154. a machine that's not already supported. Gctest is a somewhat superficial
  155. test of collector functionality. Failure is indicated by a core dump or
  156. a message to the effect that the collector is broken. Gctest takes about
  157. 35 seconds to run on a SPARCstation 2. It may use up to 8 MB of memory. (The
  158. multi-threaded version will use more. 64-bit versions may use more.)
  159. "Make test" will also, as its last step, attempt to build and test the
  160. "cord" string library. This will fail without an ANSI C compiler, but
  161. the garbage collector itself should still be usable.
  162. The Makefile will generate a library gc.a which you should link against.
  163. Typing "make cords" will add the cord library to gc.a.
  164. Note that this requires an ANSI C compiler.
  165. It is suggested that if you need to replace a piece of the collector
  166. (e.g. GC_mark_rts.c) you simply list your version ahead of gc.a on the
  167. ld command line, rather than replacing the one in gc.a. (This will
  168. generate numerous warnings under some versions of AIX, but it still
  169. works.)
  170. All include files that need to be used by clients will be put in the
  171. include subdirectory. (Normally this is just gc.h. "Make cords" adds
  172. "cord.h" and "ec.h".)
  173. The collector currently is designed to run essentially unmodified on
  174. machines that use a flat 32-bit or 64-bit address space.
  175. That includes the vast majority of Workstations and X86 (X >= 3) PCs.
  176. (The list here was deleted because it was getting too long and constantly
  177. out of date.)
  178. It does NOT run under plain 16-bit DOS or Windows 3.X. There are however
  179. various packages (e.g. win32s, djgpp) that allow flat 32-bit address
  180. applications to run under those systemsif the have at least an 80386 processor,
  181. and several of those are compatible with the collector.
  182. In a few cases (Amiga, OS/2, Win32, MacOS) a separate makefile
  183. or equivalent is supplied. Many of these have separate README.system
  184. files.
  185. Dynamic libraries are completely supported only under SunOS/Solaris,
  186. (and even that support is not functional on the last Sun 3 release),
  187. Linux, FreeBSD, NetBSD, IRIX 5&6, HP/UX, Win32 (not Win32S) and OSF/1
  188. on DEC AXP machines plus perhaps a few others listed near the top
  189. of dyn_load.c. On other machines we recommend that you do one of
  190. the following:
  191. 1) Add dynamic library support (and send us the code).
  192. 2) Use static versions of the libraries.
  193. 3) Arrange for dynamic libraries to use the standard malloc.
  194. This is still dangerous if the library stores a pointer to a
  195. garbage collected object. But nearly all standard interfaces
  196. prohibit this, because they deal correctly with pointers
  197. to stack allocated objects. (Strtok is an exception. Don't
  198. use it.)
  199. In all cases we assume that pointer alignment is consistent with that
  200. enforced by the standard C compilers. If you use a nonstandard compiler
  201. you may have to adjust the alignment parameters defined in gc_priv.h.
  202. Note that this may also be an issue with packed records/structs, if those
  203. enforce less alignment for pointers.
  204. A port to a machine that is not byte addressed, or does not use 32 bit
  205. or 64 bit addresses will require a major effort. A port to plain MSDOS
  206. or win16 is hard.
  207. For machines not already mentioned, or for nonstandard compilers, the
  208. following are likely to require change:
  209. 1. The parameters in gcconfig.h.
  210. The parameters that will usually require adjustment are
  211. STACKBOTTOM, ALIGNMENT and DATASTART. Setjmp_test
  212. prints its guesses of the first two.
  213. DATASTART should be an expression for computing the
  214. address of the beginning of the data segment. This can often be
  215. &etext. But some memory management units require that there be
  216. some unmapped space between the text and the data segment. Thus
  217. it may be more complicated. On UNIX systems, this is rarely
  218. documented. But the adb "$m" command may be helpful. (Note
  219. that DATASTART will usually be a function of &etext. Thus a
  220. single experiment is usually insufficient.)
  221. STACKBOTTOM is used to initialize GC_stackbottom, which
  222. should be a sufficient approximation to the coldest stack address.
  223. On some machines, it is difficult to obtain such a value that is
  224. valid across a variety of MMUs, OS releases, etc. A number of
  225. alternatives exist for using the collector in spite of this. See the
  226. discussion in gcconfig.h immediately preceding the various
  227. definitions of STACKBOTTOM.
  228. 2. mach_dep.c.
  229. The most important routine here is one to mark from registers.
  230. The distributed file includes a generic hack (based on setjmp) that
  231. happens to work on many machines, and may work on yours. Try
  232. compiling and running setjmp_t.c to see whether it has a chance of
  233. working. (This is not correct C, so don't blame your compiler if it
  234. doesn't work. Based on limited experience, register window machines
  235. are likely to cause trouble. If your version of setjmp claims that
  236. all accessible variables, including registers, have the value they
  237. had at the time of the longjmp, it also will not work. Vanilla 4.2 BSD
  238. on Vaxen makes such a claim. SunOS does not.)
  239. If your compiler does not allow in-line assembly code, or if you prefer
  240. not to use such a facility, mach_dep.c may be replaced by a .s file
  241. (as we did for the MIPS machine and the PC/RT).
  242. At this point enough architectures are supported by mach_dep.c
  243. that you will rarely need to do more than adjust for assembler
  244. syntax.
  245. 3. os_dep.c (and gc_priv.h).
  246. Several kinds of operating system dependent routines reside here.
  247. Many are optional. Several are invoked only through corresponding
  248. macros in gc_priv.h, which may also be redefined as appropriate.
  249. The routine GC_register_data_segments is crucial. It registers static
  250. data areas that must be traversed by the collector. (User calls to
  251. GC_add_roots may sometimes be used for similar effect.)
  252. Routines to obtain memory from the OS also reside here.
  253. Alternatively this can be done entirely by the macro GET_MEM
  254. defined in gc_priv.h. Routines to disable and reenable signals
  255. also reside here if they are need by the macros DISABLE_SIGNALS
  256. and ENABLE_SIGNALS defined in gc_priv.h.
  257. In a multithreaded environment, the macros LOCK and UNLOCK
  258. in gc_priv.h will need to be suitably redefined.
  259. The incremental collector requires page dirty information, which
  260. is acquired through routines defined in os_dep.c. Unless directed
  261. otherwise by gcconfig.h, these are implemented as stubs that simply
  262. treat all pages as dirty. (This of course makes the incremental
  263. collector much less useful.)
  264. 4. dyn_load.c
  265. This provides a routine that allows the collector to scan data
  266. segments associated with dynamic libraries. Often it is not
  267. necessary to provide this routine unless user-written dynamic
  268. libraries are used.
  269. For a different version of UN*X or different machines using the
  270. Motorola 68000, Vax, SPARC, 80386, NS 32000, PC/RT, or MIPS architecture,
  271. it should frequently suffice to change definitions in gcconfig.h.
  272. THE C INTERFACE TO THE ALLOCATOR
  273. The following routines are intended to be directly called by the user.
  274. Note that usually only GC_malloc is necessary. GC_clear_roots and GC_add_roots
  275. calls may be required if the collector has to trace from nonstandard places
  276. (e.g. from dynamic library data areas on a machine on which the
  277. collector doesn't already understand them.) On some machines, it may
  278. be desirable to set GC_stacktop to a good approximation of the stack base.
  279. (This enhances code portability on HP PA machines, since there is no
  280. good way for the collector to compute this value.) Client code may include
  281. "gc.h", which defines all of the following, plus many others.
  282. 1) GC_malloc(nbytes)
  283. - allocate an object of size nbytes. Unlike malloc, the object is
  284. cleared before being returned to the user. Gc_malloc will
  285. invoke the garbage collector when it determines this to be appropriate.
  286. GC_malloc may return 0 if it is unable to acquire sufficient
  287. space from the operating system. This is the most probable
  288. consequence of running out of space. Other possible consequences
  289. are that a function call will fail due to lack of stack space,
  290. or that the collector will fail in other ways because it cannot
  291. maintain its internal data structures, or that a crucial system
  292. process will fail and take down the machine. Most of these
  293. possibilities are independent of the malloc implementation.
  294. 2) GC_malloc_atomic(nbytes)
  295. - allocate an object of size nbytes that is guaranteed not to contain any
  296. pointers. The returned object is not guaranteed to be cleared.
  297. (Can always be replaced by GC_malloc, but results in faster collection
  298. times. The collector will probably run faster if large character
  299. arrays, etc. are allocated with GC_malloc_atomic than if they are
  300. statically allocated.)
  301. 3) GC_realloc(object, new_size)
  302. - change the size of object to be new_size. Returns a pointer to the
  303. new object, which may, or may not, be the same as the pointer to
  304. the old object. The new object is taken to be atomic iff the old one
  305. was. If the new object is composite and larger than the original object,
  306. then the newly added bytes are cleared (we hope). This is very likely
  307. to allocate a new object, unless MERGE_SIZES is defined in gc_priv.h.
  308. Even then, it is likely to recycle the old object only if the object
  309. is grown in small additive increments (which, we claim, is generally bad
  310. coding practice.)
  311. 4) GC_free(object)
  312. - explicitly deallocate an object returned by GC_malloc or
  313. GC_malloc_atomic. Not necessary, but can be used to minimize
  314. collections if performance is critical. Probably a performance
  315. loss for very small objects (<= 8 bytes).
  316. 5) GC_expand_hp(bytes)
  317. - Explicitly increase the heap size. (This is normally done automatically
  318. if a garbage collection failed to GC_reclaim enough memory. Explicit
  319. calls to GC_expand_hp may prevent unnecessarily frequent collections at
  320. program startup.)
  321. 6) GC_malloc_ignore_off_page(bytes)
  322. - identical to GC_malloc, but the client promises to keep a pointer to
  323. the somewhere within the first 256 bytes of the object while it is
  324. live. (This pointer should nortmally be declared volatile to prevent
  325. interference from compiler optimizations.) This is the recommended
  326. way to allocate anything that is likely to be larger than 100Kbytes
  327. or so. (GC_malloc may result in failure to reclaim such objects.)
  328. 7) GC_set_warn_proc(proc)
  329. - Can be used to redirect warnings from the collector. Such warnings
  330. should be rare, and should not be ignored during code development.
  331. 8) GC_enable_incremental()
  332. - Enables generational and incremental collection. Useful for large
  333. heaps on machines that provide access to page dirty information.
  334. Some dirty bit implementations may interfere with debugging
  335. (by catching address faults) and place restrictions on heap arguments
  336. to system calls (since write faults inside a system call may not be
  337. handled well).
  338. 9) Several routines to allow for registration of finalization code.
  339. User supplied finalization code may be invoked when an object becomes
  340. unreachable. To call (*f)(obj, x) when obj becomes inaccessible, use
  341. GC_register_finalizer(obj, f, x, 0, 0);
  342. For more sophisticated uses, and for finalization ordering issues,
  343. see gc.h.
  344. The global variable GC_free_space_divisor may be adjusted up from its
  345. default value of 4 to use less space and more collection time, or down for
  346. the opposite effect. Setting it to 1 or 0 will effectively disable collections
  347. and cause all allocations to simply grow the heap.
  348. The variable GC_non_gc_bytes, which is normally 0, may be changed to reflect
  349. the amount of memory allocated by the above routines that should not be
  350. considered as a candidate for collection. Careless use may, of course, result
  351. in excessive memory consumption.
  352. Some additional tuning is possible through the parameters defined
  353. near the top of gc_priv.h.
  354. If only GC_malloc is intended to be used, it might be appropriate to define:
  355. #define malloc(n) GC_malloc(n)
  356. #define calloc(m,n) GC_malloc((m)*(n))
  357. For small pieces of VERY allocation intensive code, gc_inl.h
  358. includes some allocation macros that may be used in place of GC_malloc
  359. and friends.
  360. All externally visible names in the garbage collector start with "GC_".
  361. To avoid name conflicts, client code should avoid this prefix, except when
  362. accessing garbage collector routines or variables.
  363. There are provisions for allocation with explicit type information.
  364. This is rarely necessary. Details can be found in gc_typed.h.
  365. THE C++ INTERFACE TO THE ALLOCATOR:
  366. The Ellis-Hull C++ interface to the collector is included in
  367. the collector distribution. If you intend to use this, type
  368. "make c++" after the initial build of the collector is complete.
  369. See gc_cpp.h for the definition of the interface. This interface
  370. tries to approximate the Ellis-Detlefs C++ garbage collection
  371. proposal without compiler changes.
  372. Cautions:
  373. 1. Arrays allocated without new placement syntax are
  374. allocated as uncollectable objects. They are traced by the
  375. collector, but will not be reclaimed.
  376. 2. Failure to use "make c++" in combination with (1) will
  377. result in arrays allocated using the default new operator.
  378. This is likely to result in disaster without linker warnings.
  379. 3. If your compiler supports an overloaded new[] operator,
  380. then gc_cpp.cc and gc_cpp.h should be suitably modified.
  381. 4. Many current C++ compilers have deficiencies that
  382. break some of the functionality. See the comments in gc_cpp.h
  383. for suggested workarounds.
  384. USE AS LEAK DETECTOR:
  385. The collector may be used to track down leaks in C programs that are
  386. intended to run with malloc/free (e.g. code with extreme real-time or
  387. portability constraints). To do so define FIND_LEAK in Makefile
  388. This will cause the collector to invoke the report_leak
  389. routine defined near the top of reclaim.c whenever an inaccessible
  390. object is found that has not been explicitly freed. Such objects will
  391. also be automatically reclaimed.
  392. Productive use of this facility normally involves redefining report_leak
  393. to do something more intelligent. This typically requires annotating
  394. objects with additional information (e.g. creation time stack trace) that
  395. identifies their origin. Such code is typically not very portable, and is
  396. not included here, except on SPARC machines.
  397. If all objects are allocated with GC_DEBUG_MALLOC (see next section),
  398. then the default version of report_leak will report the source file
  399. and line number at which the leaked object was allocated. This may
  400. sometimes be sufficient. (On SPARC/SUNOS4 machines, it will also report
  401. a cryptic stack trace. This can often be turned into a sympolic stack
  402. trace by invoking program "foo" with "callprocs foo". Callprocs is
  403. a short shell script that invokes adb to expand program counter values
  404. to symbolic addresses. It was largely supplied by Scott Schwartz.)
  405. Note that the debugging facilities described in the next section can
  406. sometimes be slightly LESS effective in leak finding mode, since in
  407. leak finding mode, GC_debug_free actually results in reuse of the object.
  408. (Otherwise the object is simply marked invalid.) Also note that the test
  409. program is not designed to run meaningfully in FIND_LEAK mode.
  410. Use "make gc.a" to build the collector.
  411. DEBUGGING FACILITIES:
  412. The routines GC_debug_malloc, GC_debug_malloc_atomic, GC_debug_realloc,
  413. and GC_debug_free provide an alternate interface to the collector, which
  414. provides some help with memory overwrite errors, and the like.
  415. Objects allocated in this way are annotated with additional
  416. information. Some of this information is checked during garbage
  417. collections, and detected inconsistencies are reported to stderr.
  418. Simple cases of writing past the end of an allocated object should
  419. be caught if the object is explicitly deallocated, or if the
  420. collector is invoked while the object is live. The first deallocation
  421. of an object will clear the debugging info associated with an
  422. object, so accidentally repeated calls to GC_debug_free will report the
  423. deallocation of an object without debugging information. Out of
  424. memory errors will be reported to stderr, in addition to returning
  425. NIL.
  426. GC_debug_malloc checking during garbage collection is enabled
  427. with the first call to GC_debug_malloc. This will result in some
  428. slowdown during collections. If frequent heap checks are desired,
  429. this can be achieved by explicitly invoking GC_gcollect, e.g. from
  430. the debugger.
  431. GC_debug_malloc allocated objects should not be passed to GC_realloc
  432. or GC_free, and conversely. It is however acceptable to allocate only
  433. some objects with GC_debug_malloc, and to use GC_malloc for other objects,
  434. provided the two pools are kept distinct. In this case, there is a very
  435. low probablility that GC_malloc allocated objects may be misidentified as
  436. having been overwritten. This should happen with probability at most
  437. one in 2**32. This probability is zero if GC_debug_malloc is never called.
  438. GC_debug_malloc, GC_malloc_atomic, and GC_debug_realloc take two
  439. additional trailing arguments, a string and an integer. These are not
  440. interpreted by the allocator. They are stored in the object (the string is
  441. not copied). If an error involving the object is detected, they are printed.
  442. The macros GC_MALLOC, GC_MALLOC_ATOMIC, GC_REALLOC, GC_FREE, and
  443. GC_REGISTER_FINALIZER are also provided. These require the same arguments
  444. as the corresponding (nondebugging) routines. If gc.h is included
  445. with GC_DEBUG defined, they call the debugging versions of these
  446. functions, passing the current file name and line number as the two
  447. extra arguments, where appropriate. If gc.h is included without GC_DEBUG
  448. defined, then all these macros will instead be defined to their nondebugging
  449. equivalents. (GC_REGISTER_FINALIZER is necessary, since pointers to
  450. objects with debugging information are really pointers to a displacement
  451. of 16 bytes form the object beginning, and some translation is necessary
  452. when finalization routines are invoked. For details, about what's stored
  453. in the header, see the definition of the type oh in debug_malloc.c)
  454. INCREMENTAL/GENERATIONAL COLLECTION:
  455. The collector normally interrupts client code for the duration of
  456. a garbage collection mark phase. This may be unacceptable if interactive
  457. response is needed for programs with large heaps. The collector
  458. can also run in a "generational" mode, in which it usually attempts to
  459. collect only objects allocated since the last garbage collection.
  460. Furthermore, in this mode, garbage collections run mostly incrementally,
  461. with a small amount of work performed in response to each of a large number of
  462. GC_malloc requests.
  463. This mode is enabled by a call to GC_enable_incremental().
  464. Incremental and generational collection is effective in reducing
  465. pause times only if the collector has some way to tell which objects
  466. or pages have been recently modified. The collector uses two sources
  467. of information:
  468. 1. Information provided by the VM system. This may be provided in
  469. one of several forms. Under Solaris 2.X (and potentially under other
  470. similar systems) information on dirty pages can be read from the
  471. /proc file system. Under other systems (currently SunOS4.X) it is
  472. possible to write-protect the heap, and catch the resulting faults.
  473. On these systems we require that system calls writing to the heap
  474. (other than read) be handled specially by client code.
  475. See os_dep.c for details.
  476. 2. Information supplied by the programmer. We define "stubborn"
  477. objects to be objects that are rarely changed. Such an object
  478. can be allocated (and enabled for writing) with GC_malloc_stubborn.
  479. Once it has been initialized, the collector should be informed with
  480. a call to GC_end_stubborn_change. Subsequent writes that store
  481. pointers into the object must be preceded by a call to
  482. GC_change_stubborn.
  483. This mechanism performs best for objects that are written only for
  484. initialization, and such that only one stubborn object is writable
  485. at once. It is typically not worth using for short-lived
  486. objects. Stubborn objects are treated less efficiently than pointerfree
  487. (atomic) objects.
  488. A rough rule of thumb is that, in the absence of VM information, garbage
  489. collection pauses are proportional to the amount of pointerful storage
  490. plus the amount of modified "stubborn" storage that is reachable during
  491. the collection.
  492. Initial allocation of stubborn objects takes longer than allocation
  493. of other objects, since other data structures need to be maintained.
  494. We recommend against random use of stubborn objects in client
  495. code, since bugs caused by inappropriate writes to stubborn objects
  496. are likely to be very infrequently observed and hard to trace.
  497. However, their use may be appropriate in a few carefully written
  498. library routines that do not make the objects themselves available
  499. for writing by client code.
  500. BUGS:
  501. Any memory that does not have a recognizable pointer to it will be
  502. reclaimed. Exclusive-or'ing forward and backward links in a list
  503. doesn't cut it.
  504. Some C optimizers may lose the last undisguised pointer to a memory
  505. object as a consequence of clever optimizations. This has almost
  506. never been observed in practice. Send mail to boehm@acm.org
  507. for suggestions on how to fix your compiler.
  508. This is not a real-time collector. In the standard configuration,
  509. percentage of time required for collection should be constant across
  510. heap sizes. But collection pauses will increase for larger heaps.
  511. (On SPARCstation 2s collection times will be on the order of 300 msecs
  512. per MB of accessible memory that needs to be scanned. Your mileage
  513. may vary.) The incremental/generational collection facility helps,
  514. but is portable only if "stubborn" allocation is used.
  515. Please address bug reports to boehm@acm.org. If you are
  516. contemplating a major addition, you might also send mail to ask whether
  517. it's already been done (or whether we tried and discarded it).