TIVI-153: Add as dependency for Iputils
[profile/ivi/gc.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  *
8  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
10  *
11  * Permission is hereby granted to use or copy this program
12  * for any purpose,  provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  */
17
18 /*
19  * Note that this defines a large number of tuning hooks, which can
20  * safely be ignored in nearly all cases.  For normal use it suffices
21  * to call only GC_MALLOC and perhaps GC_REALLOC.
22  * For better performance, also look at GC_MALLOC_ATOMIC, and
23  * GC_enable_incremental.  If you need an action to be performed
24  * immediately before an object is collected, look at GC_register_finalizer.
25  * If you are using Solaris threads, look at the end of this file.
26  * Everything else is best ignored unless you encounter performance
27  * problems.
28  */
29  
30 #ifndef _GC_H
31
32 # define _GC_H
33
34 # include "gc_version.h"
35         /* Define version numbers here to allow test on build machine   */
36         /* for cross-builds.  Note that this defines the header         */
37         /* version number, which may or may not match that of the       */
38         /* dynamic library.  The GC_version variable can be used        */
39         /* to obtain the latter.                                        */
40
41 # include "gc_config_macros.h"
42
43 # ifdef __cplusplus
44     extern "C" {
45 # endif
46
47
48 /* Define word and signed_word to be unsigned and signed types of the   */
49 /* size as char * or void *.  There seems to be no way to do this       */
50 /* even semi-portably.  The following is probably no better/worse       */
51 /* than almost anything else.                                           */
52 /* The ANSI standard suggests that size_t and ptr_diff_t might be       */
53 /* better choices.  But those had incorrect definitions on some older   */
54 /* systems.  Notably "typedef int size_t" is WRONG.                     */
55 #ifndef _WIN64
56   typedef unsigned long GC_word;
57   typedef long GC_signed_word;
58 #else
59   /* Win64 isn't really supported yet, but this is the first step. And  */
60   /* it might cause error messages to show up in more plausible places. */
61   /* This needs basetsd.h, which is included by windows.h.              */
62   typedef unsigned long long GC_word;
63   typedef long long GC_signed_word;
64 #endif
65
66 /* Public read-only variables */
67
68 GC_API GC_word GC_gc_no;/* Counter incremented per collection.          */
69                         /* Includes empty GCs at startup.               */
70
71 GC_API int GC_parallel; /* GC is parallelized for performance on        */
72                         /* multiprocessors.  Currently set only         */
73                         /* implicitly if collector is built with        */
74                         /* -DPARALLEL_MARK and if either:               */
75                         /*  Env variable GC_NPROC is set to > 1, or     */
76                         /*  GC_NPROC is not set and this is an MP.      */
77                         /* If GC_parallel is set, incremental           */
78                         /* collection is only partially functional,     */
79                         /* and may not be desirable.                    */
80                         
81
82 /* Public R/W variables */
83
84 GC_API void * (*GC_oom_fn) (size_t bytes_requested);
85                         /* When there is insufficient memory to satisfy */
86                         /* an allocation request, we return             */
87                         /* (*GC_oom_fn)().  By default this just        */
88                         /* returns 0.                                   */
89                         /* If it returns, it must return 0 or a valid   */
90                         /* pointer to a previously allocated heap       */
91                         /* object.                                      */
92
93 GC_API int GC_find_leak;
94                         /* Do not actually garbage collect, but simply  */
95                         /* report inaccessible memory that was not      */
96                         /* deallocated with GC_free.  Initial value     */
97                         /* is determined by FIND_LEAK macro.            */
98
99 GC_API int GC_all_interior_pointers;
100                         /* Arrange for pointers to object interiors to  */
101                         /* be recognized as valid.  May not be changed  */
102                         /* after GC initialization.                     */
103                         /* Initial value is determined by               */
104                         /* -DALL_INTERIOR_POINTERS.                     */
105                         /* Unless DONT_ADD_BYTE_AT_END is defined, this */
106                         /* also affects whether sizes are increased by  */
107                         /* at least a byte to allow "off the end"       */
108                         /* pointer recognition.                         */
109                         /* MUST BE 0 or 1.                              */
110
111 GC_API int GC_finalize_on_demand;
112                         /* If nonzero, finalizers will only be run in   */
113                         /* response to an explicit GC_invoke_finalizers */
114                         /* call.  The default is determined by whether  */
115                         /* the FINALIZE_ON_DEMAND macro is defined      */
116                         /* when the collector is built.                 */
117
118 GC_API int GC_java_finalization;
119                         /* Mark objects reachable from finalizable      */
120                         /* objects in a separate postpass.  This makes  */
121                         /* it a bit safer to use non-topologically-     */
122                         /* ordered finalization.  Default value is      */
123                         /* determined by JAVA_FINALIZATION macro.       */
124                         /* Enables register_finalizer_unreachable to    */
125                         /* work correctly.                              */
126
127 GC_API void (* GC_finalizer_notifier)(void);
128                         /* Invoked by the collector when there are      */
129                         /* objects to be finalized.  Invoked at most    */
130                         /* once per GC cycle.  Never invoked unless     */
131                         /* GC_finalize_on_demand is set.                */
132                         /* Typically this will notify a finalization    */
133                         /* thread, which will call GC_invoke_finalizers */
134                         /* in response.                                 */
135
136 GC_API int GC_dont_gc;  /* != 0 ==> Dont collect.  In versions 6.2a1+,  */
137                         /* this overrides explicit GC_gcollect() calls. */
138                         /* Used as a counter, so that nested enabling   */
139                         /* and disabling work correctly.  Should        */
140                         /* normally be updated with GC_enable() and     */
141                         /* GC_disable() calls.                          */
142                         /* Direct assignment to GC_dont_gc is           */
143                         /* deprecated.                                  */
144
145 GC_API int GC_dont_expand;
146                         /* Dont expand heap unless explicitly requested */
147                         /* or forced to.                                */
148
149 GC_API int GC_use_entire_heap;
150                 /* Causes the nonincremental collector to use the       */
151                 /* entire heap before collecting.  This was the only    */
152                 /* option for GC versions < 5.0.  This sometimes        */
153                 /* results in more large block fragmentation, since     */
154                 /* very larg blocks will tend to get broken up          */
155                 /* during each GC cycle.  It is likely to result in a   */
156                 /* larger working set, but lower collection             */
157                 /* frequencies, and hence fewer instructions executed   */
158                 /* in the collector.                                    */
159
160 GC_API int GC_full_freq;    /* Number of partial collections between    */
161                             /* full collections.  Matters only if       */
162                             /* GC_incremental is set.                   */
163                             /* Full collections are also triggered if   */
164                             /* the collector detects a substantial      */
165                             /* increase in the number of in-use heap    */
166                             /* blocks.  Values in the tens are now      */
167                             /* perfectly reasonable, unlike for         */
168                             /* earlier GC versions.                     */
169                         
170 GC_API GC_word GC_non_gc_bytes;
171                         /* Bytes not considered candidates for collection. */
172                         /* Used only to control scheduling of collections. */
173                         /* Updated by GC_malloc_uncollectable and GC_free. */
174                         /* Wizards only.                                   */
175
176 GC_API int GC_no_dls;
177                         /* Don't register dynamic library data segments. */
178                         /* Wizards only.  Should be used only if the     */
179                         /* application explicitly registers all roots.   */
180                         /* In Microsoft Windows environments, this will  */
181                         /* usually also prevent registration of the      */
182                         /* main data segment as part of the root set.    */
183
184 GC_API GC_word GC_free_space_divisor;
185                         /* We try to make sure that we allocate at      */
186                         /* least N/GC_free_space_divisor bytes between  */
187                         /* collections, where N is twice the number     */
188                         /* of traced bytes, plus the number of untraced */
189                         /* bytes (bytes in "atomic" objects), plus      */
190                         /* a rough estimate of the root set size.       */
191                         /* N approximates GC tracing work per GC.       */
192                         /* Initially, GC_free_space_divisor = 3.        */
193                         /* Increasing its value will use less space     */
194                         /* but more collection time.  Decreasing it     */
195                         /* will appreciably decrease collection time    */
196                         /* at the expense of space.                     */
197
198 GC_API GC_word GC_max_retries;
199                         /* The maximum number of GCs attempted before   */
200                         /* reporting out of memory after heap           */
201                         /* expansion fails.  Initially 0.               */
202                         
203
204 GC_API char *GC_stackbottom;    /* Cool end of user stack.              */
205                                 /* May be set in the client prior to    */
206                                 /* calling any GC_ routines.  This      */
207                                 /* avoids some overhead, and            */
208                                 /* potentially some signals that can    */
209                                 /* confuse debuggers.  Otherwise the    */
210                                 /* collector attempts to set it         */
211                                 /* automatically.                       */
212                                 /* For multithreaded code, this is the  */
213                                 /* cold end of the stack for the        */
214                                 /* primordial thread.                   */      
215                                 
216 GC_API int GC_dont_precollect;  /* Don't collect as part of             */
217                                 /* initialization.  Should be set only  */
218                                 /* if the client wants a chance to      */
219                                 /* manually initialize the root set     */
220                                 /* before the first collection.         */
221                                 /* Interferes with blacklisting.        */
222                                 /* Wizards only.                        */
223
224 GC_API unsigned long GC_time_limit;
225                                 /* If incremental collection is enabled, */
226                                 /* We try to terminate collections       */
227                                 /* after this many milliseconds.  Not a  */
228                                 /* hard time bound.  Setting this to     */
229                                 /* GC_TIME_UNLIMITED will essentially    */
230                                 /* disable incremental collection while  */
231                                 /* leaving generational collection       */
232                                 /* enabled.                              */
233 #       define GC_TIME_UNLIMITED 999999
234                                 /* Setting GC_time_limit to this value   */
235                                 /* will disable the "pause time exceeded"*/
236                                 /* tests.                                */
237
238 /* Public procedures */
239
240 /* Initialize the collector.  Portable clients should call GC_INIT() from
241  * the main program instead.
242  */
243 GC_API void GC_init(void);
244
245 /*
246  * general purpose allocation routines, with roughly malloc calling conv.
247  * The atomic versions promise that no relevant pointers are contained
248  * in the object.  The nonatomic versions guarantee that the new object
249  * is cleared.  GC_malloc_stubborn promises that no changes to the object
250  * will occur after GC_end_stubborn_change has been called on the
251  * result of GC_malloc_stubborn. GC_malloc_uncollectable allocates an object
252  * that is scanned for pointers to collectable objects, but is not itself
253  * collectable.  The object is scanned even if it does not appear to
254  * be reachable.  GC_malloc_uncollectable and GC_free called on the resulting
255  * object implicitly update GC_non_gc_bytes appropriately.
256  *
257  * Note that the GC_malloc_stubborn support is stubbed out by default
258  * starting in 6.0.  GC_malloc_stubborn is an alias for GC_malloc unless
259  * the collector is built with STUBBORN_ALLOC defined.
260  */
261 GC_API void * GC_malloc(size_t size_in_bytes);
262 GC_API void * GC_malloc_atomic(size_t size_in_bytes);
263 GC_API char * GC_strdup (const char *str);
264 GC_API void * GC_malloc_uncollectable(size_t size_in_bytes);
265 GC_API void * GC_malloc_stubborn(size_t size_in_bytes);
266
267 /* The following is only defined if the library has been suitably       */
268 /* compiled:                                                            */
269 GC_API void * GC_malloc_atomic_uncollectable(size_t size_in_bytes);
270
271 /* Explicitly deallocate an object.  Dangerous if used incorrectly.     */
272 /* Requires a pointer to the base of an object.                         */
273 /* If the argument is stubborn, it should not be changeable when freed. */
274 /* An object should not be enable for finalization when it is           */
275 /* explicitly deallocated.                                              */
276 /* GC_free(0) is a no-op, as required by ANSI C for free.               */
277 GC_API void GC_free(void * object_addr);
278
279 /*
280  * Stubborn objects may be changed only if the collector is explicitly informed.
281  * The collector is implicitly informed of coming change when such
282  * an object is first allocated.  The following routines inform the
283  * collector that an object will no longer be changed, or that it will
284  * once again be changed.  Only nonNIL pointer stores into the object
285  * are considered to be changes.  The argument to GC_end_stubborn_change
286  * must be exacly the value returned by GC_malloc_stubborn or passed to
287  * GC_change_stubborn.  (In the second case it may be an interior pointer
288  * within 512 bytes of the beginning of the objects.)
289  * There is a performance penalty for allowing more than
290  * one stubborn object to be changed at once, but it is acceptable to
291  * do so.  The same applies to dropping stubborn objects that are still
292  * changeable.
293  */
294 GC_API void GC_change_stubborn(void *);
295 GC_API void GC_end_stubborn_change(void *);
296
297 /* Return a pointer to the base (lowest address) of an object given     */
298 /* a pointer to a location within the object.                           */
299 /* I.e. map an interior pointer to the corresponding bas pointer.       */
300 /* Note that with debugging allocation, this returns a pointer to the   */
301 /* actual base of the object, i.e. the debug information, not to        */
302 /* the base of the user object.                                         */
303 /* Return 0 if displaced_pointer doesn't point to within a valid        */
304 /* object.                                                              */
305 /* Note that a deallocated object in the garbage collected heap         */
306 /* may be considered valid, even if it has been deallocated with        */
307 /* GC_free.                                                             */
308 GC_API void * GC_base(void * displaced_pointer);
309
310 /* Given a pointer to the base of an object, return its size in bytes.  */
311 /* The returned size may be slightly larger than what was originally    */
312 /* requested.                                                           */
313 GC_API size_t GC_size(void * object_addr);
314
315 /* For compatibility with C library.  This is occasionally faster than  */
316 /* a malloc followed by a bcopy.  But if you rely on that, either here  */
317 /* or with the standard C library, your code is broken.  In my          */
318 /* opinion, it shouldn't have been invented, but now we're stuck. -HB   */
319 /* The resulting object has the same kind as the original.              */
320 /* If the argument is stubborn, the result will have changes enabled.   */
321 /* It is an error to have changes enabled for the original object.      */
322 /* Follows ANSI comventions for NULL old_object.                        */
323 GC_API void * GC_realloc(void * old_object, size_t new_size_in_bytes);
324                                    
325 /* Explicitly increase the heap size.   */
326 /* Returns 0 on failure, 1 on success.  */
327 GC_API int GC_expand_hp(size_t number_of_bytes);
328
329 /* Limit the heap size to n bytes.  Useful when you're debugging,       */
330 /* especially on systems that don't handle running out of memory well.  */
331 /* n == 0 ==> unbounded.  This is the default.                          */
332 GC_API void GC_set_max_heap_size(GC_word n);
333
334 /* Inform the collector that a certain section of statically allocated  */
335 /* memory contains no pointers to garbage collected memory.  Thus it    */
336 /* need not be scanned.  This is sometimes important if the application */
337 /* maps large read/write files into the address space, which could be   */
338 /* mistaken for dynamic library data segments on some systems.          */
339 GC_API void GC_exclude_static_roots(void * low_address,
340                                     void * high_address_plus_1);
341
342 /* Clear the set of root segments.  Wizards only. */
343 GC_API void GC_clear_roots(void);
344
345 /* Add a root segment.  Wizards only. */
346 GC_API void GC_add_roots(void * low_address, void * high_address_plus_1);
347
348 /* Remove a root segment.  Wizards only. */
349 GC_API void GC_remove_roots(void * low_address, void * high_address_plus_1);
350
351 /* Add a displacement to the set of those considered valid by the       */
352 /* collector.  GC_register_displacement(n) means that if p was returned */
353 /* by GC_malloc, then (char *)p + n will be considered to be a valid    */
354 /* pointer to p.  N must be small and less than the size of p.          */
355 /* (All pointers to the interior of objects from the stack are          */
356 /* considered valid in any case.  This applies to heap objects and      */
357 /* static data.)                                                        */
358 /* Preferably, this should be called before any other GC procedures.    */
359 /* Calling it later adds to the probability of excess memory            */
360 /* retention.                                                           */
361 /* This is a no-op if the collector has recognition of                  */
362 /* arbitrary interior pointers enabled, which is now the default.       */
363 GC_API void GC_register_displacement(size_t n);
364
365 /* The following version should be used if any debugging allocation is  */
366 /* being done.                                                          */
367 GC_API void GC_debug_register_displacement(size_t n);
368
369 /* Explicitly trigger a full, world-stop collection.    */
370 GC_API void GC_gcollect(void);
371
372 /* Trigger a full world-stopped collection.  Abort the collection if    */
373 /* and when stop_func returns a nonzero value.  Stop_func will be       */
374 /* called frequently, and should be reasonably fast.  This works even   */
375 /* if virtual dirty bits, and hence incremental collection is not       */
376 /* available for this architecture.  Collections can be aborted faster  */
377 /* than normal pause times for incremental collection.  However,        */
378 /* aborted collections do no useful work; the next collection needs     */
379 /* to start from the beginning.                                         */
380 /* Return 0 if the collection was aborted, 1 if it succeeded.           */
381 typedef int (* GC_stop_func)(void);
382 GC_API int GC_try_to_collect(GC_stop_func stop_func);
383
384 /* Return the number of bytes in the heap.  Excludes collector private  */
385 /* data structures.  Includes empty blocks and fragmentation loss.      */
386 /* Includes some pages that were allocated but never written.           */
387 GC_API size_t GC_get_heap_size(void);
388
389 /* Return a lower bound on the number of free bytes in the heap.        */
390 GC_API size_t GC_get_free_bytes(void);
391
392 /* Return the number of bytes allocated since the last collection.      */
393 GC_API size_t GC_get_bytes_since_gc(void);
394
395 /* Return the total number of bytes allocated in this process.          */
396 /* Never decreases, except due to wrapping.                             */
397 GC_API size_t GC_get_total_bytes(void);
398
399 /* Disable garbage collection.  Even GC_gcollect calls will be          */
400 /* ineffective.                                                         */
401 GC_API void GC_disable(void);
402
403 /* Reenable garbage collection.  GC_disable() and GC_enable() calls     */
404 /* nest.  Garbage collection is enabled if the number of calls to both  */
405 /* both functions is equal.                                             */
406 GC_API void GC_enable(void);
407
408 /* Enable incremental/generational collection.  */
409 /* Not advisable unless dirty bits are          */
410 /* available or most heap objects are           */
411 /* pointerfree(atomic) or immutable.            */
412 /* Don't use in leak finding mode.              */
413 /* Ignored if GC_dont_gc is true.               */
414 /* Only the generational piece of this is       */
415 /* functional if GC_parallel is TRUE            */
416 /* or if GC_time_limit is GC_TIME_UNLIMITED.    */
417 /* Causes GC_local_gcj_malloc() to revert to    */
418 /* locked allocation.  Must be called           */
419 /* before any GC_local_gcj_malloc() calls.      */
420 /* For best performance, should be called as early as possible. */
421 /* On some platforms, calling it later may have adverse effects.*/
422 /* Safe to call before GC_INIT().  Includes a GC_init() call.   */
423 GC_API void GC_enable_incremental(void);
424
425 /* Does incremental mode write-protect pages?  Returns zero or  */
426 /* more of the following, or'ed together:                       */
427 #define GC_PROTECTS_POINTER_HEAP  1 /* May protect non-atomic objs.     */
428 #define GC_PROTECTS_PTRFREE_HEAP  2
429 #define GC_PROTECTS_STATIC_DATA   4 /* Currently never.                 */
430 #define GC_PROTECTS_STACK         8 /* Probably impractical.            */
431
432 #define GC_PROTECTS_NONE 0
433 GC_API int GC_incremental_protection_needs(void);
434
435 /* Perform some garbage collection work, if appropriate.        */
436 /* Return 0 if there is no more work to be done.                */
437 /* Typically performs an amount of work corresponding roughly   */
438 /* to marking from one page.  May do more work if further       */
439 /* progress requires it, e.g. if incremental collection is      */
440 /* disabled.  It is reasonable to call this in a wait loop      */
441 /* until it returns 0.                                          */
442 GC_API int GC_collect_a_little(void);
443
444 /* Allocate an object of size lb bytes.  The client guarantees that     */
445 /* as long as the object is live, it will be referenced by a pointer    */
446 /* that points to somewhere within the first 256 bytes of the object.   */
447 /* (This should normally be declared volatile to prevent the compiler   */
448 /* from invalidating this assertion.)  This routine is only useful      */
449 /* if a large array is being allocated.  It reduces the chance of       */
450 /* accidentally retaining such an array as a result of scanning an      */
451 /* integer that happens to be an address inside the array.  (Actually,  */
452 /* it reduces the chance of the allocator not finding space for such    */
453 /* an array, since it will try hard to avoid introducing such a false   */
454 /* reference.)  On a SunOS 4.X or MS Windows system this is recommended */
455 /* for arrays likely to be larger than 100K or so.  For other systems,  */
456 /* or if the collector is not configured to recognize all interior      */
457 /* pointers, the threshold is normally much higher.                     */
458 GC_API void * GC_malloc_ignore_off_page(size_t lb);
459 GC_API void * GC_malloc_atomic_ignore_off_page(size_t lb);
460
461 #if defined(__sgi) && !defined(__GNUC__) && _COMPILER_VERSION >= 720
462 #   define GC_ADD_CALLER
463 #   define GC_RETURN_ADDR (GC_word)__return_address
464 #endif
465
466 #if defined(__linux__) || defined(__GLIBC__)
467 # include <features.h>
468 # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1 || __GLIBC__ > 2) \
469      && !defined(__ia64__) && !defined(__UCLIBC__)
470 #   ifndef GC_HAVE_BUILTIN_BACKTRACE
471 #     define GC_HAVE_BUILTIN_BACKTRACE
472 #   endif
473 # endif
474 # if defined(__i386__) || defined(__x86_64__)
475 #   define GC_CAN_SAVE_CALL_STACKS
476 # endif
477 #endif
478
479 #if defined(_MSC_VER) && _MSC_VER >= 1200 /* version 12.0+ (MSVC 6.0+)  */ \
480     && !defined(_AMD64_)
481 # ifndef GC_HAVE_NO_BUILTIN_BACKTRACE
482 #   define GC_HAVE_BUILTIN_BACKTRACE
483 # endif
484 #endif
485
486 #if defined(GC_HAVE_BUILTIN_BACKTRACE) && !defined(GC_CAN_SAVE_CALL_STACKS)
487 # define GC_CAN_SAVE_CALL_STACKS
488 #endif
489
490 #if defined(__sparc__)
491 #   define GC_CAN_SAVE_CALL_STACKS
492 #endif
493
494 /* If we're on an a platform on which we can't save call stacks, but    */
495 /* gcc is normally used, we go ahead and define GC_ADD_CALLER.          */
496 /* We make this decision independent of whether gcc is actually being   */
497 /* used, in order to keep the interface consistent, and allow mixing    */
498 /* of compilers.                                                        */
499 /* This may also be desirable if it is possible but expensive to        */
500 /* retrieve the call chain.                                             */
501 #if (defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__) \
502      || defined(__FreeBSD__) || defined(__DragonFly__)) & !defined(GC_CAN_SAVE_CALL_STACKS)
503 # define GC_ADD_CALLER
504 # if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95) 
505     /* gcc knows how to retrieve return address, but we don't know */
506     /* how to generate call stacks.                                */
507 #   define GC_RETURN_ADDR (GC_word)__builtin_return_address(0)
508 # else
509     /* Just pass 0 for gcc compatibility. */
510 #   define GC_RETURN_ADDR 0
511 # endif
512 #endif
513
514 #ifdef GC_ADD_CALLER
515 #  define GC_EXTRAS GC_RETURN_ADDR, __FILE__, __LINE__
516 #  define GC_EXTRA_PARAMS GC_word ra, const char * s, int i
517 #else
518 #  define GC_EXTRAS __FILE__, __LINE__
519 #  define GC_EXTRA_PARAMS const char * s, int i
520 #endif
521
522 /* Debugging (annotated) allocation.  GC_gcollect will check            */
523 /* objects allocated in this way for overwrites, etc.                   */
524 GC_API void * GC_debug_malloc(size_t size_in_bytes, GC_EXTRA_PARAMS);
525 GC_API void * GC_debug_malloc_atomic(size_t size_in_bytes, GC_EXTRA_PARAMS);
526 GC_API char * GC_debug_strdup(const char *str, GC_EXTRA_PARAMS);
527 GC_API void * GC_debug_malloc_uncollectable
528         (size_t size_in_bytes, GC_EXTRA_PARAMS);
529 GC_API void * GC_debug_malloc_stubborn
530         (size_t size_in_bytes, GC_EXTRA_PARAMS);
531 GC_API void * GC_debug_malloc_ignore_off_page
532         (size_t size_in_bytes, GC_EXTRA_PARAMS);
533 GC_API void * GC_debug_malloc_atomic_ignore_off_page
534         (size_t size_in_bytes, GC_EXTRA_PARAMS);
535 GC_API void GC_debug_free (void * object_addr);
536 GC_API void * GC_debug_realloc
537         (void * old_object, size_t new_size_in_bytes, GC_EXTRA_PARAMS);
538 GC_API void GC_debug_change_stubborn(void *);
539 GC_API void GC_debug_end_stubborn_change(void *);
540
541 /* Routines that allocate objects with debug information (like the      */
542 /* above), but just fill in dummy file and line number information.     */
543 /* Thus they can serve as drop-in malloc/realloc replacements.  This    */
544 /* can be useful for two reasons:                                       */
545 /* 1) It allows the collector to be built with DBG_HDRS_ALL defined     */
546 /*    even if some allocation calls come from 3rd party libraries       */
547 /*    that can't be recompiled.                                         */
548 /* 2) On some platforms, the file and line information is redundant,    */
549 /*    since it can be reconstructed from a stack trace.  On such        */
550 /*    platforms it may be more convenient not to recompile, e.g. for    */
551 /*    leak detection.  This can be accomplished by instructing the      */
552 /*    linker to replace malloc/realloc with these.                      */
553 GC_API void * GC_debug_malloc_replacement (size_t size_in_bytes);
554 GC_API void * GC_debug_realloc_replacement
555               (void * object_addr, size_t size_in_bytes);
556                                  
557 # ifdef GC_DEBUG
558 #   define GC_MALLOC(sz) GC_debug_malloc(sz, GC_EXTRAS)
559 #   define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, GC_EXTRAS)
560 #   define GC_STRDUP(s) GC_debug_strdup((s), GC_EXTRAS)
561 #   define GC_MALLOC_UNCOLLECTABLE(sz) \
562                         GC_debug_malloc_uncollectable(sz, GC_EXTRAS)
563 #   define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
564                         GC_debug_malloc_ignore_off_page(sz, GC_EXTRAS)
565 #   define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
566                         GC_debug_malloc_atomic_ignore_off_page(sz, GC_EXTRAS)
567 #   define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, GC_EXTRAS)
568 #   define GC_FREE(p) GC_debug_free(p)
569 #   define GC_REGISTER_FINALIZER(p, f, d, of, od) \
570         GC_debug_register_finalizer(p, f, d, of, od)
571 #   define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
572         GC_debug_register_finalizer_ignore_self(p, f, d, of, od)
573 #   define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
574         GC_debug_register_finalizer_no_order(p, f, d, of, od)
575 #   define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \
576         GC_debug_register_finalizer_unreachable(p, f, d, of, od)
577 #   define GC_MALLOC_STUBBORN(sz) GC_debug_malloc_stubborn(sz, GC_EXTRAS);
578 #   define GC_CHANGE_STUBBORN(p) GC_debug_change_stubborn(p)
579 #   define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p)
580 #   define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
581         GC_general_register_disappearing_link(link, GC_base(obj))
582 #   define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n)
583 # else
584 #   define GC_MALLOC(sz) GC_malloc(sz)
585 #   define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz)
586 #   define GC_STRDUP(s) GC_strdup(s)
587 #   define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz)
588 #   define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
589                         GC_malloc_ignore_off_page(sz)
590 #   define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
591                         GC_malloc_atomic_ignore_off_page(sz)
592 #   define GC_REALLOC(old, sz) GC_realloc(old, sz)
593 #   define GC_FREE(p) GC_free(p)
594 #   define GC_REGISTER_FINALIZER(p, f, d, of, od) \
595         GC_register_finalizer(p, f, d, of, od)
596 #   define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
597         GC_register_finalizer_ignore_self(p, f, d, of, od)
598 #   define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
599         GC_register_finalizer_no_order(p, f, d, of, od)
600 #   define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \
601         GC_register_finalizer_unreachable(p, f, d, of, od)
602 #   define GC_MALLOC_STUBBORN(sz) GC_malloc_stubborn(sz)
603 #   define GC_CHANGE_STUBBORN(p) GC_change_stubborn(p)
604 #   define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p)
605 #   define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
606         GC_general_register_disappearing_link(link, obj)
607 #   define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n)
608 # endif
609 /* The following are included because they are often convenient, and    */
610 /* reduce the chance for a misspecifed size argument.  But calls may    */
611 /* expand to something syntactically incorrect if t is a complicated    */
612 /* type expression.                                                     */
613 # define GC_NEW(t) (t *)GC_MALLOC(sizeof (t))
614 # define GC_NEW_ATOMIC(t) (t *)GC_MALLOC_ATOMIC(sizeof (t))
615 # define GC_NEW_STUBBORN(t) (t *)GC_MALLOC_STUBBORN(sizeof (t))
616 # define GC_NEW_UNCOLLECTABLE(t) (t *)GC_MALLOC_UNCOLLECTABLE(sizeof (t))
617
618 /* Finalization.  Some of these primitives are grossly unsafe.          */
619 /* The idea is to make them both cheap, and sufficient to build         */
620 /* a safer layer, closer to Modula-3, Java, or PCedar finalization.     */
621 /* The interface represents my conclusions from a long discussion       */
622 /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes,              */
623 /* Christian Jacobi, and Russ Atkinson.  It's not perfect, and          */
624 /* probably nobody else agrees with it.     Hans-J. Boehm  3/13/92      */
625 typedef void (*GC_finalization_proc) (void * obj, void * client_data);
626
627 GC_API void GC_register_finalizer(void * obj, GC_finalization_proc fn,
628                                   void * cd, GC_finalization_proc *ofn,
629                                   void * *ocd);
630 GC_API void GC_debug_register_finalizer
631                  (void * obj, GC_finalization_proc fn, void * cd,
632                   GC_finalization_proc *ofn, void * *ocd);
633         /* When obj is no longer accessible, invoke             */
634         /* (*fn)(obj, cd).  If a and b are inaccessible, and    */
635         /* a points to b (after disappearing links have been    */
636         /* made to disappear), then only a will be              */
637         /* finalized.  (If this does not create any new         */
638         /* pointers to b, then b will be finalized after the    */
639         /* next collection.)  Any finalizable object that       */
640         /* is reachable from itself by following one or more    */
641         /* pointers will not be finalized (or collected).       */
642         /* Thus cycles involving finalizable objects should     */
643         /* be avoided, or broken by disappearing links.         */
644         /* All but the last finalizer registered for an object  */
645         /* is ignored.                                          */
646         /* Finalization may be removed by passing 0 as fn.      */
647         /* Finalizers are implicitly unregistered just before   */
648         /* they are invoked.                                    */
649         /* The old finalizer and client data are stored in      */
650         /* *ofn and *ocd.                                       */ 
651         /* Fn is never invoked on an accessible object,         */
652         /* provided hidden pointers are converted to real       */
653         /* pointers only if the allocation lock is held, and    */
654         /* such conversions are not performed by finalization   */
655         /* routines.                                            */
656         /* If GC_register_finalizer is aborted as a result of   */
657         /* a signal, the object may be left with no             */
658         /* finalization, even if neither the old nor new        */
659         /* finalizer were NULL.                                 */
660         /* Obj should be the nonNULL starting address of an     */
661         /* object allocated by GC_malloc or friends.            */
662         /* Note that any garbage collectable object referenced  */
663         /* by cd will be considered accessible until the        */
664         /* finalizer is invoked.                                */
665
666 /* Another versions of the above follow.  It ignores            */
667 /* self-cycles, i.e. pointers from a finalizable object to      */
668 /* itself.  There is a stylistic argument that this is wrong,   */
669 /* but it's unavoidable for C++, since the compiler may         */
670 /* silently introduce these.  It's also benign in that specific */
671 /* case.  And it helps if finalizable objects are split to      */
672 /* avoid cycles.                                                */
673 /* Note that cd will still be viewed as accessible, even if it  */
674 /* refers to the object itself.                                 */
675 GC_API void GC_register_finalizer_ignore_self
676                 (void * obj, GC_finalization_proc fn, void * cd,
677                  GC_finalization_proc *ofn, void * *ocd);
678 GC_API void GC_debug_register_finalizer_ignore_self
679                 (void * obj, GC_finalization_proc fn, void * cd,
680                  GC_finalization_proc *ofn, void * *ocd);
681
682 /* Another version of the above.  It ignores all cycles.        */
683 /* It should probably only be used by Java implementations.     */
684 /* Note that cd will still be viewed as accessible, even if it  */
685 /* refers to the object itself.                                 */
686 GC_API void GC_register_finalizer_no_order
687                 (void * obj, GC_finalization_proc fn, void * cd,
688                  GC_finalization_proc *ofn, void * *ocd);
689 GC_API void GC_debug_register_finalizer_no_order
690                 (void * obj, GC_finalization_proc fn, void * cd,
691                  GC_finalization_proc *ofn, void * *ocd);
692
693 /* This is a special finalizer that is useful when an object's  */
694 /* finalizer must be run when the object is known to be no      */
695 /* longer reachable, not even from other finalizable objects.   */
696 /* It behaves like "normal" finalization, except that the       */
697 /* finalizer is not run while the object is reachable from      */
698 /* other objects specifying unordered finalization.             */
699 /* Effectively it allows an object referenced, possibly         */
700 /* indirectly, from an unordered finalizable object to override */
701 /* the unordered finalization request.                          */
702 /* This can be used in combination with finalizer_no_order so   */
703 /* as to release resources that must not be released while an   */
704 /* object can still be brought back to life by other            */
705 /* finalizers.                                                  */
706 /* Only works if GC_java_finalization is set.  Probably only    */
707 /* of interest when implementing a language that requires       */
708 /* unordered finalization (e.g. Java, C#).                      */
709 GC_API void GC_register_finalizer_unreachable
710                  (void * obj, GC_finalization_proc fn, void * cd,
711                   GC_finalization_proc *ofn, void * *ocd);
712 GC_API void GC_debug_register_finalizer_unreachable
713                  (void * obj, GC_finalization_proc fn, void * cd,
714                   GC_finalization_proc *ofn, void * *ocd);
715
716 /* The following routine may be used to break cycles between    */
717 /* finalizable objects, thus causing cyclic finalizable         */
718 /* objects to be finalized in the correct order.  Standard      */
719 /* use involves calling GC_register_disappearing_link(&p),      */
720 /* where p is a pointer that is not followed by finalization    */
721 /* code, and should not be considered in determining            */
722 /* finalization order.                                          */
723 GC_API int GC_register_disappearing_link(void * * link );
724         /* Link should point to a field of a heap allocated     */
725         /* object obj.  *link will be cleared when obj is       */
726         /* found to be inaccessible.  This happens BEFORE any   */
727         /* finalization code is invoked, and BEFORE any         */
728         /* decisions about finalization order are made.         */
729         /* This is useful in telling the finalizer that         */
730         /* some pointers are not essential for proper           */
731         /* finalization.  This may avoid finalization cycles.   */
732         /* Note that obj may be resurrected by another          */
733         /* finalizer, and thus the clearing of *link may        */
734         /* be visible to non-finalization code.                 */
735         /* There's an argument that an arbitrary action should  */
736         /* be allowed here, instead of just clearing a pointer. */
737         /* But this causes problems if that action alters, or   */
738         /* examines connectivity.                               */
739         /* Returns 1 if link was already registered, 0 if       */
740         /* registration succeeded, 2 if it failed for lack of   */
741         /* memory, and GC_oom_fn did not handle the problem.    */
742         /* Only exists for backward compatibility.  See below:  */
743         
744 GC_API int GC_general_register_disappearing_link (void * * link, void * obj);
745         /* A slight generalization of the above. *link is       */
746         /* cleared when obj first becomes inaccessible.  This   */
747         /* can be used to implement weak pointers easily and    */
748         /* safely. Typically link will point to a location      */
749         /* holding a disguised pointer to obj.  (A pointer      */
750         /* inside an "atomic" object is effectively             */
751         /* disguised.)   In this way soft                       */
752         /* pointers are broken before any object                */
753         /* reachable from them are finalized.  Each link        */
754         /* May be registered only once, i.e. with one obj       */
755         /* value.  This was added after a long email discussion */
756         /* with John Ellis.                                     */
757         /* Obj must be a pointer to the first word of an object */
758         /* we allocated.  It is unsafe to explicitly deallocate */
759         /* the object containing link.  Explicitly deallocating */
760         /* obj may or may not cause link to eventually be       */
761         /* cleared.                                             */
762         /* This can be used to implement certain types of       */
763         /* weak pointers.  Note however that this generally     */
764         /* requires that thje allocation lock is held (see      */
765         /* GC_call_with_allock_lock() below) when the disguised */
766         /* pointer is accessed.  Otherwise a strong pointer     */
767         /* could be recreated between the time the collector    */
768         /* decides to reclaim the object and the link is        */
769         /* cleared.                                             */
770
771 GC_API int GC_unregister_disappearing_link (void * * link);
772         /* Returns 0 if link was not actually registered.       */
773         /* Undoes a registration by either of the above two     */
774         /* routines.                                            */
775
776 /* Returns !=0  if GC_invoke_finalizers has something to do.            */
777 GC_API int GC_should_invoke_finalizers(void);
778
779 GC_API int GC_invoke_finalizers(void);
780         /* Run finalizers for all objects that are ready to     */
781         /* be finalized.  Return the number of finalizers       */
782         /* that were run.  Normally this is also called         */
783         /* implicitly during some allocations.  If              */
784         /* GC-finalize_on_demand is nonzero, it must be called  */
785         /* explicitly.                                          */
786
787 /* Explicitly tell the collector that an object is reachable    */
788 /* at a particular program point.  This prevents the argument   */
789 /* pointer from being optimized away, even it is otherwise no   */
790 /* longer needed.  It should have no visible effect in the      */
791 /* absence of finalizers or disappearing links.  But it may be  */
792 /* needed to prevent finalizers from running while the          */
793 /* associated external resource is still in use.                */
794 /* The function is sometimes called keep_alive in other         */
795 /* settings.                                                    */
796 # if defined(__GNUC__) && !defined(__INTEL_COMPILER)
797 #   define GC_reachable_here(ptr) \
798     __asm__ volatile(" " : : "X"(ptr) : "memory");
799 # else
800     GC_API void GC_noop1(GC_word x);
801 #   define GC_reachable_here(ptr) GC_noop1((GC_word)(ptr));
802 #endif
803
804 /* GC_set_warn_proc can be used to redirect or filter warning messages. */
805 /* p may not be a NULL pointer.                                         */
806 typedef void (*GC_warn_proc) (char *msg, GC_word arg);
807 GC_API GC_warn_proc GC_set_warn_proc(GC_warn_proc p);
808     /* Returns old warning procedure.   */
809
810 GC_API GC_word GC_set_free_space_divisor(GC_word value);
811     /* Set free_space_divisor.  See above for definition.       */
812     /* Returns old value.                                       */
813         
814 /* The following is intended to be used by a higher level       */
815 /* (e.g. Java-like) finalization facility.  It is expected      */
816 /* that finalization code will arrange for hidden pointers to   */
817 /* disappear.  Otherwise objects can be accessed after they     */
818 /* have been collected.                                         */
819 /* Note that putting pointers in atomic objects or in           */
820 /* nonpointer slots of "typed" objects is equivalent to         */
821 /* disguising them in this way, and may have other advantages.  */
822 # if defined(I_HIDE_POINTERS) || defined(GC_I_HIDE_POINTERS)
823     typedef GC_word GC_hidden_pointer;
824 #   define HIDE_POINTER(p) (~(GC_hidden_pointer)(p))
825 #   define REVEAL_POINTER(p) ((void *)(HIDE_POINTER(p)))
826     /* Converting a hidden pointer to a real pointer requires verifying */
827     /* that the object still exists.  This involves acquiring the       */
828     /* allocator lock to avoid a race with the collector.               */
829 # endif /* I_HIDE_POINTERS */
830
831 typedef void * (*GC_fn_type) (void * client_data);
832 GC_API void * GC_call_with_alloc_lock (GC_fn_type fn, void * client_data);
833
834 /* These routines are intended to explicitly notify the collector       */
835 /* of new threads.  Often this is unnecessary because thread creation   */
836 /* is implicitly intercepted by the collector, using header-file        */
837 /* defines, or linker-based interception.  In the long run the intent   */
838 /* is to always make redundant registration safe.  In the short run,    */
839 /* this is being implemented a platform at a time.                      */
840 /* The interface is complicated by the fact that we probably will not   */
841 /* ever be able to automatically determine the stack base for thread    */
842 /* stacks on all platforms.                                             */
843
844 /* Structure representing the base of a thread stack.  On most          */
845 /* platforms this contains just a single address.                       */
846 struct GC_stack_base {
847         void * mem_base;        /* Base of memory stack.        */
848 #       if defined(__ia64) || defined(__ia64__)
849           void * reg_base;      /* Base of separate register stack.     */
850 #       endif
851 };
852
853 typedef void * (*GC_stack_base_func)(struct GC_stack_base *sb, void *arg);
854
855 /* Call a function with a stack base structure corresponding to         */
856 /* somewhere in the GC_call_with_stack_base frame.  This often can      */
857 /* be used to provide a sufficiently accurate stack base.  And we       */
858 /* implement it everywhere.                                             */
859 GC_API void * GC_call_with_stack_base(GC_stack_base_func fn, void *arg);
860
861 /* Register the current thread, with the indicated stack base, as       */
862 /* a new thread whose stack(s) should be traced by the GC.  If a        */
863 /* platform does not implicitly do so, this must be called before a     */
864 /* thread can allocate garbage collected memory, or assign pointers     */
865 /* to the garbage collected heap.  Once registered, a thread will be    */
866 /* stopped during garbage collections.                                  */
867 /* Return codes:        */
868 #define GC_SUCCESS 0
869 #define GC_DUPLICATE 1  /* Was already registered.      */
870 #define GC_NO_THREADS 2 /* No thread support in GC.     */
871 #define GC_UNIMPLEMENTED 3      /* Not yet implemented on this platform. */
872 GC_API int GC_register_my_thread(struct GC_stack_base *);
873
874 /* Unregister the current thread.  The thread may no longer allocate    */
875 /* garbage collected memory or manipulate pointers to the               */
876 /* garbage collected heap after making this call.                       */
877 /* Specifically, if it wants to return or otherwise communicate a       */
878 /* pointer to the garbage-collected heap to another thread, it must     */
879 /* do this before calling GC_unregister_my_thread, most probably        */
880 /* by saving it in a global data structure.                             */
881 GC_API int GC_unregister_my_thread(void);
882
883 /* Attempt to fill in the GC_stack_base structure with the stack base   */
884 /* for this thread.  This appears to be required to implement anything  */
885 /* like the JNI AttachCurrentThread in an environment in which new      */
886 /* threads are not automatically registered with the collector.         */
887 /* It is also unfortunately hard to implement well on many platforms.   */
888 /* Returns GC_SUCCESS or GC_UNIMPLEMENTED.                              */
889 GC_API int GC_get_stack_base(struct GC_stack_base *);
890
891 /* The following routines are primarily intended for use with a         */
892 /* preprocessor which inserts calls to check C pointer arithmetic.      */
893 /* They indicate failure by invoking the corresponding _print_proc.     */
894
895 /* Check that p and q point to the same object.                 */
896 /* Fail conspicuously if they don't.                            */
897 /* Returns the first argument.                                  */
898 /* Succeeds if neither p nor q points to the heap.              */
899 /* May succeed if both p and q point to between heap objects.   */
900 GC_API void * GC_same_obj (void * p, void * q);
901
902 /* Checked pointer pre- and post- increment operations.  Note that      */
903 /* the second argument is in units of bytes, not multiples of the       */
904 /* object size.  This should either be invoked from a macro, or the     */
905 /* call should be automatically generated.                              */
906 GC_API void * GC_pre_incr (void * *p, size_t how_much);
907 GC_API void * GC_post_incr (void * *p, size_t how_much);
908
909 /* Check that p is visible                                              */
910 /* to the collector as a possibly pointer containing location.          */
911 /* If it isn't fail conspicuously.                                      */
912 /* Returns the argument in all cases.  May erroneously succeed          */
913 /* in hard cases.  (This is intended for debugging use with             */
914 /* untyped allocations.  The idea is that it should be possible, though */
915 /* slow, to add such a call to all indirect pointer stores.)            */
916 /* Currently useless for multithreaded worlds.                          */
917 GC_API void * GC_is_visible (void * p);
918
919 /* Check that if p is a pointer to a heap page, then it points to       */
920 /* a valid displacement within a heap object.                           */
921 /* Fail conspicuously if this property does not hold.                   */
922 /* Uninteresting with GC_all_interior_pointers.                         */
923 /* Always returns its argument.                                         */
924 GC_API void * GC_is_valid_displacement (void *  p);
925
926 /* Explicitly dump the GC state.  This is most often called from the    */
927 /* debugger, or by setting the GC_DUMP_REGULARLY environment variable,  */
928 /* but it may be useful to call it from client code during debugging.   */
929 void GC_dump(void);
930
931 /* Safer, but slow, pointer addition.  Probably useful mainly with      */
932 /* a preprocessor.  Useful only for heap pointers.                      */
933 #ifdef GC_DEBUG
934 #   define GC_PTR_ADD3(x, n, type_of_result) \
935         ((type_of_result)GC_same_obj((x)+(n), (x)))
936 #   define GC_PRE_INCR3(x, n, type_of_result) \
937         ((type_of_result)GC_pre_incr(&(x), (n)*sizeof(*x))
938 #   define GC_POST_INCR2(x, type_of_result) \
939         ((type_of_result)GC_post_incr(&(x), sizeof(*x))
940 #   ifdef __GNUC__
941 #       define GC_PTR_ADD(x, n) \
942             GC_PTR_ADD3(x, n, typeof(x))
943 #       define GC_PRE_INCR(x, n) \
944             GC_PRE_INCR3(x, n, typeof(x))
945 #       define GC_POST_INCR(x, n) \
946             GC_POST_INCR3(x, typeof(x))
947 #   else
948         /* We can't do this right without typeof, which ANSI    */
949         /* decided was not sufficiently useful.  Repeatedly     */
950         /* mentioning the arguments seems too dangerous to be   */
951         /* useful.  So does not casting the result.             */
952 #       define GC_PTR_ADD(x, n) ((x)+(n))
953 #   endif
954 #else   /* !GC_DEBUG */
955 #   define GC_PTR_ADD3(x, n, type_of_result) ((x)+(n))
956 #   define GC_PTR_ADD(x, n) ((x)+(n))
957 #   define GC_PRE_INCR3(x, n, type_of_result) ((x) += (n))
958 #   define GC_PRE_INCR(x, n) ((x) += (n))
959 #   define GC_POST_INCR2(x, n, type_of_result) ((x)++)
960 #   define GC_POST_INCR(x, n) ((x)++)
961 #endif
962
963 /* Safer assignment of a pointer to a nonstack location.        */
964 #ifdef GC_DEBUG
965 #   define GC_PTR_STORE(p, q) \
966         (*(void **)GC_is_visible(p) = GC_is_valid_displacement(q))
967 #else /* !GC_DEBUG */
968 #   define GC_PTR_STORE(p, q) (*(p) = (q))
969 #endif
970
971 /* Functions called to report pointer checking errors */
972 GC_API void (*GC_same_obj_print_proc) (void * p, void * q);
973
974 GC_API void (*GC_is_valid_displacement_print_proc) (void * p);
975
976 GC_API void (*GC_is_visible_print_proc) (void * p);
977
978
979 /* For pthread support, we generally need to intercept a number of      */
980 /* thread library calls.  We do that here by macro defining them.       */
981
982 #if !defined(GC_USE_LD_WRAP) && !defined(GC_NO_THREAD_REDIRECTS) \
983     && defined(GC_PTHREADS)
984 # include "gc_pthread_redirects.h"
985 #endif
986
987 # if defined(PCR) || defined(GC_SOLARIS_THREADS) || \
988      defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
989         /* Any flavor of threads.       */
990 /* This returns a list of objects, linked through their first           */
991 /* word.  Its use can greatly reduce lock contention problems, since    */
992 /* the allocation lock can be acquired and released many fewer times.   */
993 /* It is used internally by gc_local_alloc.h, which provides a simpler  */
994 /* programming interface on Linux.                                      */
995 void * GC_malloc_many(size_t lb);
996 #define GC_NEXT(p) (*(void * *)(p))     /* Retrieve the next element    */
997                                         /* in returned list.            */
998
999 #endif /* THREADS */
1000
1001 /* Register a callback to control the scanning of dynamic libraries.
1002    When the GC scans the static data of a dynamic library, it will
1003    first call a user-supplied routine with filename of the library and
1004    the address and length of the memory region.  This routine should
1005    return nonzero if that region should be scanned.  */
1006 GC_API void 
1007 GC_register_has_static_roots_callback
1008   (int (*callback)(const char *, void *, size_t));
1009
1010
1011 #if defined(GC_WIN32_THREADS) && !defined(__CYGWIN32__) \
1012         && !defined(__CYGWIN__) \
1013         && !defined(GC_PTHREADS)
1014
1015 #ifdef __cplusplus
1016     }  /* Including windows.h in an extern "C" context no longer works. */
1017 #endif
1018
1019 #ifndef GC_NO_THREAD_DECLS
1020 # include <windows.h>
1021
1022 #ifdef __cplusplus
1023     extern "C" {
1024 #endif
1025   /*
1026    * All threads must be created using GC_CreateThread or GC_beginthreadex,
1027    * or must explicitly call GC_register_my_thread,
1028    * so that they will be recorded in the thread table.
1029    * For backwards compatibility, it is possible to build the GC
1030    * with GC_DLL defined, and to call GC_use_DllMain().
1031    * This implicitly registers all created threads, but appears to be
1032    * less robust.
1033    *
1034    * Currently the collector expects all threads to fall through and
1035    * terminate normally, or call GC_endthreadex() or GC_ExitThread,
1036    * so that the thread is properly unregistered.  (An explicit call
1037    * to GC_unregister_my_thread() should also work, but risks unregistering
1038    * the thread twice.)
1039    */
1040    GC_API HANDLE WINAPI GC_CreateThread(
1041       LPSECURITY_ATTRIBUTES lpThreadAttributes,
1042       DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress,
1043       LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId );
1044
1045 #  if defined(_MSC_VER) && _MSC_VER >= 1200 && !defined(_UINTPTR_T_DEFINED)
1046      typedef unsigned long uintptr_t;
1047 #  endif
1048
1049    GC_API uintptr_t GC_beginthreadex(
1050      void *security, unsigned stack_size,
1051      unsigned ( __stdcall *start_address )( void * ),
1052      void *arglist, unsigned initflag, unsigned *thrdaddr);
1053
1054    GC_API void GC_endthreadex(unsigned retval);
1055
1056    GC_API void WINAPI GC_ExitThread(DWORD dwExitCode);
1057
1058 # if defined(_WIN32_WCE)
1059   /*
1060    * win32_threads.c implements the real WinMain, which will start a new thread
1061    * to call GC_WinMain after initializing the garbage collector.
1062    */
1063   GC_API int WINAPI GC_WinMain(
1064       HINSTANCE hInstance,
1065       HINSTANCE hPrevInstance,
1066       LPWSTR lpCmdLine,
1067       int nCmdShow );
1068 #  ifndef GC_BUILD
1069 #    define WinMain GC_WinMain
1070 #  endif
1071 # endif /* defined(_WIN32_WCE) */
1072 #endif /* !GC_NO_THREAD_DECLS */
1073
1074   /*
1075    * Use implicit thread registration via DllMain.
1076    * Must be called before GC_INIT and other GC routines.
1077    * Should be avoided if GC_beginthreadex and friends can be called
1078    * instead.
1079    */
1080 GC_API void GC_use_DllMain(void);
1081
1082 # ifndef GC_NO_THREAD_REDIRECTS
1083 #   define CreateThread GC_CreateThread
1084 #   define ExitThread GC_ExitThread
1085 #   define _beginthreadex GC_beginthreadex
1086 #   define _endthreadex GC_endthreadex
1087 #   define _beginthread { > "Please use _beginthreadex instead of _beginthread" < }
1088 # endif /* !GC_NO_THREAD_REDIRECTS */
1089
1090 #endif /* defined(GC_WIN32_THREADS)  && !cygwin */
1091
1092  /*
1093   * Fully portable code should call GC_INIT() from the main program
1094   * before making any other GC_ calls.  On most platforms this is a
1095   * no-op and the collector self-initializes.  But a number of platforms
1096   * make that too hard.
1097   * A GC_INIT call is required if the collector is built with THREAD_LOCAL_ALLOC
1098   * defined and the initial allocation call is not to GC_malloc() or
1099   * GC_malloc_atomic().
1100   */
1101 #if defined(__CYGWIN32__) || defined (_AIX)
1102     /*
1103      * Similarly gnu-win32 DLLs need explicit initialization from
1104      * the main program, as does AIX.
1105      */
1106 #   ifdef __CYGWIN32__
1107       extern int _data_start__[];
1108       extern int _data_end__[];
1109       extern int _bss_start__[];
1110       extern int _bss_end__[];
1111 #     define GC_MAX(x,y) ((x) > (y) ? (x) : (y))
1112 #     define GC_MIN(x,y) ((x) < (y) ? (x) : (y))
1113 #     define GC_DATASTART ((void *) GC_MIN(_data_start__, _bss_start__))
1114 #     define GC_DATAEND  ((void *) GC_MAX(_data_end__, _bss_end__))
1115 #     define GC_INIT() { GC_add_roots(GC_DATASTART, GC_DATAEND); \
1116                            GC_gcollect(); /* For blacklisting. */}
1117         /* Required at least if GC is in dll.  And doesn't hurt. */
1118 #   endif
1119 #   if defined(_AIX)
1120       extern int _data[], _end[];
1121 #     define GC_DATASTART ((void *)((ulong)_data))
1122 #     define GC_DATAEND ((void *)((ulong)_end))
1123 #     define GC_INIT() { GC_add_roots(GC_DATASTART, GC_DATAEND); }
1124 #   endif
1125 #else
1126 #   define GC_INIT() { GC_init(); }
1127 #endif
1128
1129 #if !defined(_WIN32_WCE) \
1130     && ((defined(_MSDOS) || defined(_MSC_VER)) && (_M_IX86 >= 300) \
1131         || defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__))
1132   /* win32S may not free all resources on process exit.  */
1133   /* This explicitly deallocates the heap.               */
1134     GC_API void GC_win32_free_heap ();
1135 #endif
1136
1137 #if ( defined(_AMIGA) && !defined(GC_AMIGA_MAKINGLIB) )
1138   /* Allocation really goes through GC_amiga_allocwrapper_do */
1139 # include "gc_amiga_redirects.h"
1140 #endif
1141
1142 #if defined(GC_REDIRECT_TO_LOCAL)
1143   /* Now redundant; that's the default with THREAD_LOCAL_ALLOC */
1144 #endif
1145
1146 #ifdef __cplusplus
1147     }  /* end of extern "C" */
1148 #endif
1149
1150 #endif /* _GC_H */