root/gc/include/private/gc_priv.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. fixed_getenv

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

/* [<][>][^][v][top][bottom][index][help] */