| 1 |
# Boehm-Demers-Weiser Garbage Collector |
| 2 |
|
| 3 |
This is version 8.2.8 of a conservative garbage |
| 4 |
collector for C and C++. |
| 5 |
|
| 6 |
|
| 7 |
## Download |
| 8 |
|
| 9 |
You might find a more recent/stable version on the |
| 10 |
[Download](https://github.com/ivmai/bdwgc/wiki/Download) page, or |
| 11 |
[BDWGC site](http://www.hboehm.info/gc/). |
| 12 |
|
| 13 |
Also, the latest bug fixes and new features are available in the |
| 14 |
[development repository](https://github.com/ivmai/bdwgc). |
| 15 |
|
| 16 |
|
| 17 |
## Overview |
| 18 |
|
| 19 |
This is intended to be a general purpose, garbage collecting storage |
| 20 |
allocator. The algorithms used are described in: |
| 21 |
|
| 22 |
* Boehm, H., and M. Weiser, "Garbage Collection in an Uncooperative |
| 23 |
Environment", Software Practice & Experience, September 1988, pp. 807-820. |
| 24 |
|
| 25 |
* Boehm, H., A. Demers, and S. Shenker, "Mostly Parallel Garbage Collection", |
| 26 |
Proceedings of the ACM SIGPLAN '91 Conference on Programming Language Design |
| 27 |
and Implementation, SIGPLAN Notices 26, 6 (June 1991), pp. 157-164. |
| 28 |
|
| 29 |
* Boehm, H., "Space Efficient Conservative Garbage Collection", Proceedings |
| 30 |
of the ACM SIGPLAN '91 Conference on Programming Language Design and |
| 31 |
Implementation, SIGPLAN Notices 28, 6 (June 1993), pp. 197-206. |
| 32 |
|
| 33 |
* Boehm H., "Reducing Garbage Collector Cache Misses", Proceedings of the |
| 34 |
2000 International Symposium on Memory Management. |
| 35 |
|
| 36 |
Possible interactions between the collector and optimizing compilers are |
| 37 |
discussed in |
| 38 |
|
| 39 |
* Boehm, H., and D. Chase, "A Proposal for GC-safe C Compilation", |
| 40 |
The Journal of C Language Translation 4, 2 (December 1992). |
| 41 |
|
| 42 |
and |
| 43 |
|
| 44 |
* Boehm H., "Simple GC-safe Compilation", Proceedings of the ACM SIGPLAN '96 |
| 45 |
Conference on Programming Language Design and Implementation. |
| 46 |
|
| 47 |
Unlike the collector described in the second reference, this collector |
| 48 |
operates either with the mutator stopped during the entire collection |
| 49 |
(default) or incrementally during allocations. (The latter is supported |
| 50 |
on fewer machines.) On the most common platforms, it can be built |
| 51 |
with or without thread support. On a few platforms, it can take advantage |
| 52 |
of a multiprocessor to speed up garbage collection. |
| 53 |
|
| 54 |
Many of the ideas underlying the collector have previously been explored |
| 55 |
by others. Notably, some of the run-time systems developed at Xerox PARC |
| 56 |
in the early 1980s conservatively scanned thread stacks to locate possible |
| 57 |
pointers (cf. Paul Rovner, "On Adding Garbage Collection and Runtime Types |
| 58 |
to a Strongly-Typed Statically Checked, Concurrent Language" Xerox PARC |
| 59 |
CSL 84-7). Doug McIlroy wrote a simpler fully conservative collector that |
| 60 |
was part of version 8 UNIX (tm), but appears to not have received |
| 61 |
widespread use. |
| 62 |
|
| 63 |
Rudimentary tools for use of the collector as a |
| 64 |
[leak detector](doc/leak.md) are included, |
| 65 |
as is a fairly sophisticated string package "cord" that makes use of the |
| 66 |
collector. (See doc/README.cords and H.-J. Boehm, R. Atkinson, and M. Plass, |
| 67 |
"Ropes: An Alternative to Strings", Software Practice and Experience 25, 12 |
| 68 |
(December 1995), pp. 1315-1330. This is very similar to the "rope" package |
| 69 |
in Xerox Cedar, or the "rope" package in the SGI STL or the g++ distribution.) |
| 70 |
|
| 71 |
Further collector documentation can be found in the |
| 72 |
[overview](doc/overview.md). |
| 73 |
|
| 74 |
|
| 75 |
## General Description |
| 76 |
|
| 77 |
This is a garbage collecting storage allocator that is intended to be |
| 78 |
used as a plug-in replacement for C's malloc. |
| 79 |
|
| 80 |
Since the collector does not require pointers to be tagged, it does not |
| 81 |
attempt to ensure that all inaccessible storage is reclaimed. However, |
| 82 |
in our experience, it is typically more successful at reclaiming unused |
| 83 |
memory than most C programs using explicit deallocation. Unlike manually |
| 84 |
introduced leaks, the amount of unreclaimed memory typically stays |
| 85 |
bounded. |
| 86 |
|
| 87 |
In the following, an "object" is defined to be a region of memory allocated |
| 88 |
by the routines described below. |
| 89 |
|
| 90 |
Any objects not intended to be collected must be pointed to either |
| 91 |
from other such accessible objects, or from the registers, |
| 92 |
stack, data, or statically allocated bss segments. Pointers from |
| 93 |
the stack or registers may point to anywhere inside an object. |
| 94 |
The same is true for heap pointers if the collector is compiled with |
| 95 |
`ALL_INTERIOR_POINTERS` defined, or `GC_all_interior_pointers` is otherwise |
| 96 |
set, as is now the default. |
| 97 |
|
| 98 |
Compiling without `ALL_INTERIOR_POINTERS` may reduce accidental retention |
| 99 |
of garbage objects, by requiring pointers from the heap to the beginning |
| 100 |
of an object. But this no longer appears to be a significant |
| 101 |
issue for most programs occupying a small fraction of the possible |
| 102 |
address space. |
| 103 |
|
| 104 |
There are a number of routines which modify the pointer recognition |
| 105 |
algorithm. `GC_register_displacement` allows certain interior pointers |
| 106 |
to be recognized even if `ALL_INTERIOR_POINTERS` is not defined. |
| 107 |
`GC_malloc_ignore_off_page` allows some pointers into the middle of |
| 108 |
large objects to be disregarded, greatly reducing the probability of |
| 109 |
accidental retention of large objects. For most purposes it seems |
| 110 |
best to compile with `ALL_INTERIOR_POINTERS` and to use |
| 111 |
`GC_malloc_ignore_off_page` if you get collector warnings from |
| 112 |
allocations of very large objects. See [here](doc/debugging.md) for details. |
| 113 |
|
| 114 |
_WARNING_: pointers inside memory allocated by the standard `malloc` are not |
| 115 |
seen by the garbage collector. Thus objects pointed to only from such a |
| 116 |
region may be prematurely deallocated. It is thus suggested that the |
| 117 |
standard `malloc` be used only for memory regions, such as I/O buffers, that |
| 118 |
are guaranteed not to contain pointers to garbage collectible memory. |
| 119 |
Pointers in C language automatic, static, or register variables, |
| 120 |
are correctly recognized. (Note that `GC_malloc_uncollectable` has |
| 121 |
semantics similar to standard malloc, but allocates objects that are |
| 122 |
traced by the collector.) |
| 123 |
|
| 124 |
_WARNING_: the collector does not always know how to find pointers in data |
| 125 |
areas that are associated with dynamic libraries. This is easy to |
| 126 |
remedy IF you know how to find those data areas on your operating |
| 127 |
system (see `GC_add_roots`). Code for doing this under SunOS, IRIX |
| 128 |
5.X and 6.X, HP/UX, Alpha OSF/1, Linux, and win32 is included and used |
| 129 |
by default. (See doc/README.win32 for Win32 details.) On other systems |
| 130 |
pointers from dynamic library data areas may not be considered by the |
| 131 |
collector. If you're writing a program that depends on the collector |
| 132 |
scanning dynamic library data areas, it may be a good idea to include |
| 133 |
at least one call to `GC_is_visible` to ensure that those areas are |
| 134 |
visible to the collector. |
| 135 |
|
| 136 |
Note that the garbage collector does not need to be informed of shared |
| 137 |
read-only data. However, if the shared library mechanism can introduce |
| 138 |
discontiguous data areas that may contain pointers then the collector does |
| 139 |
need to be informed. |
| 140 |
|
| 141 |
Signal processing for most signals may be deferred during collection, |
| 142 |
and during uninterruptible parts of the allocation process. |
| 143 |
Like standard ANSI C mallocs, by default it is unsafe to invoke |
| 144 |
malloc (and other GC routines) from a signal handler while another |
| 145 |
malloc call may be in progress. |
| 146 |
|
| 147 |
The allocator/collector can also be configured for thread-safe operation. |
| 148 |
(Full signal safety can also be achieved, but only at the cost of two system |
| 149 |
calls per malloc, which is usually unacceptable.) |
| 150 |
|
| 151 |
_WARNING_: the collector does not guarantee to scan thread-local storage |
| 152 |
(e.g. of the kind accessed with `pthread_getspecific`). The collector |
| 153 |
does scan thread stacks, though, so generally the best solution is to |
| 154 |
ensure that any pointers stored in thread-local storage are also |
| 155 |
stored on the thread's stack for the duration of their lifetime. |
| 156 |
(This is arguably a longstanding bug, but it hasn't been fixed yet.) |
| 157 |
|
| 158 |
|
| 159 |
## Installation and Portability |
| 160 |
|
| 161 |
The collector operates silently in the default configuration. |
| 162 |
In the event of problems, this can usually be changed by defining the |
| 163 |
`GC_PRINT_STATS` or `GC_PRINT_VERBOSE_STATS` environment variables. This |
| 164 |
will result in a few lines of descriptive output for each collection. |
| 165 |
(The given statistics exhibit a few peculiarities. |
| 166 |
Things don't appear to add up for a variety of reasons, most notably |
| 167 |
fragmentation losses. These are probably much more significant for the |
| 168 |
contrived program "test.c" than for your application.) |
| 169 |
|
| 170 |
On most Unix-like platforms, the collector can be built either using a |
| 171 |
GNU autoconf-based build infrastructure (type `./configure; make` in the |
| 172 |
simplest case), or with a classic makefile by itself (type |
| 173 |
`make -f Makefile.direct`). |
| 174 |
|
| 175 |
Please note that the collector source repository does not contain configure |
| 176 |
and similar auto-generated files, thus the full procedure of autoconf-based |
| 177 |
build of `master` branch of the collector could look like: |
| 178 |
|
| 179 |
git clone https://github.com/ivmai/bdwgc |
| 180 |
cd bdwgc |
| 181 |
git clone https://github.com/ivmai/libatomic_ops |
| 182 |
./autogen.sh |
| 183 |
./configure |
| 184 |
make -j |
| 185 |
make check |
| 186 |
|
| 187 |
Cloning of `libatomic_ops` is now optional provided the compiler supports |
| 188 |
atomic intrinsics. |
| 189 |
|
| 190 |
Below we focus on the collector build using classic makefile. |
| 191 |
For the Makefile.direct-based process, typing `make check` instead of `make` |
| 192 |
will automatically build the collector and then run `setjmp_test` and `gctest`. |
| 193 |
`Setjmp_test` will give you information about configuring the collector, which is |
| 194 |
useful primarily if you have a machine that's not already supported. Gctest is |
| 195 |
a somewhat superficial test of collector functionality. Failure is indicated |
| 196 |
by a core dump or a message to the effect that the collector is broken. Gctest |
| 197 |
takes about a second to two to run on reasonable 2007 vintage desktops. It may |
| 198 |
use up to about 30 MB of memory. (The multi-threaded version will use more. |
| 199 |
64-bit versions may use more.) `make check` will also, as its last step, |
| 200 |
attempt to build and test the "cord" string library.) |
| 201 |
|
| 202 |
Makefile.direct will generate a library gc.a which you should link against. |
| 203 |
Typing "make cords" will build the cord library (cord.a). |
| 204 |
|
| 205 |
The GNU style build process understands the usual targets. `make check` |
| 206 |
runs a number of tests. `make install` installs at least libgc, and libcord. |
| 207 |
Try `./configure --help` to see the configuration options. It is currently |
| 208 |
not possible to exercise all combinations of build options this way. |
| 209 |
|
| 210 |
All include files that need to be used by clients will be put in the |
| 211 |
include subdirectory. (Normally this is just gc.h. `make cords` adds |
| 212 |
"cord.h" and "ec.h".) |
| 213 |
|
| 214 |
The collector currently is designed to run essentially unmodified on |
| 215 |
machines that use a flat 32-bit or 64-bit address space. |
| 216 |
That includes the vast majority of Workstations and x86 (i386 or later) PCs. |
| 217 |
|
| 218 |
In a few cases (Amiga, OS/2, Win32, MacOS) a separate makefile |
| 219 |
or equivalent is supplied. Many of these have separate README.system |
| 220 |
files. |
| 221 |
|
| 222 |
Dynamic libraries are completely supported only under SunOS/Solaris, |
| 223 |
(and even that support is not functional on the last Sun 3 release), |
| 224 |
Linux, FreeBSD, NetBSD, IRIX 5&6, HP/UX, Win32 (not win32s) and OSF/1 |
| 225 |
on DEC AXP machines plus perhaps a few others listed near the top |
| 226 |
of dyn_load.c. On other machines we recommend that you do one of |
| 227 |
the following: |
| 228 |
|
| 229 |
1. Add dynamic library support (and send us the code). |
| 230 |
2. Use static versions of the libraries. |
| 231 |
3. Arrange for dynamic libraries to use the standard malloc. This is still |
| 232 |
dangerous if the library stores a pointer to a garbage collected object. |
| 233 |
But nearly all standard interfaces prohibit this, because they deal |
| 234 |
correctly with pointers to stack allocated objects. (`strtok` is an |
| 235 |
exception. Don't use it.) |
| 236 |
|
| 237 |
In all cases we assume that pointer alignment is consistent with that |
| 238 |
enforced by the standard C compilers. If you use a nonstandard compiler |
| 239 |
you may have to adjust the alignment parameters defined in gc_priv.h. |
| 240 |
Note that this may also be an issue with packed records/structs, if those |
| 241 |
enforce less alignment for pointers. |
| 242 |
|
| 243 |
A port to a machine that is not byte addressed, or does not use 32 bit |
| 244 |
or 64 bit addresses will require a major effort. A port to plain MSDOS |
| 245 |
or win16 is hard. |
| 246 |
|
| 247 |
For machines not already mentioned, or for nonstandard compilers, |
| 248 |
some porting suggestions are provided [here](doc/porting.md). |
| 249 |
|
| 250 |
|
| 251 |
## The C Interface to the Allocator |
| 252 |
|
| 253 |
The following routines are intended to be directly called by the user. |
| 254 |
Note that usually only `GC_malloc` is necessary. `GC_clear_roots` and |
| 255 |
`GC_add_roots` calls may be required if the collector has to trace |
| 256 |
from nonstandard places (e.g. from dynamic library data areas on a |
| 257 |
machine on which the collector doesn't already understand them.) On |
| 258 |
some machines, it may be desirable to set `GC_stackbottom` to a good |
| 259 |
approximation of the stack base (bottom). |
| 260 |
|
| 261 |
Client code may include "gc.h", which defines all of the following, plus many |
| 262 |
others. |
| 263 |
|
| 264 |
1. `GC_malloc(bytes)` - Allocate an object of a given size. Unlike malloc, |
| 265 |
the object is cleared before being returned to the user. `GC_malloc` will |
| 266 |
invoke the garbage collector when it determines this to be appropriate. |
| 267 |
GC_malloc may return 0 if it is unable to acquire sufficient space from the |
| 268 |
operating system. This is the most probable consequence of running out |
| 269 |
of space. Other possible consequences are that a function call will fail |
| 270 |
due to lack of stack space, or that the collector will fail in other ways |
| 271 |
because it cannot maintain its internal data structures, or that a crucial |
| 272 |
system process will fail and take down the machine. Most of these |
| 273 |
possibilities are independent of the malloc implementation. |
| 274 |
|
| 275 |
2. `GC_malloc_atomic(bytes)` - Allocate an object of a given size that |
| 276 |
is guaranteed not to contain any pointers. The returned object is not |
| 277 |
guaranteed to be cleared. (Can always be replaced by `GC_malloc`, but |
| 278 |
results in faster collection times. The collector will probably run faster |
| 279 |
if large character arrays, etc. are allocated with `GC_malloc_atomic` than |
| 280 |
if they are statically allocated.) |
| 281 |
|
| 282 |
3. `GC_realloc(object, new_bytes)` - Change the size of object to be of |
| 283 |
a given size. Returns a pointer to the new object, which may, or may not, |
| 284 |
be the same as the pointer to the old object. The new object is taken to |
| 285 |
be atomic if and only if the old one was. If the new object is composite |
| 286 |
and larger than the original object then the newly added bytes are cleared. |
| 287 |
This is very likely to allocate a new object. |
| 288 |
|
| 289 |
4. `GC_free(object)` - Explicitly deallocate an object returned by |
| 290 |
`GC_malloc` or `GC_malloc_atomic`, or friends. Not necessary, but can be |
| 291 |
used to minimize collections if performance is critical. Probably |
| 292 |
a performance loss for very small objects (<= 8 bytes). |
| 293 |
|
| 294 |
5. `GC_expand_hp(bytes)` - Explicitly increase the heap size. (This is |
| 295 |
normally done automatically if a garbage collection failed to reclaim |
| 296 |
enough memory. Explicit calls to `GC_expand_hp` may prevent unnecessarily |
| 297 |
frequent collections at program startup.) |
| 298 |
|
| 299 |
6. `GC_malloc_ignore_off_page(bytes)` - Identical to `GC_malloc`, but the |
| 300 |
client promises to keep a pointer to the somewhere within the first GC |
| 301 |
heap block (512 .. 4096 bytes or even more, depending on the configuration) |
| 302 |
of the object while it is live. (This pointer should normally be |
| 303 |
declared volatile to prevent interference from compiler optimizations.) |
| 304 |
This is the recommended way to allocate anything that is likely to be |
| 305 |
larger than 100 KB or so. (`GC_malloc` may result in a failure to reclaim |
| 306 |
such objects.) |
| 307 |
|
| 308 |
7. `GC_set_warn_proc(proc)` - Can be used to redirect warnings from the |
| 309 |
collector. Such warnings should be rare, and should not be ignored during |
| 310 |
code development. |
| 311 |
|
| 312 |
8. `GC_enable_incremental()` - Enables generational and incremental |
| 313 |
collection. Useful for large heaps on machines that provide access to page |
| 314 |
dirty information. Some dirty bit implementations may interfere with |
| 315 |
debugging (by catching address faults) and place restrictions on heap |
| 316 |
arguments to system calls (since write faults inside a system call may not |
| 317 |
be handled well). |
| 318 |
|
| 319 |
9. `GC_register_finalizer(object, proc, data, 0, 0)` and friends - Allow for |
| 320 |
registration of finalization code. User supplied finalization code |
| 321 |
(`(*proc)(object, data)`) is invoked after object becomes unreachable. |
| 322 |
For more sophisticated uses, and for finalization ordering issues, see gc.h. |
| 323 |
|
| 324 |
The global variable `GC_free_space_divisor` may be adjusted up from it |
| 325 |
default value of 3 to use less space and more collection time, or down for |
| 326 |
the opposite effect. Setting it to 1 will almost disable collections |
| 327 |
and cause all allocations to simply grow the heap. |
| 328 |
|
| 329 |
The variable `GC_non_gc_bytes`, which is normally 0, may be changed to reflect |
| 330 |
the amount of memory allocated by the above routines that should not be |
| 331 |
considered as a candidate for collection. Careless use may, of course, result |
| 332 |
in excessive memory consumption. |
| 333 |
|
| 334 |
Some additional tuning is possible through the parameters defined |
| 335 |
near the top of gc_priv.h. |
| 336 |
|
| 337 |
If only `GC_malloc` is intended to be used, it might be appropriate to define: |
| 338 |
|
| 339 |
#define malloc(n) GC_malloc(n) |
| 340 |
#define calloc(m,n) GC_malloc((m)*(n)) |
| 341 |
|
| 342 |
For small pieces of VERY allocation intensive code, gc_inline.h includes |
| 343 |
some allocation macros that may be used in place of `GC_malloc` and |
| 344 |
friends. |
| 345 |
|
| 346 |
All externally visible names in the garbage collector start with `GC_`. |
| 347 |
To avoid name conflicts, client code should avoid this prefix, except when |
| 348 |
accessing garbage collector routines. |
| 349 |
|
| 350 |
There are provisions for allocation with explicit type information. |
| 351 |
This is rarely necessary. Details can be found in gc_typed.h. |
| 352 |
|
| 353 |
|
| 354 |
## The C++ Interface to the Allocator |
| 355 |
|
| 356 |
The Ellis-Hull C++ interface to the collector is included in the collector |
| 357 |
distribution. If you intend to use this, type |
| 358 |
`./configure --enable-cplusplus; make` (or `make -f Makefile.direct c++`) |
| 359 |
after the initial build of the collector is complete. See gc_cpp.h for the |
| 360 |
definition of the interface. This interface tries to approximate the |
| 361 |
Ellis-Detlefs C++ garbage collection proposal without compiler changes. |
| 362 |
|
| 363 |
Very often it will also be necessary to use gc_allocator.h and the |
| 364 |
allocator declared there to construct STL data structures. Otherwise |
| 365 |
subobjects of STL data structures will be allocated using a system |
| 366 |
allocator, and objects they refer to may be prematurely collected. |
| 367 |
|
| 368 |
|
| 369 |
## Use as Leak Detector |
| 370 |
|
| 371 |
The collector may be used to track down leaks in C programs that are |
| 372 |
intended to run with malloc/free (e.g. code with extreme real-time or |
| 373 |
portability constraints). To do so define `FIND_LEAK` in Makefile. |
| 374 |
This will cause the collector to print a human-readable object description |
| 375 |
whenever an inaccessible object is found that has not been explicitly freed. |
| 376 |
Such objects will also be automatically reclaimed. |
| 377 |
|
| 378 |
If all objects are allocated with `GC_DEBUG_MALLOC` (see the next section) |
| 379 |
then, by default, the human-readable object description will at least contain |
| 380 |
the source file and the line number at which the leaked object was allocated. |
| 381 |
This may sometimes be sufficient. (On a few machines, it will also report |
| 382 |
a cryptic stack trace. If this is not symbolic, it can sometimes be called |
| 383 |
into a symbolic stack trace by invoking program "foo" with |
| 384 |
`tools/callprocs.sh foo`. It is a short shell script that invokes adb to |
| 385 |
expand program counter values to symbolic addresses. It was largely supplied |
| 386 |
by Scott Schwartz.) |
| 387 |
|
| 388 |
Note that the debugging facilities described in the next section can |
| 389 |
sometimes be slightly LESS effective in leak finding mode, since in the latter |
| 390 |
`GC_debug_free` actually results in reuse of the object. (Otherwise the |
| 391 |
object is simply marked invalid.) Also, note that most GC tests are not |
| 392 |
designed to run meaningfully in `FIND_LEAK` mode. |
| 393 |
|
| 394 |
|
| 395 |
## Debugging Facilities |
| 396 |
|
| 397 |
The routines `GC_debug_malloc`, `GC_debug_malloc_atomic`, `GC_debug_realloc`, |
| 398 |
and `GC_debug_free` provide an alternate interface to the collector, which |
| 399 |
provides some help with memory overwrite errors, and the like. |
| 400 |
Objects allocated in this way are annotated with additional |
| 401 |
information. Some of this information is checked during garbage |
| 402 |
collections, and detected inconsistencies are reported to stderr. |
| 403 |
|
| 404 |
Simple cases of writing past the end of an allocated object should |
| 405 |
be caught if the object is explicitly deallocated, or if the |
| 406 |
collector is invoked while the object is live. The first deallocation |
| 407 |
of an object will clear the debugging info associated with an |
| 408 |
object, so accidentally repeated calls to `GC_debug_free` will report the |
| 409 |
deallocation of an object without debugging information. Out of |
| 410 |
memory errors will be reported to stderr, in addition to returning `NULL`. |
| 411 |
|
| 412 |
`GC_debug_malloc` checking during garbage collection is enabled |
| 413 |
with the first call to this function. This will result in some |
| 414 |
slowdown during collections. If frequent heap checks are desired, |
| 415 |
this can be achieved by explicitly invoking `GC_gcollect`, e.g. from |
| 416 |
the debugger. |
| 417 |
|
| 418 |
`GC_debug_malloc` allocated objects should not be passed to `GC_realloc` |
| 419 |
or `GC_free`, and conversely. It is however acceptable to allocate only |
| 420 |
some objects with `GC_debug_malloc`, and to use `GC_malloc` for other objects, |
| 421 |
provided the two pools are kept distinct. In this case, there is a very |
| 422 |
low probability that `GC_malloc` allocated objects may be misidentified as |
| 423 |
having been overwritten. This should happen with probability at most |
| 424 |
one in 2**32. This probability is zero if `GC_debug_malloc` is never called. |
| 425 |
|
| 426 |
`GC_debug_malloc`, `GC_debug_malloc_atomic`, and `GC_debug_realloc` take two |
| 427 |
additional trailing arguments, a string and an integer. These are not |
| 428 |
interpreted by the allocator. They are stored in the object (the string is |
| 429 |
not copied). If an error involving the object is detected, they are printed. |
| 430 |
|
| 431 |
The macros `GC_MALLOC`, `GC_MALLOC_ATOMIC`, `GC_REALLOC`, `GC_FREE`, |
| 432 |
`GC_REGISTER_FINALIZER` and friends are also provided. These require the same |
| 433 |
arguments as the corresponding (nondebugging) routines. If gc.h is included |
| 434 |
with `GC_DEBUG` defined, they call the debugging versions of these |
| 435 |
functions, passing the current file name and line number as the two |
| 436 |
extra arguments, where appropriate. If gc.h is included without `GC_DEBUG` |
| 437 |
defined then all these macros will instead be defined to their nondebugging |
| 438 |
equivalents. (`GC_REGISTER_FINALIZER` is necessary, since pointers to |
| 439 |
objects with debugging information are really pointers to a displacement |
| 440 |
of 16 bytes from the object beginning, and some translation is necessary |
| 441 |
when finalization routines are invoked. For details, about what's stored |
| 442 |
in the header, see the definition of the type oh in dbg_mlc.c file.) |
| 443 |
|
| 444 |
|
| 445 |
## Incremental/Generational Collection |
| 446 |
|
| 447 |
The collector normally interrupts client code for the duration of |
| 448 |
a garbage collection mark phase. This may be unacceptable if interactive |
| 449 |
response is needed for programs with large heaps. The collector |
| 450 |
can also run in a "generational" mode, in which it usually attempts to |
| 451 |
collect only objects allocated since the last garbage collection. |
| 452 |
Furthermore, in this mode, garbage collections run mostly incrementally, |
| 453 |
with a small amount of work performed in response to each of a large number of |
| 454 |
`GC_malloc` requests. |
| 455 |
|
| 456 |
This mode is enabled by a call to `GC_enable_incremental`. |
| 457 |
|
| 458 |
Incremental and generational collection is effective in reducing |
| 459 |
pause times only if the collector has some way to tell which objects |
| 460 |
or pages have been recently modified. The collector uses two sources |
| 461 |
of information: |
| 462 |
|
| 463 |
1. Information provided by the VM system. This may be provided in one of |
| 464 |
several forms. Under Solaris 2.X (and potentially under other similar |
| 465 |
systems) information on dirty pages can be read from the /proc file system. |
| 466 |
Under other systems (e.g. SunOS4.X) it is possible to write-protect |
| 467 |
the heap, and catch the resulting faults. On these systems we require that |
| 468 |
system calls writing to the heap (other than read) be handled specially by |
| 469 |
client code. See `os_dep.c` for details. |
| 470 |
|
| 471 |
2. Information supplied by the programmer. The object is considered dirty |
| 472 |
after a call to `GC_end_stubborn_change` provided the library has been |
| 473 |
compiled suitably. It is typically not worth using for short-lived objects. |
| 474 |
Note that bugs caused by a missing `GC_end_stubborn_change` or |
| 475 |
`GC_reachable_here` call are likely to be observed very infrequently and |
| 476 |
hard to trace. |
| 477 |
|
| 478 |
|
| 479 |
## Bugs |
| 480 |
|
| 481 |
Any memory that does not have a recognizable pointer to it will be |
| 482 |
reclaimed. Exclusive-or'ing forward and backward links in a list |
| 483 |
doesn't cut it. |
| 484 |
|
| 485 |
Some C optimizers may lose the last undisguised pointer to a memory |
| 486 |
object as a consequence of clever optimizations. This has almost |
| 487 |
never been observed in practice. |
| 488 |
|
| 489 |
This is not a real-time collector. In the standard configuration, |
| 490 |
percentage of time required for collection should be constant across |
| 491 |
heap sizes. But collection pauses will increase for larger heaps. |
| 492 |
They will decrease with the number of processors if parallel marking |
| 493 |
is enabled. |
| 494 |
|
| 495 |
(On 2007 vintage machines, GC times may be on the order of 5 ms |
| 496 |
per MB of accessible memory that needs to be scanned and processed. |
| 497 |
Your mileage may vary.) The incremental/generational collection facility |
| 498 |
may help in some cases. |
| 499 |
|
| 500 |
|
| 501 |
## Feedback, Contribution, Questions and Notifications |
| 502 |
|
| 503 |
Please address bug reports and new feature ideas to |
| 504 |
[GitHub issues](https://github.com/ivmai/bdwgc/issues). Before the |
| 505 |
submission please check that it has not been done yet by someone else. |
| 506 |
|
| 507 |
If you want to contribute, submit |
| 508 |
a [pull request](https://github.com/ivmai/bdwgc/pulls) to GitHub. |
| 509 |
|
| 510 |
If you need help, use |
| 511 |
[Stack Overflow](https://stackoverflow.com/questions/tagged/boehm-gc). |
| 512 |
Older technical discussions are available in `bdwgc` mailing list archive - it |
| 513 |
can be downloaded as a |
| 514 |
[compressed file](https://github.com/ivmai/bdwgc/files/1038163/bdwgc-mailing-list-archive-2017_04.tar.gz) |
| 515 |
or browsed at [Narkive](http://bdwgc.opendylan.narkive.com). |
| 516 |
|
| 517 |
To get new release announcements, subscribe to |
| 518 |
[RSS feed](https://github.com/ivmai/bdwgc/releases.atom). |
| 519 |
(To receive the notifications by email, a 3rd-party free service like |
| 520 |
[IFTTT RSS Feed](https://ifttt.com/feed) can be setup.) |
| 521 |
To be notified on all issues, please |
| 522 |
[watch](https://github.com/ivmai/bdwgc/watchers) the project on |
| 523 |
GitHub. |
| 524 |
|
| 525 |
|
| 526 |
## Copyright & Warranty |
| 527 |
|
| 528 |
* Copyright (c) 1988, 1989 Hans-J. Boehm, Alan J. Demers |
| 529 |
* Copyright (c) 1991-1996 by Xerox Corporation. All rights reserved. |
| 530 |
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved. |
| 531 |
* Copyright (c) 1999-2011 by Hewlett-Packard Development Company. |
| 532 |
* Copyright (c) 2008-2021 Ivan Maidanski |
| 533 |
|
| 534 |
The files pthread_stop_world.c, pthread_support.c and some others are also |
| 535 |
|
| 536 |
* Copyright (c) 1998 by Fergus Henderson. All rights reserved. |
| 537 |
|
| 538 |
The file include/gc.h is also |
| 539 |
|
| 540 |
* Copyright (c) 2007 Free Software Foundation, Inc |
| 541 |
|
| 542 |
The files Makefile.am and configure.ac are |
| 543 |
|
| 544 |
* Copyright (c) 2001 by Red Hat Inc. All rights reserved. |
| 545 |
|
| 546 |
The files extra/msvc_dbg.c and include/private/msvc_dbg.h are |
| 547 |
|
| 548 |
* Copyright (c) 2004-2005 Andrei Polushin |
| 549 |
|
| 550 |
The file tests/initsecondarythread.c is |
| 551 |
|
| 552 |
* Copyright (c) 2011 Ludovic Courtes |
| 553 |
|
| 554 |
The file tests/disclaim_weakmap_test.c is |
| 555 |
|
| 556 |
* Copyright (c) 2018 Petter A. Urkedal |
| 557 |
|
| 558 |
Several files supporting GNU-style builds are copyrighted by the Free |
| 559 |
Software Foundation, and carry a different license from that given |
| 560 |
below. |
| 561 |
|
| 562 |
THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 563 |
OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 564 |
|
| 565 |
Permission is hereby granted to use or copy this program |
| 566 |
for any purpose, provided the above notices are retained on all copies. |
| 567 |
Permission to modify the code and to distribute modified code is granted, |
| 568 |
provided the above notices are retained, and a notice that the code was |
| 569 |
modified is included with the above copyright notice. |
| 570 |
|
| 571 |
A few of the files needed to use the GNU-style build procedure come with |
| 572 |
slightly different licenses, though they are all similar in spirit. A few |
| 573 |
are GPL'ed, but with an exception that should cover all uses in the |
| 574 |
collector. (If you are concerned about such things, I recommend you look |
| 575 |
at the notice in config.guess or ltmain.sh.) |