cfe9f6bf8561992857cae8180d3b0978c91aa459
[platform/upstream/libgc.git] / 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 /* Help debug mixed up preprocessor symbols.    */
35 #if (defined(WIN64) && !defined(_WIN64)) && defined(_MSC_VER)
36 #pragma message("Warning: Expecting _WIN64 for x64 targets! Notice the leading underscore!")
37 #endif
38
39 #include "gc_version.h"
40         /* Define version numbers here to allow test on build machine   */
41         /* for cross-builds.  Note that this defines the header         */
42         /* version number, which may or may not match that of the       */
43         /* dynamic library.  GC_get_version() can be used to obtain     */
44         /* the latter.                                                  */
45
46 #include "gc_config_macros.h"
47
48 #ifdef __cplusplus
49   extern "C" {
50 #endif
51
52 typedef void * GC_PTR;  /* preserved only for backward compatibility    */
53
54 /* Define word and signed_word to be unsigned and signed types of the   */
55 /* size as char * or void *.  There seems to be no way to do this       */
56 /* even semi-portably.  The following is probably no better/worse       */
57 /* than almost anything else.                                           */
58 /* The ANSI standard suggests that size_t and ptrdiff_t might be        */
59 /* better choices.  But those had incorrect definitions on some older   */
60 /* systems.  Notably "typedef int size_t" is WRONG.                     */
61 #ifdef _WIN64
62 # if defined(__int64) && !defined(CPPCHECK)
63     typedef unsigned __int64 GC_word;
64     typedef __int64 GC_signed_word;
65 # else
66     typedef unsigned long long GC_word;
67     typedef long long GC_signed_word;
68 # endif
69 #else
70   typedef unsigned long GC_word;
71   typedef long GC_signed_word;
72 #endif
73
74 /* Get the GC library version. The returned value is a constant in the  */
75 /* form: ((version_major<<16) | (version_minor<<8) | version_micro).    */
76 GC_API unsigned GC_CALL GC_get_version(void);
77
78 /* Public read-only variables */
79 /* The supplied getter functions are preferred for new code.            */
80
81 GC_API GC_ATTR_DEPRECATED GC_word GC_gc_no;
82                         /* Counter incremented per collection.          */
83                         /* Includes empty GCs at startup.               */
84 GC_API GC_word GC_CALL GC_get_gc_no(void);
85                         /* GC_get_gc_no() is unsynchronized, so         */
86                         /* it requires GC_call_with_alloc_lock() to     */
87                         /* avoid data races on multiprocessors.         */
88
89 #ifdef GC_THREADS
90   GC_API GC_ATTR_DEPRECATED int GC_parallel;
91                         /* GC is parallelized for performance on        */
92                         /* multiprocessors.  Currently set only         */
93                         /* implicitly if collector is built with        */
94                         /* PARALLEL_MARK defined and if either:         */
95                         /*  Env variable GC_NPROC is set to > 1, or     */
96                         /*  GC_NPROC is not set and this is an MP.      */
97                         /* If GC_parallel is on (non-zero), incremental */
98                         /* collection is only partially functional,     */
99                         /* and may not be desirable.  The getter does   */
100                         /* not use or need synchronization (i.e.        */
101                         /* acquiring the GC lock).  Starting from       */
102                         /* GC v7.3, GC_parallel value is equal to the   */
103                         /* number of marker threads minus one (i.e.     */
104                         /* number of existing parallel marker threads   */
105                         /* excluding the initiating one).               */
106   GC_API int GC_CALL GC_get_parallel(void);
107 #endif
108
109
110 /* Public R/W variables */
111 /* The supplied setter and getter functions are preferred for new code. */
112
113 typedef void * (GC_CALLBACK * GC_oom_func)(size_t /* bytes_requested */);
114 GC_API GC_ATTR_DEPRECATED GC_oom_func GC_oom_fn;
115                         /* When there is insufficient memory to satisfy */
116                         /* an allocation request, we return             */
117                         /* (*GC_oom_fn)(size).  By default this just    */
118                         /* returns NULL.                                */
119                         /* If it returns, it must return 0 or a valid   */
120                         /* pointer to a previously allocated heap       */
121                         /* object.  GC_oom_fn must not be 0.            */
122                         /* Both the supplied setter and the getter      */
123                         /* acquire the GC lock (to avoid data races).   */
124 GC_API void GC_CALL GC_set_oom_fn(GC_oom_func) GC_ATTR_NONNULL(1);
125 GC_API GC_oom_func GC_CALL GC_get_oom_fn(void);
126
127 typedef void (GC_CALLBACK * GC_on_heap_resize_proc)(GC_word /* new_size */);
128 GC_API GC_ATTR_DEPRECATED GC_on_heap_resize_proc GC_on_heap_resize;
129                         /* Invoked when the heap grows or shrinks.      */
130                         /* Called with the world stopped (and the       */
131                         /* allocation lock held).  May be 0.            */
132 GC_API void GC_CALL GC_set_on_heap_resize(GC_on_heap_resize_proc);
133 GC_API GC_on_heap_resize_proc GC_CALL GC_get_on_heap_resize(void);
134                         /* Both the supplied setter and the getter      */
135                         /* acquire the GC lock (to avoid data races).   */
136
137 typedef enum {
138     GC_EVENT_START /* COLLECTION */,
139     GC_EVENT_MARK_START,
140     GC_EVENT_MARK_END,
141     GC_EVENT_RECLAIM_START,
142     GC_EVENT_RECLAIM_END,
143     GC_EVENT_END /* COLLECTION */,
144     GC_EVENT_PRE_STOP_WORLD /* STOPWORLD_BEGIN */,
145     GC_EVENT_POST_STOP_WORLD /* STOPWORLD_END */,
146     GC_EVENT_PRE_START_WORLD /* STARTWORLD_BEGIN */,
147     GC_EVENT_POST_START_WORLD /* STARTWORLD_END */,
148     GC_EVENT_THREAD_SUSPENDED,
149     GC_EVENT_THREAD_UNSUSPENDED
150 } GC_EventType;
151
152 typedef void (GC_CALLBACK * GC_on_collection_event_proc)(GC_EventType);
153                         /* Invoked to indicate progress through the     */
154                         /* collection process.  Not used for thread     */
155                         /* suspend/resume notifications.  Called with   */
156                         /* the GC lock held (or, even, the world        */
157                         /* stopped).  May be 0 (means no notifier).     */
158 GC_API void GC_CALL GC_set_on_collection_event(GC_on_collection_event_proc);
159 GC_API GC_on_collection_event_proc GC_CALL GC_get_on_collection_event(void);
160                         /* Both the supplied setter and the getter      */
161                         /* acquire the GC lock (to avoid data races).   */
162
163 #ifdef GC_THREADS
164   typedef void (GC_CALLBACK * GC_on_thread_event_proc)(GC_EventType,
165                                                 void * /* thread_id */);
166                         /* Invoked when a thread is suspended or        */
167                         /* resumed during collection.  Called with the  */
168                         /* GC lock held (and the world stopped          */
169                         /* partially).  May be 0 (means no notifier).   */
170   GC_API void GC_CALL GC_set_on_thread_event(GC_on_thread_event_proc);
171   GC_API GC_on_thread_event_proc GC_CALL GC_get_on_thread_event(void);
172                         /* Both the supplied setter and the getter      */
173                         /* acquire the GC lock (to avoid data races).   */
174 #endif
175
176 GC_API GC_ATTR_DEPRECATED int GC_find_leak;
177                         /* Set to true to turn on the leak-finding mode */
178                         /* (do not actually garbage collect, but simply */
179                         /* report inaccessible memory that was not      */
180                         /* deallocated with GC_FREE).  Initial value    */
181                         /* is determined by FIND_LEAK macro.            */
182                         /* The value should not typically be modified   */
183                         /* after GC initialization (and, thus, it does  */
184                         /* not use or need synchronization).            */
185 GC_API void GC_CALL GC_set_find_leak(int);
186 GC_API int GC_CALL GC_get_find_leak(void);
187
188 GC_API GC_ATTR_DEPRECATED int GC_all_interior_pointers;
189                         /* Arrange for pointers to object interiors to  */
190                         /* be recognized as valid.  Typically should    */
191                         /* not be changed after GC initialization (in   */
192                         /* case of calling it after the GC is           */
193                         /* initialized, the setter acquires the GC lock */
194                         /* (to avoid data races).  The initial value    */
195                         /* depends on whether the GC is built with      */
196                         /* ALL_INTERIOR_POINTERS macro defined or not.  */
197                         /* Unless DONT_ADD_BYTE_AT_END is defined, this */
198                         /* also affects whether sizes are increased by  */
199                         /* at least a byte to allow "off the end"       */
200                         /* pointer recognition.  Must be only 0 or 1.   */
201 GC_API void GC_CALL GC_set_all_interior_pointers(int);
202 GC_API int GC_CALL GC_get_all_interior_pointers(void);
203
204 GC_API GC_ATTR_DEPRECATED int GC_finalize_on_demand;
205                         /* If nonzero, finalizers will only be run in   */
206                         /* response to an explicit GC_invoke_finalizers */
207                         /* call.  The default is determined by whether  */
208                         /* the FINALIZE_ON_DEMAND macro is defined      */
209                         /* when the collector is built.                 */
210                         /* The setter and getter are unsynchronized.    */
211 GC_API void GC_CALL GC_set_finalize_on_demand(int);
212 GC_API int GC_CALL GC_get_finalize_on_demand(void);
213
214 GC_API GC_ATTR_DEPRECATED int GC_java_finalization;
215                         /* Mark objects reachable from finalizable      */
216                         /* objects in a separate post-pass.  This makes */
217                         /* it a bit safer to use non-topologically-     */
218                         /* ordered finalization.  Default value is      */
219                         /* determined by JAVA_FINALIZATION macro.       */
220                         /* Enables register_finalizer_unreachable to    */
221                         /* work correctly.                              */
222                         /* The setter and getter are unsynchronized.    */
223 GC_API void GC_CALL GC_set_java_finalization(int);
224 GC_API int GC_CALL GC_get_java_finalization(void);
225
226 typedef void (GC_CALLBACK * GC_finalizer_notifier_proc)(void);
227 GC_API GC_ATTR_DEPRECATED GC_finalizer_notifier_proc GC_finalizer_notifier;
228                         /* Invoked by the collector when there are      */
229                         /* objects to be finalized.  Invoked at most    */
230                         /* once per GC cycle.  Never invoked unless     */
231                         /* GC_finalize_on_demand is set.                */
232                         /* Typically this will notify a finalization    */
233                         /* thread, which will call GC_invoke_finalizers */
234                         /* in response.  May be 0 (means no notifier).  */
235                         /* Both the supplied setter and the getter      */
236                         /* acquire the GC lock (to avoid data races).   */
237 GC_API void GC_CALL GC_set_finalizer_notifier(GC_finalizer_notifier_proc);
238 GC_API GC_finalizer_notifier_proc GC_CALL GC_get_finalizer_notifier(void);
239
240 GC_API
241 # ifndef GC_DONT_GC
242     GC_ATTR_DEPRECATED
243 # endif
244   int GC_dont_gc;       /* != 0 ==> Don't collect.  In versions 6.2a1+, */
245                         /* this overrides explicit GC_gcollect() calls. */
246                         /* Used as a counter, so that nested enabling   */
247                         /* and disabling work correctly.  Should        */
248                         /* normally be updated with GC_enable() and     */
249                         /* GC_disable() calls.  Direct assignment to    */
250                         /* GC_dont_gc is deprecated.  To check whether  */
251                         /* GC is disabled, GC_is_disabled() is          */
252                         /* preferred for new code.                      */
253
254 GC_API GC_ATTR_DEPRECATED int GC_dont_expand;
255                         /* Do not expand the heap unless explicitly     */
256                         /* requested or forced to.  The setter and      */
257                         /* getter are unsynchronized.                   */
258 GC_API void GC_CALL GC_set_dont_expand(int);
259 GC_API int GC_CALL GC_get_dont_expand(void);
260
261 GC_API GC_ATTR_DEPRECATED int GC_use_entire_heap;
262                 /* Causes the non-incremental collector to use the      */
263                 /* entire heap before collecting.  This was the only    */
264                 /* option for GC versions < 5.0.  This sometimes        */
265                 /* results in more large block fragmentation, since     */
266                 /* very large blocks will tend to get broken up         */
267                 /* during each GC cycle.  It is likely to result in a   */
268                 /* larger working set, but lower collection             */
269                 /* frequencies, and hence fewer instructions executed   */
270                 /* in the collector.                                    */
271
272 GC_API GC_ATTR_DEPRECATED int GC_full_freq;
273                             /* Number of partial collections between    */
274                             /* full collections.  Matters only if       */
275                             /* GC_is_incremental_mode().                */
276                             /* Full collections are also triggered if   */
277                             /* the collector detects a substantial      */
278                             /* increase in the number of in-use heap    */
279                             /* blocks.  Values in the tens are now      */
280                             /* perfectly reasonable, unlike for         */
281                             /* earlier GC versions.                     */
282                         /* The setter and getter are unsynchronized, so */
283                         /* GC_call_with_alloc_lock() is required to     */
284                         /* avoid data races (if the value is modified   */
285                         /* after the GC is put to multi-threaded mode). */
286 GC_API void GC_CALL GC_set_full_freq(int);
287 GC_API int GC_CALL GC_get_full_freq(void);
288
289 GC_API GC_ATTR_DEPRECATED GC_word GC_non_gc_bytes;
290                         /* Bytes not considered candidates for          */
291                         /* collection.  Used only to control scheduling */
292                         /* of collections.  Updated by                  */
293                         /* GC_malloc_uncollectable and GC_free.         */
294                         /* Wizards only.                                */
295                         /* The setter and getter are unsynchronized, so */
296                         /* GC_call_with_alloc_lock() is required to     */
297                         /* avoid data races (if the value is modified   */
298                         /* after the GC is put to multi-threaded mode). */
299 GC_API void GC_CALL GC_set_non_gc_bytes(GC_word);
300 GC_API GC_word GC_CALL GC_get_non_gc_bytes(void);
301
302 GC_API GC_ATTR_DEPRECATED int GC_no_dls;
303                         /* Don't register dynamic library data segments. */
304                         /* Wizards only.  Should be used only if the     */
305                         /* application explicitly registers all roots.   */
306                         /* (In some environments like Microsoft Windows  */
307                         /* and Apple's Darwin, this may also prevent     */
308                         /* registration of the main data segment as part */
309                         /* of the root set.)                             */
310                         /* The setter and getter are unsynchronized.     */
311 GC_API void GC_CALL GC_set_no_dls(int);
312 GC_API int GC_CALL GC_get_no_dls(void);
313
314 GC_API GC_ATTR_DEPRECATED GC_word GC_free_space_divisor;
315                         /* We try to make sure that we allocate at      */
316                         /* least N/GC_free_space_divisor bytes between  */
317                         /* collections, where N is twice the number     */
318                         /* of traced bytes, plus the number of untraced */
319                         /* bytes (bytes in "atomic" objects), plus      */
320                         /* a rough estimate of the root set size.       */
321                         /* N approximates GC tracing work per GC.       */
322                         /* The initial value is GC_FREE_SPACE_DIVISOR.  */
323                         /* Increasing its value will use less space     */
324                         /* but more collection time.  Decreasing it     */
325                         /* will appreciably decrease collection time    */
326                         /* at the expense of space.                     */
327                         /* The setter and getter are unsynchronized, so */
328                         /* GC_call_with_alloc_lock() is required to     */
329                         /* avoid data races (if the value is modified   */
330                         /* after the GC is put to multi-threaded mode). */
331                         /* In version 7.1 (and before), the setter      */
332                         /* returned the old value.                      */
333 GC_API void GC_CALL GC_set_free_space_divisor(GC_word);
334 GC_API GC_word GC_CALL GC_get_free_space_divisor(void);
335
336 GC_API GC_ATTR_DEPRECATED GC_word GC_max_retries;
337                         /* The maximum number of GCs attempted before   */
338                         /* reporting out of memory after heap           */
339                         /* expansion fails.  Initially 0.               */
340                         /* The setter and getter are unsynchronized, so */
341                         /* GC_call_with_alloc_lock() is required to     */
342                         /* avoid data races (if the value is modified   */
343                         /* after the GC is put to multi-threaded mode). */
344 GC_API void GC_CALL GC_set_max_retries(GC_word);
345 GC_API GC_word GC_CALL GC_get_max_retries(void);
346
347
348 GC_API GC_ATTR_DEPRECATED char *GC_stackbottom;
349                                 /* Cool end of user stack.              */
350                                 /* May be set in the client prior to    */
351                                 /* calling any GC_ routines.  This      */
352                                 /* avoids some overhead, and            */
353                                 /* potentially some signals that can    */
354                                 /* confuse debuggers.  Otherwise the    */
355                                 /* collector attempts to set it         */
356                                 /* automatically.                       */
357                                 /* For multi-threaded code, this is the */
358                                 /* cold end of the stack for the        */
359                                 /* primordial thread.  Portable clients */
360                                 /* should use GC_get_stack_base(),      */
361                                 /* GC_call_with_gc_active() and         */
362                                 /* GC_register_my_thread() instead.     */
363
364 GC_API GC_ATTR_DEPRECATED int GC_dont_precollect;
365                                 /* Do not collect as part of GC         */
366                                 /* initialization.  Should be set only  */
367                                 /* if the client wants a chance to      */
368                                 /* manually initialize the root set     */
369                                 /* before the first collection.         */
370                                 /* Interferes with blacklisting.        */
371                                 /* Wizards only.  The setter and getter */
372                                 /* are unsynchronized (and no external  */
373                                 /* locking is needed since the value is */
374                                 /* accessed at GC initialization only). */
375 GC_API void GC_CALL GC_set_dont_precollect(int);
376 GC_API int GC_CALL GC_get_dont_precollect(void);
377
378 GC_API GC_ATTR_DEPRECATED unsigned long GC_time_limit;
379                                /* If incremental collection is enabled, */
380                                /* We try to terminate collections       */
381                                /* after this many milliseconds.  Not a  */
382                                /* hard time bound.  Setting this to     */
383                                /* GC_TIME_UNLIMITED will essentially    */
384                                /* disable incremental collection while  */
385                                /* leaving generational collection       */
386                                /* enabled.                              */
387 #define GC_TIME_UNLIMITED 999999
388                                /* Setting GC_time_limit to this value   */
389                                /* will disable the "pause time exceeded"*/
390                                /* tests.                                */
391                         /* The setter and getter are unsynchronized, so */
392                         /* GC_call_with_alloc_lock() is required to     */
393                         /* avoid data races (if the value is modified   */
394                         /* after the GC is put to multi-threaded mode). */
395 GC_API void GC_CALL GC_set_time_limit(unsigned long);
396 GC_API unsigned long GC_CALL GC_get_time_limit(void);
397
398 /* Public procedures */
399
400 /* Tell the collector to start various performance measurements.        */
401 /* Only the total time taken by full collections is calculated, as      */
402 /* of now.  And, currently, there is no way to stop the measurements.   */
403 /* The function does not use any synchronization.  Defined only if the  */
404 /* library has been compiled without NO_CLOCK.                          */
405 GC_API void GC_CALL GC_start_performance_measurement(void);
406
407 /* Get the total time of all full collections since the start of the    */
408 /* performance measurements.  The measurement unit is one millisecond.  */
409 /* Note that the returned value wraps around on overflow.               */
410 /* The function does not use any synchronization.  Defined only if the  */
411 /* library has been compiled without NO_CLOCK.                          */
412 GC_API unsigned long GC_CALL GC_get_full_gc_total_time(void);
413
414 /* Set whether the GC will allocate executable memory pages or not.     */
415 /* A non-zero argument instructs the collector to allocate memory with  */
416 /* the executable flag on.  Must be called before the collector is      */
417 /* initialized.  May have no effect on some platforms.  The default     */
418 /* value is controlled by NO_EXECUTE_PERMISSION macro (if present then  */
419 /* the flag is off).  Portable clients should have                      */
420 /* GC_set_pages_executable(1) call (before GC_INIT) provided they are   */
421 /* going to execute code on any of the GC-allocated memory objects.     */
422 GC_API void GC_CALL GC_set_pages_executable(int);
423
424 /* Returns non-zero value if the GC is set to the allocate-executable   */
425 /* mode.  The mode could be changed by GC_set_pages_executable (before  */
426 /* GC_INIT) unless the former has no effect on the platform.  Does not  */
427 /* use or need synchronization (i.e. acquiring the allocator lock).     */
428 GC_API int GC_CALL GC_get_pages_executable(void);
429
430 /* The setter and getter of the minimum value returned by the internal  */
431 /* min_bytes_allocd().  The value should not be zero; the default value */
432 /* is one.  Not synchronized.                                           */
433 GC_API void GC_CALL GC_set_min_bytes_allocd(size_t);
434 GC_API size_t GC_CALL GC_get_min_bytes_allocd(void);
435
436 /* Set/get the size in pages of units operated by GC_collect_a_little.  */
437 /* The value should not be zero.  Not synchronized.                     */
438 GC_API void GC_CALL GC_set_rate(int);
439 GC_API int GC_CALL GC_get_rate(void);
440
441 /* Set/get the maximum number of prior attempts at the world-stop       */
442 /* marking.  Not synchronized.                                          */
443 GC_API void GC_CALL GC_set_max_prior_attempts(int);
444 GC_API int GC_CALL GC_get_max_prior_attempts(void);
445
446 /* Overrides the default handle-fork mode.  Non-zero value means GC     */
447 /* should install proper pthread_atfork handlers.  Has effect only if   */
448 /* called before GC_INIT.  Clients should invoke GC_set_handle_fork     */
449 /* with non-zero argument if going to use fork with GC functions called */
450 /* in the forked child.  (Note that such client and atfork handlers     */
451 /* activities are not fully POSIX-compliant.)  GC_set_handle_fork       */
452 /* instructs GC_init to setup GC fork handlers using pthread_atfork,    */
453 /* the latter might fail (or, even, absent on some targets) causing     */
454 /* abort at GC initialization.  Starting from 7.3alpha3, problems with  */
455 /* missing (or failed) pthread_atfork() could be avoided by invocation  */
456 /* of GC_set_handle_fork(-1) at application start-up and surrounding    */
457 /* each fork() with the relevant GC_atfork_prepare/parent/child calls.  */
458 GC_API void GC_CALL GC_set_handle_fork(int);
459
460 /* Routines to handle POSIX fork() manually (no-op if handled           */
461 /* automatically).  GC_atfork_prepare should be called immediately      */
462 /* before fork(); GC_atfork_parent should be invoked just after fork in */
463 /* the branch that corresponds to parent process (i.e., fork result is  */
464 /* non-zero); GC_atfork_child is to be called immediately in the child  */
465 /* branch (i.e., fork result is 0). Note that GC_atfork_child() call    */
466 /* should, of course, precede GC_start_mark_threads call (if any).      */
467 GC_API void GC_CALL GC_atfork_prepare(void);
468 GC_API void GC_CALL GC_atfork_parent(void);
469 GC_API void GC_CALL GC_atfork_child(void);
470
471 /* Initialize the collector.  Portable clients should call GC_INIT()    */
472 /* from the main program instead.                                       */
473 GC_API void GC_CALL GC_init(void);
474
475 /* Returns non-zero (TRUE) if and only if the collector is initialized  */
476 /* (or, at least, the initialization is in progress).                   */
477 GC_API int GC_CALL GC_is_init_called(void);
478
479 /* Perform the collector shutdown.  (E.g. dispose critical sections on  */
480 /* Win32 target.)  A duplicate invocation is a no-op.  GC_INIT should   */
481 /* not be called after the shutdown.  See also GC_win32_free_heap().    */
482 GC_API void GC_CALL GC_deinit(void);
483
484 /* General purpose allocation routines, with roughly malloc calling     */
485 /* conv.  The atomic versions promise that no relevant pointers are     */
486 /* contained in the object.  The non-atomic versions guarantee that the */
487 /* new object is cleared.  GC_malloc_uncollectable allocates            */
488 /* an object that is scanned for pointers to collectible                */
489 /* objects, but is not itself collectible.  The object is scanned even  */
490 /* if it does not appear to be reachable.  GC_malloc_uncollectable and  */
491 /* GC_free called on the resulting object implicitly update             */
492 /* GC_non_gc_bytes appropriately.                                       */
493 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
494         GC_malloc(size_t /* size_in_bytes */);
495 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
496         GC_malloc_atomic(size_t /* size_in_bytes */);
497 GC_API GC_ATTR_MALLOC char * GC_CALL GC_strdup(const char *);
498 GC_API GC_ATTR_MALLOC char * GC_CALL
499         GC_strndup(const char *, size_t) GC_ATTR_NONNULL(1);
500 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
501         GC_malloc_uncollectable(size_t /* size_in_bytes */);
502 GC_API GC_ATTR_DEPRECATED void * GC_CALL GC_malloc_stubborn(size_t);
503
504 /* GC_memalign() is not well tested.                                    */
505 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(2) void * GC_CALL
506         GC_memalign(size_t /* align */, size_t /* lb */);
507 GC_API int GC_CALL GC_posix_memalign(void ** /* memptr */, size_t /* align */,
508                         size_t /* lb */) GC_ATTR_NONNULL(1);
509
510 /* Explicitly deallocate an object.  Dangerous if used incorrectly.     */
511 /* Requires a pointer to the base of an object.                         */
512 /* An object should not be enabled for finalization (and it should not  */
513 /* contain registered disappearing links of any kind) when it is        */
514 /* explicitly deallocated.                                              */
515 /* GC_free(0) is a no-op, as required by ANSI C for free.               */
516 GC_API void GC_CALL GC_free(void *);
517
518 /* The "stubborn" objects allocation is not supported anymore.  Exists  */
519 /* only for the backward compatibility.                                 */
520 #define GC_MALLOC_STUBBORN(sz)  GC_MALLOC(sz)
521 #define GC_NEW_STUBBORN(t)      GC_NEW(t)
522 #define GC_CHANGE_STUBBORN(p)   GC_change_stubborn(p)
523 GC_API GC_ATTR_DEPRECATED void GC_CALL GC_change_stubborn(const void *);
524
525 /* Inform the collector that the object has been changed.               */
526 /* Only non-NULL pointer stores into the object are considered to be    */
527 /* changes.  Matters only if the library has been compiled with         */
528 /* MANUAL_VDB defined (otherwise the function does nothing).            */
529 /* Should be followed typically by GC_reachable_here called for each    */
530 /* of the stored pointers.                                              */
531 GC_API void GC_CALL GC_end_stubborn_change(const void *) GC_ATTR_NONNULL(1);
532
533 /* Return a pointer to the base (lowest address) of an object given     */
534 /* a pointer to a location within the object.                           */
535 /* I.e., map an interior pointer to the corresponding base pointer.     */
536 /* Note that with debugging allocation, this returns a pointer to the   */
537 /* actual base of the object, i.e. the debug information, not to        */
538 /* the base of the user object.                                         */
539 /* Return 0 if displaced_pointer doesn't point to within a valid        */
540 /* object.                                                              */
541 /* Note that a deallocated object in the garbage collected heap         */
542 /* may be considered valid, even if it has been deallocated with        */
543 /* GC_free.                                                             */
544 GC_API void * GC_CALL GC_base(void * /* displaced_pointer */);
545
546 /* Return non-zero (TRUE) if and only if the argument points to         */
547 /* somewhere in GC heap.  Primary use is as a fast alternative to       */
548 /* GC_base to check whether the pointed object is allocated by GC       */
549 /* or not.  It is assumed that the collector is already initialized.    */
550 GC_API int GC_CALL GC_is_heap_ptr(const void *);
551
552 /* Given a pointer to the base of an object, return its size in bytes.  */
553 /* The returned size may be slightly larger than what was originally    */
554 /* requested.                                                           */
555 GC_API size_t GC_CALL GC_size(const void * /* obj_addr */) GC_ATTR_NONNULL(1);
556
557 /* For compatibility with C library.  This is occasionally faster than  */
558 /* a malloc followed by a bcopy.  But if you rely on that, either here  */
559 /* or with the standard C library, your code is broken.  In my          */
560 /* opinion, it shouldn't have been invented, but now we're stuck. -HB   */
561 /* The resulting object has the same kind as the original.              */
562 /* It is an error to have changes enabled for the original object.      */
563 /* It does not change the content of the object from its beginning to   */
564 /* the minimum of old size and new_size_in_bytes; the content above in  */
565 /* case of object size growth is initialized to zero (not guaranteed    */
566 /* for atomic object type).  The function follows ANSI conventions for  */
567 /* NULL old_object (i.e., equivalent to GC_malloc regardless of new     */
568 /* size).  If new size is zero (and old_object is non-NULL) then the    */
569 /* call is equivalent to GC_free (and NULL is returned).  If old_object */
570 /* is non-NULL, it must have been returned by an earlier call to        */
571 /* GC_malloc* or GC_realloc.  In case of the allocation failure, the    */
572 /* memory pointed by old_object is untouched (and not freed).           */
573 /* If the returned pointer is not the same as old_object and both of    */
574 /* them are non-NULL then old_object is freed.  Returns either NULL (in */
575 /* case of the allocation failure or zero new size) or pointer to the   */
576 /* allocated memory.                                                    */
577 GC_API void * GC_CALL GC_realloc(void * /* old_object */,
578                                  size_t /* new_size_in_bytes */)
579                         /* 'realloc' attr */ GC_ATTR_ALLOC_SIZE(2);
580
581 /* Explicitly increase the heap size.   */
582 /* Returns 0 on failure, 1 on success.  */
583 GC_API int GC_CALL GC_expand_hp(size_t /* number_of_bytes */);
584
585 /* Limit the heap size to n bytes.  Useful when you're debugging,       */
586 /* especially on systems that don't handle running out of memory well.  */
587 /* n == 0 ==> unbounded.  This is the default.  This setter function is */
588 /* unsynchronized (so it might require GC_call_with_alloc_lock to avoid */
589 /* data races).                                                         */
590 GC_API void GC_CALL GC_set_max_heap_size(GC_word /* n */);
591
592 /* Inform the collector that a certain section of statically allocated  */
593 /* memory contains no pointers to garbage collected memory.  Thus it    */
594 /* need not be scanned.  This is sometimes important if the application */
595 /* maps large read/write files into the address space, which could be   */
596 /* mistaken for dynamic library data segments on some systems.          */
597 /* Both section start and end are not needed to be pointer-aligned.     */
598 GC_API void GC_CALL GC_exclude_static_roots(void * /* low_address */,
599                                             void * /* high_address_plus_1 */);
600
601 /* Clear the set of root segments.  Wizards only.                       */
602 GC_API void GC_CALL GC_clear_roots(void);
603
604 /* Add a root segment.  Wizards only.                                   */
605 /* Both segment start and end are not needed to be pointer-aligned.     */
606 /* low_address must not be greater than high_address_plus_1.            */
607 GC_API void GC_CALL GC_add_roots(void * /* low_address */,
608                                  void * /* high_address_plus_1 */);
609
610 /* Remove a root segment.  Wizards only.                                */
611 /* May be unimplemented on some platforms.                              */
612 GC_API void GC_CALL GC_remove_roots(void * /* low_address */,
613                                     void * /* high_address_plus_1 */);
614
615 /* Add a displacement to the set of those considered valid by the       */
616 /* collector.  GC_register_displacement(n) means that if p was returned */
617 /* by GC_malloc, then (char *)p + n will be considered to be a valid    */
618 /* pointer to p.  N must be small and less than the size of p.          */
619 /* (All pointers to the interior of objects from the stack are          */
620 /* considered valid in any case.  This applies to heap objects and      */
621 /* static data.)                                                        */
622 /* Preferably, this should be called before any other GC procedures.    */
623 /* Calling it later adds to the probability of excess memory            */
624 /* retention.                                                           */
625 /* This is a no-op if the collector has recognition of                  */
626 /* arbitrary interior pointers enabled, which is now the default.       */
627 GC_API void GC_CALL GC_register_displacement(size_t /* n */);
628
629 /* The following version should be used if any debugging allocation is  */
630 /* being done.                                                          */
631 GC_API void GC_CALL GC_debug_register_displacement(size_t /* n */);
632
633 /* Explicitly trigger a full, world-stop collection.    */
634 GC_API void GC_CALL GC_gcollect(void);
635
636 /* Same as above but ignores the default stop_func setting and tries to */
637 /* unmap as much memory as possible (regardless of the corresponding    */
638 /* switch setting).  The recommended usage: on receiving a system       */
639 /* low-memory event; before retrying a system call failed because of    */
640 /* the system is running out of resources.                              */
641 GC_API void GC_CALL GC_gcollect_and_unmap(void);
642
643 /* Trigger a full world-stopped collection.  Abort the collection if    */
644 /* and when stop_func returns a nonzero value.  Stop_func will be       */
645 /* called frequently, and should be reasonably fast.  (stop_func is     */
646 /* called with the allocation lock held and the world might be stopped; */
647 /* it's not allowed for stop_func to manipulate pointers to the garbage */
648 /* collected heap or call most of GC functions.)  This works even       */
649 /* if virtual dirty bits, and hence incremental collection is not       */
650 /* available for this architecture.  Collections can be aborted faster  */
651 /* than normal pause times for incremental collection.  However,        */
652 /* aborted collections do no useful work; the next collection needs     */
653 /* to start from the beginning.  stop_func must not be 0.               */
654 /* GC_try_to_collect() returns 0 if the collection was aborted (or the  */
655 /* collections are disabled), 1 if it succeeded.                        */
656 typedef int (GC_CALLBACK * GC_stop_func)(void);
657 GC_API int GC_CALL GC_try_to_collect(GC_stop_func /* stop_func */)
658                                                         GC_ATTR_NONNULL(1);
659
660 /* Set and get the default stop_func.  The default stop_func is used by */
661 /* GC_gcollect() and by implicitly trigged collections (except for the  */
662 /* case when handling out of memory).  Must not be 0.                   */
663 /* Both the setter and getter acquire the GC lock to avoid data races.  */
664 GC_API void GC_CALL GC_set_stop_func(GC_stop_func /* stop_func */)
665                                                         GC_ATTR_NONNULL(1);
666 GC_API GC_stop_func GC_CALL GC_get_stop_func(void);
667
668 /* Return the number of bytes in the heap.  Excludes collector private  */
669 /* data structures.  Excludes the unmapped memory (returned to the OS). */
670 /* Includes empty blocks and fragmentation loss.  Includes some pages   */
671 /* that were allocated but never written.                               */
672 /* This is an unsynchronized getter, so it should be called typically   */
673 /* with the GC lock held to avoid data races on multiprocessors (the    */
674 /* alternative is to use GC_get_heap_usage_safe or GC_get_prof_stats    */
675 /* API calls instead).                                                  */
676 /* This getter remains lock-free (unsynchronized) for compatibility     */
677 /* reason since some existing clients call it from a GC callback        */
678 /* holding the allocator lock.  (This API function and the following    */
679 /* four ones below were made thread-safe in GC v7.2alpha1 and          */
680 /* reverted back in v7.2alpha7 for the reason described.)               */
681 GC_API size_t GC_CALL GC_get_heap_size(void);
682
683 /* Return a lower bound on the number of free bytes in the heap         */
684 /* (excluding the unmapped memory space).  This is an unsynchronized    */
685 /* getter (see GC_get_heap_size comment regarding thread-safety).       */
686 GC_API size_t GC_CALL GC_get_free_bytes(void);
687
688 /* Return the size (in bytes) of the unmapped memory (which is returned */
689 /* to the OS but could be remapped back by the collector later unless   */
690 /* the OS runs out of system/virtual memory). This is an unsynchronized */
691 /* getter (see GC_get_heap_size comment regarding thread-safety).       */
692 GC_API size_t GC_CALL GC_get_unmapped_bytes(void);
693
694 /* Return the number of bytes allocated since the last collection.      */
695 /* This is an unsynchronized getter (see GC_get_heap_size comment       */
696 /* regarding thread-safety).                                            */
697 GC_API size_t GC_CALL GC_get_bytes_since_gc(void);
698
699 /* Return the number of explicitly deallocated bytes of memory since    */
700 /* the recent collection.  This is an unsynchronized getter.            */
701 GC_API size_t GC_CALL GC_get_expl_freed_bytes_since_gc(void);
702
703 /* Return the total number of bytes allocated in this process.          */
704 /* Never decreases, except due to wrapping.  This is an unsynchronized  */
705 /* getter (see GC_get_heap_size comment regarding thread-safety).       */
706 GC_API size_t GC_CALL GC_get_total_bytes(void);
707
708 /* Return the heap usage information.  This is a thread-safe (atomic)   */
709 /* alternative for the five above getters.   (This function acquires    */
710 /* the allocator lock thus preventing data racing and returning the     */
711 /* consistent result.)  Passing NULL pointer is allowed for any         */
712 /* argument.  Returned (filled in) values are of word type.             */
713 /* (This API function was introduced in GC v7.2alpha7 at the same time  */
714 /* when GC_get_heap_size and the friends were made lock-free again.)    */
715 GC_API void GC_CALL GC_get_heap_usage_safe(GC_word * /* pheap_size */,
716                                            GC_word * /* pfree_bytes */,
717                                            GC_word * /* punmapped_bytes */,
718                                            GC_word * /* pbytes_since_gc */,
719                                            GC_word * /* ptotal_bytes */);
720
721 /* Structure used to query GC statistics (profiling information).       */
722 /* More fields could be added in the future.  To preserve compatibility */
723 /* new fields should be added only to the end, and no deprecated fields */
724 /* should be removed from.                                              */
725 struct GC_prof_stats_s {
726   GC_word heapsize_full;
727             /* Heap size in bytes (including the area unmapped to OS).  */
728             /* Same as GC_get_heap_size() + GC_get_unmapped_bytes().    */
729   GC_word free_bytes_full;
730             /* Total bytes contained in free and unmapped blocks.       */
731             /* Same as GC_get_free_bytes() + GC_get_unmapped_bytes().   */
732   GC_word unmapped_bytes;
733             /* Amount of memory unmapped to OS.  Same as the value      */
734             /* returned by GC_get_unmapped_bytes().                     */
735   GC_word bytes_allocd_since_gc;
736             /* Number of bytes allocated since the recent collection.   */
737             /* Same as returned by GC_get_bytes_since_gc().             */
738   GC_word allocd_bytes_before_gc;
739             /* Number of bytes allocated before the recent garbage      */
740             /* collection.  The value may wrap.  Same as the result of  */
741             /* GC_get_total_bytes() - GC_get_bytes_since_gc().          */
742   GC_word non_gc_bytes;
743             /* Number of bytes not considered candidates for garbage    */
744             /* collection.  Same as returned by GC_get_non_gc_bytes().  */
745   GC_word gc_no;
746             /* Garbage collection cycle number.  The value may wrap     */
747             /* (and could be -1).  Same as returned by GC_get_gc_no().  */
748   GC_word markers_m1;
749             /* Number of marker threads (excluding the initiating one). */
750             /* Same as returned by GC_get_parallel (or 0 if the         */
751             /* collector is single-threaded).                           */
752   GC_word bytes_reclaimed_since_gc;
753             /* Approximate number of reclaimed bytes after recent GC.   */
754   GC_word reclaimed_bytes_before_gc;
755             /* Approximate number of bytes reclaimed before the recent  */
756             /* garbage collection.  The value may wrap.                 */
757   GC_word expl_freed_bytes_since_gc;
758             /* Number of bytes freed explicitly since the recent GC.    */
759             /* Same as returned by GC_get_expl_freed_bytes_since_gc().  */
760 };
761
762 /* Atomically get GC statistics (various global counters).  Clients     */
763 /* should pass the size of the buffer (of GC_prof_stats_s type) to fill */
764 /* in the values - this is for interoperability between different GC    */
765 /* versions, an old client could have fewer fields, and vice versa,     */
766 /* client could use newer gc.h (with more entries declared in the       */
767 /* structure) than that of the linked libgc binary; in the latter case, */
768 /* unsupported (unknown) fields are filled in with -1.  Return the size */
769 /* (in bytes) of the filled in part of the structure (excluding all     */
770 /* unknown fields, if any).                                             */
771 GC_API size_t GC_CALL GC_get_prof_stats(struct GC_prof_stats_s *,
772                                         size_t /* stats_sz */);
773 #ifdef GC_THREADS
774   /* Same as above but unsynchronized (i.e., not holding the allocation */
775   /* lock).  Clients should call it using GC_call_with_alloc_lock to    */
776   /* avoid data races on multiprocessors.                               */
777   GC_API size_t GC_CALL GC_get_prof_stats_unsafe(struct GC_prof_stats_s *,
778                                                  size_t /* stats_sz */);
779 #endif
780
781 /* Get the element value (converted to bytes) at a given index of       */
782 /* size_map table which provides requested-to-actual allocation size    */
783 /* mapping.  Assumes the collector is initialized.  Returns -1 if the   */
784 /* index is out of size_map table bounds. Does not use synchronization, */
785 /* thus clients should call it using GC_call_with_alloc_lock typically  */
786 /* to avoid data races on multiprocessors.                              */
787 GC_API size_t GC_CALL GC_get_size_map_at(int i);
788
789 /* Count total memory use in bytes by all allocated blocks.  Acquires   */
790 /* the lock.                                                            */
791 GC_API size_t GC_CALL GC_get_memory_use(void);
792
793 /* Disable garbage collection.  Even GC_gcollect calls will be          */
794 /* ineffective.                                                         */
795 GC_API void GC_CALL GC_disable(void);
796
797 /* Return non-zero (TRUE) if and only if garbage collection is disabled */
798 /* (i.e., GC_dont_gc value is non-zero).  Does not acquire the lock.    */
799 GC_API int GC_CALL GC_is_disabled(void);
800
801 /* Try to re-enable garbage collection.  GC_disable() and GC_enable()   */
802 /* calls nest.  Garbage collection is enabled if the number of calls to */
803 /* both functions is equal.                                             */
804 GC_API void GC_CALL GC_enable(void);
805
806 /* Enable incremental/generational collection.  Not advisable unless    */
807 /* dirty bits are available or most heap objects are pointer-free       */
808 /* (atomic) or immutable.  Don't use in leak finding mode.  Ignored if  */
809 /* GC_dont_gc is non-zero.  Only the generational piece of this is      */
810 /* functional if GC_parallel is non-zero or if GC_time_limit is         */
811 /* GC_TIME_UNLIMITED.  Causes thread-local variant of GC_gcj_malloc()   */
812 /* to revert to locked allocation.  Must be called before any such      */
813 /* GC_gcj_malloc() calls.  For best performance, should be called as    */
814 /* early as possible.  On some platforms, calling it later may have     */
815 /* adverse effects.                                                     */
816 /* Safe to call before GC_INIT().  Includes a  GC_init() call.          */
817 GC_API void GC_CALL GC_enable_incremental(void);
818
819 /* Return non-zero (TRUE) if and only if the incremental mode is on.    */
820 /* Does not acquire the lock.                                           */
821 GC_API int GC_CALL GC_is_incremental_mode(void);
822
823 /* Does incremental mode write-protect pages?  Returns zero or  */
824 /* more of the following, or'ed together:                       */
825 #define GC_PROTECTS_POINTER_HEAP  1 /* May protect non-atomic objects.  */
826 #define GC_PROTECTS_PTRFREE_HEAP  2
827 #define GC_PROTECTS_STATIC_DATA   4 /* Currently never.                 */
828 #define GC_PROTECTS_STACK         8 /* Probably impractical.            */
829
830 #define GC_PROTECTS_NONE 0
831 /* The collector is assumed to be initialized before this call.         */
832 GC_API int GC_CALL GC_incremental_protection_needs(void);
833
834 /* Perform some garbage collection work, if appropriate.        */
835 /* Return 0 if there is no more work to be done (including the  */
836 /* case when garbage collection is not appropriate).            */
837 /* Typically performs an amount of work corresponding roughly   */
838 /* to marking from one page.  May do more work if further       */
839 /* progress requires it, e.g. if incremental collection is      */
840 /* disabled.  It is reasonable to call this in a wait loop      */
841 /* until it returns 0.                                          */
842 GC_API int GC_CALL GC_collect_a_little(void);
843
844 /* Allocate an object of size lb bytes.  The client guarantees that     */
845 /* as long as the object is live, it will be referenced by a pointer    */
846 /* that points to somewhere within the first 256 bytes of the object.   */
847 /* (This should normally be declared volatile to prevent the compiler   */
848 /* from invalidating this assertion.)  This routine is only useful      */
849 /* if a large array is being allocated.  It reduces the chance of       */
850 /* accidentally retaining such an array as a result of scanning an      */
851 /* integer that happens to be an address inside the array.  (Actually,  */
852 /* it reduces the chance of the allocator not finding space for such    */
853 /* an array, since it will try hard to avoid introducing such a false   */
854 /* reference.)  On a SunOS 4.X or MS Windows system this is recommended */
855 /* for arrays likely to be larger than 100K or so.  For other systems,  */
856 /* or if the collector is not configured to recognize all interior      */
857 /* pointers, the threshold is normally much higher.                     */
858 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
859         GC_malloc_ignore_off_page(size_t /* lb */);
860 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
861         GC_malloc_atomic_ignore_off_page(size_t /* lb */);
862
863 #ifdef GC_ADD_CALLER
864 # define GC_EXTRAS GC_RETURN_ADDR, __FILE__, __LINE__
865 # define GC_EXTRA_PARAMS GC_word ra, const char * s, int i
866 #else
867 # define GC_EXTRAS __FILE__, __LINE__
868 # define GC_EXTRA_PARAMS const char * s, int i
869 #endif
870
871 /* The following is only defined if the library has been suitably       */
872 /* compiled:                                                            */
873 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
874         GC_malloc_atomic_uncollectable(size_t /* size_in_bytes */);
875 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
876         GC_debug_malloc_atomic_uncollectable(size_t, GC_EXTRA_PARAMS);
877
878 /* Debugging (annotated) allocation.  GC_gcollect will check            */
879 /* objects allocated in this way for overwrites, etc.                   */
880 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
881         GC_debug_malloc(size_t /* size_in_bytes */, GC_EXTRA_PARAMS);
882 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
883         GC_debug_malloc_atomic(size_t /* size_in_bytes */, GC_EXTRA_PARAMS);
884 GC_API GC_ATTR_MALLOC char * GC_CALL
885         GC_debug_strdup(const char *, GC_EXTRA_PARAMS);
886 GC_API GC_ATTR_MALLOC char * GC_CALL
887         GC_debug_strndup(const char *, size_t, GC_EXTRA_PARAMS)
888                                                         GC_ATTR_NONNULL(1);
889 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
890         GC_debug_malloc_uncollectable(size_t /* size_in_bytes */,
891                                       GC_EXTRA_PARAMS);
892 GC_API GC_ATTR_DEPRECATED void * GC_CALL
893         GC_debug_malloc_stubborn(size_t /* size_in_bytes */, GC_EXTRA_PARAMS);
894 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
895         GC_debug_malloc_ignore_off_page(size_t /* size_in_bytes */,
896                                         GC_EXTRA_PARAMS);
897 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
898         GC_debug_malloc_atomic_ignore_off_page(size_t /* size_in_bytes */,
899                                         GC_EXTRA_PARAMS);
900 GC_API void GC_CALL GC_debug_free(void *);
901 GC_API void * GC_CALL GC_debug_realloc(void * /* old_object */,
902                         size_t /* new_size_in_bytes */, GC_EXTRA_PARAMS)
903                         /* 'realloc' attr */ GC_ATTR_ALLOC_SIZE(2);
904 GC_API
905 #if !defined(CPPCHECK)
906   GC_ATTR_DEPRECATED
907 #endif
908 void GC_CALL GC_debug_change_stubborn(const void *);
909 GC_API void GC_CALL GC_debug_end_stubborn_change(const void *)
910                                                         GC_ATTR_NONNULL(1);
911
912 /* Routines that allocate objects with debug information (like the      */
913 /* above), but just fill in dummy file and line number information.     */
914 /* Thus they can serve as drop-in malloc/realloc replacements.  This    */
915 /* can be useful for two reasons:                                       */
916 /* 1) It allows the collector to be built with DBG_HDRS_ALL defined     */
917 /*    even if some allocation calls come from 3rd party libraries       */
918 /*    that can't be recompiled.                                         */
919 /* 2) On some platforms, the file and line information is redundant,    */
920 /*    since it can be reconstructed from a stack trace.  On such        */
921 /*    platforms it may be more convenient not to recompile, e.g. for    */
922 /*    leak detection.  This can be accomplished by instructing the      */
923 /*    linker to replace malloc/realloc with these.                      */
924 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
925         GC_debug_malloc_replacement(size_t /* size_in_bytes */);
926 GC_API /* 'realloc' attr */ GC_ATTR_ALLOC_SIZE(2) void * GC_CALL
927         GC_debug_realloc_replacement(void * /* object_addr */,
928                                      size_t /* size_in_bytes */);
929
930 #ifdef GC_DEBUG_REPLACEMENT
931 # define GC_MALLOC(sz) GC_debug_malloc_replacement(sz)
932 # define GC_REALLOC(old, sz) GC_debug_realloc_replacement(old, sz)
933 #elif defined(GC_DEBUG)
934 # define GC_MALLOC(sz) GC_debug_malloc(sz, GC_EXTRAS)
935 # define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, GC_EXTRAS)
936 #else
937 # define GC_MALLOC(sz) GC_malloc(sz)
938 # define GC_REALLOC(old, sz) GC_realloc(old, sz)
939 #endif /* !GC_DEBUG_REPLACEMENT && !GC_DEBUG */
940
941 #ifdef GC_DEBUG
942 # define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, GC_EXTRAS)
943 # define GC_STRDUP(s) GC_debug_strdup(s, GC_EXTRAS)
944 # define GC_STRNDUP(s, sz) GC_debug_strndup(s, sz, GC_EXTRAS)
945 # define GC_MALLOC_ATOMIC_UNCOLLECTABLE(sz) \
946                         GC_debug_malloc_atomic_uncollectable(sz, GC_EXTRAS)
947 # define GC_MALLOC_UNCOLLECTABLE(sz) \
948                         GC_debug_malloc_uncollectable(sz, GC_EXTRAS)
949 # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
950                         GC_debug_malloc_ignore_off_page(sz, GC_EXTRAS)
951 # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
952                         GC_debug_malloc_atomic_ignore_off_page(sz, GC_EXTRAS)
953 # define GC_FREE(p) GC_debug_free(p)
954 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
955       GC_debug_register_finalizer(p, f, d, of, od)
956 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
957       GC_debug_register_finalizer_ignore_self(p, f, d, of, od)
958 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
959       GC_debug_register_finalizer_no_order(p, f, d, of, od)
960 # define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \
961       GC_debug_register_finalizer_unreachable(p, f, d, of, od)
962 # define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p)
963 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
964       GC_general_register_disappearing_link(link, \
965                                         GC_base((/* no const */ void *)(obj)))
966 # define GC_REGISTER_LONG_LINK(link, obj) \
967       GC_register_long_link(link, GC_base((/* no const */ void *)(obj)))
968 # define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n)
969 #else
970 # define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz)
971 # define GC_STRDUP(s) GC_strdup(s)
972 # define GC_STRNDUP(s, sz) GC_strndup(s, sz)
973 # define GC_MALLOC_ATOMIC_UNCOLLECTABLE(sz) GC_malloc_atomic_uncollectable(sz)
974 # define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz)
975 # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
976                         GC_malloc_ignore_off_page(sz)
977 # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
978                         GC_malloc_atomic_ignore_off_page(sz)
979 # define GC_FREE(p) GC_free(p)
980 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
981       GC_register_finalizer(p, f, d, of, od)
982 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
983       GC_register_finalizer_ignore_self(p, f, d, of, od)
984 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
985       GC_register_finalizer_no_order(p, f, d, of, od)
986 # define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \
987       GC_register_finalizer_unreachable(p, f, d, of, od)
988 # define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p)
989 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
990       GC_general_register_disappearing_link(link, obj)
991 # define GC_REGISTER_LONG_LINK(link, obj) \
992       GC_register_long_link(link, obj)
993 # define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n)
994 #endif /* !GC_DEBUG */
995
996 /* The following are included because they are often convenient, and    */
997 /* reduce the chance for a misspecified size argument.  But calls may   */
998 /* expand to something syntactically incorrect if t is a complicated    */
999 /* type expression.  Note that, unlike C++ new operator, these ones     */
1000 /* may return NULL (if out of memory).                                  */
1001 #define GC_NEW(t)               ((t*)GC_MALLOC(sizeof(t)))
1002 #define GC_NEW_ATOMIC(t)        ((t*)GC_MALLOC_ATOMIC(sizeof(t)))
1003 #define GC_NEW_UNCOLLECTABLE(t) ((t*)GC_MALLOC_UNCOLLECTABLE(sizeof(t)))
1004
1005 #ifdef GC_REQUIRE_WCSDUP
1006   /* This might be unavailable on some targets (or not needed). */
1007   /* wchar_t should be defined in stddef.h */
1008   GC_API GC_ATTR_MALLOC wchar_t * GC_CALL
1009         GC_wcsdup(const wchar_t *) GC_ATTR_NONNULL(1);
1010   GC_API GC_ATTR_MALLOC wchar_t * GC_CALL
1011         GC_debug_wcsdup(const wchar_t *, GC_EXTRA_PARAMS) GC_ATTR_NONNULL(1);
1012 # ifdef GC_DEBUG
1013 #   define GC_WCSDUP(s) GC_debug_wcsdup(s, GC_EXTRAS)
1014 # else
1015 #   define GC_WCSDUP(s) GC_wcsdup(s)
1016 # endif
1017 #endif /* GC_REQUIRE_WCSDUP */
1018
1019 /* Finalization.  Some of these primitives are grossly unsafe.          */
1020 /* The idea is to make them both cheap, and sufficient to build         */
1021 /* a safer layer, closer to Modula-3, Java, or PCedar finalization.     */
1022 /* The interface represents my conclusions from a long discussion       */
1023 /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes,              */
1024 /* Christian Jacobi, and Russ Atkinson.  It's not perfect, and          */
1025 /* probably nobody else agrees with it.     Hans-J. Boehm  3/13/92      */
1026 typedef void (GC_CALLBACK * GC_finalization_proc)(void * /* obj */,
1027                                                   void * /* client_data */);
1028
1029 GC_API void GC_CALL GC_register_finalizer(void * /* obj */,
1030                         GC_finalization_proc /* fn */, void * /* cd */,
1031                         GC_finalization_proc * /* ofn */, void ** /* ocd */)
1032                                                 GC_ATTR_NONNULL(1);
1033 GC_API void GC_CALL GC_debug_register_finalizer(void * /* obj */,
1034                         GC_finalization_proc /* fn */, void * /* cd */,
1035                         GC_finalization_proc * /* ofn */, void ** /* ocd */)
1036                                                 GC_ATTR_NONNULL(1);
1037         /* When obj is no longer accessible, invoke             */
1038         /* (*fn)(obj, cd).  If a and b are inaccessible, and    */
1039         /* a points to b (after disappearing links have been    */
1040         /* made to disappear), then only a will be              */
1041         /* finalized.  (If this does not create any new         */
1042         /* pointers to b, then b will be finalized after the    */
1043         /* next collection.)  Any finalizable object that       */
1044         /* is reachable from itself by following one or more    */
1045         /* pointers will not be finalized (or collected).       */
1046         /* Thus cycles involving finalizable objects should     */
1047         /* be avoided, or broken by disappearing links.         */
1048         /* All but the last finalizer registered for an object  */
1049         /* is ignored.                                          */
1050         /* No-op in the leak-finding mode.                      */
1051         /* Finalization may be removed by passing 0 as fn.      */
1052         /* Finalizers are implicitly unregistered when they are */
1053         /* enqueued for finalization (i.e. become ready to be   */
1054         /* finalized).                                          */
1055         /* The old finalizer and client data are stored in      */
1056         /* *ofn and *ocd.  (ofn and/or ocd may be NULL.         */
1057         /* The allocation lock is held while *ofn and *ocd are  */
1058         /* updated.  In case of error (no memory to register    */
1059         /* new finalizer), *ofn and *ocd remain unchanged.)     */
1060         /* Fn is never invoked on an accessible object,         */
1061         /* provided hidden pointers are converted to real       */
1062         /* pointers only if the allocation lock is held, and    */
1063         /* such conversions are not performed by finalization   */
1064         /* routines.                                            */
1065         /* If GC_register_finalizer is aborted as a result of   */
1066         /* a signal, the object may be left with no             */
1067         /* finalization, even if neither the old nor new        */
1068         /* finalizer were NULL.                                 */
1069         /* Obj should be the starting address of an object      */
1070         /* allocated by GC_malloc or friends. Obj may also be   */
1071         /* NULL or point to something outside GC heap (in this  */
1072         /* case, fn is ignored, *ofn and *ocd are set to NULL). */
1073         /* Note that any garbage collectible object referenced  */
1074         /* by cd will be considered accessible until the        */
1075         /* finalizer is invoked.                                */
1076
1077 /* Another versions of the above follow.  It ignores            */
1078 /* self-cycles, i.e. pointers from a finalizable object to      */
1079 /* itself.  There is a stylistic argument that this is wrong,   */
1080 /* but it's unavoidable for C++, since the compiler may         */
1081 /* silently introduce these.  It's also benign in that specific */
1082 /* case.  And it helps if finalizable objects are split to      */
1083 /* avoid cycles.                                                */
1084 /* Note that cd will still be viewed as accessible, even if it  */
1085 /* refers to the object itself.                                 */
1086 GC_API void GC_CALL GC_register_finalizer_ignore_self(void * /* obj */,
1087                         GC_finalization_proc /* fn */, void * /* cd */,
1088                         GC_finalization_proc * /* ofn */, void ** /* ocd */)
1089                                                 GC_ATTR_NONNULL(1);
1090 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self(void * /* obj */,
1091                         GC_finalization_proc /* fn */, void * /* cd */,
1092                         GC_finalization_proc * /* ofn */, void ** /* ocd */)
1093                                                 GC_ATTR_NONNULL(1);
1094
1095 /* Another version of the above.  It ignores all cycles.        */
1096 /* It should probably only be used by Java implementations.     */
1097 /* Note that cd will still be viewed as accessible, even if it  */
1098 /* refers to the object itself.                                 */
1099 GC_API void GC_CALL GC_register_finalizer_no_order(void * /* obj */,
1100                         GC_finalization_proc /* fn */, void * /* cd */,
1101                         GC_finalization_proc * /* ofn */, void ** /* ocd */)
1102                                                 GC_ATTR_NONNULL(1);
1103 GC_API void GC_CALL GC_debug_register_finalizer_no_order(void * /* obj */,
1104                         GC_finalization_proc /* fn */, void * /* cd */,
1105                         GC_finalization_proc * /* ofn */, void ** /* ocd */)
1106                                                 GC_ATTR_NONNULL(1);
1107
1108 /* This is a special finalizer that is useful when an object's  */
1109 /* finalizer must be run when the object is known to be no      */
1110 /* longer reachable, not even from other finalizable objects.   */
1111 /* It behaves like "normal" finalization, except that the       */
1112 /* finalizer is not run while the object is reachable from      */
1113 /* other objects specifying unordered finalization.             */
1114 /* Effectively it allows an object referenced, possibly         */
1115 /* indirectly, from an unordered finalizable object to override */
1116 /* the unordered finalization request.                          */
1117 /* This can be used in combination with finalizer_no_order so   */
1118 /* as to release resources that must not be released while an   */
1119 /* object can still be brought back to life by other            */
1120 /* finalizers.                                                  */
1121 /* Only works if GC_java_finalization is set.  Probably only    */
1122 /* of interest when implementing a language that requires       */
1123 /* unordered finalization (e.g. Java, C#).                      */
1124 GC_API void GC_CALL GC_register_finalizer_unreachable(void * /* obj */,
1125                         GC_finalization_proc /* fn */, void * /* cd */,
1126                         GC_finalization_proc * /* ofn */, void ** /* ocd */)
1127                                                 GC_ATTR_NONNULL(1);
1128 GC_API void GC_CALL GC_debug_register_finalizer_unreachable(void * /* obj */,
1129                         GC_finalization_proc /* fn */, void * /* cd */,
1130                         GC_finalization_proc * /* ofn */, void ** /* ocd */)
1131                                                 GC_ATTR_NONNULL(1);
1132
1133 #define GC_NO_MEMORY 2  /* Failure due to lack of memory.       */
1134
1135 /* The following routine may be used to break cycles between    */
1136 /* finalizable objects, thus causing cyclic finalizable         */
1137 /* objects to be finalized in the correct order.  Standard      */
1138 /* use involves calling GC_register_disappearing_link(&p),      */
1139 /* where p is a pointer that is not followed by finalization    */
1140 /* code, and should not be considered in determining            */
1141 /* finalization order.                                          */
1142 GC_API int GC_CALL GC_register_disappearing_link(void ** /* link */)
1143                                                 GC_ATTR_NONNULL(1);
1144         /* Link should point to a field of a heap allocated     */
1145         /* object obj.  *link will be cleared when obj is       */
1146         /* found to be inaccessible.  This happens BEFORE any   */
1147         /* finalization code is invoked, and BEFORE any         */
1148         /* decisions about finalization order are made.         */
1149         /* This is useful in telling the finalizer that         */
1150         /* some pointers are not essential for proper           */
1151         /* finalization.  This may avoid finalization cycles.   */
1152         /* Note that obj may be resurrected by another          */
1153         /* finalizer, and thus the clearing of *link may        */
1154         /* be visible to non-finalization code.                 */
1155         /* There's an argument that an arbitrary action should  */
1156         /* be allowed here, instead of just clearing a pointer. */
1157         /* But this causes problems if that action alters, or   */
1158         /* examines connectivity.  Returns GC_DUPLICATE if link */
1159         /* was already registered, GC_SUCCESS if registration   */
1160         /* succeeded, GC_NO_MEMORY if it failed for lack of     */
1161         /* memory, and GC_oom_fn did not handle the problem.    */
1162         /* Only exists for backward compatibility.  See below:  */
1163
1164 GC_API int GC_CALL GC_general_register_disappearing_link(void ** /* link */,
1165                                                     const void * /* obj */)
1166                         GC_ATTR_NONNULL(1) GC_ATTR_NONNULL(2);
1167         /* A slight generalization of the above. *link is       */
1168         /* cleared when obj first becomes inaccessible.  This   */
1169         /* can be used to implement weak pointers easily and    */
1170         /* safely. Typically link will point to a location      */
1171         /* holding a disguised pointer to obj.  (A pointer      */
1172         /* inside an "atomic" object is effectively disguised.) */
1173         /* In this way, weak pointers are broken before any     */
1174         /* object reachable from them gets finalized.           */
1175         /* Each link may be registered only with one obj value, */
1176         /* i.e. all objects but the last one (link registered   */
1177         /* with) are ignored.  This was added after a long      */
1178         /* email discussion with John Ellis.                    */
1179         /* link must be non-NULL (and be properly aligned).     */
1180         /* obj must be a pointer to the first word of an object */
1181         /* allocated by GC_malloc or friends.   A link          */
1182         /* disappears when it is unregistered manually, or when */
1183         /* (*link) is cleared, or when the object containing    */
1184         /* this link is garbage collected.  It is unsafe to     */
1185         /* explicitly deallocate the object containing link.    */
1186         /* Explicit deallocation of obj may or may not cause    */
1187         /* link to eventually be cleared.                       */
1188         /* No-op in the leak-finding mode.                      */
1189         /* This function can be used to implement certain types */
1190         /* of weak pointers.  Note, however, this generally     */
1191         /* requires that the allocation lock is held (see       */
1192         /* GC_call_with_alloc_lock() below) when the disguised  */
1193         /* pointer is accessed.  Otherwise a strong pointer     */
1194         /* could be recreated between the time the collector    */
1195         /* decides to reclaim the object and the link is        */
1196         /* cleared.  Returns GC_SUCCESS if registration         */
1197         /* succeeded (a new link is registered), GC_DUPLICATE   */
1198         /* if link was already registered (with some object),   */
1199         /* GC_NO_MEMORY if registration failed for lack of      */
1200         /* memory (and GC_oom_fn did not handle the problem),   */
1201         /* GC_UNIMPLEMENTED if GC_find_leak is true.            */
1202
1203 GC_API int GC_CALL GC_move_disappearing_link(void ** /* link */,
1204                                              void ** /* new_link */)
1205                         GC_ATTR_NONNULL(2);
1206         /* Moves a link previously registered via               */
1207         /* GC_general_register_disappearing_link (or            */
1208         /* GC_register_disappearing_link).  Does not change the */
1209         /* target object of the weak reference.  Does not       */
1210         /* change (*new_link) content.  May be called with      */
1211         /* new_link equal to link (to check whether link has    */
1212         /* been registered).  Returns GC_SUCCESS on success,    */
1213         /* GC_DUPLICATE if there is already another             */
1214         /* disappearing link at the new location (never         */
1215         /* returned if new_link is equal to link), GC_NOT_FOUND */
1216         /* if no link is registered at the original location.   */
1217
1218 GC_API int GC_CALL GC_unregister_disappearing_link(void ** /* link */);
1219         /* Undoes a registration by either of the above two     */
1220         /* routines.  Returns 0 if link was not actually        */
1221         /* registered (otherwise returns 1).                    */
1222
1223 GC_API int GC_CALL GC_register_long_link(void ** /* link */,
1224                                     const void * /* obj */)
1225                         GC_ATTR_NONNULL(1) GC_ATTR_NONNULL(2);
1226         /* Similar to GC_general_register_disappearing_link but */
1227         /* *link only gets cleared when obj becomes truly       */
1228         /* inaccessible.  An object becomes truly inaccessible  */
1229         /* when it can no longer be resurrected from its        */
1230         /* finalizer (e.g. by assigning itself to a pointer     */
1231         /* traceable from root).  This can be used to implement */
1232         /* long weak pointers easily and safely.                */
1233
1234 GC_API int GC_CALL GC_move_long_link(void ** /* link */,
1235                                      void ** /* new_link */)
1236                         GC_ATTR_NONNULL(2);
1237         /* Similar to GC_move_disappearing_link but for a link  */
1238         /* previously registered via GC_register_long_link.     */
1239
1240 GC_API int GC_CALL GC_unregister_long_link(void ** /* link */);
1241         /* Similar to GC_unregister_disappearing_link but for a */
1242         /* registration by either of the above two routines.    */
1243
1244 /* Support of toggle-ref style of external memory management    */
1245 /* without hooking up to the host retain/release machinery.     */
1246 /* The idea of toggle-ref is that an external reference to      */
1247 /* an object is kept and it can be either a strong or weak      */
1248 /* reference; a weak reference is used when the external peer   */
1249 /* has no interest in the object, and a strong otherwise.       */
1250 typedef enum {
1251    GC_TOGGLE_REF_DROP,
1252    GC_TOGGLE_REF_STRONG,
1253    GC_TOGGLE_REF_WEAK
1254 } GC_ToggleRefStatus;
1255
1256 /* The callback is to decide (return) the new state of a given  */
1257 /* object.  Invoked by the collector for all objects registered */
1258 /* for toggle-ref processing.  Invoked with the allocation lock */
1259 /* held (but the "world" is running).                           */
1260 typedef GC_ToggleRefStatus (GC_CALLBACK *GC_toggleref_func)(void * /* obj */);
1261
1262 /* Set (register) a callback that decides the state of a given  */
1263 /* object (by, probably, inspecting its native state).          */
1264 /* The argument may be 0 (means no callback).  Both the setter  */
1265 /* and the getter acquire the allocation lock (to avoid data    */
1266 /* races).                                                      */
1267 GC_API void GC_CALL GC_set_toggleref_func(GC_toggleref_func);
1268 GC_API GC_toggleref_func GC_CALL GC_get_toggleref_func(void);
1269
1270 /* Register a given object for toggle-ref processing.  It will  */
1271 /* be stored internally and the toggle-ref callback will be     */
1272 /* invoked on the object until the callback returns             */
1273 /* GC_TOGGLE_REF_DROP or the object is collected.  If is_strong */
1274 /* is true then the object is registered with a strong ref,     */
1275 /* a weak one otherwise.  Returns GC_SUCCESS if registration    */
1276 /* succeeded (or no callback registered yet), GC_NO_MEMORY if   */
1277 /* it failed for lack of memory.                                */
1278 GC_API int GC_CALL GC_toggleref_add(void * /* obj */, int /* is_strong */)
1279                                                 GC_ATTR_NONNULL(1);
1280
1281 /* Finalizer callback support.  Invoked by the collector (with  */
1282 /* the allocation lock held) for each unreachable object        */
1283 /* enqueued for finalization.                                   */
1284 typedef void (GC_CALLBACK * GC_await_finalize_proc)(void * /* obj */);
1285 GC_API void GC_CALL GC_set_await_finalize_proc(GC_await_finalize_proc);
1286 GC_API GC_await_finalize_proc GC_CALL GC_get_await_finalize_proc(void);
1287                         /* Zero means no callback.  The setter  */
1288                         /* and getter acquire the lock too.     */
1289
1290 /* Returns !=0 if GC_invoke_finalizers has something to do.     */
1291 /* Does not use any synchronization.                            */
1292 GC_API int GC_CALL GC_should_invoke_finalizers(void);
1293
1294 GC_API int GC_CALL GC_invoke_finalizers(void);
1295         /* Run finalizers for all objects that are ready to     */
1296         /* be finalized.  Return the number of finalizers       */
1297         /* that were run.  Normally this is also called         */
1298         /* implicitly during some allocations.  If              */
1299         /* GC_finalize_on_demand is nonzero, it must be called  */
1300         /* explicitly.                                          */
1301
1302 /* Explicitly tell the collector that an object is reachable    */
1303 /* at a particular program point.  This prevents the argument   */
1304 /* pointer from being optimized away, even it is otherwise no   */
1305 /* longer needed.  It should have no visible effect in the      */
1306 /* absence of finalizers or disappearing links.  But it may be  */
1307 /* needed to prevent finalizers from running while the          */
1308 /* associated external resource is still in use.                */
1309 /* The function is sometimes called keep_alive in other         */
1310 /* settings.                                                    */
1311 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
1312 # define GC_reachable_here(ptr) \
1313                 __asm__ __volatile__(" " : : "X"(ptr) : "memory")
1314 #else
1315   GC_API void GC_CALL GC_noop1(GC_word);
1316 # define GC_reachable_here(ptr) GC_noop1((GC_word)(ptr))
1317 #endif
1318
1319 /* GC_set_warn_proc can be used to redirect or filter warning messages. */
1320 /* p may not be a NULL pointer.  msg is printf format string (arg must  */
1321 /* match the format).  Both the setter and the getter acquire the GC    */
1322 /* lock (to avoid data races).  In version 7.1 (and before), the setter */
1323 /* returned the old warn_proc value.                                    */
1324 typedef void (GC_CALLBACK * GC_warn_proc)(char * /* msg */,
1325                                           GC_word /* arg */);
1326 GC_API void GC_CALL GC_set_warn_proc(GC_warn_proc /* p */) GC_ATTR_NONNULL(1);
1327 /* GC_get_warn_proc returns the current warn_proc.                      */
1328 GC_API GC_warn_proc GC_CALL GC_get_warn_proc(void);
1329
1330 /* GC_ignore_warn_proc may be used as an argument for GC_set_warn_proc  */
1331 /* to suppress all warnings (unless statistics printing is turned on).  */
1332 GC_API void GC_CALLBACK GC_ignore_warn_proc(char *, GC_word);
1333
1334 /* Change file descriptor of GC log.  Unavailable on some targets.      */
1335 GC_API void GC_CALL GC_set_log_fd(int);
1336
1337 /* abort_func is invoked on GC fatal aborts (just before OS-dependent   */
1338 /* abort or exit(1) is called).  Must be non-NULL.  The default one     */
1339 /* outputs msg to stderr provided msg is non-NULL.  msg is NULL if      */
1340 /* invoked before exit(1) otherwise msg is non-NULL (i.e., if invoked   */
1341 /* before abort).  Both the setter and getter acquire the GC lock.      */
1342 /* Both the setter and getter are defined only if the library has been  */
1343 /* compiled without SMALL_CONFIG.                                       */
1344 typedef void (GC_CALLBACK * GC_abort_func)(const char * /* msg */);
1345 GC_API void GC_CALL GC_set_abort_func(GC_abort_func) GC_ATTR_NONNULL(1);
1346 GC_API GC_abort_func GC_CALL GC_get_abort_func(void);
1347
1348 /* A portable way to abort the application because of not enough memory.*/
1349 GC_API void GC_CALL GC_abort_on_oom(void);
1350
1351 /* The following is intended to be used by a higher level       */
1352 /* (e.g. Java-like) finalization facility.  It is expected      */
1353 /* that finalization code will arrange for hidden pointers to   */
1354 /* disappear.  Otherwise objects can be accessed after they     */
1355 /* have been collected.                                         */
1356 /* Note that putting pointers in atomic objects or in           */
1357 /* non-pointer slots of "typed" objects is equivalent to        */
1358 /* disguising them in this way, and may have other advantages.  */
1359 typedef GC_word GC_hidden_pointer;
1360 #define GC_HIDE_POINTER(p) (~(GC_hidden_pointer)(p))
1361 /* Converting a hidden pointer to a real pointer requires verifying     */
1362 /* that the object still exists.  This involves acquiring the           */
1363 /* allocator lock to avoid a race with the collector.                   */
1364 #define GC_REVEAL_POINTER(p) ((void *)GC_HIDE_POINTER(p))
1365
1366 #if defined(I_HIDE_POINTERS) || defined(GC_I_HIDE_POINTERS)
1367   /* This exists only for compatibility (the GC-prefixed symbols are    */
1368   /* preferred for new code).                                           */
1369 # define HIDE_POINTER(p) GC_HIDE_POINTER(p)
1370 # define REVEAL_POINTER(p) GC_REVEAL_POINTER(p)
1371 #endif
1372
1373 typedef void * (GC_CALLBACK * GC_fn_type)(void * /* client_data */);
1374 GC_API void * GC_CALL GC_call_with_alloc_lock(GC_fn_type /* fn */,
1375                                 void * /* client_data */) GC_ATTR_NONNULL(1);
1376
1377 /* These routines are intended to explicitly notify the collector       */
1378 /* of new threads.  Often this is unnecessary because thread creation   */
1379 /* is implicitly intercepted by the collector, using header-file        */
1380 /* defines, or linker-based interception.  In the long run the intent   */
1381 /* is to always make redundant registration safe.  In the short run,    */
1382 /* this is being implemented a platform at a time.                      */
1383 /* The interface is complicated by the fact that we probably will not   */
1384 /* ever be able to automatically determine the stack base for thread    */
1385 /* stacks on all platforms.                                             */
1386
1387 /* Structure representing the base of a thread stack.  On most          */
1388 /* platforms this contains just a single address.                       */
1389 struct GC_stack_base {
1390   void * mem_base; /* Base of memory stack. */
1391 # if defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
1392     void * reg_base; /* Base of separate register stack. */
1393 # endif
1394 };
1395
1396 typedef void * (GC_CALLBACK * GC_stack_base_func)(
1397                 struct GC_stack_base * /* sb */, void * /* arg */);
1398
1399 /* Call a function with a stack base structure corresponding to         */
1400 /* somewhere in the GC_call_with_stack_base frame.  This often can      */
1401 /* be used to provide a sufficiently accurate stack base.  And we       */
1402 /* implement it everywhere.                                             */
1403 GC_API void * GC_CALL GC_call_with_stack_base(GC_stack_base_func /* fn */,
1404                                         void * /* arg */) GC_ATTR_NONNULL(1);
1405
1406 #define GC_SUCCESS 0
1407 #define GC_DUPLICATE 1          /* Was already registered.              */
1408 #define GC_NO_THREADS 2         /* No thread support in GC.             */
1409         /* GC_NO_THREADS is not returned by any GC function anymore.    */
1410 #define GC_UNIMPLEMENTED 3 /* Not yet implemented on this platform.     */
1411 #define GC_NOT_FOUND 4          /* Requested link not found (returned   */
1412                                 /* by GC_move_disappearing_link).       */
1413
1414 #if defined(GC_DARWIN_THREADS) || defined(GC_WIN32_THREADS)
1415   /* Use implicit thread registration and processing (via Win32 DllMain */
1416   /* or Darwin task_threads).  Deprecated.  Must be called before       */
1417   /* GC_INIT() and other GC routines.  Should be avoided if             */
1418   /* GC_pthread_create, GC_beginthreadex (or GC_CreateThread) could be  */
1419   /* called instead.  Disables parallelized GC on Win32.                */
1420   GC_API void GC_CALL GC_use_threads_discovery(void);
1421 #endif
1422
1423 #ifdef GC_THREADS
1424   /* Suggest the GC to use the specific signal to suspend threads.      */
1425   /* Has no effect after GC_init and on non-POSIX systems.              */
1426   GC_API void GC_CALL GC_set_suspend_signal(int);
1427
1428   /* Suggest the GC to use the specific signal to resume threads.       */
1429   /* Has no effect after GC_init and on non-POSIX systems.              */
1430   GC_API void GC_CALL GC_set_thr_restart_signal(int);
1431
1432   /* Return the signal number (constant after initialization) used by   */
1433   /* the GC to suspend threads on POSIX systems.  Return -1 otherwise.  */
1434   GC_API int GC_CALL GC_get_suspend_signal(void);
1435
1436   /* Return the signal number (constant after initialization) used by   */
1437   /* the garbage collector to restart (resume) threads on POSIX         */
1438   /* systems.  Return -1 otherwise.                                     */
1439   GC_API int GC_CALL GC_get_thr_restart_signal(void);
1440
1441   /* Restart marker threads after POSIX fork in child.  Meaningless in  */
1442   /* other situations.  Should not be called if fork followed by exec.  */
1443   GC_API void GC_CALL GC_start_mark_threads(void);
1444
1445   /* Explicitly enable GC_register_my_thread() invocation.              */
1446   /* Done implicitly if a GC thread-creation function is called (or     */
1447   /* implicit thread registration is activated, or the collector is     */
1448   /* compiled with GC_ALWAYS_MULTITHREADED defined).  Otherwise, it     */
1449   /* must be called from the main (or any previously registered) thread */
1450   /* between the collector initialization and the first explicit        */
1451   /* registering of a thread (it should be called as late as possible). */
1452   GC_API void GC_CALL GC_allow_register_threads(void);
1453
1454   /* Register the current thread, with the indicated stack base, as     */
1455   /* a new thread whose stack(s) should be traced by the GC.  If it     */
1456   /* is not implicitly called by the GC, this must be called before a   */
1457   /* thread can allocate garbage collected memory, or assign pointers   */
1458   /* to the garbage collected heap.  Once registered, a thread will be  */
1459   /* stopped during garbage collections.                                */
1460   /* This call must be previously enabled (see above).                  */
1461   /* This should never be called from the main thread, where it is      */
1462   /* always done implicitly.  This is normally done implicitly if GC_   */
1463   /* functions are called to create the thread, e.g. by including gc.h  */
1464   /* (which redefines some system functions) before calling the system  */
1465   /* thread creation function.  Nonetheless, thread cleanup routines    */
1466   /* (e.g., pthread key destructor) typically require manual thread     */
1467   /* registering (and unregistering) if pointers to GC-allocated        */
1468   /* objects are manipulated inside.                                    */
1469   /* It is also always done implicitly on some platforms if             */
1470   /* GC_use_threads_discovery() is called at start-up.  Except for the  */
1471   /* latter case, the explicit call is normally required for threads    */
1472   /* created by third-party libraries.                                  */
1473   /* A manually registered thread requires manual unregistering.        */
1474   /* Returns GC_SUCCESS on success, GC_DUPLICATE if already registered. */
1475   GC_API int GC_CALL GC_register_my_thread(const struct GC_stack_base *)
1476                                                         GC_ATTR_NONNULL(1);
1477
1478   /* Return non-zero (TRUE) if and only if the calling thread is        */
1479   /* registered with the garbage collector.                             */
1480   GC_API int GC_CALL GC_thread_is_registered(void);
1481
1482   /* Notify the collector about the stack and the alt-stack of the      */
1483   /* current thread.  stack_start/size is used to determine the stack   */
1484   /* boundaries when a thread is suspended while it is on an alt-stack. */
1485   GC_API void GC_CALL GC_register_altstack(void * /* stack_start */,
1486                                            GC_word /* stack_size */,
1487                                            void * /* altstack_base */,
1488                                            GC_word /* altstack_size */);
1489
1490   /* Unregister the current thread.  Only an explicitly registered      */
1491   /* thread (i.e. for which GC_register_my_thread() returns GC_SUCCESS) */
1492   /* is allowed (and required) to call this function.  (As a special    */
1493   /* exception, it is also allowed to once unregister the main thread.) */
1494   /* The thread may no longer allocate garbage collected memory or      */
1495   /* manipulate pointers to the garbage collected heap after making     */
1496   /* this call.  Specifically, if it wants to return or otherwise       */
1497   /* communicate a pointer to the garbage-collected heap to another     */
1498   /* thread, it must do this before calling GC_unregister_my_thread,    */
1499   /* most probably by saving it in a global data structure.  Must not   */
1500   /* be called inside a GC callback function (except for                */
1501   /* GC_call_with_stack_base() one).                                    */
1502   GC_API int GC_CALL GC_unregister_my_thread(void);
1503 #endif /* GC_THREADS */
1504
1505 /* Wrapper for functions that are likely to block (or, at least, do not */
1506 /* allocate garbage collected memory and/or manipulate pointers to the  */
1507 /* garbage collected heap) for an appreciable length of time.  While fn */
1508 /* is running, the collector is said to be in the "inactive" state for  */
1509 /* the current thread (this means that the thread is not suspended and  */
1510 /* the thread's stack frames "belonging" to the functions in the        */
1511 /* "inactive" state are not scanned during garbage collections).  It is */
1512 /* allowed for fn to call GC_call_with_gc_active() (even recursively),  */
1513 /* thus temporarily toggling the collector's state back to "active".    */
1514 GC_API void * GC_CALL GC_do_blocking(GC_fn_type /* fn */,
1515                                 void * /* client_data */) GC_ATTR_NONNULL(1);
1516
1517 /* Call a function switching to the "active" state of the collector for */
1518 /* the current thread (i.e. the user function is allowed to call any    */
1519 /* GC function and/or manipulate pointers to the garbage collected      */
1520 /* heap).  GC_call_with_gc_active() has the functionality opposite to   */
1521 /* GC_do_blocking() one.  It is assumed that the collector is already   */
1522 /* initialized and the current thread is registered.  fn may toggle     */
1523 /* the collector thread's state temporarily to "inactive" one by using  */
1524 /* GC_do_blocking.  GC_call_with_gc_active() often can be used to       */
1525 /* provide a sufficiently accurate stack base.                          */
1526 GC_API void * GC_CALL GC_call_with_gc_active(GC_fn_type /* fn */,
1527                                 void * /* client_data */) GC_ATTR_NONNULL(1);
1528
1529 /* Attempt to fill in the GC_stack_base structure with the stack base   */
1530 /* for this thread.  This appears to be required to implement anything  */
1531 /* like the JNI AttachCurrentThread in an environment in which new      */
1532 /* threads are not automatically registered with the collector.         */
1533 /* It is also unfortunately hard to implement well on many platforms.   */
1534 /* Returns GC_SUCCESS or GC_UNIMPLEMENTED.  This function acquires the  */
1535 /* GC lock on some platforms.                                           */
1536 GC_API int GC_CALL GC_get_stack_base(struct GC_stack_base *)
1537                                                         GC_ATTR_NONNULL(1);
1538
1539 /* The following routines are primarily intended for use with a         */
1540 /* preprocessor which inserts calls to check C pointer arithmetic.      */
1541 /* They indicate failure by invoking the corresponding _print_proc.     */
1542
1543 /* Check that p and q point to the same object.                 */
1544 /* Fail conspicuously if they don't.                            */
1545 /* Returns the first argument.                                  */
1546 /* Succeeds if neither p nor q points to the heap.              */
1547 /* May succeed if both p and q point to between heap objects.   */
1548 GC_API void * GC_CALL GC_same_obj(void * /* p */, void * /* q */);
1549
1550 /* Checked pointer pre- and post- increment operations.  Note that      */
1551 /* the second argument is in units of bytes, not multiples of the       */
1552 /* object size.  This should either be invoked from a macro, or the     */
1553 /* call should be automatically generated.                              */
1554 GC_API void * GC_CALL GC_pre_incr(void **, ptrdiff_t /* how_much */)
1555                                                         GC_ATTR_NONNULL(1);
1556 GC_API void * GC_CALL GC_post_incr(void **, ptrdiff_t /* how_much */)
1557                                                         GC_ATTR_NONNULL(1);
1558
1559 /* Check that p is visible                                              */
1560 /* to the collector as a possibly pointer containing location.          */
1561 /* If it isn't fail conspicuously.                                      */
1562 /* Returns the argument in all cases.  May erroneously succeed          */
1563 /* in hard cases.  (This is intended for debugging use with             */
1564 /* untyped allocations.  The idea is that it should be possible, though */
1565 /* slow, to add such a call to all indirect pointer stores.)            */
1566 /* Currently useless for multi-threaded worlds.                         */
1567 GC_API void * GC_CALL GC_is_visible(void * /* p */);
1568
1569 /* Check that if p is a pointer to a heap page, then it points to       */
1570 /* a valid displacement within a heap object.                           */
1571 /* Fail conspicuously if this property does not hold.                   */
1572 /* Uninteresting with GC_all_interior_pointers.                         */
1573 /* Always returns its argument.                                         */
1574 GC_API void * GC_CALL GC_is_valid_displacement(void * /* p */);
1575
1576 /* Explicitly dump the GC state.  This is most often called from the    */
1577 /* debugger, or by setting the GC_DUMP_REGULARLY environment variable,  */
1578 /* but it may be useful to call it from client code during debugging.   */
1579 /* The current collection number is printed in the header of the dump.  */
1580 /* Acquires the GC lock to avoid data races.                            */
1581 /* Defined only if the library has been compiled without NO_DEBUGGING.  */
1582 GC_API void GC_CALL GC_dump(void);
1583
1584 /* The same as GC_dump but allows to specify the name of dump and does  */
1585 /* not acquire the lock.  If name is non-NULL, it is printed to help    */
1586 /* identifying individual dumps.  Otherwise the current collection      */
1587 /* number is used as the name.                                          */
1588 /* Defined only if the library has been compiled without NO_DEBUGGING.  */
1589 GC_API void GC_CALL GC_dump_named(const char * /* name */);
1590
1591 /* Dump information about each block of every GC memory section.        */
1592 /* Defined only if the library has been compiled without NO_DEBUGGING.  */
1593 GC_API void GC_CALL GC_dump_regions(void);
1594
1595 /* Dump information about every registered disappearing link and        */
1596 /* finalizable object.                                                  */
1597 /* Defined only if the library has been compiled without NO_DEBUGGING.  */
1598 GC_API void GC_CALL GC_dump_finalization(void);
1599
1600 /* Safer, but slow, pointer addition.  Probably useful mainly with      */
1601 /* a preprocessor.  Useful only for heap pointers.                      */
1602 /* Only the macros without trailing digits are meant to be used         */
1603 /* by clients.  These are designed to model the available C pointer     */
1604 /* arithmetic expressions.                                              */
1605 /* Even then, these are probably more useful as                         */
1606 /* documentation than as part of the API.                               */
1607 /* Note that GC_PTR_ADD evaluates the first argument more than once.    */
1608 #if defined(GC_DEBUG) && defined(__GNUC__)
1609 # define GC_PTR_ADD3(x, n, type_of_result) \
1610         ((type_of_result)GC_same_obj((x)+(n), (x)))
1611 # define GC_PRE_INCR3(x, n, type_of_result) \
1612         ((type_of_result)GC_pre_incr((void **)(&(x)), (n)*sizeof(*x)))
1613 # define GC_POST_INCR3(x, n, type_of_result) \
1614         ((type_of_result)GC_post_incr((void **)(&(x)), (n)*sizeof(*x)))
1615 # define GC_PTR_ADD(x, n) GC_PTR_ADD3(x, n, __typeof__(x))
1616 # define GC_PRE_INCR(x, n) GC_PRE_INCR3(x, n, __typeof__(x))
1617 # define GC_POST_INCR(x) GC_POST_INCR3(x, 1, __typeof__(x))
1618 # define GC_POST_DECR(x) GC_POST_INCR3(x, -1, __typeof__(x))
1619 #else /* !GC_DEBUG || !__GNUC__ */
1620   /* We can't do this right without typeof, which ANSI decided was not    */
1621   /* sufficiently useful.  Without it we resort to the non-debug version. */
1622   /* FIXME: This should eventually support C++0x decltype.                */
1623 # define GC_PTR_ADD(x, n) ((x)+(n))
1624 # define GC_PRE_INCR(x, n) ((x) += (n))
1625 # define GC_POST_INCR(x) ((x)++)
1626 # define GC_POST_DECR(x) ((x)--)
1627 #endif /* !GC_DEBUG || !__GNUC__ */
1628
1629 /* Safer assignment of a pointer to a non-stack location.       */
1630 #ifdef GC_DEBUG
1631 # define GC_PTR_STORE(p, q) \
1632         (*(void **)GC_is_visible((void *)(p)) = \
1633                     GC_is_valid_displacement((void *)(q)))
1634 #else
1635 # define GC_PTR_STORE(p, q) (*(void **)(p) = (void *)(q))
1636 #endif
1637
1638 /* Functions called to report pointer checking errors */
1639 GC_API void (GC_CALLBACK * GC_same_obj_print_proc)(void * /* p */,
1640                                                    void * /* q */);
1641 GC_API void (GC_CALLBACK * GC_is_valid_displacement_print_proc)(void *);
1642 GC_API void (GC_CALLBACK * GC_is_visible_print_proc)(void *);
1643
1644 #ifdef GC_PTHREADS
1645   /* For pthread support, we generally need to intercept a number of    */
1646   /* thread library calls.  We do that here by macro defining them.     */
1647 # ifdef __cplusplus
1648     } /* extern "C" */
1649 # endif
1650 # include "gc_pthread_redirects.h"
1651 # ifdef __cplusplus
1652     extern "C" {
1653 # endif
1654 #endif
1655
1656 /* This returns a list of objects, linked through their first word.     */
1657 /* Its use can greatly reduce lock contention problems, since the       */
1658 /* allocation lock can be acquired and released many fewer times.       */
1659 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_many(size_t /* lb */);
1660 #define GC_NEXT(p) (*(void * *)(p))     /* Retrieve the next element    */
1661                                         /* in returned list.            */
1662
1663 /* A filter function to control the scanning of dynamic libraries.      */
1664 /* If implemented, called by GC before registering a dynamic library    */
1665 /* (discovered by GC) section as a static data root (called only as     */
1666 /* a last reason not to register).  The filename of the library, the    */
1667 /* address and the length of the memory region (section) are passed.    */
1668 /* This routine should return nonzero if that region should be scanned. */
1669 /* Always called with the allocation lock held.  Depending on the       */
1670 /* platform, might be called with the "world" stopped.                  */
1671 typedef int (GC_CALLBACK * GC_has_static_roots_func)(
1672                                         const char * /* dlpi_name */,
1673                                         void * /* section_start */,
1674                                         size_t /* section_size */);
1675
1676 /* Register a new callback (a user-supplied filter) to control the      */
1677 /* scanning of dynamic libraries.  Replaces any previously registered   */
1678 /* callback.  May be 0 (means no filtering).  May be unused on some     */
1679 /* platforms (if the filtering is unimplemented or inappropriate).      */
1680 GC_API void GC_CALL GC_register_has_static_roots_callback(
1681                                         GC_has_static_roots_func);
1682
1683 #if !defined(CPPCHECK) && !defined(GC_WINDOWS_H_INCLUDED) && defined(WINAPI)
1684   /* windows.h is included before gc.h */
1685 # define GC_WINDOWS_H_INCLUDED
1686 #endif
1687
1688 #if defined(GC_WIN32_THREADS) \
1689     && (!defined(GC_PTHREADS) || defined(GC_BUILD) \
1690         || defined(GC_WINDOWS_H_INCLUDED))
1691                 /* Note: for Cygwin and pthreads-win32, this is skipped */
1692                 /* unless windows.h is included before gc.h.            */
1693
1694 # if (!defined(GC_NO_THREAD_DECLS) || defined(GC_BUILD)) \
1695      && !defined(GC_DONT_INCL_WINDOWS_H)
1696
1697 #   ifdef __cplusplus
1698       } /* Including windows.h in an extern "C" context no longer works. */
1699 #   endif
1700
1701 #   if !defined(_WIN32_WCE) && !defined(__CEGCC__)
1702 #     include <process.h> /* For _beginthreadex, _endthreadex */
1703 #   endif
1704
1705 #   if defined(GC_BUILD) || !defined(GC_DONT_INCLUDE_WINDOWS_H)
1706 #     include <windows.h>
1707 #     define GC_WINDOWS_H_INCLUDED
1708 #   endif
1709
1710 #   ifdef __cplusplus
1711       extern "C" {
1712 #   endif
1713
1714 #   ifdef GC_UNDERSCORE_STDCALL
1715       /* Explicitly prefix exported/imported WINAPI (__stdcall) symbols */
1716       /* with '_' (underscore).  Might be useful if MinGW/x86 is used.  */
1717 #     define GC_CreateThread _GC_CreateThread
1718 #     define GC_ExitThread _GC_ExitThread
1719 #   endif
1720
1721 #   ifndef DECLSPEC_NORETURN
1722       /* Typically defined in winnt.h. */
1723 #     ifdef GC_WINDOWS_H_INCLUDED
1724 #       define DECLSPEC_NORETURN /* empty */
1725 #     else
1726 #       define DECLSPEC_NORETURN __declspec(noreturn)
1727 #     endif
1728 #   endif
1729
1730 #   if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED) \
1731        && !defined(UINTPTR_MAX)
1732       typedef GC_word GC_uintptr_t;
1733 #   else
1734       typedef uintptr_t GC_uintptr_t;
1735 #   endif
1736
1737 #   ifdef _WIN64
1738 #     define GC_WIN32_SIZE_T GC_uintptr_t
1739 #   elif defined(GC_WINDOWS_H_INCLUDED)
1740 #     define GC_WIN32_SIZE_T DWORD
1741 #   else
1742 #     define GC_WIN32_SIZE_T unsigned long
1743 #   endif
1744
1745 #   ifdef GC_INSIDE_DLL
1746       /* Export GC DllMain to be invoked from client DllMain.   */
1747 #     ifdef GC_UNDERSCORE_STDCALL
1748 #       define GC_DllMain _GC_DllMain
1749 #     endif
1750 #     ifdef GC_WINDOWS_H_INCLUDED
1751         GC_API BOOL WINAPI GC_DllMain(HINSTANCE /* inst */,
1752                                       ULONG /* reason */,
1753                                       LPVOID /* reserved */);
1754 #     else
1755         GC_API int __stdcall GC_DllMain(void *, unsigned long, void *);
1756 #     endif
1757 #   endif /* GC_INSIDE_DLL */
1758
1759     /* All threads must be created using GC_CreateThread or             */
1760     /* GC_beginthreadex, or must explicitly call GC_register_my_thread  */
1761     /* (and call GC_unregister_my_thread before thread termination), so */
1762     /* that they will be recorded in the thread table.  For backward    */
1763     /* compatibility, it is possible to build the GC with GC_DLL        */
1764     /* defined, and to call GC_use_threads_discovery.  This implicitly  */
1765     /* registers all created threads, but appears to be less robust.    */
1766     /* Currently the collector expects all threads to fall through and  */
1767     /* terminate normally, or call GC_endthreadex() or GC_ExitThread,   */
1768     /* so that the thread is properly unregistered.                     */
1769 #   ifdef GC_WINDOWS_H_INCLUDED
1770       GC_API HANDLE WINAPI GC_CreateThread(
1771                 LPSECURITY_ATTRIBUTES /* lpThreadAttributes */,
1772                 GC_WIN32_SIZE_T /* dwStackSize */,
1773                 LPTHREAD_START_ROUTINE /* lpStartAddress */,
1774                 LPVOID /* lpParameter */, DWORD /* dwCreationFlags */,
1775                 LPDWORD /* lpThreadId */);
1776
1777       GC_API DECLSPEC_NORETURN void WINAPI GC_ExitThread(
1778                                                 DWORD /* dwExitCode */);
1779 #   else
1780       struct _SECURITY_ATTRIBUTES;
1781       GC_API void *__stdcall GC_CreateThread(struct _SECURITY_ATTRIBUTES *,
1782                                 GC_WIN32_SIZE_T,
1783                                 unsigned long (__stdcall *)(void *),
1784                                 void *, unsigned long, unsigned long *);
1785       GC_API DECLSPEC_NORETURN void __stdcall GC_ExitThread(unsigned long);
1786 #   endif
1787
1788 #   if !defined(_WIN32_WCE) && !defined(__CEGCC__)
1789       GC_API GC_uintptr_t GC_CALL GC_beginthreadex(
1790                         void * /* security */, unsigned /* stack_size */,
1791                         unsigned (__stdcall *)(void *),
1792                         void * /* arglist */, unsigned /* initflag */,
1793                         unsigned * /* thrdaddr */);
1794
1795       /* Note: _endthreadex() is not currently marked as no-return in   */
1796       /* VC++ and MinGW headers, so we don't mark it neither.           */
1797       GC_API void GC_CALL GC_endthreadex(unsigned /* retval */);
1798 #   endif /* !_WIN32_WCE */
1799
1800 # endif /* !GC_NO_THREAD_DECLS */
1801
1802 # ifdef GC_WINMAIN_REDIRECT
1803     /* win32_threads.c implements the real WinMain(), which will start  */
1804     /* a new thread to call GC_WinMain() after initializing the garbage */
1805     /* collector.                                                       */
1806 #   define WinMain GC_WinMain
1807 # endif
1808
1809   /* For compatibility only. */
1810 # define GC_use_DllMain GC_use_threads_discovery
1811
1812 # ifndef GC_NO_THREAD_REDIRECTS
1813 #   define CreateThread GC_CreateThread
1814 #   define ExitThread GC_ExitThread
1815 #   undef _beginthreadex
1816 #   define _beginthreadex GC_beginthreadex
1817 #   undef _endthreadex
1818 #   define _endthreadex GC_endthreadex
1819 /* #define _beginthread { > "Please use _beginthreadex instead of _beginthread" < } */
1820 # endif /* !GC_NO_THREAD_REDIRECTS */
1821
1822 #endif /* GC_WIN32_THREADS */
1823
1824 /* Public setter and getter for switching "unmap as much as possible"   */
1825 /* mode on(1) and off(0).  Has no effect unless unmapping is turned on. */
1826 /* Has no effect on implicitly-initiated garbage collections.  Initial  */
1827 /* value is controlled by GC_FORCE_UNMAP_ON_GCOLLECT.  The setter and   */
1828 /* getter are unsynchronized.                                           */
1829 GC_API void GC_CALL GC_set_force_unmap_on_gcollect(int);
1830 GC_API int GC_CALL GC_get_force_unmap_on_gcollect(void);
1831
1832 /* Fully portable code should call GC_INIT() from the main program      */
1833 /* before making any other GC_ calls.  On most platforms this is a      */
1834 /* no-op and the collector self-initializes.  But a number of           */
1835 /* platforms make that too hard.                                        */
1836 /* A GC_INIT call is required if the collector is built with            */
1837 /* THREAD_LOCAL_ALLOC defined and the initial allocation call is not    */
1838 /* to GC_malloc() or GC_malloc_atomic().                                */
1839
1840 #if defined(__CYGWIN32__) || defined(__CYGWIN__)
1841   /* Similarly gnu-win32 DLLs need explicit initialization from the     */
1842   /* main program, as does AIX.                                         */
1843 # ifdef __x86_64__
1844     /* Cygwin/x64 does not add leading underscore to symbols anymore.   */
1845     extern int __data_start__[], __data_end__[];
1846     extern int __bss_start__[], __bss_end__[];
1847 #   define GC_DATASTART ((GC_word)__data_start__ < (GC_word)__bss_start__ \
1848                          ? (void *)__data_start__ : (void *)__bss_start__)
1849 #   define GC_DATAEND ((GC_word)__data_end__ > (GC_word)__bss_end__ \
1850                        ? (void *)__data_end__ : (void *)__bss_end__)
1851 # else
1852     extern int _data_start__[], _data_end__[], _bss_start__[], _bss_end__[];
1853 #   define GC_DATASTART ((GC_word)_data_start__ < (GC_word)_bss_start__ \
1854                          ? (void *)_data_start__ : (void *)_bss_start__)
1855 #  define GC_DATAEND ((GC_word)_data_end__ > (GC_word)_bss_end__ \
1856                       ? (void *)_data_end__ : (void *)_bss_end__)
1857 # endif /* !__x86_64__ */
1858 # define GC_INIT_CONF_ROOTS GC_add_roots(GC_DATASTART, GC_DATAEND); \
1859                                  GC_gcollect() /* For blacklisting. */
1860         /* Required at least if GC is in a DLL.  And doesn't hurt. */
1861 #elif defined(_AIX)
1862   extern int _data[], _end[];
1863 # define GC_DATASTART ((void *)((ulong)_data))
1864 # define GC_DATAEND ((void *)((ulong)_end))
1865 # define GC_INIT_CONF_ROOTS GC_add_roots(GC_DATASTART, GC_DATAEND)
1866 #elif (defined(HOST_ANDROID) || defined(__ANDROID__)) \
1867       && !defined(GC_NOT_DLL) && defined(IGNORE_DYNAMIC_LOADING)
1868   /* It causes the entire binary section of memory be pushed as a root. */
1869   /* This might be a bad idea though because on some Android devices    */
1870   /* some of the binary data might become unmapped thus causing SIGSEGV */
1871   /* with code SEGV_MAPERR.                                             */
1872 # pragma weak _etext
1873 # pragma weak __data_start
1874 # pragma weak __dso_handle
1875   extern int _etext[], __data_start[], __dso_handle[];
1876 # pragma weak __end__
1877   extern int __end__[], _end[];
1878   /* Explicitly register caller static data roots.  Workaround for      */
1879   /* __data_start: NDK "gold" linker might miss it or place it          */
1880   /* incorrectly, __dso_handle is an alternative data start reference.  */
1881   /* Workaround for _end: NDK Clang 3.5+ does not place it at correct   */
1882   /* offset (as of NDK r10e) but "bfd" linker provides __end__ symbol   */
1883   /* that could be used instead.                                        */
1884 # define GC_INIT_CONF_ROOTS \
1885                 (void)((GC_word)__data_start < (GC_word)_etext \
1886                         && (GC_word)_etext < (GC_word)__dso_handle \
1887                         ? (__end__ != 0 \
1888                             ? (GC_add_roots(__dso_handle, __end__), 0) \
1889                             : (GC_word)__dso_handle < (GC_word)_end \
1890                             ? (GC_add_roots(__dso_handle, _end), 0) : 0) \
1891                         : __data_start != 0 ? (__end__ != 0 \
1892                             ? (GC_add_roots(__data_start, __end__), 0) \
1893                             : (GC_word)__data_start < (GC_word)_end \
1894                             ? (GC_add_roots(__data_start, _end), 0) : 0) : 0)
1895 #else
1896 # define GC_INIT_CONF_ROOTS /* empty */
1897 #endif
1898
1899 #ifdef GC_DONT_EXPAND
1900   /* Set GC_dont_expand to TRUE at start-up */
1901 # define GC_INIT_CONF_DONT_EXPAND GC_set_dont_expand(1)
1902 #else
1903 # define GC_INIT_CONF_DONT_EXPAND /* empty */
1904 #endif
1905
1906 #ifdef GC_FORCE_UNMAP_ON_GCOLLECT
1907   /* Turn on "unmap as much as possible on explicit GC" mode at start-up */
1908 # define GC_INIT_CONF_FORCE_UNMAP_ON_GCOLLECT \
1909                 GC_set_force_unmap_on_gcollect(1)
1910 #else
1911 # define GC_INIT_CONF_FORCE_UNMAP_ON_GCOLLECT /* empty */
1912 #endif
1913
1914 #ifdef GC_DONT_GC
1915   /* This is for debugging only (useful if environment variables are    */
1916   /* unsupported); cannot call GC_disable as goes before GC_init.       */
1917 # define GC_INIT_CONF_MAX_RETRIES (void)(GC_dont_gc = 1)
1918 #elif defined(GC_MAX_RETRIES) && !defined(CPPCHECK)
1919   /* Set GC_max_retries to the desired value at start-up */
1920 # define GC_INIT_CONF_MAX_RETRIES GC_set_max_retries(GC_MAX_RETRIES)
1921 #else
1922 # define GC_INIT_CONF_MAX_RETRIES /* empty */
1923 #endif
1924
1925 #if defined(GC_FREE_SPACE_DIVISOR) && !defined(CPPCHECK)
1926   /* Set GC_free_space_divisor to the desired value at start-up */
1927 # define GC_INIT_CONF_FREE_SPACE_DIVISOR \
1928                 GC_set_free_space_divisor(GC_FREE_SPACE_DIVISOR)
1929 #else
1930 # define GC_INIT_CONF_FREE_SPACE_DIVISOR /* empty */
1931 #endif
1932
1933 #if defined(GC_FULL_FREQ) && !defined(CPPCHECK)
1934   /* Set GC_full_freq to the desired value at start-up */
1935 # define GC_INIT_CONF_FULL_FREQ GC_set_full_freq(GC_FULL_FREQ)
1936 #else
1937 # define GC_INIT_CONF_FULL_FREQ /* empty */
1938 #endif
1939
1940 #if defined(GC_TIME_LIMIT) && !defined(CPPCHECK)
1941   /* Set GC_time_limit to the desired value at start-up */
1942 # define GC_INIT_CONF_TIME_LIMIT GC_set_time_limit(GC_TIME_LIMIT)
1943 #else
1944 # define GC_INIT_CONF_TIME_LIMIT /* empty */
1945 #endif
1946
1947 #if defined(GC_SIG_SUSPEND) && defined(GC_THREADS) && !defined(CPPCHECK)
1948 # define GC_INIT_CONF_SUSPEND_SIGNAL GC_set_suspend_signal(GC_SIG_SUSPEND)
1949 #else
1950 # define GC_INIT_CONF_SUSPEND_SIGNAL /* empty */
1951 #endif
1952
1953 #if defined(GC_SIG_THR_RESTART) && defined(GC_THREADS) && !defined(CPPCHECK)
1954 # define GC_INIT_CONF_THR_RESTART_SIGNAL \
1955                 GC_set_thr_restart_signal(GC_SIG_THR_RESTART)
1956 #else
1957 # define GC_INIT_CONF_THR_RESTART_SIGNAL /* empty */
1958 #endif
1959
1960 #if defined(GC_MAXIMUM_HEAP_SIZE) && !defined(CPPCHECK)
1961   /* Limit the heap size to the desired value (useful for debugging).   */
1962   /* The limit could be overridden either at the program start-up by    */
1963   /* the similar environment variable or anytime later by the           */
1964   /* corresponding API function call.                                   */
1965 # define GC_INIT_CONF_MAXIMUM_HEAP_SIZE \
1966                 GC_set_max_heap_size(GC_MAXIMUM_HEAP_SIZE)
1967 #else
1968 # define GC_INIT_CONF_MAXIMUM_HEAP_SIZE /* empty */
1969 #endif
1970
1971 #ifdef GC_IGNORE_WARN
1972   /* Turn off all warnings at start-up (after GC initialization) */
1973 # define GC_INIT_CONF_IGNORE_WARN GC_set_warn_proc(GC_ignore_warn_proc)
1974 #else
1975 # define GC_INIT_CONF_IGNORE_WARN /* empty */
1976 #endif
1977
1978 #if defined(GC_INITIAL_HEAP_SIZE) && !defined(CPPCHECK)
1979   /* Set heap size to the desired value at start-up */
1980 # define GC_INIT_CONF_INITIAL_HEAP_SIZE \
1981                 { size_t heap_size = GC_get_heap_size(); \
1982                   if (heap_size < (GC_INITIAL_HEAP_SIZE)) \
1983                     (void)GC_expand_hp((GC_INITIAL_HEAP_SIZE) - heap_size); }
1984 #else
1985 # define GC_INIT_CONF_INITIAL_HEAP_SIZE /* empty */
1986 #endif
1987
1988 /* Portable clients should call this at the program start-up.  More     */
1989 /* over, some platforms require this call to be done strictly from the  */
1990 /* primordial thread.                                                   */
1991 #define GC_INIT() { GC_INIT_CONF_DONT_EXPAND; /* pre-init */ \
1992                     GC_INIT_CONF_FORCE_UNMAP_ON_GCOLLECT; \
1993                     GC_INIT_CONF_MAX_RETRIES; \
1994                     GC_INIT_CONF_FREE_SPACE_DIVISOR; \
1995                     GC_INIT_CONF_FULL_FREQ; \
1996                     GC_INIT_CONF_TIME_LIMIT; \
1997                     GC_INIT_CONF_SUSPEND_SIGNAL; \
1998                     GC_INIT_CONF_THR_RESTART_SIGNAL; \
1999                     GC_INIT_CONF_MAXIMUM_HEAP_SIZE; \
2000                     GC_init(); /* real GC initialization */ \
2001                     GC_INIT_CONF_ROOTS; /* post-init */ \
2002                     GC_INIT_CONF_IGNORE_WARN; \
2003                     GC_INIT_CONF_INITIAL_HEAP_SIZE; }
2004
2005 /* win32S may not free all resources on process exit.   */
2006 /* This explicitly deallocates the heap.                */
2007 GC_API void GC_CALL GC_win32_free_heap(void);
2008
2009 #if defined(__SYMBIAN32__)
2010   void GC_init_global_static_roots(void);
2011 #endif
2012
2013 #if defined(_AMIGA) && !defined(GC_AMIGA_MAKINGLIB)
2014   /* Allocation really goes through GC_amiga_allocwrapper_do.   */
2015   void *GC_amiga_realloc(void *, size_t);
2016 # define GC_realloc(a,b) GC_amiga_realloc(a,b)
2017   void GC_amiga_set_toany(void (*)(void));
2018   extern int GC_amiga_free_space_divisor_inc;
2019   extern void *(*GC_amiga_allocwrapper_do)(size_t, void *(GC_CALL *)(size_t));
2020 # define GC_malloc(a) \
2021         (*GC_amiga_allocwrapper_do)(a,GC_malloc)
2022 # define GC_malloc_atomic(a) \
2023         (*GC_amiga_allocwrapper_do)(a,GC_malloc_atomic)
2024 # define GC_malloc_uncollectable(a) \
2025         (*GC_amiga_allocwrapper_do)(a,GC_malloc_uncollectable)
2026 # define GC_malloc_atomic_uncollectable(a) \
2027         (*GC_amiga_allocwrapper_do)(a,GC_malloc_atomic_uncollectable)
2028 # define GC_malloc_ignore_off_page(a) \
2029         (*GC_amiga_allocwrapper_do)(a,GC_malloc_ignore_off_page)
2030 # define GC_malloc_atomic_ignore_off_page(a) \
2031         (*GC_amiga_allocwrapper_do)(a,GC_malloc_atomic_ignore_off_page)
2032 #endif /* _AMIGA && !GC_AMIGA_MAKINGLIB */
2033
2034 #ifdef __cplusplus
2035   } /* extern "C" */
2036 #endif
2037
2038 #endif /* GC_H */