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.
16 #include "private/gc_pmark.h"
26 #ifdef GC_SOLARIS_THREADS
27 # include <sys/syscall.h>
30 #if defined(UNIX_LIKE) || defined(CYGWIN32) || defined(SYMBIAN) \
31 || (defined(CONSOLE_LOG) && defined(MSWIN32))
33 # include <sys/types.h>
34 # include <sys/stat.h>
43 # include "il/PCR_IL.h"
44 GC_INNER PCR_Th_ML GC_allocate_ml;
45 # elif defined(SN_TARGET_PSP2)
46 GC_INNER WapiMutex GC_allocate_ml_PSP2 = { 0, NULL };
47 # elif defined(SN_TARGET_ORBIS) || defined(SN_TARGET_PS3)
49 GC_INNER pthread_mutex_t GC_allocate_ml;
51 /* For other platforms with threads, the lock and possibly */
52 /* GC_lock_holder variables are defined in the thread support code. */
55 #ifdef DYNAMIC_LOADING
56 /* We need to register the main data segment. Returns TRUE unless */
57 /* this is done implicitly as part of dynamic library registration. */
58 # define GC_REGISTER_MAIN_STATIC_DATA() GC_register_main_static_data()
59 #elif defined(GC_DONT_REGISTER_MAIN_STATIC_DATA)
60 # define GC_REGISTER_MAIN_STATIC_DATA() FALSE
62 /* Don't unnecessarily call GC_register_main_static_data() in case */
63 /* dyn_load.c isn't linked in. */
64 # define GC_REGISTER_MAIN_STATIC_DATA() TRUE
67 #ifdef NEED_CANCEL_DISABLE_COUNT
68 __thread unsigned char GC_cancel_disable_count = 0;
71 GC_FAR struct _GC_arrays GC_arrays /* = { 0 } */;
73 GC_INNER GC_bool GC_debugging_started = FALSE;
74 /* defined here so we don't have to load dbg_mlc.o */
76 ptr_t GC_stackbottom = 0;
79 ptr_t GC_register_stackbottom = 0;
82 int GC_dont_gc = FALSE;
84 int GC_dont_precollect = FALSE;
86 GC_bool GC_quiet = 0; /* used also in pcr_interface.c */
88 #if !defined(NO_CLOCK) || !defined(SMALL_CONFIG)
89 int GC_print_stats = 0;
92 #ifdef GC_PRINT_BACK_HEIGHT
93 GC_INNER GC_bool GC_print_back_height = TRUE;
95 GC_INNER GC_bool GC_print_back_height = FALSE;
99 # ifdef GC_DUMP_REGULARLY
100 GC_INNER GC_bool GC_dump_regularly = TRUE;
101 /* Generate regular debugging dumps. */
103 GC_INNER GC_bool GC_dump_regularly = FALSE;
106 STATIC CLOCK_TYPE GC_init_time;
107 /* The time that the GC was initialized at. */
109 #endif /* !NO_DEBUGGING */
111 #ifdef KEEP_BACK_PTRS
112 GC_INNER long GC_backtraces = 0;
113 /* Number of random backtraces to generate for each GC. */
117 int GC_find_leak = 1;
119 int GC_find_leak = 0;
122 #ifndef SHORT_DBG_HDRS
123 # ifdef GC_FINDLEAK_DELAY_FREE
124 GC_INNER GC_bool GC_findleak_delay_free = TRUE;
126 GC_INNER GC_bool GC_findleak_delay_free = FALSE;
128 #endif /* !SHORT_DBG_HDRS */
130 #ifdef ALL_INTERIOR_POINTERS
131 int GC_all_interior_pointers = 1;
133 int GC_all_interior_pointers = 0;
136 #ifdef FINALIZE_ON_DEMAND
137 int GC_finalize_on_demand = 1;
139 int GC_finalize_on_demand = 0;
142 #ifdef JAVA_FINALIZATION
143 int GC_java_finalization = 1;
145 int GC_java_finalization = 0;
148 /* All accesses to it should be synchronized to avoid data races. */
149 GC_finalizer_notifier_proc GC_finalizer_notifier =
150 (GC_finalizer_notifier_proc)0;
152 #ifdef GC_FORCE_UNMAP_ON_GCOLLECT
153 /* Has no effect unless USE_MUNMAP. */
154 /* Has no effect on implicitly-initiated garbage collections. */
155 GC_INNER GC_bool GC_force_unmap_on_gcollect = TRUE;
157 GC_INNER GC_bool GC_force_unmap_on_gcollect = FALSE;
160 #ifndef GC_LARGE_ALLOC_WARN_INTERVAL
161 # define GC_LARGE_ALLOC_WARN_INTERVAL 5
163 GC_INNER long GC_large_alloc_warn_interval = GC_LARGE_ALLOC_WARN_INTERVAL;
164 /* Interval between unsuppressed warnings. */
166 STATIC void * GC_CALLBACK GC_default_oom_fn(
167 size_t bytes_requested GC_ATTR_UNUSED)
172 /* All accesses to it should be synchronized to avoid data races. */
173 GC_oom_func GC_oom_fn = GC_default_oom_fn;
175 #ifdef CAN_HANDLE_FORK
177 GC_INNER int GC_handle_fork = 1;
178 /* The value is examined by GC_thr_init. */
180 GC_INNER int GC_handle_fork = FALSE;
183 #elif !defined(HAVE_NO_FORK)
185 /* Same as above but with GC_CALL calling conventions. */
186 GC_API void GC_CALL GC_atfork_prepare(void)
189 ABORT("fork() handling unsupported");
193 GC_API void GC_CALL GC_atfork_parent(void)
198 GC_API void GC_CALL GC_atfork_child(void)
202 #endif /* !CAN_HANDLE_FORK && !HAVE_NO_FORK */
204 /* Overrides the default automatic handle-fork mode. Has effect only */
205 /* if called before GC_INIT. */
206 GC_API void GC_CALL GC_set_handle_fork(int value GC_ATTR_UNUSED)
208 # ifdef CAN_HANDLE_FORK
209 if (!GC_is_initialized)
210 GC_handle_fork = value >= -1 ? value : 1;
211 /* Map all negative values except for -1 to a positive one. */
212 # elif defined(THREADS) || (defined(DARWIN) && defined(MPROTECT_VDB))
213 if (!GC_is_initialized && value) {
214 # ifndef SMALL_CONFIG
215 GC_init(); /* to initialize GC_manual_vdb and GC_stderr */
221 ABORT("fork() handling unsupported");
224 /* No at-fork handler is needed in the single-threaded mode. */
228 /* Set things up so that GC_size_map[i] >= granules(i), */
229 /* but not too much bigger */
230 /* and so that size_map contains relatively few distinct entries */
231 /* This was originally stolen from Russ Atkinson's Cedar */
232 /* quantization algorithm (but we precompute it). */
233 STATIC void GC_init_size_map(void)
237 /* Map size 0 to something bigger. */
238 /* This avoids problems at lower levels. */
240 for (i = 1; i <= GRANULES_TO_BYTES(TINY_FREELISTS-1) - EXTRA_BYTES; i++) {
241 GC_size_map[i] = ROUNDED_UP_GRANULES(i);
243 GC_ASSERT(GC_size_map[i] < TINY_FREELISTS);
244 /* Seems to tickle bug in VC++ 2008 for AMD64 */
247 /* We leave the rest of the array to be filled in on demand. */
251 * The following is a gross hack to deal with a problem that can occur
252 * on machines that are sloppy about stack frame sizes, notably SPARC.
253 * Bogus pointers may be written to the stack and not cleared for
254 * a LONG time, because they always fall into holes in stack frames
255 * that are not written. We partially address this by clearing
256 * sections of the stack whenever we get control.
259 #ifndef SMALL_CLEAR_SIZE
260 # define SMALL_CLEAR_SIZE 256 /* Clear this much every time. */
263 #if defined(ALWAYS_SMALL_CLEAR_STACK) || defined(STACK_NOT_SCANNED)
264 GC_API void * GC_CALL GC_clear_stack(void *arg)
266 # ifndef STACK_NOT_SCANNED
267 word volatile dummy[SMALL_CLEAR_SIZE];
268 BZERO((/* no volatile */ void *)dummy, sizeof(dummy));
275 # define BIG_CLEAR_SIZE 2048 /* Clear this much now and then. */
277 STATIC word GC_stack_last_cleared = 0; /* GC_no when we last did this */
278 STATIC ptr_t GC_min_sp = NULL;
279 /* Coolest stack pointer value from which */
280 /* we've already cleared the stack. */
281 STATIC ptr_t GC_high_water = NULL;
282 /* "hottest" stack pointer value we have seen */
283 /* recently. Degrades over time. */
284 STATIC word GC_bytes_allocd_at_reset = 0;
285 # define DEGRADE_RATE 50
288 # if defined(ASM_CLEAR_CODE)
289 void *GC_clear_stack_inner(void *, ptr_t);
291 /* Clear the stack up to about limit. Return arg. This function */
292 /* is not static because it could also be erroneously defined in .S */
293 /* file, so this error would be caught by the linker. */
294 void *GC_clear_stack_inner(void *arg,
295 # if defined(__APPLE_CC__) && !GC_CLANG_PREREQ(6, 0)
296 volatile /* to workaround some bug */
300 # define CLEAR_SIZE 213 /* granularity */
301 volatile word dummy[CLEAR_SIZE];
303 BZERO((/* no volatile */ void *)dummy, sizeof(dummy));
304 if ((word)GC_approx_sp() COOLER_THAN (word)limit) {
305 (void)GC_clear_stack_inner(arg, limit);
307 /* Make sure the recursive call is not a tail call, and the bzero */
308 /* call is not recognized as dead code. */
309 # if defined(CPPCHECK)
312 GC_noop1(COVERT_DATAFLOW(dummy));
316 # endif /* !ASM_CLEAR_CODE */
319 /* Used to occasionally clear a bigger chunk. */
320 /* TODO: Should be more random than it is ... */
321 GC_ATTR_NO_SANITIZE_THREAD
322 static unsigned next_random_no(void)
324 static unsigned random_no = 0;
325 return ++random_no % 13;
327 # endif /* THREADS */
329 /* Clear some of the inaccessible part of the stack. Returns its */
330 /* argument, so it can be used in a tail call position, hence clearing */
332 GC_API void * GC_CALL GC_clear_stack(void *arg)
334 ptr_t sp = GC_approx_sp(); /* Hotter than actual sp */
336 word volatile dummy[SMALL_CLEAR_SIZE];
340 /* Extra bytes we clear every time. This clears our own */
341 /* activation record, and should cause more frequent */
342 /* clearing near the cold end of the stack, a good thing. */
343 # define GC_SLOP 4000
344 /* We make GC_high_water this much hotter than we really saw */
345 /* it, to cover for GC noise etc. above our current frame. */
346 # define CLEAR_THRESHOLD 100000
347 /* We restart the clearing process after this many bytes of */
348 /* allocation. Otherwise very heavily recursive programs */
349 /* with sparse stacks may result in heaps that grow almost */
350 /* without bounds. As the heap gets larger, collection */
351 /* frequency decreases, thus clearing frequency would decrease, */
352 /* thus more junk remains accessible, thus the heap gets */
355 if (next_random_no() == 0) {
358 MAKE_HOTTER(limit, BIG_CLEAR_SIZE*sizeof(word));
359 limit = (ptr_t)((word)limit & ~0xf);
360 /* Make it sufficiently aligned for assembly */
361 /* implementations of GC_clear_stack_inner. */
362 return GC_clear_stack_inner(arg, limit);
364 BZERO((void *)dummy, SMALL_CLEAR_SIZE*sizeof(word));
366 if (GC_gc_no > GC_stack_last_cleared) {
367 /* Start things over, so we clear the entire stack again */
368 if (GC_stack_last_cleared == 0)
369 GC_high_water = (ptr_t)GC_stackbottom;
370 GC_min_sp = GC_high_water;
371 GC_stack_last_cleared = GC_gc_no;
372 GC_bytes_allocd_at_reset = GC_bytes_allocd;
374 /* Adjust GC_high_water */
375 MAKE_COOLER(GC_high_water, WORDS_TO_BYTES(DEGRADE_RATE) + GC_SLOP);
376 if ((word)sp HOTTER_THAN (word)GC_high_water) {
379 MAKE_HOTTER(GC_high_water, GC_SLOP);
381 ptr_t limit = GC_min_sp;
383 MAKE_HOTTER(limit, SLOP);
384 if ((word)sp COOLER_THAN (word)limit) {
385 limit = (ptr_t)((word)limit & ~0xf);
386 /* Make it sufficiently aligned for assembly */
387 /* implementations of GC_clear_stack_inner. */
389 return GC_clear_stack_inner(arg, limit);
392 if (GC_bytes_allocd - GC_bytes_allocd_at_reset > CLEAR_THRESHOLD) {
393 /* Restart clearing process, but limit how much clearing we do. */
395 MAKE_HOTTER(GC_min_sp, CLEAR_THRESHOLD/4);
396 if ((word)GC_min_sp HOTTER_THAN (word)GC_high_water)
397 GC_min_sp = GC_high_water;
398 GC_bytes_allocd_at_reset = GC_bytes_allocd;
404 #endif /* !ALWAYS_SMALL_CLEAR_STACK && !STACK_NOT_SCANNED */
406 /* Return a pointer to the base address of p, given a pointer to a */
407 /* an address within an object. Return 0 o.w. */
408 GC_API void * GC_CALL GC_base(void * p)
416 if (!EXPECT(GC_is_initialized, TRUE)) return 0;
419 candidate_hdr = HDR_FROM_BI(bi, r);
420 if (candidate_hdr == 0) return(0);
421 /* If it's a pointer to the middle of a large object, move it */
422 /* to the beginning. */
423 while (IS_FORWARDING_ADDR_OR_NIL(candidate_hdr)) {
424 h = FORWARDED_ADDR(h,candidate_hdr);
426 candidate_hdr = HDR(h);
428 if (HBLK_IS_FREE(candidate_hdr)) return(0);
429 /* Make sure r points to the beginning of the object */
430 r = (ptr_t)((word)r & ~(WORDS_TO_BYTES(1) - 1));
432 size_t offset = HBLKDISPL(r);
433 word sz = candidate_hdr -> hb_sz;
434 size_t obj_displ = offset % sz;
439 if ((word)limit > (word)(h + 1) && sz <= HBLKSIZE) {
442 if ((word)p >= (word)limit) return(0);
447 /* Return TRUE if and only if p points to somewhere in GC heap. */
448 GC_API int GC_CALL GC_is_heap_ptr(const void *p)
452 GC_ASSERT(GC_is_initialized);
454 return HDR_FROM_BI(bi, p) != 0;
457 /* Return the size of an object, given a pointer to its base. */
458 /* (For small objects this also happens to work from interior pointers, */
459 /* but that shouldn't be relied upon.) */
460 GC_API size_t GC_CALL GC_size(const void * p)
464 return (size_t)hhdr->hb_sz;
468 /* These getters remain unsynchronized for compatibility (since some */
469 /* clients could call some of them from a GC callback holding the */
470 /* allocator lock). */
471 GC_API size_t GC_CALL GC_get_heap_size(void)
473 /* ignore the memory space returned to OS (i.e. count only the */
474 /* space owned by the garbage collector) */
475 return (size_t)(GC_heapsize - GC_unmapped_bytes);
478 GC_API size_t GC_CALL GC_get_free_bytes(void)
480 /* ignore the memory space returned to OS */
481 return (size_t)(GC_large_free_bytes - GC_unmapped_bytes);
484 GC_API size_t GC_CALL GC_get_unmapped_bytes(void)
486 return (size_t)GC_unmapped_bytes;
489 GC_API size_t GC_CALL GC_get_bytes_since_gc(void)
491 return (size_t)GC_bytes_allocd;
494 GC_API size_t GC_CALL GC_get_total_bytes(void)
496 return (size_t)(GC_bytes_allocd + GC_bytes_allocd_before_gc);
499 #ifndef GC_GET_HEAP_USAGE_NOT_NEEDED
501 GC_API size_t GC_CALL GC_get_size_map_at(int i)
503 if ((unsigned)i > MAXOBJBYTES)
505 return GRANULES_TO_BYTES(GC_size_map[i]);
508 /* Return the heap usage information. This is a thread-safe (atomic) */
509 /* alternative for the five above getters. NULL pointer is allowed for */
510 /* any argument. Returned (filled in) values are of word type. */
511 GC_API void GC_CALL GC_get_heap_usage_safe(GC_word *pheap_size,
512 GC_word *pfree_bytes, GC_word *punmapped_bytes,
513 GC_word *pbytes_since_gc, GC_word *ptotal_bytes)
518 if (pheap_size != NULL)
519 *pheap_size = GC_heapsize - GC_unmapped_bytes;
520 if (pfree_bytes != NULL)
521 *pfree_bytes = GC_large_free_bytes - GC_unmapped_bytes;
522 if (punmapped_bytes != NULL)
523 *punmapped_bytes = GC_unmapped_bytes;
524 if (pbytes_since_gc != NULL)
525 *pbytes_since_gc = GC_bytes_allocd;
526 if (ptotal_bytes != NULL)
527 *ptotal_bytes = GC_bytes_allocd + GC_bytes_allocd_before_gc;
531 GC_INNER word GC_reclaimed_bytes_before_gc = 0;
533 /* Fill in GC statistics provided the destination is of enough size. */
534 static void fill_prof_stats(struct GC_prof_stats_s *pstats)
536 pstats->heapsize_full = GC_heapsize;
537 pstats->free_bytes_full = GC_large_free_bytes;
538 pstats->unmapped_bytes = GC_unmapped_bytes;
539 pstats->bytes_allocd_since_gc = GC_bytes_allocd;
540 pstats->allocd_bytes_before_gc = GC_bytes_allocd_before_gc;
541 pstats->non_gc_bytes = GC_non_gc_bytes;
542 pstats->gc_no = GC_gc_no; /* could be -1 */
543 # ifdef PARALLEL_MARK
544 pstats->markers_m1 = (word)((signed_word)GC_markers_m1);
546 pstats->markers_m1 = 0; /* one marker */
548 pstats->bytes_reclaimed_since_gc = GC_bytes_found > 0 ?
549 (word)GC_bytes_found : 0;
550 pstats->reclaimed_bytes_before_gc = GC_reclaimed_bytes_before_gc;
551 pstats->expl_freed_bytes_since_gc = GC_bytes_freed; /* since gc-7.7 */
554 # include <string.h> /* for memset() */
556 GC_API size_t GC_CALL GC_get_prof_stats(struct GC_prof_stats_s *pstats,
559 struct GC_prof_stats_s stats;
563 fill_prof_stats(stats_sz >= sizeof(stats) ? pstats : &stats);
566 if (stats_sz == sizeof(stats)) {
567 return sizeof(stats);
568 } else if (stats_sz > sizeof(stats)) {
569 /* Fill in the remaining part with -1. */
570 memset((char *)pstats + sizeof(stats), 0xff, stats_sz - sizeof(stats));
571 return sizeof(stats);
573 if (EXPECT(stats_sz > 0, TRUE))
574 BCOPY(&stats, pstats, stats_sz);
580 /* The _unsafe version assumes the caller holds the allocation lock. */
581 GC_API size_t GC_CALL GC_get_prof_stats_unsafe(
582 struct GC_prof_stats_s *pstats,
585 struct GC_prof_stats_s stats;
587 if (stats_sz >= sizeof(stats)) {
588 fill_prof_stats(pstats);
589 if (stats_sz > sizeof(stats))
590 memset((char *)pstats + sizeof(stats), 0xff,
591 stats_sz - sizeof(stats));
592 return sizeof(stats);
594 if (EXPECT(stats_sz > 0, TRUE)) {
595 fill_prof_stats(&stats);
596 BCOPY(&stats, pstats, stats_sz);
601 # endif /* THREADS */
603 #endif /* !GC_GET_HEAP_USAGE_NOT_NEEDED */
605 #if defined(GC_DARWIN_THREADS) || defined(GC_OPENBSD_UTHREADS) \
606 || defined(GC_WIN32_THREADS) || (defined(NACL) && defined(THREADS))
607 /* GC does not use signals to suspend and restart threads. */
608 GC_API void GC_CALL GC_set_suspend_signal(int sig GC_ATTR_UNUSED)
613 GC_API void GC_CALL GC_set_thr_restart_signal(int sig GC_ATTR_UNUSED)
618 GC_API int GC_CALL GC_get_suspend_signal(void)
623 GC_API int GC_CALL GC_get_thr_restart_signal(void)
627 #endif /* GC_DARWIN_THREADS || GC_WIN32_THREADS || ... */
629 #if !defined(_MAX_PATH) && (defined(MSWIN32) || defined(MSWINCE) \
630 || defined(CYGWIN32))
631 # define _MAX_PATH MAX_PATH
634 #ifdef GC_READ_ENV_FILE
635 /* This works for Win32/WinCE for now. Really useful only for WinCE. */
636 STATIC char *GC_envfile_content = NULL;
637 /* The content of the GC "env" file with CR and */
638 /* LF replaced to '\0'. NULL if the file is */
639 /* missing or empty. Otherwise, always ends */
641 STATIC unsigned GC_envfile_length = 0;
642 /* Length of GC_envfile_content (if non-NULL). */
644 # ifndef GC_ENVFILE_MAXLEN
645 # define GC_ENVFILE_MAXLEN 0x4000
648 # define GC_ENV_FILE_EXT ".gc.env"
650 /* The routine initializes GC_envfile_content from the GC "env" file. */
651 STATIC void GC_envfile_init(void)
653 # if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
659 TCHAR path[_MAX_PATH + 0x10]; /* buffer for path + ext */
660 len = (unsigned)GetModuleFileName(NULL /* hModule */, path,
662 /* If GetModuleFileName() has failed then len is 0. */
663 if (len > 4 && path[len - 4] == (TCHAR)'.') {
664 len -= 4; /* strip executable file extension */
666 BCOPY(TEXT(GC_ENV_FILE_EXT), &path[len], sizeof(TEXT(GC_ENV_FILE_EXT)));
667 hFile = CreateFile(path, GENERIC_READ,
668 FILE_SHARE_READ | FILE_SHARE_WRITE,
669 NULL /* lpSecurityAttributes */, OPEN_EXISTING,
670 FILE_ATTRIBUTE_NORMAL, NULL /* hTemplateFile */);
671 if (hFile == INVALID_HANDLE_VALUE)
672 return; /* the file is absent or the operation is failed */
673 len = (unsigned)GetFileSize(hFile, NULL);
674 if (len <= 1 || len >= GC_ENVFILE_MAXLEN) {
676 return; /* invalid file length - ignoring the file content */
678 /* At this execution point, GC_setpagesize() and GC_init_win32() */
679 /* must already be called (for GET_MEM() to work correctly). */
680 content = (char *)GET_MEM(ROUNDUP_PAGESIZE_IF_MMAP((size_t)len + 1));
681 if (content == NULL) {
683 return; /* allocation failure */
686 nBytesRead = (DWORD)-1L;
687 /* Last ReadFile() call should clear nBytesRead on success. */
688 while (ReadFile(hFile, content + ofs, len - ofs + 1, &nBytesRead,
689 NULL /* lpOverlapped */) && nBytesRead != 0) {
690 if ((ofs += nBytesRead) > len)
694 if (ofs != len || nBytesRead != 0)
695 return; /* read operation is failed - ignoring the file content */
698 if (content[ofs] == '\r' || content[ofs] == '\n')
701 GC_ASSERT(NULL == GC_envfile_content);
702 GC_envfile_length = len + 1;
703 GC_envfile_content = content;
707 /* This routine scans GC_envfile_content for the specified */
708 /* environment variable (and returns its value if found). */
709 GC_INNER char * GC_envfile_getenv(const char *name)
712 char *end_of_content;
715 p = getenv(name); /* try the standard getenv() first */
717 return *p != '\0' ? p : NULL;
719 p = GC_envfile_content;
721 return NULL; /* "env" file is absent (or empty) */
722 namelen = strlen(name);
723 if (namelen == 0) /* a sanity check */
725 for (end_of_content = p + GC_envfile_length;
726 p != end_of_content; p += strlen(p) + 1) {
727 if (strncmp(p, name, namelen) == 0 && *(p += namelen) == '=') {
728 p++; /* the match is found; skip '=' */
729 return *p != '\0' ? p : NULL;
731 /* If not matching then skip to the next line. */
733 return NULL; /* no match found */
735 #endif /* GC_READ_ENV_FILE */
737 GC_INNER GC_bool GC_is_initialized = FALSE;
739 GC_API int GC_CALL GC_is_init_called(void)
741 return GC_is_initialized;
744 #if defined(GC_WIN32_THREADS) \
745 && ((defined(MSWIN32) && !defined(CONSOLE_LOG)) || defined(MSWINCE))
746 GC_INNER CRITICAL_SECTION GC_write_cs;
749 #ifndef DONT_USE_ATEXIT
750 # if !defined(PCR) && !defined(SMALL_CONFIG)
751 /* A dedicated variable to avoid a garbage collection on abort. */
752 /* GC_find_leak cannot be used for this purpose as otherwise */
753 /* TSan finds a data race (between GC_default_on_abort and, e.g., */
754 /* GC_finish_collection). */
755 static GC_bool skip_gc_atexit = FALSE;
757 # define skip_gc_atexit FALSE
760 STATIC void GC_exit_check(void)
762 if (GC_find_leak && !skip_gc_atexit) {
764 GC_in_thread_creation = TRUE; /* OK to collect from unknown thread. */
766 GC_in_thread_creation = FALSE;
774 #if defined(UNIX_LIKE) && !defined(NO_DEBUGGING)
775 static void looping_handler(int sig)
777 GC_err_printf("Caught signal %d: looping in handler\n", sig);
783 static GC_bool installed_looping_handler = FALSE;
785 static void maybe_install_looping_handler(void)
787 /* Install looping handler before the write fault handler, so we */
788 /* handle write faults correctly. */
789 if (!installed_looping_handler && 0 != GETENV("GC_LOOP_ON_ABORT")) {
790 GC_set_and_save_fault_handler(looping_handler);
791 installed_looping_handler = TRUE;
795 #else /* !UNIX_LIKE */
796 # define maybe_install_looping_handler()
799 #define GC_DEFAULT_STDOUT_FD 1
800 #define GC_DEFAULT_STDERR_FD 2
802 #if !defined(OS2) && !defined(MACOS) && !defined(GC_ANDROID_LOG) \
803 && !defined(NN_PLATFORM_CTR) && !defined(NINTENDO_SWITCH) \
804 && (!defined(MSWIN32) || defined(CONSOLE_LOG)) && !defined(MSWINCE)
805 STATIC int GC_stdout = GC_DEFAULT_STDOUT_FD;
806 STATIC int GC_stderr = GC_DEFAULT_STDERR_FD;
807 STATIC int GC_log = GC_DEFAULT_STDERR_FD;
810 GC_API void GC_CALL GC_set_log_fd(int fd)
817 #ifdef MSGBOX_ON_ERROR
818 STATIC void GC_win32_MessageBoxA(const char *msg, const char *caption,
821 # ifndef DONT_USE_USER32_DLL
822 /* Use static binding to "user32.dll". */
823 (void)MessageBoxA(NULL, msg, caption, flags);
825 /* This simplifies linking - resolve "MessageBoxA" at run-time. */
826 HINSTANCE hU32 = LoadLibrary(TEXT("user32.dll"));
828 FARPROC pfn = GetProcAddress(hU32, "MessageBoxA");
830 (void)(*(int (WINAPI *)(HWND, LPCSTR, LPCSTR, UINT))pfn)(
831 NULL /* hWnd */, msg, caption, flags);
832 (void)FreeLibrary(hU32);
836 #endif /* MSGBOX_ON_ERROR */
838 #if defined(THREADS) && defined(UNIX_LIKE) && !defined(NO_GETCONTEXT)
839 static void callee_saves_pushed_dummy_fn(ptr_t data GC_ATTR_UNUSED,
840 void * context GC_ATTR_UNUSED) {}
845 static GC_bool manual_vdb_allowed = TRUE;
847 static GC_bool manual_vdb_allowed = FALSE;
850 GC_API void GC_CALL GC_set_manual_vdb_allowed(int value)
852 manual_vdb_allowed = (GC_bool)value;
855 GC_API int GC_CALL GC_get_manual_vdb_allowed(void)
857 return (int)manual_vdb_allowed;
859 #endif /* !SMALL_CONFIG */
861 STATIC word GC_parse_mem_size_arg(const char *str)
863 word result = 0; /* bad value */
869 result = (word)STRTOULL(str, &endptr, 10);
872 if (*(endptr + 1) != '\0')
874 /* Allow k, M or G suffix. */
896 #define GC_LOG_STD_NAME "gc.log"
898 GC_API void GC_CALL GC_init(void)
900 /* LOCK(); -- no longer does anything this early. */
901 word initial_heap_sz;
902 IF_CANCEL(int cancel_state;)
903 # if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
907 if (EXPECT(GC_is_initialized, TRUE)) return;
908 # ifdef REDIRECT_MALLOC
910 static GC_bool init_started = FALSE;
912 ABORT("Redirected malloc() called during GC init");
917 # if defined(GC_INITIAL_HEAP_SIZE) && !defined(CPPCHECK)
918 initial_heap_sz = GC_INITIAL_HEAP_SIZE;
920 initial_heap_sz = MINHINCR * HBLKSIZE;
923 DISABLE_CANCEL(cancel_state);
924 /* Note that although we are nominally called with the */
925 /* allocation lock held, the allocation lock is now */
926 /* only really acquired once a second thread is forked.*/
927 /* And the initialization code needs to run before */
928 /* then. Thus we really don't hold any locks, and can */
929 /* in fact safely initialize them here. */
931 # ifndef GC_ALWAYS_MULTITHREADED
932 GC_ASSERT(!GC_need_to_lock);
934 # ifdef SN_TARGET_PS3
936 pthread_mutexattr_t mattr;
938 if (0 != pthread_mutexattr_init(&mattr)) {
939 ABORT("pthread_mutexattr_init failed");
941 if (0 != pthread_mutex_init(&GC_allocate_ml, &mattr)) {
942 ABORT("pthread_mutex_init failed");
944 (void)pthread_mutexattr_destroy(&mattr);
947 # endif /* THREADS */
948 # if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
950 # define SPIN_COUNT 4000
952 # ifdef MSWINRT_FLAVOR
953 InitializeCriticalSectionAndSpinCount(&GC_allocate_ml, SPIN_COUNT);
957 BOOL (WINAPI *pfn)(LPCRITICAL_SECTION, DWORD) = 0;
958 HMODULE hK32 = GetModuleHandle(TEXT("kernel32.dll"));
960 pfn = (BOOL (WINAPI *)(LPCRITICAL_SECTION, DWORD))
962 "InitializeCriticalSectionAndSpinCount");
964 pfn(&GC_allocate_ml, SPIN_COUNT);
966 # endif /* !MSWINCE */
967 /* else */ InitializeCriticalSection(&GC_allocate_ml);
970 # endif /* GC_WIN32_THREADS && !GC_PTHREADS */
971 # if defined(GC_WIN32_THREADS) \
972 && ((defined(MSWIN32) && !defined(CONSOLE_LOG)) || defined(MSWINCE))
973 InitializeCriticalSection(&GC_write_cs);
979 # ifdef GC_READ_ENV_FILE
982 # if !defined(NO_CLOCK) || !defined(SMALL_CONFIG)
983 # ifdef GC_PRINT_VERBOSE_STATS
984 /* This is useful for debugging and profiling on platforms with */
985 /* missing getenv() (like WinCE). */
986 GC_print_stats = VERBOSE;
988 if (0 != GETENV("GC_PRINT_VERBOSE_STATS")) {
989 GC_print_stats = VERBOSE;
990 } else if (0 != GETENV("GC_PRINT_STATS")) {
995 # if ((defined(UNIX_LIKE) && !defined(GC_ANDROID_LOG)) \
996 || (defined(CONSOLE_LOG) && defined(MSWIN32)) \
997 || defined(CYGWIN32) || defined(SYMBIAN)) && !defined(SMALL_CONFIG)
999 char * file_name = TRUSTED_STRING(GETENV("GC_LOG_FILE"));
1000 # ifdef GC_LOG_TO_FILE_ALWAYS
1001 if (NULL == file_name)
1002 file_name = GC_LOG_STD_NAME;
1007 int log_d = open(file_name, O_CREAT | O_WRONLY | O_APPEND, 0644);
1009 GC_err_printf("Failed to open %s as log file\n", file_name);
1013 str = GETENV("GC_ONLY_LOG_TO_FILE");
1014 # ifdef GC_ONLY_LOG_TO_FILE
1015 /* The similar environment variable set to "0" */
1016 /* overrides the effect of the macro defined. */
1017 if (str != NULL && *str == '0' && *(str + 1) == '\0')
1019 /* Otherwise setting the environment variable */
1020 /* to anything other than "0" will prevent from */
1021 /* redirecting stdout/err to the log file. */
1022 if (str == NULL || (*str == '0' && *(str + 1) == '\0'))
1032 # if !defined(NO_DEBUGGING) && !defined(GC_DUMP_REGULARLY)
1033 if (0 != GETENV("GC_DUMP_REGULARLY")) {
1034 GC_dump_regularly = TRUE;
1037 # ifdef KEEP_BACK_PTRS
1039 char * backtraces_string = GETENV("GC_BACKTRACES");
1040 if (0 != backtraces_string) {
1041 GC_backtraces = atol(backtraces_string);
1042 if (backtraces_string[0] == '\0') GC_backtraces = 1;
1046 if (0 != GETENV("GC_FIND_LEAK")) {
1049 # ifndef SHORT_DBG_HDRS
1050 if (0 != GETENV("GC_FINDLEAK_DELAY_FREE")) {
1051 GC_findleak_delay_free = TRUE;
1054 if (0 != GETENV("GC_ALL_INTERIOR_POINTERS")) {
1055 GC_all_interior_pointers = 1;
1057 if (0 != GETENV("GC_DONT_GC")) {
1060 if (0 != GETENV("GC_PRINT_BACK_HEIGHT")) {
1061 GC_print_back_height = TRUE;
1063 if (0 != GETENV("GC_NO_BLACKLIST_WARNING")) {
1064 GC_large_alloc_warn_interval = LONG_MAX;
1067 char * addr_string = GETENV("GC_TRACE");
1068 if (0 != addr_string) {
1069 # ifndef ENABLE_TRACE
1070 WARN("Tracing not enabled: Ignoring GC_TRACE value\n", 0);
1072 word addr = (word)STRTOULL(addr_string, NULL, 16);
1074 WARN("Unlikely trace address: %p\n", (void *)addr);
1075 GC_trace_addr = (ptr_t)addr;
1079 # ifdef GC_COLLECT_AT_MALLOC
1081 char * string = GETENV("GC_COLLECT_AT_MALLOC");
1083 size_t min_lb = (size_t)STRTOULL(string, NULL, 10);
1085 GC_dbg_collect_at_malloc_min_lb = min_lb;
1089 # if !defined(GC_DISABLE_INCREMENTAL) && !defined(NO_CLOCK)
1091 char * time_limit_string = GETENV("GC_PAUSE_TIME_TARGET");
1092 if (0 != time_limit_string) {
1093 long time_limit = atol(time_limit_string);
1094 if (time_limit > 0) {
1095 GC_time_limit = time_limit;
1100 # ifndef SMALL_CONFIG
1102 char * full_freq_string = GETENV("GC_FULL_FREQUENCY");
1103 if (full_freq_string != NULL) {
1104 int full_freq = atoi(full_freq_string);
1106 GC_full_freq = full_freq;
1111 char * interval_string = GETENV("GC_LARGE_ALLOC_WARN_INTERVAL");
1112 if (0 != interval_string) {
1113 long interval = atol(interval_string);
1114 if (interval <= 0) {
1115 WARN("GC_LARGE_ALLOC_WARN_INTERVAL environment variable has "
1116 "bad value: Ignoring\n", 0);
1118 GC_large_alloc_warn_interval = interval;
1123 char * space_divisor_string = GETENV("GC_FREE_SPACE_DIVISOR");
1124 if (space_divisor_string != NULL) {
1125 int space_divisor = atoi(space_divisor_string);
1126 if (space_divisor > 0)
1127 GC_free_space_divisor = (unsigned)space_divisor;
1132 char * string = GETENV("GC_UNMAP_THRESHOLD");
1133 if (string != NULL) {
1134 if (*string == '0' && *(string + 1) == '\0') {
1135 /* "0" is used to disable unmapping. */
1136 GC_unmap_threshold = 0;
1138 int unmap_threshold = atoi(string);
1139 if (unmap_threshold > 0)
1140 GC_unmap_threshold = unmap_threshold;
1145 char * string = GETENV("GC_FORCE_UNMAP_ON_GCOLLECT");
1146 if (string != NULL) {
1147 if (*string == '0' && *(string + 1) == '\0') {
1148 /* "0" is used to turn off the mode. */
1149 GC_force_unmap_on_gcollect = FALSE;
1151 GC_force_unmap_on_gcollect = TRUE;
1156 char * string = GETENV("GC_USE_ENTIRE_HEAP");
1157 if (string != NULL) {
1158 if (*string == '0' && *(string + 1) == '\0') {
1159 /* "0" is used to turn off the mode. */
1160 GC_use_entire_heap = FALSE;
1162 GC_use_entire_heap = TRUE;
1167 # if !defined(NO_DEBUGGING) && !defined(NO_CLOCK)
1168 GET_TIME(GC_init_time);
1170 maybe_install_looping_handler();
1171 # if ALIGNMENT > GC_DS_TAGS
1172 /* Adjust normal object descriptor for extra allocation. */
1173 if (EXTRA_BYTES != 0)
1174 GC_obj_kinds[NORMAL].ok_descriptor = (word)(-ALIGNMENT) | GC_DS_LENGTH;
1176 GC_exclude_static_roots_inner(beginGC_arrays, endGC_arrays);
1177 GC_exclude_static_roots_inner(beginGC_obj_kinds, endGC_obj_kinds);
1178 # ifdef SEPARATE_GLOBALS
1179 GC_exclude_static_roots_inner(beginGC_objfreelist, endGC_objfreelist);
1180 GC_exclude_static_roots_inner(beginGC_aobjfreelist, endGC_aobjfreelist);
1182 # if defined(USE_PROC_FOR_LIBRARIES) && defined(GC_LINUX_THREADS)
1183 WARN("USE_PROC_FOR_LIBRARIES + GC_LINUX_THREADS performs poorly.\n", 0);
1184 /* If thread stacks are cached, they tend to be scanned in */
1185 /* entirety as part of the root set. This will grow them to */
1186 /* maximum size, and is generally not desirable. */
1188 # if defined(SEARCH_FOR_DATA_START)
1189 GC_init_linux_data_start();
1191 # if defined(NETBSD) && defined(__ELF__)
1192 GC_init_netbsd_elf();
1194 # if !defined(THREADS) || defined(GC_PTHREADS) \
1195 || defined(NN_PLATFORM_CTR) || defined(NINTENDO_SWITCH) \
1196 || defined(GC_WIN32_THREADS) || defined(GC_SOLARIS_THREADS)
1197 if (GC_stackbottom == 0) {
1198 GC_stackbottom = GC_get_main_stack_base();
1199 # if (defined(LINUX) || defined(HPUX)) && defined(IA64)
1200 GC_register_stackbottom = GC_get_register_stack_base();
1203 # if (defined(LINUX) || defined(HPUX)) && defined(IA64)
1204 if (GC_register_stackbottom == 0) {
1205 WARN("GC_register_stackbottom should be set with GC_stackbottom\n", 0);
1206 /* The following may fail, since we may rely on */
1207 /* alignment properties that may not hold with a user set */
1208 /* GC_stackbottom. */
1209 GC_register_stackbottom = GC_get_register_stack_base();
1214 # if !defined(CPPCHECK)
1215 GC_STATIC_ASSERT(sizeof(ptr_t) == sizeof(word));
1216 GC_STATIC_ASSERT(sizeof(signed_word) == sizeof(word));
1217 # if !defined(_AUX_SOURCE) || defined(__GNUC__)
1218 GC_STATIC_ASSERT((word)(-1) > (word)0);
1219 /* word should be unsigned */
1221 /* We no longer check for ((void*)(-1) > NULL) since all pointers */
1222 /* are explicitly cast to word in every less/greater comparison. */
1223 GC_STATIC_ASSERT((signed_word)(-1) < (signed_word)0);
1225 GC_STATIC_ASSERT(sizeof (struct hblk) == HBLKSIZE);
1227 GC_ASSERT(!((word)GC_stackbottom HOTTER_THAN (word)GC_approx_sp()));
1229 # ifndef GC_DISABLE_INCREMENTAL
1230 if (GC_incremental || 0 != GETENV("GC_ENABLE_INCREMENTAL")) {
1231 # if defined(BASE_ATOMIC_OPS_EMULATED) || defined(CHECKSUMS) \
1232 || defined(REDIRECT_MALLOC) || defined(REDIRECT_MALLOC_IN_HEADER) \
1233 || defined(SMALL_CONFIG)
1234 /* TODO: Implement CHECKSUMS for manual VDB. */
1236 if (manual_vdb_allowed) {
1237 GC_manual_vdb = TRUE;
1238 GC_incremental = TRUE;
1242 /* For GWW_VDB on Win32, this needs to happen before any */
1243 /* heap memory is allocated. */
1244 GC_incremental = GC_dirty_init();
1245 GC_ASSERT(GC_bytes_allocd == 0);
1250 /* Add initial guess of root sets. Do this first, since sbrk(0) */
1251 /* might be used. */
1252 if (GC_REGISTER_MAIN_STATIC_DATA()) GC_register_data_segments();
1257 char * sz_str = GETENV("GC_INITIAL_HEAP_SIZE");
1258 if (sz_str != NULL) {
1259 initial_heap_sz = GC_parse_mem_size_arg(sz_str);
1260 if (initial_heap_sz <= MINHINCR * HBLKSIZE) {
1261 WARN("Bad initial heap size %s - ignoring it.\n", sz_str);
1266 char * sz_str = GETENV("GC_MAXIMUM_HEAP_SIZE");
1267 if (sz_str != NULL) {
1268 word max_heap_sz = GC_parse_mem_size_arg(sz_str);
1269 if (max_heap_sz < initial_heap_sz) {
1270 WARN("Bad maximum heap size %s - ignoring it.\n", sz_str);
1272 if (0 == GC_max_retries) GC_max_retries = 2;
1273 GC_set_max_heap_size(max_heap_sz);
1276 # if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
1277 LOCK(); /* just to set GC_lock_holder */
1279 if (!GC_expand_hp_inner(divHBLKSZ(initial_heap_sz))) {
1280 GC_err_printf("Can't start up: not enough memory\n");
1283 GC_requested_heapsize += initial_heap_sz;
1285 if (GC_all_interior_pointers)
1286 GC_initialize_offsets();
1287 GC_register_displacement_inner(0L);
1288 # if defined(GC_LINUX_THREADS) && defined(REDIRECT_MALLOC)
1289 if (!GC_all_interior_pointers) {
1290 /* TLS ABI uses pointer-sized offsets for dtv. */
1291 GC_register_displacement_inner(sizeof(void *));
1296 if (PCR_IL_Lock(PCR_Bool_false, PCR_allSigsBlocked, PCR_waitForever)
1298 ABORT("Can't lock load state");
1299 } else if (PCR_IL_Unlock() != PCR_ERes_okay) {
1300 ABORT("Can't unlock load state");
1305 GC_is_initialized = TRUE;
1306 # if defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
1308 # ifdef PARALLEL_MARK
1309 /* Actually start helper threads. */
1310 # if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
1313 GC_start_mark_threads_inner();
1314 # if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
1320 /* Get black list set up and/or incremental GC started */
1321 if (!GC_dont_precollect || GC_incremental) {
1322 GC_gcollect_inner();
1324 # if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
1327 # if defined(THREADS) && defined(UNIX_LIKE) && !defined(NO_GETCONTEXT)
1328 /* Ensure getcontext_works is set to avoid potential data race. */
1329 if (GC_dont_gc || GC_dont_precollect)
1330 GC_with_callee_saves_pushed(callee_saves_pushed_dummy_fn, NULL);
1332 # ifndef DONT_USE_ATEXIT
1334 /* This is to give us at least one chance to detect leaks. */
1335 /* This may report some very benign leaks, but ... */
1336 atexit(GC_exit_check);
1340 /* The rest of this again assumes we don't really hold */
1341 /* the allocation lock. */
1342 # if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC) \
1343 || (defined(GC_ALWAYS_MULTITHREADED) && defined(GC_WIN32_THREADS) \
1344 && !defined(GC_NO_THREADS_DISCOVERY))
1345 /* Make sure marker threads are started and thread local */
1346 /* allocation is initialized, in case we didn't get */
1347 /* called from GC_init_parallel. */
1349 # endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
1351 # if defined(DYNAMIC_LOADING) && defined(DARWIN)
1352 /* This must be called WITHOUT the allocation lock held */
1353 /* and before any threads are created. */
1356 RESTORE_CANCEL(cancel_state);
1359 GC_API void GC_CALL GC_enable_incremental(void)
1361 # if !defined(GC_DISABLE_INCREMENTAL) && !defined(KEEP_BACK_PTRS)
1363 /* If we are keeping back pointers, the GC itself dirties all */
1364 /* pages on which objects have been marked, making */
1365 /* incremental GC pointless. */
1366 if (!GC_find_leak && 0 == GETENV("GC_DISABLE_INCREMENTAL")) {
1368 if (!GC_incremental) {
1370 /* if (GC_no_win32_dlls) goto out; Should be win32S test? */
1371 maybe_install_looping_handler(); /* Before write fault handler! */
1372 if (!GC_is_initialized) {
1374 GC_incremental = TRUE; /* indicate intention to turn it on */
1378 # if !defined(BASE_ATOMIC_OPS_EMULATED) && !defined(CHECKSUMS) \
1379 && !defined(REDIRECT_MALLOC) \
1380 && !defined(REDIRECT_MALLOC_IN_HEADER) && !defined(SMALL_CONFIG)
1381 if (manual_vdb_allowed) {
1382 GC_manual_vdb = TRUE;
1383 GC_incremental = TRUE;
1387 GC_incremental = GC_dirty_init();
1390 if (GC_incremental && !GC_dont_gc) {
1391 /* Can't easily do it if GC_dont_gc. */
1392 IF_CANCEL(int cancel_state;)
1394 DISABLE_CANCEL(cancel_state);
1395 if (GC_bytes_allocd > 0) {
1396 /* There may be unmarked reachable objects. */
1397 GC_gcollect_inner();
1399 /* else we're OK in assuming everything's */
1400 /* clean since nothing can point to an */
1401 /* unmarked object. */
1402 GC_read_dirty(FALSE);
1403 RESTORE_CANCEL(cancel_state);
1413 #if defined(THREADS)
1414 GC_API void GC_CALL GC_start_mark_threads(void)
1416 # if defined(PARALLEL_MARK) && defined(CAN_HANDLE_FORK) \
1417 && !defined(THREAD_SANITIZER)
1418 /* TSan does not support threads creation in the child process. */
1419 IF_CANCEL(int cancel_state;)
1421 DISABLE_CANCEL(cancel_state);
1422 GC_start_mark_threads_inner();
1423 RESTORE_CANCEL(cancel_state);
1425 /* No action since parallel markers are disabled (or no POSIX fork). */
1426 GC_ASSERT(I_DONT_HOLD_LOCK());
1431 GC_API void GC_CALL GC_deinit(void)
1433 if (GC_is_initialized) {
1434 /* Prevent duplicate resource close. */
1435 GC_is_initialized = FALSE;
1436 # if defined(GC_WIN32_THREADS) && (defined(MSWIN32) || defined(MSWINCE))
1437 # if !defined(CONSOLE_LOG) || defined(MSWINCE)
1438 DeleteCriticalSection(&GC_write_cs);
1440 DeleteCriticalSection(&GC_allocate_ml);
1445 #if (defined(MSWIN32) && !defined(CONSOLE_LOG)) || defined(MSWINCE)
1447 # if defined(_MSC_VER) && defined(_DEBUG) && !defined(MSWINCE)
1448 # include <crtdbg.h>
1451 STATIC HANDLE GC_log = 0;
1454 # if defined(PARALLEL_MARK) && !defined(GC_ALWAYS_MULTITHREADED)
1455 # define IF_NEED_TO_LOCK(x) if (GC_parallel || GC_need_to_lock) x
1457 # define IF_NEED_TO_LOCK(x) if (GC_need_to_lock) x
1460 # define IF_NEED_TO_LOCK(x)
1461 # endif /* !THREADS */
1463 # ifdef MSWINRT_FLAVOR
1464 # include <windows.storage.h>
1466 /* This API is defined in roapi.h, but we cannot include it here */
1467 /* since it does not compile in C. */
1468 DECLSPEC_IMPORT HRESULT WINAPI RoGetActivationFactory(
1469 HSTRING activatableClassId,
1470 REFIID iid, void** factory);
1472 static GC_bool getWinRTLogPath(wchar_t* buf, size_t bufLen)
1474 static const GUID kIID_IApplicationDataStatics = {
1475 0x5612147B, 0xE843, 0x45E3,
1476 0x94, 0xD8, 0x06, 0x16, 0x9E, 0x3C, 0x8E, 0x17
1478 static const GUID kIID_IStorageItem = {
1479 0x4207A996, 0xCA2F, 0x42F7,
1480 0xBD, 0xE8, 0x8B, 0x10, 0x45, 0x7A, 0x7F, 0x30
1482 GC_bool result = FALSE;
1483 HSTRING_HEADER appDataClassNameHeader;
1484 HSTRING appDataClassName;
1485 __x_ABI_CWindows_CStorage_CIApplicationDataStatics* appDataStatics = 0;
1487 GC_ASSERT(bufLen > 0);
1488 if (SUCCEEDED(WindowsCreateStringReference(
1489 RuntimeClass_Windows_Storage_ApplicationData,
1490 (sizeof(RuntimeClass_Windows_Storage_ApplicationData)-1)
1492 &appDataClassNameHeader, &appDataClassName))
1493 && SUCCEEDED(RoGetActivationFactory(appDataClassName,
1494 &kIID_IApplicationDataStatics,
1495 &appDataStatics))) {
1496 __x_ABI_CWindows_CStorage_CIApplicationData* appData = NULL;
1497 __x_ABI_CWindows_CStorage_CIStorageFolder* tempFolder = NULL;
1498 __x_ABI_CWindows_CStorage_CIStorageItem* tempFolderItem = NULL;
1499 HSTRING tempPath = NULL;
1501 if (SUCCEEDED(appDataStatics->lpVtbl->get_Current(appDataStatics,
1503 && SUCCEEDED(appData->lpVtbl->get_TemporaryFolder(appData,
1505 && SUCCEEDED(tempFolder->lpVtbl->QueryInterface(tempFolder,
1508 && SUCCEEDED(tempFolderItem->lpVtbl->get_Path(tempFolderItem,
1511 const wchar_t* tempPathBuf =
1512 WindowsGetStringRawBuffer(tempPath, &tempPathLen);
1515 if (wcsncat_s(buf, bufLen, tempPathBuf, tempPathLen) == 0
1516 && wcscat_s(buf, bufLen, L"\\") == 0
1517 && wcscat_s(buf, bufLen, TEXT(GC_LOG_STD_NAME)) == 0)
1519 WindowsDeleteString(tempPath);
1522 if (tempFolderItem != NULL)
1523 tempFolderItem->lpVtbl->Release(tempFolderItem);
1524 if (tempFolder != NULL)
1525 tempFolder->lpVtbl->Release(tempFolder);
1526 if (appData != NULL)
1527 appData->lpVtbl->Release(appData);
1528 appDataStatics->lpVtbl->Release(appDataStatics);
1532 # endif /* MSWINRT_FLAVOR */
1534 STATIC HANDLE GC_CreateLogFile(void)
1537 # ifdef MSWINRT_FLAVOR
1538 TCHAR pathBuf[_MAX_PATH + 0x10]; /* buffer for path + ext */
1540 hFile = INVALID_HANDLE_VALUE;
1541 if (getWinRTLogPath(pathBuf, _MAX_PATH + 1)) {
1542 CREATEFILE2_EXTENDED_PARAMETERS extParams;
1544 BZERO(&extParams, sizeof(extParams));
1545 extParams.dwSize = sizeof(extParams);
1546 extParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
1547 extParams.dwFileFlags = GC_print_stats == VERBOSE ? 0
1548 : FILE_FLAG_WRITE_THROUGH;
1549 hFile = CreateFile2(pathBuf, GENERIC_WRITE, FILE_SHARE_READ,
1550 CREATE_ALWAYS, &extParams);
1555 # if defined(NO_GETENV_WIN32) && defined(CPPCHECK)
1556 # define appendToFile FALSE
1558 BOOL appendToFile = FALSE;
1560 # if !defined(NO_GETENV_WIN32) || !defined(OLD_WIN32_LOG_FILE)
1561 TCHAR pathBuf[_MAX_PATH + 0x10]; /* buffer for path + ext */
1566 /* Use GetEnvironmentVariable instead of GETENV() for unicode support. */
1567 # ifndef NO_GETENV_WIN32
1568 if (GetEnvironmentVariable(TEXT("GC_LOG_FILE"), pathBuf,
1569 _MAX_PATH + 1) - 1U < (DWORD)_MAX_PATH) {
1570 appendToFile = TRUE;
1574 /* Env var not found or its value too long. */
1575 # ifdef OLD_WIN32_LOG_FILE
1576 logPath = TEXT(GC_LOG_STD_NAME);
1578 int len = (int)GetModuleFileName(NULL /* hModule */, pathBuf,
1580 /* If GetModuleFileName() has failed then len is 0. */
1581 if (len > 4 && pathBuf[len - 4] == (TCHAR)'.') {
1582 len -= 4; /* strip executable file extension */
1584 BCOPY(TEXT(".") TEXT(GC_LOG_STD_NAME), &pathBuf[len],
1585 sizeof(TEXT(".") TEXT(GC_LOG_STD_NAME)));
1589 hFile = CreateFile(logPath, GENERIC_WRITE, FILE_SHARE_READ,
1590 NULL /* lpSecurityAttributes */,
1591 appendToFile ? OPEN_ALWAYS : CREATE_ALWAYS,
1592 GC_print_stats == VERBOSE ? FILE_ATTRIBUTE_NORMAL :
1593 /* immediately flush writes unless very verbose */
1594 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
1595 NULL /* hTemplateFile */);
1597 # ifndef NO_GETENV_WIN32
1598 if (appendToFile && hFile != INVALID_HANDLE_VALUE) {
1600 (void)SetFilePointer(hFile, 0, &posHigh, FILE_END);
1601 /* Seek to file end (ignoring any error) */
1604 # undef appendToFile
1609 STATIC int GC_write(const char *buf, size_t len)
1613 # if defined(THREADS) && defined(GC_ASSERTIONS)
1614 static GC_bool inside_write = FALSE;
1615 /* to prevent infinite recursion at abort. */
1622 IF_NEED_TO_LOCK(EnterCriticalSection(&GC_write_cs));
1623 # if defined(THREADS) && defined(GC_ASSERTIONS)
1624 if (GC_write_disabled) {
1625 inside_write = TRUE;
1626 ABORT("Assertion failure: GC_write called with write_disabled");
1630 GC_log = GC_CreateLogFile();
1632 if (GC_log == INVALID_HANDLE_VALUE) {
1633 IF_NEED_TO_LOCK(LeaveCriticalSection(&GC_write_cs));
1634 # ifdef NO_DEBUGGING
1635 /* Ignore open log failure (e.g., it might be caused by */
1636 /* read-only folder of the client application). */
1642 res = WriteFile(GC_log, buf, (DWORD)len, &written, NULL);
1643 # if defined(_MSC_VER) && defined(_DEBUG) && !defined(NO_CRT)
1645 /* There is no CrtDbgReport() in WinCE */
1648 /* Always use Unicode variant of OutputDebugString() */
1649 wbuf[MultiByteToWideChar(CP_ACP, 0 /* dwFlags */,
1651 sizeof(wbuf) / sizeof(wbuf[0]) - 1)] = 0;
1652 OutputDebugStringW(wbuf);
1655 _CrtDbgReport(_CRT_WARN, NULL, 0, NULL, "%.*s", len, buf);
1658 IF_NEED_TO_LOCK(LeaveCriticalSection(&GC_write_cs));
1659 return res ? (int)written : -1;
1662 /* TODO: This is pretty ugly ... */
1663 # define WRITE(f, buf, len) GC_write(buf, len)
1665 #elif defined(OS2) || defined(MACOS)
1666 STATIC FILE * GC_stdout = NULL;
1667 STATIC FILE * GC_stderr = NULL;
1668 STATIC FILE * GC_log = NULL;
1670 /* Initialize GC_log (and the friends) passed to GC_write(). */
1671 STATIC void GC_set_files(void)
1673 if (GC_stdout == NULL) {
1676 if (GC_stderr == NULL) {
1679 if (GC_log == NULL) {
1684 GC_INLINE int GC_write(FILE *f, const char *buf, size_t len)
1686 int res = fwrite(buf, 1, len, f);
1691 # define WRITE(f, buf, len) (GC_set_files(), GC_write(f, buf, len))
1693 #elif defined(GC_ANDROID_LOG)
1695 # include <android/log.h>
1697 # ifndef GC_ANDROID_LOG_TAG
1698 # define GC_ANDROID_LOG_TAG "BDWGC"
1701 # define GC_stdout ANDROID_LOG_DEBUG
1702 # define GC_stderr ANDROID_LOG_ERROR
1703 # define GC_log GC_stdout
1705 # define WRITE(level, buf, unused_len) \
1706 __android_log_write(level, GC_ANDROID_LOG_TAG, buf)
1708 #elif defined(NN_PLATFORM_CTR)
1709 int n3ds_log_write(const char* text, int length);
1710 # define WRITE(level, buf, len) n3ds_log_write(buf, len)
1712 #elif defined(NINTENDO_SWITCH)
1713 int switch_log_write(const char* text, int length);
1714 # define WRITE(level, buf, len) switch_log_write(buf, len)
1717 # if !defined(AMIGA) && !defined(MSWIN32) && !defined(MSWIN_XBOX1) \
1718 && !defined(SN_TARGET_ORBIS) && !defined(SN_TARGET_PSP2) \
1719 && !defined(__CC_ARM)
1720 # include <unistd.h>
1723 STATIC int GC_write(int fd, const char *buf, size_t len)
1725 # if defined(ECOS) || defined(SN_TARGET_ORBIS) || defined(SN_TARGET_PSP2) \
1728 /* FIXME: This seems to be defined nowhere at present. */
1729 /* _Jv_diag_write(buf, len); */
1735 int bytes_written = 0;
1736 IF_CANCEL(int cancel_state;)
1738 DISABLE_CANCEL(cancel_state);
1739 while ((unsigned)bytes_written < len) {
1740 # ifdef GC_SOLARIS_THREADS
1741 int result = syscall(SYS_write, fd, buf + bytes_written,
1742 len - bytes_written);
1744 int result = write(fd, buf + bytes_written, len - bytes_written);
1748 RESTORE_CANCEL(cancel_state);
1751 bytes_written += result;
1753 RESTORE_CANCEL(cancel_state);
1754 return(bytes_written);
1758 # define WRITE(f, buf, len) GC_write(f, buf, len)
1759 #endif /* !MSWINCE && !OS2 && !MACOS && !GC_ANDROID_LOG */
1763 #if defined(DJGPP) || defined(__STRICT_ANSI__)
1764 /* vsnprintf is missing in DJGPP (v2.0.3) */
1765 # define GC_VSNPRINTF(buf, bufsz, format, args) vsprintf(buf, format, args)
1766 #elif defined(_MSC_VER)
1768 /* _vsnprintf is deprecated in WinCE */
1769 # define GC_VSNPRINTF StringCchVPrintfA
1771 # define GC_VSNPRINTF _vsnprintf
1774 # define GC_VSNPRINTF vsnprintf
1777 /* A version of printf that is unlikely to call malloc, and is thus safer */
1778 /* to call from the collector in case malloc has been bound to GC_malloc. */
1779 /* Floating point arguments and formats should be avoided, since FP */
1780 /* conversion is more likely to allocate memory. */
1781 /* Assumes that no more than BUFSZ-1 characters are written at once. */
1782 #define GC_PRINTF_FILLBUF(buf, format) \
1785 va_start(args, format); \
1786 (buf)[sizeof(buf) - 1] = 0x15; /* guard */ \
1787 (void)GC_VSNPRINTF(buf, sizeof(buf) - 1, format, args); \
1789 if ((buf)[sizeof(buf) - 1] != 0x15) \
1790 ABORT("GC_printf clobbered stack"); \
1793 void GC_printf(const char *format, ...)
1796 char buf[BUFSZ + 1];
1798 GC_PRINTF_FILLBUF(buf, format);
1800 (void)WRITE(GC_stdout, buf, strlen(buf));
1801 /* Ignore errors silently. */
1803 if (WRITE(GC_stdout, buf, strlen(buf)) < 0)
1804 ABORT("write to stdout failed");
1809 void GC_err_printf(const char *format, ...)
1811 char buf[BUFSZ + 1];
1813 GC_PRINTF_FILLBUF(buf, format);
1817 void GC_log_printf(const char *format, ...)
1819 char buf[BUFSZ + 1];
1821 GC_PRINTF_FILLBUF(buf, format);
1823 (void)WRITE(GC_log, buf, strlen(buf));
1825 if (WRITE(GC_log, buf, strlen(buf)) < 0)
1826 ABORT("write to GC log failed");
1830 #ifndef GC_ANDROID_LOG
1832 # define GC_warn_printf GC_err_printf
1836 GC_INNER void GC_info_log_printf(const char *format, ...)
1838 char buf[BUFSZ + 1];
1840 GC_PRINTF_FILLBUF(buf, format);
1841 (void)WRITE(ANDROID_LOG_INFO, buf, 0 /* unused */);
1844 GC_INNER void GC_verbose_log_printf(const char *format, ...)
1846 char buf[BUFSZ + 1];
1848 GC_PRINTF_FILLBUF(buf, format);
1849 (void)WRITE(ANDROID_LOG_VERBOSE, buf, 0); /* ignore write errors */
1852 STATIC void GC_warn_printf(const char *format, ...)
1854 char buf[BUFSZ + 1];
1856 GC_PRINTF_FILLBUF(buf, format);
1857 (void)WRITE(ANDROID_LOG_WARN, buf, 0);
1860 #endif /* GC_ANDROID_LOG */
1862 void GC_err_puts(const char *s)
1864 (void)WRITE(GC_stderr, s, strlen(s)); /* ignore errors */
1867 STATIC void GC_CALLBACK GC_default_warn_proc(char *msg, GC_word arg)
1869 /* TODO: Add assertion on arg comply with msg (format). */
1870 GC_warn_printf(msg, arg);
1873 GC_INNER GC_warn_proc GC_current_warn_proc = GC_default_warn_proc;
1875 /* This is recommended for production code (release). */
1876 GC_API void GC_CALLBACK GC_ignore_warn_proc(char *msg, GC_word arg)
1878 if (GC_print_stats) {
1879 /* Don't ignore warnings if stats printing is on. */
1880 GC_default_warn_proc(msg, arg);
1884 GC_API void GC_CALL GC_set_warn_proc(GC_warn_proc p)
1887 GC_ASSERT(NONNULL_ARG_NOT_NULL(p));
1888 # ifdef GC_WIN32_THREADS
1890 /* Need explicit GC_INIT call */
1891 GC_ASSERT(GC_is_initialized);
1893 if (!GC_is_initialized) GC_init();
1897 GC_current_warn_proc = p;
1901 GC_API GC_warn_proc GC_CALL GC_get_warn_proc(void)
1903 GC_warn_proc result;
1906 result = GC_current_warn_proc;
1911 #if !defined(PCR) && !defined(SMALL_CONFIG)
1912 /* Print (or display) a message before abnormal exit (including */
1913 /* abort). Invoked from ABORT(msg) macro (there msg is non-NULL) */
1914 /* and from EXIT() macro (msg is NULL in that case). */
1915 STATIC void GC_CALLBACK GC_default_on_abort(const char *msg)
1917 # ifndef DONT_USE_ATEXIT
1918 skip_gc_atexit = TRUE; /* disable at-exit GC_gcollect() */
1922 # ifdef MSGBOX_ON_ERROR
1923 GC_win32_MessageBoxA(msg, "Fatal error in GC", MB_ICONERROR | MB_OK);
1924 /* Also duplicate msg to GC log file. */
1927 # ifndef GC_ANDROID_LOG
1928 /* Avoid calling GC_err_printf() here, as GC_on_abort() could be */
1929 /* called from it. Note 1: this is not an atomic output. */
1930 /* Note 2: possible write errors are ignored. */
1931 # if defined(GC_WIN32_THREADS) && defined(GC_ASSERTIONS) \
1932 && ((defined(MSWIN32) && !defined(CONSOLE_LOG)) || defined(MSWINCE))
1933 if (!GC_write_disabled)
1936 if (WRITE(GC_stderr, msg, strlen(msg)) >= 0)
1937 (void)WRITE(GC_stderr, "\n", 1);
1940 __android_log_assert("*" /* cond */, GC_ANDROID_LOG_TAG, "%s\n", msg);
1944 # if !defined(NO_DEBUGGING) && !defined(GC_ANDROID_LOG)
1945 if (GETENV("GC_LOOP_ON_ABORT") != NULL) {
1946 /* In many cases it's easier to debug a running process. */
1947 /* It's arguably nicer to sleep, but that makes it harder */
1948 /* to look at the thread if the debugger doesn't know much */
1949 /* about threads. */
1957 GC_abort_func GC_on_abort = GC_default_on_abort;
1959 GC_API void GC_CALL GC_set_abort_func(GC_abort_func fn)
1962 GC_ASSERT(NONNULL_ARG_NOT_NULL(fn));
1968 GC_API GC_abort_func GC_CALL GC_get_abort_func(void)
1977 #endif /* !SMALL_CONFIG */
1979 GC_API void GC_CALL GC_enable(void)
1984 GC_ASSERT(GC_dont_gc != 0); /* ensure no counter underflow */
1989 GC_API void GC_CALL GC_disable(void)
1997 GC_API int GC_CALL GC_is_disabled(void)
1999 return GC_dont_gc != 0;
2002 /* Helper procedures for new kind creation. */
2003 GC_API void ** GC_CALL GC_new_free_list_inner(void)
2007 GC_ASSERT(I_HOLD_LOCK());
2008 result = GC_INTERNAL_MALLOC((MAXOBJGRANULES+1) * sizeof(ptr_t), PTRFREE);
2009 if (NULL == result) ABORT("Failed to allocate freelist for new kind");
2010 BZERO(result, (MAXOBJGRANULES+1)*sizeof(ptr_t));
2011 return (void **)result;
2014 GC_API void ** GC_CALL GC_new_free_list(void)
2019 result = GC_new_free_list_inner();
2024 GC_API unsigned GC_CALL GC_new_kind_inner(void **fl, GC_word descr,
2025 int adjust, int clear)
2027 unsigned result = GC_n_kinds;
2029 GC_ASSERT(adjust == FALSE || adjust == TRUE);
2030 /* If an object is not needed to be cleared (when moved to the */
2031 /* free list) then its descriptor should be zero to denote */
2032 /* a pointer-free object (and, as a consequence, the size of the */
2033 /* object should not be added to the descriptor template). */
2034 GC_ASSERT(clear == TRUE
2035 || (descr == 0 && adjust == FALSE && clear == FALSE));
2036 if (result < MAXOBJKINDS) {
2038 GC_obj_kinds[result].ok_freelist = fl;
2039 GC_obj_kinds[result].ok_reclaim_list = 0;
2040 GC_obj_kinds[result].ok_descriptor = descr;
2041 GC_obj_kinds[result].ok_relocate_descr = adjust;
2042 GC_obj_kinds[result].ok_init = (GC_bool)clear;
2043 # ifdef ENABLE_DISCLAIM
2044 GC_obj_kinds[result].ok_mark_unconditionally = FALSE;
2045 GC_obj_kinds[result].ok_disclaim_proc = 0;
2048 ABORT("Too many kinds");
2053 GC_API unsigned GC_CALL GC_new_kind(void **fl, GC_word descr, int adjust,
2059 result = GC_new_kind_inner(fl, descr, adjust, clear);
2064 GC_API unsigned GC_CALL GC_new_proc_inner(GC_mark_proc proc)
2066 unsigned result = GC_n_mark_procs;
2068 if (result < MAX_MARK_PROCS) {
2070 GC_mark_procs[result] = proc;
2072 ABORT("Too many mark procedures");
2077 GC_API unsigned GC_CALL GC_new_proc(GC_mark_proc proc)
2082 result = GC_new_proc_inner(proc);
2087 GC_API void * GC_CALL GC_call_with_alloc_lock(GC_fn_type fn, void *client_data)
2095 result = (*fn)(client_data);
2102 GC_API void * GC_CALL GC_call_with_stack_base(GC_stack_base_func fn, void *arg)
2104 struct GC_stack_base base;
2107 base.mem_base = (void *)&base;
2109 base.reg_base = (void *)GC_save_regs_in_stack();
2110 /* Unnecessarily flushes register stack, */
2111 /* but that probably doesn't hurt. */
2113 result = fn(&base, arg);
2114 /* Strongly discourage the compiler from treating the above */
2115 /* as a tail call. */
2116 GC_noop1(COVERT_DATAFLOW(&base));
2122 GC_INNER ptr_t GC_blocked_sp = NULL;
2123 /* NULL value means we are not inside GC_do_blocking() call. */
2125 STATIC ptr_t GC_blocked_register_sp = NULL;
2128 GC_INNER struct GC_traced_stack_sect_s *GC_traced_stack_sect = NULL;
2130 /* This is nearly the same as in win32_threads.c */
2131 GC_API void * GC_CALL GC_call_with_gc_active(GC_fn_type fn,
2134 struct GC_traced_stack_sect_s stacksect;
2135 GC_ASSERT(GC_is_initialized);
2137 /* Adjust our stack bottom pointer (this could happen if */
2138 /* GC_get_main_stack_base() is unimplemented or broken for */
2139 /* the platform). */
2140 if ((word)GC_stackbottom HOTTER_THAN (word)(&stacksect))
2141 GC_stackbottom = (ptr_t)COVERT_DATAFLOW(&stacksect);
2143 if (GC_blocked_sp == NULL) {
2144 /* We are not inside GC_do_blocking() - do nothing more. */
2145 client_data = fn(client_data);
2146 /* Prevent treating the above as a tail call. */
2147 GC_noop1(COVERT_DATAFLOW(&stacksect));
2148 return client_data; /* result */
2151 /* Setup new "stack section". */
2152 stacksect.saved_stack_ptr = GC_blocked_sp;
2154 /* This is the same as in GC_call_with_stack_base(). */
2155 stacksect.backing_store_end = GC_save_regs_in_stack();
2156 /* Unnecessarily flushes register stack, */
2157 /* but that probably doesn't hurt. */
2158 stacksect.saved_backing_store_ptr = GC_blocked_register_sp;
2160 stacksect.prev = GC_traced_stack_sect;
2161 GC_blocked_sp = NULL;
2162 GC_traced_stack_sect = &stacksect;
2164 client_data = fn(client_data);
2165 GC_ASSERT(GC_blocked_sp == NULL);
2166 GC_ASSERT(GC_traced_stack_sect == &stacksect);
2168 # if defined(CPPCHECK)
2169 GC_noop1((word)GC_traced_stack_sect - (word)GC_blocked_sp);
2171 /* Restore original "stack section". */
2172 GC_traced_stack_sect = stacksect.prev;
2174 GC_blocked_register_sp = stacksect.saved_backing_store_ptr;
2176 GC_blocked_sp = stacksect.saved_stack_ptr;
2178 return client_data; /* result */
2181 /* This is nearly the same as in win32_threads.c */
2182 STATIC void GC_do_blocking_inner(ptr_t data, void * context GC_ATTR_UNUSED)
2184 struct blocking_data * d = (struct blocking_data *) data;
2185 GC_ASSERT(GC_is_initialized);
2186 GC_ASSERT(GC_blocked_sp == NULL);
2188 GC_blocked_sp = GC_save_regs_in_stack();
2190 GC_blocked_sp = (ptr_t) &d; /* save approx. sp */
2193 GC_blocked_register_sp = GC_save_regs_in_stack();
2196 d -> client_data = (d -> fn)(d -> client_data);
2199 GC_ASSERT(GC_blocked_sp != NULL);
2201 GC_ASSERT(GC_blocked_sp == (ptr_t)(&d));
2203 # if defined(CPPCHECK)
2204 GC_noop1((word)GC_blocked_sp);
2206 GC_blocked_sp = NULL;
2209 GC_API void GC_CALL GC_set_stackbottom(void *gc_thread_handle,
2210 const struct GC_stack_base *sb)
2212 GC_ASSERT(sb -> mem_base != NULL);
2213 GC_ASSERT(NULL == gc_thread_handle || &GC_stackbottom == gc_thread_handle);
2214 GC_ASSERT(NULL == GC_blocked_sp
2215 && NULL == GC_traced_stack_sect); /* for now */
2216 (void)gc_thread_handle;
2218 GC_stackbottom = (char *)sb->mem_base;
2220 GC_register_stackbottom = (ptr_t)sb->reg_base;
2224 GC_API void * GC_CALL GC_get_my_stackbottom(struct GC_stack_base *sb)
2226 GC_ASSERT(GC_is_initialized);
2227 sb -> mem_base = GC_stackbottom;
2229 sb -> reg_base = GC_register_stackbottom;
2231 return &GC_stackbottom; /* gc_thread_handle */
2233 #endif /* !THREADS */
2235 GC_API void * GC_CALL GC_do_blocking(GC_fn_type fn, void * client_data)
2237 struct blocking_data my_data;
2240 my_data.client_data = client_data;
2241 GC_with_callee_saves_pushed(GC_do_blocking_inner, (ptr_t)(&my_data));
2242 return my_data.client_data; /* result */
2245 #if !defined(NO_DEBUGGING)
2246 GC_API void GC_CALL GC_dump(void)
2251 GC_dump_named(NULL);
2255 GC_API void GC_CALL GC_dump_named(const char *name)
2258 CLOCK_TYPE current_time;
2260 GET_TIME(current_time);
2263 GC_printf("***GC Dump %s\n", name);
2265 GC_printf("***GC Dump collection #%lu\n", (unsigned long)GC_gc_no);
2268 /* Note that the time is wrapped in ~49 days if sizeof(long)==4. */
2269 GC_printf("Time since GC init: %lu ms\n",
2270 MS_TIME_DIFF(current_time, GC_init_time));
2273 GC_printf("\n***Static roots:\n");
2274 GC_print_static_roots();
2275 GC_printf("\n***Heap sections:\n");
2276 GC_print_heap_sects();
2277 GC_printf("\n***Free blocks:\n");
2278 GC_print_hblkfreelist();
2279 GC_printf("\n***Blocks in use:\n");
2280 GC_print_block_list();
2282 #endif /* !NO_DEBUGGING */
2284 static void block_add_size(struct hblk *h, word pbytes)
2287 *(word *)pbytes += (WORDS_TO_BYTES(hhdr->hb_sz) + (HBLKSIZE - 1))
2288 & ~(word)(HBLKSIZE - 1);
2291 GC_API size_t GC_CALL GC_get_memory_use(void)
2297 GC_apply_to_all_blocks(block_add_size, (word)(&bytes));
2299 return (size_t)bytes;
2302 /* Getter functions for the public Read-only variables. */
2304 /* GC_get_gc_no() is unsynchronized and should be typically called */
2305 /* inside the context of GC_call_with_alloc_lock() to prevent data */
2306 /* races (on multiprocessors). */
2307 GC_API GC_word GC_CALL GC_get_gc_no(void)
2313 GC_API int GC_CALL GC_get_parallel(void)
2315 /* GC_parallel is initialized at start-up. */
2319 GC_API void GC_CALL GC_alloc_lock(void)
2325 GC_API void GC_CALL GC_alloc_unlock(void)
2327 /* no DCL_LOCK_STATE */
2331 GC_INNER GC_on_thread_event_proc GC_on_thread_event = 0;
2333 GC_API void GC_CALL GC_set_on_thread_event(GC_on_thread_event_proc fn)
2335 /* fn may be 0 (means no event notifier). */
2338 GC_on_thread_event = fn;
2342 GC_API GC_on_thread_event_proc GC_CALL GC_get_on_thread_event(void)
2344 GC_on_thread_event_proc fn;
2347 fn = GC_on_thread_event;
2351 #endif /* THREADS */
2353 /* Setter and getter functions for the public R/W function variables. */
2354 /* These functions are synchronized (like GC_set_warn_proc() and */
2355 /* GC_get_warn_proc()). */
2357 GC_API void GC_CALL GC_set_oom_fn(GC_oom_func fn)
2359 GC_ASSERT(NONNULL_ARG_NOT_NULL(fn));
2366 GC_API GC_oom_func GC_CALL GC_get_oom_fn(void)
2376 GC_API void GC_CALL GC_set_on_heap_resize(GC_on_heap_resize_proc fn)
2378 /* fn may be 0 (means no event notifier). */
2381 GC_on_heap_resize = fn;
2385 GC_API GC_on_heap_resize_proc GC_CALL GC_get_on_heap_resize(void)
2387 GC_on_heap_resize_proc fn;
2390 fn = GC_on_heap_resize;
2395 GC_API void GC_CALL GC_set_finalizer_notifier(GC_finalizer_notifier_proc fn)
2397 /* fn may be 0 (means no finalizer notifier). */
2400 GC_finalizer_notifier = fn;
2404 GC_API GC_finalizer_notifier_proc GC_CALL GC_get_finalizer_notifier(void)
2406 GC_finalizer_notifier_proc fn;
2409 fn = GC_finalizer_notifier;
2414 /* Setter and getter functions for the public numeric R/W variables. */
2415 /* It is safe to call these functions even before GC_INIT(). */
2416 /* These functions are unsynchronized and should be typically called */
2417 /* inside the context of GC_call_with_alloc_lock() (if called after */
2418 /* GC_INIT()) to prevent data races (unless it is guaranteed the */
2419 /* collector is not multi-threaded at that execution point). */
2421 GC_API void GC_CALL GC_set_find_leak(int value)
2423 /* value is of boolean type. */
2424 GC_find_leak = value;
2427 GC_API int GC_CALL GC_get_find_leak(void)
2429 return GC_find_leak;
2432 GC_API void GC_CALL GC_set_all_interior_pointers(int value)
2436 GC_all_interior_pointers = value ? 1 : 0;
2437 if (GC_is_initialized) {
2438 /* It is not recommended to change GC_all_interior_pointers value */
2439 /* after GC is initialized but it seems GC could work correctly */
2440 /* even after switching the mode. */
2442 GC_initialize_offsets(); /* NOTE: this resets manual offsets as well */
2443 if (!GC_all_interior_pointers)
2444 GC_bl_init_no_interiors();
2449 GC_API int GC_CALL GC_get_all_interior_pointers(void)
2451 return GC_all_interior_pointers;
2454 GC_API void GC_CALL GC_set_finalize_on_demand(int value)
2456 GC_ASSERT(value != -1); /* -1 was used to retrieve old value in gc-7.2 */
2457 /* value is of boolean type. */
2458 GC_finalize_on_demand = value;
2461 GC_API int GC_CALL GC_get_finalize_on_demand(void)
2463 return GC_finalize_on_demand;
2466 GC_API void GC_CALL GC_set_java_finalization(int value)
2468 GC_ASSERT(value != -1); /* -1 was used to retrieve old value in gc-7.2 */
2469 /* value is of boolean type. */
2470 GC_java_finalization = value;
2473 GC_API int GC_CALL GC_get_java_finalization(void)
2475 return GC_java_finalization;
2478 GC_API void GC_CALL GC_set_dont_expand(int value)
2480 GC_ASSERT(value != -1); /* -1 was used to retrieve old value in gc-7.2 */
2481 /* value is of boolean type. */
2482 GC_dont_expand = value;
2485 GC_API int GC_CALL GC_get_dont_expand(void)
2487 return GC_dont_expand;
2490 GC_API void GC_CALL GC_set_no_dls(int value)
2492 GC_ASSERT(value != -1); /* -1 was used to retrieve old value in gc-7.2 */
2493 /* value is of boolean type. */
2497 GC_API int GC_CALL GC_get_no_dls(void)
2502 GC_API void GC_CALL GC_set_non_gc_bytes(GC_word value)
2504 GC_non_gc_bytes = value;
2507 GC_API GC_word GC_CALL GC_get_non_gc_bytes(void)
2509 return GC_non_gc_bytes;
2512 GC_API void GC_CALL GC_set_free_space_divisor(GC_word value)
2514 GC_ASSERT(value > 0);
2515 GC_free_space_divisor = value;
2518 GC_API GC_word GC_CALL GC_get_free_space_divisor(void)
2520 return GC_free_space_divisor;
2523 GC_API void GC_CALL GC_set_max_retries(GC_word value)
2525 GC_ASSERT((GC_signed_word)value != -1);
2526 /* -1 was used to retrieve old value in gc-7.2 */
2527 GC_max_retries = value;
2530 GC_API GC_word GC_CALL GC_get_max_retries(void)
2532 return GC_max_retries;
2535 GC_API void GC_CALL GC_set_dont_precollect(int value)
2537 GC_ASSERT(value != -1); /* -1 was used to retrieve old value in gc-7.2 */
2538 /* value is of boolean type. */
2539 GC_dont_precollect = value;
2542 GC_API int GC_CALL GC_get_dont_precollect(void)
2544 return GC_dont_precollect;
2547 GC_API void GC_CALL GC_set_full_freq(int value)
2549 GC_ASSERT(value >= 0);
2550 GC_full_freq = value;
2553 GC_API int GC_CALL GC_get_full_freq(void)
2555 return GC_full_freq;
2558 GC_API void GC_CALL GC_set_time_limit(unsigned long value)
2560 GC_ASSERT((long)value != -1L);
2561 /* -1 was used to retrieve old value in gc-7.2 */
2562 GC_time_limit = value;
2565 GC_API unsigned long GC_CALL GC_get_time_limit(void)
2567 return GC_time_limit;
2570 GC_API void GC_CALL GC_set_force_unmap_on_gcollect(int value)
2572 GC_force_unmap_on_gcollect = (GC_bool)value;
2575 GC_API int GC_CALL GC_get_force_unmap_on_gcollect(void)
2577 return (int)GC_force_unmap_on_gcollect;
2580 GC_API void GC_CALL GC_abort_on_oom(void)
2582 GC_err_printf("Insufficient memory for the allocation\n");
2587 GC_API void GC_CALL GC_stop_world_external(void)
2589 GC_ASSERT(GC_is_initialized);
2591 # ifdef THREAD_LOCAL_ALLOC
2592 GC_ASSERT(!GC_world_stopped);
2595 # ifdef THREAD_LOCAL_ALLOC
2596 GC_world_stopped = TRUE;
2600 GC_API void GC_CALL GC_start_world_external(void)
2602 # ifdef THREAD_LOCAL_ALLOC
2603 GC_ASSERT(GC_world_stopped);
2604 GC_world_stopped = FALSE;
2606 GC_ASSERT(GC_is_initialized);
2611 #endif /* THREADS */