OSDN Git Service

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