implemented Setup.hs to build boehm cpp libs and install them;
[hs-boehmgc.git] / gc-7.2 / doc / gc.man
1 .TH GC_MALLOC 1L "2 October 2003"
2 .SH NAME
3 GC_malloc, GC_malloc_atomic, GC_free, GC_realloc, GC_enable_incremental, GC_register_finalizer, GC_malloc_ignore_off_page, GC_malloc_atomic_ignore_off_page, GC_set_warn_proc \- Garbage collecting malloc replacement
4 .SH SYNOPSIS
5 #include "gc.h"
6 .br
7 void * GC_malloc(size_t size);
8 .br
9 void GC_free(void *ptr);
10 .br
11 void * GC_realloc(void *ptr, size_t size);
12 .br
13 .sp
14 cc ... gc.a
15 .LP
16 .SH DESCRIPTION
17 .I GC_malloc
18 and
19 .I GC_free
20 are plug-in replacements for standard malloc and free.  However,
21 .I
22 GC_malloc
23 will attempt to reclaim inaccessible space automatically by invoking a conservative garbage collector at appropriate points.  The collector traverses all data structures accessible by following pointers from the machines registers, stack(s), data, and bss segments.  Inaccessible structures will be reclaimed.  A machine word is considered to be a valid pointer if it is an address inside an object allocated by
24 .I
25 GC_malloc
26 or friends.
27 .LP
28 In most cases it is preferable to call the macros GC_MALLOC, GC_FREE, etc.
29 instead of calling GC_malloc and friends directly.  This allows debugging
30 versions of the routines to be substituted by defining GC_DEBUG before
31 including gc.h.
32 .LP
33 See the documentation in the include files gc_cpp.h and gc_allocator.h,
34 as well as the gcinterface.html file in the distribution,
35 for an alternate, C++ specific interface to the garbage collector.
36 Note that C++ programs generally
37 need to be careful to ensure that all allocated memory (whether via new,
38 malloc, or STL allocators) that may point to garbage collected memory
39 is either itself garbage collected, or at least traced by the collector.
40 .LP
41 Unlike the standard implementations of malloc,
42 .I
43 GC_malloc
44 clears the newly allocated storage.
45 .I
46 GC_malloc_atomic
47 does not.  Furthermore, it informs the collector that the resulting object will never contain any pointers, and should therefore not be scanned by the collector.
48 .LP
49 .I
50 GC_free
51 can be used to deallocate objects, but its use is optional, and generally discouraged.
52 .I
53 GC_realloc
54 has the standard realloc semantics.  It preserves pointer-free-ness.
55 .I
56 GC_register_finalizer
57 allows for registration of functions that are invoked when an object becomes inaccessible.
58 .LP
59 The garbage collector tries to avoid allocating memory at locations that already appear to be referenced before allocation.  (Such apparent ``pointers'' are usually large integers and the like that just happen to look like an address.)  This may make it hard to allocate very large objects.  An attempt to do so may generate a warning.
60 .LP
61 .I
62 GC_malloc_ignore_off_page
63 and
64 .I
65 GC_malloc_atomic_ignore_off_page
66 inform the collector that the client code will always maintain a pointer to near the beginning of the object (within the first 512 bytes), and that pointers beyond that can be ignored by the collector.  This makes it much easier for the collector to place large objects.  These are recommended for large object allocation.  (Objects expected to be larger than about 100KBytes should be allocated this way.)
67 .LP
68 It is also possible to use the collector to find storage leaks in programs destined to be run with standard malloc/free.  The collector can be compiled for thread-safe operation.  Unlike standard malloc, it is safe to call malloc after a previous malloc call was interrupted by a signal, provided the original malloc call is not resumed.
69 .LP
70 The collector may, on rare occasion produce warning messages.  On UNIX machines these appear on stderr.  Warning messages can be filtered, redirected, or ignored with
71 .I
72 GC_set_warn_proc
73 This is recommended for production code.  See gc.h for details.
74 .LP
75 Fully portable code should call
76 .I
77 GC_INIT
78 from the main program before making any other GC calls.
79 On most platforms this does nothing and the collector is initialized on first use.
80 On a few platforms explicit initialization is necessary.  And it can never hurt. 
81 .LP
82 Debugging versions of many of the above routines are provided as macros.  Their names are identical to the above, but consist of all capital letters.  If GC_DEBUG is defined before gc.h is included, these routines do additional checking, and allow the leak detecting version of the collector to produce slightly more useful output.  Without GC_DEBUG defined, they behave exactly like the lower-case versions.
83 .LP
84 On some machines, collection will be performed incrementally after a call to
85 .I
86 GC_enable_incremental.
87 This may temporarily write protect pages in the heap.  See the README file for more information on how this interacts with system calls that write to the heap.
88 .LP
89 Other facilities not discussed here include limited facilities to support incremental collection on machines without appropriate VM support, provisions for providing more explicit object layout information to the garbage collector, more direct support for ``weak'' pointers, support for ``abortable'' garbage collections during idle time, etc.
90 .LP
91 .SH "SEE ALSO"
92 The README and gc.h files in the distribution.  More detailed definitions of the functions exported by the collector are given there.  (The above list is not complete.)
93 .LP
94 The web site at http://www.hpl.hp.com/personal/Hans_Boehm/gc .
95 .LP
96 Boehm, H., and M. Weiser, "Garbage Collection in an Uncooperative Environment",
97 \fISoftware Practice & Experience\fP, September 1988, pp. 807-820.
98 .LP
99 The malloc(3) man page.
100 .LP
101 .SH AUTHOR
102 Hans-J. Boehm (Hans.Boehm@hp.com).
103 Some of the code was written by others, most notably Alan Demers.