boehm-gc: revert all CACAO-specific modifications; this is now an exact copy of the...
[cacao.git] / src / mm / boehm-gc / misc.c
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 #if defined(UNIX_LIKE) || defined(CYGWIN32)
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
69 GC_FAR struct _GC_arrays GC_arrays /* = { 0 } */;
70
71
72 GC_bool GC_debugging_started = FALSE;
73         /* defined here so we don't have to load debug_malloc.o */
74
75 void (*GC_check_heap) (void) = (void (*) (void))0;
76 void (*GC_print_all_smashed) (void) = (void (*) (void))0;
77
78 void (*GC_start_call_back) (void) = (void (*) (void))0;
79
80 ptr_t GC_stackbottom = 0;
81
82 #ifdef IA64
83   ptr_t GC_register_stackbottom = 0;
84 #endif
85
86 GC_bool GC_dont_gc = 0;
87
88 GC_bool GC_dont_precollect = 0;
89
90 GC_bool GC_quiet = 0;
91
92 #ifndef SMALL_CONFIG
93   GC_bool GC_print_stats = 0;
94 #endif
95
96 GC_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
119 long GC_large_alloc_warn_interval = 5;
120         /* Interval between unsuppressed warnings.      */
121
122 long GC_large_alloc_warn_suppressed = 0;
123         /* Number of warnings suppressed so far.        */
124
125 /*ARGSUSED*/
126 STATIC void * GC_CALLBACK GC_default_oom_fn(size_t bytes_requested)
127 {
128     return(0);
129 }
130
131 GC_oom_func GC_oom_fn = GC_default_oom_fn;
132
133 /* Set things up so that GC_size_map[i] >= granules(i),         */
134 /* but not too much bigger                                              */
135 /* and so that size_map contains relatively few distinct entries        */
136 /* This was originally stolen from Russ Atkinson's Cedar                */
137 /* quantization algorithm (but we precompute it).                       */ 
138 STATIC void GC_init_size_map(void)
139 {
140     int i;
141
142     /* Map size 0 to something bigger.                  */
143     /* This avoids problems at lower levels.            */
144       GC_size_map[0] = 1;
145     for (i = 1; i <= GRANULES_TO_BYTES(TINY_FREELISTS-1) - EXTRA_BYTES; i++) {
146         GC_size_map[i] = ROUNDED_UP_GRANULES(i);
147 #       ifndef _MSC_VER
148           GC_ASSERT(GC_size_map[i] < TINY_FREELISTS);
149           /* Seems to tickle bug in VC++ 2008 for AMD64 */
150 #       endif
151     }
152     /* We leave the rest of the array to be filled in on demand. */
153 }
154
155 /* Fill in additional entries in GC_size_map, including the ith one */
156 /* We assume the ith entry is currently 0.                              */
157 /* Note that a filled in section of the array ending at n always    */
158 /* has length at least n/4.                                             */
159 void GC_extend_size_map(size_t i)
160 {
161     size_t orig_granule_sz = ROUNDED_UP_GRANULES(i);
162     size_t granule_sz = orig_granule_sz;
163     size_t byte_sz = GRANULES_TO_BYTES(granule_sz);
164                         /* The size we try to preserve.         */
165                         /* Close to i, unless this would        */
166                         /* introduce too many distinct sizes.   */
167     size_t smaller_than_i = byte_sz - (byte_sz >> 3);
168     size_t much_smaller_than_i = byte_sz - (byte_sz >> 2);
169     size_t low_limit;   /* The lowest indexed entry we  */
170                         /* initialize.                  */
171     size_t j;
172     
173     if (GC_size_map[smaller_than_i] == 0) {
174         low_limit = much_smaller_than_i;
175         while (GC_size_map[low_limit] != 0) low_limit++;
176     } else {
177         low_limit = smaller_than_i + 1;
178         while (GC_size_map[low_limit] != 0) low_limit++;
179         granule_sz = ROUNDED_UP_GRANULES(low_limit);
180         granule_sz += granule_sz >> 3;
181         if (granule_sz < orig_granule_sz) granule_sz = orig_granule_sz;
182     }
183     /* For these larger sizes, we use an even number of granules.       */
184     /* This makes it easier to, for example, construct a 16byte-aligned */
185     /* allocator even if GRANULE_BYTES is 8.                            */
186         granule_sz += 1;
187         granule_sz &= ~1;
188     if (granule_sz > MAXOBJGRANULES) {
189         granule_sz = MAXOBJGRANULES;
190     }
191     /* If we can fit the same number of larger objects in a block,      */
192     /* do so.                                                   */ 
193     {
194         size_t number_of_objs = HBLK_GRANULES/granule_sz;
195         granule_sz = HBLK_GRANULES/number_of_objs;
196         granule_sz &= ~1;
197     }
198     byte_sz = GRANULES_TO_BYTES(granule_sz);
199     /* We may need one extra byte;                      */
200     /* don't always fill in GC_size_map[byte_sz]        */
201     byte_sz -= EXTRA_BYTES;
202
203     for (j = low_limit; j <= byte_sz; j++) GC_size_map[j] = granule_sz;  
204 }
205
206
207 /*
208  * The following is a gross hack to deal with a problem that can occur
209  * on machines that are sloppy about stack frame sizes, notably SPARC.
210  * Bogus pointers may be written to the stack and not cleared for
211  * a LONG time, because they always fall into holes in stack frames
212  * that are not written.  We partially address this by clearing
213  * sections of the stack whenever we get control.
214  */
215 # ifdef THREADS
216 #   define BIG_CLEAR_SIZE 2048  /* Clear this much now and then.        */
217 #   define SMALL_CLEAR_SIZE 256 /* Clear this much every time.          */
218 # else
219   STATIC word GC_stack_last_cleared = 0; /* GC_no when we last did this */
220   STATIC ptr_t GC_min_sp; /* Coolest stack pointer value from which     */
221                           /* we've already cleared the stack.           */
222   STATIC ptr_t GC_high_water;
223                         /* "hottest" stack pointer value we have seen   */
224                         /* recently.  Degrades over time.               */
225   STATIC word GC_bytes_allocd_at_reset;
226 #   define DEGRADE_RATE 50
227 # endif
228
229 # define CLEAR_SIZE 213  /* Granularity for GC_clear_stack_inner */
230
231 #if defined(ASM_CLEAR_CODE)
232   extern void *GC_clear_stack_inner(void *, ptr_t);
233 #else  
234 /* Clear the stack up to about limit.  Return arg. */
235 /*ARGSUSED*/
236 void * GC_clear_stack_inner(void *arg, ptr_t limit)
237 {
238     word dummy[CLEAR_SIZE];
239     
240     BZERO(dummy, CLEAR_SIZE*sizeof(word));
241     if ((ptr_t)(dummy) COOLER_THAN limit) {
242         (void) GC_clear_stack_inner(arg, limit);
243     }
244     /* Make sure the recursive call is not a tail call, and the bzero   */
245     /* call is not recognized as dead code.                             */
246     GC_noop1((word)dummy);
247     return(arg);
248 }
249 #endif
250
251 /* Clear some of the inaccessible part of the stack.  Returns its       */
252 /* argument, so it can be used in a tail call position, hence clearing  */
253 /* another frame.                                                       */
254 void * GC_clear_stack(void *arg)
255 {
256     ptr_t sp = GC_approx_sp();  /* Hotter than actual sp */
257 #   ifdef THREADS
258         word dummy[SMALL_CLEAR_SIZE];
259         static unsigned random_no = 0;
260                                  /* Should be more random than it is ... */
261                                  /* Used to occasionally clear a bigger  */
262                                  /* chunk.                               */
263 #   endif
264     ptr_t limit;
265     
266 #   define SLOP 400
267         /* Extra bytes we clear every time.  This clears our own        */
268         /* activation record, and should cause more frequent            */
269         /* clearing near the cold end of the stack, a good thing.       */
270 #   define GC_SLOP 4000
271         /* We make GC_high_water this much hotter than we really saw    */
272         /* saw it, to cover for GC noise etc. above our current frame.  */
273 #   define CLEAR_THRESHOLD 100000
274         /* We restart the clearing process after this many bytes of     */
275         /* allocation.  Otherwise very heavily recursive programs       */
276         /* with sparse stacks may result in heaps that grow almost      */
277         /* without bounds.  As the heap gets larger, collection         */
278         /* frequency decreases, thus clearing frequency would decrease, */
279         /* thus more junk remains accessible, thus the heap gets        */
280         /* larger ...                                                   */
281 # ifdef THREADS
282     if (++random_no % 13 == 0) {
283         limit = sp;
284         MAKE_HOTTER(limit, BIG_CLEAR_SIZE*sizeof(word));
285         limit = (ptr_t)((word)limit & ~0xf);
286                         /* Make it sufficiently aligned for assembly    */
287                         /* implementations of GC_clear_stack_inner.     */
288         return GC_clear_stack_inner(arg, limit);
289     } else {
290         BZERO(dummy, SMALL_CLEAR_SIZE*sizeof(word));
291         return arg;
292     }
293 # else
294     if (GC_gc_no > GC_stack_last_cleared) {
295         /* Start things over, so we clear the entire stack again */
296         if (GC_stack_last_cleared == 0) GC_high_water = (ptr_t)GC_stackbottom;
297         GC_min_sp = GC_high_water;
298         GC_stack_last_cleared = GC_gc_no;
299         GC_bytes_allocd_at_reset = GC_bytes_allocd;
300     }
301     /* Adjust GC_high_water */
302         MAKE_COOLER(GC_high_water, WORDS_TO_BYTES(DEGRADE_RATE) + GC_SLOP);
303         if (sp HOTTER_THAN GC_high_water) {
304             GC_high_water = sp;
305         }
306         MAKE_HOTTER(GC_high_water, GC_SLOP);
307     limit = GC_min_sp;
308     MAKE_HOTTER(limit, SLOP);
309     if (sp COOLER_THAN limit) {
310         limit = (ptr_t)((word)limit & ~0xf);
311                         /* Make it sufficiently aligned for assembly    */
312                         /* implementations of GC_clear_stack_inner.     */
313         GC_min_sp = sp;
314         return(GC_clear_stack_inner(arg, limit));
315     } else if (GC_bytes_allocd - GC_bytes_allocd_at_reset > CLEAR_THRESHOLD) {
316         /* Restart clearing process, but limit how much clearing we do. */
317         GC_min_sp = sp;
318         MAKE_HOTTER(GC_min_sp, CLEAR_THRESHOLD/4);
319         if (GC_min_sp HOTTER_THAN GC_high_water) GC_min_sp = GC_high_water;
320         GC_bytes_allocd_at_reset = GC_bytes_allocd;
321     }  
322     return(arg);
323 # endif
324 }
325
326
327 /* Return a pointer to the base address of p, given a pointer to a      */
328 /* an address within an object.  Return 0 o.w.                          */
329 GC_API void * GC_CALL GC_base(void * p)
330 {
331     ptr_t r;
332     struct hblk *h;
333     bottom_index *bi;
334     hdr *candidate_hdr;
335     ptr_t limit;
336     
337     r = p;
338     if (!GC_is_initialized) return 0;
339     h = HBLKPTR(r);
340     GET_BI(r, bi);
341     candidate_hdr = HDR_FROM_BI(bi, r);
342     if (candidate_hdr == 0) return(0);
343     /* If it's a pointer to the middle of a large object, move it       */
344     /* to the beginning.                                                */
345         while (IS_FORWARDING_ADDR_OR_NIL(candidate_hdr)) {
346            h = FORWARDED_ADDR(h,candidate_hdr);
347            r = (ptr_t)h;
348            candidate_hdr = HDR(h);
349         }
350     if (HBLK_IS_FREE(candidate_hdr)) return(0);
351     /* Make sure r points to the beginning of the object */
352         r = (ptr_t)((word)r & ~(WORDS_TO_BYTES(1) - 1));
353         {
354             size_t offset = HBLKDISPL(r);
355             signed_word sz = candidate_hdr -> hb_sz;
356             size_t obj_displ = offset % sz;
357
358             r -= obj_displ;
359             limit = r + sz;
360             if (limit > (ptr_t)(h + 1) && sz <= HBLKSIZE) {
361                 return(0);
362             }
363             if ((ptr_t)p >= limit) return(0);
364         }
365     return((void *)r);
366 }
367
368
369 /* Return the size of an object, given a pointer to its base.           */
370 /* (For small objects this also happens to work from interior pointers, */
371 /* but that shouldn't be relied upon.)                                  */
372 GC_API size_t GC_CALL GC_size(void * p)
373 {
374     hdr * hhdr = HDR(p);
375     
376     return hhdr -> hb_sz;
377 }
378
379 GC_API size_t GC_CALL GC_get_heap_size(void)
380 {
381     size_t value;
382     DCL_LOCK_STATE;
383     LOCK();
384     /* ignore the memory space returned to OS (i.e. count only the      */
385     /* space owned by the garbage collector)                            */
386     value = (size_t)(GC_heapsize - GC_unmapped_bytes);
387     UNLOCK();
388     return value;
389 }
390
391 GC_API size_t GC_CALL GC_get_free_bytes(void)
392 {
393     size_t value;
394     DCL_LOCK_STATE;
395     LOCK();
396     /* ignore the memory space returned to OS */
397     value = (size_t)(GC_large_free_bytes - GC_unmapped_bytes);
398     UNLOCK();
399     return value;
400 }
401
402 GC_API size_t GC_CALL GC_get_unmapped_bytes(void)
403 {
404 # ifdef USE_MUNMAP
405     size_t value;
406     DCL_LOCK_STATE;
407     LOCK();
408     value = (size_t)GC_unmapped_bytes;
409     UNLOCK();
410     return value;
411 # else
412     return 0;
413 # endif
414 }
415
416 GC_API size_t GC_CALL GC_get_bytes_since_gc(void)
417 {
418     size_t value;
419     DCL_LOCK_STATE;
420     LOCK();
421     value = GC_bytes_allocd;
422     UNLOCK();
423     return value;
424 }
425
426 GC_API size_t GC_CALL GC_get_total_bytes(void)
427 {
428     size_t value;
429     DCL_LOCK_STATE;
430     LOCK();
431     value = GC_bytes_allocd+GC_bytes_allocd_before_gc;
432     UNLOCK();
433     return value;
434 }
435
436 GC_bool GC_is_initialized = FALSE;
437
438 # if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
439   extern void GC_init_parallel(void);
440 # endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
441
442 /* FIXME: The GC_init/GC_init_inner distinction should go away. */
443 GC_API void GC_CALL GC_init(void)
444 {
445     /* LOCK(); -- no longer does anything this early. */
446     GC_init_inner();
447     /* UNLOCK(); */
448 }
449
450 #if (defined(MSWIN32) || defined(MSWINCE)) && defined(THREADS)
451     CRITICAL_SECTION GC_write_cs;
452 #endif
453
454 #ifdef MSWIN32
455     extern void GC_init_win32(void);
456 #endif
457
458 extern void GC_setpagesize(void);
459
460 STATIC void GC_exit_check(void)
461 {
462    GC_gcollect();
463 }
464
465 #ifdef SEARCH_FOR_DATA_START
466   extern void GC_init_linux_data_start(void);
467 #endif
468
469 #ifdef UNIX_LIKE
470
471 extern void GC_set_and_save_fault_handler(void (*handler)(int));
472
473 static void looping_handler(int sig)
474 {
475     GC_err_printf("Caught signal %d: looping in handler\n", sig);
476     for(;;);
477 }
478
479 static GC_bool installed_looping_handler = FALSE;
480
481 static void maybe_install_looping_handler(void)
482 {
483     /* Install looping handler before the write fault handler, so we    */
484     /* handle write faults correctly.                                   */
485       if (!installed_looping_handler && 0 != GETENV("GC_LOOP_ON_ABORT")) {
486         GC_set_and_save_fault_handler(looping_handler);
487         installed_looping_handler = TRUE;
488       }
489 }
490
491 #else /* !UNIX_LIKE */
492
493 # define maybe_install_looping_handler()
494
495 #endif
496
497 #if defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
498   void GC_thr_init(void);
499 #endif
500
501 void GC_init_inner(void)
502 {
503 #   if !defined(THREADS) && defined(GC_ASSERTIONS)
504         word dummy;
505 #   endif
506
507 #   ifdef GC_INITIAL_HEAP_SIZE
508         word initial_heap_sz = divHBLKSZ(GC_INITIAL_HEAP_SIZE);
509 #   else
510         word initial_heap_sz = (word)MINHINCR;
511 #   endif
512     
513     if (GC_is_initialized) return;
514
515     /* Note that although we are nominally called with the */
516     /* allocation lock held, the allocation lock is now    */
517     /* only really acquired once a second thread is forked.*/
518     /* And the initialization code needs to run before     */
519     /* then.  Thus we really don't hold any locks, and can */
520     /* in fact safely initialize them here.                */
521 #   ifdef THREADS
522       GC_ASSERT(!GC_need_to_lock);
523 #   endif
524 #   if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
525       if (!GC_is_initialized) {
526         BOOL (WINAPI *pfn) (LPCRITICAL_SECTION, DWORD) = NULL;
527         HMODULE hK32 = GetModuleHandleA("kernel32.dll");
528         if (hK32)
529           pfn = (BOOL (WINAPI *) (LPCRITICAL_SECTION, DWORD))
530                 GetProcAddress (hK32,
531                                 "InitializeCriticalSectionAndSpinCount");
532         if (pfn)
533             pfn(&GC_allocate_ml, 4000);
534         else
535           InitializeCriticalSection (&GC_allocate_ml);
536       }
537 #endif /* MSWIN32 */
538 #   if (defined(MSWIN32) || defined(MSWINCE)) && defined(THREADS)
539       InitializeCriticalSection(&GC_write_cs);
540 #   endif
541 #   if (!defined(SMALL_CONFIG))
542       if (0 != GETENV("GC_PRINT_STATS")) {
543         GC_print_stats = 1;
544       } 
545       if (0 != GETENV("GC_PRINT_VERBOSE_STATS")) {
546         GC_print_stats = VERBOSE;
547       } 
548 #     if defined(UNIX_LIKE) || defined(CYGWIN32)
549         {
550           char * file_name = GETENV("GC_LOG_FILE");
551           if (0 != file_name) {
552             int log_d = open(file_name, O_CREAT|O_WRONLY|O_APPEND, 0666);
553             if (log_d < 0) {
554               GC_log_printf("Failed to open %s as log file\n", file_name);
555             } else {
556               GC_log = log_d;
557             }
558           }
559         }
560 #     endif
561 #   endif
562 #   ifndef NO_DEBUGGING
563       if (0 != GETENV("GC_DUMP_REGULARLY")) {
564         GC_dump_regularly = 1;
565       }
566 #   endif
567 #   ifdef KEEP_BACK_PTRS
568       {
569         char * backtraces_string = GETENV("GC_BACKTRACES");
570         if (0 != backtraces_string) {
571           GC_backtraces = atol(backtraces_string);
572           if (backtraces_string[0] == '\0') GC_backtraces = 1;
573         }
574       }
575 #   endif
576     if (0 != GETENV("GC_FIND_LEAK")) {
577       GC_find_leak = 1;
578       atexit(GC_exit_check);
579     }
580     if (0 != GETENV("GC_ALL_INTERIOR_POINTERS")) {
581       GC_all_interior_pointers = 1;
582     }
583     if (0 != GETENV("GC_DONT_GC")) {
584       GC_dont_gc = 1;
585     }
586     if (0 != GETENV("GC_PRINT_BACK_HEIGHT")) {
587       GC_print_back_height = 1;
588     }
589     if (0 != GETENV("GC_NO_BLACKLIST_WARNING")) {
590       GC_large_alloc_warn_interval = LONG_MAX;
591     }
592     {
593       char * addr_string = GETENV("GC_TRACE");
594       if (0 != addr_string) {
595 #       ifndef ENABLE_TRACE
596           WARN("Tracing not enabled: Ignoring GC_TRACE value\n", 0);
597 #       else
598           word addr = (word)STRTOULL(addr_string, NULL, 16);
599           if (addr < 0x1000)
600               WARN("Unlikely trace address: %p\n", addr);
601           GC_trace_addr = (ptr_t)addr;
602 #       endif
603       }
604     }
605 #   ifndef SMALL_CONFIG
606       {
607         char * time_limit_string = GETENV("GC_PAUSE_TIME_TARGET");
608         if (0 != time_limit_string) {
609           long time_limit = atol(time_limit_string);
610           if (time_limit < 5) {
611             WARN("GC_PAUSE_TIME_TARGET environment variable value too small "
612                  "or bad syntax: Ignoring\n", 0);
613           } else {
614             GC_time_limit = time_limit;
615           }
616         }
617       }
618       {
619         char * full_freq_string = GETENV("GC_FULL_FREQUENCY");
620         if (full_freq_string != NULL) {
621           int full_freq = atoi(full_freq_string);
622           if (full_freq > 0)
623             GC_full_freq = full_freq;
624         }
625       }
626 #   endif
627     {
628       char * interval_string = GETENV("GC_LARGE_ALLOC_WARN_INTERVAL");
629       if (0 != interval_string) {
630         long interval = atol(interval_string);
631         if (interval <= 0) {
632           WARN("GC_LARGE_ALLOC_WARN_INTERVAL environment variable has "
633                "bad value: Ignoring\n", 0);
634         } else {
635           GC_large_alloc_warn_interval = interval;
636         }
637       }
638     }
639     {
640         char * space_divisor_string = GETENV("GC_FREE_SPACE_DIVISOR");
641         if (space_divisor_string != NULL) {
642           int space_divisor = atoi(space_divisor_string);
643           if (space_divisor > 0)
644             GC_free_space_divisor = (GC_word)space_divisor;
645         }
646     }
647     maybe_install_looping_handler();
648     /* Adjust normal object descriptor for extra allocation.    */
649     if (ALIGNMENT > GC_DS_TAGS && EXTRA_BYTES != 0) {
650       GC_obj_kinds[NORMAL].ok_descriptor = ((word)(-ALIGNMENT) | GC_DS_LENGTH);
651     }
652     GC_setpagesize();
653     GC_exclude_static_roots(beginGC_arrays, endGC_arrays);
654     GC_exclude_static_roots(beginGC_obj_kinds, endGC_obj_kinds);
655 #   ifdef SEPARATE_GLOBALS
656       GC_exclude_static_roots(beginGC_objfreelist, endGC_objfreelist);
657       GC_exclude_static_roots(beginGC_aobjfreelist, endGC_aobjfreelist);
658 #   endif
659 #   ifdef MSWIN32
660         GC_init_win32();
661 #   endif
662 #   if defined(USE_PROC_FOR_LIBRARIES) && defined(GC_LINUX_THREADS)
663         WARN("USE_PROC_FOR_LIBRARIES + GC_LINUX_THREADS performs poorly.\n", 0);
664         /* If thread stacks are cached, they tend to be scanned in      */
665         /* entirety as part of the root set.  This wil grow them to     */
666         /* maximum size, and is generally not desirable.                */
667 #   endif
668 #   if defined(SEARCH_FOR_DATA_START)
669         GC_init_linux_data_start();
670 #   endif
671 #   if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__)
672         GC_init_netbsd_elf();
673 #   endif
674 #   if !defined(THREADS) || defined(GC_PTHREADS) || defined(GC_WIN32_THREADS) \
675         || defined(GC_SOLARIS_THREADS)
676       if (GC_stackbottom == 0) {
677         GC_stackbottom = GC_get_main_stack_base();
678 #       if (defined(LINUX) || defined(HPUX)) && defined(IA64)
679           GC_register_stackbottom = GC_get_register_stack_base();
680 #       endif
681       } else {
682 #       if (defined(LINUX) || defined(HPUX)) && defined(IA64)
683           if (GC_register_stackbottom == 0) {
684             WARN("GC_register_stackbottom should be set with GC_stackbottom\n", 0);
685             /* The following may fail, since we may rely on             */
686             /* alignment properties that may not hold with a user set   */
687             /* GC_stackbottom.                                          */
688             GC_register_stackbottom = GC_get_register_stack_base();
689           }
690 #       endif
691       }
692 #   endif
693     GC_STATIC_ASSERT(sizeof (ptr_t) == sizeof(word));
694     GC_STATIC_ASSERT(sizeof (signed_word) == sizeof(word));
695     GC_STATIC_ASSERT(sizeof (struct hblk) == HBLKSIZE);
696 #   ifndef THREADS
697 #     ifdef STACK_GROWS_DOWN
698         GC_ASSERT((word)(&dummy) <= (word)GC_stackbottom);
699 #     else
700         GC_ASSERT((word)(&dummy) >= (word)GC_stackbottom);
701 #     endif
702 #   endif
703 #   if !defined(_AUX_SOURCE) || defined(__GNUC__)
704       GC_STATIC_ASSERT((word)(-1) > (word)0);
705       /* word should be unsigned */
706 #   endif
707 #   if !defined(__BORLANDC__) /* Workaround for Borland C */
708         GC_STATIC_ASSERT((ptr_t)(word)(-1) > (ptr_t)0);
709         /* Ptr_t comparisons should behave as unsigned comparisons.     */
710 #   endif
711     GC_STATIC_ASSERT((signed_word)(-1) < (signed_word)0);
712 #   if !defined(SMALL_CONFIG)
713       if (GC_incremental || 0 != GETENV("GC_ENABLE_INCREMENTAL")) {
714         /* This used to test for !GC_no_win32_dlls.  Why? */
715         GC_setpagesize();
716         /* For GWW_MPROTECT on Win32, this needs to happen before any   */
717         /* heap memory is allocated.                                    */
718         GC_dirty_init();
719         GC_ASSERT(GC_bytes_allocd == 0)
720         GC_incremental = TRUE;
721       }
722 #   endif /* !SMALL_CONFIG */
723     
724     /* Add initial guess of root sets.  Do this first, since sbrk(0)    */
725     /* might be used.                                                   */
726       if (GC_REGISTER_MAIN_STATIC_DATA()) GC_register_data_segments();
727     GC_init_headers();
728     GC_bl_init();
729     GC_mark_init();
730     {
731         char * sz_str = GETENV("GC_INITIAL_HEAP_SIZE");
732         if (sz_str != NULL) {
733           initial_heap_sz = (word)STRTOULL(sz_str, NULL, 10);
734           if (initial_heap_sz <= MINHINCR * HBLKSIZE) {
735             WARN("Bad initial heap size %s - ignoring it.\n",
736                  sz_str);
737           } 
738           initial_heap_sz = divHBLKSZ(initial_heap_sz);
739         }
740     }
741     {
742         char * sz_str = GETENV("GC_MAXIMUM_HEAP_SIZE");
743         if (sz_str != NULL) {
744           word max_heap_sz = (word)STRTOULL(sz_str, NULL, 10);
745           if (max_heap_sz < initial_heap_sz * HBLKSIZE) {
746             WARN("Bad maximum heap size %s - ignoring it.\n",
747                  sz_str);
748           } 
749           if (0 == GC_max_retries) GC_max_retries = 2;
750           GC_set_max_heap_size(max_heap_sz);
751         }
752     }
753     if (!GC_expand_hp_inner(initial_heap_sz)) {
754         GC_err_printf("Can't start up: not enough memory\n");
755         EXIT();
756     }
757     GC_initialize_offsets();
758     GC_register_displacement_inner(0L);
759 #   if defined(GC_LINUX_THREADS) && defined(REDIRECT_MALLOC)
760       if (!GC_all_interior_pointers) {
761         /* TLS ABI uses pointer-sized offsets for dtv. */
762         GC_register_displacement_inner(sizeof(void *));
763       }
764 #   endif
765     GC_init_size_map();
766 #   ifdef PCR
767       if (PCR_IL_Lock(PCR_Bool_false, PCR_allSigsBlocked, PCR_waitForever)
768           != PCR_ERes_okay) {
769           ABORT("Can't lock load state\n");
770       } else if (PCR_IL_Unlock() != PCR_ERes_okay) {
771           ABORT("Can't unlock load state\n");
772       }
773       PCR_IL_Unlock();
774       GC_pcr_install();
775 #   endif
776     GC_is_initialized = TRUE;
777 #   if defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
778         GC_thr_init();
779 #   endif
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 #   ifdef STUBBORN_ALLOC
784         GC_stubborn_init();
785 #   endif
786     /* Convince lint that some things are used */
787 #   ifdef LINT
788       {
789           extern char * GC_copyright[];
790           extern int GC_read();
791           extern void GC_register_finalizer_no_order();
792           
793           GC_noop(GC_copyright, GC_find_header,
794                   GC_push_one, GC_call_with_alloc_lock, GC_read,
795                   GC_dont_expand,
796 #                 ifndef NO_DEBUGGING
797                     GC_dump,
798 #                 endif
799                   GC_register_finalizer_no_order);
800       }
801 #   endif
802
803     /* The rest of this again assumes we don't really hold      */
804     /* the allocation lock.                                     */
805 #   if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
806         /* Make sure marker threads are started and thread local */
807         /* allocation is initialized, in case we didn't get      */
808         /* called from GC_init_parallel();                       */
809         {
810           GC_init_parallel();
811         }
812 #   endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
813
814 #   if defined(DYNAMIC_LOADING) && defined(DARWIN)
815     {
816         /* This must be called WITHOUT the allocation lock held
817         and before any threads are created */
818         extern void GC_init_dyld();
819         GC_init_dyld();
820     }
821 #   endif
822 }
823
824 GC_API void GC_CALL GC_enable_incremental(void)
825 {
826 # if !defined(SMALL_CONFIG) && !defined(KEEP_BACK_PTRS)
827   /* If we are keeping back pointers, the GC itself dirties all */
828   /* pages on which objects have been marked, making            */
829   /* incremental GC pointless.                                  */
830   if (!GC_find_leak && 0 == GETENV("GC_DISABLE_INCREMENTAL")) {
831     DCL_LOCK_STATE;
832     
833     LOCK();
834     if (GC_incremental) goto out;
835     GC_setpagesize();
836     /* if (GC_no_win32_dlls) goto out; Should be win32S test? */
837     maybe_install_looping_handler();  /* Before write fault handler! */
838     GC_incremental = TRUE;
839     if (!GC_is_initialized) {
840         GC_init_inner();
841     } else {
842         GC_dirty_init();
843     }
844     if (!GC_dirty_maintained) goto out;
845     if (GC_dont_gc) {
846         /* Can't easily do it. */
847         UNLOCK();
848         return;
849     }
850     if (GC_bytes_allocd > 0) {
851         /* There may be unmarked reachable objects      */
852         GC_gcollect_inner();
853     }   /* else we're OK in assuming everything's       */
854         /* clean since nothing can point to an          */
855         /* unmarked object.                             */
856     GC_read_dirty();
857 out:
858     UNLOCK();
859   } else {
860     GC_init();
861   }
862 # else
863     GC_init();
864 # endif
865 }
866
867
868 #if defined(MSWIN32) || defined(MSWINCE)
869 # if defined(_MSC_VER) && defined(_DEBUG)
870 #  include <crtdbg.h>
871 # endif
872 # ifdef OLD_WIN32_LOG_FILE
873 #   define LOG_FILE _T("gc.log")
874 # endif
875
876   STATIC HANDLE GC_stdout = 0;
877
878   void GC_deinit(void)
879   {
880 #   ifdef THREADS
881       if (GC_is_initialized) {
882         DeleteCriticalSection(&GC_write_cs);
883       }
884 #   endif
885   }
886
887 #ifdef THREADS
888 # ifdef PARALLEL_MARK
889 #   define IF_NEED_TO_LOCK(x) if (GC_parallel || GC_need_to_lock) x
890 # else
891 #   define IF_NEED_TO_LOCK(x) if (GC_need_to_lock) x
892 # endif
893 #else
894 # define IF_NEED_TO_LOCK(x)
895 #endif
896
897   int GC_write(const char *buf, size_t len)
898   {
899       BOOL tmp;
900       DWORD written;
901       if (len == 0)
902           return 0;
903       IF_NEED_TO_LOCK(EnterCriticalSection(&GC_write_cs));
904       if (GC_stdout == INVALID_HANDLE_VALUE) {
905           IF_NEED_TO_LOCK(LeaveCriticalSection(&GC_write_cs));
906           return -1;
907       } else if (GC_stdout == 0) {
908         char * file_name = GETENV("GC_LOG_FILE");
909         char logPath[_MAX_PATH + 5];
910
911         if (0 == file_name) {
912 #         ifdef OLD_WIN32_LOG_FILE
913             strcpy(logPath, LOG_FILE);
914 #         else
915             GetModuleFileNameA(NULL, logPath, _MAX_PATH);
916             strcat(logPath, ".log");
917 #         endif
918           file_name = logPath;
919         }
920         GC_stdout = CreateFileA(file_name, GENERIC_WRITE,
921                                FILE_SHARE_READ,
922                                NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH,
923                                NULL); 
924         if (GC_stdout == INVALID_HANDLE_VALUE)
925             ABORT("Open of log file failed");
926       }
927       tmp = WriteFile(GC_stdout, buf, (DWORD)len, &written, NULL);
928       if (!tmp)
929           DebugBreak();
930 #     if defined(_MSC_VER) && defined(_DEBUG)
931           _CrtDbgReport(_CRT_WARN, NULL, 0, NULL, "%.*s", len, buf);
932 #     endif
933       IF_NEED_TO_LOCK(LeaveCriticalSection(&GC_write_cs));
934       return tmp ? (int)written : -1;
935   }
936
937 #endif
938
939 #if defined(OS2) || defined(MACOS)
940 STATIC FILE * GC_stdout = NULL;
941 STATIC FILE * GC_stderr = NULL;
942 STATIC FILE * GC_log = NULL;
943 STATIC int GC_tmp;  /* Should really be local ... */
944
945   STATIC void GC_set_files(void)
946   {
947       if (GC_stdout == NULL) {
948         GC_stdout = stdout;
949     }
950     if (GC_stderr == NULL) {
951         GC_stderr = stderr;
952     }
953     if (GC_log == NULL) {
954         GC_log = stderr;
955     }
956   }
957 #endif
958
959 #if !defined(OS2) && !defined(MACOS) && !defined(MSWIN32) && !defined(MSWINCE)
960   STATIC int GC_stdout = 1;
961   STATIC int GC_stderr = 2;
962   int GC_log = 2;
963 # if !defined(AMIGA)
964 #   include <unistd.h>
965 # endif
966 #endif
967
968 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(OS2) \
969     && !defined(MACOS)  && !defined(ECOS) && !defined(NOSYS)
970 int GC_write(int fd, const char *buf, size_t len)
971 {
972      register int bytes_written = 0;
973      register int result;
974      
975      while (bytes_written < len) {
976 #       ifdef GC_SOLARIS_THREADS
977             result = syscall(SYS_write, fd, buf + bytes_written,
978                                             len - bytes_written);
979 #       else
980             result = write(fd, buf + bytes_written, len - bytes_written);
981 #       endif
982         if (-1 == result) return(result);
983         bytes_written += result;
984     }
985     return(bytes_written);
986 }
987 #endif /* UN*X */
988
989 #ifdef ECOS
990 int GC_write(int fd, const char *buf, size_t len)
991 {
992   _Jv_diag_write (buf, len);
993   return len;
994 }
995 #endif
996
997 #ifdef NOSYS
998 int GC_write(int fd, const char *buf, size_t len)
999 {
1000   /* No writing.  */
1001   return len;
1002 }
1003 #endif
1004
1005
1006 #if defined(MSWIN32) || defined(MSWINCE)
1007     /* FIXME: This is pretty ugly ... */
1008 #   define WRITE(f, buf, len) GC_write(buf, len)
1009 #else
1010 #   if defined(OS2) || defined(MACOS)
1011 #   define WRITE(f, buf, len) (GC_set_files(), \
1012                                GC_tmp = fwrite((buf), 1, (len), (f)), \
1013                                fflush(f), GC_tmp)
1014 #   else
1015 #     define WRITE(f, buf, len) GC_write((f), (buf), (len))
1016 #   endif
1017 #endif
1018
1019 #define BUFSZ 1024
1020 #ifdef _MSC_VER
1021 # define vsnprintf _vsnprintf
1022 #endif
1023 /* A version of printf that is unlikely to call malloc, and is thus safer */
1024 /* to call from the collector in case malloc has been bound to GC_malloc. */
1025 /* Floating point arguments and formats should be avoided, since fp       */
1026 /* conversion is more likely to allocate.                                 */
1027 /* Assumes that no more than BUFSZ-1 characters are written at once.      */
1028 void GC_printf(const char *format, ...)
1029 {
1030     va_list args;
1031     char buf[BUFSZ+1];
1032     
1033     va_start(args, format);
1034     if (GC_quiet) return;
1035     buf[BUFSZ] = 0x15;
1036     (void) vsnprintf(buf, BUFSZ, format, args);
1037     va_end(args);
1038     if (buf[BUFSZ] != 0x15) ABORT("GC_printf clobbered stack");
1039     if (WRITE(GC_stdout, buf, strlen(buf)) < 0) ABORT("write to stdout failed");
1040 }
1041
1042 void GC_err_printf(const char *format, ...)
1043 {
1044     va_list args;
1045     char buf[BUFSZ+1];
1046     
1047     va_start(args, format);
1048     buf[BUFSZ] = 0x15;
1049     (void) vsnprintf(buf, BUFSZ, format, args);
1050     va_end(args);
1051     if (buf[BUFSZ] != 0x15) ABORT("GC_printf clobbered stack");
1052     if (WRITE(GC_stderr, buf, strlen(buf)) < 0) ABORT("write to stderr failed");
1053 }
1054
1055 void GC_log_printf(const char *format, ...)
1056 {
1057     va_list args;
1058     char buf[BUFSZ+1];
1059     
1060     va_start(args, format);
1061     buf[BUFSZ] = 0x15;
1062     (void) vsnprintf(buf, BUFSZ, format, args);
1063     va_end(args);
1064     if (buf[BUFSZ] != 0x15) ABORT("GC_printf clobbered stack");
1065     if (WRITE(GC_log, buf, strlen(buf)) < 0) ABORT("write to log failed");
1066 }
1067
1068 void GC_err_puts(const char *s)
1069 {
1070     if (WRITE(GC_stderr, s, strlen(s)) < 0) ABORT("write to stderr failed");
1071 }
1072
1073 #if defined(LINUX) && !defined(SMALL_CONFIG)
1074 void GC_err_write(const char *buf, size_t len)
1075 {
1076     if (WRITE(GC_stderr, buf, len) < 0) ABORT("write to stderr failed");
1077 }
1078 #endif
1079
1080 STATIC void GC_CALLBACK GC_default_warn_proc(char *msg, GC_word arg)
1081 {
1082     GC_err_printf(msg, arg);
1083 }
1084
1085 GC_warn_proc GC_current_warn_proc = GC_default_warn_proc;
1086
1087 /* This is recommended for production code (release). */
1088 GC_API void GC_CALLBACK GC_ignore_warn_proc(char *msg, GC_word arg)
1089 {
1090     if (GC_print_stats) {
1091       /* Don't ignore warnings if stats printing is on. */
1092       GC_default_warn_proc(msg, arg);
1093     }
1094 }
1095
1096 GC_API void GC_CALL GC_set_warn_proc(GC_warn_proc p)
1097 {
1098     GC_ASSERT(p != 0);
1099 #   ifdef GC_WIN32_THREADS
1100 #     ifdef CYGWIN32
1101         /* Need explicit GC_INIT call */
1102         GC_ASSERT(GC_is_initialized);
1103 #     else
1104         if (!GC_is_initialized) GC_init();
1105 #     endif
1106 #   endif
1107     LOCK();
1108     GC_current_warn_proc = p;
1109     UNLOCK();
1110 }
1111
1112 GC_API GC_warn_proc GC_CALL GC_get_warn_proc(void)
1113 {
1114     GC_warn_proc result;
1115     LOCK();
1116     result = GC_current_warn_proc;
1117     UNLOCK();
1118     return(result);
1119 }
1120
1121 #if !defined(PCR) && !defined(SMALL_CONFIG)
1122 void GC_abort(const char *msg)
1123 {
1124 #   if defined(MSWIN32) && !defined(DONT_USE_USER32_DLL)
1125       (void) MessageBoxA(NULL, msg, "Fatal error in gc", MB_ICONERROR|MB_OK);
1126 #   else
1127       /* Avoid calling GC_err_printf() here, as GC_abort() could be     */
1128       /* called from it.  Note 1: this is not an atomic output.         */
1129       /* Note 2: possible write errors are ignored.                     */
1130       if (WRITE(GC_stderr, (void *)msg, strlen(msg)) >= 0)
1131         (void)WRITE(GC_stderr, (void *)("\n"), 1);
1132 #   endif
1133     if (GETENV("GC_LOOP_ON_ABORT") != NULL) {
1134             /* In many cases it's easier to debug a running process.    */
1135             /* It's arguably nicer to sleep, but that makes it harder   */
1136             /* to look at the thread if the debugger doesn't know much  */
1137             /* about threads.                                           */
1138             for(;;) {}
1139     }
1140 #   if defined(MSWIN32) || defined(MSWINCE)
1141         DebugBreak();
1142 #   else
1143         (void) abort();
1144 #   endif
1145 }
1146 #endif
1147
1148 GC_API void GC_CALL GC_enable(void)
1149 {
1150     LOCK();
1151     GC_dont_gc--;
1152     UNLOCK();
1153 }
1154
1155 GC_API void GC_CALL GC_disable(void)
1156 {
1157     LOCK();
1158     GC_dont_gc++;
1159     UNLOCK();
1160 }
1161
1162 /* Helper procedures for new kind creation.     */
1163 void ** GC_new_free_list_inner(void)
1164 {
1165     void *result = GC_INTERNAL_MALLOC((MAXOBJGRANULES+1)*sizeof(ptr_t),
1166                                       PTRFREE);
1167     if (result == 0) ABORT("Failed to allocate freelist for new kind");
1168     BZERO(result, (MAXOBJGRANULES+1)*sizeof(ptr_t));
1169     return result;
1170 }
1171
1172 void ** GC_new_free_list(void)
1173 {
1174     void *result;
1175     LOCK();
1176     result = GC_new_free_list_inner();
1177     UNLOCK();
1178     return result;
1179 }
1180
1181 unsigned GC_new_kind_inner(void **fl, GC_word descr, int adjust, int clear)
1182 {
1183     unsigned result = GC_n_kinds++;
1184
1185     if (GC_n_kinds > MAXOBJKINDS) ABORT("Too many kinds");
1186     GC_obj_kinds[result].ok_freelist = fl;
1187     GC_obj_kinds[result].ok_reclaim_list = 0;
1188     GC_obj_kinds[result].ok_descriptor = descr;
1189     GC_obj_kinds[result].ok_relocate_descr = adjust;
1190     GC_obj_kinds[result].ok_init = clear;
1191     return result;
1192 }
1193
1194 unsigned GC_new_kind(void **fl, GC_word descr, int adjust, int clear)
1195 {
1196     unsigned result;
1197     LOCK();
1198     result = GC_new_kind_inner(fl, descr, adjust, clear);
1199     UNLOCK();
1200     return result;
1201 }
1202
1203 unsigned GC_new_proc_inner(GC_mark_proc proc)
1204 {
1205     unsigned result = GC_n_mark_procs++;
1206
1207     if (GC_n_mark_procs > MAX_MARK_PROCS) ABORT("Too many mark procedures");
1208     GC_mark_procs[result] = proc;
1209     return result;
1210 }
1211
1212 unsigned GC_new_proc(GC_mark_proc proc)
1213 {
1214     unsigned result;
1215     LOCK();
1216     result = GC_new_proc_inner(proc);
1217     UNLOCK();
1218     return result;
1219 }
1220
1221 GC_API void * GC_CALL GC_call_with_stack_base(GC_stack_base_func fn, void *arg)
1222 {
1223     int dummy;
1224     struct GC_stack_base base;
1225
1226     base.mem_base = (void *)&dummy;
1227 #   ifdef IA64
1228       base.reg_base = (void *)GC_save_regs_in_stack();
1229       /* Unnecessarily flushes register stack,          */
1230       /* but that probably doesn't hurt.                */
1231 #   endif
1232     return fn(&base, arg);
1233 }
1234
1235 #if !defined(NO_DEBUGGING)
1236
1237 GC_API void GC_CALL GC_dump(void)
1238 {
1239     GC_printf("***Static roots:\n");
1240     GC_print_static_roots();
1241     GC_printf("\n***Heap sections:\n");
1242     GC_print_heap_sects();
1243     GC_printf("\n***Free blocks:\n");
1244     GC_print_hblkfreelist();
1245     GC_printf("\n***Blocks in use:\n");
1246     GC_print_block_list();
1247     GC_printf("\n***Finalization statistics:\n");
1248     GC_print_finalization_stats();
1249 }
1250
1251 #endif /* NO_DEBUGGING */
1252
1253 GC_API GC_word GC_CALL GC_get_gc_no(void)
1254 {
1255     return GC_gc_no;
1256 }
1257
1258 GC_API int GC_CALL GC_get_parallel(void)
1259 {
1260     return GC_parallel;
1261 }
1262
1263 /* Setter and getter functions for the public R/W variables.    */
1264
1265 GC_API void GC_CALL GC_set_oom_fn(GC_oom_func fn)
1266 {
1267     GC_ASSERT(fn != 0);
1268     GC_oom_fn = fn;
1269 }
1270
1271 GC_API GC_oom_func GC_CALL GC_get_oom_fn(void)
1272 {
1273     return GC_oom_fn;
1274 }
1275
1276 GC_API void GC_CALL GC_set_finalizer_notifier(GC_finalizer_notifier_proc fn)
1277 {
1278     GC_finalizer_notifier = fn;
1279 }
1280
1281 GC_API GC_finalizer_notifier_proc GC_CALL GC_get_finalizer_notifier(void)
1282 {
1283     return GC_finalizer_notifier;
1284 }
1285
1286 GC_API void GC_CALL GC_set_find_leak(int value)
1287 {
1288     /* value is of boolean type. */
1289     GC_find_leak = value;
1290 }
1291
1292 GC_API int GC_CALL GC_get_find_leak(void)
1293 {
1294     return GC_find_leak;
1295 }
1296
1297 GC_API void GC_CALL GC_set_all_interior_pointers(int value)
1298 {
1299     GC_ASSERT(!GC_is_initialized || value == GC_all_interior_pointers);
1300     GC_ASSERT(value == 0 || value == 1);
1301     GC_all_interior_pointers = value;
1302 }
1303
1304 GC_API int GC_CALL GC_get_all_interior_pointers(void)
1305 {
1306     return GC_all_interior_pointers;
1307 }
1308
1309 GC_API void GC_CALL GC_set_finalize_on_demand(int value)
1310 {
1311     GC_ASSERT(value != -1);
1312     /* value is of boolean type. */
1313     GC_finalize_on_demand = value;
1314 }
1315
1316 GC_API int GC_CALL GC_get_finalize_on_demand(void)
1317 {
1318     return GC_finalize_on_demand;
1319 }
1320
1321 GC_API void GC_CALL GC_set_java_finalization(int value)
1322 {
1323     GC_ASSERT(value != -1);
1324     /* value is of boolean type. */
1325     GC_java_finalization = value;
1326 }
1327
1328 GC_API int GC_CALL GC_get_java_finalization(void)
1329 {
1330     return GC_java_finalization;
1331 }
1332
1333 GC_API void GC_CALL GC_set_dont_expand(int value)
1334 {
1335     GC_ASSERT(value != -1);
1336     /* value is of boolean type. */
1337     GC_dont_expand = value;
1338 }
1339
1340 GC_API int GC_CALL GC_get_dont_expand(void)
1341 {
1342     return GC_dont_expand;
1343 }
1344
1345 GC_API void GC_CALL GC_set_no_dls(int value)
1346 {
1347     GC_ASSERT(value != -1);
1348     /* value is of boolean type. */
1349     GC_no_dls = value;
1350 }
1351
1352 GC_API int GC_CALL GC_get_no_dls(void)
1353 {
1354     return GC_no_dls;
1355 }
1356
1357 GC_API void GC_CALL GC_set_non_gc_bytes(GC_word value)
1358 {
1359     GC_non_gc_bytes = value;
1360 }
1361
1362 GC_API GC_word GC_CALL GC_get_non_gc_bytes(void)
1363 {
1364     return GC_non_gc_bytes;
1365 }
1366
1367 GC_API void GC_CALL GC_set_free_space_divisor(GC_word value)
1368 {
1369     GC_ASSERT(value > 0);
1370     GC_free_space_divisor = value;
1371 }
1372
1373 GC_API GC_word GC_CALL GC_get_free_space_divisor(void)
1374 {
1375     return GC_free_space_divisor;
1376 }
1377
1378 GC_API void GC_CALL GC_set_max_retries(GC_word value)
1379 {
1380     GC_ASSERT(value != ~(GC_word)0);
1381     GC_max_retries = value;
1382 }
1383
1384 GC_API GC_word GC_CALL GC_get_max_retries(void)
1385 {
1386     return GC_max_retries;
1387 }
1388
1389 GC_API void GC_CALL GC_set_dont_precollect(int value)
1390 {
1391     GC_ASSERT(value != -1);
1392     /* value is of boolean type. */
1393     GC_dont_precollect = value;
1394 }
1395
1396 GC_API int GC_CALL GC_get_dont_precollect(void)
1397 {
1398     return GC_dont_precollect;
1399 }
1400
1401 GC_API void GC_CALL GC_set_full_freq(int value)
1402 {
1403     GC_ASSERT(value >= 0);
1404     GC_full_freq = value;
1405 }
1406
1407 GC_API int GC_CALL GC_get_full_freq(void)
1408 {
1409     return GC_full_freq;
1410 }
1411
1412 GC_API void GC_CALL GC_set_time_limit(unsigned long value)
1413 {
1414     GC_ASSERT(value != (unsigned long)-1L);
1415     GC_time_limit = value;
1416 }
1417
1418 GC_API unsigned long GC_CALL GC_get_time_limit(void)
1419 {
1420     return GC_time_limit;
1421 }