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