OSDN Git Service

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