OSDN Git Service

2000-07-19 Benjamin Kosnik <bkoz@milou.soma.redhat.com>
[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
894
895 /*
896  * Hash table representation of sets of pages.  This assumes it is
897  * OK to add spurious entries to sets.
898  * Used by black-listing code, and perhaps by dirty bit maintenance code.
899  */
900  
901 # ifdef LARGE_CONFIG
902 #   define LOG_PHT_ENTRIES  17
903 # else
904 #   define LOG_PHT_ENTRIES  14  /* Collisions are likely if heap grows  */
905                                 /* to more than 16K hblks = 64MB.       */
906                                 /* Each hash table occupies 2K bytes.   */
907 # endif
908 # define PHT_ENTRIES ((word)1 << LOG_PHT_ENTRIES)
909 # define PHT_SIZE (PHT_ENTRIES >> LOGWL)
910 typedef word page_hash_table[PHT_SIZE];
911
912 # define PHT_HASH(addr) ((((word)(addr)) >> LOG_HBLKSIZE) & (PHT_ENTRIES - 1))
913
914 # define get_pht_entry_from_index(bl, index) \
915                 (((bl)[divWORDSZ(index)] >> modWORDSZ(index)) & 1)
916 # define set_pht_entry_from_index(bl, index) \
917                 (bl)[divWORDSZ(index)] |= (word)1 << modWORDSZ(index)
918 # define clear_pht_entry_from_index(bl, index) \
919                 (bl)[divWORDSZ(index)] &= ~((word)1 << modWORDSZ(index))
920         
921
922
923 /********************************************/
924 /*                                          */
925 /*    H e a p   B l o c k s                 */
926 /*                                          */
927 /********************************************/
928
929 /*  heap block header */
930 #define HBLKMASK   (HBLKSIZE-1)
931
932 #define BITS_PER_HBLK (HBLKSIZE * 8)
933
934 #define MARK_BITS_PER_HBLK (BITS_PER_HBLK/CPP_WORDSZ)
935            /* upper bound                                    */
936            /* We allocate 1 bit/word.  Only the first word   */
937            /* in each object is actually marked.             */
938
939 # ifdef ALIGN_DOUBLE
940 #   define MARK_BITS_SZ (((MARK_BITS_PER_HBLK + 2*CPP_WORDSZ - 1) \
941                           / (2*CPP_WORDSZ))*2)
942 # else
943 #   define MARK_BITS_SZ ((MARK_BITS_PER_HBLK + CPP_WORDSZ - 1)/CPP_WORDSZ)
944 # endif
945            /* Upper bound on number of mark words per heap block  */
946
947 struct hblkhdr {
948     word hb_sz;  /* If in use, size in words, of objects in the block. */
949                  /* if free, the size in bytes of the whole block      */
950     struct hblk * hb_next;      /* Link field for hblk free list         */
951                                 /* and for lists of chunks waiting to be */
952                                 /* reclaimed.                            */
953     struct hblk * hb_prev;      /* Backwards link for free list.        */
954     word hb_descr;              /* object descriptor for marking.  See  */
955                                 /* mark.h.                              */
956     char* hb_map;       /* A pointer to a pointer validity map of the block. */
957                         /* See GC_obj_map.                                   */
958                         /* Valid for all blocks with headers.                */
959                         /* Free blocks point to GC_invalid_map.              */
960     unsigned char hb_obj_kind;
961                          /* Kind of objects in the block.  Each kind    */
962                          /* identifies a mark procedure and a set of    */
963                          /* list headers.  Sometimes called regions.    */
964     unsigned char hb_flags;
965 #       define IGNORE_OFF_PAGE  1       /* Ignore pointers that do not  */
966                                         /* point to the first page of   */
967                                         /* this object.                 */
968 #       define WAS_UNMAPPED 2   /* This is a free block, which has      */
969                                 /* been unmapped from the address       */
970                                 /* space.                               */
971                                 /* GC_remap must be invoked on it       */
972                                 /* before it can be reallocated.        */
973                                 /* Only set with USE_MUNMAP.            */
974     unsigned short hb_last_reclaimed;
975                                 /* Value of GC_gc_no when block was     */
976                                 /* last allocated or swept. May wrap.   */
977                                 /* For a free block, this is maintained */
978                                 /* unly for USE_MUNMAP, and indicates   */
979                                 /* when the header was allocated, or    */
980                                 /* when the size of the block last      */
981                                 /* changed.                             */
982     word hb_marks[MARK_BITS_SZ];
983                             /* Bit i in the array refers to the             */
984                             /* object starting at the ith word (header      */
985                             /* INCLUDED) in the heap block.                 */
986                             /* The lsb of word 0 is numbered 0.             */
987                             /* Unused bits are invalid, and are             */
988                             /* occasionally set, e.g for uncollectable      */
989                             /* objects.                                     */
990 };
991
992 /*  heap block body */
993
994 # define DISCARD_WORDS 0
995         /* Number of words to be dropped at the beginning of each block */
996         /* Must be a multiple of WORDSZ.  May reasonably be nonzero     */
997         /* on machines that don't guarantee longword alignment of       */
998         /* pointers, so that the number of false hits is minimized.     */
999         /* 0 and WORDSZ are probably the only reasonable values.        */
1000
1001 # define BODY_SZ ((HBLKSIZE-WORDS_TO_BYTES(DISCARD_WORDS))/sizeof(word))
1002
1003 struct hblk {
1004 #   if (DISCARD_WORDS != 0)
1005         word garbage[DISCARD_WORDS];
1006 #   endif
1007     word hb_body[BODY_SZ];
1008 };
1009
1010 # define HDR_WORDS ((word)DISCARD_WORDS)
1011 # define HDR_BYTES ((word)WORDS_TO_BYTES(DISCARD_WORDS))
1012
1013 # define OBJ_SZ_TO_BLOCKS(sz) \
1014     divHBLKSZ(HDR_BYTES + WORDS_TO_BYTES(sz) + HBLKSIZE-1)
1015     /* Size of block (in units of HBLKSIZE) needed to hold objects of   */
1016     /* given sz (in words).                                             */
1017
1018 /* Object free list link */
1019 # define obj_link(p) (*(ptr_t *)(p))
1020
1021 /* The type of mark procedures.  This really belongs in gc_mark.h.      */
1022 /* But we put it here, so that we can avoid scanning the mark proc      */
1023 /* table.                                                               */
1024 typedef struct ms_entry * (*mark_proc)(/* word * addr,
1025                                           struct ms_entry *mark_stack_ptr,
1026                                           struct ms_entry *mark_stack_limit,
1027                                           word env */);
1028 # define LOG_MAX_MARK_PROCS 6
1029 # define MAX_MARK_PROCS (1 << LOG_MAX_MARK_PROCS)
1030
1031 /* Root sets.  Logically private to mark_rts.c.  But we don't want the  */
1032 /* tables scanned, so we put them here.                                 */
1033 /* MAX_ROOT_SETS is the maximum number of ranges that can be    */
1034 /* registered as static roots.                                  */
1035 # ifdef LARGE_CONFIG
1036 #   define MAX_ROOT_SETS 4096
1037 # else
1038 #   ifdef PCR
1039 #     define MAX_ROOT_SETS 1024
1040 #   else
1041 #     ifdef MSWIN32
1042 #       define MAX_ROOT_SETS 512
1043             /* Under NT, we add only written pages, which can result    */
1044             /* in many small root sets.                                 */
1045 #     else
1046 #       define MAX_ROOT_SETS 64
1047 #     endif
1048 #   endif
1049 # endif
1050
1051 # define MAX_EXCLUSIONS (MAX_ROOT_SETS/4)
1052 /* Maximum number of segments that can be excluded from root sets.      */
1053
1054 /*
1055  * Data structure for excluded static roots.
1056  */
1057 struct exclusion {
1058     ptr_t e_start;
1059     ptr_t e_end;
1060 };
1061
1062 /* Data structure for list of root sets.                                */
1063 /* We keep a hash table, so that we can filter out duplicate additions. */
1064 /* Under Win32, we need to do a better job of filtering overlaps, so    */
1065 /* we resort to sequential search, and pay the price.                   */
1066 struct roots {
1067         ptr_t r_start;
1068         ptr_t r_end;
1069 #       ifndef MSWIN32
1070           struct roots * r_next;
1071 #       endif
1072         GC_bool r_tmp;
1073                 /* Delete before registering new dynamic libraries */
1074 };
1075
1076 #ifndef MSWIN32
1077     /* Size of hash table index to roots.       */
1078 #   define LOG_RT_SIZE 6
1079 #   define RT_SIZE (1 << LOG_RT_SIZE) /* Power of 2, may be != MAX_ROOT_SETS */
1080 #endif
1081
1082 /* Lists of all heap blocks and free lists      */
1083 /* as well as other random data structures      */
1084 /* that should not be scanned by the            */
1085 /* collector.                                   */
1086 /* These are grouped together in a struct       */
1087 /* so that they can be easily skipped by the    */
1088 /* GC_mark routine.                             */
1089 /* The ordering is weird to make GC_malloc      */
1090 /* faster by keeping the important fields       */
1091 /* sufficiently close together that a           */
1092 /* single load of a base register will do.      */
1093 /* Scalars that could easily appear to          */
1094 /* be pointers are also put here.               */
1095 /* The main fields should precede any           */
1096 /* conditionally included fields, so that       */
1097 /* gc_inl.h will work even if a different set   */
1098 /* of macros is defined when the client is      */
1099 /* compiled.                                    */
1100
1101 struct _GC_arrays {
1102   word _heapsize;
1103   word _max_heapsize;
1104   word _requested_heapsize;     /* Heap size due to explicit expansion */
1105   ptr_t _last_heap_addr;
1106   ptr_t _prev_heap_addr;
1107   word _large_free_bytes;
1108         /* Total bytes contained in blocks on large object free */
1109         /* list.                                                */
1110   word _words_allocd_before_gc;
1111                 /* Number of words allocated before this        */
1112                 /* collection cycle.                            */
1113   word _words_allocd;
1114         /* Number of words allocated during this collection cycle */
1115   word _words_wasted;
1116         /* Number of words wasted due to internal fragmentation */
1117         /* in large objects, or due to dropping blacklisted     */
1118         /* blocks, since last gc.  Approximate.                 */
1119   word _words_finalized;
1120         /* Approximate number of words in objects (and headers) */
1121         /* That became ready for finalization in the last       */
1122         /* collection.                                          */
1123   word _non_gc_bytes_at_gc;
1124         /* Number of explicitly managed bytes of storage        */
1125         /* at last collection.                                  */
1126   word _mem_freed;
1127         /* Number of explicitly deallocated words of memory     */
1128         /* since last collection.                               */
1129   ptr_t _scratch_end_ptr;
1130   ptr_t _scratch_last_end_ptr;
1131         /* Used by headers.c, and can easily appear to point to */
1132         /* heap.                                                */
1133   mark_proc _mark_procs[MAX_MARK_PROCS];
1134         /* Table of user-defined mark procedures.  There is     */
1135         /* a small number of these, which can be referenced     */
1136         /* by DS_PROC mark descriptors.  See gc_mark.h.         */
1137   ptr_t _objfreelist[MAXOBJSZ+1];
1138                           /* free list for objects */
1139   ptr_t _aobjfreelist[MAXOBJSZ+1];
1140                           /* free list for atomic objs  */
1141
1142   ptr_t _uobjfreelist[MAXOBJSZ+1];
1143                           /* uncollectable but traced objs      */
1144                           /* objects on this and auobjfreelist  */
1145                           /* are always marked, except during   */
1146                           /* garbage collections.               */
1147 # ifdef ATOMIC_UNCOLLECTABLE
1148     ptr_t _auobjfreelist[MAXOBJSZ+1];
1149 # endif
1150                           /* uncollectable but traced objs      */
1151
1152 # ifdef GATHERSTATS
1153     word _composite_in_use;
1154                 /* Number of words in accessible composite      */
1155                 /* objects.                                     */
1156     word _atomic_in_use;
1157                 /* Number of words in accessible atomic         */
1158                 /* objects.                                     */
1159 # endif
1160 # ifdef USE_MUNMAP
1161     word _unmapped_bytes;
1162 # endif
1163 # ifdef MERGE_SIZES
1164     unsigned _size_map[WORDS_TO_BYTES(MAXOBJSZ+1)];
1165         /* Number of words to allocate for a given allocation request in */
1166         /* bytes.                                                        */
1167 # endif 
1168
1169 # ifdef STUBBORN_ALLOC
1170     ptr_t _sobjfreelist[MAXOBJSZ+1];
1171 # endif
1172                           /* free list for immutable objects    */
1173   ptr_t _obj_map[MAXOBJSZ+1];
1174                        /* If not NIL, then a pointer to a map of valid  */
1175                        /* object addresses. _obj_map[sz][i] is j if the */
1176                        /* address block_start+i is a valid pointer      */
1177                        /* to an object at                               */
1178                        /* block_start+i&~3 - WORDS_TO_BYTES(j).         */
1179                        /* (If ALL_INTERIOR_POINTERS is defined, then    */
1180                        /* instead ((short *)(hb_map[sz])[i] is j if     */
1181                        /* block_start+WORDS_TO_BYTES(i) is in the       */
1182                        /* interior of an object starting at             */
1183                        /* block_start+WORDS_TO_BYTES(i-j)).             */
1184                        /* It is OBJ_INVALID if                          */
1185                        /* block_start+WORDS_TO_BYTES(i) is not          */
1186                        /* valid as a pointer to an object.              */
1187                        /* We assume all values of j <= OBJ_INVALID.     */
1188                        /* The zeroth entry corresponds to large objects.*/
1189 #   ifdef ALL_INTERIOR_POINTERS
1190 #       define map_entry_type short
1191 #       define OBJ_INVALID 0x7fff
1192 #       define MAP_ENTRY(map, bytes) \
1193                 (((map_entry_type *)(map))[BYTES_TO_WORDS(bytes)])
1194 #       define MAP_ENTRIES BYTES_TO_WORDS(HBLKSIZE)
1195 #       define MAP_SIZE (MAP_ENTRIES * sizeof(map_entry_type))
1196 #       define OFFSET_VALID(displ) TRUE
1197 #       define CPP_MAX_OFFSET (HBLKSIZE - HDR_BYTES - 1)
1198 #       define MAX_OFFSET ((word)CPP_MAX_OFFSET)
1199 #   else
1200 #       define map_entry_type char
1201 #       define OBJ_INVALID 0x7f
1202 #       define MAP_ENTRY(map, bytes) \
1203                 (map)[bytes]
1204 #       define MAP_ENTRIES HBLKSIZE
1205 #       define MAP_SIZE MAP_ENTRIES
1206 #       define CPP_MAX_OFFSET (WORDS_TO_BYTES(OBJ_INVALID) - 1) 
1207 #       define MAX_OFFSET ((word)CPP_MAX_OFFSET)
1208 #       define VALID_OFFSET_SZ \
1209           (CPP_MAX_OFFSET > WORDS_TO_BYTES(CPP_MAXOBJSZ)? \
1210            CPP_MAX_OFFSET+1 \
1211            : WORDS_TO_BYTES(CPP_MAXOBJSZ)+1)
1212         char _valid_offsets[VALID_OFFSET_SZ];
1213                                 /* GC_valid_offsets[i] == TRUE ==> i    */
1214                                 /* is registered as a displacement.     */
1215 #       define OFFSET_VALID(displ) GC_valid_offsets[displ]
1216         char _modws_valid_offsets[sizeof(word)];
1217                                 /* GC_valid_offsets[i] ==>                */
1218                                 /* GC_modws_valid_offsets[i%sizeof(word)] */
1219 #   endif
1220 # ifdef STUBBORN_ALLOC
1221     page_hash_table _changed_pages;
1222         /* Stubborn object pages that were changes since last call to   */
1223         /* GC_read_changed.                                             */
1224     page_hash_table _prev_changed_pages;
1225         /* Stubborn object pages that were changes before last call to  */
1226         /* GC_read_changed.                                             */
1227 # endif
1228 # if defined(PROC_VDB) || defined(MPROTECT_VDB)
1229     page_hash_table _grungy_pages; /* Pages that were dirty at last        */
1230                                      /* GC_read_dirty.                     */
1231 # endif
1232 # ifdef MPROTECT_VDB
1233     VOLATILE page_hash_table _dirty_pages;      
1234                         /* Pages dirtied since last GC_read_dirty. */
1235 # endif
1236 # ifdef PROC_VDB
1237     page_hash_table _written_pages;     /* Pages ever dirtied   */
1238 # endif
1239 # ifdef LARGE_CONFIG
1240 #   if CPP_WORDSZ > 32
1241 #     define MAX_HEAP_SECTS 4096        /* overflows at roughly 64 GB      */
1242 #   else
1243 #     define MAX_HEAP_SECTS 768         /* Separately added heap sections. */
1244 #   endif
1245 # else
1246 #   define MAX_HEAP_SECTS 256
1247 # endif
1248   struct HeapSect {
1249       ptr_t hs_start; word hs_bytes;
1250   } _heap_sects[MAX_HEAP_SECTS];
1251 # ifdef MSWIN32
1252     ptr_t _heap_bases[MAX_HEAP_SECTS];
1253                 /* Start address of memory regions obtained from kernel. */
1254 # endif
1255   struct roots _static_roots[MAX_ROOT_SETS];
1256 # ifndef MSWIN32
1257     struct roots * _root_index[RT_SIZE];
1258 # endif
1259   struct exclusion _excl_table[MAX_EXCLUSIONS];
1260   /* Block header index; see gc_headers.h */
1261   bottom_index * _all_nils;
1262   bottom_index * _top_index [TOP_SZ];
1263 #ifdef SAVE_CALL_CHAIN
1264   struct callinfo _last_stack[NFRAMES]; /* Stack at last garbage collection.*/
1265                                         /* Useful for debugging mysterious  */
1266                                         /* object disappearances.           */
1267                                         /* In the multithreaded case, we    */
1268                                         /* currently only save the calling  */
1269                                         /* stack.                           */
1270 #endif
1271 };
1272
1273 GC_API GC_FAR struct _GC_arrays GC_arrays; 
1274
1275 # define GC_objfreelist GC_arrays._objfreelist
1276 # define GC_aobjfreelist GC_arrays._aobjfreelist
1277 # define GC_uobjfreelist GC_arrays._uobjfreelist
1278 # ifdef ATOMIC_UNCOLLECTABLE
1279 #   define GC_auobjfreelist GC_arrays._auobjfreelist
1280 # endif
1281 # define GC_sobjfreelist GC_arrays._sobjfreelist
1282 # define GC_valid_offsets GC_arrays._valid_offsets
1283 # define GC_modws_valid_offsets GC_arrays._modws_valid_offsets
1284 # ifdef STUBBORN_ALLOC
1285 #    define GC_changed_pages GC_arrays._changed_pages
1286 #    define GC_prev_changed_pages GC_arrays._prev_changed_pages
1287 # endif
1288 # define GC_obj_map GC_arrays._obj_map
1289 # define GC_last_heap_addr GC_arrays._last_heap_addr
1290 # define GC_prev_heap_addr GC_arrays._prev_heap_addr
1291 # define GC_words_allocd GC_arrays._words_allocd
1292 # define GC_words_wasted GC_arrays._words_wasted
1293 # define GC_large_free_bytes GC_arrays._large_free_bytes
1294 # define GC_words_finalized GC_arrays._words_finalized
1295 # define GC_non_gc_bytes_at_gc GC_arrays._non_gc_bytes_at_gc
1296 # define GC_mem_freed GC_arrays._mem_freed
1297 # define GC_scratch_end_ptr GC_arrays._scratch_end_ptr
1298 # define GC_scratch_last_end_ptr GC_arrays._scratch_last_end_ptr
1299 # define GC_mark_procs GC_arrays._mark_procs
1300 # define GC_heapsize GC_arrays._heapsize
1301 # define GC_max_heapsize GC_arrays._max_heapsize
1302 # define GC_requested_heapsize GC_arrays._requested_heapsize
1303 # define GC_words_allocd_before_gc GC_arrays._words_allocd_before_gc
1304 # define GC_heap_sects GC_arrays._heap_sects
1305 # define GC_last_stack GC_arrays._last_stack
1306 # ifdef USE_MUNMAP
1307 #   define GC_unmapped_bytes GC_arrays._unmapped_bytes
1308 # endif
1309 # ifdef MSWIN32
1310 #   define GC_heap_bases GC_arrays._heap_bases
1311 # endif
1312 # define GC_static_roots GC_arrays._static_roots
1313 # define GC_root_index GC_arrays._root_index
1314 # define GC_excl_table GC_arrays._excl_table
1315 # define GC_all_nils GC_arrays._all_nils
1316 # define GC_top_index GC_arrays._top_index
1317 # if defined(PROC_VDB) || defined(MPROTECT_VDB)
1318 #   define GC_grungy_pages GC_arrays._grungy_pages
1319 # endif
1320 # ifdef MPROTECT_VDB
1321 #   define GC_dirty_pages GC_arrays._dirty_pages
1322 # endif
1323 # ifdef PROC_VDB
1324 #   define GC_written_pages GC_arrays._written_pages
1325 # endif
1326 # ifdef GATHERSTATS
1327 #   define GC_composite_in_use GC_arrays._composite_in_use
1328 #   define GC_atomic_in_use GC_arrays._atomic_in_use
1329 # endif
1330 # ifdef MERGE_SIZES
1331 #   define GC_size_map GC_arrays._size_map
1332 # endif
1333
1334 # define beginGC_arrays ((ptr_t)(&GC_arrays))
1335 # define endGC_arrays (((ptr_t)(&GC_arrays)) + (sizeof GC_arrays))
1336
1337 #define USED_HEAP_SIZE (GC_heapsize - GC_large_free_bytes)
1338
1339 /* Object kinds: */
1340 # define MAXOBJKINDS 16
1341
1342 extern struct obj_kind {
1343    ptr_t *ok_freelist;  /* Array of free listheaders for this kind of object */
1344                         /* Point either to GC_arrays or to storage allocated */
1345                         /* with GC_scratch_alloc.                            */
1346    struct hblk **ok_reclaim_list;
1347                         /* List headers for lists of blocks waiting to be */
1348                         /* swept.                                         */
1349    word ok_descriptor;  /* Descriptor template for objects in this      */
1350                         /* block.                                       */
1351    GC_bool ok_relocate_descr;
1352                         /* Add object size in bytes to descriptor       */
1353                         /* template to obtain descriptor.  Otherwise    */
1354                         /* template is used as is.                      */
1355    GC_bool ok_init;   /* Clear objects before putting them on the free list. */
1356 } GC_obj_kinds[MAXOBJKINDS];
1357
1358 # define endGC_obj_kinds (((ptr_t)(&GC_obj_kinds)) + (sizeof GC_obj_kinds))
1359
1360 # define end_gc_area ((ptr_t)endGC_arrays == (ptr_t)(&GC_obj_kinds) ? \
1361                         endGC_obj_kinds : endGC_arrays)
1362
1363 /* Predefined kinds: */
1364 # define PTRFREE 0
1365 # define NORMAL  1
1366 # define UNCOLLECTABLE 2
1367 # ifdef ATOMIC_UNCOLLECTABLE
1368 #   define AUNCOLLECTABLE 3
1369 #   define STUBBORN 4
1370 #   define IS_UNCOLLECTABLE(k) (((k) & ~1) == UNCOLLECTABLE)
1371 # else
1372 #   define STUBBORN 3
1373 #   define IS_UNCOLLECTABLE(k) ((k) == UNCOLLECTABLE)
1374 # endif
1375
1376 extern int GC_n_kinds;
1377
1378 GC_API word GC_fo_entries;
1379
1380 extern word GC_n_heap_sects;    /* Number of separately added heap      */
1381                                 /* sections.                            */
1382
1383 extern word GC_page_size;
1384
1385 # ifdef MSWIN32
1386 extern word GC_n_heap_bases;    /* See GC_heap_bases.   */
1387 # endif
1388
1389 extern word GC_total_stack_black_listed;
1390                         /* Number of bytes on stack blacklist.  */
1391
1392 extern word GC_black_list_spacing;
1393                         /* Average number of bytes between blacklisted  */
1394                         /* blocks. Approximate.                         */
1395                         /* Counts only blocks that are                  */
1396                         /* "stack-blacklisted", i.e. that are           */
1397                         /* problematic in the interior of an object.    */
1398
1399 extern char * GC_invalid_map;
1400                         /* Pointer to the nowhere valid hblk map */
1401                         /* Blocks pointing to this map are free. */
1402
1403 extern struct hblk * GC_hblkfreelist[];
1404                                 /* List of completely empty heap blocks */
1405                                 /* Linked through hb_next field of      */
1406                                 /* header structure associated with     */
1407                                 /* block.                               */
1408
1409 extern GC_bool GC_is_initialized;       /* GC_init() has been run.      */
1410
1411 extern GC_bool GC_objects_are_marked;   /* There are marked objects in  */
1412                                         /* the heap.                    */
1413
1414 #ifndef SMALL_CONFIG
1415   extern GC_bool GC_incremental;
1416                         /* Using incremental/generational collection. */
1417 #else
1418 # define GC_incremental FALSE
1419                         /* Hopefully allow optimizer to remove some code. */
1420 #endif
1421
1422 extern GC_bool GC_dirty_maintained;
1423                                 /* Dirty bits are being maintained,     */
1424                                 /* either for incremental collection,   */
1425                                 /* or to limit the root set.            */
1426
1427 extern word GC_root_size;       /* Total size of registered root sections */
1428
1429 extern GC_bool GC_debugging_started;    /* GC_debug_malloc has been called. */ 
1430
1431 extern ptr_t GC_least_plausible_heap_addr;
1432 extern ptr_t GC_greatest_plausible_heap_addr;
1433                         /* Bounds on the heap.  Guaranteed valid        */
1434                         /* Likely to include future heap expansion.     */
1435                         
1436 /* Operations */
1437 # ifndef abs
1438 #   define abs(x)  ((x) < 0? (-(x)) : (x))
1439 # endif
1440
1441
1442 /*  Marks are in a reserved area in                          */
1443 /*  each heap block.  Each word has one mark bit associated  */
1444 /*  with it. Only those corresponding to the beginning of an */
1445 /*  object are used.                                         */
1446
1447
1448 /* Mark bit operations */
1449
1450 /*
1451  * Retrieve, set, clear the mark bit corresponding
1452  * to the nth word in a given heap block.
1453  *
1454  * (Recall that bit n corresponds to object beginning at word n
1455  * relative to the beginning of the block, including unused words)
1456  */
1457
1458 # define mark_bit_from_hdr(hhdr,n) (((hhdr)->hb_marks[divWORDSZ(n)] \
1459                             >> (modWORDSZ(n))) & (word)1)
1460 # define set_mark_bit_from_hdr(hhdr,n) (hhdr)->hb_marks[divWORDSZ(n)] \
1461                                 |= (word)1 << modWORDSZ(n)
1462
1463 # define clear_mark_bit_from_hdr(hhdr,n) (hhdr)->hb_marks[divWORDSZ(n)] \
1464                                 &= ~((word)1 << modWORDSZ(n))
1465
1466 /* Important internal collector routines */
1467
1468 ptr_t GC_approx_sp();
1469
1470 GC_bool GC_should_collect();
1471
1472 void GC_apply_to_all_blocks(/*fn, client_data*/);
1473                         /* Invoke fn(hbp, client_data) for each         */
1474                         /* allocated heap block.                        */
1475 struct hblk * GC_next_used_block(/* struct hblk * h */);
1476                         /* Return first in-use block >= h       */
1477 struct hblk * GC_prev_block(/* struct hblk * h */);
1478                         /* Return last block <= h.  Returned block      */
1479                         /* is managed by GC, but may or may not be in   */
1480                         /* use.                                         */
1481 void GC_mark_init();
1482 void GC_clear_marks();  /* Clear mark bits for all heap objects. */
1483 void GC_invalidate_mark_state();        /* Tell the marker that marked     */
1484                                         /* objects may point to unmarked   */
1485                                         /* ones, and roots may point to    */
1486                                         /* unmarked objects.               */
1487                                         /* Reset mark stack.               */
1488 void GC_mark_from_mark_stack(); /* Mark from everything on the mark stack. */
1489                                 /* Return after about one pages worth of   */
1490                                 /* work.                                   */
1491 GC_bool GC_mark_stack_empty();
1492 GC_bool GC_mark_some(/* cold_gc_frame */);
1493                         /* Perform about one pages worth of marking     */
1494                         /* work of whatever kind is needed.  Returns    */
1495                         /* quickly if no collection is in progress.     */
1496                         /* Return TRUE if mark phase finished.          */
1497 void GC_initiate_gc();          /* initiate collection.                 */
1498                                 /* If the mark state is invalid, this   */
1499                                 /* becomes full colleection.  Otherwise */
1500                                 /* it's partial.                        */
1501 void GC_push_all(/*b,t*/);      /* Push everything in a range           */
1502                                 /* onto mark stack.                     */
1503 void GC_push_dirty(/*b,t*/);      /* Push all possibly changed          */
1504                                   /* subintervals of [b,t) onto         */
1505                                   /* mark stack.                        */
1506 #ifndef SMALL_CONFIG
1507   void GC_push_conditional(/* ptr_t b, ptr_t t, GC_bool all*/);
1508 #else
1509 # define GC_push_conditional(b, t, all) GC_push_all(b, t)
1510 #endif
1511                                 /* Do either of the above, depending    */
1512                                 /* on the third arg.                    */
1513 void GC_push_all_stack(/*b,t*/);    /* As above, but consider           */
1514                                     /*  interior pointers as valid      */
1515 void GC_push_all_eager(/*b,t*/);    /* Same as GC_push_all_stack, but   */
1516                                     /* ensures that stack is scanned    */
1517                                     /* immediately, not just scheduled  */
1518                                     /* for scanning.                    */
1519 #ifndef THREADS
1520   void GC_push_all_stack_partially_eager(/* bottom, top, cold_gc_frame */);
1521                         /* Similar to GC_push_all_eager, but only the   */
1522                         /* part hotter than cold_gc_frame is scanned    */
1523                         /* immediately.  Needed to endure that callee-  */
1524                         /* save registers are not missed.               */
1525 #else
1526   /* In the threads case, we push part of the current thread stack      */
1527   /* with GC_push_all_eager when we push the registers.  This gets the  */
1528   /* callee-save registers that may disappear.  The remainder of the    */
1529   /* stacks are scheduled for scanning in *GC_push_other_roots, which   */
1530   /* is thread-package-specific.                                        */
1531 #endif
1532 void GC_push_current_stack(/* ptr_t cold_gc_frame */);
1533                         /* Push enough of the current stack eagerly to  */
1534                         /* ensure that callee-save registers saved in   */
1535                         /* GC frames are scanned.                       */
1536                         /* In the non-threads case, schedule entire     */
1537                         /* stack for scanning.                          */
1538 void GC_push_roots(/* GC_bool all, ptr_t cold_gc_frame */);
1539                         /* Push all or dirty roots.     */
1540 extern void (*GC_push_other_roots)();
1541                         /* Push system or application specific roots    */
1542                         /* onto the mark stack.  In some environments   */
1543                         /* (e.g. threads environments) this is          */
1544                         /* predfined to be non-zero.  A client supplied */
1545                         /* replacement should also call the original    */
1546                         /* function.                                    */
1547 extern void (*GC_start_call_back)(/* void */);
1548                         /* Called at start of full collections.         */
1549                         /* Not called if 0.  Called with allocation     */
1550                         /* lock held.                                   */
1551                         /* 0 by default.                                */
1552 void GC_push_regs();    /* Push register contents onto mark stack.      */
1553                         /* If NURSERY is defined, the default push      */
1554                         /* action can be overridden with GC_push_proc   */
1555 void GC_remark();       /* Mark from all marked objects.  Used  */
1556                         /* only if we had to drop something.    */
1557
1558 # ifdef NURSERY
1559     extern void (*GC_push_proc)(ptr_t);
1560 # endif
1561 # if defined(MSWIN32)
1562   void __cdecl GC_push_one();
1563 # else
1564   void GC_push_one(/*p*/);    /* If p points to an object, mark it    */
1565                               /* and push contents on the mark stack  */
1566 # endif
1567 void GC_push_one_checked(/*p*/); /* Ditto, omits plausibility test      */
1568 void GC_push_marked(/* struct hblk h, hdr * hhdr */);
1569                 /* Push contents of all marked objects in h onto        */
1570                 /* mark stack.                                          */
1571 #ifdef SMALL_CONFIG
1572 # define GC_push_next_marked_dirty(h) GC_push_next_marked(h)
1573 #else
1574   struct hblk * GC_push_next_marked_dirty(/* h */);
1575                 /* Invoke GC_push_marked on next dirty block above h.   */
1576                 /* Return a pointer just past the end of this block.    */
1577 #endif /* !SMALL_CONFIG */
1578 struct hblk * GC_push_next_marked(/* h */);
1579                 /* Ditto, but also mark from clean pages.       */
1580 struct hblk * GC_push_next_marked_uncollectable(/* h */);
1581                 /* Ditto, but mark only from uncollectable pages.       */
1582 GC_bool GC_stopped_mark(); /* Stop world and mark from all roots        */
1583                         /* and rescuers.                        */
1584 void GC_clear_hdr_marks(/* hhdr */);  /* Clear the mark bits in a header */
1585 void GC_set_hdr_marks(/* hhdr */);  /* Set the mark bits in a header */
1586 void GC_add_roots_inner();
1587 GC_bool GC_is_static_root(/* ptr_t p */);
1588                 /* Is the address p in one of the registered static     */
1589                 /* root sections?                                       */
1590 void GC_register_dynamic_libraries();
1591                 /* Add dynamic library data sections to the root set. */
1592
1593 /* Machine dependent startup routines */
1594 ptr_t GC_get_stack_base();
1595 void GC_register_data_segments();
1596
1597 /* Black listing: */
1598 void GC_bl_init();      
1599 # ifndef ALL_INTERIOR_POINTERS
1600     void GC_add_to_black_list_normal(/* bits, maybe source */);
1601                         /* Register bits as a possible future false     */
1602                         /* reference from the heap or static data       */
1603 #   ifdef PRINT_BLACK_LIST
1604 #     define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1605                         GC_add_to_black_list_normal(bits, source)
1606 #   else
1607 #     define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1608                         GC_add_to_black_list_normal(bits)
1609 #   endif
1610 # else
1611 #   ifdef PRINT_BLACK_LIST
1612 #     define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1613                         GC_add_to_black_list_stack(bits, source)
1614 #   else
1615 #     define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1616                         GC_add_to_black_list_stack(bits)
1617 #   endif
1618 # endif
1619
1620 void GC_add_to_black_list_stack(/* bits, maybe source */);
1621 struct hblk * GC_is_black_listed(/* h, len */);
1622                         /* If there are likely to be false references   */
1623                         /* to a block starting at h of the indicated    */
1624                         /* length, then return the next plausible       */
1625                         /* starting location for h that might avoid     */
1626                         /* these false references.                      */
1627 void GC_promote_black_lists();
1628                         /* Declare an end to a black listing phase.     */
1629 void GC_unpromote_black_lists();
1630                         /* Approximately undo the effect of the above.  */
1631                         /* This actually loses some information, but    */
1632                         /* only in a reasonably safe way.               */
1633 word GC_number_stack_black_listed(/*struct hblk *start, struct hblk *endp1 */);
1634                         /* Return the number of (stack) blacklisted     */
1635                         /* blocks in the range for statistical          */
1636                         /* purposes.                                    */
1637                         
1638 ptr_t GC_scratch_alloc(/*bytes*/);
1639                                 /* GC internal memory allocation for    */
1640                                 /* small objects.  Deallocation is not  */
1641                                 /* possible.                            */
1642         
1643 /* Heap block layout maps: */                   
1644 void GC_invalidate_map(/* hdr */);
1645                                 /* Remove the object map associated     */
1646                                 /* with the block.  This identifies     */
1647                                 /* the block as invalid to the mark     */
1648                                 /* routines.                            */
1649 GC_bool GC_add_map_entry(/*sz*/);
1650                                 /* Add a heap block map for objects of  */
1651                                 /* size sz to obj_map.                  */
1652                                 /* Return FALSE on failure.             */
1653 void GC_register_displacement_inner(/*offset*/);
1654                                 /* Version of GC_register_displacement  */
1655                                 /* that assumes lock is already held    */
1656                                 /* and signals are already disabled.    */
1657
1658 /*  hblk allocation: */         
1659 void GC_new_hblk(/*size_in_words, kind*/);
1660                                 /* Allocate a new heap block, and build */
1661                                 /* a free list in it.                   */                              
1662 struct hblk * GC_allochblk(/*size_in_words, kind*/);
1663                                 /* Allocate a heap block, clear it if   */
1664                                 /* for composite objects, inform        */
1665                                 /* the marker that block is valid       */
1666                                 /* for objects of indicated size.       */
1667                                 /* sz < 0 ==> atomic.                   */ 
1668 void GC_freehblk();             /* Deallocate a heap block and mark it  */
1669                                 /* as invalid.                          */
1670                                 
1671 /*  Misc GC: */
1672 void GC_init_inner();
1673 GC_bool GC_expand_hp_inner();
1674 void GC_start_reclaim(/*abort_if_found*/);
1675                                 /* Restore unmarked objects to free     */
1676                                 /* lists, or (if abort_if_found is      */
1677                                 /* TRUE) report them.                   */
1678                                 /* Sweeping of small object pages is    */
1679                                 /* largely deferred.                    */
1680 void GC_continue_reclaim(/*size, kind*/);
1681                                 /* Sweep pages of the given size and    */
1682                                 /* kind, as long as possible, and       */
1683                                 /* as long as the corr. free list is    */
1684                                 /* empty.                               */
1685 void GC_reclaim_or_delete_all();
1686                                 /* Arrange for all reclaim lists to be  */
1687                                 /* empty.  Judiciously choose between   */
1688                                 /* sweeping and discarding each page.   */
1689 GC_bool GC_reclaim_all(/* GC_stop_func f*/);
1690                                 /* Reclaim all blocks.  Abort (in a     */
1691                                 /* consistent state) if f returns TRUE. */
1692 GC_bool GC_block_empty(/* hhdr */); /* Block completely unmarked?       */
1693 GC_bool GC_never_stop_func();   /* Returns FALSE.               */
1694 GC_bool GC_try_to_collect_inner(/* GC_stop_func f */);
1695                                 /* Collect; caller must have acquired   */
1696                                 /* lock and disabled signals.           */
1697                                 /* Collection is aborted if f returns   */
1698                                 /* TRUE.  Returns TRUE if it completes  */
1699                                 /* successfully.                        */
1700 # define GC_gcollect_inner() \
1701         (void) GC_try_to_collect_inner(GC_never_stop_func)
1702 void GC_finish_collection();    /* Finish collection.  Mark bits are    */
1703                                 /* consistent and lock is still held.   */
1704 GC_bool GC_collect_or_expand(/* needed_blocks */);
1705                                 /* Collect or expand heap in an attempt */
1706                                 /* make the indicated number of free    */
1707                                 /* blocks available.  Should be called  */
1708                                 /* until the blocks are available or    */
1709                                 /* until it fails by returning FALSE.   */
1710 GC_API void GC_init();          /* Initialize collector.                */
1711 void GC_collect_a_little_inner(/* int n */);
1712                                 /* Do n units worth of garbage          */
1713                                 /* collection work, if appropriate.     */
1714                                 /* A unit is an amount appropriate for  */
1715                                 /* HBLKSIZE bytes of allocation.        */
1716 ptr_t GC_generic_malloc(/* bytes, kind */);
1717                                 /* Allocate an object of the given      */
1718                                 /* kind.  By default, there are only    */
1719                                 /* a few kinds: composite(pointerfree), */
1720                                 /* atomic, uncollectable, etc.          */
1721                                 /* We claim it's possible for clever    */
1722                                 /* client code that understands GC      */
1723                                 /* internals to add more, e.g. to       */
1724                                 /* communicate object layout info       */
1725                                 /* to the collector.                    */
1726 ptr_t GC_generic_malloc_ignore_off_page(/* bytes, kind */);
1727                                 /* As above, but pointers past the      */
1728                                 /* first page of the resulting object   */
1729                                 /* are ignored.                         */
1730 ptr_t GC_generic_malloc_inner(/* bytes, kind */);
1731                                 /* Ditto, but I already hold lock, etc. */
1732 ptr_t GC_generic_malloc_words_small GC_PROTO((size_t words, int kind));
1733                                 /* As above, but size in units of words */
1734                                 /* Bypasses MERGE_SIZES.  Assumes       */
1735                                 /* words <= MAXOBJSZ.                   */
1736 ptr_t GC_generic_malloc_inner_ignore_off_page(/* bytes, kind */);
1737                                 /* Allocate an object, where            */
1738                                 /* the client guarantees that there     */
1739                                 /* will always be a pointer to the      */
1740                                 /* beginning of the object while the    */
1741                                 /* object is live.                      */
1742 ptr_t GC_allocobj(/* sz_inn_words, kind */);
1743                                 /* Make the indicated                   */
1744                                 /* free list nonempty, and return its   */
1745                                 /* head.                                */
1746
1747 void GC_init_headers();
1748 struct hblkhdr * GC_install_header(/*h*/);
1749                                 /* Install a header for block h.        */
1750                                 /* Return 0 on failure, or the header   */
1751                                 /* otherwise.                           */
1752 GC_bool GC_install_counts(/*h, sz*/);
1753                                 /* Set up forwarding counts for block   */
1754                                 /* h of size sz.                        */
1755                                 /* Return FALSE on failure.             */
1756 void GC_remove_header(/*h*/);
1757                                 /* Remove the header for block h.       */
1758 void GC_remove_counts(/*h, sz*/);
1759                                 /* Remove forwarding counts for h.      */
1760 hdr * GC_find_header(/*p*/);    /* Debugging only.                      */
1761
1762 void GC_finalize();     /* Perform all indicated finalization actions   */
1763                         /* on unmarked objects.                         */
1764                         /* Unreachable finalizable objects are enqueued */
1765                         /* for processing by GC_invoke_finalizers.      */
1766                         /* Invoked with lock.                           */
1767                         
1768 void GC_add_to_heap(/*p, bytes*/);
1769                         /* Add a HBLKSIZE aligned chunk to the heap.    */
1770
1771 void GC_print_obj(/* ptr_t p */);
1772                         /* P points to somewhere inside an object with  */
1773                         /* debugging info.  Print a human readable      */
1774                         /* description of the object to stderr.         */
1775 ptr_t GC_debug_object_start(/* ptr_t p */);
1776                         /* P points to the start of an object that may  */
1777                         /* have debug info at its head.  Return the     */
1778                         /* start of the real data.                      */
1779 extern void (*GC_check_heap)();
1780                         /* Check that all objects in the heap with      */
1781                         /* debugging info are intact.  Print            */
1782                         /* descriptions of any that are not.            */
1783 extern void (*GC_print_heap_obj)(/* ptr_t p */);
1784                         /* If possible print s followed by a more       */
1785                         /* detailed description of the object           */
1786                         /* referred to by p.                            */
1787                         
1788 /* Memory unmapping: */
1789 #ifdef USE_MUNMAP
1790   void GC_unmap_old(void);
1791   void GC_merge_unmapped(void);
1792   void GC_unmap(ptr_t start, word bytes);
1793   void GC_remap(ptr_t start, word bytes);
1794   void GC_unmap_gap(ptr_t start1, word bytes1, ptr_t start2, word bytes2);
1795 #endif
1796
1797 /* Virtual dirty bit implementation:            */
1798 /* Each implementation exports the following:   */
1799 void GC_read_dirty();   /* Retrieve dirty bits. */
1800 GC_bool GC_page_was_dirty(/* struct hblk * h  */);
1801                         /* Read retrieved dirty bits.   */
1802 GC_bool GC_page_was_ever_dirty(/* struct hblk * h  */);
1803                         /* Could the page contain valid heap pointers?  */
1804 void GC_is_fresh(/* struct hblk * h, word number_of_blocks  */);
1805                         /* Assert the region currently contains no      */
1806                         /* valid pointers.                              */
1807 void GC_write_hint(/* struct hblk * h  */);
1808                         /* h is about to be written.    */
1809 void GC_dirty_init();
1810
1811 /* Slow/general mark bit manipulation: */
1812 GC_API GC_bool GC_is_marked();
1813 void GC_clear_mark_bit();
1814 void GC_set_mark_bit();
1815
1816 /* Stubborn objects: */
1817 void GC_read_changed(); /* Analogous to GC_read_dirty */
1818 GC_bool GC_page_was_changed(/* h */);   /* Analogous to GC_page_was_dirty */
1819 void GC_clean_changing_list();  /* Collect obsolete changing list entries */
1820 void GC_stubborn_init();
1821
1822 /* Debugging print routines: */
1823 void GC_print_block_list();
1824 void GC_print_hblkfreelist();
1825 void GC_print_heap_sects();
1826 void GC_print_static_roots();
1827 void GC_dump();
1828
1829 #ifdef KEEP_BACK_PTRS
1830    void GC_store_back_pointer(ptr_t source, ptr_t dest);
1831    void GC_marked_for_finalization(ptr_t dest);
1832 #  define GC_STORE_BACK_PTR(source, dest) GC_store_back_pointer(source, dest)
1833 #  define GC_MARKED_FOR_FINALIZATION(dest) GC_marked_for_finalization(dest)
1834 #else
1835 #  define GC_STORE_BACK_PTR(source, dest) 
1836 #  define GC_MARKED_FOR_FINALIZATION(dest)
1837 #endif
1838
1839 /* Make arguments appear live to compiler */
1840 # ifdef __WATCOMC__
1841   void GC_noop(void*, ...);
1842 # else
1843   GC_API void GC_noop();
1844 # endif
1845
1846 void GC_noop1(/* word arg */);
1847
1848 /* Logging and diagnostic output:       */
1849 GC_API void GC_printf GC_PROTO((char * format, long, long, long, long, long, long));
1850                         /* A version of printf that doesn't allocate,   */
1851                         /* is restricted to long arguments, and         */
1852                         /* (unfortunately) doesn't use varargs for      */
1853                         /* portability.  Restricted to 6 args and       */
1854                         /* 1K total output length.                      */
1855                         /* (We use sprintf.  Hopefully that doesn't     */
1856                         /* allocate for long arguments.)                */
1857 # define GC_printf0(f) GC_printf(f, 0l, 0l, 0l, 0l, 0l, 0l)
1858 # define GC_printf1(f,a) GC_printf(f, (long)a, 0l, 0l, 0l, 0l, 0l)
1859 # define GC_printf2(f,a,b) GC_printf(f, (long)a, (long)b, 0l, 0l, 0l, 0l)
1860 # define GC_printf3(f,a,b,c) GC_printf(f, (long)a, (long)b, (long)c, 0l, 0l, 0l)
1861 # define GC_printf4(f,a,b,c,d) GC_printf(f, (long)a, (long)b, (long)c, \
1862                                             (long)d, 0l, 0l)
1863 # define GC_printf5(f,a,b,c,d,e) GC_printf(f, (long)a, (long)b, (long)c, \
1864                                               (long)d, (long)e, 0l)
1865 # define GC_printf6(f,a,b,c,d,e,g) GC_printf(f, (long)a, (long)b, (long)c, \
1866                                                 (long)d, (long)e, (long)g)
1867
1868 void GC_err_printf(/* format, a, b, c, d, e, f */);
1869 # define GC_err_printf0(f) GC_err_puts(f)
1870 # define GC_err_printf1(f,a) GC_err_printf(f, (long)a, 0l, 0l, 0l, 0l, 0l)
1871 # define GC_err_printf2(f,a,b) GC_err_printf(f, (long)a, (long)b, 0l, 0l, 0l, 0l)
1872 # define GC_err_printf3(f,a,b,c) GC_err_printf(f, (long)a, (long)b, (long)c, \
1873                                                   0l, 0l, 0l)
1874 # define GC_err_printf4(f,a,b,c,d) GC_err_printf(f, (long)a, (long)b, \
1875                                                     (long)c, (long)d, 0l, 0l)
1876 # define GC_err_printf5(f,a,b,c,d,e) GC_err_printf(f, (long)a, (long)b, \
1877                                                       (long)c, (long)d, \
1878                                                       (long)e, 0l)
1879 # define GC_err_printf6(f,a,b,c,d,e,g) GC_err_printf(f, (long)a, (long)b, \
1880                                                         (long)c, (long)d, \
1881                                                         (long)e, (long)g)
1882                         /* Ditto, writes to stderr.                     */
1883                         
1884 void GC_err_puts(/* char *s */);
1885                         /* Write s to stderr, don't buffer, don't add   */
1886                         /* newlines, don't ...                          */
1887
1888
1889 #   ifdef GC_ASSERTIONS
1890 #       define GC_ASSERT(expr) if(!(expr)) {\
1891                 GC_err_printf2("Assertion failure: %s:%ld\n", \
1892                                 __FILE__, (unsigned long)__LINE__); \
1893                 ABORT("assertion failure"); }
1894 #   else 
1895 #       define GC_ASSERT(expr)
1896 #   endif
1897
1898 # endif /* GC_PRIVATE_H */