OSDN Git Service

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