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