Remove a note in README.macros that work in progress
[platform/upstream/libgc.git] / misc.c
1 /*
2  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
4  * Copyright (c) 1999-2001 by Hewlett-Packard Company. All rights reserved.
5  *
6  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
8  *
9  * Permission is hereby granted to use or copy this program
10  * for any purpose,  provided the above notices are retained on all copies.
11  * Permission to modify the code and to distribute modified code is granted,
12  * provided the above notices are retained, and a notice that the code was
13  * modified is included with the above copyright notice.
14  */
15
16 #include "private/gc_pmark.h"
17
18 #include <stdio.h>
19 #include <limits.h>
20 #include <stdarg.h>
21
22 #ifndef MSWINCE
23 # include <signal.h>
24 #endif
25
26 #ifdef GC_SOLARIS_THREADS
27 # include <sys/syscall.h>
28 #endif
29
30 #if defined(UNIX_LIKE) || defined(CYGWIN32) || defined(SYMBIAN) \
31     || (defined(CONSOLE_LOG) && defined(MSWIN32))
32 # include <fcntl.h>
33 # include <sys/types.h>
34 # include <sys/stat.h>
35 #endif
36
37 #ifdef NONSTOP
38 # include <floss.h>
39 #endif
40
41 #ifdef THREADS
42 # ifdef PCR
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)
48 #   include <pthread.h>
49     GC_INNER pthread_mutex_t GC_allocate_ml;
50 # endif
51   /* For other platforms with threads, the lock and possibly            */
52   /* GC_lock_holder variables are defined in the thread support code.   */
53 #endif /* THREADS */
54
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
61 #else
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
65 #endif
66
67 #ifdef NEED_CANCEL_DISABLE_COUNT
68   __thread unsigned char GC_cancel_disable_count = 0;
69 #endif
70
71 GC_FAR struct _GC_arrays GC_arrays /* = { 0 } */;
72
73 GC_INNER GC_bool GC_debugging_started = FALSE;
74                 /* defined here so we don't have to load dbg_mlc.o */
75
76 ptr_t GC_stackbottom = 0;
77
78 #ifdef IA64
79   ptr_t GC_register_stackbottom = 0;
80 #endif
81
82 int GC_dont_gc = FALSE;
83
84 int GC_dont_precollect = FALSE;
85
86 GC_bool GC_quiet = 0; /* used also in pcr_interface.c */
87
88 #if !defined(NO_CLOCK) || !defined(SMALL_CONFIG)
89   int GC_print_stats = 0;
90 #endif
91
92 #ifdef GC_PRINT_BACK_HEIGHT
93   GC_INNER GC_bool GC_print_back_height = TRUE;
94 #else
95   GC_INNER GC_bool GC_print_back_height = FALSE;
96 #endif
97
98 #ifndef NO_DEBUGGING
99 # ifdef GC_DUMP_REGULARLY
100     GC_INNER GC_bool GC_dump_regularly = TRUE;
101                                 /* Generate regular debugging dumps. */
102 # else
103     GC_INNER GC_bool GC_dump_regularly = FALSE;
104 # endif
105 # ifndef NO_CLOCK
106     STATIC CLOCK_TYPE GC_init_time;
107                 /* The time that the GC was initialized at.     */
108 # endif
109 #endif /* !NO_DEBUGGING */
110
111 #ifdef KEEP_BACK_PTRS
112   GC_INNER long GC_backtraces = 0;
113                 /* Number of random backtraces to generate for each GC. */
114 #endif
115
116 #ifdef FIND_LEAK
117   int GC_find_leak = 1;
118 #else
119   int GC_find_leak = 0;
120 #endif
121
122 #ifndef SHORT_DBG_HDRS
123 # ifdef GC_FINDLEAK_DELAY_FREE
124     GC_INNER GC_bool GC_findleak_delay_free = TRUE;
125 # else
126     GC_INNER GC_bool GC_findleak_delay_free = FALSE;
127 # endif
128 #endif /* !SHORT_DBG_HDRS */
129
130 #ifdef ALL_INTERIOR_POINTERS
131   int GC_all_interior_pointers = 1;
132 #else
133   int GC_all_interior_pointers = 0;
134 #endif
135
136 #ifdef FINALIZE_ON_DEMAND
137   int GC_finalize_on_demand = 1;
138 #else
139   int GC_finalize_on_demand = 0;
140 #endif
141
142 #ifdef JAVA_FINALIZATION
143   int GC_java_finalization = 1;
144 #else
145   int GC_java_finalization = 0;
146 #endif
147
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;
151
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;
156 #else
157   GC_INNER GC_bool GC_force_unmap_on_gcollect = FALSE;
158 #endif
159
160 #ifndef GC_LARGE_ALLOC_WARN_INTERVAL
161 # define GC_LARGE_ALLOC_WARN_INTERVAL 5
162 #endif
163 GC_INNER long GC_large_alloc_warn_interval = GC_LARGE_ALLOC_WARN_INTERVAL;
164                         /* Interval between unsuppressed warnings.      */
165
166 STATIC void * GC_CALLBACK GC_default_oom_fn(
167                                         size_t bytes_requested GC_ATTR_UNUSED)
168 {
169     return(0);
170 }
171
172 /* All accesses to it should be synchronized to avoid data races.       */
173 GC_oom_func GC_oom_fn = GC_default_oom_fn;
174
175 #ifdef CAN_HANDLE_FORK
176 # ifdef HANDLE_FORK
177     GC_INNER int GC_handle_fork = 1;
178                         /* The value is examined by GC_thr_init.        */
179 # else
180     GC_INNER int GC_handle_fork = FALSE;
181 # endif
182
183 #elif !defined(HAVE_NO_FORK)
184
185   /* Same as above but with GC_CALL calling conventions.  */
186   GC_API void GC_CALL GC_atfork_prepare(void)
187   {
188 #   ifdef THREADS
189       ABORT("fork() handling unsupported");
190 #   endif
191   }
192
193   GC_API void GC_CALL GC_atfork_parent(void)
194   {
195     /* empty */
196   }
197
198   GC_API void GC_CALL GC_atfork_child(void)
199   {
200     /* empty */
201   }
202 #endif /* !CAN_HANDLE_FORK && !HAVE_NO_FORK */
203
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)
207 {
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 */
216 #       ifndef THREADS
217           if (GC_manual_vdb)
218             return;
219 #       endif
220 #     endif
221       ABORT("fork() handling unsupported");
222     }
223 # else
224     /* No at-fork handler is needed in the single-threaded mode.        */
225 # endif
226 }
227
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)
234 {
235     size_t i;
236
237     /* Map size 0 to something bigger.                  */
238     /* This avoids problems at lower levels.            */
239       GC_size_map[0] = 1;
240     for (i = 1; i <= GRANULES_TO_BYTES(TINY_FREELISTS-1) - EXTRA_BYTES; i++) {
241         GC_size_map[i] = ROUNDED_UP_GRANULES(i);
242 #       ifndef _MSC_VER
243           GC_ASSERT(GC_size_map[i] < TINY_FREELISTS);
244           /* Seems to tickle bug in VC++ 2008 for AMD64 */
245 #       endif
246     }
247     /* We leave the rest of the array to be filled in on demand. */
248 }
249
250 /*
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.
257  */
258
259 #ifndef SMALL_CLEAR_SIZE
260 # define SMALL_CLEAR_SIZE 256   /* Clear this much every time.  */
261 #endif
262
263 #if defined(ALWAYS_SMALL_CLEAR_STACK) || defined(STACK_NOT_SCANNED)
264   GC_API void * GC_CALL GC_clear_stack(void *arg)
265   {
266 #   ifndef STACK_NOT_SCANNED
267       word volatile dummy[SMALL_CLEAR_SIZE];
268       BZERO((/* no volatile */ void *)dummy, sizeof(dummy));
269 #   endif
270     return arg;
271   }
272 #else
273
274 # ifdef THREADS
275 #   define BIG_CLEAR_SIZE 2048  /* Clear this much now and then.        */
276 # else
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
286 # endif
287
288 # if defined(ASM_CLEAR_CODE)
289     void *GC_clear_stack_inner(void *, ptr_t);
290 # else
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 */
297 #                           endif
298                                ptr_t limit)
299     {
300 #     define CLEAR_SIZE 213 /* granularity */
301       volatile word dummy[CLEAR_SIZE];
302
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);
306       }
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)
310         GC_noop1(dummy[0]);
311 #     else
312         GC_noop1(COVERT_DATAFLOW(dummy));
313 #     endif
314       return(arg);
315     }
316 # endif /* !ASM_CLEAR_CODE */
317
318 # ifdef THREADS
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)
323     {
324       static unsigned random_no = 0;
325       return ++random_no % 13;
326     }
327 # endif /* THREADS */
328
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  */
331 /* another frame.                                                       */
332   GC_API void * GC_CALL GC_clear_stack(void *arg)
333   {
334     ptr_t sp = GC_approx_sp();  /* Hotter than actual sp */
335 #   ifdef THREADS
336         word volatile dummy[SMALL_CLEAR_SIZE];
337 #   endif
338
339 #   define SLOP 400
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        */
353         /* larger ...                                                   */
354 #   ifdef THREADS
355       if (next_random_no() == 0) {
356         ptr_t limit = sp;
357
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);
363       }
364       BZERO((void *)dummy, SMALL_CLEAR_SIZE*sizeof(word));
365 #   else
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;
373       }
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) {
377           GC_high_water = sp;
378       }
379       MAKE_HOTTER(GC_high_water, GC_SLOP);
380       {
381         ptr_t limit = GC_min_sp;
382
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.   */
388           GC_min_sp = sp;
389           return GC_clear_stack_inner(arg, limit);
390         }
391       }
392       if (GC_bytes_allocd - GC_bytes_allocd_at_reset > CLEAR_THRESHOLD) {
393         /* Restart clearing process, but limit how much clearing we do. */
394         GC_min_sp = sp;
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;
399       }
400 #   endif
401     return arg;
402   }
403
404 #endif /* !ALWAYS_SMALL_CLEAR_STACK && !STACK_NOT_SCANNED */
405
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)
409 {
410     ptr_t r;
411     struct hblk *h;
412     bottom_index *bi;
413     hdr *candidate_hdr;
414
415     r = (ptr_t)p;
416     if (!EXPECT(GC_is_initialized, TRUE)) return 0;
417     h = HBLKPTR(r);
418     GET_BI(r, bi);
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);
425            r = (ptr_t)h;
426            candidate_hdr = HDR(h);
427         }
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));
431         {
432             size_t offset = HBLKDISPL(r);
433             word sz = candidate_hdr -> hb_sz;
434             size_t obj_displ = offset % sz;
435             ptr_t limit;
436
437             r -= obj_displ;
438             limit = r + sz;
439             if ((word)limit > (word)(h + 1) && sz <= HBLKSIZE) {
440                 return(0);
441             }
442             if ((word)p >= (word)limit) return(0);
443         }
444     return((void *)r);
445 }
446
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)
449 {
450     bottom_index *bi;
451
452     GC_ASSERT(GC_is_initialized);
453     GET_BI(p, bi);
454     return HDR_FROM_BI(bi, p) != 0;
455 }
456
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)
461 {
462     hdr * hhdr = HDR(p);
463
464     return (size_t)hhdr->hb_sz;
465 }
466
467
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)
472 {
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);
476 }
477
478 GC_API size_t GC_CALL GC_get_free_bytes(void)
479 {
480     /* ignore the memory space returned to OS */
481     return (size_t)(GC_large_free_bytes - GC_unmapped_bytes);
482 }
483
484 GC_API size_t GC_CALL GC_get_unmapped_bytes(void)
485 {
486     return (size_t)GC_unmapped_bytes;
487 }
488
489 GC_API size_t GC_CALL GC_get_bytes_since_gc(void)
490 {
491     return (size_t)GC_bytes_allocd;
492 }
493
494 GC_API size_t GC_CALL GC_get_total_bytes(void)
495 {
496     return (size_t)(GC_bytes_allocd + GC_bytes_allocd_before_gc);
497 }
498
499 #ifndef GC_GET_HEAP_USAGE_NOT_NEEDED
500
501 GC_API size_t GC_CALL GC_get_size_map_at(int i)
502 {
503   if ((unsigned)i > MAXOBJBYTES)
504     return GC_SIZE_MAX;
505   return GRANULES_TO_BYTES(GC_size_map[i]);
506 }
507
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)
514 {
515   DCL_LOCK_STATE;
516
517   LOCK();
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;
528   UNLOCK();
529 }
530
531   GC_INNER word GC_reclaimed_bytes_before_gc = 0;
532
533   /* Fill in GC statistics provided the destination is of enough size.  */
534   static void fill_prof_stats(struct GC_prof_stats_s *pstats)
535   {
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);
545 #   else
546       pstats->markers_m1 = 0; /* one marker */
547 #   endif
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 */
552   }
553
554 # include <string.h> /* for memset() */
555
556   GC_API size_t GC_CALL GC_get_prof_stats(struct GC_prof_stats_s *pstats,
557                                           size_t stats_sz)
558   {
559     struct GC_prof_stats_s stats;
560     DCL_LOCK_STATE;
561
562     LOCK();
563     fill_prof_stats(stats_sz >= sizeof(stats) ? pstats : &stats);
564     UNLOCK();
565
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);
572     } else {
573       if (EXPECT(stats_sz > 0, TRUE))
574         BCOPY(&stats, pstats, stats_sz);
575       return stats_sz;
576     }
577   }
578
579 # ifdef THREADS
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,
583                                             size_t stats_sz)
584     {
585       struct GC_prof_stats_s stats;
586
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);
593       } else {
594         if (EXPECT(stats_sz > 0, TRUE)) {
595           fill_prof_stats(&stats);
596           BCOPY(&stats, pstats, stats_sz);
597         }
598         return stats_sz;
599       }
600     }
601 # endif /* THREADS */
602
603 #endif /* !GC_GET_HEAP_USAGE_NOT_NEEDED */
604
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)
609   {
610     /* empty */
611   }
612
613   GC_API void GC_CALL GC_set_thr_restart_signal(int sig GC_ATTR_UNUSED)
614   {
615     /* empty */
616   }
617
618   GC_API int GC_CALL GC_get_suspend_signal(void)
619   {
620     return -1;
621   }
622
623   GC_API int GC_CALL GC_get_thr_restart_signal(void)
624   {
625     return -1;
626   }
627 #endif /* GC_DARWIN_THREADS || GC_WIN32_THREADS || ... */
628
629 #if !defined(_MAX_PATH) && (defined(MSWIN32) || defined(MSWINCE) \
630                             || defined(CYGWIN32))
631 # define _MAX_PATH MAX_PATH
632 #endif
633
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    */
640                         /* with '\0'.                                   */
641   STATIC unsigned GC_envfile_length = 0;
642                         /* Length of GC_envfile_content (if non-NULL).  */
643
644 # ifndef GC_ENVFILE_MAXLEN
645 #   define GC_ENVFILE_MAXLEN 0x4000
646 # endif
647
648 # define GC_ENV_FILE_EXT ".gc.env"
649
650   /* The routine initializes GC_envfile_content from the GC "env" file. */
651   STATIC void GC_envfile_init(void)
652   {
653 #   if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
654       HANDLE hFile;
655       char *content;
656       unsigned ofs;
657       unsigned len;
658       DWORD nBytesRead;
659       TCHAR path[_MAX_PATH + 0x10]; /* buffer for path + ext */
660       len = (unsigned)GetModuleFileName(NULL /* hModule */, path,
661                                         _MAX_PATH + 1);
662       /* If GetModuleFileName() has failed then len is 0. */
663       if (len > 4 && path[len - 4] == (TCHAR)'.') {
664         len -= 4; /* strip executable file extension */
665       }
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) {
675         CloseHandle(hFile);
676         return; /* invalid file length - ignoring the file content */
677       }
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) {
682         CloseHandle(hFile);
683         return; /* allocation failure */
684       }
685       ofs = 0;
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)
691           break;
692       }
693       CloseHandle(hFile);
694       if (ofs != len || nBytesRead != 0)
695         return; /* read operation is failed - ignoring the file content */
696       content[ofs] = '\0';
697       while (ofs-- > 0) {
698        if (content[ofs] == '\r' || content[ofs] == '\n')
699          content[ofs] = '\0';
700       }
701       GC_ASSERT(NULL == GC_envfile_content);
702       GC_envfile_length = len + 1;
703       GC_envfile_content = content;
704 #   endif
705   }
706
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)
710   {
711     char *p;
712     char *end_of_content;
713     unsigned namelen;
714 #   ifndef NO_GETENV
715       p = getenv(name); /* try the standard getenv() first */
716       if (p != NULL)
717         return *p != '\0' ? p : NULL;
718 #   endif
719     p = GC_envfile_content;
720     if (p == NULL)
721       return NULL; /* "env" file is absent (or empty) */
722     namelen = strlen(name);
723     if (namelen == 0) /* a sanity check */
724       return NULL;
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;
730       }
731       /* If not matching then skip to the next line. */
732     }
733     return NULL; /* no match found */
734   }
735 #endif /* GC_READ_ENV_FILE */
736
737 GC_INNER GC_bool GC_is_initialized = FALSE;
738
739 GC_API int GC_CALL GC_is_init_called(void)
740 {
741   return GC_is_initialized;
742 }
743
744 #if defined(GC_WIN32_THREADS) \
745     && ((defined(MSWIN32) && !defined(CONSOLE_LOG)) || defined(MSWINCE))
746   GC_INNER CRITICAL_SECTION GC_write_cs;
747 #endif
748
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;
756 # else
757 #   define skip_gc_atexit FALSE
758 # endif
759
760   STATIC void GC_exit_check(void)
761   {
762     if (GC_find_leak && !skip_gc_atexit) {
763 #     ifdef THREADS
764         GC_in_thread_creation = TRUE; /* OK to collect from unknown thread. */
765         GC_gcollect();
766         GC_in_thread_creation = FALSE;
767 #     else
768         GC_gcollect();
769 #     endif
770     }
771   }
772 #endif
773
774 #if defined(UNIX_LIKE) && !defined(NO_DEBUGGING)
775   static void looping_handler(int sig)
776   {
777     GC_err_printf("Caught signal %d: looping in handler\n", sig);
778     for (;;) {
779        /* empty */
780     }
781   }
782
783   static GC_bool installed_looping_handler = FALSE;
784
785   static void maybe_install_looping_handler(void)
786   {
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;
792     }
793   }
794
795 #else /* !UNIX_LIKE */
796 # define maybe_install_looping_handler()
797 #endif
798
799 #define GC_DEFAULT_STDOUT_FD 1
800 #define GC_DEFAULT_STDERR_FD 2
801
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;
808
809 # ifndef MSWIN32
810     GC_API void GC_CALL GC_set_log_fd(int fd)
811     {
812       GC_log = fd;
813     }
814 # endif
815 #endif
816
817 #ifdef MSGBOX_ON_ERROR
818   STATIC void GC_win32_MessageBoxA(const char *msg, const char *caption,
819                                    unsigned flags)
820   {
821 #   ifndef DONT_USE_USER32_DLL
822       /* Use static binding to "user32.dll".    */
823       (void)MessageBoxA(NULL, msg, caption, flags);
824 #   else
825       /* This simplifies linking - resolve "MessageBoxA" at run-time. */
826       HINSTANCE hU32 = LoadLibrary(TEXT("user32.dll"));
827       if (hU32) {
828         FARPROC pfn = GetProcAddress(hU32, "MessageBoxA");
829         if (pfn)
830           (void)(*(int (WINAPI *)(HWND, LPCSTR, LPCSTR, UINT))pfn)(
831                               NULL /* hWnd */, msg, caption, flags);
832         (void)FreeLibrary(hU32);
833       }
834 #   endif
835   }
836 #endif /* MSGBOX_ON_ERROR */
837
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) {}
841 #endif
842
843 #ifndef SMALL_CONFIG
844 # ifdef MANUAL_VDB
845     static GC_bool manual_vdb_allowed = TRUE;
846 # else
847     static GC_bool manual_vdb_allowed = FALSE;
848 # endif
849
850   GC_API void GC_CALL GC_set_manual_vdb_allowed(int value)
851   {
852     manual_vdb_allowed = (GC_bool)value;
853   }
854
855   GC_API int GC_CALL GC_get_manual_vdb_allowed(void)
856   {
857     return (int)manual_vdb_allowed;
858   }
859 #endif /* !SMALL_CONFIG */
860
861 STATIC word GC_parse_mem_size_arg(const char *str)
862 {
863   word result = 0; /* bad value */
864
865   if (*str != '\0') {
866     char *endptr;
867     char ch;
868
869     result = (word)STRTOULL(str, &endptr, 10);
870     ch = *endptr;
871     if (ch != '\0') {
872       if (*(endptr + 1) != '\0')
873         return 0;
874       /* Allow k, M or G suffix. */
875       switch (ch) {
876       case 'K':
877       case 'k':
878         result <<= 10;
879         break;
880       case 'M':
881       case 'm':
882         result <<= 20;
883         break;
884       case 'G':
885       case 'g':
886         result <<= 30;
887         break;
888       default:
889         result = 0;
890       }
891     }
892   }
893   return result;
894 }
895
896 #define GC_LOG_STD_NAME "gc.log"
897
898 GC_API void GC_CALL GC_init(void)
899 {
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)
904       DCL_LOCK_STATE;
905 #   endif
906
907     if (EXPECT(GC_is_initialized, TRUE)) return;
908 #   ifdef REDIRECT_MALLOC
909       {
910         static GC_bool init_started = FALSE;
911         if (init_started)
912           ABORT("Redirected malloc() called during GC init");
913         init_started = TRUE;
914       }
915 #   endif
916
917 #   if defined(GC_INITIAL_HEAP_SIZE) && !defined(CPPCHECK)
918       initial_heap_sz = GC_INITIAL_HEAP_SIZE;
919 #   else
920       initial_heap_sz = MINHINCR * HBLKSIZE;
921 #   endif
922
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.                */
930 #   ifdef THREADS
931 #     ifndef GC_ALWAYS_MULTITHREADED
932         GC_ASSERT(!GC_need_to_lock);
933 #     endif
934 #     ifdef SN_TARGET_PS3
935         {
936           pthread_mutexattr_t mattr;
937
938           if (0 != pthread_mutexattr_init(&mattr)) {
939             ABORT("pthread_mutexattr_init failed");
940           }
941           if (0 != pthread_mutex_init(&GC_allocate_ml, &mattr)) {
942             ABORT("pthread_mutex_init failed");
943           }
944           (void)pthread_mutexattr_destroy(&mattr);
945         }
946 #     endif
947 #   endif /* THREADS */
948 #   if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
949 #     ifndef SPIN_COUNT
950 #       define SPIN_COUNT 4000
951 #     endif
952 #     ifdef MSWINRT_FLAVOR
953         InitializeCriticalSectionAndSpinCount(&GC_allocate_ml, SPIN_COUNT);
954 #     else
955         {
956 #         ifndef MSWINCE
957             BOOL (WINAPI *pfn)(LPCRITICAL_SECTION, DWORD) = 0;
958             HMODULE hK32 = GetModuleHandle(TEXT("kernel32.dll"));
959             if (hK32)
960               pfn = (BOOL (WINAPI *)(LPCRITICAL_SECTION, DWORD))
961                       GetProcAddress(hK32,
962                                      "InitializeCriticalSectionAndSpinCount");
963             if (pfn) {
964               pfn(&GC_allocate_ml, SPIN_COUNT);
965             } else
966 #         endif /* !MSWINCE */
967           /* else */ InitializeCriticalSection(&GC_allocate_ml);
968         }
969 #     endif
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);
974 #   endif
975     GC_setpagesize();
976 #   ifdef MSWIN32
977       GC_init_win32();
978 #   endif
979 #   ifdef GC_READ_ENV_FILE
980       GC_envfile_init();
981 #   endif
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;
987 #     else
988         if (0 != GETENV("GC_PRINT_VERBOSE_STATS")) {
989           GC_print_stats = VERBOSE;
990         } else if (0 != GETENV("GC_PRINT_STATS")) {
991           GC_print_stats = 1;
992         }
993 #     endif
994 #   endif
995 #   if ((defined(UNIX_LIKE) && !defined(GC_ANDROID_LOG)) \
996         || (defined(CONSOLE_LOG) && defined(MSWIN32)) \
997         || defined(CYGWIN32) || defined(SYMBIAN)) && !defined(SMALL_CONFIG)
998         {
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;
1003 #         else
1004             if (0 != file_name)
1005 #         endif
1006           {
1007             int log_d = open(file_name, O_CREAT | O_WRONLY | O_APPEND, 0644);
1008             if (log_d < 0) {
1009               GC_err_printf("Failed to open %s as log file\n", file_name);
1010             } else {
1011               char *str;
1012               GC_log = log_d;
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')
1018 #             else
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'))
1023 #             endif
1024               {
1025                 GC_stdout = log_d;
1026                 GC_stderr = log_d;
1027               }
1028             }
1029           }
1030         }
1031 #   endif
1032 #   if !defined(NO_DEBUGGING) && !defined(GC_DUMP_REGULARLY)
1033       if (0 != GETENV("GC_DUMP_REGULARLY")) {
1034         GC_dump_regularly = TRUE;
1035       }
1036 #   endif
1037 #   ifdef KEEP_BACK_PTRS
1038       {
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;
1043         }
1044       }
1045 #   endif
1046     if (0 != GETENV("GC_FIND_LEAK")) {
1047       GC_find_leak = 1;
1048     }
1049 #   ifndef SHORT_DBG_HDRS
1050       if (0 != GETENV("GC_FINDLEAK_DELAY_FREE")) {
1051         GC_findleak_delay_free = TRUE;
1052       }
1053 #   endif
1054     if (0 != GETENV("GC_ALL_INTERIOR_POINTERS")) {
1055       GC_all_interior_pointers = 1;
1056     }
1057     if (0 != GETENV("GC_DONT_GC")) {
1058       GC_dont_gc = 1;
1059     }
1060     if (0 != GETENV("GC_PRINT_BACK_HEIGHT")) {
1061       GC_print_back_height = TRUE;
1062     }
1063     if (0 != GETENV("GC_NO_BLACKLIST_WARNING")) {
1064       GC_large_alloc_warn_interval = LONG_MAX;
1065     }
1066     {
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);
1071 #       else
1072           word addr = (word)STRTOULL(addr_string, NULL, 16);
1073           if (addr < 0x1000)
1074               WARN("Unlikely trace address: %p\n", (void *)addr);
1075           GC_trace_addr = (ptr_t)addr;
1076 #       endif
1077       }
1078     }
1079 #   ifdef GC_COLLECT_AT_MALLOC
1080       {
1081         char * string = GETENV("GC_COLLECT_AT_MALLOC");
1082         if (0 != string) {
1083           size_t min_lb = (size_t)STRTOULL(string, NULL, 10);
1084           if (min_lb > 0)
1085             GC_dbg_collect_at_malloc_min_lb = min_lb;
1086         }
1087       }
1088 #   endif
1089 #   if !defined(GC_DISABLE_INCREMENTAL) && !defined(NO_CLOCK)
1090       {
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;
1096           }
1097         }
1098       }
1099 #   endif
1100 #   ifndef SMALL_CONFIG
1101       {
1102         char * full_freq_string = GETENV("GC_FULL_FREQUENCY");
1103         if (full_freq_string != NULL) {
1104           int full_freq = atoi(full_freq_string);
1105           if (full_freq > 0)
1106             GC_full_freq = full_freq;
1107         }
1108       }
1109 #   endif
1110     {
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);
1117         } else {
1118           GC_large_alloc_warn_interval = interval;
1119         }
1120       }
1121     }
1122     {
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;
1128         }
1129     }
1130 #   ifdef USE_MUNMAP
1131       {
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;
1137           } else {
1138             int unmap_threshold = atoi(string);
1139             if (unmap_threshold > 0)
1140               GC_unmap_threshold = unmap_threshold;
1141           }
1142         }
1143       }
1144       {
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;
1150           } else {
1151             GC_force_unmap_on_gcollect = TRUE;
1152           }
1153         }
1154       }
1155       {
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;
1161           } else {
1162             GC_use_entire_heap = TRUE;
1163           }
1164         }
1165       }
1166 #   endif
1167 #   if !defined(NO_DEBUGGING) && !defined(NO_CLOCK)
1168       GET_TIME(GC_init_time);
1169 #   endif
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;
1175 #   endif
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);
1181 #   endif
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.                */
1187 #   endif
1188 #   if defined(SEARCH_FOR_DATA_START)
1189         GC_init_linux_data_start();
1190 #   endif
1191 #   if defined(NETBSD) && defined(__ELF__)
1192         GC_init_netbsd_elf();
1193 #   endif
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();
1201 #       endif
1202       } else {
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();
1210           }
1211 #       endif
1212       }
1213 #   endif
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 */
1220 #     endif
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);
1224 #   endif
1225     GC_STATIC_ASSERT(sizeof (struct hblk) == HBLKSIZE);
1226 #   ifndef THREADS
1227       GC_ASSERT(!((word)GC_stackbottom HOTTER_THAN (word)GC_approx_sp()));
1228 #   endif
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. */
1235 #       else
1236           if (manual_vdb_allowed) {
1237               GC_manual_vdb = TRUE;
1238               GC_incremental = TRUE;
1239           } else
1240 #       endif
1241         /* else */ {
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);
1246         }
1247       }
1248 #   endif
1249
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();
1253     GC_init_headers();
1254     GC_bl_init();
1255     GC_mark_init();
1256     {
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);
1262           }
1263         }
1264     }
1265     {
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);
1271           }
1272           if (0 == GC_max_retries) GC_max_retries = 2;
1273           GC_set_max_heap_size(max_heap_sz);
1274         }
1275     }
1276 #   if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
1277         LOCK(); /* just to set GC_lock_holder */
1278 #   endif
1279     if (!GC_expand_hp_inner(divHBLKSZ(initial_heap_sz))) {
1280         GC_err_printf("Can't start up: not enough memory\n");
1281         EXIT();
1282     } else {
1283         GC_requested_heapsize += initial_heap_sz;
1284     }
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 *));
1292       }
1293 #   endif
1294     GC_init_size_map();
1295 #   ifdef PCR
1296       if (PCR_IL_Lock(PCR_Bool_false, PCR_allSigsBlocked, PCR_waitForever)
1297           != PCR_ERes_okay) {
1298           ABORT("Can't lock load state");
1299       } else if (PCR_IL_Unlock() != PCR_ERes_okay) {
1300           ABORT("Can't unlock load state");
1301       }
1302       PCR_IL_Unlock();
1303       GC_pcr_install();
1304 #   endif
1305     GC_is_initialized = TRUE;
1306 #   if defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
1307         GC_thr_init();
1308 #       ifdef PARALLEL_MARK
1309           /* Actually start helper threads.     */
1310 #         if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
1311             UNLOCK();
1312 #         endif
1313           GC_start_mark_threads_inner();
1314 #         if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
1315             LOCK();
1316 #         endif
1317 #       endif
1318 #   endif
1319     COND_DUMP;
1320     /* Get black list set up and/or incremental GC started */
1321     if (!GC_dont_precollect || GC_incremental) {
1322         GC_gcollect_inner();
1323     }
1324 #   if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
1325         UNLOCK();
1326 #   endif
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);
1331 #   endif
1332 #   ifndef DONT_USE_ATEXIT
1333       if (GC_find_leak) {
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);
1337       }
1338 #   endif
1339
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.                         */
1348         GC_init_parallel();
1349 #   endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
1350
1351 #   if defined(DYNAMIC_LOADING) && defined(DARWIN)
1352         /* This must be called WITHOUT the allocation lock held */
1353         /* and before any threads are created.                  */
1354         GC_init_dyld();
1355 #   endif
1356     RESTORE_CANCEL(cancel_state);
1357 }
1358
1359 GC_API void GC_CALL GC_enable_incremental(void)
1360 {
1361 # if !defined(GC_DISABLE_INCREMENTAL) && !defined(KEEP_BACK_PTRS)
1362     DCL_LOCK_STATE;
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")) {
1367       LOCK();
1368       if (!GC_incremental) {
1369         GC_setpagesize();
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) {
1373           UNLOCK();
1374           GC_incremental = TRUE; /* indicate intention to turn it on */
1375           GC_init();
1376           LOCK();
1377         } else {
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;
1384             } else
1385 #         endif
1386           /* else */ {
1387             GC_incremental = GC_dirty_init();
1388           }
1389         }
1390         if (GC_incremental && !GC_dont_gc) {
1391                                 /* Can't easily do it if GC_dont_gc.    */
1392           IF_CANCEL(int cancel_state;)
1393
1394           DISABLE_CANCEL(cancel_state);
1395           if (GC_bytes_allocd > 0) {
1396             /* There may be unmarked reachable objects. */
1397             GC_gcollect_inner();
1398           }
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);
1404         }
1405       }
1406       UNLOCK();
1407       return;
1408     }
1409 # endif
1410   GC_init();
1411 }
1412
1413 #if defined(THREADS)
1414   GC_API void GC_CALL GC_start_mark_threads(void)
1415   {
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;)
1420
1421       DISABLE_CANCEL(cancel_state);
1422       GC_start_mark_threads_inner();
1423       RESTORE_CANCEL(cancel_state);
1424 #   else
1425       /* No action since parallel markers are disabled (or no POSIX fork). */
1426       GC_ASSERT(I_DONT_HOLD_LOCK());
1427 #   endif
1428   }
1429 #endif
1430
1431   GC_API void GC_CALL GC_deinit(void)
1432   {
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);
1439 #       endif
1440         DeleteCriticalSection(&GC_allocate_ml);
1441 #     endif
1442     }
1443   }
1444
1445 #if (defined(MSWIN32) && !defined(CONSOLE_LOG)) || defined(MSWINCE)
1446
1447 # if defined(_MSC_VER) && defined(_DEBUG) && !defined(MSWINCE)
1448 #   include <crtdbg.h>
1449 # endif
1450
1451   STATIC HANDLE GC_log = 0;
1452
1453 # ifdef THREADS
1454 #   if defined(PARALLEL_MARK) && !defined(GC_ALWAYS_MULTITHREADED)
1455 #     define IF_NEED_TO_LOCK(x) if (GC_parallel || GC_need_to_lock) x
1456 #   else
1457 #     define IF_NEED_TO_LOCK(x) if (GC_need_to_lock) x
1458 #   endif
1459 # else
1460 #   define IF_NEED_TO_LOCK(x)
1461 # endif /* !THREADS */
1462
1463 # ifdef MSWINRT_FLAVOR
1464 #   include <windows.storage.h>
1465
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);
1471
1472     static GC_bool getWinRTLogPath(wchar_t* buf, size_t bufLen)
1473     {
1474       static const GUID kIID_IApplicationDataStatics = {
1475         0x5612147B, 0xE843, 0x45E3,
1476         0x94, 0xD8, 0x06, 0x16, 0x9E, 0x3C, 0x8E, 0x17
1477       };
1478       static const GUID kIID_IStorageItem = {
1479         0x4207A996, 0xCA2F, 0x42F7,
1480         0xBD, 0xE8, 0x8B, 0x10, 0x45, 0x7A, 0x7F, 0x30
1481       };
1482       GC_bool result = FALSE;
1483       HSTRING_HEADER appDataClassNameHeader;
1484       HSTRING appDataClassName;
1485       __x_ABI_CWindows_CStorage_CIApplicationDataStatics* appDataStatics = 0;
1486
1487       GC_ASSERT(bufLen > 0);
1488       if (SUCCEEDED(WindowsCreateStringReference(
1489                       RuntimeClass_Windows_Storage_ApplicationData,
1490                       (sizeof(RuntimeClass_Windows_Storage_ApplicationData)-1)
1491                         / sizeof(wchar_t),
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;
1500
1501         if (SUCCEEDED(appDataStatics->lpVtbl->get_Current(appDataStatics,
1502                                                           &appData))
1503             && SUCCEEDED(appData->lpVtbl->get_TemporaryFolder(appData,
1504                                                               &tempFolder))
1505             && SUCCEEDED(tempFolder->lpVtbl->QueryInterface(tempFolder,
1506                                                         &kIID_IStorageItem,
1507                                                         &tempFolderItem))
1508             && SUCCEEDED(tempFolderItem->lpVtbl->get_Path(tempFolderItem,
1509                                                           &tempPath))) {
1510           UINT32 tempPathLen;
1511           const wchar_t* tempPathBuf =
1512                           WindowsGetStringRawBuffer(tempPath, &tempPathLen);
1513
1514           buf[0] = '\0';
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)
1518             result = TRUE;
1519           WindowsDeleteString(tempPath);
1520         }
1521
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);
1529       }
1530       return result;
1531     }
1532 # endif /* MSWINRT_FLAVOR */
1533
1534   STATIC HANDLE GC_CreateLogFile(void)
1535   {
1536     HANDLE hFile;
1537 # ifdef MSWINRT_FLAVOR
1538       TCHAR pathBuf[_MAX_PATH + 0x10]; /* buffer for path + ext */
1539
1540       hFile = INVALID_HANDLE_VALUE;
1541       if (getWinRTLogPath(pathBuf, _MAX_PATH + 1)) {
1542         CREATEFILE2_EXTENDED_PARAMETERS extParams;
1543
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);
1551       }
1552
1553 # else
1554     TCHAR *logPath;
1555 #   if defined(NO_GETENV_WIN32) && defined(CPPCHECK)
1556 #     define appendToFile FALSE
1557 #   else
1558       BOOL appendToFile = FALSE;
1559 #   endif
1560 #   if !defined(NO_GETENV_WIN32) || !defined(OLD_WIN32_LOG_FILE)
1561       TCHAR pathBuf[_MAX_PATH + 0x10]; /* buffer for path + ext */
1562
1563       logPath = pathBuf;
1564 #   endif
1565
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;
1571       } else
1572 #   endif
1573     /* else */ {
1574       /* Env var not found or its value too long.       */
1575 #     ifdef OLD_WIN32_LOG_FILE
1576         logPath = TEXT(GC_LOG_STD_NAME);
1577 #     else
1578         int len = (int)GetModuleFileName(NULL /* hModule */, pathBuf,
1579                                          _MAX_PATH + 1);
1580         /* If GetModuleFileName() has failed then len is 0. */
1581         if (len > 4 && pathBuf[len - 4] == (TCHAR)'.') {
1582           len -= 4; /* strip executable file extension */
1583         }
1584         BCOPY(TEXT(".") TEXT(GC_LOG_STD_NAME), &pathBuf[len],
1585               sizeof(TEXT(".") TEXT(GC_LOG_STD_NAME)));
1586 #     endif
1587     }
1588
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 */);
1596
1597 #   ifndef NO_GETENV_WIN32
1598       if (appendToFile && hFile != INVALID_HANDLE_VALUE) {
1599         LONG posHigh = 0;
1600         (void)SetFilePointer(hFile, 0, &posHigh, FILE_END);
1601                                   /* Seek to file end (ignoring any error) */
1602       }
1603 #   endif
1604 #   undef appendToFile
1605 # endif
1606     return hFile;
1607   }
1608
1609   STATIC int GC_write(const char *buf, size_t len)
1610   {
1611       BOOL res;
1612       DWORD written;
1613 #     if defined(THREADS) && defined(GC_ASSERTIONS)
1614         static GC_bool inside_write = FALSE;
1615                         /* to prevent infinite recursion at abort.      */
1616         if (inside_write)
1617           return -1;
1618 #     endif
1619
1620       if (len == 0)
1621           return 0;
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");
1627         }
1628 #     endif
1629       if (GC_log == 0) {
1630         GC_log = GC_CreateLogFile();
1631       }
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).               */
1637           return 0;
1638 #       else
1639           return -1;
1640 #       endif
1641       }
1642       res = WriteFile(GC_log, buf, (DWORD)len, &written, NULL);
1643 #     if defined(_MSC_VER) && defined(_DEBUG) && !defined(NO_CRT)
1644 #         ifdef MSWINCE
1645               /* There is no CrtDbgReport() in WinCE */
1646               {
1647                   WCHAR wbuf[1024];
1648                   /* Always use Unicode variant of OutputDebugString() */
1649                   wbuf[MultiByteToWideChar(CP_ACP, 0 /* dwFlags */,
1650                                 buf, len, wbuf,
1651                                 sizeof(wbuf) / sizeof(wbuf[0]) - 1)] = 0;
1652                   OutputDebugStringW(wbuf);
1653               }
1654 #         else
1655               _CrtDbgReport(_CRT_WARN, NULL, 0, NULL, "%.*s", len, buf);
1656 #         endif
1657 #     endif
1658       IF_NEED_TO_LOCK(LeaveCriticalSection(&GC_write_cs));
1659       return res ? (int)written : -1;
1660   }
1661
1662   /* TODO: This is pretty ugly ... */
1663 # define WRITE(f, buf, len) GC_write(buf, len)
1664
1665 #elif defined(OS2) || defined(MACOS)
1666   STATIC FILE * GC_stdout = NULL;
1667   STATIC FILE * GC_stderr = NULL;
1668   STATIC FILE * GC_log = NULL;
1669
1670   /* Initialize GC_log (and the friends) passed to GC_write().  */
1671   STATIC void GC_set_files(void)
1672   {
1673     if (GC_stdout == NULL) {
1674       GC_stdout = stdout;
1675     }
1676     if (GC_stderr == NULL) {
1677       GC_stderr = stderr;
1678     }
1679     if (GC_log == NULL) {
1680       GC_log = stderr;
1681     }
1682   }
1683
1684   GC_INLINE int GC_write(FILE *f, const char *buf, size_t len)
1685   {
1686     int res = fwrite(buf, 1, len, f);
1687     fflush(f);
1688     return res;
1689   }
1690
1691 # define WRITE(f, buf, len) (GC_set_files(), GC_write(f, buf, len))
1692
1693 #elif defined(GC_ANDROID_LOG)
1694
1695 # include <android/log.h>
1696
1697 # ifndef GC_ANDROID_LOG_TAG
1698 #   define GC_ANDROID_LOG_TAG "BDWGC"
1699 # endif
1700
1701 # define GC_stdout ANDROID_LOG_DEBUG
1702 # define GC_stderr ANDROID_LOG_ERROR
1703 # define GC_log GC_stdout
1704
1705 # define WRITE(level, buf, unused_len) \
1706                 __android_log_write(level, GC_ANDROID_LOG_TAG, buf)
1707
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)
1711
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)
1715
1716 #else
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>
1721 # endif
1722
1723   STATIC int GC_write(int fd, const char *buf, size_t len)
1724   {
1725 #   if defined(ECOS) || defined(SN_TARGET_ORBIS) || defined(SN_TARGET_PSP2) \
1726        || defined(NOSYS)
1727 #     ifdef ECOS
1728         /* FIXME: This seems to be defined nowhere at present.  */
1729         /* _Jv_diag_write(buf, len); */
1730 #     else
1731         /* No writing.  */
1732 #     endif
1733       return len;
1734 #   else
1735       int bytes_written = 0;
1736       IF_CANCEL(int cancel_state;)
1737
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);
1743 #        else
1744              int result = write(fd, buf + bytes_written, len - bytes_written);
1745 #        endif
1746
1747          if (-1 == result) {
1748              RESTORE_CANCEL(cancel_state);
1749              return(result);
1750          }
1751          bytes_written += result;
1752       }
1753       RESTORE_CANCEL(cancel_state);
1754       return(bytes_written);
1755 #   endif
1756   }
1757
1758 # define WRITE(f, buf, len) GC_write(f, buf, len)
1759 #endif /* !MSWINCE && !OS2 && !MACOS && !GC_ANDROID_LOG */
1760
1761 #define BUFSZ 1024
1762
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)
1767 # ifdef MSWINCE
1768     /* _vsnprintf is deprecated in WinCE */
1769 #   define GC_VSNPRINTF StringCchVPrintfA
1770 # else
1771 #   define GC_VSNPRINTF _vsnprintf
1772 # endif
1773 #else
1774 # define GC_VSNPRINTF vsnprintf
1775 #endif
1776
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) \
1783         do { \
1784           va_list args; \
1785           va_start(args, format); \
1786           (buf)[sizeof(buf) - 1] = 0x15; /* guard */ \
1787           (void)GC_VSNPRINTF(buf, sizeof(buf) - 1, format, args); \
1788           va_end(args); \
1789           if ((buf)[sizeof(buf) - 1] != 0x15) \
1790             ABORT("GC_printf clobbered stack"); \
1791         } while (0)
1792
1793 void GC_printf(const char *format, ...)
1794 {
1795     if (!GC_quiet) {
1796       char buf[BUFSZ + 1];
1797
1798       GC_PRINTF_FILLBUF(buf, format);
1799 #     ifdef NACL
1800         (void)WRITE(GC_stdout, buf, strlen(buf));
1801         /* Ignore errors silently.      */
1802 #     else
1803         if (WRITE(GC_stdout, buf, strlen(buf)) < 0)
1804           ABORT("write to stdout failed");
1805 #     endif
1806     }
1807 }
1808
1809 void GC_err_printf(const char *format, ...)
1810 {
1811     char buf[BUFSZ + 1];
1812
1813     GC_PRINTF_FILLBUF(buf, format);
1814     GC_err_puts(buf);
1815 }
1816
1817 void GC_log_printf(const char *format, ...)
1818 {
1819     char buf[BUFSZ + 1];
1820
1821     GC_PRINTF_FILLBUF(buf, format);
1822 #   ifdef NACL
1823       (void)WRITE(GC_log, buf, strlen(buf));
1824 #   else
1825       if (WRITE(GC_log, buf, strlen(buf)) < 0)
1826         ABORT("write to GC log failed");
1827 #   endif
1828 }
1829
1830 #ifndef GC_ANDROID_LOG
1831
1832 # define GC_warn_printf GC_err_printf
1833
1834 #else
1835
1836   GC_INNER void GC_info_log_printf(const char *format, ...)
1837   {
1838     char buf[BUFSZ + 1];
1839
1840     GC_PRINTF_FILLBUF(buf, format);
1841     (void)WRITE(ANDROID_LOG_INFO, buf, 0 /* unused */);
1842   }
1843
1844   GC_INNER void GC_verbose_log_printf(const char *format, ...)
1845   {
1846     char buf[BUFSZ + 1];
1847
1848     GC_PRINTF_FILLBUF(buf, format);
1849     (void)WRITE(ANDROID_LOG_VERBOSE, buf, 0); /* ignore write errors */
1850   }
1851
1852   STATIC void GC_warn_printf(const char *format, ...)
1853   {
1854     char buf[BUFSZ + 1];
1855
1856     GC_PRINTF_FILLBUF(buf, format);
1857     (void)WRITE(ANDROID_LOG_WARN, buf, 0);
1858   }
1859
1860 #endif /* GC_ANDROID_LOG */
1861
1862 void GC_err_puts(const char *s)
1863 {
1864     (void)WRITE(GC_stderr, s, strlen(s)); /* ignore errors */
1865 }
1866
1867 STATIC void GC_CALLBACK GC_default_warn_proc(char *msg, GC_word arg)
1868 {
1869     /* TODO: Add assertion on arg comply with msg (format).     */
1870     GC_warn_printf(msg, arg);
1871 }
1872
1873 GC_INNER GC_warn_proc GC_current_warn_proc = GC_default_warn_proc;
1874
1875 /* This is recommended for production code (release). */
1876 GC_API void GC_CALLBACK GC_ignore_warn_proc(char *msg, GC_word arg)
1877 {
1878     if (GC_print_stats) {
1879       /* Don't ignore warnings if stats printing is on. */
1880       GC_default_warn_proc(msg, arg);
1881     }
1882 }
1883
1884 GC_API void GC_CALL GC_set_warn_proc(GC_warn_proc p)
1885 {
1886     DCL_LOCK_STATE;
1887     GC_ASSERT(NONNULL_ARG_NOT_NULL(p));
1888 #   ifdef GC_WIN32_THREADS
1889 #     ifdef CYGWIN32
1890         /* Need explicit GC_INIT call */
1891         GC_ASSERT(GC_is_initialized);
1892 #     else
1893         if (!GC_is_initialized) GC_init();
1894 #     endif
1895 #   endif
1896     LOCK();
1897     GC_current_warn_proc = p;
1898     UNLOCK();
1899 }
1900
1901 GC_API GC_warn_proc GC_CALL GC_get_warn_proc(void)
1902 {
1903     GC_warn_proc result;
1904     DCL_LOCK_STATE;
1905     LOCK();
1906     result = GC_current_warn_proc;
1907     UNLOCK();
1908     return(result);
1909 }
1910
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)
1916   {
1917 #   ifndef DONT_USE_ATEXIT
1918       skip_gc_atexit = TRUE; /* disable at-exit GC_gcollect() */
1919 #   endif
1920
1921     if (msg != NULL) {
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.   */
1925 #     endif
1926
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)
1934 #     endif
1935       {
1936         if (WRITE(GC_stderr, msg, strlen(msg)) >= 0)
1937           (void)WRITE(GC_stderr, "\n", 1);
1938       }
1939 #   else
1940       __android_log_assert("*" /* cond */, GC_ANDROID_LOG_TAG, "%s\n", msg);
1941 #   endif
1942     }
1943
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.                                           */
1950             for(;;) {
1951               /* Empty */
1952             }
1953       }
1954 #   endif
1955   }
1956
1957   GC_abort_func GC_on_abort = GC_default_on_abort;
1958
1959   GC_API void GC_CALL GC_set_abort_func(GC_abort_func fn)
1960   {
1961       DCL_LOCK_STATE;
1962       GC_ASSERT(NONNULL_ARG_NOT_NULL(fn));
1963       LOCK();
1964       GC_on_abort = fn;
1965       UNLOCK();
1966   }
1967
1968   GC_API GC_abort_func GC_CALL GC_get_abort_func(void)
1969   {
1970       GC_abort_func fn;
1971       DCL_LOCK_STATE;
1972       LOCK();
1973       fn = GC_on_abort;
1974       UNLOCK();
1975       return fn;
1976   }
1977 #endif /* !SMALL_CONFIG */
1978
1979 GC_API void GC_CALL GC_enable(void)
1980 {
1981     DCL_LOCK_STATE;
1982
1983     LOCK();
1984     GC_ASSERT(GC_dont_gc != 0); /* ensure no counter underflow */
1985     GC_dont_gc--;
1986     UNLOCK();
1987 }
1988
1989 GC_API void GC_CALL GC_disable(void)
1990 {
1991     DCL_LOCK_STATE;
1992     LOCK();
1993     GC_dont_gc++;
1994     UNLOCK();
1995 }
1996
1997 GC_API int GC_CALL GC_is_disabled(void)
1998 {
1999     return GC_dont_gc != 0;
2000 }
2001
2002 /* Helper procedures for new kind creation.     */
2003 GC_API void ** GC_CALL GC_new_free_list_inner(void)
2004 {
2005     void *result;
2006
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;
2012 }
2013
2014 GC_API void ** GC_CALL GC_new_free_list(void)
2015 {
2016     void ** result;
2017     DCL_LOCK_STATE;
2018     LOCK();
2019     result = GC_new_free_list_inner();
2020     UNLOCK();
2021     return result;
2022 }
2023
2024 GC_API unsigned GC_CALL GC_new_kind_inner(void **fl, GC_word descr,
2025                                           int adjust, int clear)
2026 {
2027     unsigned result = GC_n_kinds;
2028
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) {
2037       GC_n_kinds++;
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;
2046 #     endif
2047     } else {
2048       ABORT("Too many kinds");
2049     }
2050     return result;
2051 }
2052
2053 GC_API unsigned GC_CALL GC_new_kind(void **fl, GC_word descr, int adjust,
2054                                     int clear)
2055 {
2056     unsigned result;
2057     DCL_LOCK_STATE;
2058     LOCK();
2059     result = GC_new_kind_inner(fl, descr, adjust, clear);
2060     UNLOCK();
2061     return result;
2062 }
2063
2064 GC_API unsigned GC_CALL GC_new_proc_inner(GC_mark_proc proc)
2065 {
2066     unsigned result = GC_n_mark_procs;
2067
2068     if (result < MAX_MARK_PROCS) {
2069       GC_n_mark_procs++;
2070       GC_mark_procs[result] = proc;
2071     } else {
2072       ABORT("Too many mark procedures");
2073     }
2074     return result;
2075 }
2076
2077 GC_API unsigned GC_CALL GC_new_proc(GC_mark_proc proc)
2078 {
2079     unsigned result;
2080     DCL_LOCK_STATE;
2081     LOCK();
2082     result = GC_new_proc_inner(proc);
2083     UNLOCK();
2084     return result;
2085 }
2086
2087 GC_API void * GC_CALL GC_call_with_alloc_lock(GC_fn_type fn, void *client_data)
2088 {
2089     void * result;
2090     DCL_LOCK_STATE;
2091
2092 #   ifdef THREADS
2093       LOCK();
2094 #   endif
2095     result = (*fn)(client_data);
2096 #   ifdef THREADS
2097       UNLOCK();
2098 #   endif
2099     return(result);
2100 }
2101
2102 GC_API void * GC_CALL GC_call_with_stack_base(GC_stack_base_func fn, void *arg)
2103 {
2104     struct GC_stack_base base;
2105     void *result;
2106
2107     base.mem_base = (void *)&base;
2108 #   ifdef IA64
2109       base.reg_base = (void *)GC_save_regs_in_stack();
2110       /* Unnecessarily flushes register stack,          */
2111       /* but that probably doesn't hurt.                */
2112 #   endif
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));
2117     return result;
2118 }
2119
2120 #ifndef THREADS
2121
2122 GC_INNER ptr_t GC_blocked_sp = NULL;
2123         /* NULL value means we are not inside GC_do_blocking() call. */
2124 # ifdef IA64
2125     STATIC ptr_t GC_blocked_register_sp = NULL;
2126 # endif
2127
2128 GC_INNER struct GC_traced_stack_sect_s *GC_traced_stack_sect = NULL;
2129
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,
2132                                              void * client_data)
2133 {
2134     struct GC_traced_stack_sect_s stacksect;
2135     GC_ASSERT(GC_is_initialized);
2136
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);
2142
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 */
2149     }
2150
2151     /* Setup new "stack section".       */
2152     stacksect.saved_stack_ptr = GC_blocked_sp;
2153 #   ifdef IA64
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;
2159 #   endif
2160     stacksect.prev = GC_traced_stack_sect;
2161     GC_blocked_sp = NULL;
2162     GC_traced_stack_sect = &stacksect;
2163
2164     client_data = fn(client_data);
2165     GC_ASSERT(GC_blocked_sp == NULL);
2166     GC_ASSERT(GC_traced_stack_sect == &stacksect);
2167
2168 #   if defined(CPPCHECK)
2169       GC_noop1((word)GC_traced_stack_sect - (word)GC_blocked_sp);
2170 #   endif
2171     /* Restore original "stack section".        */
2172     GC_traced_stack_sect = stacksect.prev;
2173 #   ifdef IA64
2174       GC_blocked_register_sp = stacksect.saved_backing_store_ptr;
2175 #   endif
2176     GC_blocked_sp = stacksect.saved_stack_ptr;
2177
2178     return client_data; /* result */
2179 }
2180
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)
2183 {
2184     struct blocking_data * d = (struct blocking_data *) data;
2185     GC_ASSERT(GC_is_initialized);
2186     GC_ASSERT(GC_blocked_sp == NULL);
2187 #   ifdef SPARC
2188         GC_blocked_sp = GC_save_regs_in_stack();
2189 #   else
2190         GC_blocked_sp = (ptr_t) &d; /* save approx. sp */
2191 #   endif
2192 #   ifdef IA64
2193         GC_blocked_register_sp = GC_save_regs_in_stack();
2194 #   endif
2195
2196     d -> client_data = (d -> fn)(d -> client_data);
2197
2198 #   ifdef SPARC
2199         GC_ASSERT(GC_blocked_sp != NULL);
2200 #   else
2201         GC_ASSERT(GC_blocked_sp == (ptr_t)(&d));
2202 #   endif
2203 #   if defined(CPPCHECK)
2204       GC_noop1((word)GC_blocked_sp);
2205 #   endif
2206     GC_blocked_sp = NULL;
2207 }
2208
2209   GC_API void GC_CALL GC_set_stackbottom(void *gc_thread_handle,
2210                                          const struct GC_stack_base *sb)
2211   {
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;
2217
2218     GC_stackbottom = (char *)sb->mem_base;
2219 #   ifdef IA64
2220       GC_register_stackbottom = (ptr_t)sb->reg_base;
2221 #   endif
2222   }
2223
2224   GC_API void * GC_CALL GC_get_my_stackbottom(struct GC_stack_base *sb)
2225   {
2226     GC_ASSERT(GC_is_initialized);
2227     sb -> mem_base = GC_stackbottom;
2228 #   ifdef IA64
2229       sb -> reg_base = GC_register_stackbottom;
2230 #   endif
2231     return &GC_stackbottom; /* gc_thread_handle */
2232   }
2233 #endif /* !THREADS */
2234
2235 GC_API void * GC_CALL GC_do_blocking(GC_fn_type fn, void * client_data)
2236 {
2237     struct blocking_data my_data;
2238
2239     my_data.fn = fn;
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 */
2243 }
2244
2245 #if !defined(NO_DEBUGGING)
2246   GC_API void GC_CALL GC_dump(void)
2247   {
2248     DCL_LOCK_STATE;
2249
2250     LOCK();
2251     GC_dump_named(NULL);
2252     UNLOCK();
2253   }
2254
2255   GC_API void GC_CALL GC_dump_named(const char *name)
2256   {
2257 #   ifndef NO_CLOCK
2258       CLOCK_TYPE current_time;
2259
2260       GET_TIME(current_time);
2261 #   endif
2262     if (name != NULL) {
2263       GC_printf("***GC Dump %s\n", name);
2264     } else {
2265       GC_printf("***GC Dump collection #%lu\n", (unsigned long)GC_gc_no);
2266     }
2267 #   ifndef NO_CLOCK
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));
2271 #   endif
2272
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();
2281   }
2282 #endif /* !NO_DEBUGGING */
2283
2284 static void block_add_size(struct hblk *h, word pbytes)
2285 {
2286   hdr *hhdr = HDR(h);
2287   *(word *)pbytes += (WORDS_TO_BYTES(hhdr->hb_sz) + (HBLKSIZE - 1))
2288                         & ~(word)(HBLKSIZE - 1);
2289 }
2290
2291 GC_API size_t GC_CALL GC_get_memory_use(void)
2292 {
2293   word bytes = 0;
2294   DCL_LOCK_STATE;
2295
2296   LOCK();
2297   GC_apply_to_all_blocks(block_add_size, (word)(&bytes));
2298   UNLOCK();
2299   return (size_t)bytes;
2300 }
2301
2302 /* Getter functions for the public Read-only variables.                 */
2303
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)
2308 {
2309     return GC_gc_no;
2310 }
2311
2312 #ifdef THREADS
2313   GC_API int GC_CALL GC_get_parallel(void)
2314   {
2315     /* GC_parallel is initialized at start-up.  */
2316     return GC_parallel;
2317   }
2318
2319   GC_API void GC_CALL GC_alloc_lock(void)
2320   {
2321     DCL_LOCK_STATE;
2322     LOCK();
2323   }
2324
2325   GC_API void GC_CALL GC_alloc_unlock(void)
2326   {
2327     /* no DCL_LOCK_STATE */
2328     UNLOCK();
2329   }
2330
2331   GC_INNER GC_on_thread_event_proc GC_on_thread_event = 0;
2332
2333   GC_API void GC_CALL GC_set_on_thread_event(GC_on_thread_event_proc fn)
2334   {
2335     /* fn may be 0 (means no event notifier). */
2336     DCL_LOCK_STATE;
2337     LOCK();
2338     GC_on_thread_event = fn;
2339     UNLOCK();
2340   }
2341
2342   GC_API GC_on_thread_event_proc GC_CALL GC_get_on_thread_event(void)
2343   {
2344     GC_on_thread_event_proc fn;
2345     DCL_LOCK_STATE;
2346     LOCK();
2347     fn = GC_on_thread_event;
2348     UNLOCK();
2349     return fn;
2350   }
2351 #endif /* THREADS */
2352
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()).                                                 */
2356
2357 GC_API void GC_CALL GC_set_oom_fn(GC_oom_func fn)
2358 {
2359     GC_ASSERT(NONNULL_ARG_NOT_NULL(fn));
2360     DCL_LOCK_STATE;
2361     LOCK();
2362     GC_oom_fn = fn;
2363     UNLOCK();
2364 }
2365
2366 GC_API GC_oom_func GC_CALL GC_get_oom_fn(void)
2367 {
2368     GC_oom_func fn;
2369     DCL_LOCK_STATE;
2370     LOCK();
2371     fn = GC_oom_fn;
2372     UNLOCK();
2373     return fn;
2374 }
2375
2376 GC_API void GC_CALL GC_set_on_heap_resize(GC_on_heap_resize_proc fn)
2377 {
2378     /* fn may be 0 (means no event notifier). */
2379     DCL_LOCK_STATE;
2380     LOCK();
2381     GC_on_heap_resize = fn;
2382     UNLOCK();
2383 }
2384
2385 GC_API GC_on_heap_resize_proc GC_CALL GC_get_on_heap_resize(void)
2386 {
2387     GC_on_heap_resize_proc fn;
2388     DCL_LOCK_STATE;
2389     LOCK();
2390     fn = GC_on_heap_resize;
2391     UNLOCK();
2392     return fn;
2393 }
2394
2395 GC_API void GC_CALL GC_set_finalizer_notifier(GC_finalizer_notifier_proc fn)
2396 {
2397     /* fn may be 0 (means no finalizer notifier). */
2398     DCL_LOCK_STATE;
2399     LOCK();
2400     GC_finalizer_notifier = fn;
2401     UNLOCK();
2402 }
2403
2404 GC_API GC_finalizer_notifier_proc GC_CALL GC_get_finalizer_notifier(void)
2405 {
2406     GC_finalizer_notifier_proc fn;
2407     DCL_LOCK_STATE;
2408     LOCK();
2409     fn = GC_finalizer_notifier;
2410     UNLOCK();
2411     return fn;
2412 }
2413
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).            */
2420
2421 GC_API void GC_CALL GC_set_find_leak(int value)
2422 {
2423     /* value is of boolean type. */
2424     GC_find_leak = value;
2425 }
2426
2427 GC_API int GC_CALL GC_get_find_leak(void)
2428 {
2429     return GC_find_leak;
2430 }
2431
2432 GC_API void GC_CALL GC_set_all_interior_pointers(int value)
2433 {
2434     DCL_LOCK_STATE;
2435
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.                                 */
2441       LOCK();
2442       GC_initialize_offsets(); /* NOTE: this resets manual offsets as well */
2443       if (!GC_all_interior_pointers)
2444         GC_bl_init_no_interiors();
2445       UNLOCK();
2446     }
2447 }
2448
2449 GC_API int GC_CALL GC_get_all_interior_pointers(void)
2450 {
2451     return GC_all_interior_pointers;
2452 }
2453
2454 GC_API void GC_CALL GC_set_finalize_on_demand(int value)
2455 {
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;
2459 }
2460
2461 GC_API int GC_CALL GC_get_finalize_on_demand(void)
2462 {
2463     return GC_finalize_on_demand;
2464 }
2465
2466 GC_API void GC_CALL GC_set_java_finalization(int value)
2467 {
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;
2471 }
2472
2473 GC_API int GC_CALL GC_get_java_finalization(void)
2474 {
2475     return GC_java_finalization;
2476 }
2477
2478 GC_API void GC_CALL GC_set_dont_expand(int value)
2479 {
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;
2483 }
2484
2485 GC_API int GC_CALL GC_get_dont_expand(void)
2486 {
2487     return GC_dont_expand;
2488 }
2489
2490 GC_API void GC_CALL GC_set_no_dls(int value)
2491 {
2492     GC_ASSERT(value != -1); /* -1 was used to retrieve old value in gc-7.2 */
2493     /* value is of boolean type. */
2494     GC_no_dls = value;
2495 }
2496
2497 GC_API int GC_CALL GC_get_no_dls(void)
2498 {
2499     return GC_no_dls;
2500 }
2501
2502 GC_API void GC_CALL GC_set_non_gc_bytes(GC_word value)
2503 {
2504     GC_non_gc_bytes = value;
2505 }
2506
2507 GC_API GC_word GC_CALL GC_get_non_gc_bytes(void)
2508 {
2509     return GC_non_gc_bytes;
2510 }
2511
2512 GC_API void GC_CALL GC_set_free_space_divisor(GC_word value)
2513 {
2514     GC_ASSERT(value > 0);
2515     GC_free_space_divisor = value;
2516 }
2517
2518 GC_API GC_word GC_CALL GC_get_free_space_divisor(void)
2519 {
2520     return GC_free_space_divisor;
2521 }
2522
2523 GC_API void GC_CALL GC_set_max_retries(GC_word value)
2524 {
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;
2528 }
2529
2530 GC_API GC_word GC_CALL GC_get_max_retries(void)
2531 {
2532     return GC_max_retries;
2533 }
2534
2535 GC_API void GC_CALL GC_set_dont_precollect(int value)
2536 {
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;
2540 }
2541
2542 GC_API int GC_CALL GC_get_dont_precollect(void)
2543 {
2544     return GC_dont_precollect;
2545 }
2546
2547 GC_API void GC_CALL GC_set_full_freq(int value)
2548 {
2549     GC_ASSERT(value >= 0);
2550     GC_full_freq = value;
2551 }
2552
2553 GC_API int GC_CALL GC_get_full_freq(void)
2554 {
2555     return GC_full_freq;
2556 }
2557
2558 GC_API void GC_CALL GC_set_time_limit(unsigned long value)
2559 {
2560     GC_ASSERT((long)value != -1L);
2561                         /* -1 was used to retrieve old value in gc-7.2 */
2562     GC_time_limit = value;
2563 }
2564
2565 GC_API unsigned long GC_CALL GC_get_time_limit(void)
2566 {
2567     return GC_time_limit;
2568 }
2569
2570 GC_API void GC_CALL GC_set_force_unmap_on_gcollect(int value)
2571 {
2572     GC_force_unmap_on_gcollect = (GC_bool)value;
2573 }
2574
2575 GC_API int GC_CALL GC_get_force_unmap_on_gcollect(void)
2576 {
2577     return (int)GC_force_unmap_on_gcollect;
2578 }
2579
2580 GC_API void GC_CALL GC_abort_on_oom(void)
2581 {
2582     GC_err_printf("Insufficient memory for the allocation\n");
2583     EXIT();
2584 }
2585
2586 #ifdef THREADS
2587   GC_API void GC_CALL GC_stop_world_external(void)
2588   {
2589     GC_ASSERT(GC_is_initialized);
2590     LOCK();
2591 #   ifdef THREAD_LOCAL_ALLOC
2592       GC_ASSERT(!GC_world_stopped);
2593 #   endif
2594     STOP_WORLD();
2595 #   ifdef THREAD_LOCAL_ALLOC
2596       GC_world_stopped = TRUE;
2597 #   endif
2598   }
2599
2600   GC_API void GC_CALL GC_start_world_external(void)
2601   {
2602 #   ifdef THREAD_LOCAL_ALLOC
2603       GC_ASSERT(GC_world_stopped);
2604       GC_world_stopped = FALSE;
2605 #   else
2606       GC_ASSERT(GC_is_initialized);
2607 #   endif
2608     START_WORLD();
2609     UNLOCK();
2610   }
2611 #endif /* THREADS */