PageRenderTime 101ms CodeModel.GetById 15ms app.highlight 76ms RepoModel.GetById 2ms app.codeStats 0ms

/vendor/gc/misc.c

http://github.com/feyeleanor/RubyGoLightly
C | 1184 lines | 903 code | 128 blank | 153 comment | 225 complexity | 617d6da37dec76e9a19ab08e04d7e9fa MD5 | raw file
   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) 1999-2001 by Hewlett-Packard Company. All rights reserved.
   5 *
   6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
   7 * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
   8 *
   9 * Permission is hereby granted to use or copy this program
  10 * for any purpose,  provided the above notices are retained on all copies.
  11 * Permission to modify the code and to distribute modified code is granted,
  12 * provided the above notices are retained, and a notice that the code was
  13 * modified is included with the above copyright notice.
  14 */
  15/* Boehm, July 31, 1995 5:02 pm PDT */
  16
  17
  18#include <stdio.h>
  19#include <limits.h>
  20#include <stdarg.h>
  21#ifndef _WIN32_WCE
  22#include <signal.h>
  23#endif
  24
  25#define I_HIDE_POINTERS	/* To make GC_call_with_alloc_lock visible */
  26#include "private/gc_pmark.h"
  27
  28#ifdef GC_SOLARIS_THREADS
  29# include <sys/syscall.h>
  30#endif
  31#if defined(MSWIN32) || defined(MSWINCE)
  32# define WIN32_LEAN_AND_MEAN
  33# define NOSERVICE
  34# include <windows.h>
  35# include <tchar.h>
  36#endif
  37
  38#ifdef UNIX_LIKE
  39# include <fcntl.h>
  40# include <sys/types.h>
  41# include <sys/stat.h>
  42
  43  int GC_log;  /* Forward decl, so we can set it.	*/
  44#endif
  45
  46#ifdef NONSTOP
  47# include <floss.h>
  48#endif
  49
  50#if defined(THREADS) && defined(PCR)
  51# include "il/PCR_IL.h"
  52  PCR_Th_ML GC_allocate_ml;
  53#endif
  54/* For other platforms with threads, the lock and possibly		*/
  55/* GC_lock_holder variables are defined in the thread support code.	*/
  56
  57#if defined(NOSYS) || defined(ECOS)
  58#undef STACKBASE
  59#endif
  60
  61/* Dont unnecessarily call GC_register_main_static_data() in case 	*/
  62/* dyn_load.c isn't linked in.						*/
  63#ifdef DYNAMIC_LOADING
  64# define GC_REGISTER_MAIN_STATIC_DATA() GC_register_main_static_data()
  65#else
  66# define GC_REGISTER_MAIN_STATIC_DATA() TRUE
  67#endif
  68
  69GC_FAR struct _GC_arrays GC_arrays /* = { 0 } */;
  70
  71
  72GC_bool GC_debugging_started = FALSE;
  73	/* defined here so we don't have to load debug_malloc.o */
  74
  75void (*GC_check_heap) (void) = (void (*) (void))0;
  76void (*GC_print_all_smashed) (void) = (void (*) (void))0;
  77
  78void (*GC_start_call_back) (void) = (void (*) (void))0;
  79
  80ptr_t GC_stackbottom = 0;
  81
  82#ifdef IA64
  83  ptr_t GC_register_stackbottom = 0;
  84#endif
  85
  86GC_bool GC_dont_gc = 0;
  87
  88GC_bool GC_dont_precollect = 0;
  89
  90GC_bool GC_quiet = 0;
  91
  92#ifndef SMALL_CONFIG
  93  GC_bool GC_print_stats = 0;
  94#endif
  95
  96GC_bool GC_print_back_height = 0;
  97
  98#ifndef NO_DEBUGGING
  99  GC_bool GC_dump_regularly = 0;  /* Generate regular debugging dumps. */
 100#endif
 101
 102#ifdef KEEP_BACK_PTRS
 103  long GC_backtraces = 0;	/* Number of random backtraces to 	*/
 104  				/* generate for each GC.		*/
 105#endif
 106
 107#ifdef FIND_LEAK
 108  int GC_find_leak = 1;
 109#else
 110  int GC_find_leak = 0;
 111#endif
 112
 113#ifdef ALL_INTERIOR_POINTERS
 114  int GC_all_interior_pointers = 1;
 115#else
 116  int GC_all_interior_pointers = 0;
 117#endif
 118
 119long GC_large_alloc_warn_interval = 5;
 120	/* Interval between unsuppressed warnings.	*/
 121
 122long GC_large_alloc_warn_suppressed = 0;
 123	/* Number of warnings suppressed so far.	*/
 124
 125/*ARGSUSED*/
 126void * GC_default_oom_fn(size_t bytes_requested)
 127{
 128    return(0);
 129}
 130
 131void * (*GC_oom_fn) (size_t bytes_requested) = GC_default_oom_fn;
 132
 133void * GC_project2(void *arg1, void *arg2)
 134{
 135  return arg2;
 136}
 137
 138/* Set things up so that GC_size_map[i] >= granules(i),		*/
 139/* but not too much bigger						*/
 140/* and so that size_map contains relatively few distinct entries 	*/
 141/* This was originally stolen from Russ Atkinson's Cedar		*/
 142/* quantization alogrithm (but we precompute it).			*/ 
 143void GC_init_size_map(void)
 144{
 145    int i;
 146
 147    /* Map size 0 to something bigger.			*/
 148    /* This avoids problems at lower levels.		*/
 149      GC_size_map[0] = 1;
 150    for (i = 1; i <= GRANULES_TO_BYTES(TINY_FREELISTS-1) - EXTRA_BYTES; i++) {
 151        GC_size_map[i] = ROUNDED_UP_GRANULES(i);
 152        GC_ASSERT(GC_size_map[i] < TINY_FREELISTS);
 153    }
 154    /* We leave the rest of the array to be filled in on demand. */
 155}
 156
 157/* Fill in additional entries in GC_size_map, including the ith one */
 158/* We assume the ith entry is currently 0.				*/
 159/* Note that a filled in section of the array ending at n always    */
 160/* has length at least n/4.						*/
 161void GC_extend_size_map(size_t i)
 162{
 163    size_t orig_granule_sz = ROUNDED_UP_GRANULES(i);
 164    size_t granule_sz = orig_granule_sz;
 165    size_t byte_sz = GRANULES_TO_BYTES(granule_sz);
 166    			/* The size we try to preserve.		*/
 167    			/* Close to i, unless this would	*/
 168    			/* introduce too many distinct sizes.	*/
 169    size_t smaller_than_i = byte_sz - (byte_sz >> 3);
 170    size_t much_smaller_than_i = byte_sz - (byte_sz >> 2);
 171    size_t low_limit;	/* The lowest indexed entry we 	*/
 172    			/* initialize.			*/
 173    size_t j;
 174    
 175    if (GC_size_map[smaller_than_i] == 0) {
 176        low_limit = much_smaller_than_i;
 177        while (GC_size_map[low_limit] != 0) low_limit++;
 178    } else {
 179        low_limit = smaller_than_i + 1;
 180        while (GC_size_map[low_limit] != 0) low_limit++;
 181        granule_sz = ROUNDED_UP_GRANULES(low_limit);
 182        granule_sz += granule_sz >> 3;
 183        if (granule_sz < orig_granule_sz) granule_sz = orig_granule_sz;
 184    }
 185    /* For these larger sizes, we use an even number of granules.	*/
 186    /* This makes it easier to, for example, construct a 16byte-aligned	*/
 187    /* allocator even if GRANULE_BYTES is 8.				*/
 188        granule_sz += 1;
 189        granule_sz &= ~1;
 190    if (granule_sz > MAXOBJGRANULES) {
 191        granule_sz = MAXOBJGRANULES;
 192    }
 193    /* If we can fit the same number of larger objects in a block,	*/
 194    /* do so.							*/ 
 195    {
 196        size_t number_of_objs = HBLK_GRANULES/granule_sz;
 197        granule_sz = HBLK_GRANULES/number_of_objs;
 198    	granule_sz &= ~1;
 199    }
 200    byte_sz = GRANULES_TO_BYTES(granule_sz);
 201    /* We may need one extra byte;			*/
 202    /* don't always fill in GC_size_map[byte_sz]	*/
 203    byte_sz -= EXTRA_BYTES;
 204
 205    for (j = low_limit; j <= byte_sz; j++) GC_size_map[j] = granule_sz;  
 206}
 207
 208
 209/*
 210 * The following is a gross hack to deal with a problem that can occur
 211 * on machines that are sloppy about stack frame sizes, notably SPARC.
 212 * Bogus pointers may be written to the stack and not cleared for
 213 * a LONG time, because they always fall into holes in stack frames
 214 * that are not written.  We partially address this by clearing
 215 * sections of the stack whenever we get control.
 216 */
 217word GC_stack_last_cleared = 0;	/* GC_no when we last did this */
 218# ifdef THREADS
 219#   define BIG_CLEAR_SIZE 2048	/* Clear this much now and then.	*/
 220#   define SMALL_CLEAR_SIZE 256 /* Clear this much every time.		*/
 221# endif
 222# define CLEAR_SIZE 213  /* Granularity for GC_clear_stack_inner */
 223# define DEGRADE_RATE 50
 224
 225ptr_t GC_min_sp;	/* Coolest stack pointer value from which we've */
 226			/* already cleared the stack.			*/
 227			
 228ptr_t GC_high_water;
 229			/* "hottest" stack pointer value we have seen	*/
 230			/* recently.  Degrades over time.		*/
 231
 232word GC_bytes_allocd_at_reset;
 233
 234#if defined(ASM_CLEAR_CODE)
 235  extern void *GC_clear_stack_inner(void *, ptr_t);
 236#else  
 237/* Clear the stack up to about limit.  Return arg. */
 238/*ARGSUSED*/
 239void * GC_clear_stack_inner(void *arg, ptr_t limit)
 240{
 241    word dummy[CLEAR_SIZE];
 242    
 243    BZERO(dummy, CLEAR_SIZE*sizeof(word));
 244    if ((ptr_t)(dummy) COOLER_THAN limit) {
 245        (void) GC_clear_stack_inner(arg, limit);
 246    }
 247    /* Make sure the recursive call is not a tail call, and the bzero	*/
 248    /* call is not recognized as dead code.				*/
 249    GC_noop1((word)dummy);
 250    return(arg);
 251}
 252#endif
 253
 254/* Clear some of the inaccessible part of the stack.  Returns its	*/
 255/* argument, so it can be used in a tail call position, hence clearing  */
 256/* another frame.							*/
 257void * GC_clear_stack(void *arg)
 258{
 259    ptr_t sp = GC_approx_sp();  /* Hotter than actual sp */
 260#   ifdef THREADS
 261        word dummy[SMALL_CLEAR_SIZE];
 262	static unsigned random_no = 0;
 263       			 	 /* Should be more random than it is ... */
 264				 /* Used to occasionally clear a bigger	 */
 265				 /* chunk.				 */
 266#   endif
 267    ptr_t limit;
 268    
 269#   define SLOP 400
 270	/* Extra bytes we clear every time.  This clears our own	*/
 271	/* activation record, and should cause more frequent		*/
 272	/* clearing near the cold end of the stack, a good thing.	*/
 273#   define GC_SLOP 4000
 274	/* We make GC_high_water this much hotter than we really saw   	*/
 275	/* saw it, to cover for GC noise etc. above our current frame.	*/
 276#   define CLEAR_THRESHOLD 100000
 277	/* We restart the clearing process after this many bytes of	*/
 278	/* allocation.  Otherwise very heavily recursive programs	*/
 279	/* with sparse stacks may result in heaps that grow almost	*/
 280	/* without bounds.  As the heap gets larger, collection 	*/
 281	/* frequency decreases, thus clearing frequency would decrease, */
 282	/* thus more junk remains accessible, thus the heap gets	*/
 283	/* larger ...							*/
 284# ifdef THREADS
 285    if (++random_no % 13 == 0) {
 286	limit = sp;
 287	MAKE_HOTTER(limit, BIG_CLEAR_SIZE*sizeof(word));
 288	limit = (ptr_t)((word)limit & ~0xf);
 289        		/* Make it sufficiently aligned for assembly	*/
 290        		/* implementations of GC_clear_stack_inner.	*/
 291	return GC_clear_stack_inner(arg, limit);
 292    } else {
 293	BZERO(dummy, SMALL_CLEAR_SIZE*sizeof(word));
 294	return arg;
 295    }
 296# else
 297    if (GC_gc_no > GC_stack_last_cleared) {
 298        /* Start things over, so we clear the entire stack again */
 299        if (GC_stack_last_cleared == 0) GC_high_water = (ptr_t)GC_stackbottom;
 300        GC_min_sp = GC_high_water;
 301        GC_stack_last_cleared = GC_gc_no;
 302        GC_bytes_allocd_at_reset = GC_bytes_allocd;
 303    }
 304    /* Adjust GC_high_water */
 305        MAKE_COOLER(GC_high_water, WORDS_TO_BYTES(DEGRADE_RATE) + GC_SLOP);
 306        if (sp HOTTER_THAN GC_high_water) {
 307            GC_high_water = sp;
 308        }
 309        MAKE_HOTTER(GC_high_water, GC_SLOP);
 310    limit = GC_min_sp;
 311    MAKE_HOTTER(limit, SLOP);
 312    if (sp COOLER_THAN limit) {
 313        limit = (ptr_t)((word)limit & ~0xf);
 314			/* Make it sufficiently aligned for assembly	*/
 315        		/* implementations of GC_clear_stack_inner.	*/
 316        GC_min_sp = sp;
 317        return(GC_clear_stack_inner(arg, limit));
 318    } else if (GC_bytes_allocd - GC_bytes_allocd_at_reset > CLEAR_THRESHOLD) {
 319    	/* Restart clearing process, but limit how much clearing we do. */
 320    	GC_min_sp = sp;
 321    	MAKE_HOTTER(GC_min_sp, CLEAR_THRESHOLD/4);
 322    	if (GC_min_sp HOTTER_THAN GC_high_water) GC_min_sp = GC_high_water;
 323    	GC_bytes_allocd_at_reset = GC_bytes_allocd;
 324    }  
 325    return(arg);
 326# endif
 327}
 328
 329
 330/* Return a pointer to the base address of p, given a pointer to a	*/
 331/* an address within an object.  Return 0 o.w.				*/
 332void * GC_base(void * p)
 333{
 334    ptr_t r;
 335    struct hblk *h;
 336    bottom_index *bi;
 337    hdr *candidate_hdr;
 338    ptr_t limit;
 339    
 340    r = p;
 341    if (!GC_is_initialized) return 0;
 342    h = HBLKPTR(r);
 343    GET_BI(r, bi);
 344    candidate_hdr = HDR_FROM_BI(bi, r);
 345    if (candidate_hdr == 0) return(0);
 346    /* If it's a pointer to the middle of a large object, move it	*/
 347    /* to the beginning.						*/
 348	while (IS_FORWARDING_ADDR_OR_NIL(candidate_hdr)) {
 349	   h = FORWARDED_ADDR(h,candidate_hdr);
 350	   r = (ptr_t)h;
 351	   candidate_hdr = HDR(h);
 352	}
 353    if (HBLK_IS_FREE(candidate_hdr)) return(0);
 354    /* Make sure r points to the beginning of the object */
 355	r = (ptr_t)((word)r & ~(WORDS_TO_BYTES(1) - 1));
 356        {
 357	    size_t offset = HBLKDISPL(r);
 358	    signed_word sz = candidate_hdr -> hb_sz;
 359	    size_t obj_displ = offset % sz;
 360
 361	    r -= obj_displ;
 362            limit = r + sz;
 363	    if (limit > (ptr_t)(h + 1) && sz <= HBLKSIZE) {
 364	        return(0);
 365	    }
 366	    if ((ptr_t)p >= limit) return(0);
 367	}
 368    return((void *)r);
 369}
 370
 371
 372/* Return the size of an object, given a pointer to its base.		*/
 373/* (For small obects this also happens to work from interior pointers,	*/
 374/* but that shouldn't be relied upon.)					*/
 375size_t GC_size(void * p)
 376{
 377    hdr * hhdr = HDR(p);
 378    
 379    return hhdr -> hb_sz;
 380}
 381
 382size_t GC_get_heap_size(void)
 383{
 384    return GC_heapsize;
 385}
 386
 387size_t GC_get_free_bytes(void)
 388{
 389    return GC_large_free_bytes;
 390}
 391
 392size_t GC_get_bytes_since_gc(void)
 393{
 394    return GC_bytes_allocd;
 395}
 396
 397size_t GC_get_total_bytes(void)
 398{
 399    return GC_bytes_allocd+GC_bytes_allocd_before_gc;
 400}
 401
 402GC_bool GC_is_initialized = FALSE;
 403
 404# if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
 405  extern void GC_init_parallel(void);
 406# endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
 407
 408/* FIXME: The GC_init/GC_init_inner distinction should go away. */
 409void GC_init(void)
 410{
 411    /* LOCK(); -- no longer does anything this early. */
 412    GC_init_inner();
 413    /* UNLOCK(); */
 414}
 415
 416#if defined(MSWIN32) || defined(MSWINCE)
 417    CRITICAL_SECTION GC_write_cs;
 418#endif
 419
 420#ifdef MSWIN32
 421    extern void GC_init_win32(void);
 422#endif
 423
 424extern void GC_setpagesize();
 425
 426#ifdef MSWIN32
 427extern GC_bool GC_no_win32_dlls;
 428#else
 429# define GC_no_win32_dlls FALSE
 430#endif
 431
 432void GC_exit_check(void)
 433{
 434   GC_gcollect();
 435}
 436
 437#ifdef SEARCH_FOR_DATA_START
 438  extern void GC_init_linux_data_start(void);
 439#endif
 440
 441#ifdef UNIX_LIKE
 442
 443extern void GC_set_and_save_fault_handler(void (*handler)(int));
 444
 445static void looping_handler(sig)
 446int sig;
 447{
 448    GC_err_printf("Caught signal %d: looping in handler\n", sig);
 449    for(;;);
 450}
 451
 452static GC_bool installed_looping_handler = FALSE;
 453
 454static void maybe_install_looping_handler()
 455{
 456    /* Install looping handler before the write fault handler, so we	*/
 457    /* handle write faults correctly.					*/
 458      if (!installed_looping_handler && 0 != GETENV("GC_LOOP_ON_ABORT")) {
 459        GC_set_and_save_fault_handler(looping_handler);
 460        installed_looping_handler = TRUE;
 461      }
 462}
 463
 464#else /* !UNIX_LIKE */
 465
 466# define maybe_install_looping_handler()
 467
 468#endif
 469
 470#if defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
 471  void GC_thr_init(void);
 472#endif
 473
 474void GC_init_inner()
 475{
 476#   if !defined(THREADS) && defined(GC_ASSERTIONS)
 477        word dummy;
 478#   endif
 479    word initial_heap_sz = (word)MINHINCR;
 480    
 481    if (GC_is_initialized) return;
 482
 483    /* Note that although we are nominally called with the */
 484    /* allocation lock held, the allocation lock is now	   */
 485    /* only really acquired once a second thread is forked.*/
 486    /* And the initialization code needs to run before     */
 487    /* then.  Thus we really don't hold any locks, and can */
 488    /* in fact safely initialize them here.		   */
 489#   ifdef THREADS
 490      GC_ASSERT(!GC_need_to_lock);
 491#   endif
 492#   if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
 493      if (!GC_is_initialized) {
 494        BOOL (WINAPI *pfn) (LPCRITICAL_SECTION, DWORD) = NULL;
 495        HMODULE hK32 = GetModuleHandleA("kernel32.dll");
 496        if (hK32)
 497  	  pfn = (BOOL (WINAPI *) (LPCRITICAL_SECTION, DWORD))
 498  		GetProcAddress (hK32,
 499  				"InitializeCriticalSectionAndSpinCount");
 500        if (pfn)
 501            pfn(&GC_allocate_ml, 4000);
 502        else
 503  	  InitializeCriticalSection (&GC_allocate_ml);
 504      }
 505#endif /* MSWIN32 */
 506#   if defined(MSWIN32) || defined(MSWINCE)
 507      InitializeCriticalSection(&GC_write_cs);
 508#   endif
 509#   if (!defined(SMALL_CONFIG))
 510      if (0 != GETENV("GC_PRINT_STATS")) {
 511        GC_print_stats = 1;
 512      } 
 513      if (0 != GETENV("GC_PRINT_VERBOSE_STATS")) {
 514        GC_print_stats = VERBOSE;
 515      } 
 516#     if defined(UNIX_LIKE)
 517        {
 518	  char * file_name = GETENV("GC_LOG_FILE");
 519          if (0 != file_name) {
 520	    int log_d = open(file_name, O_CREAT|O_WRONLY|O_APPEND, 0666);
 521	    if (log_d < 0) {
 522	      GC_log_printf("Failed to open %s as log file\n", file_name);
 523	    } else {
 524	      GC_log = log_d;
 525	    }
 526	  }
 527	}
 528#     endif
 529#   endif
 530#   ifndef NO_DEBUGGING
 531      if (0 != GETENV("GC_DUMP_REGULARLY")) {
 532        GC_dump_regularly = 1;
 533      }
 534#   endif
 535#   ifdef KEEP_BACK_PTRS
 536      {
 537        char * backtraces_string = GETENV("GC_BACKTRACES");
 538        if (0 != backtraces_string) {
 539          GC_backtraces = atol(backtraces_string);
 540	  if (backtraces_string[0] == '\0') GC_backtraces = 1;
 541        }
 542      }
 543#   endif
 544    if (0 != GETENV("GC_FIND_LEAK")) {
 545      GC_find_leak = 1;
 546      atexit(GC_exit_check);
 547    }
 548    if (0 != GETENV("GC_ALL_INTERIOR_POINTERS")) {
 549      GC_all_interior_pointers = 1;
 550    }
 551    if (0 != GETENV("GC_DONT_GC")) {
 552      GC_dont_gc = 1;
 553    }
 554    if (0 != GETENV("GC_PRINT_BACK_HEIGHT")) {
 555      GC_print_back_height = 1;
 556    }
 557    if (0 != GETENV("GC_NO_BLACKLIST_WARNING")) {
 558      GC_large_alloc_warn_interval = LONG_MAX;
 559    }
 560    {
 561      char * addr_string = GETENV("GC_TRACE");
 562      if (0 != addr_string) {
 563#       ifndef ENABLE_TRACE
 564	  WARN("Tracing not enabled: Ignoring GC_TRACE value\n", 0);
 565#       else
 566#	  ifdef STRTOULL
 567	    long long addr = strtoull(addr_string, NULL, 16);
 568#	  else
 569	    long addr = strtoul(addr_string, NULL, 16);
 570#	  endif
 571	  if (addr < 0x1000)
 572	      WARN("Unlikely trace address: 0x%lx\n", (GC_word)addr);
 573	  GC_trace_addr = (ptr_t)addr;
 574#	endif
 575      }
 576    }
 577    {
 578      char * time_limit_string = GETENV("GC_PAUSE_TIME_TARGET");
 579      if (0 != time_limit_string) {
 580        long time_limit = atol(time_limit_string);
 581        if (time_limit < 5) {
 582	  WARN("GC_PAUSE_TIME_TARGET environment variable value too small "
 583	       "or bad syntax: Ignoring\n", 0);
 584        } else {
 585	  GC_time_limit = time_limit;
 586        }
 587      }
 588    }
 589    {
 590      char * interval_string = GETENV("GC_LARGE_ALLOC_WARN_INTERVAL");
 591      if (0 != interval_string) {
 592        long interval = atol(interval_string);
 593        if (interval <= 0) {
 594	  WARN("GC_LARGE_ALLOC_WARN_INTERVAL environment variable has "
 595	       "bad value: Ignoring\n", 0);
 596        } else {
 597	  GC_large_alloc_warn_interval = interval;
 598        }
 599      }
 600    }
 601    maybe_install_looping_handler();
 602    /* Adjust normal object descriptor for extra allocation.	*/
 603    if (ALIGNMENT > GC_DS_TAGS && EXTRA_BYTES != 0) {
 604      GC_obj_kinds[NORMAL].ok_descriptor = ((word)(-ALIGNMENT) | GC_DS_LENGTH);
 605    }
 606    GC_setpagesize();
 607    GC_exclude_static_roots(beginGC_arrays, endGC_arrays);
 608    GC_exclude_static_roots(beginGC_obj_kinds, endGC_obj_kinds);
 609#   ifdef SEPARATE_GLOBALS
 610      GC_exclude_static_roots(beginGC_objfreelist, endGC_objfreelist);
 611      GC_exclude_static_roots(beginGC_aobjfreelist, endGC_aobjfreelist);
 612#   endif
 613#   ifdef MSWIN32
 614 	GC_init_win32();
 615#   endif
 616#   if defined(USE_PROC_FOR_LIBRARIES) && defined(GC_LINUX_THREADS)
 617	WARN("USE_PROC_FOR_LIBRARIES + GC_LINUX_THREADS performs poorly.\n", 0);
 618	/* If thread stacks are cached, they tend to be scanned in 	*/
 619	/* entirety as part of the root set.  This wil grow them to	*/
 620	/* maximum size, and is generally not desirable.		*/
 621#   endif
 622#   if defined(SEARCH_FOR_DATA_START)
 623	GC_init_linux_data_start();
 624#   endif
 625#   if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__)
 626	GC_init_netbsd_elf();
 627#   endif
 628#   if !defined(THREADS) || defined(GC_PTHREADS) || defined(GC_WIN32_THREADS) \
 629	|| defined(GC_SOLARIS_THREADS)
 630      if (GC_stackbottom == 0) {
 631	GC_stackbottom = GC_get_main_stack_base();
 632#       if (defined(LINUX) || defined(HPUX)) && defined(IA64)
 633	  GC_register_stackbottom = GC_get_register_stack_base();
 634#       endif
 635      } else {
 636#       if (defined(LINUX) || defined(HPUX)) && defined(IA64)
 637	  if (GC_register_stackbottom == 0) {
 638	    WARN("GC_register_stackbottom should be set with GC_stackbottom\n", 0);
 639	    /* The following may fail, since we may rely on	 	*/
 640	    /* alignment properties that may not hold with a user set	*/
 641	    /* GC_stackbottom.						*/
 642	    GC_register_stackbottom = GC_get_register_stack_base();
 643	  }
 644#	endif
 645      }
 646#   endif
 647    /* Ignore gcc -Wall warnings on the following. */
 648    GC_STATIC_ASSERT(sizeof (ptr_t) == sizeof(word));
 649    GC_STATIC_ASSERT(sizeof (signed_word) == sizeof(word));
 650    GC_STATIC_ASSERT(sizeof (struct hblk) == HBLKSIZE);
 651#   ifndef THREADS
 652#     ifdef STACK_GROWS_DOWN
 653        GC_ASSERT((word)(&dummy) <= (word)GC_stackbottom);
 654#     else
 655        GC_ASSERT((word)(&dummy) >= (word)GC_stackbottom);
 656#     endif
 657#   endif
 658#   if !defined(_AUX_SOURCE) || defined(__GNUC__)
 659      GC_ASSERT((word)(-1) > (word)0);
 660      /* word should be unsigned */
 661#   endif
 662    GC_ASSERT((ptr_t)(word)(-1) > (ptr_t)0);
 663    	/* Ptr_t comparisons should behave as unsigned comparisons.	*/
 664    GC_ASSERT((signed_word)(-1) < (signed_word)0);
 665#   if !defined(SMALL_CONFIG)
 666      if (GC_incremental || 0 != GETENV("GC_ENABLE_INCREMENTAL")) {
 667	/* This used to test for !GC_no_win32_dlls.  Why? */
 668        GC_setpagesize();
 669	/* For GWW_MPROTECT on Win32, this needs to happen before any	*/
 670	/* heap memory is allocated.					*/
 671        GC_dirty_init();
 672        GC_ASSERT(GC_bytes_allocd == 0)
 673    	GC_incremental = TRUE;
 674      }
 675#   endif /* !SMALL_CONFIG */
 676    
 677    /* Add initial guess of root sets.  Do this first, since sbrk(0)	*/
 678    /* might be used.							*/
 679      if (GC_REGISTER_MAIN_STATIC_DATA()) GC_register_data_segments();
 680    GC_init_headers();
 681    GC_bl_init();
 682    GC_mark_init();
 683    {
 684	char * sz_str = GETENV("GC_INITIAL_HEAP_SIZE");
 685	if (sz_str != NULL) {
 686	  initial_heap_sz = atoi(sz_str);
 687	  if (initial_heap_sz <= MINHINCR * HBLKSIZE) {
 688	    WARN("Bad initial heap size %s - ignoring it.\n",
 689		 sz_str);
 690	  } 
 691	  initial_heap_sz = divHBLKSZ(initial_heap_sz);
 692	}
 693    }
 694    {
 695	char * sz_str = GETENV("GC_MAXIMUM_HEAP_SIZE");
 696	if (sz_str != NULL) {
 697	  word max_heap_sz = (word)atol(sz_str);
 698	  if (max_heap_sz < initial_heap_sz * HBLKSIZE) {
 699	    WARN("Bad maximum heap size %s - ignoring it.\n",
 700		 sz_str);
 701	  } 
 702	  if (0 == GC_max_retries) GC_max_retries = 2;
 703	  GC_set_max_heap_size(max_heap_sz);
 704	}
 705    }
 706    if (!GC_expand_hp_inner(initial_heap_sz)) {
 707        GC_err_printf("Can't start up: not enough memory\n");
 708        EXIT();
 709    }
 710    GC_initialize_offsets();
 711    GC_register_displacement_inner(0L);
 712#   if defined(GC_LINUX_THREADS) && defined(REDIRECT_MALLOC)
 713      if (!GC_all_interior_pointers) {
 714	/* TLS ABI uses pointer-sized offsets for dtv. */
 715        GC_register_displacement_inner(sizeof(void *));
 716      }
 717#   endif
 718    GC_init_size_map();
 719#   ifdef PCR
 720      if (PCR_IL_Lock(PCR_Bool_false, PCR_allSigsBlocked, PCR_waitForever)
 721          != PCR_ERes_okay) {
 722          ABORT("Can't lock load state\n");
 723      } else if (PCR_IL_Unlock() != PCR_ERes_okay) {
 724          ABORT("Can't unlock load state\n");
 725      }
 726      PCR_IL_Unlock();
 727      GC_pcr_install();
 728#   endif
 729    GC_is_initialized = TRUE;
 730#   if defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
 731        GC_thr_init();
 732#   endif
 733    COND_DUMP;
 734    /* Get black list set up and/or incremental GC started */
 735      if (!GC_dont_precollect || GC_incremental) GC_gcollect_inner();
 736#   ifdef STUBBORN_ALLOC
 737    	GC_stubborn_init();
 738#   endif
 739    /* Convince lint that some things are used */
 740#   ifdef LINT
 741      {
 742          extern char * GC_copyright[];
 743          extern int GC_read();
 744          extern void GC_register_finalizer_no_order();
 745          
 746          GC_noop(GC_copyright, GC_find_header,
 747                  GC_push_one, GC_call_with_alloc_lock, GC_read,
 748                  GC_dont_expand,
 749#		  ifndef NO_DEBUGGING
 750		    GC_dump,
 751#		  endif
 752                  GC_register_finalizer_no_order);
 753      }
 754#   endif
 755
 756    /* The rest of this again assumes we don't really hold	*/
 757    /* the allocation lock.					*/
 758#   if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
 759	/* Make sure marker threads and started and thread local */
 760	/* allocation is initialized, in case we didn't get 	 */
 761	/* called from GC_init_parallel();			 */
 762        {
 763	  GC_init_parallel();
 764	}
 765#   endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
 766
 767#   if defined(DYNAMIC_LOADING) && defined(DARWIN)
 768    {
 769        /* This must be called WITHOUT the allocation lock held
 770        and before any threads are created */
 771        extern void GC_init_dyld();
 772        GC_init_dyld();
 773    }
 774#   endif
 775}
 776
 777void GC_enable_incremental(void)
 778{
 779# if !defined(SMALL_CONFIG) && !defined(KEEP_BACK_PTRS)
 780  /* If we are keeping back pointers, the GC itself dirties all	*/
 781  /* pages on which objects have been marked, making 		*/
 782  /* incremental GC pointless.					*/
 783  if (!GC_find_leak) {
 784    DCL_LOCK_STATE;
 785    
 786    LOCK();
 787    if (GC_incremental) goto out;
 788    GC_setpagesize();
 789    /* if (GC_no_win32_dlls) goto out; Should be win32S test? */
 790    maybe_install_looping_handler();  /* Before write fault handler! */
 791    GC_incremental = TRUE;
 792    if (!GC_is_initialized) {
 793        GC_init_inner();
 794    } else {
 795	GC_dirty_init();
 796    }
 797    if (!GC_dirty_maintained) goto out;
 798    if (GC_dont_gc) {
 799        /* Can't easily do it. */
 800        UNLOCK();
 801    	return;
 802    }
 803    if (GC_bytes_allocd > 0) {
 804    	/* There may be unmarked reachable objects	*/
 805    	GC_gcollect_inner();
 806    }   /* else we're OK in assuming everything's	*/
 807    	/* clean since nothing can point to an	  	*/
 808    	/* unmarked object.			  	*/
 809    GC_read_dirty();
 810out:
 811    UNLOCK();
 812  } else {
 813    GC_init();
 814  }
 815# else
 816    GC_init();
 817# endif
 818}
 819
 820
 821#if defined(MSWIN32) || defined(MSWINCE)
 822# if defined(_MSC_VER) && defined(_DEBUG)
 823#  include <crtdbg.h>
 824# endif
 825# ifdef OLD_WIN32_LOG_FILE
 826#   define LOG_FILE _T("gc.log")
 827# endif
 828
 829  HANDLE GC_stdout = 0;
 830
 831  void GC_deinit()
 832  {
 833      if (GC_is_initialized) {
 834  	DeleteCriticalSection(&GC_write_cs);
 835      }
 836  }
 837
 838# ifndef THREADS
 839#   define GC_need_to_lock 0  /* Not defined without threads */
 840# endif
 841  int GC_write(const char *buf, size_t len)
 842  {
 843      BOOL tmp;
 844      DWORD written;
 845      if (len == 0)
 846	  return 0;
 847      if (GC_need_to_lock) EnterCriticalSection(&GC_write_cs);
 848      if (GC_stdout == INVALID_HANDLE_VALUE) {
 849          if (GC_need_to_lock) LeaveCriticalSection(&GC_write_cs);
 850	  return -1;
 851      } else if (GC_stdout == 0) {
 852	char * file_name = GETENV("GC_LOG_FILE");
 853	char logPath[_MAX_PATH + 5];
 854
 855        if (0 == file_name) {
 856#         ifdef OLD_WIN32_LOG_FILE
 857	    strcpy(logPath, LOG_FILE);
 858#	  else
 859	    GetModuleFileName(NULL, logPath, _MAX_PATH);
 860	    strcat(logPath, ".log");
 861#	  endif
 862	  file_name = logPath;
 863	}
 864	GC_stdout = CreateFile(logPath, GENERIC_WRITE,
 865        		       FILE_SHARE_READ,
 866        		       NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH,
 867        		       NULL); 
 868    	if (GC_stdout == INVALID_HANDLE_VALUE)
 869	    ABORT("Open of log file failed");
 870      }
 871      tmp = WriteFile(GC_stdout, buf, (DWORD)len, &written, NULL);
 872      if (!tmp)
 873	  DebugBreak();
 874#     if defined(_MSC_VER) && defined(_DEBUG)
 875	  _CrtDbgReport(_CRT_WARN, NULL, 0, NULL, "%.*s", len, buf);
 876#     endif
 877      if (GC_need_to_lock) LeaveCriticalSection(&GC_write_cs);
 878      return tmp ? (int)written : -1;
 879  }
 880# undef GC_need_to_lock
 881
 882#endif
 883
 884#if defined(OS2) || defined(MACOS)
 885FILE * GC_stdout = NULL;
 886FILE * GC_stderr = NULL;
 887FILE * GC_log = NULL;
 888int GC_tmp;  /* Should really be local ... */
 889
 890  void GC_set_files()
 891  {
 892      if (GC_stdout == NULL) {
 893	GC_stdout = stdout;
 894    }
 895    if (GC_stderr == NULL) {
 896	GC_stderr = stderr;
 897    }
 898    if (GC_log == NULL) {
 899	GC_log = stderr;
 900    }
 901  }
 902#endif
 903
 904#if !defined(OS2) && !defined(MACOS) && !defined(MSWIN32) && !defined(MSWINCE)
 905  int GC_stdout = 1;
 906  int GC_stderr = 2;
 907  int GC_log = 2;
 908# if !defined(AMIGA)
 909#   include <unistd.h>
 910# endif
 911#endif
 912
 913#if !defined(MSWIN32) && !defined(MSWINCE) && !defined(OS2) \
 914    && !defined(MACOS)  && !defined(ECOS) && !defined(NOSYS)
 915int GC_write(fd, buf, len)
 916int fd;
 917const char *buf;
 918size_t len;
 919{
 920     register int bytes_written = 0;
 921     register int result;
 922     
 923     while (bytes_written < len) {
 924#	ifdef GC_SOLARIS_THREADS
 925	    result = syscall(SYS_write, fd, buf + bytes_written,
 926	    			  	    len - bytes_written);
 927#	else
 928     	    result = write(fd, buf + bytes_written, len - bytes_written);
 929#	endif
 930	if (-1 == result) return(result);
 931	bytes_written += result;
 932    }
 933    return(bytes_written);
 934}
 935#endif /* UN*X */
 936
 937#ifdef ECOS
 938int GC_write(fd, buf, len)
 939{
 940  _Jv_diag_write (buf, len);
 941  return len;
 942}
 943#endif
 944
 945#ifdef NOSYS
 946int GC_write(fd, buf, len)
 947{
 948  /* No writing.  */
 949  return len;
 950}
 951#endif
 952
 953
 954#if defined(MSWIN32) || defined(MSWINCE)
 955    /* FIXME: This is pretty ugly ... */
 956#   define WRITE(f, buf, len) GC_write(buf, len)
 957#else
 958#   if defined(OS2) || defined(MACOS)
 959#   define WRITE(f, buf, len) (GC_set_files(), \
 960			       GC_tmp = fwrite((buf), 1, (len), (f)), \
 961			       fflush(f), GC_tmp)
 962#   else
 963#     define WRITE(f, buf, len) GC_write((f), (buf), (len))
 964#   endif
 965#endif
 966
 967#define BUFSZ 1024
 968#ifdef _MSC_VER
 969# define vsnprintf _vsnprintf
 970#endif
 971/* A version of printf that is unlikely to call malloc, and is thus safer */
 972/* to call from the collector in case malloc has been bound to GC_malloc. */
 973/* Floating point arguments ans formats should be avoided, since fp	  */
 974/* conversion is more likely to allocate.				  */
 975/* Assumes that no more than BUFSZ-1 characters are written at once.	  */
 976void GC_printf(const char *format, ...)
 977{
 978    va_list args;
 979    char buf[BUFSZ+1];
 980    
 981    va_start(args, format);
 982    if (GC_quiet) return;
 983    buf[BUFSZ] = 0x15;
 984    (void) vsnprintf(buf, BUFSZ, format, args);
 985    va_end(args);
 986    if (buf[BUFSZ] != 0x15) ABORT("GC_printf clobbered stack");
 987    if (WRITE(GC_stdout, buf, strlen(buf)) < 0) ABORT("write to stdout failed");
 988}
 989
 990void GC_err_printf(const char *format, ...)
 991{
 992    va_list args;
 993    char buf[BUFSZ+1];
 994    
 995    va_start(args, format);
 996    buf[BUFSZ] = 0x15;
 997    (void) vsnprintf(buf, BUFSZ, format, args);
 998    va_end(args);
 999    if (buf[BUFSZ] != 0x15) ABORT("GC_printf clobbered stack");
1000    if (WRITE(GC_stderr, buf, strlen(buf)) < 0) ABORT("write to stderr failed");
1001}
1002
1003void GC_log_printf(const char *format, ...)
1004{
1005    va_list args;
1006    char buf[BUFSZ+1];
1007    
1008    va_start(args, format);
1009    buf[BUFSZ] = 0x15;
1010    (void) vsnprintf(buf, BUFSZ, format, args);
1011    va_end(args);
1012    if (buf[BUFSZ] != 0x15) ABORT("GC_printf clobbered stack");
1013    if (WRITE(GC_log, buf, strlen(buf)) < 0) ABORT("write to log failed");
1014}
1015
1016void GC_err_puts(const char *s)
1017{
1018    if (WRITE(GC_stderr, s, strlen(s)) < 0) ABORT("write to stderr failed");
1019}
1020
1021#if defined(LINUX) && !defined(SMALL_CONFIG)
1022void GC_err_write(buf, len)
1023const char *buf;
1024size_t len;
1025{
1026    if (WRITE(GC_stderr, buf, len) < 0) ABORT("write to stderr failed");
1027}
1028#endif
1029
1030void GC_default_warn_proc(char *msg, GC_word arg)
1031{
1032    GC_err_printf(msg, arg);
1033}
1034
1035GC_warn_proc GC_current_warn_proc = GC_default_warn_proc;
1036
1037GC_warn_proc GC_set_warn_proc(GC_warn_proc p)
1038{
1039    GC_warn_proc result;
1040
1041#   ifdef GC_WIN32_THREADS
1042      GC_ASSERT(GC_is_initialized);
1043#   endif
1044    LOCK();
1045    result = GC_current_warn_proc;
1046    GC_current_warn_proc = p;
1047    UNLOCK();
1048    return(result);
1049}
1050
1051GC_word GC_set_free_space_divisor (GC_word value)
1052{
1053    GC_word old = GC_free_space_divisor;
1054    GC_free_space_divisor = value;
1055    return old;
1056}
1057
1058#ifndef PCR
1059void GC_abort(const char *msg)
1060{
1061#   if defined(MSWIN32)
1062      (void) MessageBoxA(NULL, msg, "Fatal error in gc", MB_ICONERROR|MB_OK);
1063#   else
1064      GC_err_printf("%s\n", msg);
1065#   endif
1066    if (GETENV("GC_LOOP_ON_ABORT") != NULL) {
1067	    /* In many cases it's easier to debug a running process.	*/
1068	    /* It's arguably nicer to sleep, but that makes it harder	*/
1069	    /* to look at the thread if the debugger doesn't know much	*/
1070	    /* about threads.						*/
1071	    for(;;) {}
1072    }
1073#   if defined(MSWIN32) || defined(MSWINCE)
1074	DebugBreak();
1075#   else
1076        (void) abort();
1077#   endif
1078}
1079#endif
1080
1081void GC_enable()
1082{
1083    LOCK();
1084    GC_dont_gc--;
1085    UNLOCK();
1086}
1087
1088void GC_disable()
1089{
1090    LOCK();
1091    GC_dont_gc++;
1092    UNLOCK();
1093}
1094
1095/* Helper procedures for new kind creation.	*/
1096void ** GC_new_free_list_inner()
1097{
1098    void *result = GC_INTERNAL_MALLOC((MAXOBJGRANULES+1)*sizeof(ptr_t),
1099		    		      PTRFREE);
1100    if (result == 0) ABORT("Failed to allocate freelist for new kind");
1101    BZERO(result, (MAXOBJGRANULES+1)*sizeof(ptr_t));
1102    return result;
1103}
1104
1105void ** GC_new_free_list()
1106{
1107    void *result;
1108    LOCK();
1109    result = GC_new_free_list_inner();
1110    UNLOCK();
1111    return result;
1112}
1113
1114unsigned GC_new_kind_inner(void **fl, GC_word descr, int adjust, int clear)
1115{
1116    unsigned result = GC_n_kinds++;
1117
1118    if (GC_n_kinds > MAXOBJKINDS) ABORT("Too many kinds");
1119    GC_obj_kinds[result].ok_freelist = fl;
1120    GC_obj_kinds[result].ok_reclaim_list = 0;
1121    GC_obj_kinds[result].ok_descriptor = descr;
1122    GC_obj_kinds[result].ok_relocate_descr = adjust;
1123    GC_obj_kinds[result].ok_init = clear;
1124    return result;
1125}
1126
1127unsigned GC_new_kind(void **fl, GC_word descr, int adjust, int clear)
1128{
1129    unsigned result;
1130    LOCK();
1131    result = GC_new_kind_inner(fl, descr, adjust, clear);
1132    UNLOCK();
1133    return result;
1134}
1135
1136unsigned GC_new_proc_inner(GC_mark_proc proc)
1137{
1138    unsigned result = GC_n_mark_procs++;
1139
1140    if (GC_n_mark_procs > MAX_MARK_PROCS) ABORT("Too many mark procedures");
1141    GC_mark_procs[result] = proc;
1142    return result;
1143}
1144
1145unsigned GC_new_proc(GC_mark_proc proc)
1146{
1147    unsigned result;
1148    LOCK();
1149    result = GC_new_proc_inner(proc);
1150    UNLOCK();
1151    return result;
1152}
1153
1154GC_API void * GC_call_with_stack_base(GC_stack_base_func fn, void *arg)
1155{
1156    int dummy;
1157    struct GC_stack_base base;
1158
1159    base.mem_base = (void *)&dummy;
1160#   ifdef IA64
1161      base.reg_base = (void *)GC_save_regs_in_stack();
1162      /* Unnecessarily flushes register stack, 		*/
1163      /* but that probably doesn't hurt.		*/
1164#   endif
1165    return fn(&base, arg);
1166}
1167
1168#if !defined(NO_DEBUGGING)
1169
1170void GC_dump()
1171{
1172    GC_printf("***Static roots:\n");
1173    GC_print_static_roots();
1174    GC_printf("\n***Heap sections:\n");
1175    GC_print_heap_sects();
1176    GC_printf("\n***Free blocks:\n");
1177    GC_print_hblkfreelist();
1178    GC_printf("\n***Blocks in use:\n");
1179    GC_print_block_list();
1180    GC_printf("\n***Finalization statistics:\n");
1181    GC_print_finalization_stats();
1182}
1183
1184#endif /* NO_DEBUGGING */