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(MSWIN32) || defined(MSWINCE) \
31 || (defined(CYGWIN32) && defined(GC_READ_ENV_FILE))
32 # ifndef WIN32_LEAN_AND_MEAN
33 # define WIN32_LEAN_AND_MEAN 1
34 # endif
35 # define NOSERVICE
36 # include <windows.h>
37 #endif
38
39 #if defined(UNIX_LIKE) || defined(CYGWIN32) || defined(SYMBIAN)
40 # include <fcntl.h>
41 # include <sys/types.h>
42 # include <sys/stat.h>
43 #endif
44
45 #ifdef NONSTOP
46 # include <floss.h>
47 #endif
48
49 #ifdef THREADS
50 # ifdef PCR
51 # include "il/PCR_IL.h"
52 GC_INNER PCR_Th_ML GC_allocate_ml;
53 # elif defined(SN_TARGET_PSP2)
54 GC_INNER WapiMutex GC_allocate_ml_PSP2 = { 0, NULL };
55 # elif defined(SN_TARGET_ORBIS) || defined(SN_TARGET_PS3)
56 # include <pthread.h>
57 GC_INNER pthread_mutex_t GC_allocate_ml;
58 # endif
59 /* For other platforms with threads, the lock and possibly */
60 /* GC_lock_holder variables are defined in the thread support code. */
61 #endif /* THREADS */
62
63 #ifdef DYNAMIC_LOADING
64 /* We need to register the main data segment. Returns TRUE unless */
65 /* this is done implicitly as part of dynamic library registration. */
66 # define GC_REGISTER_MAIN_STATIC_DATA() GC_register_main_static_data()
67 #elif defined(GC_DONT_REGISTER_MAIN_STATIC_DATA)
68 # define GC_REGISTER_MAIN_STATIC_DATA() FALSE
69 #else
70 /* Don't unnecessarily call GC_register_main_static_data() in case */
71 /* dyn_load.c isn't linked in. */
72 # define GC_REGISTER_MAIN_STATIC_DATA() TRUE
73 #endif
74
75 #ifdef NEED_CANCEL_DISABLE_COUNT
76 __thread unsigned char GC_cancel_disable_count = 0;
77 #endif
78
79 GC_FAR struct _GC_arrays GC_arrays /* = { 0 } */;
80
81 GC_INNER GC_bool GC_debugging_started = FALSE;
82 /* defined here so we don't have to load dbg_mlc.o */
83
84 ptr_t GC_stackbottom = 0;
85
86 #ifdef IA64
87 ptr_t GC_register_stackbottom = 0;
88 #endif
89
90 int GC_dont_gc = FALSE;
91
92 int GC_dont_precollect = FALSE;
93
94 GC_bool GC_quiet = 0; /* used also in pcr_interface.c */
95
96 #if !defined(NO_CLOCK) || !defined(SMALL_CONFIG)
97 int GC_print_stats = 0;
98 #endif
99
100 #ifdef GC_PRINT_BACK_HEIGHT
101 GC_INNER GC_bool GC_print_back_height = TRUE;
102 #else
103 GC_INNER GC_bool GC_print_back_height = FALSE;
104 #endif
105
106 #ifndef NO_DEBUGGING
107 # ifdef GC_DUMP_REGULARLY
108 GC_INNER GC_bool GC_dump_regularly = TRUE;
109 /* Generate regular debugging dumps. */
110 # else
111 GC_INNER GC_bool GC_dump_regularly = FALSE;
112 # endif
113 # ifndef NO_CLOCK
114 STATIC CLOCK_TYPE GC_init_time;
115 /* The time that the GC was initialized at. */
116 # endif
117 #endif /* !NO_DEBUGGING */
118
119 #ifdef KEEP_BACK_PTRS
120 GC_INNER long GC_backtraces = 0;
121 /* Number of random backtraces to generate for each GC. */
122 #endif
123
124 #ifdef FIND_LEAK
125 int GC_find_leak = 1;
126 #else
127 int GC_find_leak = 0;
128 #endif
129
130 #ifndef SHORT_DBG_HDRS
131 # ifdef GC_FINDLEAK_DELAY_FREE
132 GC_INNER GC_bool GC_findleak_delay_free = TRUE;
133 # else
134 GC_INNER GC_bool GC_findleak_delay_free = FALSE;
135 # endif
136 #endif /* !SHORT_DBG_HDRS */
137
138 #ifdef ALL_INTERIOR_POINTERS
139 int GC_all_interior_pointers = 1;
140 #else
141 int GC_all_interior_pointers = 0;
142 #endif
143
144 #ifdef FINALIZE_ON_DEMAND
145 int GC_finalize_on_demand = 1;
146 #else
147 int GC_finalize_on_demand = 0;
148 #endif
149
150 #ifdef JAVA_FINALIZATION
151 int GC_java_finalization = 1;
152 #else
153 int GC_java_finalization = 0;
154 #endif
155
156 /* All accesses to it should be synchronized to avoid data races. */
157 GC_finalizer_notifier_proc GC_finalizer_notifier =
158 (GC_finalizer_notifier_proc)0;
159
160 #ifdef GC_FORCE_UNMAP_ON_GCOLLECT
161 /* Has no effect unless USE_MUNMAP. */
162 /* Has no effect on implicitly-initiated garbage collections. */
163 GC_INNER GC_bool GC_force_unmap_on_gcollect = TRUE;
164 #else
165 GC_INNER GC_bool GC_force_unmap_on_gcollect = FALSE;
166 #endif
167
168 #ifndef GC_LARGE_ALLOC_WARN_INTERVAL
169 # define GC_LARGE_ALLOC_WARN_INTERVAL 5
170 #endif
171 GC_INNER long GC_large_alloc_warn_interval = GC_LARGE_ALLOC_WARN_INTERVAL;
172 /* Interval between unsuppressed warnings. */
173
GC_default_oom_fn(size_t bytes_requested GC_ATTR_UNUSED)174 STATIC void * GC_CALLBACK GC_default_oom_fn(
175 size_t bytes_requested GC_ATTR_UNUSED)
176 {
177 return(0);
178 }
179
180 /* All accesses to it should be synchronized to avoid data races. */
181 GC_oom_func GC_oom_fn = GC_default_oom_fn;
182
183 #ifdef CAN_HANDLE_FORK
184 # ifdef HANDLE_FORK
185 GC_INNER int GC_handle_fork = 1;
186 /* The value is examined by GC_thr_init. */
187 # else
188 GC_INNER int GC_handle_fork = FALSE;
189 # endif
190
191 #elif !defined(HAVE_NO_FORK)
192
193 /* Same as above but with GC_CALL calling conventions. */
GC_atfork_prepare(void)194 GC_API void GC_CALL GC_atfork_prepare(void)
195 {
196 # ifdef THREADS
197 ABORT("fork() handling unsupported");
198 # endif
199 }
200
GC_atfork_parent(void)201 GC_API void GC_CALL GC_atfork_parent(void)
202 {
203 /* empty */
204 }
205
GC_atfork_child(void)206 GC_API void GC_CALL GC_atfork_child(void)
207 {
208 /* empty */
209 }
210 #endif /* !CAN_HANDLE_FORK && !HAVE_NO_FORK */
211
212 /* Overrides the default automatic handle-fork mode. Has effect only */
213 /* if called before GC_INIT. */
GC_set_handle_fork(int value GC_ATTR_UNUSED)214 GC_API void GC_CALL GC_set_handle_fork(int value GC_ATTR_UNUSED)
215 {
216 # ifdef CAN_HANDLE_FORK
217 if (!GC_is_initialized)
218 GC_handle_fork = value >= -1 ? value : 1;
219 /* Map all negative values except for -1 to a positive one. */
220 # elif defined(THREADS) || (defined(DARWIN) && defined(MPROTECT_VDB))
221 if (!GC_is_initialized && value) {
222 # ifndef SMALL_CONFIG
223 GC_init(); /* to initialize GC_manual_vdb and GC_stderr */
224 # ifndef THREADS
225 if (GC_manual_vdb)
226 return;
227 # endif
228 # endif
229 ABORT("fork() handling unsupported");
230 }
231 # else
232 /* No at-fork handler is needed in the single-threaded mode. */
233 # endif
234 }
235
236 /* Set things up so that GC_size_map[i] >= granules(i), */
237 /* but not too much bigger */
238 /* and so that size_map contains relatively few distinct entries */
239 /* This was originally stolen from Russ Atkinson's Cedar */
240 /* quantization algorithm (but we precompute it). */
GC_init_size_map(void)241 STATIC void GC_init_size_map(void)
242 {
243 size_t i;
244
245 /* Map size 0 to something bigger. */
246 /* This avoids problems at lower levels. */
247 GC_size_map[0] = 1;
248 for (i = 1; i <= GRANULES_TO_BYTES(TINY_FREELISTS-1) - EXTRA_BYTES; i++) {
249 GC_size_map[i] = ROUNDED_UP_GRANULES(i);
250 # ifndef _MSC_VER
251 GC_ASSERT(GC_size_map[i] < TINY_FREELISTS);
252 /* Seems to tickle bug in VC++ 2008 for AMD64 */
253 # endif
254 }
255 /* We leave the rest of the array to be filled in on demand. */
256 }
257
258 /*
259 * The following is a gross hack to deal with a problem that can occur
260 * on machines that are sloppy about stack frame sizes, notably SPARC.
261 * Bogus pointers may be written to the stack and not cleared for
262 * a LONG time, because they always fall into holes in stack frames
263 * that are not written. We partially address this by clearing
264 * sections of the stack whenever we get control.
265 */
266
267 #ifndef SMALL_CLEAR_SIZE
268 # define SMALL_CLEAR_SIZE 256 /* Clear this much every time. */
269 #endif
270
271 #if defined(ALWAYS_SMALL_CLEAR_STACK) || defined(STACK_NOT_SCANNED)
GC_clear_stack(void * arg)272 GC_API void * GC_CALL GC_clear_stack(void *arg)
273 {
274 # ifndef STACK_NOT_SCANNED
275 word volatile dummy[SMALL_CLEAR_SIZE];
276 BZERO((/* no volatile */ void *)dummy, sizeof(dummy));
277 # endif
278 return arg;
279 }
280 #else
281
282 # ifdef THREADS
283 # define BIG_CLEAR_SIZE 2048 /* Clear this much now and then. */
284 # else
285 STATIC word GC_stack_last_cleared = 0; /* GC_no when we last did this */
286 STATIC ptr_t GC_min_sp = NULL;
287 /* Coolest stack pointer value from which */
288 /* we've already cleared the stack. */
289 STATIC ptr_t GC_high_water = NULL;
290 /* "hottest" stack pointer value we have seen */
291 /* recently. Degrades over time. */
292 STATIC word GC_bytes_allocd_at_reset = 0;
293 # define DEGRADE_RATE 50
294 # endif
295
296 # if defined(ASM_CLEAR_CODE)
297 void *GC_clear_stack_inner(void *, ptr_t);
298 # else
299 /* Clear the stack up to about limit. Return arg. This function */
300 /* is not static because it could also be erroneously defined in .S */
301 /* file, so this error would be caught by the linker. */
GC_clear_stack_inner(void * arg,volatile ptr_t limit)302 void *GC_clear_stack_inner(void *arg,
303 # if defined(__APPLE_CC__) && !GC_CLANG_PREREQ(6, 0)
304 volatile /* to workaround some bug */
305 # endif
306 ptr_t limit)
307 {
308 # define CLEAR_SIZE 213 /* granularity */
309 volatile word dummy[CLEAR_SIZE];
310
311 BZERO((/* no volatile */ void *)dummy, sizeof(dummy));
312 if ((word)GC_approx_sp() COOLER_THAN (word)limit) {
313 (void)GC_clear_stack_inner(arg, limit);
314 }
315 /* Make sure the recursive call is not a tail call, and the bzero */
316 /* call is not recognized as dead code. */
317 GC_noop1(COVERT_DATAFLOW(dummy));
318 return(arg);
319 }
320 # endif /* !ASM_CLEAR_CODE */
321
322 # ifdef THREADS
323 /* Used to occasionally clear a bigger chunk. */
324 /* TODO: Should be more random than it is ... */
325 GC_ATTR_NO_SANITIZE_THREAD
next_random_no(void)326 static unsigned next_random_no(void)
327 {
328 static unsigned random_no = 0;
329 return ++random_no % 13;
330 }
331 # endif /* THREADS */
332
333 /* Clear some of the inaccessible part of the stack. Returns its */
334 /* argument, so it can be used in a tail call position, hence clearing */
335 /* another frame. */
GC_clear_stack(void * arg)336 GC_API void * GC_CALL GC_clear_stack(void *arg)
337 {
338 ptr_t sp = GC_approx_sp(); /* Hotter than actual sp */
339 # ifdef THREADS
340 word volatile dummy[SMALL_CLEAR_SIZE];
341 # endif
342
343 # define SLOP 400
344 /* Extra bytes we clear every time. This clears our own */
345 /* activation record, and should cause more frequent */
346 /* clearing near the cold end of the stack, a good thing. */
347 # define GC_SLOP 4000
348 /* We make GC_high_water this much hotter than we really saw */
349 /* it, to cover for GC noise etc. above our current frame. */
350 # define CLEAR_THRESHOLD 100000
351 /* We restart the clearing process after this many bytes of */
352 /* allocation. Otherwise very heavily recursive programs */
353 /* with sparse stacks may result in heaps that grow almost */
354 /* without bounds. As the heap gets larger, collection */
355 /* frequency decreases, thus clearing frequency would decrease, */
356 /* thus more junk remains accessible, thus the heap gets */
357 /* larger ... */
358 # ifdef THREADS
359 if (next_random_no() == 0) {
360 ptr_t limit = sp;
361
362 MAKE_HOTTER(limit, BIG_CLEAR_SIZE*sizeof(word));
363 limit = (ptr_t)((word)limit & ~0xf);
364 /* Make it sufficiently aligned for assembly */
365 /* implementations of GC_clear_stack_inner. */
366 return GC_clear_stack_inner(arg, limit);
367 }
368 BZERO((void *)dummy, SMALL_CLEAR_SIZE*sizeof(word));
369 # else
370 if (GC_gc_no > GC_stack_last_cleared) {
371 /* Start things over, so we clear the entire stack again */
372 if (GC_stack_last_cleared == 0)
373 GC_high_water = (ptr_t)GC_stackbottom;
374 GC_min_sp = GC_high_water;
375 GC_stack_last_cleared = GC_gc_no;
376 GC_bytes_allocd_at_reset = GC_bytes_allocd;
377 }
378 /* Adjust GC_high_water */
379 MAKE_COOLER(GC_high_water, WORDS_TO_BYTES(DEGRADE_RATE) + GC_SLOP);
380 if ((word)sp HOTTER_THAN (word)GC_high_water) {
381 GC_high_water = sp;
382 }
383 MAKE_HOTTER(GC_high_water, GC_SLOP);
384 {
385 ptr_t limit = GC_min_sp;
386
387 MAKE_HOTTER(limit, SLOP);
388 if ((word)sp COOLER_THAN (word)limit) {
389 limit = (ptr_t)((word)limit & ~0xf);
390 /* Make it sufficiently aligned for assembly */
391 /* implementations of GC_clear_stack_inner. */
392 GC_min_sp = sp;
393 return GC_clear_stack_inner(arg, limit);
394 }
395 }
396 if (GC_bytes_allocd - GC_bytes_allocd_at_reset > CLEAR_THRESHOLD) {
397 /* Restart clearing process, but limit how much clearing we do. */
398 GC_min_sp = sp;
399 MAKE_HOTTER(GC_min_sp, CLEAR_THRESHOLD/4);
400 if ((word)GC_min_sp HOTTER_THAN (word)GC_high_water)
401 GC_min_sp = GC_high_water;
402 GC_bytes_allocd_at_reset = GC_bytes_allocd;
403 }
404 # endif
405 return arg;
406 }
407
408 #endif /* !ALWAYS_SMALL_CLEAR_STACK && !STACK_NOT_SCANNED */
409
410 /* Return a pointer to the base address of p, given a pointer to a */
411 /* an address within an object. Return 0 o.w. */
GC_base(void * p)412 GC_API void * GC_CALL GC_base(void * p)
413 {
414 ptr_t r;
415 struct hblk *h;
416 bottom_index *bi;
417 hdr *candidate_hdr;
418
419 r = (ptr_t)p;
420 if (!EXPECT(GC_is_initialized, TRUE)) return 0;
421 h = HBLKPTR(r);
422 GET_BI(r, bi);
423 candidate_hdr = HDR_FROM_BI(bi, r);
424 if (candidate_hdr == 0) return(0);
425 /* If it's a pointer to the middle of a large object, move it */
426 /* to the beginning. */
427 while (IS_FORWARDING_ADDR_OR_NIL(candidate_hdr)) {
428 h = FORWARDED_ADDR(h,candidate_hdr);
429 r = (ptr_t)h;
430 candidate_hdr = HDR(h);
431 }
432 if (HBLK_IS_FREE(candidate_hdr)) return(0);
433 /* Make sure r points to the beginning of the object */
434 r = (ptr_t)((word)r & ~(WORDS_TO_BYTES(1) - 1));
435 {
436 size_t offset = HBLKDISPL(r);
437 word sz = candidate_hdr -> hb_sz;
438 size_t obj_displ = offset % sz;
439 ptr_t limit;
440
441 r -= obj_displ;
442 limit = r + sz;
443 if ((word)limit > (word)(h + 1) && sz <= HBLKSIZE) {
444 return(0);
445 }
446 if ((word)p >= (word)limit) return(0);
447 }
448 return((void *)r);
449 }
450
451 /* Return TRUE if and only if p points to somewhere in GC heap. */
GC_is_heap_ptr(const void * p)452 GC_API int GC_CALL GC_is_heap_ptr(const void *p)
453 {
454 bottom_index *bi;
455
456 GC_ASSERT(GC_is_initialized);
457 GET_BI(p, bi);
458 return HDR_FROM_BI(bi, p) != 0;
459 }
460
461 /* Return the size of an object, given a pointer to its base. */
462 /* (For small objects this also happens to work from interior pointers, */
463 /* but that shouldn't be relied upon.) */
GC_size(const void * p)464 GC_API size_t GC_CALL GC_size(const void * p)
465 {
466 hdr * hhdr = HDR(p);
467
468 return (size_t)hhdr->hb_sz;
469 }
470
471
472 /* These getters remain unsynchronized for compatibility (since some */
473 /* clients could call some of them from a GC callback holding the */
474 /* allocator lock). */
GC_get_heap_size(void)475 GC_API size_t GC_CALL GC_get_heap_size(void)
476 {
477 /* ignore the memory space returned to OS (i.e. count only the */
478 /* space owned by the garbage collector) */
479 return (size_t)(GC_heapsize - GC_unmapped_bytes);
480 }
481
GC_get_free_bytes(void)482 GC_API size_t GC_CALL GC_get_free_bytes(void)
483 {
484 /* ignore the memory space returned to OS */
485 return (size_t)(GC_large_free_bytes - GC_unmapped_bytes);
486 }
487
GC_get_unmapped_bytes(void)488 GC_API size_t GC_CALL GC_get_unmapped_bytes(void)
489 {
490 return (size_t)GC_unmapped_bytes;
491 }
492
GC_get_bytes_since_gc(void)493 GC_API size_t GC_CALL GC_get_bytes_since_gc(void)
494 {
495 return (size_t)GC_bytes_allocd;
496 }
497
GC_get_total_bytes(void)498 GC_API size_t GC_CALL GC_get_total_bytes(void)
499 {
500 return (size_t)(GC_bytes_allocd + GC_bytes_allocd_before_gc);
501 }
502
503 #ifndef GC_GET_HEAP_USAGE_NOT_NEEDED
504
GC_get_size_map_at(int i)505 GC_API size_t GC_CALL GC_get_size_map_at(int i)
506 {
507 if ((unsigned)i > MAXOBJBYTES)
508 return GC_SIZE_MAX;
509 return GRANULES_TO_BYTES(GC_size_map[i]);
510 }
511
512 /* Return the heap usage information. This is a thread-safe (atomic) */
513 /* alternative for the five above getters. NULL pointer is allowed for */
514 /* any argument. Returned (filled in) values are of word type. */
GC_get_heap_usage_safe(GC_word * pheap_size,GC_word * pfree_bytes,GC_word * punmapped_bytes,GC_word * pbytes_since_gc,GC_word * ptotal_bytes)515 GC_API void GC_CALL GC_get_heap_usage_safe(GC_word *pheap_size,
516 GC_word *pfree_bytes, GC_word *punmapped_bytes,
517 GC_word *pbytes_since_gc, GC_word *ptotal_bytes)
518 {
519 DCL_LOCK_STATE;
520
521 LOCK();
522 if (pheap_size != NULL)
523 *pheap_size = GC_heapsize - GC_unmapped_bytes;
524 if (pfree_bytes != NULL)
525 *pfree_bytes = GC_large_free_bytes - GC_unmapped_bytes;
526 if (punmapped_bytes != NULL)
527 *punmapped_bytes = GC_unmapped_bytes;
528 if (pbytes_since_gc != NULL)
529 *pbytes_since_gc = GC_bytes_allocd;
530 if (ptotal_bytes != NULL)
531 *ptotal_bytes = GC_bytes_allocd + GC_bytes_allocd_before_gc;
532 UNLOCK();
533 }
534
535 GC_INNER word GC_reclaimed_bytes_before_gc = 0;
536
537 /* Fill in GC statistics provided the destination is of enough size. */
fill_prof_stats(struct GC_prof_stats_s * pstats)538 static void fill_prof_stats(struct GC_prof_stats_s *pstats)
539 {
540 pstats->heapsize_full = GC_heapsize;
541 pstats->free_bytes_full = GC_large_free_bytes;
542 pstats->unmapped_bytes = GC_unmapped_bytes;
543 pstats->bytes_allocd_since_gc = GC_bytes_allocd;
544 pstats->allocd_bytes_before_gc = GC_bytes_allocd_before_gc;
545 pstats->non_gc_bytes = GC_non_gc_bytes;
546 pstats->gc_no = GC_gc_no; /* could be -1 */
547 # ifdef PARALLEL_MARK
548 pstats->markers_m1 = (word)((signed_word)GC_markers_m1);
549 # else
550 pstats->markers_m1 = 0; /* one marker */
551 # endif
552 pstats->bytes_reclaimed_since_gc = GC_bytes_found > 0 ?
553 (word)GC_bytes_found : 0;
554 pstats->reclaimed_bytes_before_gc = GC_reclaimed_bytes_before_gc;
555 pstats->expl_freed_bytes_since_gc = GC_bytes_freed; /* since gc-7.7 */
556 }
557
558 # include <string.h> /* for memset() */
559
GC_get_prof_stats(struct GC_prof_stats_s * pstats,size_t stats_sz)560 GC_API size_t GC_CALL GC_get_prof_stats(struct GC_prof_stats_s *pstats,
561 size_t stats_sz)
562 {
563 struct GC_prof_stats_s stats;
564 DCL_LOCK_STATE;
565
566 LOCK();
567 fill_prof_stats(stats_sz >= sizeof(stats) ? pstats : &stats);
568 UNLOCK();
569
570 if (stats_sz == sizeof(stats)) {
571 return sizeof(stats);
572 } else if (stats_sz > sizeof(stats)) {
573 /* Fill in the remaining part with -1. */
574 memset((char *)pstats + sizeof(stats), 0xff, stats_sz - sizeof(stats));
575 return sizeof(stats);
576 } else {
577 if (EXPECT(stats_sz > 0, TRUE))
578 BCOPY(&stats, pstats, stats_sz);
579 return stats_sz;
580 }
581 }
582
583 # ifdef THREADS
584 /* The _unsafe version assumes the caller holds the allocation lock. */
GC_get_prof_stats_unsafe(struct GC_prof_stats_s * pstats,size_t stats_sz)585 GC_API size_t GC_CALL GC_get_prof_stats_unsafe(
586 struct GC_prof_stats_s *pstats,
587 size_t stats_sz)
588 {
589 struct GC_prof_stats_s stats;
590
591 if (stats_sz >= sizeof(stats)) {
592 fill_prof_stats(pstats);
593 if (stats_sz > sizeof(stats))
594 memset((char *)pstats + sizeof(stats), 0xff,
595 stats_sz - sizeof(stats));
596 return sizeof(stats);
597 } else {
598 if (EXPECT(stats_sz > 0, TRUE)) {
599 fill_prof_stats(&stats);
600 BCOPY(&stats, pstats, stats_sz);
601 }
602 return stats_sz;
603 }
604 }
605 # endif /* THREADS */
606
607 #endif /* !GC_GET_HEAP_USAGE_NOT_NEEDED */
608
609 #if defined(GC_DARWIN_THREADS) || defined(GC_OPENBSD_UTHREADS) \
610 || defined(GC_WIN32_THREADS) || (defined(NACL) && defined(THREADS))
611 /* GC does not use signals to suspend and restart threads. */
GC_set_suspend_signal(int sig GC_ATTR_UNUSED)612 GC_API void GC_CALL GC_set_suspend_signal(int sig GC_ATTR_UNUSED)
613 {
614 /* empty */
615 }
616
GC_set_thr_restart_signal(int sig GC_ATTR_UNUSED)617 GC_API void GC_CALL GC_set_thr_restart_signal(int sig GC_ATTR_UNUSED)
618 {
619 /* empty */
620 }
621
GC_get_suspend_signal(void)622 GC_API int GC_CALL GC_get_suspend_signal(void)
623 {
624 return -1;
625 }
626
GC_get_thr_restart_signal(void)627 GC_API int GC_CALL GC_get_thr_restart_signal(void)
628 {
629 return -1;
630 }
631 #endif /* GC_DARWIN_THREADS || GC_WIN32_THREADS || ... */
632
633 #if !defined(_MAX_PATH) && (defined(MSWIN32) || defined(MSWINCE) \
634 || defined(CYGWIN32))
635 # define _MAX_PATH MAX_PATH
636 #endif
637
638 #ifdef GC_READ_ENV_FILE
639 /* This works for Win32/WinCE for now. Really useful only for WinCE. */
640 STATIC char *GC_envfile_content = NULL;
641 /* The content of the GC "env" file with CR and */
642 /* LF replaced to '\0'. NULL if the file is */
643 /* missing or empty. Otherwise, always ends */
644 /* with '\0'. */
645 STATIC unsigned GC_envfile_length = 0;
646 /* Length of GC_envfile_content (if non-NULL). */
647
648 # ifndef GC_ENVFILE_MAXLEN
649 # define GC_ENVFILE_MAXLEN 0x4000
650 # endif
651
652 # define GC_ENV_FILE_EXT ".gc.env"
653
654 /* The routine initializes GC_envfile_content from the GC "env" file. */
GC_envfile_init(void)655 STATIC void GC_envfile_init(void)
656 {
657 # if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
658 HANDLE hFile;
659 char *content;
660 unsigned ofs;
661 unsigned len;
662 DWORD nBytesRead;
663 TCHAR path[_MAX_PATH + 0x10]; /* buffer for path + ext */
664 len = (unsigned)GetModuleFileName(NULL /* hModule */, path,
665 _MAX_PATH + 1);
666 /* If GetModuleFileName() has failed then len is 0. */
667 if (len > 4 && path[len - 4] == (TCHAR)'.') {
668 len -= 4; /* strip executable file extension */
669 }
670 BCOPY(TEXT(GC_ENV_FILE_EXT), &path[len], sizeof(TEXT(GC_ENV_FILE_EXT)));
671 hFile = CreateFile(path, GENERIC_READ,
672 FILE_SHARE_READ | FILE_SHARE_WRITE,
673 NULL /* lpSecurityAttributes */, OPEN_EXISTING,
674 FILE_ATTRIBUTE_NORMAL, NULL /* hTemplateFile */);
675 if (hFile == INVALID_HANDLE_VALUE)
676 return; /* the file is absent or the operation is failed */
677 len = (unsigned)GetFileSize(hFile, NULL);
678 if (len <= 1 || len >= GC_ENVFILE_MAXLEN) {
679 CloseHandle(hFile);
680 return; /* invalid file length - ignoring the file content */
681 }
682 /* At this execution point, GC_setpagesize() and GC_init_win32() */
683 /* must already be called (for GET_MEM() to work correctly). */
684 content = (char *)GET_MEM(ROUNDUP_PAGESIZE_IF_MMAP((size_t)len + 1));
685 if (content == NULL) {
686 CloseHandle(hFile);
687 return; /* allocation failure */
688 }
689 ofs = 0;
690 nBytesRead = (DWORD)-1L;
691 /* Last ReadFile() call should clear nBytesRead on success. */
692 while (ReadFile(hFile, content + ofs, len - ofs + 1, &nBytesRead,
693 NULL /* lpOverlapped */) && nBytesRead != 0) {
694 if ((ofs += nBytesRead) > len)
695 break;
696 }
697 CloseHandle(hFile);
698 if (ofs != len || nBytesRead != 0)
699 return; /* read operation is failed - ignoring the file content */
700 content[ofs] = '\0';
701 while (ofs-- > 0) {
702 if (content[ofs] == '\r' || content[ofs] == '\n')
703 content[ofs] = '\0';
704 }
705 GC_ASSERT(NULL == GC_envfile_content);
706 GC_envfile_length = len + 1;
707 GC_envfile_content = content;
708 # endif
709 }
710
711 /* This routine scans GC_envfile_content for the specified */
712 /* environment variable (and returns its value if found). */
GC_envfile_getenv(const char * name)713 GC_INNER char * GC_envfile_getenv(const char *name)
714 {
715 char *p;
716 char *end_of_content;
717 unsigned namelen;
718 # ifndef NO_GETENV
719 p = getenv(name); /* try the standard getenv() first */
720 if (p != NULL)
721 return *p != '\0' ? p : NULL;
722 # endif
723 p = GC_envfile_content;
724 if (p == NULL)
725 return NULL; /* "env" file is absent (or empty) */
726 namelen = strlen(name);
727 if (namelen == 0) /* a sanity check */
728 return NULL;
729 for (end_of_content = p + GC_envfile_length;
730 p != end_of_content; p += strlen(p) + 1) {
731 if (strncmp(p, name, namelen) == 0 && *(p += namelen) == '=') {
732 p++; /* the match is found; skip '=' */
733 return *p != '\0' ? p : NULL;
734 }
735 /* If not matching then skip to the next line. */
736 }
737 return NULL; /* no match found */
738 }
739 #endif /* GC_READ_ENV_FILE */
740
741 GC_INNER GC_bool GC_is_initialized = FALSE;
742
GC_is_init_called(void)743 GC_API int GC_CALL GC_is_init_called(void)
744 {
745 return GC_is_initialized;
746 }
747
748 #if (defined(MSWIN32) || defined(MSWINCE) || defined(MSWIN_XBOX1)) \
749 && defined(THREADS)
750 GC_INNER CRITICAL_SECTION GC_write_cs;
751 #endif
752
753 #ifndef DONT_USE_ATEXIT
754 # if !defined(PCR) && !defined(SMALL_CONFIG)
755 /* A dedicated variable to avoid a garbage collection on abort. */
756 /* GC_find_leak cannot be used for this purpose as otherwise */
757 /* TSan finds a data race (between GC_default_on_abort and, e.g., */
758 /* GC_finish_collection). */
759 static GC_bool skip_gc_atexit = FALSE;
760 # else
761 # define skip_gc_atexit FALSE
762 # endif
763
GC_exit_check(void)764 STATIC void GC_exit_check(void)
765 {
766 if (GC_find_leak && !skip_gc_atexit) {
767 # ifdef THREADS
768 GC_in_thread_creation = TRUE; /* OK to collect from unknown thread. */
769 GC_gcollect();
770 GC_in_thread_creation = FALSE;
771 # else
772 GC_gcollect();
773 # endif
774 }
775 }
776 #endif
777
778 #if defined(UNIX_LIKE) && !defined(NO_DEBUGGING)
looping_handler(int sig)779 static void looping_handler(int sig)
780 {
781 GC_err_printf("Caught signal %d: looping in handler\n", sig);
782 for (;;) {
783 /* empty */
784 }
785 }
786
787 static GC_bool installed_looping_handler = FALSE;
788
maybe_install_looping_handler(void)789 static void maybe_install_looping_handler(void)
790 {
791 /* Install looping handler before the write fault handler, so we */
792 /* handle write faults correctly. */
793 if (!installed_looping_handler && 0 != GETENV("GC_LOOP_ON_ABORT")) {
794 GC_set_and_save_fault_handler(looping_handler);
795 installed_looping_handler = TRUE;
796 }
797 }
798
799 #else /* !UNIX_LIKE */
800 # define maybe_install_looping_handler()
801 #endif
802
803 #define GC_DEFAULT_STDOUT_FD 1
804 #define GC_DEFAULT_STDERR_FD 2
805
806 #if !defined(OS2) && !defined(MACOS) && !defined(GC_ANDROID_LOG) \
807 && !defined(NN_PLATFORM_CTR) && !defined(NINTENDO_SWITCH) \
808 && !defined(MSWIN32) && !defined(MSWINCE)
809 STATIC int GC_stdout = GC_DEFAULT_STDOUT_FD;
810 STATIC int GC_stderr = GC_DEFAULT_STDERR_FD;
811 STATIC int GC_log = GC_DEFAULT_STDERR_FD;
812
GC_set_log_fd(int fd)813 GC_API void GC_CALL GC_set_log_fd(int fd)
814 {
815 GC_log = fd;
816 }
817 #endif
818
819 #if defined(MSWIN32) && !defined(MSWINRT_FLAVOR) && !defined(MSWIN_XBOX1) \
820 && !defined(SMALL_CONFIG)
GC_win32_MessageBoxA(const char * msg,const char * caption,unsigned flags)821 STATIC void GC_win32_MessageBoxA(const char *msg, const char *caption,
822 unsigned flags)
823 {
824 # ifndef DONT_USE_USER32_DLL
825 /* Use static binding to "user32.dll". */
826 (void)MessageBoxA(NULL, msg, caption, flags);
827 # else
828 /* This simplifies linking - resolve "MessageBoxA" at run-time. */
829 HINSTANCE hU32 = LoadLibrary(TEXT("user32.dll"));
830 if (hU32) {
831 FARPROC pfn = GetProcAddress(hU32, "MessageBoxA");
832 if (pfn)
833 (void)(*(int (WINAPI *)(HWND, LPCSTR, LPCSTR, UINT))(word)pfn)(
834 NULL /* hWnd */, msg, caption, flags);
835 (void)FreeLibrary(hU32);
836 }
837 # endif
838 }
839 #endif /* MSWIN32 */
840
841 #if defined(THREADS) && defined(UNIX_LIKE) && !defined(NO_GETCONTEXT)
callee_saves_pushed_dummy_fn(ptr_t data GC_ATTR_UNUSED,void * context GC_ATTR_UNUSED)842 static void callee_saves_pushed_dummy_fn(ptr_t data GC_ATTR_UNUSED,
843 void * context GC_ATTR_UNUSED) {}
844 #endif
845
846 #ifndef SMALL_CONFIG
847 # ifdef MANUAL_VDB
848 static GC_bool manual_vdb_allowed = TRUE;
849 # else
850 static GC_bool manual_vdb_allowed = FALSE;
851 # endif
852
GC_set_manual_vdb_allowed(int value)853 GC_API void GC_CALL GC_set_manual_vdb_allowed(int value)
854 {
855 manual_vdb_allowed = (GC_bool)value;
856 }
857
GC_get_manual_vdb_allowed(void)858 GC_API int GC_CALL GC_get_manual_vdb_allowed(void)
859 {
860 return (int)manual_vdb_allowed;
861 }
862 #endif /* !SMALL_CONFIG */
863
GC_parse_mem_size_arg(const char * str)864 STATIC word GC_parse_mem_size_arg(const char *str)
865 {
866 word result = 0; /* bad value */
867
868 if (*str != '\0') {
869 char *endptr;
870 char ch;
871
872 result = (word)STRTOULL(str, &endptr, 10);
873 ch = *endptr;
874 if (ch != '\0') {
875 if (*(endptr + 1) != '\0')
876 return 0;
877 /* Allow k, M or G suffix. */
878 switch (ch) {
879 case 'K':
880 case 'k':
881 result <<= 10;
882 break;
883 case 'M':
884 case 'm':
885 result <<= 20;
886 break;
887 case 'G':
888 case 'g':
889 result <<= 30;
890 break;
891 default:
892 result = 0;
893 }
894 }
895 }
896 return result;
897 }
898
899 #define GC_LOG_STD_NAME "gc.log"
900
GC_init(void)901 GC_API void GC_CALL GC_init(void)
902 {
903 /* LOCK(); -- no longer does anything this early. */
904 word initial_heap_sz;
905 IF_CANCEL(int cancel_state;)
906 # if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
907 DCL_LOCK_STATE;
908 # endif
909
910 if (EXPECT(GC_is_initialized, TRUE)) return;
911 # ifdef REDIRECT_MALLOC
912 {
913 static GC_bool init_started = FALSE;
914 if (init_started)
915 ABORT("Redirected malloc() called during GC init");
916 init_started = TRUE;
917 }
918 # endif
919
920 # if defined(GC_INITIAL_HEAP_SIZE) && !defined(CPPCHECK)
921 initial_heap_sz = GC_INITIAL_HEAP_SIZE;
922 # else
923 initial_heap_sz = MINHINCR * HBLKSIZE;
924 # endif
925
926 DISABLE_CANCEL(cancel_state);
927 /* Note that although we are nominally called with the */
928 /* allocation lock held, the allocation lock is now */
929 /* only really acquired once a second thread is forked.*/
930 /* And the initialization code needs to run before */
931 /* then. Thus we really don't hold any locks, and can */
932 /* in fact safely initialize them here. */
933 # ifdef THREADS
934 # ifndef GC_ALWAYS_MULTITHREADED
935 GC_ASSERT(!GC_need_to_lock);
936 # endif
937 # ifdef SN_TARGET_PS3
938 {
939 pthread_mutexattr_t mattr;
940
941 if (0 != pthread_mutexattr_init(&mattr)) {
942 ABORT("pthread_mutexattr_init failed");
943 }
944 if (0 != pthread_mutex_init(&GC_allocate_ml, &mattr)) {
945 ABORT("pthread_mutex_init failed");
946 }
947 (void)pthread_mutexattr_destroy(&mattr);
948 }
949 # endif
950 # endif /* THREADS */
951 # if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
952 # ifndef SPIN_COUNT
953 # define SPIN_COUNT 4000
954 # endif
955 # ifdef MSWINRT_FLAVOR
956 InitializeCriticalSectionAndSpinCount(&GC_allocate_ml, SPIN_COUNT);
957 # else
958 {
959 # ifndef MSWINCE
960 FARPROC pfn = 0;
961 HMODULE hK32 = GetModuleHandle(TEXT("kernel32.dll"));
962 if (hK32)
963 pfn = GetProcAddress(hK32,
964 "InitializeCriticalSectionAndSpinCount");
965 if (pfn) {
966 (*(BOOL (WINAPI *)(LPCRITICAL_SECTION, DWORD))(word)pfn)(
967 &GC_allocate_ml, SPIN_COUNT);
968 } else
969 # endif /* !MSWINCE */
970 /* else */ InitializeCriticalSection(&GC_allocate_ml);
971 }
972 # endif
973 # endif /* GC_WIN32_THREADS */
974 # if (defined(MSWIN32) || defined(MSWINCE)) && defined(THREADS)
975 InitializeCriticalSection(&GC_write_cs);
976 # endif
977 GC_setpagesize();
978 # ifdef MSWIN32
979 GC_init_win32();
980 # endif
981 # ifdef GC_READ_ENV_FILE
982 GC_envfile_init();
983 # endif
984 # if !defined(NO_CLOCK) || !defined(SMALL_CONFIG)
985 # ifdef GC_PRINT_VERBOSE_STATS
986 /* This is useful for debugging and profiling on platforms with */
987 /* missing getenv() (like WinCE). */
988 GC_print_stats = VERBOSE;
989 # else
990 if (0 != GETENV("GC_PRINT_VERBOSE_STATS")) {
991 GC_print_stats = VERBOSE;
992 } else if (0 != GETENV("GC_PRINT_STATS")) {
993 GC_print_stats = 1;
994 }
995 # endif
996 # endif
997 # if ((defined(UNIX_LIKE) && !defined(GC_ANDROID_LOG)) \
998 || defined(CYGWIN32) || defined(SYMBIAN)) && !defined(SMALL_CONFIG)
999 {
1000 char * file_name = TRUSTED_STRING(GETENV("GC_LOG_FILE"));
1001 # ifdef GC_LOG_TO_FILE_ALWAYS
1002 if (NULL == file_name)
1003 file_name = GC_LOG_STD_NAME;
1004 # else
1005 if (0 != file_name)
1006 # endif
1007 {
1008 int log_d = open(file_name, O_CREAT | O_WRONLY | O_APPEND, 0644);
1009 if (log_d < 0) {
1010 GC_err_printf("Failed to open %s as log file\n", file_name);
1011 } else {
1012 char *str;
1013 GC_log = log_d;
1014 str = GETENV("GC_ONLY_LOG_TO_FILE");
1015 # ifdef GC_ONLY_LOG_TO_FILE
1016 /* The similar environment variable set to "0" */
1017 /* overrides the effect of the macro defined. */
1018 if (str != NULL && *str == '0' && *(str + 1) == '\0')
1019 # else
1020 /* Otherwise setting the environment variable */
1021 /* to anything other than "0" will prevent from */
1022 /* redirecting stdout/err to the log file. */
1023 if (str == NULL || (*str == '0' && *(str + 1) == '\0'))
1024 # endif
1025 {
1026 GC_stdout = log_d;
1027 GC_stderr = log_d;
1028 }
1029 }
1030 }
1031 }
1032 # endif
1033 # if !defined(NO_DEBUGGING) && !defined(GC_DUMP_REGULARLY)
1034 if (0 != GETENV("GC_DUMP_REGULARLY")) {
1035 GC_dump_regularly = TRUE;
1036 }
1037 # endif
1038 # ifdef KEEP_BACK_PTRS
1039 {
1040 char * backtraces_string = GETENV("GC_BACKTRACES");
1041 if (0 != backtraces_string) {
1042 GC_backtraces = atol(backtraces_string);
1043 if (backtraces_string[0] == '\0') GC_backtraces = 1;
1044 }
1045 }
1046 # endif
1047 if (0 != GETENV("GC_FIND_LEAK")) {
1048 GC_find_leak = 1;
1049 }
1050 # ifndef SHORT_DBG_HDRS
1051 if (0 != GETENV("GC_FINDLEAK_DELAY_FREE")) {
1052 GC_findleak_delay_free = TRUE;
1053 }
1054 # endif
1055 if (0 != GETENV("GC_ALL_INTERIOR_POINTERS")) {
1056 GC_all_interior_pointers = 1;
1057 }
1058 if (0 != GETENV("GC_DONT_GC")) {
1059 GC_dont_gc = 1;
1060 }
1061 if (0 != GETENV("GC_PRINT_BACK_HEIGHT")) {
1062 GC_print_back_height = TRUE;
1063 }
1064 if (0 != GETENV("GC_NO_BLACKLIST_WARNING")) {
1065 GC_large_alloc_warn_interval = LONG_MAX;
1066 }
1067 {
1068 char * addr_string = GETENV("GC_TRACE");
1069 if (0 != addr_string) {
1070 # ifndef ENABLE_TRACE
1071 WARN("Tracing not enabled: Ignoring GC_TRACE value\n", 0);
1072 # else
1073 word addr = (word)STRTOULL(addr_string, NULL, 16);
1074 if (addr < 0x1000)
1075 WARN("Unlikely trace address: %p\n", (void *)addr);
1076 GC_trace_addr = (ptr_t)addr;
1077 # endif
1078 }
1079 }
1080 # ifdef GC_COLLECT_AT_MALLOC
1081 {
1082 char * string = GETENV("GC_COLLECT_AT_MALLOC");
1083 if (0 != string) {
1084 size_t min_lb = (size_t)STRTOULL(string, NULL, 10);
1085 if (min_lb > 0)
1086 GC_dbg_collect_at_malloc_min_lb = min_lb;
1087 }
1088 }
1089 # endif
1090 # if !defined(GC_DISABLE_INCREMENTAL) && !defined(NO_CLOCK)
1091 {
1092 char * time_limit_string = GETENV("GC_PAUSE_TIME_TARGET");
1093 if (0 != time_limit_string) {
1094 long time_limit = atol(time_limit_string);
1095 if (time_limit > 0) {
1096 GC_time_limit = time_limit;
1097 }
1098 }
1099 }
1100 # endif
1101 # ifndef SMALL_CONFIG
1102 {
1103 char * full_freq_string = GETENV("GC_FULL_FREQUENCY");
1104 if (full_freq_string != NULL) {
1105 int full_freq = atoi(full_freq_string);
1106 if (full_freq > 0)
1107 GC_full_freq = full_freq;
1108 }
1109 }
1110 # endif
1111 {
1112 char * interval_string = GETENV("GC_LARGE_ALLOC_WARN_INTERVAL");
1113 if (0 != interval_string) {
1114 long interval = atol(interval_string);
1115 if (interval <= 0) {
1116 WARN("GC_LARGE_ALLOC_WARN_INTERVAL environment variable has "
1117 "bad value: Ignoring\n", 0);
1118 } else {
1119 GC_large_alloc_warn_interval = interval;
1120 }
1121 }
1122 }
1123 {
1124 char * space_divisor_string = GETENV("GC_FREE_SPACE_DIVISOR");
1125 if (space_divisor_string != NULL) {
1126 int space_divisor = atoi(space_divisor_string);
1127 if (space_divisor > 0)
1128 GC_free_space_divisor = (unsigned)space_divisor;
1129 }
1130 }
1131 # ifdef USE_MUNMAP
1132 {
1133 char * string = GETENV("GC_UNMAP_THRESHOLD");
1134 if (string != NULL) {
1135 if (*string == '0' && *(string + 1) == '\0') {
1136 /* "0" is used to disable unmapping. */
1137 GC_unmap_threshold = 0;
1138 } else {
1139 int unmap_threshold = atoi(string);
1140 if (unmap_threshold > 0)
1141 GC_unmap_threshold = unmap_threshold;
1142 }
1143 }
1144 }
1145 {
1146 char * string = GETENV("GC_FORCE_UNMAP_ON_GCOLLECT");
1147 if (string != NULL) {
1148 if (*string == '0' && *(string + 1) == '\0') {
1149 /* "0" is used to turn off the mode. */
1150 GC_force_unmap_on_gcollect = FALSE;
1151 } else {
1152 GC_force_unmap_on_gcollect = TRUE;
1153 }
1154 }
1155 }
1156 {
1157 char * string = GETENV("GC_USE_ENTIRE_HEAP");
1158 if (string != NULL) {
1159 if (*string == '0' && *(string + 1) == '\0') {
1160 /* "0" is used to turn off the mode. */
1161 GC_use_entire_heap = FALSE;
1162 } else {
1163 GC_use_entire_heap = TRUE;
1164 }
1165 }
1166 }
1167 # endif
1168 # if !defined(NO_DEBUGGING) && !defined(NO_CLOCK)
1169 GET_TIME(GC_init_time);
1170 # endif
1171 maybe_install_looping_handler();
1172 # if ALIGNMENT > GC_DS_TAGS
1173 /* Adjust normal object descriptor for extra allocation. */
1174 if (EXTRA_BYTES != 0)
1175 GC_obj_kinds[NORMAL].ok_descriptor = (word)(-ALIGNMENT) | GC_DS_LENGTH;
1176 # endif
1177 GC_exclude_static_roots_inner(beginGC_arrays, endGC_arrays);
1178 GC_exclude_static_roots_inner(beginGC_obj_kinds, endGC_obj_kinds);
1179 # ifdef SEPARATE_GLOBALS
1180 GC_exclude_static_roots_inner(beginGC_objfreelist, endGC_objfreelist);
1181 GC_exclude_static_roots_inner(beginGC_aobjfreelist, endGC_aobjfreelist);
1182 # endif
1183 # if defined(USE_PROC_FOR_LIBRARIES) && defined(GC_LINUX_THREADS)
1184 WARN("USE_PROC_FOR_LIBRARIES + GC_LINUX_THREADS performs poorly.\n", 0);
1185 /* If thread stacks are cached, they tend to be scanned in */
1186 /* entirety as part of the root set. This will grow them to */
1187 /* maximum size, and is generally not desirable. */
1188 # endif
1189 # if defined(SEARCH_FOR_DATA_START)
1190 GC_init_linux_data_start();
1191 # endif
1192 # if defined(NETBSD) && defined(__ELF__)
1193 GC_init_netbsd_elf();
1194 # endif
1195 # if !defined(THREADS) || defined(GC_PTHREADS) \
1196 || defined(NN_PLATFORM_CTR) || defined(NINTENDO_SWITCH) \
1197 || defined(GC_WIN32_THREADS) || defined(GC_SOLARIS_THREADS)
1198 if (GC_stackbottom == 0) {
1199 GC_stackbottom = GC_get_main_stack_base();
1200 # if (defined(LINUX) || defined(HPUX)) && defined(IA64)
1201 GC_register_stackbottom = GC_get_register_stack_base();
1202 # endif
1203 } else {
1204 # if (defined(LINUX) || defined(HPUX)) && defined(IA64)
1205 if (GC_register_stackbottom == 0) {
1206 WARN("GC_register_stackbottom should be set with GC_stackbottom\n", 0);
1207 /* The following may fail, since we may rely on */
1208 /* alignment properties that may not hold with a user set */
1209 /* GC_stackbottom. */
1210 GC_register_stackbottom = GC_get_register_stack_base();
1211 }
1212 # endif
1213 }
1214 # endif
1215 # if !defined(CPPCHECK)
1216 GC_STATIC_ASSERT(sizeof(ptr_t) == sizeof(word));
1217 GC_STATIC_ASSERT(sizeof(signed_word) == sizeof(word));
1218 # if !defined(_AUX_SOURCE) || defined(__GNUC__)
1219 GC_STATIC_ASSERT((word)(-1) > (word)0);
1220 /* word should be unsigned */
1221 # endif
1222 /* We no longer check for ((void*)(-1) > NULL) since all pointers */
1223 /* are explicitly cast to word in every less/greater comparison. */
1224 GC_STATIC_ASSERT((signed_word)(-1) < (signed_word)0);
1225 # endif
1226 GC_STATIC_ASSERT(sizeof (struct hblk) == HBLKSIZE);
1227 # ifndef THREADS
1228 GC_ASSERT(!((word)GC_stackbottom HOTTER_THAN (word)GC_approx_sp()));
1229 # endif
1230 # ifndef GC_DISABLE_INCREMENTAL
1231 if (GC_incremental || 0 != GETENV("GC_ENABLE_INCREMENTAL")) {
1232 # if defined(BASE_ATOMIC_OPS_EMULATED) || defined(CHECKSUMS) \
1233 || defined(REDIRECT_MALLOC) || defined(REDIRECT_MALLOC_IN_HEADER) \
1234 || defined(SMALL_CONFIG)
1235 /* TODO: Implement CHECKSUMS for manual VDB. */
1236 # else
1237 if (manual_vdb_allowed) {
1238 GC_manual_vdb = TRUE;
1239 GC_incremental = TRUE;
1240 } else
1241 # endif
1242 /* else */ {
1243 /* For GWW_VDB on Win32, this needs to happen before any */
1244 /* heap memory is allocated. */
1245 GC_incremental = GC_dirty_init();
1246 GC_ASSERT(GC_bytes_allocd == 0);
1247 }
1248 }
1249 # endif
1250
1251 /* Add initial guess of root sets. Do this first, since sbrk(0) */
1252 /* might be used. */
1253 if (GC_REGISTER_MAIN_STATIC_DATA()) GC_register_data_segments();
1254 GC_init_headers();
1255 GC_bl_init();
1256 GC_mark_init();
1257 {
1258 char * sz_str = GETENV("GC_INITIAL_HEAP_SIZE");
1259 if (sz_str != NULL) {
1260 initial_heap_sz = GC_parse_mem_size_arg(sz_str);
1261 if (initial_heap_sz <= MINHINCR * HBLKSIZE) {
1262 WARN("Bad initial heap size %s - ignoring it.\n", sz_str);
1263 }
1264 }
1265 }
1266 {
1267 char * sz_str = GETENV("GC_MAXIMUM_HEAP_SIZE");
1268 if (sz_str != NULL) {
1269 word max_heap_sz = GC_parse_mem_size_arg(sz_str);
1270 if (max_heap_sz < initial_heap_sz) {
1271 WARN("Bad maximum heap size %s - ignoring it.\n", sz_str);
1272 }
1273 if (0 == GC_max_retries) GC_max_retries = 2;
1274 GC_set_max_heap_size(max_heap_sz);
1275 }
1276 }
1277 # if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
1278 LOCK(); /* just to set GC_lock_holder */
1279 # endif
1280 if (!GC_expand_hp_inner(divHBLKSZ(initial_heap_sz))) {
1281 GC_err_printf("Can't start up: not enough memory\n");
1282 EXIT();
1283 } else {
1284 GC_requested_heapsize += initial_heap_sz;
1285 }
1286 if (GC_all_interior_pointers)
1287 GC_initialize_offsets();
1288 GC_register_displacement_inner(0L);
1289 # if defined(GC_LINUX_THREADS) && defined(REDIRECT_MALLOC)
1290 if (!GC_all_interior_pointers) {
1291 /* TLS ABI uses pointer-sized offsets for dtv. */
1292 GC_register_displacement_inner(sizeof(void *));
1293 }
1294 # endif
1295 GC_init_size_map();
1296 # ifdef PCR
1297 if (PCR_IL_Lock(PCR_Bool_false, PCR_allSigsBlocked, PCR_waitForever)
1298 != PCR_ERes_okay) {
1299 ABORT("Can't lock load state");
1300 } else if (PCR_IL_Unlock() != PCR_ERes_okay) {
1301 ABORT("Can't unlock load state");
1302 }
1303 PCR_IL_Unlock();
1304 GC_pcr_install();
1305 # endif
1306 GC_is_initialized = TRUE;
1307 # if defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
1308 GC_thr_init();
1309 # ifdef PARALLEL_MARK
1310 /* Actually start helper threads. */
1311 # if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
1312 UNLOCK();
1313 # endif
1314 GC_start_mark_threads_inner();
1315 # if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
1316 LOCK();
1317 # endif
1318 # endif
1319 # endif
1320 COND_DUMP;
1321 /* Get black list set up and/or incremental GC started */
1322 if (!GC_dont_precollect || GC_incremental) {
1323 GC_gcollect_inner();
1324 }
1325 # if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
1326 UNLOCK();
1327 # endif
1328 # if defined(THREADS) && defined(UNIX_LIKE) && !defined(NO_GETCONTEXT)
1329 /* Ensure getcontext_works is set to avoid potential data race. */
1330 if (GC_dont_gc || GC_dont_precollect)
1331 GC_with_callee_saves_pushed(callee_saves_pushed_dummy_fn, NULL);
1332 # endif
1333 # ifndef DONT_USE_ATEXIT
1334 if (GC_find_leak) {
1335 /* This is to give us at least one chance to detect leaks. */
1336 /* This may report some very benign leaks, but ... */
1337 atexit(GC_exit_check);
1338 }
1339 # endif
1340
1341 /* The rest of this again assumes we don't really hold */
1342 /* the allocation lock. */
1343 # if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC) \
1344 || (defined(GC_ALWAYS_MULTITHREADED) && defined(GC_WIN32_THREADS) \
1345 && !defined(GC_NO_THREADS_DISCOVERY))
1346 /* Make sure marker threads are started and thread local */
1347 /* allocation is initialized, in case we didn't get */
1348 /* called from GC_init_parallel. */
1349 GC_init_parallel();
1350 # endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
1351
1352 # if defined(DYNAMIC_LOADING) && defined(DARWIN)
1353 /* This must be called WITHOUT the allocation lock held */
1354 /* and before any threads are created. */
1355 GC_init_dyld();
1356 # endif
1357 RESTORE_CANCEL(cancel_state);
1358 }
1359
GC_enable_incremental(void)1360 GC_API void GC_CALL GC_enable_incremental(void)
1361 {
1362 # if !defined(GC_DISABLE_INCREMENTAL) && !defined(KEEP_BACK_PTRS)
1363 DCL_LOCK_STATE;
1364 /* If we are keeping back pointers, the GC itself dirties all */
1365 /* pages on which objects have been marked, making */
1366 /* incremental GC pointless. */
1367 if (!GC_find_leak && 0 == GETENV("GC_DISABLE_INCREMENTAL")) {
1368 LOCK();
1369 if (!GC_incremental) {
1370 GC_setpagesize();
1371 /* if (GC_no_win32_dlls) goto out; Should be win32S test? */
1372 maybe_install_looping_handler(); /* Before write fault handler! */
1373 if (!GC_is_initialized) {
1374 UNLOCK();
1375 GC_incremental = TRUE; /* indicate intention to turn it on */
1376 GC_init();
1377 LOCK();
1378 } else {
1379 # if !defined(BASE_ATOMIC_OPS_EMULATED) && !defined(CHECKSUMS) \
1380 && !defined(REDIRECT_MALLOC) \
1381 && !defined(REDIRECT_MALLOC_IN_HEADER) && !defined(SMALL_CONFIG)
1382 if (manual_vdb_allowed) {
1383 GC_manual_vdb = TRUE;
1384 GC_incremental = TRUE;
1385 } else
1386 # endif
1387 /* else */ {
1388 GC_incremental = GC_dirty_init();
1389 }
1390 }
1391 if (GC_incremental && !GC_dont_gc) {
1392 /* Can't easily do it if GC_dont_gc. */
1393 IF_CANCEL(int cancel_state;)
1394
1395 DISABLE_CANCEL(cancel_state);
1396 if (GC_bytes_allocd > 0) {
1397 /* There may be unmarked reachable objects. */
1398 GC_gcollect_inner();
1399 }
1400 /* else we're OK in assuming everything's */
1401 /* clean since nothing can point to an */
1402 /* unmarked object. */
1403 GC_read_dirty(FALSE);
1404 RESTORE_CANCEL(cancel_state);
1405 }
1406 }
1407 UNLOCK();
1408 return;
1409 }
1410 # endif
1411 GC_init();
1412 }
1413
1414 #if defined(THREADS)
GC_start_mark_threads(void)1415 GC_API void GC_CALL GC_start_mark_threads(void)
1416 {
1417 # if defined(PARALLEL_MARK) && defined(CAN_HANDLE_FORK) \
1418 && !defined(THREAD_SANITIZER)
1419 /* TSan does not support threads creation in the child process. */
1420 IF_CANCEL(int cancel_state;)
1421
1422 DISABLE_CANCEL(cancel_state);
1423 GC_start_mark_threads_inner();
1424 RESTORE_CANCEL(cancel_state);
1425 # else
1426 /* No action since parallel markers are disabled (or no POSIX fork). */
1427 GC_ASSERT(I_DONT_HOLD_LOCK());
1428 # endif
1429 }
1430 #endif
1431
GC_deinit(void)1432 GC_API void GC_CALL GC_deinit(void)
1433 {
1434 if (GC_is_initialized) {
1435 /* Prevent duplicate resource close. */
1436 GC_is_initialized = FALSE;
1437 # if defined(THREADS) && (defined(MSWIN32) || defined(MSWINCE))
1438 DeleteCriticalSection(&GC_write_cs);
1439 DeleteCriticalSection(&GC_allocate_ml);
1440 # endif
1441 }
1442 }
1443
1444 #if defined(MSWIN32) || defined(MSWINCE)
1445
1446 # if defined(_MSC_VER) && defined(_DEBUG) && !defined(MSWINCE)
1447 # include <crtdbg.h>
1448 # endif
1449
1450 STATIC HANDLE GC_log = 0;
1451
1452 # ifdef THREADS
1453 # if defined(PARALLEL_MARK) && !defined(GC_ALWAYS_MULTITHREADED)
1454 # define IF_NEED_TO_LOCK(x) if (GC_parallel || GC_need_to_lock) x
1455 # else
1456 # define IF_NEED_TO_LOCK(x) if (GC_need_to_lock) x
1457 # endif
1458 # else
1459 # define IF_NEED_TO_LOCK(x)
1460 # endif /* !THREADS */
1461
1462 # ifdef MSWINRT_FLAVOR
1463 # include <windows.storage.h>
1464
1465 /* This API is defined in roapi.h, but we cannot include it here */
1466 /* since it does not compile in C. */
1467 DECLSPEC_IMPORT HRESULT WINAPI RoGetActivationFactory(
1468 HSTRING activatableClassId,
1469 REFIID iid, void** factory);
1470
getWinRTLogPath(wchar_t * buf,size_t bufLen)1471 static GC_bool getWinRTLogPath(wchar_t* buf, size_t bufLen)
1472 {
1473 static const GUID kIID_IApplicationDataStatics = {
1474 0x5612147B, 0xE843, 0x45E3,
1475 0x94, 0xD8, 0x06, 0x16, 0x9E, 0x3C, 0x8E, 0x17
1476 };
1477 static const GUID kIID_IStorageItem = {
1478 0x4207A996, 0xCA2F, 0x42F7,
1479 0xBD, 0xE8, 0x8B, 0x10, 0x45, 0x7A, 0x7F, 0x30
1480 };
1481 GC_bool result = FALSE;
1482 HSTRING_HEADER appDataClassNameHeader;
1483 HSTRING appDataClassName;
1484 __x_ABI_CWindows_CStorage_CIApplicationDataStatics* appDataStatics = 0;
1485
1486 GC_ASSERT(bufLen > 0);
1487 if (SUCCEEDED(WindowsCreateStringReference(
1488 RuntimeClass_Windows_Storage_ApplicationData,
1489 (sizeof(RuntimeClass_Windows_Storage_ApplicationData)-1)
1490 / sizeof(wchar_t),
1491 &appDataClassNameHeader, &appDataClassName))
1492 && SUCCEEDED(RoGetActivationFactory(appDataClassName,
1493 &kIID_IApplicationDataStatics,
1494 &appDataStatics))) {
1495 __x_ABI_CWindows_CStorage_CIApplicationData* appData = NULL;
1496 __x_ABI_CWindows_CStorage_CIStorageFolder* tempFolder = NULL;
1497 __x_ABI_CWindows_CStorage_CIStorageItem* tempFolderItem = NULL;
1498 HSTRING tempPath = NULL;
1499
1500 if (SUCCEEDED(appDataStatics->lpVtbl->get_Current(appDataStatics,
1501 &appData))
1502 && SUCCEEDED(appData->lpVtbl->get_TemporaryFolder(appData,
1503 &tempFolder))
1504 && SUCCEEDED(tempFolder->lpVtbl->QueryInterface(tempFolder,
1505 &kIID_IStorageItem,
1506 &tempFolderItem))
1507 && SUCCEEDED(tempFolderItem->lpVtbl->get_Path(tempFolderItem,
1508 &tempPath))) {
1509 UINT32 tempPathLen;
1510 const wchar_t* tempPathBuf =
1511 WindowsGetStringRawBuffer(tempPath, &tempPathLen);
1512
1513 buf[0] = '\0';
1514 if (wcsncat_s(buf, bufLen, tempPathBuf, tempPathLen) == 0
1515 && wcscat_s(buf, bufLen, L"\\") == 0
1516 && wcscat_s(buf, bufLen, TEXT(GC_LOG_STD_NAME)) == 0)
1517 result = TRUE;
1518 WindowsDeleteString(tempPath);
1519 }
1520
1521 if (tempFolderItem != NULL)
1522 tempFolderItem->lpVtbl->Release(tempFolderItem);
1523 if (tempFolder != NULL)
1524 tempFolder->lpVtbl->Release(tempFolder);
1525 if (appData != NULL)
1526 appData->lpVtbl->Release(appData);
1527 appDataStatics->lpVtbl->Release(appDataStatics);
1528 }
1529 return result;
1530 }
1531 # endif /* MSWINRT_FLAVOR */
1532
GC_CreateLogFile(void)1533 STATIC HANDLE GC_CreateLogFile(void)
1534 {
1535 HANDLE hFile;
1536 # ifdef MSWINRT_FLAVOR
1537 TCHAR pathBuf[_MAX_PATH + 0x10]; /* buffer for path + ext */
1538
1539 hFile = INVALID_HANDLE_VALUE;
1540 if (getWinRTLogPath(pathBuf, _MAX_PATH + 1)) {
1541 CREATEFILE2_EXTENDED_PARAMETERS extParams;
1542
1543 BZERO(&extParams, sizeof(extParams));
1544 extParams.dwSize = sizeof(extParams);
1545 extParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
1546 extParams.dwFileFlags = GC_print_stats == VERBOSE ? 0
1547 : FILE_FLAG_WRITE_THROUGH;
1548 hFile = CreateFile2(pathBuf, GENERIC_WRITE, FILE_SHARE_READ,
1549 CREATE_ALWAYS, &extParams);
1550 }
1551
1552 # else
1553 TCHAR *logPath;
1554 BOOL appendToFile = FALSE;
1555 # if !defined(NO_GETENV_WIN32) || !defined(OLD_WIN32_LOG_FILE)
1556 TCHAR pathBuf[_MAX_PATH + 0x10]; /* buffer for path + ext */
1557
1558 logPath = pathBuf;
1559 # endif
1560
1561 /* Use GetEnvironmentVariable instead of GETENV() for unicode support. */
1562 # ifndef NO_GETENV_WIN32
1563 if (GetEnvironmentVariable(TEXT("GC_LOG_FILE"), pathBuf,
1564 _MAX_PATH + 1) - 1U < (DWORD)_MAX_PATH) {
1565 appendToFile = TRUE;
1566 } else
1567 # endif
1568 /* else */ {
1569 /* Env var not found or its value too long. */
1570 # ifdef OLD_WIN32_LOG_FILE
1571 logPath = TEXT(GC_LOG_STD_NAME);
1572 # else
1573 int len = (int)GetModuleFileName(NULL /* hModule */, pathBuf,
1574 _MAX_PATH + 1);
1575 /* If GetModuleFileName() has failed then len is 0. */
1576 if (len > 4 && pathBuf[len - 4] == (TCHAR)'.') {
1577 len -= 4; /* strip executable file extension */
1578 }
1579 BCOPY(TEXT(".") TEXT(GC_LOG_STD_NAME), &pathBuf[len],
1580 sizeof(TEXT(".") TEXT(GC_LOG_STD_NAME)));
1581 # endif
1582 }
1583
1584 hFile = CreateFile(logPath, GENERIC_WRITE, FILE_SHARE_READ,
1585 NULL /* lpSecurityAttributes */,
1586 appendToFile ? OPEN_ALWAYS : CREATE_ALWAYS,
1587 GC_print_stats == VERBOSE ? FILE_ATTRIBUTE_NORMAL :
1588 /* immediately flush writes unless very verbose */
1589 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
1590 NULL /* hTemplateFile */);
1591
1592 # ifndef NO_GETENV_WIN32
1593 if (appendToFile && hFile != INVALID_HANDLE_VALUE) {
1594 LONG posHigh = 0;
1595 (void)SetFilePointer(hFile, 0, &posHigh, FILE_END);
1596 /* Seek to file end (ignoring any error) */
1597 }
1598 # endif
1599 # endif
1600 return hFile;
1601 }
1602
GC_write(const char * buf,size_t len)1603 STATIC int GC_write(const char *buf, size_t len)
1604 {
1605 BOOL res;
1606 DWORD written;
1607 # if defined(THREADS) && defined(GC_ASSERTIONS)
1608 static GC_bool inside_write = FALSE;
1609 /* to prevent infinite recursion at abort. */
1610 if (inside_write)
1611 return -1;
1612 # endif
1613
1614 if (len == 0)
1615 return 0;
1616 IF_NEED_TO_LOCK(EnterCriticalSection(&GC_write_cs));
1617 # if defined(THREADS) && defined(GC_ASSERTIONS)
1618 if (GC_write_disabled) {
1619 inside_write = TRUE;
1620 ABORT("Assertion failure: GC_write called with write_disabled");
1621 }
1622 # endif
1623 if (GC_log == 0) {
1624 GC_log = GC_CreateLogFile();
1625 }
1626 if (GC_log == INVALID_HANDLE_VALUE) {
1627 IF_NEED_TO_LOCK(LeaveCriticalSection(&GC_write_cs));
1628 # ifdef NO_DEBUGGING
1629 /* Ignore open log failure (e.g., it might be caused by */
1630 /* read-only folder of the client application). */
1631 return 0;
1632 # else
1633 return -1;
1634 # endif
1635 }
1636 res = WriteFile(GC_log, buf, (DWORD)len, &written, NULL);
1637 # if defined(_MSC_VER) && defined(_DEBUG)
1638 # ifdef MSWINCE
1639 /* There is no CrtDbgReport() in WinCE */
1640 {
1641 WCHAR wbuf[1024];
1642 /* Always use Unicode variant of OutputDebugString() */
1643 wbuf[MultiByteToWideChar(CP_ACP, 0 /* dwFlags */,
1644 buf, len, wbuf,
1645 sizeof(wbuf) / sizeof(wbuf[0]) - 1)] = 0;
1646 OutputDebugStringW(wbuf);
1647 }
1648 # else
1649 _CrtDbgReport(_CRT_WARN, NULL, 0, NULL, "%.*s", len, buf);
1650 # endif
1651 # endif
1652 IF_NEED_TO_LOCK(LeaveCriticalSection(&GC_write_cs));
1653 return res ? (int)written : -1;
1654 }
1655
1656 /* TODO: This is pretty ugly ... */
1657 # define WRITE(f, buf, len) GC_write(buf, len)
1658
1659 #elif defined(OS2) || defined(MACOS)
1660 STATIC FILE * GC_stdout = NULL;
1661 STATIC FILE * GC_stderr = NULL;
1662 STATIC FILE * GC_log = NULL;
1663
1664 /* Initialize GC_log (and the friends) passed to GC_write(). */
GC_set_files(void)1665 STATIC void GC_set_files(void)
1666 {
1667 if (GC_stdout == NULL) {
1668 GC_stdout = stdout;
1669 }
1670 if (GC_stderr == NULL) {
1671 GC_stderr = stderr;
1672 }
1673 if (GC_log == NULL) {
1674 GC_log = stderr;
1675 }
1676 }
1677
GC_write(FILE * f,const char * buf,size_t len)1678 GC_INLINE int GC_write(FILE *f, const char *buf, size_t len)
1679 {
1680 int res = fwrite(buf, 1, len, f);
1681 fflush(f);
1682 return res;
1683 }
1684
1685 # define WRITE(f, buf, len) (GC_set_files(), GC_write(f, buf, len))
1686
1687 #elif defined(GC_ANDROID_LOG)
1688
1689 # include <android/log.h>
1690
1691 # ifndef GC_ANDROID_LOG_TAG
1692 # define GC_ANDROID_LOG_TAG "BDWGC"
1693 # endif
1694
1695 # define GC_stdout ANDROID_LOG_DEBUG
1696 # define GC_stderr ANDROID_LOG_ERROR
1697 # define GC_log GC_stdout
1698
1699 # define WRITE(level, buf, unused_len) \
1700 __android_log_write(level, GC_ANDROID_LOG_TAG, buf)
1701
1702 # elif defined(NN_PLATFORM_CTR)
1703 int n3ds_log_write(const char* text, int length);
1704 # define WRITE(level, buf, len) n3ds_log_write(buf, len)
1705 # elif defined(NINTENDO_SWITCH)
1706 int switch_log_write(const char* text, int length);
1707 # define WRITE(level, buf, len) switch_log_write(buf, len)
1708
1709 #else
1710
1711 # if !defined(SN_TARGET_ORBIS) && !defined(SN_TARGET_PSP2)
1712 # if !defined(AMIGA) && !defined(MSWIN_XBOX1) \
1713 && !defined(__CC_ARM)
1714 # include <unistd.h>
1715 # endif
1716 # if !defined(ECOS) && !defined(NOSYS)
1717 # include <errno.h>
1718 # endif
1719 # endif /* !SN_TARGET_ORBIS && !SN_TARGET_PSP2 */
1720
GC_write(int fd,const char * buf,size_t len)1721 STATIC int GC_write(int fd, const char *buf, size_t len)
1722 {
1723 # if defined(ECOS) || defined(SN_TARGET_ORBIS) || defined(SN_TARGET_PSP2) \
1724 || defined(NOSYS)
1725 # ifdef ECOS
1726 /* FIXME: This seems to be defined nowhere at present. */
1727 /* _Jv_diag_write(buf, len); */
1728 # else
1729 /* No writing. */
1730 # endif
1731 return len;
1732 # else
1733 int bytes_written = 0;
1734 IF_CANCEL(int cancel_state;)
1735
1736 DISABLE_CANCEL(cancel_state);
1737 while ((unsigned)bytes_written < len) {
1738 # ifdef GC_SOLARIS_THREADS
1739 int result = syscall(SYS_write, fd, buf + bytes_written,
1740 len - bytes_written);
1741 # else
1742 int result = write(fd, buf + bytes_written, len - bytes_written);
1743 # endif
1744
1745 if (-1 == result) {
1746 if (EAGAIN == errno) /* Resource temporarily unavailable */
1747 continue;
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 /* !MSWIN32 && !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
GC_printf(const char * format,...)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 # if defined(CYGWIN32)
1805 && GC_stdout != GC_DEFAULT_STDOUT_FD
1806 # endif
1807 ) {
1808 ABORT("write to stdout failed");
1809 }
1810 # endif
1811 }
1812 }
1813
GC_err_printf(const char * format,...)1814 void GC_err_printf(const char *format, ...)
1815 {
1816 char buf[BUFSZ + 1];
1817
1818 GC_PRINTF_FILLBUF(buf, format);
1819 GC_err_puts(buf);
1820 }
1821
GC_log_printf(const char * format,...)1822 void GC_log_printf(const char *format, ...)
1823 {
1824 char buf[BUFSZ + 1];
1825
1826 GC_PRINTF_FILLBUF(buf, format);
1827 # ifdef NACL
1828 (void)WRITE(GC_log, buf, strlen(buf));
1829 # else
1830 if (WRITE(GC_log, buf, strlen(buf)) < 0
1831 # if defined(CYGWIN32)
1832 && GC_log != GC_DEFAULT_STDERR_FD
1833 # endif
1834 ) {
1835 ABORT("write to GC log failed");
1836 }
1837 # endif
1838 }
1839
1840 #ifndef GC_ANDROID_LOG
1841
1842 # define GC_warn_printf GC_err_printf
1843
1844 #else
1845
GC_info_log_printf(const char * format,...)1846 GC_INNER void GC_info_log_printf(const char *format, ...)
1847 {
1848 char buf[BUFSZ + 1];
1849
1850 GC_PRINTF_FILLBUF(buf, format);
1851 (void)WRITE(ANDROID_LOG_INFO, buf, 0 /* unused */);
1852 }
1853
GC_verbose_log_printf(const char * format,...)1854 GC_INNER void GC_verbose_log_printf(const char *format, ...)
1855 {
1856 char buf[BUFSZ + 1];
1857
1858 GC_PRINTF_FILLBUF(buf, format);
1859 (void)WRITE(ANDROID_LOG_VERBOSE, buf, 0); /* ignore write errors */
1860 }
1861
GC_warn_printf(const char * format,...)1862 STATIC void GC_warn_printf(const char *format, ...)
1863 {
1864 char buf[BUFSZ + 1];
1865
1866 GC_PRINTF_FILLBUF(buf, format);
1867 (void)WRITE(ANDROID_LOG_WARN, buf, 0);
1868 }
1869
1870 #endif /* GC_ANDROID_LOG */
1871
GC_err_puts(const char * s)1872 void GC_err_puts(const char *s)
1873 {
1874 (void)WRITE(GC_stderr, s, strlen(s)); /* ignore errors */
1875 }
1876
GC_default_warn_proc(char * msg,GC_word arg)1877 STATIC void GC_CALLBACK GC_default_warn_proc(char *msg, GC_word arg)
1878 {
1879 /* TODO: Add assertion on arg comply with msg (format). */
1880 GC_warn_printf(msg, arg);
1881 }
1882
1883 GC_INNER GC_warn_proc GC_current_warn_proc = GC_default_warn_proc;
1884
1885 /* This is recommended for production code (release). */
GC_ignore_warn_proc(char * msg,GC_word arg)1886 GC_API void GC_CALLBACK GC_ignore_warn_proc(char *msg, GC_word arg)
1887 {
1888 if (GC_print_stats) {
1889 /* Don't ignore warnings if stats printing is on. */
1890 GC_default_warn_proc(msg, arg);
1891 }
1892 }
1893
GC_set_warn_proc(GC_warn_proc p)1894 GC_API void GC_CALL GC_set_warn_proc(GC_warn_proc p)
1895 {
1896 DCL_LOCK_STATE;
1897 GC_ASSERT(NONNULL_ARG_NOT_NULL(p));
1898 # ifdef GC_WIN32_THREADS
1899 # ifdef CYGWIN32
1900 /* Need explicit GC_INIT call */
1901 GC_ASSERT(GC_is_initialized);
1902 # else
1903 if (!GC_is_initialized) GC_init();
1904 # endif
1905 # endif
1906 LOCK();
1907 GC_current_warn_proc = p;
1908 UNLOCK();
1909 }
1910
GC_get_warn_proc(void)1911 GC_API GC_warn_proc GC_CALL GC_get_warn_proc(void)
1912 {
1913 GC_warn_proc result;
1914 DCL_LOCK_STATE;
1915 LOCK();
1916 result = GC_current_warn_proc;
1917 UNLOCK();
1918 return(result);
1919 }
1920
1921 #if !defined(PCR) && !defined(SMALL_CONFIG)
1922 /* Print (or display) a message before abnormal exit (including */
1923 /* abort). Invoked from ABORT(msg) macro (there msg is non-NULL) */
1924 /* and from EXIT() macro (msg is NULL in that case). */
GC_default_on_abort(const char * msg)1925 STATIC void GC_CALLBACK GC_default_on_abort(const char *msg)
1926 {
1927 # ifndef DONT_USE_ATEXIT
1928 skip_gc_atexit = TRUE; /* disable at-exit GC_gcollect() */
1929 # endif
1930
1931 if (msg != NULL) {
1932 # if defined(MSWIN32) && !defined(MSWINRT_FLAVOR) && !defined(MSWIN_XBOX1)
1933 GC_win32_MessageBoxA(msg, "Fatal error in GC", MB_ICONERROR | MB_OK);
1934 /* Also duplicate msg to GC log file. */
1935 # endif
1936
1937 # ifndef GC_ANDROID_LOG
1938 /* Avoid calling GC_err_printf() here, as GC_on_abort() could be */
1939 /* called from it. Note 1: this is not an atomic output. */
1940 /* Note 2: possible write errors are ignored. */
1941 # if defined(THREADS) && defined(GC_ASSERTIONS) \
1942 && (defined(MSWIN32) || defined(MSWINCE))
1943 if (!GC_write_disabled)
1944 # endif
1945 {
1946 if (WRITE(GC_stderr, msg, strlen(msg)) >= 0)
1947 (void)WRITE(GC_stderr, "\n", 1);
1948 }
1949 # else
1950 __android_log_assert("*" /* cond */, GC_ANDROID_LOG_TAG, "%s\n", msg);
1951 # endif
1952 }
1953
1954 # if !defined(NO_DEBUGGING) && !defined(GC_ANDROID_LOG)
1955 if (GETENV("GC_LOOP_ON_ABORT") != NULL) {
1956 /* In many cases it's easier to debug a running process. */
1957 /* It's arguably nicer to sleep, but that makes it harder */
1958 /* to look at the thread if the debugger doesn't know much */
1959 /* about threads. */
1960 for(;;) {
1961 /* Empty */
1962 }
1963 }
1964 # endif
1965 }
1966
1967 GC_abort_func GC_on_abort = GC_default_on_abort;
1968
GC_set_abort_func(GC_abort_func fn)1969 GC_API void GC_CALL GC_set_abort_func(GC_abort_func fn)
1970 {
1971 DCL_LOCK_STATE;
1972 GC_ASSERT(NONNULL_ARG_NOT_NULL(fn));
1973 LOCK();
1974 GC_on_abort = fn;
1975 UNLOCK();
1976 }
1977
GC_get_abort_func(void)1978 GC_API GC_abort_func GC_CALL GC_get_abort_func(void)
1979 {
1980 GC_abort_func fn;
1981 DCL_LOCK_STATE;
1982 LOCK();
1983 fn = GC_on_abort;
1984 UNLOCK();
1985 return fn;
1986 }
1987 #endif /* !SMALL_CONFIG */
1988
GC_enable(void)1989 GC_API void GC_CALL GC_enable(void)
1990 {
1991 DCL_LOCK_STATE;
1992
1993 LOCK();
1994 GC_ASSERT(GC_dont_gc != 0); /* ensure no counter underflow */
1995 GC_dont_gc--;
1996 UNLOCK();
1997 }
1998
GC_disable(void)1999 GC_API void GC_CALL GC_disable(void)
2000 {
2001 DCL_LOCK_STATE;
2002 LOCK();
2003 GC_dont_gc++;
2004 UNLOCK();
2005 }
2006
GC_is_disabled(void)2007 GC_API int GC_CALL GC_is_disabled(void)
2008 {
2009 return GC_dont_gc != 0;
2010 }
2011
2012 /* Helper procedures for new kind creation. */
GC_new_free_list_inner(void)2013 GC_API void ** GC_CALL GC_new_free_list_inner(void)
2014 {
2015 void *result;
2016
2017 GC_ASSERT(I_HOLD_LOCK());
2018 result = GC_INTERNAL_MALLOC((MAXOBJGRANULES+1) * sizeof(ptr_t), PTRFREE);
2019 if (NULL == result) ABORT("Failed to allocate freelist for new kind");
2020 BZERO(result, (MAXOBJGRANULES+1)*sizeof(ptr_t));
2021 return (void **)result;
2022 }
2023
GC_new_free_list(void)2024 GC_API void ** GC_CALL GC_new_free_list(void)
2025 {
2026 void ** result;
2027 DCL_LOCK_STATE;
2028 LOCK();
2029 result = GC_new_free_list_inner();
2030 UNLOCK();
2031 return result;
2032 }
2033
GC_new_kind_inner(void ** fl,GC_word descr,int adjust,int clear)2034 GC_API unsigned GC_CALL GC_new_kind_inner(void **fl, GC_word descr,
2035 int adjust, int clear)
2036 {
2037 unsigned result = GC_n_kinds;
2038
2039 GC_ASSERT(adjust == FALSE || adjust == TRUE);
2040 /* If an object is not needed to be cleared (when moved to the */
2041 /* free list) then its descriptor should be zero to denote */
2042 /* a pointer-free object (and, as a consequence, the size of the */
2043 /* object should not be added to the descriptor template). */
2044 GC_ASSERT(clear == TRUE
2045 || (descr == 0 && adjust == FALSE && clear == FALSE));
2046 if (result < MAXOBJKINDS) {
2047 GC_n_kinds++;
2048 GC_obj_kinds[result].ok_freelist = fl;
2049 GC_obj_kinds[result].ok_reclaim_list = 0;
2050 GC_obj_kinds[result].ok_descriptor = descr;
2051 GC_obj_kinds[result].ok_relocate_descr = adjust;
2052 GC_obj_kinds[result].ok_init = (GC_bool)clear;
2053 # ifdef ENABLE_DISCLAIM
2054 GC_obj_kinds[result].ok_mark_unconditionally = FALSE;
2055 GC_obj_kinds[result].ok_disclaim_proc = 0;
2056 # endif
2057 } else {
2058 ABORT("Too many kinds");
2059 }
2060 return result;
2061 }
2062
GC_new_kind(void ** fl,GC_word descr,int adjust,int clear)2063 GC_API unsigned GC_CALL GC_new_kind(void **fl, GC_word descr, int adjust,
2064 int clear)
2065 {
2066 unsigned result;
2067 DCL_LOCK_STATE;
2068 LOCK();
2069 result = GC_new_kind_inner(fl, descr, adjust, clear);
2070 UNLOCK();
2071 return result;
2072 }
2073
GC_new_proc_inner(GC_mark_proc proc)2074 GC_API unsigned GC_CALL GC_new_proc_inner(GC_mark_proc proc)
2075 {
2076 unsigned result = GC_n_mark_procs;
2077
2078 if (result < MAX_MARK_PROCS) {
2079 GC_n_mark_procs++;
2080 GC_mark_procs[result] = proc;
2081 } else {
2082 ABORT("Too many mark procedures");
2083 }
2084 return result;
2085 }
2086
GC_new_proc(GC_mark_proc proc)2087 GC_API unsigned GC_CALL GC_new_proc(GC_mark_proc proc)
2088 {
2089 unsigned result;
2090 DCL_LOCK_STATE;
2091 LOCK();
2092 result = GC_new_proc_inner(proc);
2093 UNLOCK();
2094 return result;
2095 }
2096
GC_call_with_alloc_lock(GC_fn_type fn,void * client_data)2097 GC_API void * GC_CALL GC_call_with_alloc_lock(GC_fn_type fn, void *client_data)
2098 {
2099 void * result;
2100 DCL_LOCK_STATE;
2101
2102 # ifdef THREADS
2103 LOCK();
2104 # endif
2105 result = (*fn)(client_data);
2106 # ifdef THREADS
2107 UNLOCK();
2108 # endif
2109 return(result);
2110 }
2111
GC_call_with_stack_base(GC_stack_base_func fn,void * arg)2112 GC_API void * GC_CALL GC_call_with_stack_base(GC_stack_base_func fn, void *arg)
2113 {
2114 struct GC_stack_base base;
2115 void *result;
2116
2117 base.mem_base = (void *)&base;
2118 # ifdef IA64
2119 base.reg_base = (void *)GC_save_regs_in_stack();
2120 /* Unnecessarily flushes register stack, */
2121 /* but that probably doesn't hurt. */
2122 # endif
2123 result = fn(&base, arg);
2124 /* Strongly discourage the compiler from treating the above */
2125 /* as a tail call. */
2126 GC_noop1(COVERT_DATAFLOW(&base));
2127 return result;
2128 }
2129
2130 #ifndef THREADS
2131
2132 GC_INNER ptr_t GC_blocked_sp = NULL;
2133 /* NULL value means we are not inside GC_do_blocking() call. */
2134 # ifdef IA64
2135 STATIC ptr_t GC_blocked_register_sp = NULL;
2136 # endif
2137
2138 GC_INNER struct GC_traced_stack_sect_s *GC_traced_stack_sect = NULL;
2139
2140 /* This is nearly the same as in win32_threads.c */
GC_call_with_gc_active(GC_fn_type fn,void * client_data)2141 GC_API void * GC_CALL GC_call_with_gc_active(GC_fn_type fn,
2142 void * client_data)
2143 {
2144 struct GC_traced_stack_sect_s stacksect;
2145 GC_ASSERT(GC_is_initialized);
2146
2147 /* Adjust our stack base value (this could happen if */
2148 /* GC_get_main_stack_base() is unimplemented or broken for */
2149 /* the platform). */
2150 if ((word)GC_stackbottom HOTTER_THAN (word)(&stacksect))
2151 GC_stackbottom = (ptr_t)COVERT_DATAFLOW(&stacksect);
2152
2153 if (GC_blocked_sp == NULL) {
2154 /* We are not inside GC_do_blocking() - do nothing more. */
2155 client_data = fn(client_data);
2156 /* Prevent treating the above as a tail call. */
2157 GC_noop1(COVERT_DATAFLOW(&stacksect));
2158 return client_data; /* result */
2159 }
2160
2161 /* Setup new "stack section". */
2162 stacksect.saved_stack_ptr = GC_blocked_sp;
2163 # ifdef IA64
2164 /* This is the same as in GC_call_with_stack_base(). */
2165 stacksect.backing_store_end = GC_save_regs_in_stack();
2166 /* Unnecessarily flushes register stack, */
2167 /* but that probably doesn't hurt. */
2168 stacksect.saved_backing_store_ptr = GC_blocked_register_sp;
2169 # endif
2170 stacksect.prev = GC_traced_stack_sect;
2171 GC_blocked_sp = NULL;
2172 GC_traced_stack_sect = &stacksect;
2173
2174 client_data = fn(client_data);
2175 GC_ASSERT(GC_blocked_sp == NULL);
2176 GC_ASSERT(GC_traced_stack_sect == &stacksect);
2177
2178 # if defined(CPPCHECK)
2179 GC_noop1((word)GC_traced_stack_sect - (word)GC_blocked_sp);
2180 # endif
2181 /* Restore original "stack section". */
2182 GC_traced_stack_sect = stacksect.prev;
2183 # ifdef IA64
2184 GC_blocked_register_sp = stacksect.saved_backing_store_ptr;
2185 # endif
2186 GC_blocked_sp = stacksect.saved_stack_ptr;
2187
2188 return client_data; /* result */
2189 }
2190
2191 /* This is nearly the same as in win32_threads.c */
GC_do_blocking_inner(ptr_t data,void * context GC_ATTR_UNUSED)2192 STATIC void GC_do_blocking_inner(ptr_t data, void * context GC_ATTR_UNUSED)
2193 {
2194 struct blocking_data * d = (struct blocking_data *) data;
2195 GC_ASSERT(GC_is_initialized);
2196 GC_ASSERT(GC_blocked_sp == NULL);
2197 # ifdef SPARC
2198 GC_blocked_sp = GC_save_regs_in_stack();
2199 # else
2200 GC_blocked_sp = (ptr_t) &d; /* save approx. sp */
2201 # endif
2202 # ifdef IA64
2203 GC_blocked_register_sp = GC_save_regs_in_stack();
2204 # endif
2205
2206 d -> client_data = (d -> fn)(d -> client_data);
2207
2208 # ifdef SPARC
2209 GC_ASSERT(GC_blocked_sp != NULL);
2210 # else
2211 GC_ASSERT(GC_blocked_sp == (ptr_t)(&d));
2212 # endif
2213 # if defined(CPPCHECK)
2214 GC_noop1((word)GC_blocked_sp);
2215 # endif
2216 GC_blocked_sp = NULL;
2217 }
2218
2219 #endif /* !THREADS */
2220
GC_do_blocking(GC_fn_type fn,void * client_data)2221 GC_API void * GC_CALL GC_do_blocking(GC_fn_type fn, void * client_data)
2222 {
2223 struct blocking_data my_data;
2224
2225 my_data.fn = fn;
2226 my_data.client_data = client_data;
2227 GC_with_callee_saves_pushed(GC_do_blocking_inner, (ptr_t)(&my_data));
2228 return my_data.client_data; /* result */
2229 }
2230
2231 #if !defined(NO_DEBUGGING)
GC_dump(void)2232 GC_API void GC_CALL GC_dump(void)
2233 {
2234 DCL_LOCK_STATE;
2235
2236 LOCK();
2237 GC_dump_named(NULL);
2238 UNLOCK();
2239 }
2240
GC_dump_named(const char * name)2241 GC_API void GC_CALL GC_dump_named(const char *name)
2242 {
2243 # ifndef NO_CLOCK
2244 CLOCK_TYPE current_time;
2245
2246 GET_TIME(current_time);
2247 # endif
2248 if (name != NULL) {
2249 GC_printf("***GC Dump %s\n", name);
2250 } else {
2251 GC_printf("***GC Dump collection #%lu\n", (unsigned long)GC_gc_no);
2252 }
2253 # ifndef NO_CLOCK
2254 /* Note that the time is wrapped in ~49 days if sizeof(long)==4. */
2255 GC_printf("Time since GC init: %lu msecs\n",
2256 MS_TIME_DIFF(current_time, GC_init_time));
2257 # endif
2258
2259 GC_printf("\n***Static roots:\n");
2260 GC_print_static_roots();
2261 GC_printf("\n***Heap sections:\n");
2262 GC_print_heap_sects();
2263 GC_printf("\n***Free blocks:\n");
2264 GC_print_hblkfreelist();
2265 GC_printf("\n***Blocks in use:\n");
2266 GC_print_block_list();
2267 }
2268 #endif /* !NO_DEBUGGING */
2269
block_add_size(struct hblk * h,word pbytes)2270 static void block_add_size(struct hblk *h, word pbytes)
2271 {
2272 hdr *hhdr = HDR(h);
2273 *(word *)pbytes += (WORDS_TO_BYTES(hhdr->hb_sz) + (HBLKSIZE - 1))
2274 & ~(word)(HBLKSIZE - 1);
2275 }
2276
GC_get_memory_use(void)2277 GC_API size_t GC_CALL GC_get_memory_use(void)
2278 {
2279 word bytes = 0;
2280 DCL_LOCK_STATE;
2281
2282 LOCK();
2283 GC_apply_to_all_blocks(block_add_size, (word)(&bytes));
2284 UNLOCK();
2285 return (size_t)bytes;
2286 }
2287
2288 /* Getter functions for the public Read-only variables. */
2289
2290 /* GC_get_gc_no() is unsynchronized and should be typically called */
2291 /* inside the context of GC_call_with_alloc_lock() to prevent data */
2292 /* races (on multiprocessors). */
GC_get_gc_no(void)2293 GC_API GC_word GC_CALL GC_get_gc_no(void)
2294 {
2295 return GC_gc_no;
2296 }
2297
2298 #ifdef THREADS
GC_get_parallel(void)2299 GC_API int GC_CALL GC_get_parallel(void)
2300 {
2301 /* GC_parallel is initialized at start-up. */
2302 return GC_parallel;
2303 }
2304
2305 GC_INNER GC_on_thread_event_proc GC_on_thread_event = 0;
2306
GC_set_on_thread_event(GC_on_thread_event_proc fn)2307 GC_API void GC_CALL GC_set_on_thread_event(GC_on_thread_event_proc fn)
2308 {
2309 /* fn may be 0 (means no event notifier). */
2310 DCL_LOCK_STATE;
2311 LOCK();
2312 GC_on_thread_event = fn;
2313 UNLOCK();
2314 }
2315
GC_get_on_thread_event(void)2316 GC_API GC_on_thread_event_proc GC_CALL GC_get_on_thread_event(void)
2317 {
2318 GC_on_thread_event_proc fn;
2319 DCL_LOCK_STATE;
2320 LOCK();
2321 fn = GC_on_thread_event;
2322 UNLOCK();
2323 return fn;
2324 }
2325 #endif /* THREADS */
2326
2327 /* Setter and getter functions for the public R/W function variables. */
2328 /* These functions are synchronized (like GC_set_warn_proc() and */
2329 /* GC_get_warn_proc()). */
2330
GC_set_oom_fn(GC_oom_func fn)2331 GC_API void GC_CALL GC_set_oom_fn(GC_oom_func fn)
2332 {
2333 GC_ASSERT(NONNULL_ARG_NOT_NULL(fn));
2334 DCL_LOCK_STATE;
2335 LOCK();
2336 GC_oom_fn = fn;
2337 UNLOCK();
2338 }
2339
GC_get_oom_fn(void)2340 GC_API GC_oom_func GC_CALL GC_get_oom_fn(void)
2341 {
2342 GC_oom_func fn;
2343 DCL_LOCK_STATE;
2344 LOCK();
2345 fn = GC_oom_fn;
2346 UNLOCK();
2347 return fn;
2348 }
2349
GC_set_on_heap_resize(GC_on_heap_resize_proc fn)2350 GC_API void GC_CALL GC_set_on_heap_resize(GC_on_heap_resize_proc fn)
2351 {
2352 /* fn may be 0 (means no event notifier). */
2353 DCL_LOCK_STATE;
2354 LOCK();
2355 GC_on_heap_resize = fn;
2356 UNLOCK();
2357 }
2358
GC_get_on_heap_resize(void)2359 GC_API GC_on_heap_resize_proc GC_CALL GC_get_on_heap_resize(void)
2360 {
2361 GC_on_heap_resize_proc fn;
2362 DCL_LOCK_STATE;
2363 LOCK();
2364 fn = GC_on_heap_resize;
2365 UNLOCK();
2366 return fn;
2367 }
2368
GC_set_finalizer_notifier(GC_finalizer_notifier_proc fn)2369 GC_API void GC_CALL GC_set_finalizer_notifier(GC_finalizer_notifier_proc fn)
2370 {
2371 /* fn may be 0 (means no finalizer notifier). */
2372 DCL_LOCK_STATE;
2373 LOCK();
2374 GC_finalizer_notifier = fn;
2375 UNLOCK();
2376 }
2377
GC_get_finalizer_notifier(void)2378 GC_API GC_finalizer_notifier_proc GC_CALL GC_get_finalizer_notifier(void)
2379 {
2380 GC_finalizer_notifier_proc fn;
2381 DCL_LOCK_STATE;
2382 LOCK();
2383 fn = GC_finalizer_notifier;
2384 UNLOCK();
2385 return fn;
2386 }
2387
2388 /* Setter and getter functions for the public numeric R/W variables. */
2389 /* It is safe to call these functions even before GC_INIT(). */
2390 /* These functions are unsynchronized and should be typically called */
2391 /* inside the context of GC_call_with_alloc_lock() (if called after */
2392 /* GC_INIT()) to prevent data races (unless it is guaranteed the */
2393 /* collector is not multi-threaded at that execution point). */
2394
GC_set_find_leak(int value)2395 GC_API void GC_CALL GC_set_find_leak(int value)
2396 {
2397 /* value is of boolean type. */
2398 GC_find_leak = value;
2399 }
2400
GC_get_find_leak(void)2401 GC_API int GC_CALL GC_get_find_leak(void)
2402 {
2403 return GC_find_leak;
2404 }
2405
GC_set_all_interior_pointers(int value)2406 GC_API void GC_CALL GC_set_all_interior_pointers(int value)
2407 {
2408 DCL_LOCK_STATE;
2409
2410 GC_all_interior_pointers = value ? 1 : 0;
2411 if (GC_is_initialized) {
2412 /* It is not recommended to change GC_all_interior_pointers value */
2413 /* after GC is initialized but it seems GC could work correctly */
2414 /* even after switching the mode. */
2415 LOCK();
2416 GC_initialize_offsets(); /* NOTE: this resets manual offsets as well */
2417 if (!GC_all_interior_pointers)
2418 GC_bl_init_no_interiors();
2419 UNLOCK();
2420 }
2421 }
2422
GC_get_all_interior_pointers(void)2423 GC_API int GC_CALL GC_get_all_interior_pointers(void)
2424 {
2425 return GC_all_interior_pointers;
2426 }
2427
GC_set_finalize_on_demand(int value)2428 GC_API void GC_CALL GC_set_finalize_on_demand(int value)
2429 {
2430 GC_ASSERT(value != -1); /* -1 was used to retrieve old value in gc-7.2 */
2431 /* value is of boolean type. */
2432 GC_finalize_on_demand = value;
2433 }
2434
GC_get_finalize_on_demand(void)2435 GC_API int GC_CALL GC_get_finalize_on_demand(void)
2436 {
2437 return GC_finalize_on_demand;
2438 }
2439
GC_set_java_finalization(int value)2440 GC_API void GC_CALL GC_set_java_finalization(int value)
2441 {
2442 GC_ASSERT(value != -1); /* -1 was used to retrieve old value in gc-7.2 */
2443 /* value is of boolean type. */
2444 GC_java_finalization = value;
2445 }
2446
GC_get_java_finalization(void)2447 GC_API int GC_CALL GC_get_java_finalization(void)
2448 {
2449 return GC_java_finalization;
2450 }
2451
GC_set_dont_expand(int value)2452 GC_API void GC_CALL GC_set_dont_expand(int value)
2453 {
2454 GC_ASSERT(value != -1); /* -1 was used to retrieve old value in gc-7.2 */
2455 /* value is of boolean type. */
2456 GC_dont_expand = value;
2457 }
2458
GC_get_dont_expand(void)2459 GC_API int GC_CALL GC_get_dont_expand(void)
2460 {
2461 return GC_dont_expand;
2462 }
2463
GC_set_no_dls(int value)2464 GC_API void GC_CALL GC_set_no_dls(int value)
2465 {
2466 GC_ASSERT(value != -1); /* -1 was used to retrieve old value in gc-7.2 */
2467 /* value is of boolean type. */
2468 GC_no_dls = value;
2469 }
2470
GC_get_no_dls(void)2471 GC_API int GC_CALL GC_get_no_dls(void)
2472 {
2473 return GC_no_dls;
2474 }
2475
GC_set_non_gc_bytes(GC_word value)2476 GC_API void GC_CALL GC_set_non_gc_bytes(GC_word value)
2477 {
2478 GC_non_gc_bytes = value;
2479 }
2480
GC_get_non_gc_bytes(void)2481 GC_API GC_word GC_CALL GC_get_non_gc_bytes(void)
2482 {
2483 return GC_non_gc_bytes;
2484 }
2485
GC_set_free_space_divisor(GC_word value)2486 GC_API void GC_CALL GC_set_free_space_divisor(GC_word value)
2487 {
2488 GC_ASSERT(value > 0);
2489 GC_free_space_divisor = value;
2490 }
2491
GC_get_free_space_divisor(void)2492 GC_API GC_word GC_CALL GC_get_free_space_divisor(void)
2493 {
2494 return GC_free_space_divisor;
2495 }
2496
GC_set_max_retries(GC_word value)2497 GC_API void GC_CALL GC_set_max_retries(GC_word value)
2498 {
2499 GC_ASSERT((GC_signed_word)value != -1);
2500 /* -1 was used to retrieve old value in gc-7.2 */
2501 GC_max_retries = value;
2502 }
2503
GC_get_max_retries(void)2504 GC_API GC_word GC_CALL GC_get_max_retries(void)
2505 {
2506 return GC_max_retries;
2507 }
2508
GC_set_dont_precollect(int value)2509 GC_API void GC_CALL GC_set_dont_precollect(int value)
2510 {
2511 GC_ASSERT(value != -1); /* -1 was used to retrieve old value in gc-7.2 */
2512 /* value is of boolean type. */
2513 GC_dont_precollect = value;
2514 }
2515
GC_get_dont_precollect(void)2516 GC_API int GC_CALL GC_get_dont_precollect(void)
2517 {
2518 return GC_dont_precollect;
2519 }
2520
GC_set_full_freq(int value)2521 GC_API void GC_CALL GC_set_full_freq(int value)
2522 {
2523 GC_ASSERT(value >= 0);
2524 GC_full_freq = value;
2525 }
2526
GC_get_full_freq(void)2527 GC_API int GC_CALL GC_get_full_freq(void)
2528 {
2529 return GC_full_freq;
2530 }
2531
GC_set_time_limit(unsigned long value)2532 GC_API void GC_CALL GC_set_time_limit(unsigned long value)
2533 {
2534 GC_ASSERT((long)value != -1L);
2535 /* -1 was used to retrieve old value in gc-7.2 */
2536 GC_time_limit = value;
2537 }
2538
GC_get_time_limit(void)2539 GC_API unsigned long GC_CALL GC_get_time_limit(void)
2540 {
2541 return GC_time_limit;
2542 }
2543
GC_set_force_unmap_on_gcollect(int value)2544 GC_API void GC_CALL GC_set_force_unmap_on_gcollect(int value)
2545 {
2546 GC_force_unmap_on_gcollect = (GC_bool)value;
2547 }
2548
GC_get_force_unmap_on_gcollect(void)2549 GC_API int GC_CALL GC_get_force_unmap_on_gcollect(void)
2550 {
2551 return (int)GC_force_unmap_on_gcollect;
2552 }
2553
GC_abort_on_oom(void)2554 GC_API void GC_CALL GC_abort_on_oom(void)
2555 {
2556 GC_err_printf("Insufficient memory for the allocation\n");
2557 EXIT();
2558 }
2559
2560 #ifdef THREADS
GC_stop_world_external(void)2561 GC_API void GC_CALL GC_stop_world_external(void)
2562 {
2563 GC_ASSERT(GC_is_initialized);
2564 LOCK();
2565 # ifdef THREAD_LOCAL_ALLOC
2566 GC_ASSERT(!GC_world_stopped);
2567 # endif
2568 STOP_WORLD();
2569 # ifdef THREAD_LOCAL_ALLOC
2570 GC_world_stopped = TRUE;
2571 # endif
2572 }
2573
GC_start_world_external(void)2574 GC_API void GC_CALL GC_start_world_external(void)
2575 {
2576 # ifdef THREAD_LOCAL_ALLOC
2577 GC_ASSERT(GC_world_stopped);
2578 GC_world_stopped = FALSE;
2579 # else
2580 GC_ASSERT(GC_is_initialized);
2581 # endif
2582 START_WORLD();
2583 UNLOCK();
2584 }
2585 #endif /* THREADS */
2586