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