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