OSDN Git Service

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