1 /*
2  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3  * Copyright (c) 1991-1995 by Xerox Corporation.  All rights reserved.
4  * Copyright 1996-1999 by Silicon Graphics.  All rights reserved.
5  * Copyright 1999 by Hewlett-Packard Company.  All rights reserved.
6  * Copyright (C) 2007 Free Software Foundation, Inc
7  * Copyright (c) 2000-2011 by Hewlett-Packard Development Company.
8  *
9  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
10  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
11  *
12  * Permission is hereby granted to use or copy this program
13  * for any purpose,  provided the above notices are retained on all copies.
14  * Permission to modify the code and to distribute modified code is granted,
15  * provided the above notices are retained, and a notice that the code was
16  * modified is included with the above copyright notice.
17  */
18 
19 /*
20  * Note that this defines a large number of tuning hooks, which can
21  * safely be ignored in nearly all cases.  For normal use it suffices
22  * to call only GC_MALLOC and perhaps GC_REALLOC.
23  * For better performance, also look at GC_MALLOC_ATOMIC, and
24  * GC_enable_incremental.  If you need an action to be performed
25  * immediately before an object is collected, look at GC_register_finalizer.
26  * If you are using Solaris threads, look at the end of this file.
27  * Everything else is best ignored unless you encounter performance
28  * problems.
29  */
30 
31 #ifndef GC_H
32 #define GC_H
33 
34 #include "gc_version.h"
35         /* Define version numbers here to allow test on build machine   */
36         /* for cross-builds.  Note that this defines the header         */
37         /* version number, which may or may not match that of the       */
38         /* dynamic library.  GC_get_version() can be used to obtain     */
39         /* the latter.                                                  */
40 
41 #include "gc_config_macros.h"
42 
43 #ifdef __cplusplus
44   extern "C" {
45 #endif
46 
47 
48 /* Define word and signed_word to be unsigned and signed types of the   */
49 /* size as char * or void *.  There seems to be no way to do this       */
50 /* even semi-portably.  The following is probably no better/worse       */
51 /* than almost anything else.                                           */
52 /* The ANSI standard suggests that size_t and ptrdiff_t might be        */
53 /* better choices.  But those had incorrect definitions on some older   */
54 /* systems.  Notably "typedef int size_t" is WRONG.                     */
55 #ifdef _WIN64
56 # ifdef __int64
57     typedef unsigned __int64 GC_word;
58     typedef __int64 GC_signed_word;
59 # else
60     typedef unsigned long long GC_word;
61     typedef long long GC_signed_word;
62 # endif
63 #else
64   typedef unsigned long GC_word;
65   typedef long GC_signed_word;
66 #endif
67 
68 /* Get the GC library version. The returned value is in the form:       */
69 /* ((version_major<<16) | (version_minor<<8) | alpha_version).          */
70 GC_API unsigned GC_CALL GC_get_version(void);
71 
72 /* Public read-only variables */
73 /* The supplied getter functions are preferred for new code.            */
74 
75 GC_API GC_word GC_gc_no;/* Counter incremented per collection.          */
76                         /* Includes empty GCs at startup.               */
77 GC_API GC_word GC_CALL GC_get_gc_no(void);
78                         /* GC_get_gc_no() uses no synchronization, so   */
79                         /* it requires GC_call_with_alloc_lock() to     */
80                         /* avoid data races on multiprocessors.         */
81 
82 #ifdef GC_THREADS
83   GC_API int GC_parallel;
84                         /* GC is parallelized for performance on        */
85                         /* multiprocessors.  Currently set only         */
86                         /* implicitly if collector is built with        */
87                         /* PARALLEL_MARK defined and if either:         */
88                         /*  Env variable GC_NPROC is set to > 1, or     */
89                         /*  GC_NPROC is not set and this is an MP.      */
90                         /* If GC_parallel is set, incremental           */
91                         /* collection is only partially functional,     */
92                         /* and may not be desirable.                    */
93   GC_API int GC_CALL GC_get_parallel(void);
94 #endif
95 
96 
97 /* Public R/W variables */
98 /* The supplied setter and getter functions are preferred for new code. */
99 
100 typedef void * (GC_CALLBACK * GC_oom_func)(size_t /* bytes_requested */);
101 GC_API GC_oom_func GC_oom_fn;
102                         /* When there is insufficient memory to satisfy */
103                         /* an allocation request, we return             */
104                         /* (*GC_oom_fn)(size).  By default this just    */
105                         /* returns NULL.                                */
106                         /* If it returns, it must return 0 or a valid   */
107                         /* pointer to a previously allocated heap       */
108                         /* object.  GC_oom_fn must not be 0.            */
109                         /* Both the supplied setter and the getter      */
110                         /* acquire the GC lock (to avoid data races).   */
111 GC_API void GC_CALL GC_set_oom_fn(GC_oom_func);
112 GC_API GC_oom_func GC_CALL GC_get_oom_fn(void);
113 
114 GC_API int GC_find_leak;
115                         /* Do not actually garbage collect, but simply  */
116                         /* report inaccessible memory that was not      */
117                         /* deallocated with GC_free.  Initial value     */
118                         /* is determined by FIND_LEAK macro.            */
119                         /* The value should not typically be modified   */
120                         /* after GC initialization.                     */
121 GC_API void GC_CALL GC_set_find_leak(int);
122 GC_API int GC_CALL GC_get_find_leak(void);
123 
124 GC_API int GC_all_interior_pointers;
125                         /* Arrange for pointers to object interiors to  */
126                         /* be recognized as valid.  May not be changed  */
127                         /* after GC initialization.  The initial value  */
128                         /* depends on whether the GC is built with      */
129                         /* ALL_INTERIOR_POINTERS macro defined or not.  */
130                         /* Unless DONT_ADD_BYTE_AT_END is defined, this */
131                         /* also affects whether sizes are increased by  */
132                         /* at least a byte to allow "off the end"       */
133                         /* pointer recognition.                         */
134                         /* MUST BE 0 or 1.                              */
135 GC_API void GC_CALL GC_set_all_interior_pointers(int);
136 GC_API int GC_CALL GC_get_all_interior_pointers(void);
137 
138 GC_API int GC_finalize_on_demand;
139                         /* If nonzero, finalizers will only be run in   */
140                         /* response to an explicit GC_invoke_finalizers */
141                         /* call.  The default is determined by whether  */
142                         /* the FINALIZE_ON_DEMAND macro is defined      */
143                         /* when the collector is built.                 */
144                         /* The setter and getter are unsynchronized.    */
145 GC_API void GC_CALL GC_set_finalize_on_demand(int);
146 GC_API int GC_CALL GC_get_finalize_on_demand(void);
147 
148 GC_API int GC_java_finalization;
149                         /* Mark objects reachable from finalizable      */
150                         /* objects in a separate post-pass.  This makes */
151                         /* it a bit safer to use non-topologically-     */
152                         /* ordered finalization.  Default value is      */
153                         /* determined by JAVA_FINALIZATION macro.       */
154                         /* Enables register_finalizer_unreachable to    */
155                         /* work correctly.                              */
156                         /* The setter and getter are unsynchronized.    */
157 GC_API void GC_CALL GC_set_java_finalization(int);
158 GC_API int GC_CALL GC_get_java_finalization(void);
159 
160 typedef void (GC_CALLBACK * GC_finalizer_notifier_proc)(void);
161 GC_API GC_finalizer_notifier_proc GC_finalizer_notifier;
162                         /* Invoked by the collector when there are      */
163                         /* objects to be finalized.  Invoked at most    */
164                         /* once per GC cycle.  Never invoked unless     */
165                         /* GC_finalize_on_demand is set.                */
166                         /* Typically this will notify a finalization    */
167                         /* thread, which will call GC_invoke_finalizers */
168                         /* in response.  May be 0 (means no notifier).  */
169                         /* Both the supplied setter and the getter      */
170                         /* acquire the GC lock (to avoid data races).   */
171 GC_API void GC_CALL GC_set_finalizer_notifier(GC_finalizer_notifier_proc);
172 GC_API GC_finalizer_notifier_proc GC_CALL GC_get_finalizer_notifier(void);
173 
174 GC_API int GC_dont_gc;  /* != 0 ==> Don't collect.  In versions 6.2a1+, */
175                         /* this overrides explicit GC_gcollect() calls. */
176                         /* Used as a counter, so that nested enabling   */
177                         /* and disabling work correctly.  Should        */
178                         /* normally be updated with GC_enable() and     */
179                         /* GC_disable() calls.                          */
180                         /* Direct assignment to GC_dont_gc is           */
181                         /* deprecated.                                  */
182 
183 GC_API int GC_dont_expand;
184                         /* Don't expand the heap unless explicitly      */
185                         /* requested or forced to.  The setter and      */
186                         /* getter are unsynchronized.                   */
187 GC_API void GC_CALL GC_set_dont_expand(int);
188 GC_API int GC_CALL GC_get_dont_expand(void);
189 
190 GC_API int GC_use_entire_heap;
191                 /* Causes the non-incremental collector to use the      */
192                 /* entire heap before collecting.  This was the only    */
193                 /* option for GC versions < 5.0.  This sometimes        */
194                 /* results in more large block fragmentation, since     */
195                 /* very large blocks will tend to get broken up         */
196                 /* during each GC cycle.  It is likely to result in a   */
197                 /* larger working set, but lower collection             */
198                 /* frequencies, and hence fewer instructions executed   */
199                 /* in the collector.                                    */
200 
201 GC_API int GC_full_freq;    /* Number of partial collections between    */
202                             /* full collections.  Matters only if       */
203                             /* GC_incremental is set.                   */
204                             /* Full collections are also triggered if   */
205                             /* the collector detects a substantial      */
206                             /* increase in the number of in-use heap    */
207                             /* blocks.  Values in the tens are now      */
208                             /* perfectly reasonable, unlike for         */
209                             /* earlier GC versions.                     */
210                         /* The setter and getter are unsynchronized, so */
211                         /* GC_call_with_alloc_lock() is required to     */
212                         /* avoid data races (if the value is modified   */
213                         /* after the GC is put to multi-threaded mode). */
214 GC_API void GC_CALL GC_set_full_freq(int);
215 GC_API int GC_CALL GC_get_full_freq(void);
216 
217 GC_API GC_word GC_non_gc_bytes;
218                         /* Bytes not considered candidates for          */
219                         /* collection.  Used only to control scheduling */
220                         /* of collections.  Updated by                  */
221                         /* GC_malloc_uncollectable and GC_free.         */
222                         /* Wizards only.                                */
223                         /* The setter and getter are unsynchronized, so */
224                         /* GC_call_with_alloc_lock() is required to     */
225                         /* avoid data races (if the value is modified   */
226                         /* after the GC is put to multi-threaded mode). */
227 GC_API void GC_CALL GC_set_non_gc_bytes(GC_word);
228 GC_API GC_word GC_CALL GC_get_non_gc_bytes(void);
229 
230 GC_API int GC_no_dls;
231                         /* Don't register dynamic library data segments. */
232                         /* Wizards only.  Should be used only if the     */
233                         /* application explicitly registers all roots.   */
234                         /* (In some environments like Microsoft Windows  */
235                         /* and Apple's Darwin, this may also prevent     */
236                         /* registration of the main data segment as part */
237                         /* of the root set.)                             */
238                         /* The setter and getter are unsynchronized.     */
239 GC_API void GC_CALL GC_set_no_dls(int);
240 GC_API int GC_CALL GC_get_no_dls(void);
241 
242 GC_API GC_word GC_free_space_divisor;
243                         /* We try to make sure that we allocate at      */
244                         /* least N/GC_free_space_divisor bytes between  */
245                         /* collections, where N is twice the number     */
246                         /* of traced bytes, plus the number of untraced */
247                         /* bytes (bytes in "atomic" objects), plus      */
248                         /* a rough estimate of the root set size.       */
249                         /* N approximates GC tracing work per GC.       */
250                         /* Initially, GC_free_space_divisor = 3.        */
251                         /* Increasing its value will use less space     */
252                         /* but more collection time.  Decreasing it     */
253                         /* will appreciably decrease collection time    */
254                         /* at the expense of space.                     */
255                         /* The setter and getter are unsynchronized, so */
256                         /* GC_call_with_alloc_lock() is required to     */
257                         /* avoid data races (if the value is modified   */
258                         /* after the GC is put to multi-threaded mode). */
259 GC_API void GC_CALL GC_set_free_space_divisor(GC_word);
260 GC_API GC_word GC_CALL GC_get_free_space_divisor(void);
261 
262 GC_API GC_word GC_max_retries;
263                         /* The maximum number of GCs attempted before   */
264                         /* reporting out of memory after heap           */
265                         /* expansion fails.  Initially 0.               */
266                         /* The setter and getter are unsynchronized, so */
267                         /* GC_call_with_alloc_lock() is required to     */
268                         /* avoid data races (if the value is modified   */
269                         /* after the GC is put to multi-threaded mode). */
270 GC_API void GC_CALL GC_set_max_retries(GC_word);
271 GC_API GC_word GC_CALL GC_get_max_retries(void);
272 
273 
274 GC_API char *GC_stackbottom;    /* Cool end of user stack.              */
275                                 /* May be set in the client prior to    */
276                                 /* calling any GC_ routines.  This      */
277                                 /* avoids some overhead, and            */
278                                 /* potentially some signals that can    */
279                                 /* confuse debuggers.  Otherwise the    */
280                                 /* collector attempts to set it         */
281                                 /* automatically.                       */
282                                 /* For multi-threaded code, this is the */
283                                 /* cold end of the stack for the        */
284                                 /* primordial thread.                   */
285 
286 GC_API int GC_dont_precollect;  /* Don't collect as part of GC          */
287                                 /* initialization.  Should be set only  */
288                                 /* if the client wants a chance to      */
289                                 /* manually initialize the root set     */
290                                 /* before the first collection.         */
291                                 /* Interferes with blacklisting.        */
292                                 /* Wizards only.                        */
293 GC_API void GC_CALL GC_set_dont_precollect(int);
294 GC_API int GC_CALL GC_get_dont_precollect(void);
295 
296 GC_API unsigned long GC_time_limit;
297                                /* If incremental collection is enabled, */
298                                /* We try to terminate collections       */
299                                /* after this many milliseconds.  Not a  */
300                                /* hard time bound.  Setting this to     */
301                                /* GC_TIME_UNLIMITED will essentially    */
302                                /* disable incremental collection while  */
303                                /* leaving generational collection       */
304                                /* enabled.                              */
305 #define GC_TIME_UNLIMITED 999999
306                                /* Setting GC_time_limit to this value   */
307                                /* will disable the "pause time exceeded"*/
308                                /* tests.                                */
309                         /* The setter and getter are unsynchronized, so */
310                         /* GC_call_with_alloc_lock() is required to     */
311                         /* avoid data races (if the value is modified   */
312                         /* after the GC is put to multi-threaded mode). */
313 GC_API void GC_CALL GC_set_time_limit(unsigned long);
314 GC_API unsigned long GC_CALL GC_get_time_limit(void);
315 
316 /* Public procedures */
317 
318 /* Set whether the GC will allocate executable memory pages or not.     */
319 /* A non-zero argument instructs the collector to allocate memory with  */
320 /* the executable flag on.  Must be called before the collector is      */
321 /* initialized.  May have no effect on some platforms.  The default     */
322 /* value is controlled by NO_EXECUTE_PERMISSION macro (if present then  */
323 /* the flag is off).  Portable clients should have                      */
324 /* GC_set_pages_executable(1) call (before GC_INIT) provided they are   */
325 /* going to execute code on any of the GC-allocated memory objects.     */
326 GC_API void GC_CALL GC_set_pages_executable(int);
327 
328 /* Returns non-zero value if the GC is set to the allocate-executable   */
329 /* mode.  The mode could be changed by GC_set_pages_executable() unless */
330 /* the latter has no effect on the platform.                            */
331 GC_API int GC_CALL GC_get_pages_executable(void);
332 
333 /* Initialize the collector.  Portable clients should call GC_INIT()    */
334 /* from the main program instead.                                       */
335 GC_API void GC_CALL GC_init(void);
336 
337 /* General purpose allocation routines, with roughly malloc calling     */
338 /* conv.  The atomic versions promise that no relevant pointers are     */
339 /* contained in the object.  The non-atomic versions guarantee that the */
340 /* new object is cleared.  GC_malloc_stubborn promises that no changes  */
341 /* to the object will occur after GC_end_stubborn_change has been       */
342 /* called on the result of GC_malloc_stubborn.  GC_malloc_uncollectable */
343 /* allocates an object that is scanned for pointers to collectable      */
344 /* objects, but is not itself collectable.  The object is scanned even  */
345 /* if it does not appear to be reachable.  GC_malloc_uncollectable and  */
346 /* GC_free called on the resulting object implicitly update             */
347 /* GC_non_gc_bytes appropriately.                                       */
348 /* Note that the GC_malloc_stubborn support doesn't really exist        */
349 /* anymore.  MANUAL_VDB provides comparable functionality.              */
350 GC_API void * GC_CALL GC_malloc(size_t /* size_in_bytes */)
351                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
352 GC_API void * GC_CALL GC_malloc_atomic(size_t /* size_in_bytes */)
353                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
354 GC_API char * GC_CALL GC_strdup(const char *) GC_ATTR_MALLOC;
355 GC_API char * GC_CALL GC_strndup(const char *, size_t) GC_ATTR_MALLOC;
356 GC_API void * GC_CALL GC_malloc_uncollectable(size_t /* size_in_bytes */)
357                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
358 GC_API void * GC_CALL GC_malloc_stubborn(size_t /* size_in_bytes */)
359                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
360 
361 /* GC_memalign() is not well tested.                                    */
362 GC_API void * GC_CALL GC_memalign(size_t /* align */, size_t /* lb */)
363                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(2);
364 GC_API int GC_CALL GC_posix_memalign(void ** /* memptr */, size_t /* align */,
365                         size_t /* lb */);
366 
367 /* Explicitly deallocate an object.  Dangerous if used incorrectly.     */
368 /* Requires a pointer to the base of an object.                         */
369 /* If the argument is stubborn, it should not be changeable when freed. */
370 /* An object should not be enabled for finalization when it is          */
371 /* explicitly deallocated.                                              */
372 /* GC_free(0) is a no-op, as required by ANSI C for free.               */
373 GC_API void GC_CALL GC_free(void *);
374 
375 /* Stubborn objects may be changed only if the collector is explicitly  */
376 /* informed.  The collector is implicitly informed of coming change     */
377 /* when such an object is first allocated.  The following routines      */
378 /* inform the collector that an object will no longer be changed, or    */
379 /* that it will once again be changed.  Only non-NULL pointer stores    */
380 /* into the object are considered to be changes.  The argument to       */
381 /* GC_end_stubborn_change must be exactly the value returned by         */
382 /* GC_malloc_stubborn or passed to GC_change_stubborn.  (In the second  */
383 /* case, it may be an interior pointer within 512 bytes of the          */
384 /* beginning of the objects.)  There is a performance penalty for       */
385 /* allowing more than one stubborn object to be changed at once, but it */
386 /* is acceptable to do so.  The same applies to dropping stubborn       */
387 /* objects that are still changeable.                                   */
388 GC_API void GC_CALL GC_change_stubborn(void *);
389 GC_API void GC_CALL GC_end_stubborn_change(void *);
390 
391 /* Return a pointer to the base (lowest address) of an object given     */
392 /* a pointer to a location within the object.                           */
393 /* I.e. map an interior pointer to the corresponding bas pointer.       */
394 /* Note that with debugging allocation, this returns a pointer to the   */
395 /* actual base of the object, i.e. the debug information, not to        */
396 /* the base of the user object.                                         */
397 /* Return 0 if displaced_pointer doesn't point to within a valid        */
398 /* object.                                                              */
399 /* Note that a deallocated object in the garbage collected heap         */
400 /* may be considered valid, even if it has been deallocated with        */
401 /* GC_free.                                                             */
402 GC_API void * GC_CALL GC_base(void * /* displaced_pointer */);
403 
404 /* Given a pointer to the base of an object, return its size in bytes.  */
405 /* The returned size may be slightly larger than what was originally    */
406 /* requested.                                                           */
407 GC_API size_t GC_CALL GC_size(const void * /* object_addr */);
408 
409 /* For compatibility with C library.  This is occasionally faster than  */
410 /* a malloc followed by a bcopy.  But if you rely on that, either here  */
411 /* or with the standard C library, your code is broken.  In my          */
412 /* opinion, it shouldn't have been invented, but now we're stuck. -HB   */
413 /* The resulting object has the same kind as the original.              */
414 /* If the argument is stubborn, the result will have changes enabled.   */
415 /* It is an error to have changes enabled for the original object.      */
416 /* Follows ANSI conventions for NULL old_object.                        */
417 GC_API void * GC_CALL GC_realloc(void * /* old_object */,
418                                  size_t /* new_size_in_bytes */)
419                         /* 'realloc' attr */ GC_ATTR_ALLOC_SIZE(2);
420 
421 /* Explicitly increase the heap size.   */
422 /* Returns 0 on failure, 1 on success.  */
423 GC_API int GC_CALL GC_expand_hp(size_t /* number_of_bytes */);
424 
425 /* Limit the heap size to n bytes.  Useful when you're debugging,       */
426 /* especially on systems that don't handle running out of memory well.  */
427 /* n == 0 ==> unbounded.  This is the default.                          */
428 GC_API void GC_CALL GC_set_max_heap_size(GC_word /* n */);
429 
430 /* Inform the collector that a certain section of statically allocated  */
431 /* memory contains no pointers to garbage collected memory.  Thus it    */
432 /* need not be scanned.  This is sometimes important if the application */
433 /* maps large read/write files into the address space, which could be   */
434 /* mistaken for dynamic library data segments on some systems.          */
435 /* The section (referred to by low_address) must be pointer-aligned.    */
436 /* low_address must not be greater than high_address_plus_1.            */
437 GC_API void GC_CALL GC_exclude_static_roots(void * /* low_address */,
438                                         void * /* high_address_plus_1 */);
439 
440 /* Clear the set of root segments.  Wizards only.                       */
441 GC_API void GC_CALL GC_clear_roots(void);
442 
443 /* Add a root segment.  Wizards only.                                   */
444 /* The segment (referred to by low_address) must be pointer-aligned.    */
445 /* low_address must not be greater than high_address_plus_1.            */
446 GC_API void GC_CALL GC_add_roots(void * /* low_address */,
447                                  void * /* high_address_plus_1 */);
448 
449 /* Remove a root segment.  Wizards only.                                */
450 /* May be unimplemented on some platforms.                              */
451 GC_API void GC_CALL GC_remove_roots(void * /* low_address */,
452                                     void * /* high_address_plus_1 */);
453 
454 /* Add a displacement to the set of those considered valid by the       */
455 /* collector.  GC_register_displacement(n) means that if p was returned */
456 /* by GC_malloc, then (char *)p + n will be considered to be a valid    */
457 /* pointer to p.  N must be small and less than the size of p.          */
458 /* (All pointers to the interior of objects from the stack are          */
459 /* considered valid in any case.  This applies to heap objects and      */
460 /* static data.)                                                        */
461 /* Preferably, this should be called before any other GC procedures.    */
462 /* Calling it later adds to the probability of excess memory            */
463 /* retention.                                                           */
464 /* This is a no-op if the collector has recognition of                  */
465 /* arbitrary interior pointers enabled, which is now the default.       */
466 GC_API void GC_CALL GC_register_displacement(size_t /* n */);
467 
468 /* The following version should be used if any debugging allocation is  */
469 /* being done.                                                          */
470 GC_API void GC_CALL GC_debug_register_displacement(size_t /* n */);
471 
472 /* Explicitly trigger a full, world-stop collection.    */
473 GC_API void GC_CALL GC_gcollect(void);
474 
475 /* Same as above but ignores the default stop_func setting and tries to */
476 /* unmap as much memory as possible (regardless of the corresponding    */
477 /* switch setting).  The recommended usage: on receiving a system       */
478 /* low-memory event; before retrying a system call failed because of    */
479 /* the system is running out of resources.                              */
480 GC_API void GC_CALL GC_gcollect_and_unmap(void);
481 
482 /* Trigger a full world-stopped collection.  Abort the collection if    */
483 /* and when stop_func returns a nonzero value.  Stop_func will be       */
484 /* called frequently, and should be reasonably fast.  (stop_func is     */
485 /* called with the allocation lock held and the world might be stopped; */
486 /* it's not allowed for stop_func to manipulate pointers to the garbage */
487 /* collected heap or call most of GC functions.)  This works even       */
488 /* if virtual dirty bits, and hence incremental collection is not       */
489 /* available for this architecture.  Collections can be aborted faster  */
490 /* than normal pause times for incremental collection.  However,        */
491 /* aborted collections do no useful work; the next collection needs     */
492 /* to start from the beginning.  stop_func must not be 0.               */
493 /* GC_try_to_collect() returns 0 if the collection was aborted (or the  */
494 /* collections are disabled), 1 if it succeeded.                        */
495 typedef int (GC_CALLBACK * GC_stop_func)(void);
496 GC_API int GC_CALL GC_try_to_collect(GC_stop_func /* stop_func */);
497 
498 /* Set and get the default stop_func.  The default stop_func is used by */
499 /* GC_gcollect() and by implicitly trigged collections (except for the  */
500 /* case when handling out of memory).  Must not be 0.                   */
501 GC_API void GC_CALL GC_set_stop_func(GC_stop_func /* stop_func */);
502 GC_API GC_stop_func GC_CALL GC_get_stop_func(void);
503 
504 /* Return the number of bytes in the heap.  Excludes collector private  */
505 /* data structures.  Excludes the unmapped memory (returned to the OS). */
506 /* Includes empty blocks and fragmentation loss.  Includes some pages   */
507 /* that were allocated but never written.                               */
508 GC_API size_t GC_CALL GC_get_heap_size(void);
509 
510 /* Return a lower bound on the number of free bytes in the heap         */
511 /* (excluding the unmapped memory space).                               */
512 GC_API size_t GC_CALL GC_get_free_bytes(void);
513 
514 /* Return the size (in bytes) of the unmapped memory (which is returned */
515 /* to the OS but could be remapped back by the collector later unless   */
516 /* the OS runs out of system/virtual memory).                           */
517 GC_API size_t GC_CALL GC_get_unmapped_bytes(void);
518 
519 /* Return the number of bytes allocated since the last collection.      */
520 GC_API size_t GC_CALL GC_get_bytes_since_gc(void);
521 
522 /* Return the total number of bytes allocated in this process.          */
523 /* Never decreases, except due to wrapping.                             */
524 GC_API size_t GC_CALL GC_get_total_bytes(void);
525 
526 /* Disable garbage collection.  Even GC_gcollect calls will be          */
527 /* ineffective.                                                         */
528 GC_API void GC_CALL GC_disable(void);
529 
530 /* Re-enable garbage collection.  GC_disable() and GC_enable() calls    */
531 /* nest.  Garbage collection is enabled if the number of calls to both  */
532 /* both functions is equal.                                             */
533 GC_API void GC_CALL GC_enable(void);
534 
535 /* Enable incremental/generational collection.  Not advisable unless    */
536 /* dirty bits are available or most heap objects are pointer-free       */
537 /* (atomic) or immutable.  Don't use in leak finding mode.  Ignored if  */
538 /* GC_dont_gc is non-zero.  Only the generational piece of this is      */
539 /* functional if GC_parallel is TRUE or if GC_time_limit is             */
540 /* GC_TIME_UNLIMITED.  Causes thread-local variant of GC_gcj_malloc()   */
541 /* to revert to locked allocation.  Must be called before any such      */
542 /* GC_gcj_malloc() calls.  For best performance, should be called as    */
543 /* early as possible.  On some platforms, calling it later may have     */
544 /* adverse effects.                                                     */
545 /* Safe to call before GC_INIT().  Includes a  GC_init() call.          */
546 GC_API void GC_CALL GC_enable_incremental(void);
547 
548 /* Does incremental mode write-protect pages?  Returns zero or  */
549 /* more of the following, or'ed together:                       */
550 #define GC_PROTECTS_POINTER_HEAP  1 /* May protect non-atomic objs.     */
551 #define GC_PROTECTS_PTRFREE_HEAP  2
552 #define GC_PROTECTS_STATIC_DATA   4 /* Currently never.                 */
553 #define GC_PROTECTS_STACK         8 /* Probably impractical.            */
554 
555 #define GC_PROTECTS_NONE 0
556 GC_API int GC_CALL GC_incremental_protection_needs(void);
557 
558 /* Perform some garbage collection work, if appropriate.        */
559 /* Return 0 if there is no more work to be done.                */
560 /* Typically performs an amount of work corresponding roughly   */
561 /* to marking from one page.  May do more work if further       */
562 /* progress requires it, e.g. if incremental collection is      */
563 /* disabled.  It is reasonable to call this in a wait loop      */
564 /* until it returns 0.                                          */
565 GC_API int GC_CALL GC_collect_a_little(void);
566 
567 /* Allocate an object of size lb bytes.  The client guarantees that     */
568 /* as long as the object is live, it will be referenced by a pointer    */
569 /* that points to somewhere within the first 256 bytes of the object.   */
570 /* (This should normally be declared volatile to prevent the compiler   */
571 /* from invalidating this assertion.)  This routine is only useful      */
572 /* if a large array is being allocated.  It reduces the chance of       */
573 /* accidentally retaining such an array as a result of scanning an      */
574 /* integer that happens to be an address inside the array.  (Actually,  */
575 /* it reduces the chance of the allocator not finding space for such    */
576 /* an array, since it will try hard to avoid introducing such a false   */
577 /* reference.)  On a SunOS 4.X or MS Windows system this is recommended */
578 /* for arrays likely to be larger than 100K or so.  For other systems,  */
579 /* or if the collector is not configured to recognize all interior      */
580 /* pointers, the threshold is normally much higher.                     */
581 GC_API void * GC_CALL GC_malloc_ignore_off_page(size_t /* lb */)
582                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
583 GC_API void * GC_CALL GC_malloc_atomic_ignore_off_page(size_t /* lb */)
584                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
585 
586 #ifdef GC_ADD_CALLER
587 # define GC_EXTRAS GC_RETURN_ADDR, __FILE__, __LINE__
588 # define GC_EXTRA_PARAMS GC_word ra, const char * s, int i
589 #else
590 # define GC_EXTRAS __FILE__, __LINE__
591 # define GC_EXTRA_PARAMS const char * s, int i
592 #endif
593 
594 /* The following is only defined if the library has been suitably       */
595 /* compiled:                                                            */
596 GC_API void * GC_CALL GC_malloc_atomic_uncollectable(
597                                                 size_t /* size_in_bytes */)
598                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
599 GC_API void * GC_CALL GC_debug_malloc_atomic_uncollectable(size_t,
600                                                            GC_EXTRA_PARAMS)
601                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
602 
603 /* Debugging (annotated) allocation.  GC_gcollect will check            */
604 /* objects allocated in this way for overwrites, etc.                   */
605 GC_API void * GC_CALL GC_debug_malloc(size_t /* size_in_bytes */,
606                                       GC_EXTRA_PARAMS)
607                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
608 GC_API void * GC_CALL GC_debug_malloc_atomic(size_t /* size_in_bytes */,
609                                              GC_EXTRA_PARAMS)
610                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
611 GC_API char * GC_CALL GC_debug_strdup(const char *,
612                                       GC_EXTRA_PARAMS) GC_ATTR_MALLOC;
613 GC_API char * GC_CALL GC_debug_strndup(const char *, size_t,
614                                        GC_EXTRA_PARAMS) GC_ATTR_MALLOC;
615 GC_API void * GC_CALL GC_debug_malloc_uncollectable(
616                         size_t /* size_in_bytes */, GC_EXTRA_PARAMS)
617                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
618 GC_API void * GC_CALL GC_debug_malloc_stubborn(size_t /* size_in_bytes */,
619                                                GC_EXTRA_PARAMS)
620                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
621 GC_API void * GC_CALL GC_debug_malloc_ignore_off_page(
622                         size_t /* size_in_bytes */, GC_EXTRA_PARAMS)
623                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
624 GC_API void * GC_CALL GC_debug_malloc_atomic_ignore_off_page(
625                         size_t /* size_in_bytes */, GC_EXTRA_PARAMS)
626                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
627 GC_API void GC_CALL GC_debug_free(void *);
628 GC_API void * GC_CALL GC_debug_realloc(void * /* old_object */,
629                         size_t /* new_size_in_bytes */, GC_EXTRA_PARAMS)
630                         /* 'realloc' attr */ GC_ATTR_ALLOC_SIZE(2);
631 GC_API void GC_CALL GC_debug_change_stubborn(void *);
632 GC_API void GC_CALL GC_debug_end_stubborn_change(void *);
633 
634 /* Routines that allocate objects with debug information (like the      */
635 /* above), but just fill in dummy file and line number information.     */
636 /* Thus they can serve as drop-in malloc/realloc replacements.  This    */
637 /* can be useful for two reasons:                                       */
638 /* 1) It allows the collector to be built with DBG_HDRS_ALL defined     */
639 /*    even if some allocation calls come from 3rd party libraries       */
640 /*    that can't be recompiled.                                         */
641 /* 2) On some platforms, the file and line information is redundant,    */
642 /*    since it can be reconstructed from a stack trace.  On such        */
643 /*    platforms it may be more convenient not to recompile, e.g. for    */
644 /*    leak detection.  This can be accomplished by instructing the      */
645 /*    linker to replace malloc/realloc with these.                      */
646 GC_API void * GC_CALL GC_debug_malloc_replacement(size_t /* size_in_bytes */)
647                         GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1);
648 GC_API void * GC_CALL GC_debug_realloc_replacement(void * /* object_addr */,
649                                                    size_t /* size_in_bytes */)
650                         /* 'realloc' attr */ GC_ATTR_ALLOC_SIZE(2);
651 
652 #ifdef GC_DEBUG_REPLACEMENT
653 # define GC_MALLOC(sz) GC_debug_malloc_replacement(sz)
654 # define GC_REALLOC(old, sz) GC_debug_realloc_replacement(old, sz)
655 #elif defined(GC_DEBUG)
656 # define GC_MALLOC(sz) GC_debug_malloc(sz, GC_EXTRAS)
657 # define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, GC_EXTRAS)
658 #else
659 # define GC_MALLOC(sz) GC_malloc(sz)
660 # define GC_REALLOC(old, sz) GC_realloc(old, sz)
661 #endif /* !GC_DEBUG_REPLACEMENT && !GC_DEBUG */
662 
663 #ifdef GC_DEBUG
664 # define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, GC_EXTRAS)
665 # define GC_STRDUP(s) GC_debug_strdup(s, GC_EXTRAS)
666 # define GC_STRNDUP(s, sz) GC_debug_strndup(s, sz, GC_EXTRAS)
667 # define GC_MALLOC_ATOMIC_UNCOLLECTABLE(sz) \
668                         GC_debug_malloc_atomic_uncollectable(sz, GC_EXTRAS)
669 # define GC_MALLOC_UNCOLLECTABLE(sz) \
670                         GC_debug_malloc_uncollectable(sz, GC_EXTRAS)
671 # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
672                         GC_debug_malloc_ignore_off_page(sz, GC_EXTRAS)
673 # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
674                         GC_debug_malloc_atomic_ignore_off_page(sz, GC_EXTRAS)
675 # define GC_FREE(p) GC_debug_free(p)
676 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
677       GC_debug_register_finalizer(p, f, d, of, od)
678 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
679       GC_debug_register_finalizer_ignore_self(p, f, d, of, od)
680 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
681       GC_debug_register_finalizer_no_order(p, f, d, of, od)
682 # define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \
683       GC_debug_register_finalizer_unreachable(p, f, d, of, od)
684 # define GC_MALLOC_STUBBORN(sz) GC_debug_malloc_stubborn(sz, GC_EXTRAS)
685 # define GC_CHANGE_STUBBORN(p) GC_debug_change_stubborn(p)
686 # define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p)
687 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
688       GC_general_register_disappearing_link(link, GC_base(obj))
689 # define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n)
690 #else
691 # define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz)
692 # define GC_STRDUP(s) GC_strdup(s)
693 # define GC_STRNDUP(s, sz) GC_strndup(s, sz)
694 # define GC_MALLOC_ATOMIC_UNCOLLECTABLE(sz) GC_malloc_atomic_uncollectable(sz)
695 # define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz)
696 # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
697                         GC_malloc_ignore_off_page(sz)
698 # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
699                         GC_malloc_atomic_ignore_off_page(sz)
700 # define GC_FREE(p) GC_free(p)
701 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
702       GC_register_finalizer(p, f, d, of, od)
703 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
704       GC_register_finalizer_ignore_self(p, f, d, of, od)
705 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
706       GC_register_finalizer_no_order(p, f, d, of, od)
707 # define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \
708       GC_register_finalizer_unreachable(p, f, d, of, od)
709 # define GC_MALLOC_STUBBORN(sz) GC_malloc_stubborn(sz)
710 # define GC_CHANGE_STUBBORN(p) GC_change_stubborn(p)
711 # define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p)
712 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
713       GC_general_register_disappearing_link(link, obj)
714 # define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n)
715 #endif /* !GC_DEBUG */
716 
717 /* The following are included because they are often convenient, and    */
718 /* reduce the chance for a misspecified size argument.  But calls may   */
719 /* expand to something syntactically incorrect if t is a complicated    */
720 /* type expression.                                                     */
721 #define GC_NEW(t)               ((t*)GC_MALLOC(sizeof(t)))
722 #define GC_NEW_ATOMIC(t)        ((t*)GC_MALLOC_ATOMIC(sizeof(t)))
723 #define GC_NEW_STUBBORN(t)      ((t*)GC_MALLOC_STUBBORN(sizeof(t)))
724 #define GC_NEW_UNCOLLECTABLE(t) ((t*)GC_MALLOC_UNCOLLECTABLE(sizeof(t)))
725 
726 #ifdef GC_REQUIRE_WCSDUP
727   /* This might be unavailable on some targets (or not needed). */
728   /* wchar_t should be defined in stddef.h */
729   GC_API wchar_t * GC_CALL GC_wcsdup(const wchar_t *) GC_ATTR_MALLOC;
730   GC_API wchar_t * GC_CALL GC_debug_wcsdup(const wchar_t *,
731                                            GC_EXTRA_PARAMS) GC_ATTR_MALLOC;
732 # ifdef GC_DEBUG
733 #   define GC_WCSDUP(s) GC_debug_wcsdup(s, GC_EXTRAS)
734 # else
735 #   define GC_WCSDUP(s) GC_wcsdup(s)
736 # endif
737 #endif /* GC_REQUIRE_WCSDUP */
738 
739 /* Finalization.  Some of these primitives are grossly unsafe.          */
740 /* The idea is to make them both cheap, and sufficient to build         */
741 /* a safer layer, closer to Modula-3, Java, or PCedar finalization.     */
742 /* The interface represents my conclusions from a long discussion       */
743 /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes,              */
744 /* Christian Jacobi, and Russ Atkinson.  It's not perfect, and          */
745 /* probably nobody else agrees with it.     Hans-J. Boehm  3/13/92      */
746 typedef void (GC_CALLBACK * GC_finalization_proc)(void * /* obj */,
747                                                   void * /* client_data */);
748 
749 GC_API void GC_CALL GC_register_finalizer(void * /* obj */,
750                         GC_finalization_proc /* fn */, void * /* cd */,
751                         GC_finalization_proc * /* ofn */, void ** /* ocd */);
752 GC_API void GC_CALL GC_debug_register_finalizer(void * /* obj */,
753                         GC_finalization_proc /* fn */, void * /* cd */,
754                         GC_finalization_proc * /* ofn */, void ** /* ocd */);
755         /* When obj is no longer accessible, invoke             */
756         /* (*fn)(obj, cd).  If a and b are inaccessible, and    */
757         /* a points to b (after disappearing links have been    */
758         /* made to disappear), then only a will be              */
759         /* finalized.  (If this does not create any new         */
760         /* pointers to b, then b will be finalized after the    */
761         /* next collection.)  Any finalizable object that       */
762         /* is reachable from itself by following one or more    */
763         /* pointers will not be finalized (or collected).       */
764         /* Thus cycles involving finalizable objects should     */
765         /* be avoided, or broken by disappearing links.         */
766         /* All but the last finalizer registered for an object  */
767         /* is ignored.                                          */
768         /* Finalization may be removed by passing 0 as fn.      */
769         /* Finalizers are implicitly unregistered when they are */
770         /* enqueued for finalization (i.e. become ready to be   */
771         /* finalized).                                          */
772         /* The old finalizer and client data are stored in      */
773         /* *ofn and *ocd.  (ofn and/or ocd may be NULL.         */
774         /* The allocation lock is held while *ofn and *ocd are  */
775         /* updated.  In case of error (no memory to register    */
776         /* new finalizer), *ofn and *ocd remain unchanged.)     */
777         /* Fn is never invoked on an accessible object,         */
778         /* provided hidden pointers are converted to real       */
779         /* pointers only if the allocation lock is held, and    */
780         /* such conversions are not performed by finalization   */
781         /* routines.                                            */
782         /* If GC_register_finalizer is aborted as a result of   */
783         /* a signal, the object may be left with no             */
784         /* finalization, even if neither the old nor new        */
785         /* finalizer were NULL.                                 */
786         /* Obj should be the starting address of an object      */
787         /* allocated by GC_malloc or friends. Obj may also be   */
788         /* NULL or point to something outside GC heap (in this  */
789         /* case, fn is ignored, *ofn and *ocd are set to NULL). */
790         /* Note that any garbage collectable object referenced  */
791         /* by cd will be considered accessible until the        */
792         /* finalizer is invoked.                                */
793 
794 /* Another versions of the above follow.  It ignores            */
795 /* self-cycles, i.e. pointers from a finalizable object to      */
796 /* itself.  There is a stylistic argument that this is wrong,   */
797 /* but it's unavoidable for C++, since the compiler may         */
798 /* silently introduce these.  It's also benign in that specific */
799 /* case.  And it helps if finalizable objects are split to      */
800 /* avoid cycles.                                                */
801 /* Note that cd will still be viewed as accessible, even if it  */
802 /* refers to the object itself.                                 */
803 GC_API void GC_CALL GC_register_finalizer_ignore_self(void * /* obj */,
804                         GC_finalization_proc /* fn */, void * /* cd */,
805                         GC_finalization_proc * /* ofn */, void ** /* ocd */);
806 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self(void * /* obj */,
807                         GC_finalization_proc /* fn */, void * /* cd */,
808                         GC_finalization_proc * /* ofn */, void ** /* ocd */);
809 
810 /* Another version of the above.  It ignores all cycles.        */
811 /* It should probably only be used by Java implementations.     */
812 /* Note that cd will still be viewed as accessible, even if it  */
813 /* refers to the object itself.                                 */
814 GC_API void GC_CALL GC_register_finalizer_no_order(void * /* obj */,
815                         GC_finalization_proc /* fn */, void * /* cd */,
816                         GC_finalization_proc * /* ofn */, void ** /* ocd */);
817 GC_API void GC_CALL GC_debug_register_finalizer_no_order(void * /* obj */,
818                         GC_finalization_proc /* fn */, void * /* cd */,
819                         GC_finalization_proc * /* ofn */, void ** /* ocd */);
820 
821 /* This is a special finalizer that is useful when an object's  */
822 /* finalizer must be run when the object is known to be no      */
823 /* longer reachable, not even from other finalizable objects.   */
824 /* It behaves like "normal" finalization, except that the       */
825 /* finalizer is not run while the object is reachable from      */
826 /* other objects specifying unordered finalization.             */
827 /* Effectively it allows an object referenced, possibly         */
828 /* indirectly, from an unordered finalizable object to override */
829 /* the unordered finalization request.                          */
830 /* This can be used in combination with finalizer_no_order so   */
831 /* as to release resources that must not be released while an   */
832 /* object can still be brought back to life by other            */
833 /* finalizers.                                                  */
834 /* Only works if GC_java_finalization is set.  Probably only    */
835 /* of interest when implementing a language that requires       */
836 /* unordered finalization (e.g. Java, C#).                      */
837 GC_API void GC_CALL GC_register_finalizer_unreachable(void * /* obj */,
838                         GC_finalization_proc /* fn */, void * /* cd */,
839                         GC_finalization_proc * /* ofn */, void ** /* ocd */);
840 GC_API void GC_CALL GC_debug_register_finalizer_unreachable(void * /* obj */,
841                         GC_finalization_proc /* fn */, void * /* cd */,
842                         GC_finalization_proc * /* ofn */, void ** /* ocd */);
843 
844 #define GC_NO_MEMORY 2  /* Failure due to lack of memory.       */
845 
846 /* The following routine may be used to break cycles between    */
847 /* finalizable objects, thus causing cyclic finalizable         */
848 /* objects to be finalized in the correct order.  Standard      */
849 /* use involves calling GC_register_disappearing_link(&p),      */
850 /* where p is a pointer that is not followed by finalization    */
851 /* code, and should not be considered in determining            */
852 /* finalization order.                                          */
853 GC_API int GC_CALL GC_register_disappearing_link(void ** /* link */);
854         /* Link should point to a field of a heap allocated     */
855         /* object obj.  *link will be cleared when obj is       */
856         /* found to be inaccessible.  This happens BEFORE any   */
857         /* finalization code is invoked, and BEFORE any         */
858         /* decisions about finalization order are made.         */
859         /* This is useful in telling the finalizer that         */
860         /* some pointers are not essential for proper           */
861         /* finalization.  This may avoid finalization cycles.   */
862         /* Note that obj may be resurrected by another          */
863         /* finalizer, and thus the clearing of *link may        */
864         /* be visible to non-finalization code.                 */
865         /* There's an argument that an arbitrary action should  */
866         /* be allowed here, instead of just clearing a pointer. */
867         /* But this causes problems if that action alters, or   */
868         /* examines connectivity.  Returns GC_DUPLICATE if link */
869         /* was already registered, GC_SUCCESS if registration   */
870         /* succeeded, GC_NO_MEMORY if it failed for lack of     */
871         /* memory, and GC_oom_fn did not handle the problem.    */
872         /* Only exists for backward compatibility.  See below:  */
873 
874 GC_API int GC_CALL GC_general_register_disappearing_link(void ** /* link */,
875                                                          void * /* obj */);
876         /* A slight generalization of the above. *link is       */
877         /* cleared when obj first becomes inaccessible.  This   */
878         /* can be used to implement weak pointers easily and    */
879         /* safely. Typically link will point to a location      */
880         /* holding a disguised pointer to obj.  (A pointer      */
881         /* inside an "atomic" object is effectively disguised.) */
882         /* In this way, weak pointers are broken before any     */
883         /* object reachable from them gets finalized.           */
884         /* Each link may be registered only with one obj value, */
885         /* i.e. all objects but the last one (link registered   */
886         /* with) are ignored.  This was added after a long      */
887         /* email discussion with John Ellis.                    */
888         /* link must be non-NULL (and be properly aligned).     */
889         /* obj must be a pointer to the first word of an object */
890         /* allocated by GC_malloc or friends.  It is unsafe to  */
891         /* explicitly deallocate the object containing link.    */
892         /* Explicit deallocation of obj may or may not cause    */
893         /* link to eventually be cleared.                       */
894         /* This function can be used to implement certain types */
895         /* of weak pointers.  Note, however, this generally     */
896         /* requires that the allocation lock is held (see       */
897         /* GC_call_with_alloc_lock() below) when the disguised  */
898         /* pointer is accessed.  Otherwise a strong pointer     */
899         /* could be recreated between the time the collector    */
900         /* decides to reclaim the object and the link is        */
901         /* cleared.  Returns GC_SUCCESS if registration         */
902         /* succeeded (a new link is registered), GC_DUPLICATE   */
903         /* if link was already registered (with some object),   */
904         /* GC_NO_MEMORY if registration failed for lack of      */
905         /* memory (and GC_oom_fn did not handle the problem).   */
906 
907 GC_API int GC_CALL GC_unregister_disappearing_link(void ** /* link */);
908         /* Undoes a registration by either of the above two     */
909         /* routines.  Returns 0 if link was not actually        */
910         /* registered (otherwise returns 1).                    */
911 
912 /* Returns !=0 if GC_invoke_finalizers has something to do.     */
913 GC_API int GC_CALL GC_should_invoke_finalizers(void);
914 
915 GC_API int GC_CALL GC_invoke_finalizers(void);
916         /* Run finalizers for all objects that are ready to     */
917         /* be finalized.  Return the number of finalizers       */
918         /* that were run.  Normally this is also called         */
919         /* implicitly during some allocations.  If              */
920         /* GC_finalize_on_demand is nonzero, it must be called  */
921         /* explicitly.                                          */
922 
923 /* Explicitly tell the collector that an object is reachable    */
924 /* at a particular program point.  This prevents the argument   */
925 /* pointer from being optimized away, even it is otherwise no   */
926 /* longer needed.  It should have no visible effect in the      */
927 /* absence of finalizers or disappearing links.  But it may be  */
928 /* needed to prevent finalizers from running while the          */
929 /* associated external resource is still in use.                */
930 /* The function is sometimes called keep_alive in other         */
931 /* settings.                                                    */
932 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
933 # define GC_reachable_here(ptr) \
934                 __asm__ __volatile__(" " : : "X"(ptr) : "memory")
935 #else
936   GC_API void GC_CALL GC_noop1(GC_word);
937 # define GC_reachable_here(ptr) GC_noop1((GC_word)(ptr))
938 #endif
939 
940 /* GC_set_warn_proc can be used to redirect or filter warning messages. */
941 /* p may not be a NULL pointer.  Both the setter and the getter acquire */
942 /* the GC lock (to avoid data races).                                   */
943 typedef void (GC_CALLBACK * GC_warn_proc)(char * /* msg */,
944                                           GC_word /* arg */);
945 GC_API void GC_CALL GC_set_warn_proc(GC_warn_proc /* p */);
946 /* GC_get_warn_proc returns the current warn_proc.                      */
947 GC_API GC_warn_proc GC_CALL GC_get_warn_proc(void);
948 
949 /* GC_ignore_warn_proc may be used as an argument for GC_set_warn_proc  */
950 /* to suppress all warnings (unless statistics printing is turned on).  */
951 GC_API void GC_CALLBACK GC_ignore_warn_proc(char *, GC_word);
952 
953 /* The following is intended to be used by a higher level       */
954 /* (e.g. Java-like) finalization facility.  It is expected      */
955 /* that finalization code will arrange for hidden pointers to   */
956 /* disappear.  Otherwise objects can be accessed after they     */
957 /* have been collected.                                         */
958 /* Note that putting pointers in atomic objects or in           */
959 /* non-pointer slots of "typed" objects is equivalent to        */
960 /* disguising them in this way, and may have other advantages.  */
961 typedef GC_word GC_hidden_pointer;
962 #define GC_HIDE_POINTER(p) (~(GC_hidden_pointer)(p))
963 /* Converting a hidden pointer to a real pointer requires verifying     */
964 /* that the object still exists.  This involves acquiring the           */
965 /* allocator lock to avoid a race with the collector.                   */
966 #define GC_REVEAL_POINTER(p) ((void *)GC_HIDE_POINTER(p))
967 
968 #ifdef I_HIDE_POINTERS
969   /* This exists only for compatibility (the GC-prefixed symbols are    */
970   /* preferred for new code).                                           */
971 # define HIDE_POINTER(p) GC_HIDE_POINTER(p)
972 # define REVEAL_POINTER(p) GC_REVEAL_POINTER(p)
973 #endif
974 
975 typedef void * (GC_CALLBACK * GC_fn_type)(void * /* client_data */);
976 GC_API void * GC_CALL GC_call_with_alloc_lock(GC_fn_type /* fn */,
977                                                 void * /* client_data */);
978 
979 /* These routines are intended to explicitly notify the collector       */
980 /* of new threads.  Often this is unnecessary because thread creation   */
981 /* is implicitly intercepted by the collector, using header-file        */
982 /* defines, or linker-based interception.  In the long run the intent   */
983 /* is to always make redundant registration safe.  In the short run,    */
984 /* this is being implemented a platform at a time.                      */
985 /* The interface is complicated by the fact that we probably will not   */
986 /* ever be able to automatically determine the stack base for thread    */
987 /* stacks on all platforms.                                             */
988 
989 /* Structure representing the base of a thread stack.  On most          */
990 /* platforms this contains just a single address.                       */
991 struct GC_stack_base {
992   void * mem_base; /* Base of memory stack. */
993 # if defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
994     void * reg_base; /* Base of separate register stack. */
995 # endif
996 };
997 
998 typedef void * (GC_CALLBACK * GC_stack_base_func)(
999                 struct GC_stack_base * /* sb */, void * /* arg */);
1000 
1001 /* Call a function with a stack base structure corresponding to         */
1002 /* somewhere in the GC_call_with_stack_base frame.  This often can      */
1003 /* be used to provide a sufficiently accurate stack base.  And we       */
1004 /* implement it everywhere.                                             */
1005 GC_API void * GC_CALL GC_call_with_stack_base(GC_stack_base_func /* fn */,
1006                                               void * /* arg */);
1007 
1008 #define GC_SUCCESS 0
1009 #define GC_DUPLICATE 1          /* Was already registered.              */
1010 #define GC_NO_THREADS 2         /* No thread support in GC.             */
1011         /* GC_NO_THREADS is not returned by any GC function anymore.    */
1012 #define GC_UNIMPLEMENTED 3 /* Not yet implemented on this platform.     */
1013 
1014 #if defined(GC_DARWIN_THREADS) || defined(GC_WIN32_THREADS)
1015   /* Use implicit thread registration and processing (via Win32 DllMain */
1016   /* or Darwin task_threads).  Deprecated.  Must be called before       */
1017   /* GC_INIT() and other GC routines.  Should be avoided if             */
1018   /* GC_pthread_create, GC_beginthreadex (or GC_CreateThread) could be  */
1019   /* called instead.  Disables parallelized GC on Win32.                */
1020   GC_API void GC_CALL GC_use_threads_discovery(void);
1021 #endif
1022 
1023 #ifdef GC_THREADS
1024   /* Return the signal number (constant) used by the garbage collector  */
1025   /* to suspend threads on POSIX systems.  Return -1 otherwise.         */
1026   GC_API int GC_CALL GC_get_suspend_signal(void);
1027 
1028   /* Explicitly enable GC_register_my_thread() invocation.              */
1029   /* Done implicitly if a GC thread-creation function is called (or     */
1030   /* implicit thread registration is activated).  Otherwise, it must    */
1031   /* be called from the main (or any previously registered) thread      */
1032   /* between the collector initialization and the first explicit        */
1033   /* registering of a thread (it should be called as late as possible). */
1034   GC_API void GC_CALL GC_allow_register_threads(void);
1035 
1036   /* Register the current thread, with the indicated stack base, as     */
1037   /* a new thread whose stack(s) should be traced by the GC.  If it     */
1038   /* is not implicitly called by the GC, this must be called before a   */
1039   /* thread can allocate garbage collected memory, or assign pointers   */
1040   /* to the garbage collected heap.  Once registered, a thread will be  */
1041   /* stopped during garbage collections.                                */
1042   /* This call must be previously enabled (see above).                  */
1043   /* This should never be called from the main thread, where it is      */
1044   /* always done implicitly.  This is normally done implicitly if GC_   */
1045   /* functions are called to create the thread, e.g. by including gc.h  */
1046   /* (which redefines some system functions) before calling the system  */
1047   /* thread creation function.  Nonetheless, thread cleanup routines    */
1048   /* (eg., pthread key destructor) typically require manual thread      */
1049   /* registering (and unregistering) if pointers to GC-allocated        */
1050   /* objects are manipulated inside.                                    */
1051   /* It is also always done implicitly on some platforms if             */
1052   /* GC_use_threads_discovery() is called at start-up.  Except for the  */
1053   /* latter case, the explicit call is normally required for threads    */
1054   /* created by third-party libraries.                                  */
1055   /* A manually registered thread requires manual unregistering.        */
1056   GC_API int GC_CALL GC_register_my_thread(const struct GC_stack_base *);
1057 
1058   /* Unregister the current thread.  Only an explicitly registered      */
1059   /* thread (i.e. for which GC_register_my_thread() returns GC_SUCCESS) */
1060   /* is allowed (and required) to call this function.  (As a special    */
1061   /* exception, it is also allowed to once unregister the main thread.) */
1062   /* The thread may no longer allocate garbage collected memory or      */
1063   /* manipulate pointers to the garbage collected heap after making     */
1064   /* this call.  Specifically, if it wants to return or otherwise       */
1065   /* communicate a pointer to the garbage-collected heap to another     */
1066   /* thread, it must do this before calling GC_unregister_my_thread,    */
1067   /* most probably by saving it in a global data structure.  Must not   */
1068   /* be called inside a GC callback function (except for                */
1069   /* GC_call_with_stack_base() one).                                    */
1070   GC_API int GC_CALL GC_unregister_my_thread(void);
1071 #endif /* GC_THREADS */
1072 
1073 /* Wrapper for functions that are likely to block (or, at least, do not */
1074 /* allocate garbage collected memory and/or manipulate pointers to the  */
1075 /* garbage collected heap) for an appreciable length of time.  While fn */
1076 /* is running, the collector is said to be in the "inactive" state for  */
1077 /* the current thread (this means that the thread is not suspended and  */
1078 /* the thread's stack frames "belonging" to the functions in the        */
1079 /* "inactive" state are not scanned during garbage collections).  It is */
1080 /* allowed for fn to call GC_call_with_gc_active() (even recursively),  */
1081 /* thus temporarily toggling the collector's state back to "active".    */
1082 GC_API void * GC_CALL GC_do_blocking(GC_fn_type /* fn */,
1083                                      void * /* client_data */);
1084 
1085 /* Call a function switching to the "active" state of the collector for */
1086 /* the current thread (i.e. the user function is allowed to call any    */
1087 /* GC function and/or manipulate pointers to the garbage collected      */
1088 /* heap).  GC_call_with_gc_active() has the functionality opposite to   */
1089 /* GC_do_blocking() one.  It is assumed that the collector is already   */
1090 /* initialized and the current thread is registered.  fn may toggle     */
1091 /* the collector thread's state temporarily to "inactive" one by using  */
1092 /* GC_do_blocking.  GC_call_with_gc_active() often can be used to       */
1093 /* provide a sufficiently accurate stack base.                          */
1094 GC_API void * GC_CALL GC_call_with_gc_active(GC_fn_type /* fn */,
1095                                              void * /* client_data */);
1096 
1097 /* Attempt to fill in the GC_stack_base structure with the stack base   */
1098 /* for this thread.  This appears to be required to implement anything  */
1099 /* like the JNI AttachCurrentThread in an environment in which new      */
1100 /* threads are not automatically registered with the collector.         */
1101 /* It is also unfortunately hard to implement well on many platforms.   */
1102 /* Returns GC_SUCCESS or GC_UNIMPLEMENTED.                              */
1103 GC_API int GC_CALL GC_get_stack_base(struct GC_stack_base *);
1104 
1105 /* The following routines are primarily intended for use with a         */
1106 /* preprocessor which inserts calls to check C pointer arithmetic.      */
1107 /* They indicate failure by invoking the corresponding _print_proc.     */
1108 
1109 /* Check that p and q point to the same object.                 */
1110 /* Fail conspicuously if they don't.                            */
1111 /* Returns the first argument.                                  */
1112 /* Succeeds if neither p nor q points to the heap.              */
1113 /* May succeed if both p and q point to between heap objects.   */
1114 GC_API void * GC_CALL GC_same_obj(void * /* p */, void * /* q */);
1115 
1116 /* Checked pointer pre- and post- increment operations.  Note that      */
1117 /* the second argument is in units of bytes, not multiples of the       */
1118 /* object size.  This should either be invoked from a macro, or the     */
1119 /* call should be automatically generated.                              */
1120 GC_API void * GC_CALL GC_pre_incr(void **, ptrdiff_t /* how_much */);
1121 GC_API void * GC_CALL GC_post_incr(void **, ptrdiff_t /* how_much */);
1122 
1123 /* Check that p is visible                                              */
1124 /* to the collector as a possibly pointer containing location.          */
1125 /* If it isn't fail conspicuously.                                      */
1126 /* Returns the argument in all cases.  May erroneously succeed          */
1127 /* in hard cases.  (This is intended for debugging use with             */
1128 /* untyped allocations.  The idea is that it should be possible, though */
1129 /* slow, to add such a call to all indirect pointer stores.)            */
1130 /* Currently useless for multi-threaded worlds.                         */
1131 GC_API void * GC_CALL GC_is_visible(void * /* p */);
1132 
1133 /* Check that if p is a pointer to a heap page, then it points to       */
1134 /* a valid displacement within a heap object.                           */
1135 /* Fail conspicuously if this property does not hold.                   */
1136 /* Uninteresting with GC_all_interior_pointers.                         */
1137 /* Always returns its argument.                                         */
1138 GC_API void * GC_CALL GC_is_valid_displacement(void * /* p */);
1139 
1140 /* Explicitly dump the GC state.  This is most often called from the    */
1141 /* debugger, or by setting the GC_DUMP_REGULARLY environment variable,  */
1142 /* but it may be useful to call it from client code during debugging.   */
1143 /* Defined only if the library has been compiled without NO_DEBUGGING.  */
1144 GC_API void GC_CALL GC_dump(void);
1145 
1146 /* Safer, but slow, pointer addition.  Probably useful mainly with      */
1147 /* a preprocessor.  Useful only for heap pointers.                      */
1148 /* Only the macros without trailing digits are meant to be used         */
1149 /* by clients.  These are designed to model the available C pointer     */
1150 /* arithmetic expressions.                                              */
1151 /* Even then, these are probably more useful as                         */
1152 /* documentation than as part of the API.                               */
1153 /* Note that GC_PTR_ADD evaluates the first argument more than once.    */
1154 #if defined(GC_DEBUG) && defined(__GNUC__)
1155 # define GC_PTR_ADD3(x, n, type_of_result) \
1156         ((type_of_result)GC_same_obj((x)+(n), (x)))
1157 # define GC_PRE_INCR3(x, n, type_of_result) \
1158         ((type_of_result)GC_pre_incr((void **)(&(x)), (n)*sizeof(*x)))
1159 # define GC_POST_INCR3(x, n, type_of_result) \
1160         ((type_of_result)GC_post_incr((void **)(&(x)), (n)*sizeof(*x)))
1161 # define GC_PTR_ADD(x, n) GC_PTR_ADD3(x, n, typeof(x))
1162 # define GC_PRE_INCR(x, n) GC_PRE_INCR3(x, n, typeof(x))
1163 # define GC_POST_INCR(x) GC_POST_INCR3(x, 1, typeof(x))
1164 # define GC_POST_DECR(x) GC_POST_INCR3(x, -1, typeof(x))
1165 #else /* !GC_DEBUG || !__GNUC__ */
1166   /* We can't do this right without typeof, which ANSI decided was not    */
1167   /* sufficiently useful.  Without it we resort to the non-debug version. */
1168   /* FIXME: This should eventually support C++0x decltype.                */
1169 # define GC_PTR_ADD(x, n) ((x)+(n))
1170 # define GC_PRE_INCR(x, n) ((x) += (n))
1171 # define GC_POST_INCR(x) ((x)++)
1172 # define GC_POST_DECR(x) ((x)--)
1173 #endif /* !GC_DEBUG || !__GNUC__ */
1174 
1175 /* Safer assignment of a pointer to a non-stack location.       */
1176 #ifdef GC_DEBUG
1177 # define GC_PTR_STORE(p, q) \
1178         (*(void **)GC_is_visible(p) = GC_is_valid_displacement(q))
1179 #else
1180 # define GC_PTR_STORE(p, q) (*(p) = (q))
1181 #endif
1182 
1183 /* Functions called to report pointer checking errors */
1184 GC_API void (GC_CALLBACK * GC_same_obj_print_proc)(void * /* p */,
1185                                                    void * /* q */);
1186 GC_API void (GC_CALLBACK * GC_is_valid_displacement_print_proc)(void *);
1187 GC_API void (GC_CALLBACK * GC_is_visible_print_proc)(void *);
1188 
1189 #ifdef GC_PTHREADS
1190   /* For pthread support, we generally need to intercept a number of    */
1191   /* thread library calls.  We do that here by macro defining them.     */
1192 # include "gc_pthread_redirects.h"
1193 #endif
1194 
1195 /* This returns a list of objects, linked through their first word.     */
1196 /* Its use can greatly reduce lock contention problems, since the       */
1197 /* allocation lock can be acquired and released many fewer times.       */
1198 GC_API void * GC_CALL GC_malloc_many(size_t /* lb */);
1199 #define GC_NEXT(p) (*(void * *)(p))     /* Retrieve the next element    */
1200                                         /* in returned list.            */
1201 
1202 /* A filter function to control the scanning of dynamic libraries.      */
1203 /* If implemented, called by GC before registering a dynamic library    */
1204 /* (discovered by GC) section as a static data root (called only as     */
1205 /* a last reason not to register).  The filename of the library, the    */
1206 /* address and the length of the memory region (section) are passed.    */
1207 /* This routine should return nonzero if that region should be scanned. */
1208 /* Always called with the allocation lock held.  Depending on the       */
1209 /* platform, might be called with the "world" stopped.                  */
1210 typedef int (GC_CALLBACK * GC_has_static_roots_func)(
1211                                         const char * /* dlpi_name */,
1212                                         void * /* section_start */,
1213                                         size_t /* section_size */);
1214 
1215 /* Register a new callback (a user-supplied filter) to control the      */
1216 /* scanning of dynamic libraries.  Replaces any previously registered   */
1217 /* callback.  May be 0 (means no filtering).  May be unused on some     */
1218 /* platforms (if the filtering is unimplemented or inappropriate).      */
1219 GC_API void GC_CALL GC_register_has_static_roots_callback(
1220                                         GC_has_static_roots_func);
1221 
1222 #if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
1223 
1224 # ifndef GC_NO_THREAD_DECLS
1225 
1226 #   ifdef __cplusplus
1227       } /* Including windows.h in an extern "C" context no longer works. */
1228 #   endif
1229 
1230 #   if !defined(_WIN32_WCE) && !defined(__CEGCC__)
1231 #     include <process.h> /* For _beginthreadex, _endthreadex */
1232 #   endif
1233 
1234 #   include <windows.h>
1235 
1236 #   ifdef __cplusplus
1237       extern "C" {
1238 #   endif
1239 
1240 #   ifdef GC_UNDERSCORE_STDCALL
1241       /* Explicitly prefix exported/imported WINAPI (__stdcall) symbols */
1242       /* with '_' (underscore).  Might be useful if MinGW/x86 is used.  */
1243 #     define GC_CreateThread _GC_CreateThread
1244 #     define GC_ExitThread _GC_ExitThread
1245 #   endif
1246 
1247     /* All threads must be created using GC_CreateThread or             */
1248     /* GC_beginthreadex, or must explicitly call GC_register_my_thread  */
1249     /* (and call GC_unregister_my_thread before thread termination), so */
1250     /* that they will be recorded in the thread table.  For backward    */
1251     /* compatibility, it is possible to build the GC with GC_DLL        */
1252     /* defined, and to call GC_use_threads_discovery.  This implicitly  */
1253     /* registers all created threads, but appears to be less robust.    */
1254     /* Currently the collector expects all threads to fall through and  */
1255     /* terminate normally, or call GC_endthreadex() or GC_ExitThread,   */
1256     /* so that the thread is properly unregistered.                     */
1257     GC_API HANDLE WINAPI GC_CreateThread(
1258                 LPSECURITY_ATTRIBUTES /* lpThreadAttributes */,
1259                 DWORD /* dwStackSize */,
1260                 LPTHREAD_START_ROUTINE /* lpStartAddress */,
1261                 LPVOID /* lpParameter */, DWORD /* dwCreationFlags */,
1262                 LPDWORD /* lpThreadId */);
1263 
1264 #   ifndef DECLSPEC_NORETURN
1265       /* Typically defined in winnt.h. */
1266 #     define DECLSPEC_NORETURN /* empty */
1267 #   endif
1268 
1269     GC_API DECLSPEC_NORETURN void WINAPI GC_ExitThread(
1270                                                 DWORD /* dwExitCode */);
1271 
1272 #   if !defined(_WIN32_WCE) && !defined(__CEGCC__)
1273 #     if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED) \
1274                 && !defined(UINTPTR_MAX)
1275         typedef GC_word GC_uintptr_t;
1276 #     else
1277         typedef uintptr_t GC_uintptr_t;
1278 #     endif
1279 
1280       GC_API GC_uintptr_t GC_CALL GC_beginthreadex(
1281                         void * /* security */, unsigned /* stack_size */,
1282                         unsigned (__stdcall *)(void *),
1283                         void * /* arglist */, unsigned /* initflag */,
1284                         unsigned * /* thrdaddr */);
1285 
1286       /* Note: _endthreadex() is not currently marked as no-return in   */
1287       /* VC++ and MinGW headers, so we don't mark it neither.           */
1288       GC_API void GC_CALL GC_endthreadex(unsigned /* retval */);
1289 #   endif /* !_WIN32_WCE */
1290 
1291 # endif /* !GC_NO_THREAD_DECLS */
1292 
1293 # ifdef GC_WINMAIN_REDIRECT
1294     /* win32_threads.c implements the real WinMain(), which will start  */
1295     /* a new thread to call GC_WinMain() after initializing the garbage */
1296     /* collector.                                                       */
1297 #   define WinMain GC_WinMain
1298 # endif
1299 
1300   /* For compatibility only. */
1301 # define GC_use_DllMain GC_use_threads_discovery
1302 
1303 # ifndef GC_NO_THREAD_REDIRECTS
1304 #   define CreateThread GC_CreateThread
1305 #   define ExitThread GC_ExitThread
1306 #   undef _beginthreadex
1307 #   define _beginthreadex GC_beginthreadex
1308 #   undef _endthreadex
1309 #   define _endthreadex GC_endthreadex
1310 /* #define _beginthread { > "Please use _beginthreadex instead of _beginthread" < } */
1311 # endif /* !GC_NO_THREAD_REDIRECTS */
1312 
1313 #endif /* GC_WIN32_THREADS */
1314 
1315 /* Public setter and getter for switching "unmap as much as possible"   */
1316 /* mode on(1) and off(0).  Has no effect unless unmapping is turned on. */
1317 /* Has no effect on implicitly-initiated garbage collections.  Initial  */
1318 /* value is controlled by GC_FORCE_UNMAP_ON_GCOLLECT.                   */
1319 GC_API void GC_CALL GC_set_force_unmap_on_gcollect(int);
1320 GC_API int GC_CALL GC_get_force_unmap_on_gcollect(void);
1321 
1322 /* Fully portable code should call GC_INIT() from the main program      */
1323 /* before making any other GC_ calls.  On most platforms this is a      */
1324 /* no-op and the collector self-initializes.  But a number of           */
1325 /* platforms make that too hard.                                        */
1326 /* A GC_INIT call is required if the collector is built with            */
1327 /* THREAD_LOCAL_ALLOC defined and the initial allocation call is not    */
1328 /* to GC_malloc() or GC_malloc_atomic().                                */
1329 
1330 #ifdef __CYGWIN32__
1331   /* Similarly gnu-win32 DLLs need explicit initialization from the     */
1332   /* main program, as does AIX.                                         */
1333   extern int _data_start__[], _data_end__[], _bss_start__[], _bss_end__[];
1334 # define GC_DATASTART (_data_start__ < _bss_start__ ? \
1335                        (void *)_data_start__ : (void *)_bss_start__)
1336 # define GC_DATAEND (_data_end__ > _bss_end__ ? \
1337                      (void *)_data_end__ : (void *)_bss_end__)
1338 # define GC_INIT_CONF_ROOTS GC_add_roots(GC_DATASTART, GC_DATAEND); \
1339                                  GC_gcollect() /* For blacklisting. */
1340         /* Required at least if GC is in a DLL.  And doesn't hurt. */
1341 #elif defined(_AIX)
1342   extern int _data[], _end[];
1343 # define GC_DATASTART ((void *)((ulong)_data))
1344 # define GC_DATAEND ((void *)((ulong)_end))
1345 # define GC_INIT_CONF_ROOTS GC_add_roots(GC_DATASTART, GC_DATAEND)
1346 #else
1347 # define GC_INIT_CONF_ROOTS /* empty */
1348 #endif
1349 
1350 #ifdef GC_DONT_EXPAND
1351   /* Set GC_dont_expand to TRUE at start-up */
1352 # define GC_INIT_CONF_DONT_EXPAND GC_set_dont_expand(1)
1353 #else
1354 # define GC_INIT_CONF_DONT_EXPAND /* empty */
1355 #endif
1356 
1357 #ifdef GC_FORCE_UNMAP_ON_GCOLLECT
1358   /* Turn on "unmap as much as possible on explicit GC" mode at start-up */
1359 # define GC_INIT_CONF_FORCE_UNMAP_ON_GCOLLECT \
1360                 GC_set_force_unmap_on_gcollect(1)
1361 #else
1362 # define GC_INIT_CONF_FORCE_UNMAP_ON_GCOLLECT /* empty */
1363 #endif
1364 
1365 #ifdef GC_MAX_RETRIES
1366   /* Set GC_max_retries to the desired value at start-up */
1367 # define GC_INIT_CONF_MAX_RETRIES GC_set_max_retries(GC_MAX_RETRIES)
1368 #else
1369 # define GC_INIT_CONF_MAX_RETRIES /* empty */
1370 #endif
1371 
1372 #ifdef GC_FREE_SPACE_DIVISOR
1373   /* Set GC_free_space_divisor to the desired value at start-up */
1374 # define GC_INIT_CONF_FREE_SPACE_DIVISOR \
1375                 GC_set_free_space_divisor(GC_FREE_SPACE_DIVISOR)
1376 #else
1377 # define GC_INIT_CONF_FREE_SPACE_DIVISOR /* empty */
1378 #endif
1379 
1380 #ifdef GC_FULL_FREQ
1381   /* Set GC_full_freq to the desired value at start-up */
1382 # define GC_INIT_CONF_FULL_FREQ GC_set_full_freq(GC_FULL_FREQ)
1383 #else
1384 # define GC_INIT_CONF_FULL_FREQ /* empty */
1385 #endif
1386 
1387 #ifdef GC_TIME_LIMIT
1388   /* Set GC_time_limit to the desired value at start-up */
1389 # define GC_INIT_CONF_TIME_LIMIT GC_set_time_limit(GC_TIME_LIMIT)
1390 #else
1391 # define GC_INIT_CONF_TIME_LIMIT /* empty */
1392 #endif
1393 
1394 #ifdef GC_MAXIMUM_HEAP_SIZE
1395   /* Limit the heap size to the desired value (useful for debugging).   */
1396   /* The limit could be overridden either at the program start-up by    */
1397   /* the similar environment variable or anytime later by the           */
1398   /* corresponding API function call.                                   */
1399 # define GC_INIT_CONF_MAXIMUM_HEAP_SIZE \
1400                 GC_set_max_heap_size(GC_MAXIMUM_HEAP_SIZE)
1401 #else
1402 # define GC_INIT_CONF_MAXIMUM_HEAP_SIZE /* empty */
1403 #endif
1404 
1405 #ifdef GC_IGNORE_WARN
1406   /* Turn off all warnings at start-up (after GC initialization) */
1407 # define GC_INIT_CONF_IGNORE_WARN GC_set_warn_proc(GC_ignore_warn_proc)
1408 #else
1409 # define GC_INIT_CONF_IGNORE_WARN /* empty */
1410 #endif
1411 
1412 #ifdef GC_INITIAL_HEAP_SIZE
1413   /* Set heap size to the desired value at start-up */
1414 # define GC_INIT_CONF_INITIAL_HEAP_SIZE \
1415                 { size_t heap_size = GC_get_heap_size(); \
1416                   if (heap_size < (GC_INITIAL_HEAP_SIZE)) \
1417                     (void)GC_expand_hp((GC_INITIAL_HEAP_SIZE) - heap_size); }
1418 #else
1419 # define GC_INIT_CONF_INITIAL_HEAP_SIZE /* empty */
1420 #endif
1421 
1422 /* Portable clients should call this at the program start-up.  More     */
1423 /* over, some platforms require this call to be done strictly from the  */
1424 /* primordial thread.                                                   */
1425 #define GC_INIT() { GC_INIT_CONF_DONT_EXPAND; /* pre-init */ \
1426                     GC_INIT_CONF_FORCE_UNMAP_ON_GCOLLECT; \
1427                     GC_INIT_CONF_MAX_RETRIES; \
1428                     GC_INIT_CONF_FREE_SPACE_DIVISOR; \
1429                     GC_INIT_CONF_FULL_FREQ; \
1430                     GC_INIT_CONF_TIME_LIMIT; \
1431                     GC_INIT_CONF_MAXIMUM_HEAP_SIZE; \
1432                     GC_init(); /* real GC initialization */ \
1433                     GC_INIT_CONF_ROOTS; /* post-init */ \
1434                     GC_INIT_CONF_IGNORE_WARN; \
1435                     GC_INIT_CONF_INITIAL_HEAP_SIZE; }
1436 
1437 /* win32S may not free all resources on process exit.   */
1438 /* This explicitly deallocates the heap.                */
1439 GC_API void GC_CALL GC_win32_free_heap(void);
1440 
1441 #if defined(_AMIGA) && !defined(GC_AMIGA_MAKINGLIB)
1442   /* Allocation really goes through GC_amiga_allocwrapper_do    */
1443 # include "gc_amiga_redirects.h"
1444 #endif
1445 
1446 #ifdef __cplusplus
1447   }  /* end of extern "C" */
1448 #endif
1449 
1450 #endif /* GC_H */
1451