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