OSDN Git Service

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