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.
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
15 /* Boehm, July 31, 1995 5:02 pm PDT */
25 #define I_HIDE_POINTERS /* To make GC_call_with_alloc_lock visible */
26 #include "private/gc_pmark.h"
28 #ifdef GC_SOLARIS_THREADS
29 # include <sys/syscall.h>
31 #if defined(MSWIN32) || defined(MSWINCE)
32 # define WIN32_LEAN_AND_MEAN
38 #if defined(UNIX_LIKE) || defined(CYGWIN32)
40 # include <sys/types.h>
41 # include <sys/stat.h>
43 int GC_log; /* Forward decl, so we can set it. */
50 #if defined(THREADS) && defined(PCR)
51 # include "il/PCR_IL.h"
52 PCR_Th_ML GC_allocate_ml;
54 /* For other platforms with threads, the lock and possibly */
55 /* GC_lock_holder variables are defined in the thread support code. */
57 #if defined(NOSYS) || defined(ECOS)
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()
66 # define GC_REGISTER_MAIN_STATIC_DATA() TRUE
69 GC_FAR struct _GC_arrays GC_arrays /* = { 0 } */;
72 GC_bool GC_debugging_started = FALSE;
73 /* defined here so we don't have to load debug_malloc.o */
75 void (*GC_check_heap) (void) = (void (*) (void))0;
76 void (*GC_print_all_smashed) (void) = (void (*) (void))0;
78 void (*GC_start_call_back) (void) = (void (*) (void))0;
80 ptr_t GC_stackbottom = 0;
83 ptr_t GC_register_stackbottom = 0;
86 GC_bool GC_dont_gc = 0;
88 GC_bool GC_dont_precollect = 0;
93 GC_bool GC_print_stats = 0;
96 GC_bool GC_print_back_height = 0;
99 GC_bool GC_dump_regularly = 0; /* Generate regular debugging dumps. */
102 #ifdef KEEP_BACK_PTRS
103 long GC_backtraces = 0; /* Number of random backtraces to */
104 /* generate for each GC. */
108 int GC_find_leak = 1;
110 int GC_find_leak = 0;
113 #ifdef ALL_INTERIOR_POINTERS
114 int GC_all_interior_pointers = 1;
116 int GC_all_interior_pointers = 0;
119 long GC_large_alloc_warn_interval = 5;
120 /* Interval between unsuppressed warnings. */
122 long GC_large_alloc_warn_suppressed = 0;
123 /* Number of warnings suppressed so far. */
126 STATIC void * GC_CALLBACK GC_default_oom_fn(size_t bytes_requested)
131 GC_oom_func GC_oom_fn = GC_default_oom_fn;
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)
142 /* Map size 0 to something bigger. */
143 /* This avoids problems at lower levels. */
145 for (i = 1; i <= GRANULES_TO_BYTES(TINY_FREELISTS-1) - EXTRA_BYTES; i++) {
146 GC_size_map[i] = ROUNDED_UP_GRANULES(i);
148 GC_ASSERT(GC_size_map[i] < TINY_FREELISTS);
149 /* Seems to tickle bug in VC++ 2008 for AMD64 */
152 /* We leave the rest of the array to be filled in on demand. */
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)
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 */
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++;
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;
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. */
188 if (granule_sz > MAXOBJGRANULES) {
189 granule_sz = MAXOBJGRANULES;
191 /* If we can fit the same number of larger objects in a block, */
194 size_t number_of_objs = HBLK_GRANULES/granule_sz;
195 granule_sz = HBLK_GRANULES/number_of_objs;
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;
203 for (j = low_limit; j <= byte_sz; j++) GC_size_map[j] = granule_sz;
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.
216 # define BIG_CLEAR_SIZE 2048 /* Clear this much now and then. */
217 # define SMALL_CLEAR_SIZE 256 /* Clear this much every time. */
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
229 # define CLEAR_SIZE 213 /* Granularity for GC_clear_stack_inner */
231 #if defined(ASM_CLEAR_CODE)
232 extern void *GC_clear_stack_inner(void *, ptr_t);
234 /* Clear the stack up to about limit. Return arg. */
236 void * GC_clear_stack_inner(void *arg, ptr_t limit)
238 word dummy[CLEAR_SIZE];
240 BZERO(dummy, CLEAR_SIZE*sizeof(word));
241 if ((ptr_t)(dummy) COOLER_THAN limit) {
242 (void) GC_clear_stack_inner(arg, limit);
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);
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 */
254 void * GC_clear_stack(void *arg)
256 ptr_t sp = GC_approx_sp(); /* Hotter than actual sp */
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 */
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 */
282 if (++random_no % 13 == 0) {
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);
290 BZERO(dummy, SMALL_CLEAR_SIZE*sizeof(word));
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;
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) {
306 MAKE_HOTTER(GC_high_water, GC_SLOP);
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. */
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. */
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;
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)
338 if (!GC_is_initialized) return 0;
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);
348 candidate_hdr = HDR(h);
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));
354 size_t offset = HBLKDISPL(r);
355 signed_word sz = candidate_hdr -> hb_sz;
356 size_t obj_displ = offset % sz;
360 if (limit > (ptr_t)(h + 1) && sz <= HBLKSIZE) {
363 if ((ptr_t)p >= limit) return(0);
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)
376 return hhdr -> hb_sz;
379 GC_API size_t GC_CALL GC_get_heap_size(void)
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);
391 GC_API size_t GC_CALL GC_get_free_bytes(void)
396 /* ignore the memory space returned to OS */
397 value = (size_t)(GC_large_free_bytes - GC_unmapped_bytes);
402 GC_API size_t GC_CALL GC_get_unmapped_bytes(void)
408 value = (size_t)GC_unmapped_bytes;
416 GC_API size_t GC_CALL GC_get_bytes_since_gc(void)
421 value = GC_bytes_allocd;
426 GC_API size_t GC_CALL GC_get_total_bytes(void)
431 value = GC_bytes_allocd+GC_bytes_allocd_before_gc;
436 GC_bool GC_is_initialized = FALSE;
438 # if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
439 extern void GC_init_parallel(void);
440 # endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
442 /* FIXME: The GC_init/GC_init_inner distinction should go away. */
443 GC_API void GC_CALL GC_init(void)
445 /* LOCK(); -- no longer does anything this early. */
450 #if (defined(MSWIN32) || defined(MSWINCE)) && defined(THREADS)
451 CRITICAL_SECTION GC_write_cs;
455 extern void GC_init_win32(void);
458 extern void GC_setpagesize(void);
460 STATIC void GC_exit_check(void)
465 #ifdef SEARCH_FOR_DATA_START
466 extern void GC_init_linux_data_start(void);
471 extern void GC_set_and_save_fault_handler(void (*handler)(int));
473 static void looping_handler(int sig)
475 GC_err_printf("Caught signal %d: looping in handler\n", sig);
479 static GC_bool installed_looping_handler = FALSE;
481 static void maybe_install_looping_handler(void)
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;
491 #else /* !UNIX_LIKE */
493 # define maybe_install_looping_handler()
497 #if defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
498 void GC_thr_init(void);
501 void GC_init_inner(void)
503 # if !defined(THREADS) && defined(GC_ASSERTIONS)
507 # ifdef GC_INITIAL_HEAP_SIZE
508 word initial_heap_sz = divHBLKSZ(GC_INITIAL_HEAP_SIZE);
510 word initial_heap_sz = (word)MINHINCR;
513 if (GC_is_initialized) return;
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. */
522 GC_ASSERT(!GC_need_to_lock);
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");
529 pfn = (BOOL (WINAPI *) (LPCRITICAL_SECTION, DWORD))
530 GetProcAddress (hK32,
531 "InitializeCriticalSectionAndSpinCount");
533 pfn(&GC_allocate_ml, 4000);
535 InitializeCriticalSection (&GC_allocate_ml);
538 # if (defined(MSWIN32) || defined(MSWINCE)) && defined(THREADS)
539 InitializeCriticalSection(&GC_write_cs);
541 # if (!defined(SMALL_CONFIG))
542 if (0 != GETENV("GC_PRINT_STATS")) {
545 if (0 != GETENV("GC_PRINT_VERBOSE_STATS")) {
546 GC_print_stats = VERBOSE;
548 # if defined(UNIX_LIKE) || defined(CYGWIN32)
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);
554 GC_log_printf("Failed to open %s as log file\n", file_name);
562 # ifndef NO_DEBUGGING
563 if (0 != GETENV("GC_DUMP_REGULARLY")) {
564 GC_dump_regularly = 1;
567 # ifdef KEEP_BACK_PTRS
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;
576 if (0 != GETENV("GC_FIND_LEAK")) {
578 atexit(GC_exit_check);
580 if (0 != GETENV("GC_ALL_INTERIOR_POINTERS")) {
581 GC_all_interior_pointers = 1;
583 if (0 != GETENV("GC_DONT_GC")) {
586 if (0 != GETENV("GC_PRINT_BACK_HEIGHT")) {
587 GC_print_back_height = 1;
589 if (0 != GETENV("GC_NO_BLACKLIST_WARNING")) {
590 GC_large_alloc_warn_interval = LONG_MAX;
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);
598 word addr = (word)STRTOULL(addr_string, NULL, 16);
600 WARN("Unlikely trace address: %p\n", addr);
601 GC_trace_addr = (ptr_t)addr;
605 # ifndef SMALL_CONFIG
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);
614 GC_time_limit = time_limit;
619 char * full_freq_string = GETENV("GC_FULL_FREQUENCY");
620 if (full_freq_string != NULL) {
621 int full_freq = atoi(full_freq_string);
623 GC_full_freq = full_freq;
628 char * interval_string = GETENV("GC_LARGE_ALLOC_WARN_INTERVAL");
629 if (0 != interval_string) {
630 long interval = atol(interval_string);
632 WARN("GC_LARGE_ALLOC_WARN_INTERVAL environment variable has "
633 "bad value: Ignoring\n", 0);
635 GC_large_alloc_warn_interval = interval;
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;
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);
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);
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. */
668 # if defined(SEARCH_FOR_DATA_START)
669 GC_init_linux_data_start();
671 # if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__)
672 GC_init_netbsd_elf();
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();
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();
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);
697 # ifdef STACK_GROWS_DOWN
698 GC_ASSERT((word)(&dummy) <= (word)GC_stackbottom);
700 GC_ASSERT((word)(&dummy) >= (word)GC_stackbottom);
703 # if !defined(_AUX_SOURCE) || defined(__GNUC__)
704 GC_STATIC_ASSERT((word)(-1) > (word)0);
705 /* word should be unsigned */
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. */
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? */
716 /* For GWW_MPROTECT on Win32, this needs to happen before any */
717 /* heap memory is allocated. */
719 GC_ASSERT(GC_bytes_allocd == 0)
720 GC_incremental = TRUE;
722 # endif /* !SMALL_CONFIG */
724 /* Add initial guess of root sets. Do this first, since sbrk(0) */
726 if (GC_REGISTER_MAIN_STATIC_DATA()) GC_register_data_segments();
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",
738 initial_heap_sz = divHBLKSZ(initial_heap_sz);
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",
749 if (0 == GC_max_retries) GC_max_retries = 2;
750 GC_set_max_heap_size(max_heap_sz);
753 if (!GC_expand_hp_inner(initial_heap_sz)) {
754 GC_err_printf("Can't start up: not enough memory\n");
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 *));
767 if (PCR_IL_Lock(PCR_Bool_false, PCR_allSigsBlocked, PCR_waitForever)
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");
776 GC_is_initialized = TRUE;
777 # if defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
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
786 /* Convince lint that some things are used */
789 extern char * GC_copyright[];
790 extern int GC_read();
791 extern void GC_register_finalizer_no_order();
793 GC_noop(GC_copyright, GC_find_header,
794 GC_push_one, GC_call_with_alloc_lock, GC_read,
796 # ifndef NO_DEBUGGING
799 GC_register_finalizer_no_order);
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(); */
812 # endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
814 # if defined(DYNAMIC_LOADING) && defined(DARWIN)
816 /* This must be called WITHOUT the allocation lock held
817 and before any threads are created */
818 extern void GC_init_dyld();
824 GC_API void GC_CALL GC_enable_incremental(void)
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")) {
834 if (GC_incremental) goto out;
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) {
844 if (!GC_dirty_maintained) goto out;
846 /* Can't easily do it. */
850 if (GC_bytes_allocd > 0) {
851 /* There may be unmarked reachable objects */
853 } /* else we're OK in assuming everything's */
854 /* clean since nothing can point to an */
855 /* unmarked object. */
868 #if defined(MSWIN32) || defined(MSWINCE)
869 # if defined(_MSC_VER) && defined(_DEBUG)
872 # ifdef OLD_WIN32_LOG_FILE
873 # define LOG_FILE _T("gc.log")
876 STATIC HANDLE GC_stdout = 0;
881 if (GC_is_initialized) {
882 DeleteCriticalSection(&GC_write_cs);
888 # ifdef PARALLEL_MARK
889 # define IF_NEED_TO_LOCK(x) if (GC_parallel || GC_need_to_lock) x
891 # define IF_NEED_TO_LOCK(x) if (GC_need_to_lock) x
894 # define IF_NEED_TO_LOCK(x)
897 int GC_write(const char *buf, size_t len)
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));
907 } else if (GC_stdout == 0) {
908 char * file_name = GETENV("GC_LOG_FILE");
909 char logPath[_MAX_PATH + 5];
911 if (0 == file_name) {
912 # ifdef OLD_WIN32_LOG_FILE
913 strcpy(logPath, LOG_FILE);
915 GetModuleFileNameA(NULL, logPath, _MAX_PATH);
916 strcat(logPath, ".log");
920 GC_stdout = CreateFileA(file_name, GENERIC_WRITE,
922 NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH,
924 if (GC_stdout == INVALID_HANDLE_VALUE)
925 ABORT("Open of log file failed");
927 tmp = WriteFile(GC_stdout, buf, (DWORD)len, &written, NULL);
930 # if defined(_MSC_VER) && defined(_DEBUG)
931 _CrtDbgReport(_CRT_WARN, NULL, 0, NULL, "%.*s", len, buf);
933 IF_NEED_TO_LOCK(LeaveCriticalSection(&GC_write_cs));
934 return tmp ? (int)written : -1;
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 ... */
945 STATIC void GC_set_files(void)
947 if (GC_stdout == NULL) {
950 if (GC_stderr == NULL) {
953 if (GC_log == NULL) {
959 #if !defined(OS2) && !defined(MACOS) && !defined(MSWIN32) && !defined(MSWINCE)
960 STATIC int GC_stdout = 1;
961 STATIC int GC_stderr = 2;
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)
972 register int bytes_written = 0;
975 while (bytes_written < len) {
976 # ifdef GC_SOLARIS_THREADS
977 result = syscall(SYS_write, fd, buf + bytes_written,
978 len - bytes_written);
980 result = write(fd, buf + bytes_written, len - bytes_written);
982 if (-1 == result) return(result);
983 bytes_written += result;
985 return(bytes_written);
990 int GC_write(int fd, const char *buf, size_t len)
992 _Jv_diag_write (buf, len);
998 int GC_write(int fd, const char *buf, size_t len)
1006 #if defined(MSWIN32) || defined(MSWINCE)
1007 /* FIXME: This is pretty ugly ... */
1008 # define WRITE(f, buf, len) GC_write(buf, len)
1010 # if defined(OS2) || defined(MACOS)
1011 # define WRITE(f, buf, len) (GC_set_files(), \
1012 GC_tmp = fwrite((buf), 1, (len), (f)), \
1015 # define WRITE(f, buf, len) GC_write((f), (buf), (len))
1021 # define vsnprintf _vsnprintf
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, ...)
1033 va_start(args, format);
1034 if (GC_quiet) return;
1036 (void) vsnprintf(buf, BUFSZ, format, 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");
1042 void GC_err_printf(const char *format, ...)
1047 va_start(args, format);
1049 (void) vsnprintf(buf, BUFSZ, format, 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");
1055 void GC_log_printf(const char *format, ...)
1060 va_start(args, format);
1062 (void) vsnprintf(buf, BUFSZ, format, 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");
1068 void GC_err_puts(const char *s)
1070 if (WRITE(GC_stderr, s, strlen(s)) < 0) ABORT("write to stderr failed");
1073 #if defined(LINUX) && !defined(SMALL_CONFIG)
1074 void GC_err_write(const char *buf, size_t len)
1076 if (WRITE(GC_stderr, buf, len) < 0) ABORT("write to stderr failed");
1080 STATIC void GC_CALLBACK GC_default_warn_proc(char *msg, GC_word arg)
1082 GC_err_printf(msg, arg);
1085 GC_warn_proc GC_current_warn_proc = GC_default_warn_proc;
1087 /* This is recommended for production code (release). */
1088 GC_API void GC_CALLBACK GC_ignore_warn_proc(char *msg, GC_word arg)
1090 if (GC_print_stats) {
1091 /* Don't ignore warnings if stats printing is on. */
1092 GC_default_warn_proc(msg, arg);
1096 GC_API void GC_CALL GC_set_warn_proc(GC_warn_proc p)
1099 # ifdef GC_WIN32_THREADS
1101 /* Need explicit GC_INIT call */
1102 GC_ASSERT(GC_is_initialized);
1104 if (!GC_is_initialized) GC_init();
1108 GC_current_warn_proc = p;
1112 GC_API GC_warn_proc GC_CALL GC_get_warn_proc(void)
1114 GC_warn_proc result;
1116 result = GC_current_warn_proc;
1121 #if !defined(PCR) && !defined(SMALL_CONFIG)
1122 void GC_abort(const char *msg)
1124 # if defined(MSWIN32) && !defined(DONT_USE_USER32_DLL)
1125 (void) MessageBoxA(NULL, msg, "Fatal error in gc", MB_ICONERROR|MB_OK);
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);
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. */
1140 # if defined(MSWIN32) || defined(MSWINCE)
1148 GC_API void GC_CALL GC_enable(void)
1155 GC_API void GC_CALL GC_disable(void)
1162 /* Helper procedures for new kind creation. */
1163 void ** GC_new_free_list_inner(void)
1165 void *result = GC_INTERNAL_MALLOC((MAXOBJGRANULES+1)*sizeof(ptr_t),
1167 if (result == 0) ABORT("Failed to allocate freelist for new kind");
1168 BZERO(result, (MAXOBJGRANULES+1)*sizeof(ptr_t));
1172 void ** GC_new_free_list(void)
1176 result = GC_new_free_list_inner();
1181 unsigned GC_new_kind_inner(void **fl, GC_word descr, int adjust, int clear)
1183 unsigned result = GC_n_kinds++;
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;
1194 unsigned GC_new_kind(void **fl, GC_word descr, int adjust, int clear)
1198 result = GC_new_kind_inner(fl, descr, adjust, clear);
1203 unsigned GC_new_proc_inner(GC_mark_proc proc)
1205 unsigned result = GC_n_mark_procs++;
1207 if (GC_n_mark_procs > MAX_MARK_PROCS) ABORT("Too many mark procedures");
1208 GC_mark_procs[result] = proc;
1212 unsigned GC_new_proc(GC_mark_proc proc)
1216 result = GC_new_proc_inner(proc);
1221 GC_API void * GC_CALL GC_call_with_stack_base(GC_stack_base_func fn, void *arg)
1224 struct GC_stack_base base;
1226 base.mem_base = (void *)&dummy;
1228 base.reg_base = (void *)GC_save_regs_in_stack();
1229 /* Unnecessarily flushes register stack, */
1230 /* but that probably doesn't hurt. */
1232 return fn(&base, arg);
1235 #if !defined(NO_DEBUGGING)
1237 GC_API void GC_CALL GC_dump(void)
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();
1251 #endif /* NO_DEBUGGING */
1253 GC_API GC_word GC_CALL GC_get_gc_no(void)
1258 GC_API int GC_CALL GC_get_parallel(void)
1263 /* Setter and getter functions for the public R/W variables. */
1265 GC_API void GC_CALL GC_set_oom_fn(GC_oom_func fn)
1271 GC_API GC_oom_func GC_CALL GC_get_oom_fn(void)
1276 GC_API void GC_CALL GC_set_finalizer_notifier(GC_finalizer_notifier_proc fn)
1278 GC_finalizer_notifier = fn;
1281 GC_API GC_finalizer_notifier_proc GC_CALL GC_get_finalizer_notifier(void)
1283 return GC_finalizer_notifier;
1286 GC_API void GC_CALL GC_set_find_leak(int value)
1288 /* value is of boolean type. */
1289 GC_find_leak = value;
1292 GC_API int GC_CALL GC_get_find_leak(void)
1294 return GC_find_leak;
1297 GC_API void GC_CALL GC_set_all_interior_pointers(int value)
1299 GC_ASSERT(!GC_is_initialized || value == GC_all_interior_pointers);
1300 GC_ASSERT(value == 0 || value == 1);
1301 GC_all_interior_pointers = value;
1304 GC_API int GC_CALL GC_get_all_interior_pointers(void)
1306 return GC_all_interior_pointers;
1309 GC_API void GC_CALL GC_set_finalize_on_demand(int value)
1311 GC_ASSERT(value != -1);
1312 /* value is of boolean type. */
1313 GC_finalize_on_demand = value;
1316 GC_API int GC_CALL GC_get_finalize_on_demand(void)
1318 return GC_finalize_on_demand;
1321 GC_API void GC_CALL GC_set_java_finalization(int value)
1323 GC_ASSERT(value != -1);
1324 /* value is of boolean type. */
1325 GC_java_finalization = value;
1328 GC_API int GC_CALL GC_get_java_finalization(void)
1330 return GC_java_finalization;
1333 GC_API void GC_CALL GC_set_dont_expand(int value)
1335 GC_ASSERT(value != -1);
1336 /* value is of boolean type. */
1337 GC_dont_expand = value;
1340 GC_API int GC_CALL GC_get_dont_expand(void)
1342 return GC_dont_expand;
1345 GC_API void GC_CALL GC_set_no_dls(int value)
1347 GC_ASSERT(value != -1);
1348 /* value is of boolean type. */
1352 GC_API int GC_CALL GC_get_no_dls(void)
1357 GC_API void GC_CALL GC_set_non_gc_bytes(GC_word value)
1359 GC_non_gc_bytes = value;
1362 GC_API GC_word GC_CALL GC_get_non_gc_bytes(void)
1364 return GC_non_gc_bytes;
1367 GC_API void GC_CALL GC_set_free_space_divisor(GC_word value)
1369 GC_ASSERT(value > 0);
1370 GC_free_space_divisor = value;
1373 GC_API GC_word GC_CALL GC_get_free_space_divisor(void)
1375 return GC_free_space_divisor;
1378 GC_API void GC_CALL GC_set_max_retries(GC_word value)
1380 GC_ASSERT(value != ~(GC_word)0);
1381 GC_max_retries = value;
1384 GC_API GC_word GC_CALL GC_get_max_retries(void)
1386 return GC_max_retries;
1389 GC_API void GC_CALL GC_set_dont_precollect(int value)
1391 GC_ASSERT(value != -1);
1392 /* value is of boolean type. */
1393 GC_dont_precollect = value;
1396 GC_API int GC_CALL GC_get_dont_precollect(void)
1398 return GC_dont_precollect;
1401 GC_API void GC_CALL GC_set_full_freq(int value)
1403 GC_ASSERT(value >= 0);
1404 GC_full_freq = value;
1407 GC_API int GC_CALL GC_get_full_freq(void)
1409 return GC_full_freq;
1412 GC_API void GC_CALL GC_set_time_limit(unsigned long value)
1414 GC_ASSERT(value != (unsigned long)-1L);
1415 GC_time_limit = value;
1418 GC_API unsigned long GC_CALL GC_get_time_limit(void)
1420 return GC_time_limit;