OSDN Git Service

* Makefile.am, acinclude.m4, configure.in: Imported GC 6.0 and
[pf3gnuchains/gcc-fork.git] / boehm-gc / 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  *
7  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
9  *
10  * Permission is hereby granted to use or copy this program
11  * for any purpose,  provided the above notices are retained on all copies.
12  * Permission to modify the code and to distribute modified code is granted,
13  * provided the above notices are retained, and a notice that the code was
14  * modified is included with the above copyright notice.
15  */
16
17 /*
18  * Note that this defines a large number of tuning hooks, which can
19  * safely be ignored in nearly all cases.  For normal use it suffices
20  * to call only GC_MALLOC and perhaps GC_REALLOC.
21  * For better performance, also look at GC_MALLOC_ATOMIC, and
22  * GC_enable_incremental.  If you need an action to be performed
23  * immediately before an object is collected, look at GC_register_finalizer.
24  * If you are using Solaris threads, look at the end of this file.
25  * Everything else is best ignored unless you encounter performance
26  * problems.
27  */
28  
29 #ifndef _GC_H
30
31 # define _GC_H
32
33 #if defined(_SOLARIS_PTHREADS) && !defined(SOLARIS_THREADS)
34 #   define SOLARIS_THREADS
35 #endif
36
37 /*
38  * Some tests for old macros.  These violate our namespace rules and will
39  * disappear shortly.
40  */
41 #if defined(SOLARIS_THREADS) || defined(_SOLARIS_THREADS)
42 # define GC_SOLARIS_THREADS
43 #endif
44 #if defined(_SOLARIS_PTHREADS)
45 # define GC_SOLARIS_PTHREADS
46 #endif
47 #if defined(IRIX_THREADS)
48 # define GC_IRIX_THREADS
49 #endif
50 #if defined(HPUX_THREADS)
51 # define GC_HPUX_THREADS
52 #endif
53 #if defined(OSF1_THREADS)
54 # define GC_OSF1_THREADS
55 #endif
56 #if defined(LINUX_THREADS)
57 # define GC_LINUX_THREADS
58 #endif
59 #if defined(WIN32_THREADS)
60 # define GC_WIN32_THREADS
61 #endif
62 #if defined(USE_LD_WRAP)
63 # define GC_USE_LD_WRAP
64 #endif
65
66 #if !defined(_REENTRANT) && (defined(GC_SOLARIS_THREADS) \
67                              || defined(GC_SOLARIS_PTHREADS) \
68                              || defined(GC_HPUX_THREADS) \
69                              || defined(GC_LINUX_THREADS))
70 # define _REENTRANT
71         /* Better late than never.  This fails if system headers that   */
72         /* depend on this were previously included.                     */
73 #endif
74
75 # define __GC
76 # include <stddef.h>
77 # ifdef _WIN32_WCE
78 /* Yet more kluges for WinCE */
79 #   include <stdlib.h>          /* size_t is defined here */
80     typedef long ptrdiff_t;     /* ptrdiff_t is not defined */
81 # endif
82
83 #if defined(__MINGW32__) && defined(WIN32_THREADS)
84 # ifdef GC_BUILD
85 #   define GC_API __declspec(dllexport)
86 # else
87 #   define GC_API __declspec(dllimport)
88 # endif
89 #endif
90
91 #if (defined(__DMC__) || defined(_MSC_VER)) \
92                 && (defined(_DLL) && !defined(GC_NOT_DLL) \
93                     || defined(GC_DLL))
94 # ifdef GC_BUILD
95 #   define GC_API extern __declspec(dllexport)
96 # else
97 #   define GC_API __declspec(dllimport)
98 # endif
99 #endif
100
101 #if defined(__WATCOMC__) && defined(GC_DLL)
102 # ifdef GC_BUILD
103 #   define GC_API extern __declspec(dllexport)
104 # else
105 #   define GC_API extern __declspec(dllimport)
106 # endif
107 #endif
108
109 #ifndef GC_API
110 #define GC_API extern
111 #endif
112
113 # if defined(__STDC__) || defined(__cplusplus)
114 #   define GC_PROTO(args) args
115     typedef void * GC_PTR;
116 #   define GC_CONST const
117 # else
118 #   define GC_PROTO(args) ()
119     typedef char * GC_PTR;
120 #   define GC_CONST
121 #  endif
122
123 # ifdef __cplusplus
124     extern "C" {
125 # endif
126
127
128 /* Define word and signed_word to be unsigned and signed types of the   */
129 /* size as char * or void *.  There seems to be no way to do this       */
130 /* even semi-portably.  The following is probably no better/worse       */
131 /* than almost anything else.                                           */
132 /* The ANSI standard suggests that size_t and ptr_diff_t might be       */
133 /* better choices.  But those appear to have incorrect definitions      */
134 /* on may systems.  Notably "typedef int size_t" seems to be both       */
135 /* frequent and WRONG.                                                  */
136 typedef unsigned long GC_word;
137 typedef long GC_signed_word;
138
139 /* Public read-only variables */
140
141 GC_API GC_word GC_gc_no;/* Counter incremented per collection.          */
142                         /* Includes empty GCs at startup.               */
143
144 GC_API int GC_parallel; /* GC is parallelized for performance on        */
145                         /* multiprocessors.  Currently set only         */
146                         /* implicitly if collector is built with        */
147                         /* -DPARALLEL_MARK and if either:               */
148                         /*  Env variable GC_NPROC is set to > 1, or     */
149                         /*  GC_NPROC is not set and this is an MP.      */
150                         /* If GC_parallel is set, incremental           */
151                         /* collection is aonly partially functional,    */
152                         /* and may not be desirable.                    */
153                         
154
155 /* Public R/W variables */
156
157 GC_API GC_PTR (*GC_oom_fn) GC_PROTO((size_t bytes_requested));
158                         /* When there is insufficient memory to satisfy */
159                         /* an allocation request, we return             */
160                         /* (*GC_oom_fn)().  By default this just        */
161                         /* returns 0.                                   */
162                         /* If it returns, it must return 0 or a valid   */
163                         /* pointer to a previously allocated heap       */
164                         /* object.                                      */
165
166 GC_API int GC_find_leak;
167                         /* Do not actually garbage collect, but simply  */
168                         /* report inaccessible memory that was not      */
169                         /* deallocated with GC_free.  Initial value     */
170                         /* is determined by FIND_LEAK macro.            */
171
172 GC_API int GC_all_interior_pointers;
173                         /* Arrange for pointers to object interiors to  */
174                         /* be recognized as valid.  May not be changed  */
175                         /* after GC initialization.                     */
176                         /* Initial value is determined by               */
177                         /* -DALL_INTERIOR_POINTERS.                     */
178                         /* Unless DONT_ADD_BYTE_AT_END is defined, this */
179                         /* also affects whether sizes are increased by  */
180                         /* at least a byte to allow "off the end"       */
181                         /* pointer recognition.                         */
182                         /* MUST BE 0 or 1.                              */
183
184 GC_API int GC_quiet;    /* Disable statistics output.  Only matters if  */
185                         /* collector has been compiled with statistics  */
186                         /* enabled.  This involves a performance cost,  */
187                         /* and is thus not the default.                 */
188
189 GC_API int GC_finalize_on_demand;
190                         /* If nonzero, finalizers will only be run in   */
191                         /* response to an explicit GC_invoke_finalizers */
192                         /* call.  The default is determined by whether  */
193                         /* the FINALIZE_ON_DEMAND macro is defined      */
194                         /* when the collector is built.                 */
195
196 GC_API int GC_java_finalization;
197                         /* Mark objects reachable from finalizable      */
198                         /* objects in a separate postpass.  This makes  */
199                         /* it a bit safer to use non-topologically-     */
200                         /* ordered finalization.  Default value is      */
201                         /* determined by JAVA_FINALIZATION macro.       */
202
203 GC_API void (* GC_finalizer_notifier)();
204                         /* Invoked by the collector when there are      */
205                         /* objects to be finalized.  Invoked at most    */
206                         /* once per GC cycle.  Never invoked unless     */
207                         /* GC_finalize_on_demand is set.                */
208                         /* Typically this will notify a finalization    */
209                         /* thread, which will call GC_invoke_finalizers */
210                         /* in response.                                 */
211
212 GC_API int GC_dont_gc;  /* Dont collect unless explicitly requested, e.g. */
213                         /* because it's not safe.                         */
214
215 GC_API int GC_dont_expand;
216                         /* Dont expand heap unless explicitly requested */
217                         /* or forced to.                                */
218
219 GC_API int GC_use_entire_heap;
220                 /* Causes the nonincremental collector to use the       */
221                 /* entire heap before collecting.  This was the only    */
222                 /* option for GC versions < 5.0.  This sometimes        */
223                 /* results in more large block fragmentation, since     */
224                 /* very larg blocks will tend to get broken up          */
225                 /* during each GC cycle.  It is likely to result in a   */
226                 /* larger working set, but lower collection             */
227                 /* frequencies, and hence fewer instructions executed   */
228                 /* in the collector.                                    */
229
230 GC_API int GC_full_freq;    /* Number of partial collections between    */
231                             /* full collections.  Matters only if       */
232                             /* GC_incremental is set.                   */
233                             /* Full collections are also triggered if   */
234                             /* the collector detects a substantial      */
235                             /* increase in the number of in-use heap    */
236                             /* blocks.  Values in the tens are now      */
237                             /* perfectly reasonable, unlike for         */
238                             /* earlier GC versions.                     */
239                         
240 GC_API GC_word GC_non_gc_bytes;
241                         /* Bytes not considered candidates for collection. */
242                         /* Used only to control scheduling of collections. */
243                         /* Updated by GC_malloc_uncollectable and GC_free. */
244                         /* Wizards only.                                   */
245
246 GC_API int GC_no_dls;
247                         /* Don't register dynamic library data segments. */
248                         /* Wizards only.  Should be used only if the     */
249                         /* application explicitly registers all roots.   */
250                         /* In Microsoft Windows environments, this will  */
251                         /* usually also prevent registration of the      */
252                         /* main data segment as part of the root set.    */
253
254 GC_API GC_word GC_free_space_divisor;
255                         /* We try to make sure that we allocate at      */
256                         /* least N/GC_free_space_divisor bytes between  */
257                         /* collections, where N is the heap size plus   */
258                         /* a rough estimate of the root set size.       */
259                         /* Initially, GC_free_space_divisor = 4.        */
260                         /* Increasing its value will use less space     */
261                         /* but more collection time.  Decreasing it     */
262                         /* will appreciably decrease collection time    */
263                         /* at the expense of space.                     */
264                         /* GC_free_space_divisor = 1 will effectively   */
265                         /* disable collections.                         */
266
267 GC_API GC_word GC_max_retries;
268                         /* The maximum number of GCs attempted before   */
269                         /* reporting out of memory after heap           */
270                         /* expansion fails.  Initially 0.               */
271                         
272
273 GC_API char *GC_stackbottom;    /* Cool end of user stack.              */
274                                 /* May be set in the client prior to    */
275                                 /* calling any GC_ routines.  This      */
276                                 /* avoids some overhead, and            */
277                                 /* potentially some signals that can    */
278                                 /* confuse debuggers.  Otherwise the    */
279                                 /* collector attempts to set it         */
280                                 /* automatically.                       */
281                                 /* For multithreaded code, this is the  */
282                                 /* cold end of the stack for the        */
283                                 /* primordial thread.                   */      
284                                 
285 GC_API int GC_dont_precollect;  /* Don't collect as part of             */
286                                 /* initialization.  Should be set only  */
287                                 /* if the client wants a chance to      */
288                                 /* manually initialize the root set     */
289                                 /* before the first collection.         */
290                                 /* Interferes with blacklisting.        */
291                                 /* Wizards only.                        */
292
293 /* Public procedures */
294 /*
295  * general purpose allocation routines, with roughly malloc calling conv.
296  * The atomic versions promise that no relevant pointers are contained
297  * in the object.  The nonatomic versions guarantee that the new object
298  * is cleared.  GC_malloc_stubborn promises that no changes to the object
299  * will occur after GC_end_stubborn_change has been called on the
300  * result of GC_malloc_stubborn. GC_malloc_uncollectable allocates an object
301  * that is scanned for pointers to collectable objects, but is not itself
302  * collectable.  The object is scanned even if it does not appear to
303  * be reachable.  GC_malloc_uncollectable and GC_free called on the resulting
304  * object implicitly update GC_non_gc_bytes appropriately.
305  *
306  * Note that the GC_malloc_stubborn support is stubbed out by default
307  * starting in 6.0.  GC_malloc_stubborn is an alias for GC_malloc unless
308  * the collector is built with STUBBORN_ALLOC defined.
309  */
310 GC_API GC_PTR GC_malloc GC_PROTO((size_t size_in_bytes));
311 GC_API GC_PTR GC_malloc_atomic GC_PROTO((size_t size_in_bytes));
312 GC_API GC_PTR GC_malloc_uncollectable GC_PROTO((size_t size_in_bytes));
313 GC_API GC_PTR GC_malloc_stubborn GC_PROTO((size_t size_in_bytes));
314
315 /* The following is only defined if the library has been suitably       */
316 /* compiled:                                                            */
317 GC_API GC_PTR GC_malloc_atomic_uncollectable GC_PROTO((size_t size_in_bytes));
318
319 /* Explicitly deallocate an object.  Dangerous if used incorrectly.     */
320 /* Requires a pointer to the base of an object.                         */
321 /* If the argument is stubborn, it should not be changeable when freed. */
322 /* An object should not be enable for finalization when it is           */
323 /* explicitly deallocated.                                              */
324 /* GC_free(0) is a no-op, as required by ANSI C for free.               */
325 GC_API void GC_free GC_PROTO((GC_PTR object_addr));
326
327 /*
328  * Stubborn objects may be changed only if the collector is explicitly informed.
329  * The collector is implicitly informed of coming change when such
330  * an object is first allocated.  The following routines inform the
331  * collector that an object will no longer be changed, or that it will
332  * once again be changed.  Only nonNIL pointer stores into the object
333  * are considered to be changes.  The argument to GC_end_stubborn_change
334  * must be exacly the value returned by GC_malloc_stubborn or passed to
335  * GC_change_stubborn.  (In the second case it may be an interior pointer
336  * within 512 bytes of the beginning of the objects.)
337  * There is a performance penalty for allowing more than
338  * one stubborn object to be changed at once, but it is acceptable to
339  * do so.  The same applies to dropping stubborn objects that are still
340  * changeable.
341  */
342 GC_API void GC_change_stubborn GC_PROTO((GC_PTR));
343 GC_API void GC_end_stubborn_change GC_PROTO((GC_PTR));
344
345 /* Return a pointer to the base (lowest address) of an object given     */
346 /* a pointer to a location within the object.                           */
347 /* I.e. map an interior pointer to the corresponding bas pointer.       */
348 /* Note that with debugging allocation, this returns a pointer to the   */
349 /* actual base of the object, i.e. the debug information, not to        */
350 /* the base of the user object.                                         */
351 /* Return 0 if displaced_pointer doesn't point to within a valid        */
352 /* object.                                                              */
353 GC_API GC_PTR GC_base GC_PROTO((GC_PTR displaced_pointer));
354
355 /* Given a pointer to the base of an object, return its size in bytes.  */
356 /* The returned size may be slightly larger than what was originally    */
357 /* requested.                                                           */
358 GC_API size_t GC_size GC_PROTO((GC_PTR object_addr));
359
360 /* For compatibility with C library.  This is occasionally faster than  */
361 /* a malloc followed by a bcopy.  But if you rely on that, either here  */
362 /* or with the standard C library, your code is broken.  In my          */
363 /* opinion, it shouldn't have been invented, but now we're stuck. -HB   */
364 /* The resulting object has the same kind as the original.              */
365 /* If the argument is stubborn, the result will have changes enabled.   */
366 /* It is an error to have changes enabled for the original object.      */
367 /* Follows ANSI comventions for NULL old_object.                        */
368 GC_API GC_PTR GC_realloc
369         GC_PROTO((GC_PTR old_object, size_t new_size_in_bytes));
370                                    
371 /* Explicitly increase the heap size.   */
372 /* Returns 0 on failure, 1 on success.  */
373 GC_API int GC_expand_hp GC_PROTO((size_t number_of_bytes));
374
375 /* Limit the heap size to n bytes.  Useful when you're debugging,       */
376 /* especially on systems that don't handle running out of memory well.  */
377 /* n == 0 ==> unbounded.  This is the default.                          */
378 GC_API void GC_set_max_heap_size GC_PROTO((GC_word n));
379
380 /* Inform the collector that a certain section of statically allocated  */
381 /* memory contains no pointers to garbage collected memory.  Thus it    */
382 /* need not be scanned.  This is sometimes important if the application */
383 /* maps large read/write files into the address space, which could be   */
384 /* mistaken for dynamic library data segments on some systems.          */
385 GC_API void GC_exclude_static_roots GC_PROTO((GC_PTR start, GC_PTR finish));
386
387 /* Clear the set of root segments.  Wizards only. */
388 GC_API void GC_clear_roots GC_PROTO((void));
389
390 /* Add a root segment.  Wizards only. */
391 GC_API void GC_add_roots GC_PROTO((char * low_address,
392                                    char * high_address_plus_1));
393
394 /* Add a displacement to the set of those considered valid by the       */
395 /* collector.  GC_register_displacement(n) means that if p was returned */
396 /* by GC_malloc, then (char *)p + n will be considered to be a valid    */
397 /* pointer to n.  N must be small and less than the size of p.          */
398 /* (All pointers to the interior of objects from the stack are          */
399 /* considered valid in any case.  This applies to heap objects and      */
400 /* static data.)                                                        */
401 /* Preferably, this should be called before any other GC procedures.    */
402 /* Calling it later adds to the probability of excess memory            */
403 /* retention.                                                           */
404 /* This is a no-op if the collector was compiled with recognition of    */
405 /* arbitrary interior pointers enabled, which is now the default.       */
406 GC_API void GC_register_displacement GC_PROTO((GC_word n));
407
408 /* The following version should be used if any debugging allocation is  */
409 /* being done.                                                          */
410 GC_API void GC_debug_register_displacement GC_PROTO((GC_word n));
411
412 /* Explicitly trigger a full, world-stop collection.    */
413 GC_API void GC_gcollect GC_PROTO((void));
414
415 /* Trigger a full world-stopped collection.  Abort the collection if    */
416 /* and when stop_func returns a nonzero value.  Stop_func will be       */
417 /* called frequently, and should be reasonably fast.  This works even   */
418 /* if virtual dirty bits, and hence incremental collection is not       */
419 /* available for this architecture.  Collections can be aborted faster  */
420 /* than normal pause times for incremental collection.  However,        */
421 /* aborted collections do no useful work; the next collection needs     */
422 /* to start from the beginning.                                         */
423 /* Return 0 if the collection was aborted, 1 if it succeeded.           */
424 typedef int (* GC_stop_func) GC_PROTO((void));
425 GC_API int GC_try_to_collect GC_PROTO((GC_stop_func stop_func));
426
427 /* Return the number of bytes in the heap.  Excludes collector private  */
428 /* data structures.  Includes empty blocks and fragmentation loss.      */
429 /* Includes some pages that were allocated but never written.           */
430 GC_API size_t GC_get_heap_size GC_PROTO((void));
431
432 /* Return a lower bound on the number of free bytes in the heap.        */
433 GC_API size_t GC_get_free_bytes GC_PROTO((void));
434
435 /* Return the number of bytes allocated since the last collection.      */
436 GC_API size_t GC_get_bytes_since_gc GC_PROTO((void));
437
438 /* Return the total number of bytes allocated in this process.          */
439 /* Never decreases.                                                     */
440 GC_API size_t GC_get_total_bytes GC_PROTO((void));
441
442 /* Enable incremental/generational collection.  */
443 /* Not advisable unless dirty bits are          */
444 /* available or most heap objects are           */
445 /* pointerfree(atomic) or immutable.            */
446 /* Don't use in leak finding mode.              */
447 /* Ignored if GC_dont_gc is true.               */
448 /* Only the generational piece of this is       */
449 /* functional if GC_parallel is TRUE.           */
450 GC_API void GC_enable_incremental GC_PROTO((void));
451
452 /* Perform some garbage collection work, if appropriate.        */
453 /* Return 0 if there is no more work to be done.                */
454 /* Typically performs an amount of work corresponding roughly   */
455 /* to marking from one page.  May do more work if further       */
456 /* progress requires it, e.g. if incremental collection is      */
457 /* disabled.  It is reasonable to call this in a wait loop      */
458 /* until it returns 0.                                          */
459 GC_API int GC_collect_a_little GC_PROTO((void));
460
461 /* Allocate an object of size lb bytes.  The client guarantees that     */
462 /* as long as the object is live, it will be referenced by a pointer    */
463 /* that points to somewhere within the first 256 bytes of the object.   */
464 /* (This should normally be declared volatile to prevent the compiler   */
465 /* from invalidating this assertion.)  This routine is only useful      */
466 /* if a large array is being allocated.  It reduces the chance of       */
467 /* accidentally retaining such an array as a result of scanning an      */
468 /* integer that happens to be an address inside the array.  (Actually,  */
469 /* it reduces the chance of the allocator not finding space for such    */
470 /* an array, since it will try hard to avoid introducing such a false   */
471 /* reference.)  On a SunOS 4.X or MS Windows system this is recommended */
472 /* for arrays likely to be larger than 100K or so.  For other systems,  */
473 /* or if the collector is not configured to recognize all interior      */
474 /* pointers, the threshold is normally much higher.                     */
475 GC_API GC_PTR GC_malloc_ignore_off_page GC_PROTO((size_t lb));
476 GC_API GC_PTR GC_malloc_atomic_ignore_off_page GC_PROTO((size_t lb));
477
478 #if defined(__sgi) && !defined(__GNUC__) && _COMPILER_VERSION >= 720
479 #   define GC_ADD_CALLER
480 #   define GC_RETURN_ADDR (GC_word)__return_address
481 #endif
482
483 #ifdef GC_ADD_CALLER
484 #  define GC_EXTRAS GC_RETURN_ADDR, __FILE__, __LINE__
485 #  define GC_EXTRA_PARAMS GC_word ra, GC_CONST char * s, int i
486 #else
487 #  define GC_EXTRAS __FILE__, __LINE__
488 #  define GC_EXTRA_PARAMS GC_CONST char * s, int i
489 #endif
490
491 /* Debugging (annotated) allocation.  GC_gcollect will check            */
492 /* objects allocated in this way for overwrites, etc.                   */
493 GC_API GC_PTR GC_debug_malloc
494         GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
495 GC_API GC_PTR GC_debug_malloc_atomic
496         GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
497 GC_API GC_PTR GC_debug_malloc_uncollectable
498         GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
499 GC_API GC_PTR GC_debug_malloc_stubborn
500         GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
501 GC_API void GC_debug_free GC_PROTO((GC_PTR object_addr));
502 GC_API GC_PTR GC_debug_realloc
503         GC_PROTO((GC_PTR old_object, size_t new_size_in_bytes,
504                   GC_EXTRA_PARAMS));
505                                  
506 GC_API void GC_debug_change_stubborn GC_PROTO((GC_PTR));
507 GC_API void GC_debug_end_stubborn_change GC_PROTO((GC_PTR));
508 # ifdef GC_DEBUG
509 #   define GC_MALLOC(sz) GC_debug_malloc(sz, GC_EXTRAS)
510 #   define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, GC_EXTRAS)
511 #   define GC_MALLOC_UNCOLLECTABLE(sz) GC_debug_malloc_uncollectable(sz, \
512                                                         GC_EXTRAS)
513 #   define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, GC_EXTRAS)
514 #   define GC_FREE(p) GC_debug_free(p)
515 #   define GC_REGISTER_FINALIZER(p, f, d, of, od) \
516         GC_debug_register_finalizer(p, f, d, of, od)
517 #   define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
518         GC_debug_register_finalizer_ignore_self(p, f, d, of, od)
519 #   define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
520         GC_debug_register_finalizer_no_order(p, f, d, of, od)
521 #   define GC_MALLOC_STUBBORN(sz) GC_debug_malloc_stubborn(sz, GC_EXTRAS);
522 #   define GC_CHANGE_STUBBORN(p) GC_debug_change_stubborn(p)
523 #   define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p)
524 #   define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
525         GC_general_register_disappearing_link(link, GC_base(obj))
526 #   define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n)
527 # else
528 #   define GC_MALLOC(sz) GC_malloc(sz)
529 #   define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz)
530 #   define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz)
531 #   define GC_REALLOC(old, sz) GC_realloc(old, sz)
532 #   define GC_FREE(p) GC_free(p)
533 #   define GC_REGISTER_FINALIZER(p, f, d, of, od) \
534         GC_register_finalizer(p, f, d, of, od)
535 #   define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
536         GC_register_finalizer_ignore_self(p, f, d, of, od)
537 #   define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
538         GC_register_finalizer_no_order(p, f, d, of, od)
539 #   define GC_MALLOC_STUBBORN(sz) GC_malloc_stubborn(sz)
540 #   define GC_CHANGE_STUBBORN(p) GC_change_stubborn(p)
541 #   define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p)
542 #   define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
543         GC_general_register_disappearing_link(link, obj)
544 #   define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n)
545 # endif
546 /* The following are included because they are often convenient, and    */
547 /* reduce the chance for a misspecifed size argument.  But calls may    */
548 /* expand to something syntactically incorrect if t is a complicated    */
549 /* type expression.                                                     */
550 # define GC_NEW(t) (t *)GC_MALLOC(sizeof (t))
551 # define GC_NEW_ATOMIC(t) (t *)GC_MALLOC_ATOMIC(sizeof (t))
552 # define GC_NEW_STUBBORN(t) (t *)GC_MALLOC_STUBBORN(sizeof (t))
553 # define GC_NEW_UNCOLLECTABLE(t) (t *)GC_MALLOC_UNCOLLECTABLE(sizeof (t))
554
555 /* Finalization.  Some of these primitives are grossly unsafe.          */
556 /* The idea is to make them both cheap, and sufficient to build         */
557 /* a safer layer, closer to PCedar finalization.                        */
558 /* The interface represents my conclusions from a long discussion       */
559 /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes,              */
560 /* Christian Jacobi, and Russ Atkinson.  It's not perfect, and          */
561 /* probably nobody else agrees with it.     Hans-J. Boehm  3/13/92      */
562 typedef void (*GC_finalization_proc)
563         GC_PROTO((GC_PTR obj, GC_PTR client_data));
564
565 GC_API void GC_register_finalizer
566         GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
567                   GC_finalization_proc *ofn, GC_PTR *ocd));
568 GC_API void GC_debug_register_finalizer
569         GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
570                   GC_finalization_proc *ofn, GC_PTR *ocd));
571         /* When obj is no longer accessible, invoke             */
572         /* (*fn)(obj, cd).  If a and b are inaccessible, and    */
573         /* a points to b (after disappearing links have been    */
574         /* made to disappear), then only a will be              */
575         /* finalized.  (If this does not create any new         */
576         /* pointers to b, then b will be finalized after the    */
577         /* next collection.)  Any finalizable object that       */
578         /* is reachable from itself by following one or more    */
579         /* pointers will not be finalized (or collected).       */
580         /* Thus cycles involving finalizable objects should     */
581         /* be avoided, or broken by disappearing links.         */
582         /* All but the last finalizer registered for an object  */
583         /* is ignored.                                          */
584         /* Finalization may be removed by passing 0 as fn.      */
585         /* Finalizers are implicitly unregistered just before   */
586         /* they are invoked.                                    */
587         /* The old finalizer and client data are stored in      */
588         /* *ofn and *ocd.                                       */ 
589         /* Fn is never invoked on an accessible object,         */
590         /* provided hidden pointers are converted to real       */
591         /* pointers only if the allocation lock is held, and    */
592         /* such conversions are not performed by finalization   */
593         /* routines.                                            */
594         /* If GC_register_finalizer is aborted as a result of   */
595         /* a signal, the object may be left with no             */
596         /* finalization, even if neither the old nor new        */
597         /* finalizer were NULL.                                 */
598         /* Obj should be the nonNULL starting address of an     */
599         /* object allocated by GC_malloc or friends.            */
600         /* Note that any garbage collectable object referenced  */
601         /* by cd will be considered accessible until the        */
602         /* finalizer is invoked.                                */
603
604 /* Another versions of the above follow.  It ignores            */
605 /* self-cycles, i.e. pointers from a finalizable object to      */
606 /* itself.  There is a stylistic argument that this is wrong,   */
607 /* but it's unavoidable for C++, since the compiler may         */
608 /* silently introduce these.  It's also benign in that specific */
609 /* case.                                                        */
610 /* Note that cd will still be viewed as accessible, even if it  */
611 /* refers to the object itself.                                 */
612 GC_API void GC_register_finalizer_ignore_self
613         GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
614                   GC_finalization_proc *ofn, GC_PTR *ocd));
615 GC_API void GC_debug_register_finalizer_ignore_self
616         GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
617                   GC_finalization_proc *ofn, GC_PTR *ocd));
618
619 /* Another version of the above.  It ignores all cycles.        */
620 /* It should probably only be used by Java implementations.     */
621 /* Note that cd will still be viewed as accessible, even if it  */
622 /* refers to the object itself.                                 */
623 GC_API void GC_register_finalizer_no_order
624         GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
625                   GC_finalization_proc *ofn, GC_PTR *ocd));
626 GC_API void GC_debug_register_finalizer_no_order
627         GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
628                   GC_finalization_proc *ofn, GC_PTR *ocd));
629
630
631 /* The following routine may be used to break cycles between    */
632 /* finalizable objects, thus causing cyclic finalizable         */
633 /* objects to be finalized in the correct order.  Standard      */
634 /* use involves calling GC_register_disappearing_link(&p),      */
635 /* where p is a pointer that is not followed by finalization    */
636 /* code, and should not be considered in determining            */
637 /* finalization order.                                          */
638 GC_API int GC_register_disappearing_link GC_PROTO((GC_PTR * /* link */));
639         /* Link should point to a field of a heap allocated     */
640         /* object obj.  *link will be cleared when obj is       */
641         /* found to be inaccessible.  This happens BEFORE any   */
642         /* finalization code is invoked, and BEFORE any         */
643         /* decisions about finalization order are made.         */
644         /* This is useful in telling the finalizer that         */
645         /* some pointers are not essential for proper           */
646         /* finalization.  This may avoid finalization cycles.   */
647         /* Note that obj may be resurrected by another          */
648         /* finalizer, and thus the clearing of *link may        */
649         /* be visible to non-finalization code.                 */
650         /* There's an argument that an arbitrary action should  */
651         /* be allowed here, instead of just clearing a pointer. */
652         /* But this causes problems if that action alters, or   */
653         /* examines connectivity.                               */
654         /* Returns 1 if link was already registered, 0          */
655         /* otherwise.                                           */
656         /* Only exists for backward compatibility.  See below:  */
657         
658 GC_API int GC_general_register_disappearing_link
659         GC_PROTO((GC_PTR * /* link */, GC_PTR obj));
660         /* A slight generalization of the above. *link is       */
661         /* cleared when obj first becomes inaccessible.  This   */
662         /* can be used to implement weak pointers easily and    */
663         /* safely. Typically link will point to a location      */
664         /* holding a disguised pointer to obj.  (A pointer      */
665         /* inside an "atomic" object is effectively             */
666         /* disguised.)   In this way soft                       */
667         /* pointers are broken before any object                */
668         /* reachable from them are finalized.  Each link        */
669         /* May be registered only once, i.e. with one obj       */
670         /* value.  This was added after a long email discussion */
671         /* with John Ellis.                                     */
672         /* Obj must be a pointer to the first word of an object */
673         /* we allocated.  It is unsafe to explicitly deallocate */
674         /* the object containing link.  Explicitly deallocating */
675         /* obj may or may not cause link to eventually be       */
676         /* cleared.                                             */
677 GC_API int GC_unregister_disappearing_link GC_PROTO((GC_PTR * /* link */));
678         /* Returns 0 if link was not actually registered.       */
679         /* Undoes a registration by either of the above two     */
680         /* routines.                                            */
681
682 /* Auxiliary fns to make finalization work correctly with displaced     */
683 /* pointers introduced by the debugging allocators.                     */
684 GC_API GC_PTR GC_make_closure GC_PROTO((GC_finalization_proc fn, GC_PTR data));
685 GC_API void GC_debug_invoke_finalizer GC_PROTO((GC_PTR obj, GC_PTR data));
686
687 /* Returns !=0  if GC_invoke_finalizers has something to do.            */
688 GC_API int GC_should_invoke_finalizers GC_PROTO((void));
689
690 GC_API int GC_invoke_finalizers GC_PROTO((void));
691         /* Run finalizers for all objects that are ready to     */
692         /* be finalized.  Return the number of finalizers       */
693         /* that were run.  Normally this is also called         */
694         /* implicitly during some allocations.  If              */
695         /* GC-finalize_on_demand is nonzero, it must be called  */
696         /* explicitly.                                          */
697
698 /* GC_set_warn_proc can be used to redirect or filter warning messages. */
699 /* p may not be a NULL pointer.                                         */
700 typedef void (*GC_warn_proc) GC_PROTO((char *msg, GC_word arg));
701 GC_API GC_warn_proc GC_set_warn_proc GC_PROTO((GC_warn_proc p));
702     /* Returns old warning procedure.   */
703         
704 /* The following is intended to be used by a higher level       */
705 /* (e.g. Java-like) finalization facility.  It is expected      */
706 /* that finalization code will arrange for hidden pointers to   */
707 /* disappear.  Otherwise objects can be accessed after they     */
708 /* have been collected.                                         */
709 /* Note that putting pointers in atomic objects or in           */
710 /* nonpointer slots of "typed" objects is equivalent to         */
711 /* disguising them in this way, and may have other advantages.  */
712 # if defined(I_HIDE_POINTERS) || defined(GC_I_HIDE_POINTERS)
713     typedef GC_word GC_hidden_pointer;
714 #   define HIDE_POINTER(p) (~(GC_hidden_pointer)(p))
715 #   define REVEAL_POINTER(p) ((GC_PTR)(HIDE_POINTER(p)))
716     /* Converting a hidden pointer to a real pointer requires verifying */
717     /* that the object still exists.  This involves acquiring the       */
718     /* allocator lock to avoid a race with the collector.               */
719 # endif /* I_HIDE_POINTERS */
720
721 typedef GC_PTR (*GC_fn_type) GC_PROTO((GC_PTR client_data));
722 GC_API GC_PTR GC_call_with_alloc_lock
723                 GC_PROTO((GC_fn_type fn, GC_PTR client_data));
724
725 /* The following routines are primarily intended for use with a         */
726 /* preprocessor which inserts calls to check C pointer arithmetic.      */
727
728 /* Check that p and q point to the same object.                 */
729 /* Fail conspicuously if they don't.                            */
730 /* Returns the first argument.                                  */
731 /* Succeeds if neither p nor q points to the heap.              */
732 /* May succeed if both p and q point to between heap objects.   */
733 GC_API GC_PTR GC_same_obj GC_PROTO((GC_PTR p, GC_PTR q));
734
735 /* Checked pointer pre- and post- increment operations.  Note that      */
736 /* the second argument is in units of bytes, not multiples of the       */
737 /* object size.  This should either be invoked from a macro, or the     */
738 /* call should be automatically generated.                              */
739 GC_API GC_PTR GC_pre_incr GC_PROTO((GC_PTR *p, size_t how_much));
740 GC_API GC_PTR GC_post_incr GC_PROTO((GC_PTR *p, size_t how_much));
741
742 /* Check that p is visible                                              */
743 /* to the collector as a possibly pointer containing location.          */
744 /* If it isn't fail conspicuously.                                      */
745 /* Returns the argument in all cases.  May erroneously succeed          */
746 /* in hard cases.  (This is intended for debugging use with             */
747 /* untyped allocations.  The idea is that it should be possible, though */
748 /* slow, to add such a call to all indirect pointer stores.)            */
749 /* Currently useless for multithreaded worlds.                          */
750 GC_API GC_PTR GC_is_visible GC_PROTO((GC_PTR p));
751
752 /* Check that if p is a pointer to a heap page, then it points to       */
753 /* a valid displacement within a heap object.                           */
754 /* Fail conspicuously if this property does not hold.                   */
755 /* Uninteresting with GC_all_interior_pointers.                         */
756 /* Always returns its argument.                                         */
757 GC_API GC_PTR GC_is_valid_displacement GC_PROTO((GC_PTR p));
758
759 /* Safer, but slow, pointer addition.  Probably useful mainly with      */
760 /* a preprocessor.  Useful only for heap pointers.                      */
761 #ifdef GC_DEBUG
762 #   define GC_PTR_ADD3(x, n, type_of_result) \
763         ((type_of_result)GC_same_obj((x)+(n), (x)))
764 #   define GC_PRE_INCR3(x, n, type_of_result) \
765         ((type_of_result)GC_pre_incr(&(x), (n)*sizeof(*x))
766 #   define GC_POST_INCR2(x, type_of_result) \
767         ((type_of_result)GC_post_incr(&(x), sizeof(*x))
768 #   ifdef __GNUC__
769 #       define GC_PTR_ADD(x, n) \
770             GC_PTR_ADD3(x, n, typeof(x))
771 #       define GC_PRE_INCR(x, n) \
772             GC_PRE_INCR3(x, n, typeof(x))
773 #       define GC_POST_INCR(x, n) \
774             GC_POST_INCR3(x, typeof(x))
775 #   else
776         /* We can't do this right without typeof, which ANSI    */
777         /* decided was not sufficiently useful.  Repeatedly     */
778         /* mentioning the arguments seems too dangerous to be   */
779         /* useful.  So does not casting the result.             */
780 #       define GC_PTR_ADD(x, n) ((x)+(n))
781 #   endif
782 #else   /* !GC_DEBUG */
783 #   define GC_PTR_ADD3(x, n, type_of_result) ((x)+(n))
784 #   define GC_PTR_ADD(x, n) ((x)+(n))
785 #   define GC_PRE_INCR3(x, n, type_of_result) ((x) += (n))
786 #   define GC_PRE_INCR(x, n) ((x) += (n))
787 #   define GC_POST_INCR2(x, n, type_of_result) ((x)++)
788 #   define GC_POST_INCR(x, n) ((x)++)
789 #endif
790
791 /* Safer assignment of a pointer to a nonstack location.        */
792 #ifdef GC_DEBUG
793 # ifdef __STDC__
794 #   define GC_PTR_STORE(p, q) \
795         (*(void **)GC_is_visible(p) = GC_is_valid_displacement(q))
796 # else
797 #   define GC_PTR_STORE(p, q) \
798         (*(char **)GC_is_visible(p) = GC_is_valid_displacement(q))
799 # endif
800 #else /* !GC_DEBUG */
801 #   define GC_PTR_STORE(p, q) *((p) = (q))
802 #endif
803
804 /* Fynctions called to report pointer checking errors */
805 GC_API void (*GC_same_obj_print_proc) GC_PROTO((GC_PTR p, GC_PTR q));
806
807 GC_API void (*GC_is_valid_displacement_print_proc)
808         GC_PROTO((GC_PTR p));
809
810 GC_API void (*GC_is_visible_print_proc)
811         GC_PROTO((GC_PTR p));
812
813
814 /* For pthread support, we generally need to intercept a number of      */
815 /* thread library calls.  We do that here by macro defining them.       */
816
817 #if !defined(GC_USE_LD_WRAP) && \
818     (defined(GC_LINUX_THREADS) || defined(GC_HPUX_THREADS) || \
819      defined(GC_IRIX_THREADS) || defined(GC_SOLARIS_PTHREADS) || \
820      defined(GC_SOLARIS_THREADS) || defined(GC_OSF1_THREADS))
821 # include "gc_pthread_redirects.h"
822 #endif
823
824 # if defined(PCR) || defined(GC_SOLARIS_THREADS) || \
825      defined(GC_SOLARIS_PTHREADS) || defined(GC_WIN32_THREADS) || \
826      defined(GC_IRIX_THREADS) || defined(GC_LINUX_THREADS) || \
827      defined(GC_HPUX_THREADS)
828         /* Any flavor of threads except SRC_M3. */
829 /* This returns a list of objects, linked through their first           */
830 /* word.  Its use can greatly reduce lock contention problems, since    */
831 /* the allocation lock can be acquired and released many fewer times.   */
832 /* lb must be large enough to hold the pointer field.                   */
833 /* It is used internally by gc_local_alloc.h, which provides a simpler  */
834 /* programming interface on Linux.                                      */
835 GC_PTR GC_malloc_many(size_t lb);
836 #define GC_NEXT(p) (*(GC_PTR *)(p))     /* Retrieve the next element    */
837                                         /* in returned list.            */
838 extern void GC_thr_init();      /* Needed for Solaris/X86       */
839
840 #endif /* THREADS && !SRC_M3 */
841
842 #if defined(WIN32_THREADS) && defined(_WIN32_WCE)
843 # include <windows.h>
844
845   /*
846    * win32_threads.c implements the real WinMain, which will start a new thread
847    * to call GC_WinMain after initializing the garbage collector.
848    */
849   int WINAPI GC_WinMain(
850       HINSTANCE hInstance,
851       HINSTANCE hPrevInstance,
852       LPWSTR lpCmdLine,
853       int nCmdShow );
854
855   /*
856    * All threads must be created using GC_CreateThread, so that they will be
857    * recorded in the thread table.
858    */
859   HANDLE WINAPI GC_CreateThread(
860       LPSECURITY_ATTRIBUTES lpThreadAttributes, 
861       DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, 
862       LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId );
863
864 # ifndef GC_BUILD
865 #   define WinMain GC_WinMain
866 #   define CreateThread GC_CreateThread
867 # endif
868
869 #endif
870
871 /*
872  * If you are planning on putting
873  * the collector in a SunOS 5 dynamic library, you need to call GC_INIT()
874  * from the statically loaded program section.
875  * This circumvents a Solaris 2.X (X<=4) linker bug.
876  */
877 #if defined(sparc) || defined(__sparc)
878 #   define GC_INIT() { extern end, etext; \
879                        GC_noop(&end, &etext); }
880 #else
881 # if defined(__CYGWIN32__) && defined(GC_USE_DLL)
882     /*
883      * Similarly gnu-win32 DLLs need explicit initialization
884      */
885 #   define GC_INIT() { GC_add_roots(DATASTART, DATAEND); }
886 # else
887 #   define GC_INIT()
888 # endif
889 #endif
890
891 #if !defined(_WIN32_WCE) \
892     && ((defined(_MSDOS) || defined(_MSC_VER)) && (_M_IX86 >= 300) \
893         || defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__))
894   /* win32S may not free all resources on process exit.  */
895   /* This explicitly deallocates the heap.               */
896     GC_API void GC_win32_free_heap ();
897 #endif
898
899 #if ( defined(_AMIGA) && !defined(GC_AMIGA_MAKINGLIB) )
900   /* Allocation really goes through GC_amiga_allocwrapper_do */
901 # include "gc_amiga_redirects.h"
902 #endif
903
904 #if defined(GC_REDIRECT_TO_LOCAL) && !defined(GC_LOCAL_ALLOC_H)
905 #  include  "gc_local_alloc.h"
906 #endif
907
908 #ifdef __cplusplus
909     }  /* end of extern "C" */
910 #endif
911
912 #endif /* _GC_H */