OSDN Git Service

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