OSDN Git Service

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