Upgrade Boehm GC to 7.2alpha4.
[cacao.git] / src / mm / boehm-gc / include / private / gc_priv.h
1 /*
2  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
4  * Copyright (c) 1996-1999 by Silicon Graphics.  All rights reserved.
5  * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
6  *
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 #ifndef GC_PRIVATE_H
19 #define GC_PRIVATE_H
20
21 #ifdef HAVE_CONFIG_H
22 # include "private/config.h"
23 #endif
24
25 #ifndef GC_BUILD
26 # define GC_BUILD
27 #endif
28
29 #if (defined(__linux__) || defined(__GLIBC__) || defined(__GNU__)) \
30     && !defined(_GNU_SOURCE)
31   /* Can't test LINUX, since this must be defined before other includes. */
32 # define _GNU_SOURCE
33 #endif
34
35 #if (defined(DGUX) && defined(GC_THREADS) || defined(DGUX386_THREADS) \
36      || defined(GC_DGUX386_THREADS)) && !defined(_USING_POSIX4A_DRAFT10)
37 # define _USING_POSIX4A_DRAFT10 1
38 #endif
39
40 # if defined(NO_DEBUGGING) && !defined(GC_ASSERTIONS) && !defined(NDEBUG)
41     /* To turn off assertion checking (in atomic_ops.h). */
42 #   define NDEBUG 1
43 # endif
44
45 #ifndef GC_H
46 # define GC_I_HIDE_POINTERS /* to get GC_HIDE_POINTER() and friends */
47 # include "../gc.h"
48 #endif
49
50 #include <stdlib.h>
51 #if !defined(sony_news)
52 # include <stddef.h>
53 #endif
54
55 #ifdef DGUX
56 # include <sys/types.h>
57 # include <sys/time.h>
58 # include <sys/resource.h>
59 #endif /* DGUX */
60
61 #ifdef BSD_TIME
62 # include <sys/types.h>
63 # include <sys/time.h>
64 # include <sys/resource.h>
65 #endif /* BSD_TIME */
66
67 #ifdef PARALLEL_MARK
68 # define AO_REQUIRE_CAS
69 # if !defined(__GNUC__) && !defined(AO_ASSUME_WINDOWS98)
70 #   define AO_ASSUME_WINDOWS98
71 # endif
72 #endif
73
74 #ifndef GC_TINY_FL_H
75 # include "../gc_tiny_fl.h"
76 #endif
77
78 #ifndef GC_MARK_H
79 # include "../gc_mark.h"
80 #endif
81
82 typedef GC_word word;
83 typedef GC_signed_word signed_word;
84 typedef unsigned int unsigned32;
85
86 typedef int GC_bool;
87 #define TRUE 1
88 #define FALSE 0
89
90 typedef char * ptr_t;   /* A generic pointer to which we can add        */
91                         /* byte displacements and which can be used     */
92                         /* for address comparisons.                     */
93
94 #ifndef GCCONFIG_H
95 # include "gcconfig.h"
96 #endif
97
98 #ifndef GC_INNER
99   /* This tagging macro must be used at the start of every variable     */
100   /* definition which is declared with GC_EXTERN.  Should be also used  */
101   /* for the GC-scope function definitions and prototypes.  Must not be */
102   /* used in gcconfig.h.  Shouldn't be used for the debugging-only      */
103   /* functions.  Currently, not used for the functions declared in or   */
104   /* called from the "dated" source files (pcr_interface.c, specific.c  */
105   /* and in the "extra" folder).                                        */
106 # if defined(GC_DLL) && defined(__GNUC__) && !defined(MSWIN32) \
107         && !defined(MSWINCE)
108 #   if __GNUC__ >= 4
109       /* See the corresponding GC_API definition. */
110 #     define GC_INNER __attribute__((__visibility__("hidden")))
111 #   else
112       /* The attribute is unsupported. */
113 #     define GC_INNER /* empty */
114 #   endif
115 # else
116 #   define GC_INNER /* empty */
117 # endif
118
119 # define GC_EXTERN extern GC_INNER
120   /* Used only for the GC-scope variables (prefixed with "GC_")         */
121   /* declared in the header files.  Must not be used for thread-local   */
122   /* variables.  Must not be used in gcconfig.h.  Shouldn't be used for */
123   /* the debugging-only or profiling-only variables.  Currently, not    */
124   /* used for the variables accessed from the "dated" source files      */
125   /* (pcr_interface.c, specific.c/h, and in the "extra" folder).        */
126   /* The corresponding variable definition must start with GC_INNER.    */
127 #endif /* !GC_INNER */
128
129 #ifndef HEADERS_H
130 # include "gc_hdrs.h"
131 #endif
132
133 #if __GNUC__ >= 3
134 # define EXPECT(expr, outcome) __builtin_expect(expr,outcome)
135   /* Equivalent to (expr), but predict that usually (expr)==outcome. */
136 #else
137 # define EXPECT(expr, outcome) (expr)
138 #endif /* __GNUC__ */
139
140 #if defined(_MSC_VER) || defined(__INTEL_COMPILER) || defined(__DMC__) \
141         || defined(__WATCOMC__)
142 # define GC_INLINE static __inline
143 #elif (__GNUC__ >= 3) || defined(__sun)
144 # define GC_INLINE static inline
145 #else
146 # define GC_INLINE static
147 #endif
148
149 #ifndef GC_API_PRIV
150 # define GC_API_PRIV GC_API
151 #endif
152
153 #ifndef GC_LOCKS_H
154 # include "gc_locks.h"
155 #endif
156
157 # ifdef STACK_GROWS_DOWN
158 #   define COOLER_THAN >
159 #   define HOTTER_THAN <
160 #   define MAKE_COOLER(x,y) if ((x)+(y) > (x)) {(x) += (y);} \
161                             else {(x) = (ptr_t)ONES;}
162 #   define MAKE_HOTTER(x,y) (x) -= (y)
163 # else
164 #   define COOLER_THAN <
165 #   define HOTTER_THAN >
166 #   define MAKE_COOLER(x,y) if ((x)-(y) < (x)) {(x) -= (y);} else {(x) = 0;}
167 #   define MAKE_HOTTER(x,y) (x) += (y)
168 # endif
169
170 #if defined(AMIGA) && defined(__SASC)
171 #   define GC_FAR __far
172 #else
173 #   define GC_FAR
174 #endif
175
176
177 /*********************************/
178 /*                               */
179 /* Definitions for conservative  */
180 /* collector                     */
181 /*                               */
182 /*********************************/
183
184 /*********************************/
185 /*                               */
186 /* Easily changeable parameters  */
187 /*                               */
188 /*********************************/
189
190 /* #define STUBBORN_ALLOC */
191                     /* Enable stubborn allocation, and thus a limited   */
192                     /* form of incremental collection w/o dirty bits.   */
193
194 /* #define ALL_INTERIOR_POINTERS */
195                     /* Forces all pointers into the interior of an      */
196                     /* object to be considered valid.  Also causes the  */
197                     /* sizes of all objects to be inflated by at least  */
198                     /* one byte.  This should suffice to guarantee      */
199                     /* that in the presence of a compiler that does     */
200                     /* not perform garbage-collector-unsafe             */
201                     /* optimizations, all portable, strictly ANSI       */
202                     /* conforming C programs should be safely usable    */
203                     /* with malloc replaced by GC_malloc and free       */
204                     /* calls removed.  There are several disadvantages: */
205                     /* 1. There are probably no interesting, portable,  */
206                     /*    strictly ANSI conforming C programs.          */
207                     /* 2. This option makes it hard for the collector   */
208                     /*    to allocate space that is not ``pointed to''  */
209                     /*    by integers, etc.  Under SunOS 4.X with a     */
210                     /*    statically linked libc, we empirically        */
211                     /*    observed that it would be difficult to        */
212                     /*    allocate individual objects larger than 100K. */
213                     /*    Even if only smaller objects are allocated,   */
214                     /*    more swap space is likely to be needed.       */
215                     /*    Fortunately, much of this will never be       */
216                     /*    touched.                                      */
217                     /* If you can easily avoid using this option, do.   */
218                     /* If not, try to keep individual objects small.    */
219                     /* This is now really controlled at startup,        */
220                     /* through GC_all_interior_pointers.                */
221
222
223 #define GC_INVOKE_FINALIZERS() GC_notify_or_invoke_finalizers()
224
225 #if !defined(DONT_ADD_BYTE_AT_END)
226 # define EXTRA_BYTES GC_all_interior_pointers
227 # define MAX_EXTRA_BYTES 1
228 #else
229 # define EXTRA_BYTES 0
230 # define MAX_EXTRA_BYTES 0
231 #endif
232
233
234 # ifndef LARGE_CONFIG
235 #   define MINHINCR 16   /* Minimum heap increment, in blocks of HBLKSIZE  */
236                          /* Must be multiple of largest page size.         */
237 #   define MAXHINCR 2048 /* Maximum heap increment, in blocks              */
238 # else
239 #   define MINHINCR 64
240 #   define MAXHINCR 4096
241 # endif
242
243 # define BL_LIMIT GC_black_list_spacing
244                            /* If we need a block of N bytes, and we have */
245                            /* a block of N + BL_LIMIT bytes available,   */
246                            /* and N > BL_LIMIT,                          */
247                            /* but all possible positions in it are       */
248                            /* blacklisted, we just use it anyway (and    */
249                            /* print a warning, if warnings are enabled). */
250                            /* This risks subsequently leaking the block  */
251                            /* due to a false reference.  But not using   */
252                            /* the block risks unreasonable immediate     */
253                            /* heap growth.                               */
254
255 /*********************************/
256 /*                               */
257 /* Stack saving for debugging    */
258 /*                               */
259 /*********************************/
260
261 #ifdef NEED_CALLINFO
262     struct callinfo {
263         word ci_pc;     /* Caller, not callee, pc       */
264 #       if NARGS > 0
265             word ci_arg[NARGS]; /* bit-wise complement to avoid retention */
266 #       endif
267 #       if (NFRAMES * (NARGS + 1)) % 2 == 1
268             /* Likely alignment problem. */
269             word ci_dummy;
270 #       endif
271     };
272 #endif
273
274 #ifdef SAVE_CALL_CHAIN
275   /* Fill in the pc and argument information for up to NFRAMES of my    */
276   /* callers.  Ignore my frame and my callers frame.                    */
277   GC_INNER void GC_save_callers(struct callinfo info[NFRAMES]);
278   GC_INNER void GC_print_callers(struct callinfo info[NFRAMES]);
279 #endif
280
281
282 /*********************************/
283 /*                               */
284 /* OS interface routines         */
285 /*                               */
286 /*********************************/
287
288 #ifdef BSD_TIME
289 #   undef CLOCK_TYPE
290 #   undef GET_TIME
291 #   undef MS_TIME_DIFF
292 #   define CLOCK_TYPE struct timeval
293 #   define GET_TIME(x) { struct rusage rusage; \
294                          getrusage (RUSAGE_SELF,  &rusage); \
295                          x = rusage.ru_utime; }
296 #   define MS_TIME_DIFF(a,b) \
297                 ((unsigned long)((double) (a.tv_sec - b.tv_sec) * 1000.0 \
298                                + (double) (a.tv_usec - b.tv_usec) / 1000.0))
299 #else /* !BSD_TIME */
300 # if defined(MSWIN32) || defined(MSWINCE)
301 #   include <windows.h>
302 #   include <winbase.h>
303 #   define CLOCK_TYPE DWORD
304 #   define GET_TIME(x) x = GetTickCount()
305 #   define MS_TIME_DIFF(a,b) ((long)((a)-(b)))
306 # else /* !MSWIN32, !MSWINCE, !BSD_TIME */
307 #   include <time.h>
308 #   if !defined(__STDC__) && defined(SPARC) && defined(SUNOS4)
309       clock_t clock(void);      /* Not in time.h, where it belongs      */
310 #   endif
311 #   if defined(FREEBSD) && !defined(CLOCKS_PER_SEC)
312 #     include <machine/limits.h>
313 #     define CLOCKS_PER_SEC CLK_TCK
314 #   endif
315 #   if !defined(CLOCKS_PER_SEC)
316 #     define CLOCKS_PER_SEC 1000000
317 /*
318  * This is technically a bug in the implementation.  ANSI requires that
319  * CLOCKS_PER_SEC be defined.  But at least under SunOS4.1.1, it isn't.
320  * Also note that the combination of ANSI C and POSIX is incredibly gross
321  * here. The type clock_t is used by both clock() and times().  But on
322  * some machines these use different notions of a clock tick, CLOCKS_PER_SEC
323  * seems to apply only to clock.  Hence we use it here.  On many machines,
324  * including SunOS, clock actually uses units of microseconds (which are
325  * not really clock ticks).
326  */
327 #   endif
328 #   define CLOCK_TYPE clock_t
329 #   define GET_TIME(x) x = clock()
330 #   define MS_TIME_DIFF(a,b) ((unsigned long) \
331                 (1000.0*(double)((a)-(b))/(double)CLOCKS_PER_SEC))
332 # endif /* !MSWIN32 */
333 #endif /* !BSD_TIME */
334
335 /* We use bzero and bcopy internally.  They may not be available.       */
336 # if defined(SPARC) && defined(SUNOS4)
337 #   define BCOPY_EXISTS
338 # endif
339 # if defined(M68K) && defined(AMIGA)
340 #   define BCOPY_EXISTS
341 # endif
342 # if defined(M68K) && defined(NEXT)
343 #   define BCOPY_EXISTS
344 # endif
345 # if defined(VAX)
346 #   define BCOPY_EXISTS
347 # endif
348 # if defined(AMIGA)
349 #   include <string.h>
350 #   define BCOPY_EXISTS
351 # endif
352 # if defined(DARWIN)
353 #   include <string.h>
354 #   define BCOPY_EXISTS
355 # endif
356
357 # ifndef BCOPY_EXISTS
358 #   include <string.h>
359 #   define BCOPY(x,y,n) memcpy(y, x, (size_t)(n))
360 #   define BZERO(x,n)  memset(x, 0, (size_t)(n))
361 # else
362 #   define BCOPY(x,y,n) bcopy((void *)(x),(void *)(y),(size_t)(n))
363 #   define BZERO(x,n) bzero((void *)(x),(size_t)(n))
364 # endif
365
366 /*
367  * Stop and restart mutator threads.
368  */
369 # ifdef PCR
370 #     include "th/PCR_ThCtl.h"
371 #     define STOP_WORLD() \
372         PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_stopNormal, \
373                                    PCR_allSigsBlocked, \
374                                    PCR_waitForever)
375 #     define START_WORLD() \
376         PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_null, \
377                                    PCR_allSigsBlocked, \
378                                    PCR_waitForever);
379 # else
380 #   if defined(GC_WIN32_THREADS) || defined(GC_PTHREADS)
381       GC_INNER void GC_stop_world(void);
382       GC_INNER void GC_start_world(void);
383 #     define STOP_WORLD() GC_stop_world()
384 #     define START_WORLD() GC_start_world()
385 #   else
386         /* Just do a sanity check: we are not inside GC_do_blocking().  */
387 #     define STOP_WORLD() GC_ASSERT(GC_blocked_sp == NULL)
388 #     define START_WORLD()
389 #   endif
390 # endif
391
392 /* Abandon ship */
393 # ifdef PCR
394 #   define ABORT(s) PCR_Base_Panic(s)
395 # else
396 #   if defined(MSWINCE) && !defined(DebugBreak) \
397        && (!defined(UNDER_CE) || (defined(__MINGW32CE__) && !defined(ARM32)))
398       /* This simplifies linking for WinCE (and, probably, doesn't      */
399       /* hurt debugging much); use -DDebugBreak=DebugBreak to override  */
400       /* this behavior if really needed.  This is also a workaround for */
401       /* x86mingw32ce toolchain (if it is still declaring DebugBreak()  */
402       /* instead of defining it as a macro).                            */
403 #     define DebugBreak() _exit(-1) /* there is no abort() in WinCE */
404 #   endif
405 #   ifdef SMALL_CONFIG
406 #       if defined(MSWIN32) || defined(MSWINCE)
407 #           define ABORT(msg) DebugBreak()
408 #       else
409 #           define ABORT(msg) abort()
410 #       endif
411 #   else
412         GC_API_PRIV void GC_abort(const char * msg);
413 #       define ABORT(msg) GC_abort(msg)
414 #   endif
415 # endif
416
417 /* Exit abnormally, but without making a mess (e.g. out of memory) */
418 # ifdef PCR
419 #   define EXIT() PCR_Base_Exit(1,PCR_waitForever)
420 # else
421 #   define EXIT() (void)exit(1)
422 # endif
423
424 /* Print warning message, e.g. almost out of memory.    */
425 #define WARN(msg, arg) (*GC_current_warn_proc)("GC Warning: " msg, \
426                                                (GC_word)(arg))
427 GC_EXTERN GC_warn_proc GC_current_warn_proc;
428
429 /* Print format type macro for signed_word.  Currently used for WARN()  */
430 /* only.  This could be of use on Win64 but commented out since Win64   */
431 /* is only a little-endian architecture (for now) and the WARN format   */
432 /* string is, possibly, processed on the client side, so non-standard   */
433 /* print type modifiers should be avoided (if possible).                */
434 #if defined(_MSC_VER) && defined(_WIN64) && !defined(GC_PRIdPTR)
435 /* #define GC_PRIdPTR "I64d" */
436 #endif
437
438 #if !defined(GC_PRIdPTR) && (defined(_LLP64) || defined(__LLP64__) \
439         || defined(_WIN64))
440 /* #include <inttypes.h> */
441 /* #define GC_PRIdPTR PRIdPTR */
442 #endif
443
444 #ifndef GC_PRIdPTR
445   /* Assume sizeof(void *) == sizeof(long) (or a little-endian machine) */
446 # define GC_PRIdPTR "ld"
447 #endif
448
449 /* Get environment entry */
450 #if !defined(NO_GETENV)
451 #   if defined(EMPTY_GETENV_RESULTS)
452         /* Workaround for a reputed Wine bug.   */
453         GC_INLINE char * fixed_getenv(const char *name)
454         {
455           char * tmp = getenv(name);
456           if (tmp == 0 || strlen(tmp) == 0)
457             return 0;
458           return tmp;
459         }
460 #       define GETENV(name) fixed_getenv(name)
461 #   else
462 #       define GETENV(name) getenv(name)
463 #   endif
464 #else
465 #   define GETENV(name) 0
466 #endif
467
468 #if defined(DARWIN)
469 #       if defined(POWERPC)
470 #               if CPP_WORDSZ == 32
471 #                 define GC_THREAD_STATE_T ppc_thread_state_t
472 #                 define GC_MACH_THREAD_STATE PPC_THREAD_STATE
473 #                 define GC_MACH_THREAD_STATE_COUNT PPC_THREAD_STATE_COUNT
474 #                 define GC_MACH_HEADER mach_header
475 #                 define GC_MACH_SECTION section
476 #                 define GC_GETSECTBYNAME getsectbynamefromheader
477 #               else
478 #                 define GC_THREAD_STATE_T ppc_thread_state64_t
479 #                 define GC_MACH_THREAD_STATE PPC_THREAD_STATE64
480 #                 define GC_MACH_THREAD_STATE_COUNT PPC_THREAD_STATE64_COUNT
481 #                 define GC_MACH_HEADER mach_header_64
482 #                 define GC_MACH_SECTION section_64
483 #                 define GC_GETSECTBYNAME getsectbynamefromheader_64
484 #               endif
485 #       elif defined(I386) || defined(X86_64)
486 #               if CPP_WORDSZ == 32
487 #                 define GC_THREAD_STATE_T x86_thread_state32_t
488 #                 define GC_MACH_THREAD_STATE x86_THREAD_STATE32
489 #                 define GC_MACH_THREAD_STATE_COUNT x86_THREAD_STATE32_COUNT
490 #                 define GC_MACH_HEADER mach_header
491 #                 define GC_MACH_SECTION section
492 #                 define GC_GETSECTBYNAME getsectbynamefromheader
493 #               else
494 #                 define GC_THREAD_STATE_T x86_thread_state64_t
495 #                 define GC_MACH_THREAD_STATE x86_THREAD_STATE64
496 #                 define GC_MACH_THREAD_STATE_COUNT x86_THREAD_STATE64_COUNT
497 #                 define GC_MACH_HEADER mach_header_64
498 #                 define GC_MACH_SECTION section_64
499 #                 define GC_GETSECTBYNAME getsectbynamefromheader_64
500 #               endif
501 #       else
502 #               if defined(ARM32)
503 #                 define GC_THREAD_STATE_T arm_thread_state_t
504 #               else
505 #                 error define GC_THREAD_STATE_T
506 #               endif
507 #               define GC_MACH_THREAD_STATE MACHINE_THREAD_STATE
508 #               define GC_MACH_THREAD_STATE_COUNT MACHINE_THREAD_STATE_COUNT
509 #       endif
510 /* Try to work out the right way to access thread state structure members.
511    The structure has changed its definition in different Darwin versions.
512    This now defaults to the (older) names without __, thus hopefully,
513    not breaking any existing Makefile.direct builds.  */
514 #       if defined (HAS_PPC_THREAD_STATE___R0) \
515           || defined (HAS_PPC_THREAD_STATE64___R0) \
516           || defined (HAS_X86_THREAD_STATE32___EAX) \
517           || defined (HAS_X86_THREAD_STATE64___RAX)
518 #         define THREAD_FLD(x) __ ## x
519 #       else
520 #         define THREAD_FLD(x) x
521 #       endif
522 #endif
523
524 /*********************************/
525 /*                               */
526 /* Word-size-dependent defines   */
527 /*                               */
528 /*********************************/
529
530 #if CPP_WORDSZ == 32
531 #  define WORDS_TO_BYTES(x)   ((x)<<2)
532 #  define BYTES_TO_WORDS(x)   ((x)>>2)
533 #  define LOGWL               ((word)5)    /* log[2] of CPP_WORDSZ */
534 #  define modWORDSZ(n) ((n) & 0x1f)        /* n mod size of word            */
535 #  if ALIGNMENT != 4
536 #       define UNALIGNED_PTRS
537 #  endif
538 #endif
539
540 #if CPP_WORDSZ == 64
541 #  define WORDS_TO_BYTES(x)   ((x)<<3)
542 #  define BYTES_TO_WORDS(x)   ((x)>>3)
543 #  define LOGWL               ((word)6)    /* log[2] of CPP_WORDSZ */
544 #  define modWORDSZ(n) ((n) & 0x3f)        /* n mod size of word            */
545 #  if ALIGNMENT != 8
546 #       define UNALIGNED_PTRS
547 #  endif
548 #endif
549
550 /* The first TINY_FREELISTS free lists correspond to the first  */
551 /* TINY_FREELISTS multiples of GRANULE_BYTES, i.e. we keep      */
552 /* separate free lists for each multiple of GRANULE_BYTES       */
553 /* up to (TINY_FREELISTS-1) * GRANULE_BYTES.  After that they   */
554 /* may be spread out further.                                   */
555 #include "../gc_tiny_fl.h"
556 #define GRANULE_BYTES GC_GRANULE_BYTES
557 #define TINY_FREELISTS GC_TINY_FREELISTS
558
559 #define WORDSZ ((word)CPP_WORDSZ)
560 #define SIGNB  ((word)1 << (WORDSZ-1))
561 #define BYTES_PER_WORD      ((word)(sizeof (word)))
562 #define ONES                ((word)(signed_word)(-1))
563 #define divWORDSZ(n) ((n) >> LOGWL)     /* divide n by size of word */
564
565 #if GRANULE_BYTES == 8
566 # define BYTES_TO_GRANULES(n) ((n)>>3)
567 # define GRANULES_TO_BYTES(n) ((n)<<3)
568 # if CPP_WORDSZ == 64
569 #   define GRANULES_TO_WORDS(n) (n)
570 # elif CPP_WORDSZ == 32
571 #   define GRANULES_TO_WORDS(n) ((n)<<1)
572 # else
573 #   define GRANULES_TO_WORDS(n) BYTES_TO_WORDS(GRANULES_TO_BYTES(n))
574 # endif
575 #elif GRANULE_BYTES == 16
576 # define BYTES_TO_GRANULES(n) ((n)>>4)
577 # define GRANULES_TO_BYTES(n) ((n)<<4)
578 # if CPP_WORDSZ == 64
579 #   define GRANULES_TO_WORDS(n) ((n)<<1)
580 # elif CPP_WORDSZ == 32
581 #   define GRANULES_TO_WORDS(n) ((n)<<2)
582 # else
583 #   define GRANULES_TO_WORDS(n) BYTES_TO_WORDS(GRANULES_TO_BYTES(n))
584 # endif
585 #else
586 # error Bad GRANULE_BYTES value
587 #endif
588
589 /*********************/
590 /*                   */
591 /*  Size Parameters  */
592 /*                   */
593 /*********************/
594
595 /*  Heap block size, bytes. Should be power of 2.               */
596 /* Incremental GC with MPROTECT_VDB currently requires the      */
597 /* page size to be a multiple of HBLKSIZE.  Since most modern   */
598 /* architectures support variable page sizes down to 4K, and    */
599 /* X86 is generally 4K, we now default to 4K, except for        */
600 /*   Alpha: Seems to be used with 8K pages.                     */
601 /*   SMALL_CONFIG: Want less block-level fragmentation.         */
602
603 #ifndef HBLKSIZE
604 # ifdef SMALL_CONFIG
605 #   define CPP_LOG_HBLKSIZE 10
606 # else
607 #   if defined(ALPHA)
608 #     define CPP_LOG_HBLKSIZE 13
609 #   else
610 #     define CPP_LOG_HBLKSIZE 12
611 #   endif
612 # endif
613 #else
614 # if HBLKSIZE == 512
615 #   define CPP_LOG_HBLKSIZE 9
616 # endif
617 # if HBLKSIZE == 1024
618 #   define CPP_LOG_HBLKSIZE 10
619 # endif
620 # if HBLKSIZE == 2048
621 #   define CPP_LOG_HBLKSIZE 11
622 # endif
623 # if HBLKSIZE == 4096
624 #   define CPP_LOG_HBLKSIZE 12
625 # endif
626 # if HBLKSIZE == 8192
627 #   define CPP_LOG_HBLKSIZE 13
628 # endif
629 # if HBLKSIZE == 16384
630 #   define CPP_LOG_HBLKSIZE 14
631 # endif
632 # ifndef CPP_LOG_HBLKSIZE
633     --> fix HBLKSIZE
634 # endif
635 # undef HBLKSIZE
636 #endif
637
638 # define CPP_HBLKSIZE (1 << CPP_LOG_HBLKSIZE)
639 # define LOG_HBLKSIZE   ((size_t)CPP_LOG_HBLKSIZE)
640 # define HBLKSIZE ((size_t)CPP_HBLKSIZE)
641
642
643 /*  max size objects supported by freelist (larger objects are  */
644 /*  allocated directly with allchblk(), by rounding to the next */
645 /*  multiple of HBLKSIZE.                                       */
646
647 #define CPP_MAXOBJBYTES (CPP_HBLKSIZE/2)
648 #define MAXOBJBYTES ((size_t)CPP_MAXOBJBYTES)
649 #define CPP_MAXOBJWORDS BYTES_TO_WORDS(CPP_MAXOBJBYTES)
650 #define MAXOBJWORDS ((size_t)CPP_MAXOBJWORDS)
651 #define CPP_MAXOBJGRANULES BYTES_TO_GRANULES(CPP_MAXOBJBYTES)
652 #define MAXOBJGRANULES ((size_t)CPP_MAXOBJGRANULES)
653
654 # define divHBLKSZ(n) ((n) >> LOG_HBLKSIZE)
655
656 # define HBLK_PTR_DIFF(p,q) divHBLKSZ((ptr_t)p - (ptr_t)q)
657         /* Equivalent to subtracting 2 hblk pointers.   */
658         /* We do it this way because a compiler should  */
659         /* find it hard to use an integer division      */
660         /* instead of a shift.  The bundled SunOS 4.1   */
661         /* o.w. sometimes pessimizes the subtraction to */
662         /* involve a call to .div.                      */
663
664 # define modHBLKSZ(n) ((n) & (HBLKSIZE-1))
665
666 # define HBLKPTR(objptr) ((struct hblk *)(((word) (objptr)) & ~(HBLKSIZE-1)))
667
668 # define HBLKDISPL(objptr) (((size_t) (objptr)) & (HBLKSIZE-1))
669
670 /* Round up byte allocation requests to integral number of words, etc. */
671 # define ROUNDED_UP_GRANULES(n) \
672         BYTES_TO_GRANULES((n) + (GRANULE_BYTES - 1 + EXTRA_BYTES))
673 # if MAX_EXTRA_BYTES == 0
674 #  define SMALL_OBJ(bytes) EXPECT((bytes) <= (MAXOBJBYTES), 1)
675 # else
676 #  define SMALL_OBJ(bytes) \
677             (EXPECT((bytes) <= (MAXOBJBYTES - MAX_EXTRA_BYTES), 1) || \
678              (bytes) <= (MAXOBJBYTES - EXTRA_BYTES))
679         /* This really just tests bytes <= MAXOBJBYTES - EXTRA_BYTES.   */
680         /* But we try to avoid looking up EXTRA_BYTES.                  */
681 # endif
682 # define ADD_SLOP(bytes) ((bytes) + EXTRA_BYTES)
683 # ifndef MIN_WORDS
684 #  define MIN_WORDS 2   /* FIXME: obsolete */
685 # endif
686
687 /*
688  * Hash table representation of sets of pages.
689  * Implements a map from aligned HBLKSIZE chunks of the address space to one
690  * bit each.
691  * This assumes it is OK to spuriously set bits, e.g. because multiple
692  * addresses are represented by a single location.
693  * Used by black-listing code, and perhaps by dirty bit maintenance code.
694  */
695
696 # ifdef LARGE_CONFIG
697 #   if CPP_WORDSZ == 32
698 #    define LOG_PHT_ENTRIES  20 /* Collisions likely at 1M blocks,      */
699                                 /* which is >= 4GB.  Each table takes   */
700                                 /* 128KB, some of which may never be    */
701                                 /* touched.                             */
702 #   else
703 #    define LOG_PHT_ENTRIES  21 /* Collisions likely at 2M blocks,      */
704                                 /* which is >= 8GB.  Each table takes   */
705                                 /* 256KB, some of which may never be    */
706                                 /* touched.                             */
707 #   endif
708 # else
709 #   ifdef SMALL_CONFIG
710 #     define LOG_PHT_ENTRIES  15 /* Collisions are likely if heap grows */
711                                  /* to more than 32K hblks = 128MB.     */
712                                  /* Each hash table occupies 4K bytes.  */
713 #   else /* default "medium" configuration */
714 #     define LOG_PHT_ENTRIES  18 /* Collisions are likely if heap grows */
715                                  /* to more than 256K hblks >= 1GB.     */
716                                  /* Each hash table occupies 32K bytes. */
717                                  /* Even for somewhat smaller heaps,    */
718                                  /* say half that, collisions may be an */
719                                  /* issue because we blacklist          */
720                                  /* addresses outside the heap.         */
721 #   endif
722 # endif
723 # define PHT_ENTRIES ((word)1 << LOG_PHT_ENTRIES)
724 # define PHT_SIZE (PHT_ENTRIES >> LOGWL)
725 typedef word page_hash_table[PHT_SIZE];
726
727 # define PHT_HASH(addr) ((((word)(addr)) >> LOG_HBLKSIZE) & (PHT_ENTRIES - 1))
728
729 # define get_pht_entry_from_index(bl, index) \
730                 (((bl)[divWORDSZ(index)] >> modWORDSZ(index)) & 1)
731 # define set_pht_entry_from_index(bl, index) \
732                 (bl)[divWORDSZ(index)] |= (word)1 << modWORDSZ(index)
733 # define clear_pht_entry_from_index(bl, index) \
734                 (bl)[divWORDSZ(index)] &= ~((word)1 << modWORDSZ(index))
735 /* And a dumb but thread-safe version of set_pht_entry_from_index.      */
736 /* This sets (many) extra bits.                                         */
737 # define set_pht_entry_from_index_safe(bl, index) \
738                 (bl)[divWORDSZ(index)] = ONES
739
740
741 /********************************************/
742 /*                                          */
743 /*    H e a p   B l o c k s                 */
744 /*                                          */
745 /********************************************/
746
747 /*  heap block header */
748 #define HBLKMASK   (HBLKSIZE-1)
749
750 #define MARK_BITS_PER_HBLK (HBLKSIZE/GRANULE_BYTES)
751            /* upper bound                                    */
752            /* We allocate 1 bit per allocation granule.      */
753            /* If MARK_BIT_PER_GRANULE is defined, we use     */
754            /* every nth bit, where n is the number of        */
755            /* allocation granules per object.  If            */
756            /* MARK_BIT_PER_OBJ is defined, we only use the   */
757            /* initial group of mark bits, and it is safe     */
758            /* to allocate smaller header for large objects.  */
759
760 # ifdef USE_MARK_BYTES
761 #   define MARK_BITS_SZ (MARK_BITS_PER_HBLK + 1)
762         /* Unlike the other case, this is in units of bytes.            */
763         /* Since we force doubleword alignment, we need at most one     */
764         /* mark bit per 2 words.  But we do allocate and set one        */
765         /* extra mark bit to avoid an explicit check for the            */
766         /* partial object at the end of each block.                     */
767 # else
768 #   define MARK_BITS_SZ (MARK_BITS_PER_HBLK/CPP_WORDSZ + 1)
769 # endif
770
771 #ifdef PARALLEL_MARK
772 # include "atomic_ops.h"
773   typedef AO_t counter_t;
774 #else
775   typedef size_t counter_t;
776 # if defined(THREADS) && defined(MPROTECT_VDB)
777 #   include "atomic_ops.h"
778 # endif
779 #endif
780
781 /* We maintain layout maps for heap blocks containing objects of a given */
782 /* size.  Each entry in this map describes a byte offset and has the     */
783 /* following type.                                                       */
784 struct hblkhdr {
785     struct hblk * hb_next;      /* Link field for hblk free list         */
786                                 /* and for lists of chunks waiting to be */
787                                 /* reclaimed.                            */
788     struct hblk * hb_prev;      /* Backwards link for free list.        */
789     struct hblk * hb_block;     /* The corresponding block.             */
790     unsigned char hb_obj_kind;
791                          /* Kind of objects in the block.  Each kind    */
792                          /* identifies a mark procedure and a set of    */
793                          /* list headers.  Sometimes called regions.    */
794     unsigned char hb_flags;
795 #       define IGNORE_OFF_PAGE  1       /* Ignore pointers that do not  */
796                                         /* point to the first page of   */
797                                         /* this object.                 */
798 #       define WAS_UNMAPPED 2   /* This is a free block, which has      */
799                                 /* been unmapped from the address       */
800                                 /* space.                               */
801                                 /* GC_remap must be invoked on it       */
802                                 /* before it can be reallocated.        */
803                                 /* Only set with USE_MUNMAP.            */
804 #       define FREE_BLK 4       /* Block is free, i.e. not in use.      */
805     unsigned short hb_last_reclaimed;
806                                 /* Value of GC_gc_no when block was     */
807                                 /* last allocated or swept. May wrap.   */
808                                 /* For a free block, this is maintained */
809                                 /* only for USE_MUNMAP, and indicates   */
810                                 /* when the header was allocated, or    */
811                                 /* when the size of the block last      */
812                                 /* changed.                             */
813     size_t hb_sz;  /* If in use, size in bytes, of objects in the block. */
814                    /* if free, the size in bytes of the whole block      */
815                    /* We assume that this is convertible to signed_word  */
816                    /* without generating a negative result.  We avoid    */
817                    /* generating free blocks larger than that.           */
818     word hb_descr;              /* object descriptor for marking.  See  */
819                                 /* mark.h.                              */
820 #   ifdef MARK_BIT_PER_OBJ
821       unsigned32 hb_inv_sz;     /* A good upper bound for 2**32/hb_sz.  */
822                                 /* For large objects, we use            */
823                                 /* LARGE_INV_SZ.                        */
824 #     define LARGE_INV_SZ (1 << 16)
825 #   else
826       unsigned char hb_large_block;
827       short * hb_map;           /* Essentially a table of remainders    */
828                                 /* mod BYTES_TO_GRANULES(hb_sz), except */
829                                 /* for large blocks.  See GC_obj_map.   */
830 #   endif
831     counter_t hb_n_marks;       /* Number of set mark bits, excluding   */
832                                 /* the one always set at the end.       */
833                                 /* Currently it is concurrently         */
834                                 /* updated and hence only approximate.  */
835                                 /* But a zero value does guarantee that */
836                                 /* the block contains no marked         */
837                                 /* objects.                             */
838                                 /* Ensuring this property means that we */
839                                 /* never decrement it to zero during a  */
840                                 /* collection, and hence the count may  */
841                                 /* be one too high.  Due to concurrent  */
842                                 /* updates, an arbitrary number of      */
843                                 /* increments, but not all of them (!)  */
844                                 /* may be lost, hence it may in theory  */
845                                 /* be much too low.                     */
846                                 /* The count may also be too high if    */
847                                 /* multiple mark threads mark the       */
848                                 /* same object due to a race.           */
849                                 /* Without parallel marking, the count  */
850                                 /* is accurate.                         */
851 #   ifdef USE_MARK_BYTES
852       union {
853         char _hb_marks[MARK_BITS_SZ];
854                             /* The i'th byte is 1 if the object         */
855                             /* starting at granule i or object i is     */
856                             /* marked, 0 o.w.                           */
857                             /* The mark bit for the "one past the       */
858                             /* end" object is always set to avoid a     */
859                             /* special case test in the marker.         */
860         word dummy;     /* Force word alignment of mark bytes. */
861       } _mark_byte_union;
862 #     define hb_marks _mark_byte_union._hb_marks
863 #   else
864       word hb_marks[MARK_BITS_SZ];
865 #   endif /* !USE_MARK_BYTES */
866 };
867
868 # define ANY_INDEX 23   /* "Random" mark bit index for assertions */
869
870 /*  heap block body */
871
872 # define HBLK_WORDS (HBLKSIZE/sizeof(word))
873 # define HBLK_GRANULES (HBLKSIZE/GRANULE_BYTES)
874
875 /* The number of objects in a block dedicated to a certain size.        */
876 /* may erroneously yield zero (instead of one) for large objects.       */
877 # define HBLK_OBJS(sz_in_bytes) (HBLKSIZE/(sz_in_bytes))
878
879 struct hblk {
880     char hb_body[HBLKSIZE];
881 };
882
883 # define HBLK_IS_FREE(hdr) (((hdr) -> hb_flags & FREE_BLK) != 0)
884
885 # define OBJ_SZ_TO_BLOCKS(sz) divHBLKSZ((sz) + HBLKSIZE-1)
886     /* Size of block (in units of HBLKSIZE) needed to hold objects of   */
887     /* given sz (in bytes).                                             */
888
889 /* Object free list link */
890 # define obj_link(p) (*(void  **)(p))
891
892 # define LOG_MAX_MARK_PROCS 6
893 # define MAX_MARK_PROCS (1 << LOG_MAX_MARK_PROCS)
894
895 /* Root sets.  Logically private to mark_rts.c.  But we don't want the  */
896 /* tables scanned, so we put them here.                                 */
897 /* MAX_ROOT_SETS is the maximum number of ranges that can be    */
898 /* registered as static roots.                                  */
899 # ifdef LARGE_CONFIG
900 #   define MAX_ROOT_SETS 8192
901 # else
902 #   ifdef SMALL_CONFIG
903 #     define MAX_ROOT_SETS 512
904 #   else
905 #     define MAX_ROOT_SETS 2048
906 #   endif
907 # endif
908
909 # define MAX_EXCLUSIONS (MAX_ROOT_SETS/4)
910 /* Maximum number of segments that can be excluded from root sets.      */
911
912 /*
913  * Data structure for excluded static roots.
914  */
915 struct exclusion {
916     ptr_t e_start;
917     ptr_t e_end;
918 };
919
920 /* Data structure for list of root sets.                                */
921 /* We keep a hash table, so that we can filter out duplicate additions. */
922 /* Under Win32, we need to do a better job of filtering overlaps, so    */
923 /* we resort to sequential search, and pay the price.                   */
924 struct roots {
925         ptr_t r_start;/* multiple of word size */
926         ptr_t r_end;  /* multiple of word size and greater than r_start */
927 #       if !defined(MSWIN32) && !defined(MSWINCE)
928           struct roots * r_next;
929 #       endif
930         GC_bool r_tmp;
931                 /* Delete before registering new dynamic libraries */
932 };
933
934 #if !defined(MSWIN32) && !defined(MSWINCE)
935     /* Size of hash table index to roots.       */
936 #   define LOG_RT_SIZE 6
937 #   define RT_SIZE (1 << LOG_RT_SIZE) /* Power of 2, may be != MAX_ROOT_SETS */
938 #endif
939
940 /* Lists of all heap blocks and free lists      */
941 /* as well as other random data structures      */
942 /* that should not be scanned by the            */
943 /* collector.                                   */
944 /* These are grouped together in a struct       */
945 /* so that they can be easily skipped by the    */
946 /* GC_mark routine.                             */
947 /* The ordering is weird to make GC_malloc      */
948 /* faster by keeping the important fields       */
949 /* sufficiently close together that a           */
950 /* single load of a base register will do.      */
951 /* Scalars that could easily appear to          */
952 /* be pointers are also put here.               */
953 /* The main fields should precede any           */
954 /* conditionally included fields, so that       */
955 /* gc_inl.h will work even if a different set   */
956 /* of macros is defined when the client is      */
957 /* compiled.                                    */
958
959 struct _GC_arrays {
960   word _heapsize;               /* Heap size in bytes.                  */
961   word _max_heapsize;
962   word _requested_heapsize;     /* Heap size due to explicit expansion */
963   ptr_t _last_heap_addr;
964   ptr_t _prev_heap_addr;
965   word _large_free_bytes;
966         /* Total bytes contained in blocks on large object free */
967         /* list.                                                */
968   word _large_allocd_bytes;
969         /* Total number of bytes in allocated large objects blocks.     */
970         /* For the purposes of this counter and the next one only, a    */
971         /* large object is one that occupies a block of at least        */
972         /* 2*HBLKSIZE.                                                  */
973   word _max_large_allocd_bytes;
974         /* Maximum number of bytes that were ever allocated in          */
975         /* large object blocks.  This is used to help decide when it    */
976         /* is safe to split up a large block.                           */
977   word _bytes_allocd_before_gc;
978                 /* Number of words allocated before this        */
979                 /* collection cycle.                            */
980 # ifndef SEPARATE_GLOBALS
981     word _bytes_allocd;
982         /* Number of words allocated during this collection cycle */
983 # endif
984   word _bytes_dropped;
985         /* Number of black-listed bytes dropped during GC cycle */
986         /* as a result of repeated scanning during allocation   */
987         /* attempts.  These are treated largely as allocated,   */
988         /* even though they are not useful to the client.       */
989   word _bytes_finalized;
990         /* Approximate number of bytes in objects (and headers) */
991         /* that became ready for finalization in the last       */
992         /* collection.                                          */
993   word _non_gc_bytes_at_gc;
994         /* Number of explicitly managed bytes of storage        */
995         /* at last collection.                                  */
996   word _bytes_freed;
997         /* Number of explicitly deallocated bytes of memory     */
998         /* since last collection.                               */
999   word _finalizer_bytes_freed;
1000         /* Bytes of memory explicitly deallocated while         */
1001         /* finalizers were running.  Used to approximate mem.   */
1002         /* explicitly deallocated by finalizers.                */
1003   ptr_t _scratch_end_ptr;
1004   ptr_t _scratch_last_end_ptr;
1005         /* Used by headers.c, and can easily appear to point to */
1006         /* heap.                                                */
1007   GC_mark_proc _mark_procs[MAX_MARK_PROCS];
1008         /* Table of user-defined mark procedures.  There is     */
1009         /* a small number of these, which can be referenced     */
1010         /* by DS_PROC mark descriptors.  See gc_mark.h.         */
1011
1012 # ifndef SEPARATE_GLOBALS
1013     void *_objfreelist[MAXOBJGRANULES+1];
1014                           /* free list for objects */
1015     void *_aobjfreelist[MAXOBJGRANULES+1];
1016                           /* free list for atomic objs  */
1017 # endif
1018
1019   void *_uobjfreelist[MAXOBJGRANULES+1];
1020                           /* uncollectable but traced objs      */
1021                           /* objects on this and auobjfreelist  */
1022                           /* are always marked, except during   */
1023                           /* garbage collections.               */
1024 # ifdef ATOMIC_UNCOLLECTABLE
1025     void *_auobjfreelist[MAXOBJGRANULES+1];
1026 # endif
1027                           /* uncollectable but traced objs      */
1028
1029     word _composite_in_use;
1030                 /* Number of words in accessible composite      */
1031                 /* objects.                                     */
1032     word _atomic_in_use;
1033                 /* Number of words in accessible atomic         */
1034                 /* objects.                                     */
1035 # ifdef USE_MUNMAP
1036     word _unmapped_bytes;
1037 # endif
1038
1039     size_t _size_map[MAXOBJBYTES+1];
1040         /* Number of granules to allocate when asked for a certain      */
1041         /* number of bytes.                                             */
1042
1043 # ifdef STUBBORN_ALLOC
1044     ptr_t _sobjfreelist[MAXOBJGRANULES+1];
1045 # endif
1046                           /* free list for immutable objects    */
1047 # ifdef MARK_BIT_PER_GRANULE
1048     short * _obj_map[MAXOBJGRANULES+1];
1049                        /* If not NIL, then a pointer to a map of valid  */
1050                        /* object addresses.                             */
1051                        /* _obj_map[sz_in_granules][i] is                */
1052                        /* i % sz_in_granules.                           */
1053                        /* This is now used purely to replace a          */
1054                        /* division in the marker by a table lookup.     */
1055                        /* _obj_map[0] is used for large objects and     */
1056                        /* contains all nonzero entries.  This gets us   */
1057                        /* out of the marker fast path without an extra  */
1058                        /* test.                                         */
1059 #   define MAP_LEN BYTES_TO_GRANULES(HBLKSIZE)
1060 # endif
1061 #   define VALID_OFFSET_SZ HBLKSIZE
1062   char _valid_offsets[VALID_OFFSET_SZ];
1063                                 /* GC_valid_offsets[i] == TRUE ==> i    */
1064                                 /* is registered as a displacement.     */
1065   char _modws_valid_offsets[sizeof(word)];
1066                                 /* GC_valid_offsets[i] ==>                */
1067                                 /* GC_modws_valid_offsets[i%sizeof(word)] */
1068 # ifdef STUBBORN_ALLOC
1069     page_hash_table _changed_pages;
1070         /* Stubborn object pages that were changes since last call to   */
1071         /* GC_read_changed.                                             */
1072     page_hash_table _prev_changed_pages;
1073         /* Stubborn object pages that were changes before last call to  */
1074         /* GC_read_changed.                                             */
1075 # endif
1076 # if defined(PROC_VDB) || defined(MPROTECT_VDB) || \
1077      defined(GWW_VDB) || defined(MANUAL_VDB)
1078     page_hash_table _grungy_pages; /* Pages that were dirty at last     */
1079                                    /* GC_read_dirty.                    */
1080 # endif
1081 # if defined(MPROTECT_VDB) || defined(MANUAL_VDB)
1082     volatile page_hash_table _dirty_pages;
1083                         /* Pages dirtied since last GC_read_dirty. */
1084 # endif
1085 # if defined(PROC_VDB) || defined(GWW_VDB)
1086     page_hash_table _written_pages;     /* Pages ever dirtied   */
1087 # endif
1088
1089 #ifndef MAX_HEAP_SECTS
1090 # ifdef LARGE_CONFIG
1091 #   if CPP_WORDSZ > 32
1092 #     define MAX_HEAP_SECTS 8192        /* overflows at roughly 128 GB     */
1093 #   else
1094 #     define MAX_HEAP_SECTS 768         /* Separately added heap sections. */
1095 #   endif
1096 # else
1097 #   ifdef SMALL_CONFIG
1098 #     define MAX_HEAP_SECTS 128         /* Roughly 256MB (128*2048*1K)  */
1099 #   else
1100 #     if CPP_WORDSZ > 32
1101 #       define MAX_HEAP_SECTS 1024      /* Roughly 8GB                   */
1102 #     else
1103 #       define MAX_HEAP_SECTS 512       /* Roughly 4GB                   */
1104 #     endif
1105 #   endif
1106 # endif
1107 #endif /* !MAX_HEAP_SECTS */
1108
1109   struct HeapSect {
1110       ptr_t hs_start; size_t hs_bytes;
1111   } _heap_sects[MAX_HEAP_SECTS];        /* Heap segments potentially    */
1112                                         /* client objects.              */
1113 # if defined(USE_PROC_FOR_LIBRARIES)
1114      struct HeapSect _our_memory[MAX_HEAP_SECTS];
1115                                         /* All GET_MEM allocated        */
1116                                         /* memory.  Includes block      */
1117                                         /* headers and the like.        */
1118 # endif
1119 # if defined(MSWIN32) || defined(MSWINCE)
1120     ptr_t _heap_bases[MAX_HEAP_SECTS];
1121                 /* Start address of memory regions obtained from kernel. */
1122 # endif
1123 # ifdef MSWINCE
1124     word _heap_lengths[MAX_HEAP_SECTS];
1125                 /* Committed lengths of memory regions obtained from kernel. */
1126 # endif
1127   struct roots _static_roots[MAX_ROOT_SETS];
1128 # if !defined(MSWIN32) && !defined(MSWINCE)
1129     struct roots * _root_index[RT_SIZE];
1130 # endif
1131   struct exclusion _excl_table[MAX_EXCLUSIONS];
1132   /* Block header index; see gc_headers.h */
1133   bottom_index * _all_nils;
1134   bottom_index * _top_index [TOP_SZ];
1135 #ifdef ENABLE_TRACE
1136   ptr_t _trace_addr;
1137 #endif
1138 #ifdef SAVE_CALL_CHAIN
1139   struct callinfo _last_stack[NFRAMES]; /* Stack at last garbage collection.*/
1140                                         /* Useful for debugging mysterious  */
1141                                         /* object disappearances.           */
1142                                         /* In the multithreaded case, we    */
1143                                         /* currently only save the calling  */
1144                                         /* stack.                           */
1145 #endif
1146 };
1147
1148 GC_API_PRIV GC_FAR struct _GC_arrays GC_arrays;
1149
1150 # ifndef SEPARATE_GLOBALS
1151 #   define GC_objfreelist GC_arrays._objfreelist
1152 #   define GC_aobjfreelist GC_arrays._aobjfreelist
1153 #   define GC_bytes_allocd GC_arrays._bytes_allocd
1154 # endif
1155 # define GC_uobjfreelist GC_arrays._uobjfreelist
1156 # ifdef ATOMIC_UNCOLLECTABLE
1157 #   define GC_auobjfreelist GC_arrays._auobjfreelist
1158 # endif
1159 # define GC_sobjfreelist GC_arrays._sobjfreelist
1160 # define GC_valid_offsets GC_arrays._valid_offsets
1161 # define GC_modws_valid_offsets GC_arrays._modws_valid_offsets
1162 # ifdef STUBBORN_ALLOC
1163 #    define GC_changed_pages GC_arrays._changed_pages
1164 #    define GC_prev_changed_pages GC_arrays._prev_changed_pages
1165 # endif
1166 # ifdef MARK_BIT_PER_GRANULE
1167 #   define GC_obj_map GC_arrays._obj_map
1168 # endif
1169 # define GC_last_heap_addr GC_arrays._last_heap_addr
1170 # define GC_prev_heap_addr GC_arrays._prev_heap_addr
1171 # define GC_large_free_bytes GC_arrays._large_free_bytes
1172 # define GC_large_allocd_bytes GC_arrays._large_allocd_bytes
1173 # define GC_max_large_allocd_bytes GC_arrays._max_large_allocd_bytes
1174 # define GC_bytes_dropped GC_arrays._bytes_dropped
1175 # define GC_bytes_finalized GC_arrays._bytes_finalized
1176 # define GC_non_gc_bytes_at_gc GC_arrays._non_gc_bytes_at_gc
1177 # define GC_bytes_freed GC_arrays._bytes_freed
1178 # define GC_finalizer_bytes_freed GC_arrays._finalizer_bytes_freed
1179 # define GC_scratch_end_ptr GC_arrays._scratch_end_ptr
1180 # define GC_scratch_last_end_ptr GC_arrays._scratch_last_end_ptr
1181 # define GC_mark_procs GC_arrays._mark_procs
1182 # define GC_heapsize GC_arrays._heapsize
1183 # define GC_max_heapsize GC_arrays._max_heapsize
1184 # define GC_requested_heapsize GC_arrays._requested_heapsize
1185 # define GC_bytes_allocd_before_gc GC_arrays._bytes_allocd_before_gc
1186 # define GC_heap_sects GC_arrays._heap_sects
1187 # ifdef USE_PROC_FOR_LIBRARIES
1188 #   define GC_our_memory GC_arrays._our_memory
1189 # endif
1190 # define GC_last_stack GC_arrays._last_stack
1191 #ifdef ENABLE_TRACE
1192 #define GC_trace_addr GC_arrays._trace_addr
1193 #endif
1194 # ifdef USE_MUNMAP
1195 #   define GC_unmapped_bytes GC_arrays._unmapped_bytes
1196 # else
1197 #   define GC_unmapped_bytes 0
1198 # endif
1199 # if defined(MSWIN32) || defined(MSWINCE)
1200 #   define GC_heap_bases GC_arrays._heap_bases
1201 # endif
1202 # ifdef MSWINCE
1203 #   define GC_heap_lengths GC_arrays._heap_lengths
1204 # endif
1205 # define GC_static_roots GC_arrays._static_roots
1206 # define GC_root_index GC_arrays._root_index
1207 # define GC_excl_table GC_arrays._excl_table
1208 # define GC_all_nils GC_arrays._all_nils
1209 # define GC_top_index GC_arrays._top_index
1210 # if defined(PROC_VDB) || defined(MPROTECT_VDB) || \
1211      defined(GWW_VDB) || defined(MANUAL_VDB)
1212 #   define GC_grungy_pages GC_arrays._grungy_pages
1213 # endif
1214 # if defined(MPROTECT_VDB) || defined(MANUAL_VDB)
1215 #   define GC_dirty_pages GC_arrays._dirty_pages
1216 # endif
1217 # if defined(PROC_VDB) || defined(GWW_VDB)
1218 #   define GC_written_pages GC_arrays._written_pages
1219 # endif
1220 # define GC_composite_in_use GC_arrays._composite_in_use
1221 # define GC_atomic_in_use GC_arrays._atomic_in_use
1222 # define GC_size_map GC_arrays._size_map
1223
1224 # define beginGC_arrays ((ptr_t)(&GC_arrays))
1225 # define endGC_arrays (((ptr_t)(&GC_arrays)) + (sizeof GC_arrays))
1226
1227 #define USED_HEAP_SIZE (GC_heapsize - GC_large_free_bytes)
1228
1229 /* Object kinds: */
1230 # define MAXOBJKINDS 16
1231
1232 GC_EXTERN struct obj_kind {
1233    void **ok_freelist;  /* Array of free listheaders for this kind of object */
1234                         /* Point either to GC_arrays or to storage allocated */
1235                         /* with GC_scratch_alloc.                            */
1236    struct hblk **ok_reclaim_list;
1237                         /* List headers for lists of blocks waiting to be */
1238                         /* swept.                                         */
1239                         /* Indexed by object size in granules.            */
1240    word ok_descriptor;  /* Descriptor template for objects in this      */
1241                         /* block.                                       */
1242    GC_bool ok_relocate_descr;
1243                         /* Add object size in bytes to descriptor       */
1244                         /* template to obtain descriptor.  Otherwise    */
1245                         /* template is used as is.                      */
1246    GC_bool ok_init;   /* Clear objects before putting them on the free list. */
1247 } GC_obj_kinds[MAXOBJKINDS];
1248
1249 # define beginGC_obj_kinds ((ptr_t)(&GC_obj_kinds))
1250 # define endGC_obj_kinds (beginGC_obj_kinds + (sizeof GC_obj_kinds))
1251
1252 /* Variables that used to be in GC_arrays, but need to be accessed by   */
1253 /* inline allocation code.  If they were in GC_arrays, the inlined      */
1254 /* allocation code would include GC_arrays offsets (as it did), which   */
1255 /* introduce maintenance problems.                                      */
1256
1257 #ifdef SEPARATE_GLOBALS
1258   extern word GC_bytes_allocd;
1259         /* Number of words allocated during this collection cycle */
1260   extern ptr_t GC_objfreelist[MAXOBJGRANULES+1];
1261                           /* free list for NORMAL objects */
1262 # define beginGC_objfreelist ((ptr_t)(&GC_objfreelist))
1263 # define endGC_objfreelist (beginGC_objfreelist + sizeof(GC_objfreelist))
1264
1265   extern ptr_t GC_aobjfreelist[MAXOBJGRANULES+1];
1266                           /* free list for atomic (PTRFREE) objs        */
1267 # define beginGC_aobjfreelist ((ptr_t)(&GC_aobjfreelist))
1268 # define endGC_aobjfreelist (beginGC_aobjfreelist + sizeof(GC_aobjfreelist))
1269 #endif
1270
1271 /* Predefined kinds: */
1272 # define PTRFREE 0
1273 # define NORMAL  1
1274 # define UNCOLLECTABLE 2
1275 # ifdef ATOMIC_UNCOLLECTABLE
1276 #   define AUNCOLLECTABLE 3
1277 #   define STUBBORN 4
1278 #   define IS_UNCOLLECTABLE(k) (((k) & ~1) == UNCOLLECTABLE)
1279 # else
1280 #   define STUBBORN 3
1281 #   define IS_UNCOLLECTABLE(k) ((k) == UNCOLLECTABLE)
1282 # endif
1283
1284 GC_EXTERN unsigned GC_n_kinds;
1285
1286 GC_EXTERN word GC_n_heap_sects; /* Number of separately added heap      */
1287                                 /* sections.                            */
1288
1289 #ifdef USE_PROC_FOR_LIBRARIES
1290   GC_EXTERN word GC_n_memory;   /* Number of GET_MEM allocated memory   */
1291                                 /* sections.                            */
1292 #endif
1293
1294 GC_EXTERN word GC_page_size;
1295
1296 #if defined(MSWIN32) || defined(MSWINCE)
1297   struct _SYSTEM_INFO;
1298   GC_EXTERN struct _SYSTEM_INFO GC_sysinfo;
1299 #endif
1300
1301 GC_EXTERN word GC_black_list_spacing;
1302                         /* Average number of bytes between blacklisted  */
1303                         /* blocks. Approximate.                         */
1304                         /* Counts only blocks that are                  */
1305                         /* "stack-blacklisted", i.e. that are           */
1306                         /* problematic in the interior of an object.    */
1307
1308 #ifdef GC_DISABLE_INCREMENTAL
1309 # define GC_incremental FALSE
1310                         /* Hopefully allow optimizer to remove some code. */
1311 # define TRUE_INCREMENTAL FALSE
1312 #else
1313   GC_EXTERN GC_bool GC_incremental;
1314                         /* Using incremental/generational collection. */
1315 # define TRUE_INCREMENTAL \
1316         (GC_incremental && GC_time_limit != GC_TIME_UNLIMITED)
1317         /* True incremental, not just generational, mode */
1318 #endif /* !GC_DISABLE_INCREMENTAL */
1319
1320 GC_EXTERN GC_bool GC_dirty_maintained;
1321                                 /* Dirty bits are being maintained,     */
1322                                 /* either for incremental collection,   */
1323                                 /* or to limit the root set.            */
1324
1325 GC_EXTERN word GC_root_size; /* Total size of registered root sections. */
1326
1327 GC_EXTERN GC_bool GC_debugging_started;
1328                                 /* GC_debug_malloc has been called.     */
1329
1330 /* This is used by GC_do_blocking[_inner]().            */
1331 struct blocking_data {
1332     GC_fn_type fn;
1333     void * client_data; /* and result */
1334 };
1335
1336 /* This is used by GC_call_with_gc_active(), GC_push_all_stack_frames(). */
1337 struct GC_activation_frame_s {
1338   ptr_t saved_stack_ptr;
1339 #ifdef IA64
1340     ptr_t saved_backing_store_ptr;
1341     ptr_t backing_store_end;
1342 #endif
1343   struct GC_activation_frame_s *prev;
1344 };
1345
1346 #ifdef THREADS
1347 /* Process all activation "frames" - scan entire stack except for       */
1348 /* frames belonging to the user functions invoked by GC_do_blocking().  */
1349   GC_INNER void GC_push_all_stack_frames(ptr_t lo, ptr_t hi,
1350                         struct GC_activation_frame_s *activation_frame);
1351   GC_EXTERN word GC_total_stacksize; /* updated on every push_all_stacks */
1352 #else
1353   GC_EXTERN ptr_t GC_blocked_sp;
1354   GC_EXTERN struct GC_activation_frame_s *GC_activation_frame;
1355                         /* Points to the "frame" data held in stack by  */
1356                         /* the innermost GC_call_with_gc_active().      */
1357                         /* NULL if no such "frame" active.              */
1358 #endif /* !THREADS */
1359
1360 #ifdef IA64
1361   /* Similar to GC_push_all_stack_frames() but for IA-64 registers store. */
1362   GC_INNER void GC_push_all_register_frames(ptr_t bs_lo, ptr_t bs_hi,
1363                    int eager, struct GC_activation_frame_s *activation_frame);
1364 #endif
1365
1366 /*  Marks are in a reserved area in                          */
1367 /*  each heap block.  Each word has one mark bit associated  */
1368 /*  with it. Only those corresponding to the beginning of an */
1369 /*  object are used.                                         */
1370
1371 /* Set mark bit correctly, even if mark bits may be concurrently        */
1372 /* accessed.                                                            */
1373 #ifdef PARALLEL_MARK
1374 # define OR_WORD(addr, bits) \
1375         { AO_or((volatile AO_t *)(addr), (AO_t)bits); }
1376 #else
1377 # define OR_WORD(addr, bits) *(addr) |= (bits)
1378 #endif
1379
1380 /* Mark bit operations */
1381
1382 /*
1383  * Retrieve, set, clear the nth mark bit in a given heap block.
1384  *
1385  * (Recall that bit n corresponds to nth object or allocation granule
1386  * relative to the beginning of the block, including unused words)
1387  */
1388
1389 #ifdef USE_MARK_BYTES
1390 # define mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[n])
1391 # define set_mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[n]) = 1
1392 # define clear_mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[n]) = 0
1393 #else /* !USE_MARK_BYTES */
1394 # define mark_bit_from_hdr(hhdr,n) (((hhdr)->hb_marks[divWORDSZ(n)] \
1395                             >> (modWORDSZ(n))) & (word)1)
1396 # define set_mark_bit_from_hdr(hhdr,n) \
1397                             OR_WORD((hhdr)->hb_marks+divWORDSZ(n), \
1398                                     (word)1 << modWORDSZ(n))
1399 # define clear_mark_bit_from_hdr(hhdr,n) (hhdr)->hb_marks[divWORDSZ(n)] \
1400                                 &= ~((word)1 << modWORDSZ(n))
1401 #endif /* !USE_MARK_BYTES */
1402
1403 #ifdef MARK_BIT_PER_OBJ
1404 #  define MARK_BIT_NO(offset, sz) (((unsigned)(offset))/(sz))
1405         /* Get the mark bit index corresponding to the given byte       */
1406         /* offset and size (in bytes).                                  */
1407 #  define MARK_BIT_OFFSET(sz) 1
1408         /* Spacing between useful mark bits.                            */
1409 #  define IF_PER_OBJ(x) x
1410 #  define FINAL_MARK_BIT(sz) ((sz) > MAXOBJBYTES? 1 : HBLK_OBJS(sz))
1411         /* Position of final, always set, mark bit.                     */
1412 #else /* MARK_BIT_PER_GRANULE */
1413 #  define MARK_BIT_NO(offset, sz) BYTES_TO_GRANULES((unsigned)(offset))
1414 #  define MARK_BIT_OFFSET(sz) BYTES_TO_GRANULES(sz)
1415 #  define IF_PER_OBJ(x)
1416 #  define FINAL_MARK_BIT(sz) \
1417         ((sz) > MAXOBJBYTES? MARK_BITS_PER_HBLK \
1418                         : BYTES_TO_GRANULES((sz) * HBLK_OBJS(sz)))
1419 #endif
1420
1421 /* Important internal collector routines */
1422
1423 GC_INNER ptr_t GC_approx_sp(void);
1424
1425 GC_INNER GC_bool GC_should_collect(void);
1426
1427 void GC_apply_to_all_blocks(void (*fn)(struct hblk *h, word client_data),
1428                             word client_data);
1429                         /* Invoke fn(hbp, client_data) for each         */
1430                         /* allocated heap block.                        */
1431 GC_INNER struct hblk * GC_next_used_block(struct hblk * h);
1432                         /* Return first in-use block >= h       */
1433 GC_INNER struct hblk * GC_prev_block(struct hblk * h);
1434                         /* Return last block <= h.  Returned block      */
1435                         /* is managed by GC, but may or may not be in   */
1436                         /* use.                                         */
1437 GC_INNER void GC_mark_init(void);
1438 GC_INNER void GC_clear_marks(void);
1439                         /* Clear mark bits for all heap objects.        */
1440 GC_INNER void GC_invalidate_mark_state(void);
1441                                 /* Tell the marker that marked          */
1442                                 /* objects may point to unmarked        */
1443                                 /* ones, and roots may point to         */
1444                                 /* unmarked objects.  Reset mark stack. */
1445 GC_INNER GC_bool GC_mark_stack_empty(void);
1446 GC_INNER GC_bool GC_mark_some(ptr_t cold_gc_frame);
1447                         /* Perform about one pages worth of marking     */
1448                         /* work of whatever kind is needed.  Returns    */
1449                         /* quickly if no collection is in progress.     */
1450                         /* Return TRUE if mark phase finished.          */
1451 GC_INNER void GC_initiate_gc(void);
1452                                 /* initiate collection.                 */
1453                                 /* If the mark state is invalid, this   */
1454                                 /* becomes full collection.  Otherwise  */
1455                                 /* it's partial.                        */
1456
1457 GC_INNER GC_bool GC_collection_in_progress(void);
1458                         /* Collection is in progress, or was abandoned. */
1459
1460 GC_INNER void GC_push_all(ptr_t bottom, ptr_t top);
1461                                 /* Push everything in a range           */
1462                                 /* onto mark stack.                     */
1463 #ifndef GC_DISABLE_INCREMENTAL
1464   GC_INNER void GC_push_conditional(ptr_t b, ptr_t t, GC_bool all);
1465 #else
1466 # define GC_push_conditional(b, t, all) GC_push_all(b, t)
1467 #endif
1468                                 /* Do either of the above, depending    */
1469                                 /* on the third arg.                    */
1470 GC_INNER void GC_push_all_stack(ptr_t b, ptr_t t);
1471                                     /* As above, but consider           */
1472                                     /*  interior pointers as valid      */
1473 GC_INNER void GC_push_all_eager(ptr_t b, ptr_t t);
1474                                     /* Same as GC_push_all_stack, but   */
1475                                     /* ensures that stack is scanned    */
1476                                     /* immediately, not just scheduled  */
1477                                     /* for scanning.                    */
1478
1479   /* In the threads case, we push part of the current thread stack      */
1480   /* with GC_push_all_eager when we push the registers.  This gets the  */
1481   /* callee-save registers that may disappear.  The remainder of the    */
1482   /* stacks are scheduled for scanning in *GC_push_other_roots, which   */
1483   /* is thread-package-specific.                                        */
1484
1485 GC_INNER void GC_push_roots(GC_bool all, ptr_t cold_gc_frame);
1486                                         /* Push all or dirty roots.     */
1487
1488 GC_EXTERN void (*GC_push_other_roots)(void);
1489                         /* Push system or application specific roots    */
1490                         /* onto the mark stack.  In some environments   */
1491                         /* (e.g. threads environments) this is          */
1492                         /* predefined to be non-zero.  A client         */
1493                         /* supplied replacement should also call the    */
1494                         /* original function.                           */
1495
1496 GC_INNER void GC_push_finalizer_structures(void);
1497 #ifdef THREADS
1498   void GC_push_thread_structures(void);
1499 #endif
1500 GC_EXTERN void (*GC_push_typed_structures)(void);
1501                         /* A pointer such that we can avoid linking in  */
1502                         /* the typed allocation support if unused.      */
1503
1504 GC_INNER void GC_with_callee_saves_pushed(void (*fn)(ptr_t, void *),
1505                                           ptr_t arg);
1506
1507 #if defined(SPARC) || defined(IA64)
1508   /* Cause all stacked registers to be saved in memory.  Return a       */
1509   /* pointer to the top of the corresponding memory stack.              */
1510   ptr_t GC_save_regs_in_stack(void);
1511 #endif
1512                         /* Push register contents onto mark stack.      */
1513
1514 #if defined(MSWIN32) || defined(MSWINCE)
1515   void __cdecl GC_push_one(word p);
1516 #else
1517   void GC_push_one(word p);
1518                               /* If p points to an object, mark it    */
1519                               /* and push contents on the mark stack  */
1520                               /* Pointer recognition test always      */
1521                               /* accepts interior pointers, i.e. this */
1522                               /* is appropriate for pointers found on */
1523                               /* stack.                               */
1524 #endif
1525
1526 #if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
1527   GC_INNER void GC_mark_and_push_stack(ptr_t p, ptr_t source);
1528                                 /* Ditto, omits plausibility test       */
1529 #else
1530   GC_INNER void GC_mark_and_push_stack(ptr_t p);
1531 #endif
1532
1533 GC_INNER void GC_clear_hdr_marks(hdr * hhdr);
1534                                     /* Clear the mark bits in a header */
1535 GC_INNER void GC_set_hdr_marks(hdr * hhdr);
1536                                     /* Set the mark bits in a header */
1537 GC_INNER void GC_set_fl_marks(ptr_t p);
1538                                     /* Set all mark bits associated with */
1539                                     /* a free list.                      */
1540 #ifdef GC_ASSERTIONS
1541   void GC_check_fl_marks(ptr_t p);
1542                                     /* Check that all mark bits         */
1543                                     /* associated with a free list are  */
1544                                     /* set.  Abort if not.              */
1545 #endif
1546 void GC_add_roots_inner(ptr_t b, ptr_t e, GC_bool tmp);
1547 GC_INNER void GC_exclude_static_roots_inner(void *start, void *finish);
1548 GC_INNER void GC_register_dynamic_libraries(void);
1549                 /* Add dynamic library data sections to the root set. */
1550 GC_INNER void GC_cond_register_dynamic_libraries(void);
1551                 /* Remove and reregister dynamic libraries if we're     */
1552                 /* configured to do that at each GC.                    */
1553
1554 /* Machine dependent startup routines */
1555 ptr_t GC_get_main_stack_base(void);     /* Cold end of stack.           */
1556 #ifdef IA64
1557   GC_INNER ptr_t GC_get_register_stack_base(void);
1558                                         /* Cold end of register stack.  */
1559 #endif
1560 void GC_register_data_segments(void);
1561
1562 #ifdef THREADS
1563   GC_INNER void GC_thr_init(void);
1564   GC_INNER void GC_init_parallel(void);
1565 #else
1566   GC_INNER GC_bool GC_is_static_root(ptr_t p);
1567                 /* Is the address p in one of the registered static     */
1568                 /* root sections?                                       */
1569 #endif
1570
1571 /* Black listing: */
1572 GC_INNER void GC_bl_init(void);
1573 # ifdef PRINT_BLACK_LIST
1574       GC_INNER void GC_add_to_black_list_normal(word p, ptr_t source);
1575                         /* Register bits as a possible future false     */
1576                         /* reference from the heap or static data       */
1577 #     define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1578                 if (GC_all_interior_pointers) { \
1579                   GC_add_to_black_list_stack((word)(bits), (source)); \
1580                 } else { \
1581                   GC_add_to_black_list_normal((word)(bits), (source)); \
1582                 }
1583 # else
1584       GC_INNER void GC_add_to_black_list_normal(word p);
1585 #     define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1586                 if (GC_all_interior_pointers) { \
1587                   GC_add_to_black_list_stack((word)(bits)); \
1588                 } else { \
1589                   GC_add_to_black_list_normal((word)(bits)); \
1590                 }
1591 # endif
1592
1593 # ifdef PRINT_BLACK_LIST
1594     GC_INNER void GC_add_to_black_list_stack(word p, ptr_t source);
1595 #   define GC_ADD_TO_BLACK_LIST_STACK(bits, source) \
1596             GC_add_to_black_list_stack((word)(bits), (source))
1597 # else
1598     GC_INNER void GC_add_to_black_list_stack(word p);
1599 #   define GC_ADD_TO_BLACK_LIST_STACK(bits, source) \
1600             GC_add_to_black_list_stack((word)(bits))
1601 # endif
1602 GC_INNER struct hblk * GC_is_black_listed(struct hblk * h, word len);
1603                         /* If there are likely to be false references   */
1604                         /* to a block starting at h of the indicated    */
1605                         /* length, then return the next plausible       */
1606                         /* starting location for h that might avoid     */
1607                         /* these false references.                      */
1608 GC_INNER void GC_promote_black_lists(void);
1609                         /* Declare an end to a black listing phase.     */
1610 GC_INNER void GC_unpromote_black_lists(void);
1611                         /* Approximately undo the effect of the above.  */
1612                         /* This actually loses some information, but    */
1613                         /* only in a reasonably safe way.               */
1614
1615 GC_INNER ptr_t GC_scratch_alloc(size_t bytes);
1616                                 /* GC internal memory allocation for    */
1617                                 /* small objects.  Deallocation is not  */
1618                                 /* possible.                            */
1619
1620 /* Heap block layout maps: */
1621 GC_INNER GC_bool GC_add_map_entry(size_t sz);
1622                                 /* Add a heap block map for objects of  */
1623                                 /* size sz to obj_map.                  */
1624                                 /* Return FALSE on failure.             */
1625 GC_INNER void GC_register_displacement_inner(size_t offset);
1626                                 /* Version of GC_register_displacement  */
1627                                 /* that assumes lock is already held.   */
1628
1629 GC_INNER void GC_initialize_offsets(void);
1630                                 /* Initialize GC_valid_offsets,         */
1631                                 /* depending on current                 */
1632                                 /* GC_all_interior_pointers settings.   */
1633
1634 /*  hblk allocation: */
1635 GC_INNER void GC_new_hblk(size_t size_in_granules, int kind);
1636                                 /* Allocate a new heap block, and build */
1637                                 /* a free list in it.                   */
1638
1639 GC_INNER ptr_t GC_build_fl(struct hblk *h, size_t words, GC_bool clear,
1640                            ptr_t list);
1641                                 /* Build a free list for objects of     */
1642                                 /* size sz in block h.  Append list to  */
1643                                 /* end of the free lists.  Possibly     */
1644                                 /* clear objects on the list.  Normally */
1645                                 /* called by GC_new_hblk, but also      */
1646                                 /* called explicitly without GC lock.   */
1647
1648 GC_INNER struct hblk * GC_allochblk(size_t size_in_bytes, int kind,
1649                                     unsigned flags);
1650                                 /* Allocate a heap block, inform        */
1651                                 /* the marker that block is valid       */
1652                                 /* for objects of indicated size.       */
1653
1654 GC_INNER ptr_t GC_alloc_large(size_t lb, int k, unsigned flags);
1655                         /* Allocate a large block of size lb bytes.     */
1656                         /* The block is not cleared.                    */
1657                         /* Flags is 0 or IGNORE_OFF_PAGE.               */
1658                         /* Calls GC_allchblk to do the actual           */
1659                         /* allocation, but also triggers GC and/or      */
1660                         /* heap expansion as appropriate.               */
1661                         /* Does not update GC_bytes_allocd, but does    */
1662                         /* other accounting.                            */
1663
1664 GC_INNER void GC_freehblk(struct hblk * p);
1665                                 /* Deallocate a heap block and mark it  */
1666                                 /* as invalid.                          */
1667
1668 /*  Misc GC: */
1669 GC_INNER GC_bool GC_expand_hp_inner(word n);
1670 GC_INNER void GC_start_reclaim(int abort_if_found);
1671                                 /* Restore unmarked objects to free     */
1672                                 /* lists, or (if abort_if_found is      */
1673                                 /* TRUE) report them.                   */
1674                                 /* Sweeping of small object pages is    */
1675                                 /* largely deferred.                    */
1676 GC_INNER void GC_continue_reclaim(size_t sz, int kind);
1677                                 /* Sweep pages of the given size and    */
1678                                 /* kind, as long as possible, and       */
1679                                 /* as long as the corr. free list is    */
1680                                 /* empty.  Sz is in granules.           */
1681
1682 GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old);
1683                                 /* Reclaim all blocks.  Abort (in a     */
1684                                 /* consistent state) if f returns TRUE. */
1685 GC_INNER ptr_t GC_reclaim_generic(struct hblk * hbp, hdr *hhdr, size_t sz,
1686                                   GC_bool init, ptr_t list,
1687                                   signed_word *count);
1688                                 /* Rebuild free list in hbp with        */
1689                                 /* header hhdr, with objects of size sz */
1690                                 /* bytes.  Add list to the end of the   */
1691                                 /* free list.  Add the number of        */
1692                                 /* reclaimed bytes to *count.           */
1693 GC_INNER GC_bool GC_block_empty(hdr * hhdr);
1694                                 /* Block completely unmarked?   */
1695 GC_INNER int GC_CALLBACK GC_never_stop_func(void);
1696                                 /* Always returns 0 (FALSE).            */
1697 GC_INNER GC_bool GC_try_to_collect_inner(GC_stop_func f);
1698
1699                                 /* Collect; caller must have acquired   */
1700                                 /* lock.  Collection is aborted if f    */
1701                                 /* returns TRUE.  Returns TRUE if it    */
1702                                 /* completes successfully.              */
1703 # define GC_gcollect_inner() \
1704                 (void)GC_try_to_collect_inner(GC_never_stop_func)
1705
1706 GC_EXTERN GC_bool GC_is_initialized; /* GC_init() has been run. */
1707
1708 #if defined(MSWIN32) || defined(MSWINCE)
1709   void GC_deinit(void);
1710                                 /* Free any resources allocated by      */
1711                                 /* GC_init                              */
1712 #endif
1713
1714 GC_INNER void GC_collect_a_little_inner(int n);
1715                                 /* Do n units worth of garbage          */
1716                                 /* collection work, if appropriate.     */
1717                                 /* A unit is an amount appropriate for  */
1718                                 /* HBLKSIZE bytes of allocation.        */
1719 /* void * GC_generic_malloc(size_t lb, int k); */
1720                                 /* Allocate an object of the given      */
1721                                 /* kind.  By default, there are only    */
1722                                 /* a few kinds: composite(pointerfree), */
1723                                 /* atomic, uncollectable, etc.          */
1724                                 /* We claim it's possible for clever    */
1725                                 /* client code that understands GC      */
1726                                 /* internals to add more, e.g. to       */
1727                                 /* communicate object layout info       */
1728                                 /* to the collector.                    */
1729                                 /* The actual decl is in gc_mark.h.     */
1730 GC_INNER void * GC_generic_malloc_ignore_off_page(size_t b, int k);
1731                                 /* As above, but pointers past the      */
1732                                 /* first page of the resulting object   */
1733                                 /* are ignored.                         */
1734 GC_INNER void * GC_generic_malloc_inner(size_t lb, int k);
1735                                 /* Ditto, but I already hold lock, etc. */
1736 GC_INNER void * GC_generic_malloc_inner_ignore_off_page(size_t lb, int k);
1737                                 /* Allocate an object, where            */
1738                                 /* the client guarantees that there     */
1739                                 /* will always be a pointer to the      */
1740                                 /* beginning of the object while the    */
1741                                 /* object is live.                      */
1742
1743 GC_INNER ptr_t GC_allocobj(size_t sz, int kind);
1744                                 /* Make the indicated                   */
1745                                 /* free list nonempty, and return its   */
1746                                 /* head.  Sz is in granules.            */
1747
1748 GC_INNER void * GC_clear_stack(void *);
1749                                 /* in misc.c, behaves like identity.    */
1750
1751 /* We make the GC_clear_stack() call a tail one, hoping to get more of  */
1752 /* the stack.                                                           */
1753 #define GENERAL_MALLOC(lb,k) \
1754     GC_clear_stack(GC_generic_malloc(lb, k))
1755 #define GENERAL_MALLOC_IOP(lb,k) \
1756     GC_clear_stack(GC_generic_malloc_ignore_off_page(lb, k))
1757
1758 /* Allocation routines that bypass the thread local cache.      */
1759 #ifdef THREAD_LOCAL_ALLOC
1760   GC_INNER void * GC_core_malloc(size_t);
1761   GC_INNER void * GC_core_malloc_atomic(size_t);
1762 # ifdef GC_GCJ_SUPPORT
1763     GC_INNER void * GC_core_gcj_malloc(size_t, void *);
1764 # endif
1765 #endif /* THREAD_LOCAL_ALLOC */
1766
1767 GC_INNER void GC_init_headers(void);
1768 GC_INNER struct hblkhdr * GC_install_header(struct hblk *h);
1769                                 /* Install a header for block h.        */
1770                                 /* Return 0 on failure, or the header   */
1771                                 /* otherwise.                           */
1772 GC_INNER GC_bool GC_install_counts(struct hblk * h, size_t sz);
1773                                 /* Set up forwarding counts for block   */
1774                                 /* h of size sz.                        */
1775                                 /* Return FALSE on failure.             */
1776 GC_INNER void GC_remove_header(struct hblk * h);
1777                                 /* Remove the header for block h.       */
1778 GC_INNER void GC_remove_counts(struct hblk * h, size_t sz);
1779                                 /* Remove forwarding counts for h.      */
1780 GC_INNER hdr * GC_find_header(ptr_t h);
1781
1782 GC_INNER void GC_finalize(void);
1783                         /* Perform all indicated finalization actions   */
1784                         /* on unmarked objects.                         */
1785                         /* Unreachable finalizable objects are enqueued */
1786                         /* for processing by GC_invoke_finalizers.      */
1787                         /* Invoked with lock.                           */
1788
1789 GC_INNER void GC_notify_or_invoke_finalizers(void);
1790                         /* If GC_finalize_on_demand is not set, invoke  */
1791                         /* eligible finalizers. Otherwise:              */
1792                         /* Call *GC_finalizer_notifier if there are     */
1793                         /* finalizers to be run, and we haven't called  */
1794                         /* this procedure yet this GC cycle.            */
1795
1796 GC_INNER void GC_add_to_heap(struct hblk *p, size_t bytes);
1797                         /* Add a HBLKSIZE aligned chunk to the heap.    */
1798
1799 #ifdef USE_PROC_FOR_LIBRARIES
1800   GC_INNER void GC_add_to_our_memory(ptr_t p, size_t bytes);
1801                         /* Add a chunk to GC_our_memory.        */
1802                         /* If p == 0, do nothing.               */
1803 #else
1804 # define GC_add_to_our_memory(p, bytes)
1805 #endif
1806
1807 GC_INNER void GC_print_all_errors(void);
1808                         /* Print smashed and leaked objects, if any.    */
1809                         /* Clear the lists of such objects.             */
1810
1811 GC_EXTERN void (*GC_check_heap)(void);
1812                         /* Check that all objects in the heap with      */
1813                         /* debugging info are intact.                   */
1814                         /* Add any that are not to GC_smashed list.     */
1815 GC_EXTERN void (*GC_print_all_smashed)(void);
1816                         /* Print GC_smashed if it's not empty.          */
1817                         /* Clear GC_smashed list.                       */
1818 GC_EXTERN void (*GC_print_heap_obj)(ptr_t p);
1819                         /* If possible print s followed by a more       */
1820                         /* detailed description of the object           */
1821                         /* referred to by p.                            */
1822
1823 #if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
1824   void GC_print_address_map(void);
1825                         /* Print an address map of the process.         */
1826 #endif
1827
1828 GC_EXTERN GC_bool GC_have_errors; /* We saw a smashed or leaked object. */
1829                                   /* Call error printing routine        */
1830                                   /* occasionally.                      */
1831
1832 #ifndef SMALL_CONFIG
1833   /* GC_print_stats should be visible outside the GC in some cases.     */
1834   extern int GC_print_stats;    /* Nonzero generates basic GC log.      */
1835                                 /* VERBOSE generates add'l messages.    */
1836 #else
1837 # define GC_print_stats 0
1838   /* Will this remove the message character strings from the executable? */
1839   /* With a particular level of optimizations, it should...              */
1840 #endif
1841 #define VERBOSE 2
1842
1843 #ifndef NO_DEBUGGING
1844   GC_EXTERN GC_bool GC_dump_regularly;
1845                                 /* Generate regular debugging dumps.    */
1846 # define COND_DUMP if (GC_dump_regularly) GC_dump();
1847 #else
1848 # define COND_DUMP
1849 #endif
1850
1851 #ifdef KEEP_BACK_PTRS
1852   GC_EXTERN long GC_backtraces;
1853   GC_INNER void GC_generate_random_backtrace_no_gc(void);
1854 #endif
1855
1856 GC_EXTERN GC_bool GC_print_back_height;
1857
1858 #ifdef MAKE_BACK_GRAPH
1859   void GC_print_back_graph_stats(void);
1860 #endif
1861
1862 #ifdef THREADS
1863   GC_INNER void GC_free_inner(void * p);
1864 #endif
1865
1866 /* Macros used for collector internal allocation.       */
1867 /* These assume the collector lock is held.             */
1868 #ifdef DBG_HDRS_ALL
1869     GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k);
1870     GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
1871                                                                   int k);
1872 #   define GC_INTERNAL_MALLOC GC_debug_generic_malloc_inner
1873 #   define GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE \
1874                  GC_debug_generic_malloc_inner_ignore_off_page
1875 #   ifdef THREADS
1876         GC_INNER void GC_debug_free_inner(void * p);
1877 #       define GC_INTERNAL_FREE GC_debug_free_inner
1878 #   else
1879 #       define GC_INTERNAL_FREE GC_debug_free
1880 #   endif
1881 #else
1882 #   define GC_INTERNAL_MALLOC GC_generic_malloc_inner
1883 #   define GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE \
1884                  GC_generic_malloc_inner_ignore_off_page
1885 #   ifdef THREADS
1886 #       define GC_INTERNAL_FREE GC_free_inner
1887 #   else
1888 #       define GC_INTERNAL_FREE GC_free
1889 #   endif
1890 #endif
1891
1892 #ifdef USE_MUNMAP
1893   /* Memory unmapping: */
1894   GC_INNER void GC_unmap_old(void);
1895   GC_INNER void GC_merge_unmapped(void);
1896   GC_INNER void GC_unmap(ptr_t start, size_t bytes);
1897   GC_INNER void GC_remap(ptr_t start, size_t bytes);
1898   GC_INNER void GC_unmap_gap(ptr_t start1, size_t bytes1, ptr_t start2,
1899                              size_t bytes2);
1900 #endif
1901
1902 /* Virtual dirty bit implementation:            */
1903 /* Each implementation exports the following:   */
1904 GC_INNER void GC_read_dirty(void);
1905                         /* Retrieve dirty bits. */
1906 GC_INNER GC_bool GC_page_was_dirty(struct hblk *h);
1907                         /* Read retrieved dirty bits.   */
1908 GC_INNER void GC_remove_protection(struct hblk *h, word nblocks,
1909                                    GC_bool pointerfree);
1910                         /* h is about to be written or allocated.  Ensure   */
1911                         /* that it's not write protected by the virtual     */
1912                         /* dirty bit implementation.                        */
1913
1914 GC_INNER void GC_dirty_init(void);
1915
1916 /* Slow/general mark bit manipulation: */
1917 GC_API_PRIV GC_bool GC_is_marked(ptr_t p);
1918 GC_INNER void GC_clear_mark_bit(ptr_t p);
1919 GC_INNER void GC_set_mark_bit(ptr_t p);
1920
1921 /* Stubborn objects: */
1922 void GC_read_changed(void); /* Analogous to GC_read_dirty */
1923 GC_bool GC_page_was_changed(struct hblk * h);
1924                                 /* Analogous to GC_page_was_dirty */
1925 void GC_clean_changing_list(void);
1926                                 /* Collect obsolete changing list entries */
1927 void GC_stubborn_init(void);
1928
1929 /* Debugging print routines: */
1930 void GC_print_block_list(void);
1931 void GC_print_hblkfreelist(void);
1932 void GC_print_heap_sects(void);
1933 void GC_print_static_roots(void);
1934 GC_INNER void GC_print_finalization_stats(void);
1935 /* void GC_dump(void); - declared in gc.h */
1936
1937 #ifdef KEEP_BACK_PTRS
1938    GC_INNER void GC_store_back_pointer(ptr_t source, ptr_t dest);
1939    GC_INNER void GC_marked_for_finalization(ptr_t dest);
1940 #  define GC_STORE_BACK_PTR(source, dest) GC_store_back_pointer(source, dest)
1941 #  define GC_MARKED_FOR_FINALIZATION(dest) GC_marked_for_finalization(dest)
1942 #else
1943 #  define GC_STORE_BACK_PTR(source, dest)
1944 #  define GC_MARKED_FOR_FINALIZATION(dest)
1945 #endif
1946
1947 /* Make arguments appear live to compiler */
1948 # if defined(__BORLANDC__) || defined(__WATCOMC__)
1949     void GC_noop(void*, ...);
1950 # else
1951 #   ifdef __DMC__
1952       void GC_noop(...);
1953 #   else
1954       void GC_noop();
1955 #   endif
1956 # endif
1957
1958 GC_API void GC_CALL GC_noop1(word);
1959
1960 #ifndef GC_ATTR_FORMAT_PRINTF
1961 # if defined(__GNUC__) && __GNUC__ >= 3
1962 #   define GC_ATTR_FORMAT_PRINTF(spec_argnum, first_checked) \
1963         __attribute__((__format__(__printf__, spec_argnum, first_checked)))
1964 # else
1965 #   define GC_ATTR_FORMAT_PRINTF(spec_argnum, first_checked)
1966 # endif
1967 #endif
1968
1969 /* Logging and diagnostic output:       */
1970 GC_API_PRIV void GC_printf(const char * format, ...)
1971                         GC_ATTR_FORMAT_PRINTF(1, 2);
1972                         /* A version of printf that doesn't allocate,   */
1973                         /* 1K total output length.                      */
1974                         /* (We use sprintf.  Hopefully that doesn't     */
1975                         /* allocate for long arguments.)                */
1976 GC_API_PRIV void GC_err_printf(const char * format, ...)
1977                         GC_ATTR_FORMAT_PRINTF(1, 2);
1978 GC_API_PRIV void GC_log_printf(const char * format, ...)
1979                         GC_ATTR_FORMAT_PRINTF(1, 2);
1980 void GC_err_puts(const char *s);
1981                         /* Write s to stderr, don't buffer, don't add   */
1982                         /* newlines, don't ...                          */
1983
1984 #if defined(LINUX) && !defined(SMALL_CONFIG)
1985   GC_INNER void GC_err_write(const char *buf, size_t len);
1986                         /* Write buf to stderr, don't buffer, don't add */
1987                         /* newlines, don't ...                          */
1988 #endif
1989
1990 GC_EXTERN unsigned GC_fail_count;
1991                         /* How many consecutive GC/expansion failures?  */
1992                         /* Reset by GC_allochblk(); defined in alloc.c. */
1993
1994 GC_EXTERN long GC_large_alloc_warn_interval; /* defined in misc.c */
1995
1996 GC_EXTERN signed_word GC_bytes_found;
1997                 /* Number of reclaimed bytes after garbage collection;  */
1998                 /* protected by GC lock; defined in reclaim.c.          */
1999
2000 #ifdef USE_MUNMAP
2001   GC_EXTERN int GC_unmap_threshold; /* defined in allchblk.c */
2002   GC_EXTERN GC_bool GC_force_unmap_on_gcollect; /* defined in misc.c */
2003 #endif
2004
2005 #ifdef MSWIN32
2006   GC_EXTERN GC_bool GC_no_win32_dlls; /* defined in os_dep.c */
2007   GC_EXTERN GC_bool GC_wnt;     /* Is Windows NT derivative;    */
2008                                 /* defined and set in os_dep.c. */
2009 #endif
2010
2011 #ifdef THREADS
2012 # if defined(MSWIN32) || defined(MSWINCE)
2013     GC_EXTERN CRITICAL_SECTION GC_write_cs; /* defined in misc.c */
2014 #   ifdef GC_ASSERTIONS
2015       GC_EXTERN GC_bool GC_write_disabled;
2016                                 /* defined in win32_threads.c;  */
2017                                 /* protected by GC_write_cs.    */
2018
2019 #   endif
2020 # endif
2021 # ifdef MPROTECT_VDB
2022     GC_EXTERN volatile AO_TS_t GC_fault_handler_lock;
2023                                         /* defined in os_dep.c */
2024 # endif
2025 # ifdef MSWINCE
2026     GC_EXTERN GC_bool GC_dont_query_stack_min;
2027                                 /* Defined and set in os_dep.c. */
2028 # endif
2029 #elif defined(IA64)
2030   GC_EXTERN ptr_t GC_save_regs_ret_val; /* defined in mach_dep.c. */
2031                         /* Previously set to backing store pointer.     */
2032 #endif /* !THREADS */
2033
2034 #ifdef THREAD_LOCAL_ALLOC
2035   GC_EXTERN GC_bool GC_world_stopped; /* defined in alloc.c */
2036 #endif
2037
2038 #ifdef GC_GCJ_SUPPORT
2039   GC_EXTERN GC_bool GC_gcj_malloc_initialized; /* defined in gcj_mlc.c */
2040   GC_EXTERN ptr_t * GC_gcjobjfreelist;
2041 #endif
2042
2043 # ifdef GC_ASSERTIONS
2044 #       define GC_ASSERT(expr) if(!(expr)) {\
2045                 GC_err_printf("Assertion failure: %s:%ld\n", \
2046                                 __FILE__, (unsigned long)__LINE__); \
2047                 ABORT("assertion failure"); }
2048 # else
2049 #       define GC_ASSERT(expr)
2050 # endif
2051
2052 /* Check a compile time assertion at compile time.  The error   */
2053 /* message for failure is a bit baroque, but ...                */
2054 #if defined(mips) && !defined(__GNUC__)
2055 /* DOB: MIPSPro C gets an internal error taking the sizeof an array type.
2056    This code works correctly (ugliness is to avoid "unused var" warnings) */
2057 # define GC_STATIC_ASSERT(expr) \
2058     do { if (0) { char j[(expr)? 1 : -1]; j[0]='\0'; j[0]=j[0]; } } while(0)
2059 #else
2060 # define GC_STATIC_ASSERT(expr) (void)sizeof(char[(expr)? 1 : -1])
2061 #endif
2062
2063 # if defined(PARALLEL_MARK)
2064     /* We need additional synchronization facilities from the thread    */
2065     /* support.  We believe these are less performance critical         */
2066     /* than the main garbage collector lock; standard pthreads-based    */
2067     /* implementations should be sufficient.                            */
2068
2069     GC_EXTERN long GC_markers;  /* Number of mark threads we would like */
2070                                 /* to have.  Includes the initiating    */
2071                                 /* thread.  Defined in mark.c.          */
2072
2073     /* The mark lock and condition variable.  If the GC lock is also    */
2074     /* acquired, the GC lock must be acquired first.  The mark lock is  */
2075     /* used to both protect some variables used by the parallel         */
2076     /* marker, and to protect GC_fl_builder_count, below.               */
2077     /* GC_notify_all_marker() is called when                            */
2078     /* the state of the parallel marker changes                         */
2079     /* in some significant way (see gc_mark.h for details).  The        */
2080     /* latter set of events includes incrementing GC_mark_no.           */
2081     /* GC_notify_all_builder() is called when GC_fl_builder_count       */
2082     /* reaches 0.                                                       */
2083
2084     GC_INNER void GC_acquire_mark_lock(void);
2085     GC_INNER void GC_release_mark_lock(void);
2086     GC_INNER void GC_notify_all_builder(void);
2087     GC_INNER void GC_wait_for_reclaim(void);
2088
2089     GC_EXTERN word GC_fl_builder_count;   /* Protected by mark lock.    */
2090
2091     GC_INNER void GC_notify_all_marker(void);
2092     GC_INNER void GC_wait_marker(void);
2093     GC_EXTERN word GC_mark_no;            /* Protected by mark lock.    */
2094
2095     GC_INNER void GC_help_marker(word my_mark_no);
2096                 /* Try to help out parallel marker for mark cycle       */
2097                 /* my_mark_no.  Returns if the mark cycle finishes or   */
2098                 /* was already done, or there was nothing to do for     */
2099                 /* some other reason.                                   */
2100 # endif /* PARALLEL_MARK */
2101
2102 # if defined(GC_PTHREADS)
2103   /* We define the thread suspension signal here, so that we can refer  */
2104   /* to it in the dirty bit implementation, if necessary.  Ideally we   */
2105   /* would allocate a (real-time ?) signal using the standard mechanism.*/
2106   /* unfortunately, there is no standard mechanism.  (There is one      */
2107   /* in Linux glibc, but it's not exported.)  Thus we continue to use   */
2108   /* the same hard-coded signals we've always used.                     */
2109 #  if !defined(SIG_SUSPEND)
2110 #   if defined(GC_LINUX_THREADS) || defined(GC_DGUX386_THREADS)
2111 #    if defined(SPARC) && !defined(SIGPWR)
2112        /* SPARC/Linux doesn't properly define SIGPWR in <signal.h>.
2113         * It is aliased to SIGLOST in asm/signal.h, though.             */
2114 #      define SIG_SUSPEND SIGLOST
2115 #    else
2116        /* Linuxthreads itself uses SIGUSR1 and SIGUSR2.                 */
2117 #      define SIG_SUSPEND SIGPWR
2118 #    endif
2119 #   else  /* !GC_LINUX_THREADS */
2120 #     if defined(_SIGRTMIN)
2121 #       define SIG_SUSPEND _SIGRTMIN + 6
2122 #     else
2123 #       define SIG_SUSPEND SIGRTMIN + 6
2124 #     endif
2125 #   endif
2126 #  endif /* !SIG_SUSPEND */
2127
2128 # endif
2129
2130 /* Some macros for setjmp that works across signal handlers     */
2131 /* were possible, and a couple of routines to facilitate        */
2132 /* catching accesses to bad addresses when that's               */
2133 /* possible/needed.                                             */
2134 #if defined(UNIX_LIKE) || (defined(NEED_FIND_LIMIT) && defined(CYGWIN32))
2135 # include <setjmp.h>
2136 # if defined(SUNOS5SIGS) && !defined(FREEBSD)
2137 #  include <sys/siginfo.h>
2138 # endif
2139   /* Define SETJMP and friends to be the version that restores  */
2140   /* the signal mask.                                           */
2141 # define SETJMP(env) sigsetjmp(env, 1)
2142 # define LONGJMP(env, val) siglongjmp(env, val)
2143 # define JMP_BUF sigjmp_buf
2144 #else
2145 # ifdef ECOS
2146 #   define SETJMP(env)  hal_setjmp(env)
2147 # else
2148 #   define SETJMP(env) setjmp(env)
2149 # endif
2150 # define LONGJMP(env, val) longjmp(env, val)
2151 # define JMP_BUF jmp_buf
2152 #endif
2153
2154 /* Do we need the GC_find_limit machinery to find the end of a  */
2155 /* data segment.                                                */
2156 # if defined(HEURISTIC2) || defined(SEARCH_FOR_DATA_START)
2157 #   define NEED_FIND_LIMIT
2158 # endif
2159
2160 # if !defined(STACKBOTTOM) && defined(HEURISTIC2)
2161 #   define NEED_FIND_LIMIT
2162 # endif
2163
2164 # if (defined(SVR4) || defined(AUX) || defined(DGUX) \
2165       || (defined(LINUX) && defined(SPARC))) && !defined(PCR)
2166 #   define NEED_FIND_LIMIT
2167 # endif
2168
2169 #if defined(FREEBSD) && (defined(I386) || defined(X86_64) || defined(powerpc) \
2170     || defined(__powerpc__))
2171 #  include <machine/trap.h>
2172 #  if !defined(PCR)
2173 #    define NEED_FIND_LIMIT
2174 #  endif
2175 #endif
2176
2177 #if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__) \
2178     && !defined(NEED_FIND_LIMIT)
2179    /* Used by GC_init_netbsd_elf() in os_dep.c. */
2180 #  define NEED_FIND_LIMIT
2181 #endif
2182
2183 #if defined(IA64) && !defined(NEED_FIND_LIMIT)
2184 #  define NEED_FIND_LIMIT
2185      /* May be needed for register backing store base. */
2186 #endif
2187
2188 # if defined(NEED_FIND_LIMIT) || \
2189      defined(USE_PROC_FOR_LIBRARIES) && defined(THREADS)
2190 JMP_BUF GC_jmp_buf;
2191
2192 /* Set up a handler for address faults which will longjmp to    */
2193 /* GC_jmp_buf;                                                  */
2194 GC_INNER void GC_setup_temporary_fault_handler(void);
2195
2196 /* Undo the effect of GC_setup_temporary_fault_handler.         */
2197 GC_INNER void GC_reset_fault_handler(void);
2198
2199 # endif /* Need to handle address faults.       */
2200
2201 /* Some convenience macros for cancellation support. */
2202 # if defined(CANCEL_SAFE)
2203 #   if defined(GC_ASSERTIONS) && (defined(USE_COMPILER_TLS) \
2204        || (defined(LINUX) && !defined(ARM32) \
2205                 && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) \
2206        || defined(HPUX) /* and probably others ... */))
2207       extern __thread unsigned char GC_cancel_disable_count;
2208 #     define NEED_CANCEL_DISABLE_COUNT
2209 #     define INCR_CANCEL_DISABLE() ++GC_cancel_disable_count
2210 #     define DECR_CANCEL_DISABLE() --GC_cancel_disable_count
2211 #     define ASSERT_CANCEL_DISABLED() GC_ASSERT(GC_cancel_disable_count > 0)
2212 #   else
2213 #     define INCR_CANCEL_DISABLE()
2214 #     define DECR_CANCEL_DISABLE()
2215 #     define ASSERT_CANCEL_DISABLED()
2216 #   endif /* GC_ASSERTIONS & ... */
2217 #   define DISABLE_CANCEL(state) \
2218         { pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &state); \
2219           INCR_CANCEL_DISABLE(); }
2220 #   define RESTORE_CANCEL(state) \
2221         { ASSERT_CANCEL_DISABLED(); \
2222           pthread_setcancelstate(state, NULL); \
2223           DECR_CANCEL_DISABLE(); }
2224 # else /* !CANCEL_SAFE */
2225 #   define DISABLE_CANCEL(state)
2226 #   define RESTORE_CANCEL(state)
2227 #   define ASSERT_CANCEL_DISABLED()
2228 # endif /* !CANCEL_SAFE */
2229
2230 # endif /* GC_PRIVATE_H */