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