root/gc/misc.c

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

DEFINITIONS

This source file includes following definitions.
  1. GC_PROTO
  2. GC_project2
  3. GC_init_size_map
  4. GC_extend_size_map
  5. GC_clear_stack_inner
  6. GC_clear_stack
  7. GC_base
  8. GC_size
  9. GC_PROTO
  10. GC_PROTO
  11. GC_PROTO
  12. GC_PROTO
  13. GC_init
  14. GC_PROTO
  15. looping_handler
  16. maybe_install_looping_handler
  17. GC_init_inner
  18. GC_PROTO
  19. GC_deinit
  20. GC_write
  21. GC_set_files
  22. GC_write
  23. GC_write
  24. GC_write
  25. GC_printf
  26. GC_err_printf
  27. GC_err_puts
  28. GC_err_write
  29. GC_default_warn_proc
  30. GC_set_warn_proc
  31. GC_set_free_space_divisor
  32. GC_abort
  33. GC_enable
  34. GC_disable
  35. GC_new_free_list_inner
  36. GC_new_free_list
  37. GC_new_kind_inner
  38. GC_new_kind
  39. GC_new_proc_inner
  40. GC_new_proc
  41. GC_dump

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

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