1 /* 2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers 3 * Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved. 4 * Copyright 1996-1999 by Silicon Graphics. All rights reserved. 5 * Copyright 1999 by Hewlett-Packard Company. All rights reserved. 6 * Copyright (C) 2007 Free Software Foundation, Inc 7 * Copyright (c) 2000-2011 by Hewlett-Packard Development Company. 8 * Copyright (c) 2009-2018 Ivan Maidanski 9 * 10 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED 11 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. 12 * 13 * Permission is hereby granted to use or copy this program 14 * for any purpose, provided the above notices are retained on all copies. 15 * Permission to modify the code and to distribute modified code is granted, 16 * provided the above notices are retained, and a notice that the code was 17 * modified is included with the above copyright notice. 18 */ 19 20 /* 21 * Note that this defines a large number of tuning hooks, which can 22 * safely be ignored in nearly all cases. For normal use it suffices 23 * to call only GC_MALLOC and perhaps GC_REALLOC. 24 * For better performance, also look at GC_MALLOC_ATOMIC, and 25 * GC_enable_incremental. If you need an action to be performed 26 * immediately before an object is collected, look at GC_register_finalizer. 27 * Everything else is best ignored unless you encounter performance 28 * problems. 29 */ 30 31 #ifndef GC_H 32 #define GC_H 33 34 /* Help debug mixed up preprocessor symbols. */ 35 #if (defined(WIN64) && !defined(_WIN64)) && defined(_MSC_VER) 36 #pragma message("Warning: Expecting _WIN64 for x64 targets! Notice the leading underscore!") 37 #endif 38 39 #include "gc_version.h" 40 /* Define version numbers here to allow test on build machine */ 41 /* for cross-builds. Note that this defines the header */ 42 /* version number, which may or may not match that of the */ 43 /* dynamic library. GC_get_version() can be used to obtain */ 44 /* the latter. */ 45 46 #include "gc_config_macros.h" 47 48 #ifdef __cplusplus 49 extern "C" { 50 #endif 51 52 typedef void * GC_PTR; /* preserved only for backward compatibility */ 53 54 /* Define word and signed_word to be unsigned and signed types of the */ 55 /* size as char * or void *. There seems to be no way to do this */ 56 /* even semi-portably. The following is probably no better/worse */ 57 /* than almost anything else. */ 58 /* The ANSI standard suggests that size_t and ptrdiff_t might be */ 59 /* better choices. But those had incorrect definitions on some older */ 60 /* systems. Notably "typedef int size_t" is WRONG. */ 61 #ifdef _WIN64 62 # if defined(__int64) && !defined(CPPCHECK) 63 typedef unsigned __int64 GC_word; 64 typedef __int64 GC_signed_word; 65 # else 66 typedef unsigned long long GC_word; 67 typedef long long GC_signed_word; 68 # endif 69 #else 70 typedef unsigned long GC_word; 71 typedef long GC_signed_word; 72 #endif 73 74 /* Get the GC library version. The returned value is a constant in the */ 75 /* form: ((version_major<<16) | (version_minor<<8) | version_micro). */ 76 GC_API unsigned GC_CALL GC_get_version(void); 77 78 /* Public read-only variables */ 79 /* The supplied getter functions are preferred for new code. */ 80 81 GC_API GC_ATTR_DEPRECATED GC_word GC_gc_no; 82 /* Counter incremented per collection. */ 83 /* Includes empty GCs at startup. */ 84 GC_API GC_word GC_CALL GC_get_gc_no(void); 85 /* GC_get_gc_no() is unsynchronized, so */ 86 /* it requires GC_call_with_alloc_lock() to */ 87 /* avoid data races on multiprocessors. */ 88 89 #ifdef GC_THREADS 90 GC_API GC_ATTR_DEPRECATED int GC_parallel; 91 /* GC is parallelized for performance on */ 92 /* multiprocessors. Set to a non-zero value */ 93 /* only implicitly if collector is built with */ 94 /* PARALLEL_MARK defined, and if either */ 95 /* GC_MARKERS (or GC_NPROCS) environment */ 96 /* variable is set to > 1, or multiple cores */ 97 /* (processors) are available. */ 98 /* If GC_parallel is on (non-zero), incremental */ 99 /* collection is only partially functional, */ 100 /* and may not be desirable. The getter does */ 101 /* not use or need synchronization (i.e. */ 102 /* acquiring the GC lock). Starting from */ 103 /* GC v7.3, GC_parallel value is equal to the */ 104 /* number of marker threads minus one (i.e. */ 105 /* number of existing parallel marker threads */ 106 /* excluding the initiating one). */ 107 GC_API int GC_CALL GC_get_parallel(void); 108 #endif 109 110 111 /* Public R/W variables */ 112 /* The supplied setter and getter functions are preferred for new code. */ 113 114 typedef void * (GC_CALLBACK * GC_oom_func)(size_t /* bytes_requested */); 115 GC_API GC_ATTR_DEPRECATED GC_oom_func GC_oom_fn; 116 /* When there is insufficient memory to satisfy */ 117 /* an allocation request, we return */ 118 /* (*GC_oom_fn)(size). By default this just */ 119 /* returns NULL. */ 120 /* If it returns, it must return 0 or a valid */ 121 /* pointer to a previously allocated heap */ 122 /* object. GC_oom_fn must not be 0. */ 123 /* Both the supplied setter and the getter */ 124 /* acquire the GC lock (to avoid data races). */ 125 GC_API void GC_CALL GC_set_oom_fn(GC_oom_func) GC_ATTR_NONNULL(1); 126 GC_API GC_oom_func GC_CALL GC_get_oom_fn(void); 127 128 typedef void (GC_CALLBACK * GC_on_heap_resize_proc)(GC_word /* new_size */); 129 GC_API GC_ATTR_DEPRECATED GC_on_heap_resize_proc GC_on_heap_resize; 130 /* Invoked when the heap grows or shrinks. */ 131 /* Called with the world stopped (and the */ 132 /* allocation lock held). May be 0. */ 133 GC_API void GC_CALL GC_set_on_heap_resize(GC_on_heap_resize_proc); 134 GC_API GC_on_heap_resize_proc GC_CALL GC_get_on_heap_resize(void); 135 /* Both the supplied setter and the getter */ 136 /* acquire the GC lock (to avoid data races). */ 137 138 typedef enum { 139 GC_EVENT_START /* COLLECTION */, 140 GC_EVENT_MARK_START, 141 GC_EVENT_MARK_END, 142 GC_EVENT_RECLAIM_START, 143 GC_EVENT_RECLAIM_END, 144 GC_EVENT_END /* COLLECTION */, 145 GC_EVENT_PRE_STOP_WORLD /* STOPWORLD_BEGIN */, 146 GC_EVENT_POST_STOP_WORLD /* STOPWORLD_END */, 147 GC_EVENT_PRE_START_WORLD /* STARTWORLD_BEGIN */, 148 GC_EVENT_POST_START_WORLD /* STARTWORLD_END */, 149 GC_EVENT_THREAD_SUSPENDED, 150 GC_EVENT_THREAD_UNSUSPENDED 151 } GC_EventType; 152 153 typedef void (GC_CALLBACK * GC_on_collection_event_proc)(GC_EventType); 154 /* Invoked to indicate progress through the */ 155 /* collection process. Not used for thread */ 156 /* suspend/resume notifications. Called with */ 157 /* the GC lock held (or, even, the world */ 158 /* stopped). May be 0 (means no notifier). */ 159 GC_API void GC_CALL GC_set_on_collection_event(GC_on_collection_event_proc); 160 GC_API GC_on_collection_event_proc GC_CALL GC_get_on_collection_event(void); 161 /* Both the supplied setter and the getter */ 162 /* acquire the GC lock (to avoid data races). */ 163 164 #if defined(GC_THREADS) || (defined(GC_BUILD) && defined(NN_PLATFORM_CTR)) 165 typedef void (GC_CALLBACK * GC_on_thread_event_proc)(GC_EventType, 166 void * /* thread_id */); 167 /* Invoked when a thread is suspended or */ 168 /* resumed during collection. Called with the */ 169 /* GC lock held (and the world stopped */ 170 /* partially). May be 0 (means no notifier). */ 171 GC_API void GC_CALL GC_set_on_thread_event(GC_on_thread_event_proc); 172 GC_API GC_on_thread_event_proc GC_CALL GC_get_on_thread_event(void); 173 /* Both the supplied setter and the getter */ 174 /* acquire the GC lock (to avoid data races). */ 175 #endif 176 177 GC_API GC_ATTR_DEPRECATED int GC_find_leak; 178 /* Set to true to turn on the leak-finding mode */ 179 /* (do not actually garbage collect, but simply */ 180 /* report inaccessible memory that was not */ 181 /* deallocated with GC_FREE). Initial value */ 182 /* is determined by FIND_LEAK macro. */ 183 /* The value should not typically be modified */ 184 /* after GC initialization (and, thus, it does */ 185 /* not use or need synchronization). */ 186 GC_API void GC_CALL GC_set_find_leak(int); 187 GC_API int GC_CALL GC_get_find_leak(void); 188 189 GC_API GC_ATTR_DEPRECATED int GC_all_interior_pointers; 190 /* Arrange for pointers to object interiors to */ 191 /* be recognized as valid. Typically should */ 192 /* not be changed after GC initialization (in */ 193 /* case of calling it after the GC is */ 194 /* initialized, the setter acquires the GC lock */ 195 /* (to avoid data races). The initial value */ 196 /* depends on whether the GC is built with */ 197 /* ALL_INTERIOR_POINTERS macro defined or not. */ 198 /* Unless DONT_ADD_BYTE_AT_END is defined, this */ 199 /* also affects whether sizes are increased by */ 200 /* at least a byte to allow "off the end" */ 201 /* pointer recognition. Must be only 0 or 1. */ 202 GC_API void GC_CALL GC_set_all_interior_pointers(int); 203 GC_API int GC_CALL GC_get_all_interior_pointers(void); 204 205 GC_API GC_ATTR_DEPRECATED int GC_finalize_on_demand; 206 /* If nonzero, finalizers will only be run in */ 207 /* response to an explicit GC_invoke_finalizers */ 208 /* call. The default is determined by whether */ 209 /* the FINALIZE_ON_DEMAND macro is defined */ 210 /* when the collector is built. */ 211 /* The setter and getter are unsynchronized. */ 212 GC_API void GC_CALL GC_set_finalize_on_demand(int); 213 GC_API int GC_CALL GC_get_finalize_on_demand(void); 214 215 GC_API GC_ATTR_DEPRECATED int GC_java_finalization; 216 /* Mark objects reachable from finalizable */ 217 /* objects in a separate post-pass. This makes */ 218 /* it a bit safer to use non-topologically- */ 219 /* ordered finalization. Default value is */ 220 /* determined by JAVA_FINALIZATION macro. */ 221 /* Enables register_finalizer_unreachable to */ 222 /* work correctly. */ 223 /* The setter and getter are unsynchronized. */ 224 GC_API void GC_CALL GC_set_java_finalization(int); 225 GC_API int GC_CALL GC_get_java_finalization(void); 226 227 typedef void (GC_CALLBACK * GC_finalizer_notifier_proc)(void); 228 GC_API GC_ATTR_DEPRECATED GC_finalizer_notifier_proc GC_finalizer_notifier; 229 /* Invoked by the collector when there are */ 230 /* objects to be finalized. Invoked at most */ 231 /* once per GC cycle. Never invoked unless */ 232 /* GC_finalize_on_demand is set. */ 233 /* Typically this will notify a finalization */ 234 /* thread, which will call GC_invoke_finalizers */ 235 /* in response. May be 0 (means no notifier). */ 236 /* Both the supplied setter and the getter */ 237 /* acquire the GC lock (to avoid data races). */ 238 GC_API void GC_CALL GC_set_finalizer_notifier(GC_finalizer_notifier_proc); 239 GC_API GC_finalizer_notifier_proc GC_CALL GC_get_finalizer_notifier(void); 240 241 GC_API 242 # ifndef GC_DONT_GC 243 GC_ATTR_DEPRECATED 244 # endif 245 int GC_dont_gc; /* != 0 ==> Don't collect. In versions 6.2a1+, */ 246 /* this overrides explicit GC_gcollect() calls. */ 247 /* Used as a counter, so that nested enabling */ 248 /* and disabling work correctly. Should */ 249 /* normally be updated with GC_enable() and */ 250 /* GC_disable() calls. Direct assignment to */ 251 /* GC_dont_gc is deprecated. To check whether */ 252 /* GC is disabled, GC_is_disabled() is */ 253 /* preferred for new code. */ 254 255 GC_API GC_ATTR_DEPRECATED int GC_dont_expand; 256 /* Do not expand the heap unless explicitly */ 257 /* requested or forced to. The setter and */ 258 /* getter are unsynchronized. */ 259 GC_API void GC_CALL GC_set_dont_expand(int); 260 GC_API int GC_CALL GC_get_dont_expand(void); 261 262 GC_API GC_ATTR_DEPRECATED int GC_use_entire_heap; 263 /* Causes the non-incremental collector to use the */ 264 /* entire heap before collecting. This was the only */ 265 /* option for GC versions < 5.0. This sometimes */ 266 /* results in more large block fragmentation, since */ 267 /* very large blocks will tend to get broken up */ 268 /* during each GC cycle. It is likely to result in a */ 269 /* larger working set, but lower collection */ 270 /* frequencies, and hence fewer instructions executed */ 271 /* in the collector. */ 272 273 GC_API GC_ATTR_DEPRECATED int GC_full_freq; 274 /* Number of partial collections between */ 275 /* full collections. Matters only if */ 276 /* GC_is_incremental_mode(). */ 277 /* Full collections are also triggered if */ 278 /* the collector detects a substantial */ 279 /* increase in the number of in-use heap */ 280 /* blocks. Values in the tens are now */ 281 /* perfectly reasonable, unlike for */ 282 /* earlier GC versions. */ 283 /* The setter and getter are unsynchronized, so */ 284 /* GC_call_with_alloc_lock() is required to */ 285 /* avoid data races (if the value is modified */ 286 /* after the GC is put to multi-threaded mode). */ 287 GC_API void GC_CALL GC_set_full_freq(int); 288 GC_API int GC_CALL GC_get_full_freq(void); 289 290 GC_API GC_ATTR_DEPRECATED GC_word GC_non_gc_bytes; 291 /* Bytes not considered candidates for */ 292 /* collection. Used only to control scheduling */ 293 /* of collections. Updated by */ 294 /* GC_malloc_uncollectable and GC_free. */ 295 /* Wizards only. */ 296 /* The setter and getter are unsynchronized, so */ 297 /* GC_call_with_alloc_lock() is required to */ 298 /* avoid data races (if the value is modified */ 299 /* after the GC is put to multi-threaded mode). */ 300 GC_API void GC_CALL GC_set_non_gc_bytes(GC_word); 301 GC_API GC_word GC_CALL GC_get_non_gc_bytes(void); 302 303 GC_API GC_ATTR_DEPRECATED int GC_no_dls; 304 /* Don't register dynamic library data segments. */ 305 /* Wizards only. Should be used only if the */ 306 /* application explicitly registers all roots. */ 307 /* (In some environments like Microsoft Windows */ 308 /* and Apple's Darwin, this may also prevent */ 309 /* registration of the main data segment as part */ 310 /* of the root set.) */ 311 /* The setter and getter are unsynchronized. */ 312 GC_API void GC_CALL GC_set_no_dls(int); 313 GC_API int GC_CALL GC_get_no_dls(void); 314 315 GC_API GC_ATTR_DEPRECATED GC_word GC_free_space_divisor; 316 /* We try to make sure that we allocate at */ 317 /* least N/GC_free_space_divisor bytes between */ 318 /* collections, where N is twice the number */ 319 /* of traced bytes, plus the number of untraced */ 320 /* bytes (bytes in "atomic" objects), plus */ 321 /* a rough estimate of the root set size. */ 322 /* N approximates GC tracing work per GC. */ 323 /* The initial value is GC_FREE_SPACE_DIVISOR. */ 324 /* Increasing its value will use less space */ 325 /* but more collection time. Decreasing it */ 326 /* will appreciably decrease collection time */ 327 /* at the expense of space. */ 328 /* The setter and getter are unsynchronized, so */ 329 /* GC_call_with_alloc_lock() is required to */ 330 /* avoid data races (if the value is modified */ 331 /* after the GC is put to multi-threaded mode). */ 332 /* In version 7.1 (and before), the setter */ 333 /* returned the old value. */ 334 GC_API void GC_CALL GC_set_free_space_divisor(GC_word); 335 GC_API GC_word GC_CALL GC_get_free_space_divisor(void); 336 337 GC_API GC_ATTR_DEPRECATED GC_word GC_max_retries; 338 /* The maximum number of GCs attempted before */ 339 /* reporting out of memory after heap */ 340 /* expansion fails. Initially 0. */ 341 /* The setter and getter are unsynchronized, so */ 342 /* GC_call_with_alloc_lock() is required to */ 343 /* avoid data races (if the value is modified */ 344 /* after the GC is put to multi-threaded mode). */ 345 GC_API void GC_CALL GC_set_max_retries(GC_word); 346 GC_API GC_word GC_CALL GC_get_max_retries(void); 347 348 349 GC_API GC_ATTR_DEPRECATED char *GC_stackbottom; 350 /* Cool end of user stack. */ 351 /* May be set in the client prior to */ 352 /* calling any GC_ routines. This */ 353 /* avoids some overhead, and */ 354 /* potentially some signals that can */ 355 /* confuse debuggers. Otherwise the */ 356 /* collector attempts to set it */ 357 /* automatically. */ 358 /* For multi-threaded code, this is the */ 359 /* cold end of the stack for the */ 360 /* primordial thread. Portable clients */ 361 /* should use GC_get_stack_base(), */ 362 /* GC_call_with_gc_active() and */ 363 /* GC_register_my_thread() instead. */ 364 365 GC_API GC_ATTR_DEPRECATED int GC_dont_precollect; 366 /* Do not collect as part of GC */ 367 /* initialization. Should be set only */ 368 /* if the client wants a chance to */ 369 /* manually initialize the root set */ 370 /* before the first collection. */ 371 /* Interferes with blacklisting. */ 372 /* Wizards only. The setter and getter */ 373 /* are unsynchronized (and no external */ 374 /* locking is needed since the value is */ 375 /* accessed at GC initialization only). */ 376 GC_API void GC_CALL GC_set_dont_precollect(int); 377 GC_API int GC_CALL GC_get_dont_precollect(void); 378 379 GC_API GC_ATTR_DEPRECATED unsigned long GC_time_limit; 380 /* If incremental collection is enabled, */ 381 /* We try to terminate collections */ 382 /* after this many milliseconds. Not a */ 383 /* hard time bound. Setting this to */ 384 /* GC_TIME_UNLIMITED will essentially */ 385 /* disable incremental collection while */ 386 /* leaving generational collection */ 387 /* enabled. */ 388 #define GC_TIME_UNLIMITED 999999 389 /* Setting GC_time_limit to this value */ 390 /* will disable the "pause time exceeded"*/ 391 /* tests. */ 392 /* The setter and getter are unsynchronized, so */ 393 /* GC_call_with_alloc_lock() is required to */ 394 /* avoid data races (if the value is modified */ 395 /* after the GC is put to multi-threaded mode). */ 396 GC_API void GC_CALL GC_set_time_limit(unsigned long); 397 GC_API unsigned long GC_CALL GC_get_time_limit(void); 398 399 /* Public procedures */ 400 401 /* Tell the collector to start various performance measurements. */ 402 /* Only the total time taken by full collections is calculated, as */ 403 /* of now. And, currently, there is no way to stop the measurements. */ 404 /* The function does not use any synchronization. Defined only if the */ 405 /* library has been compiled without NO_CLOCK. */ 406 GC_API void GC_CALL GC_start_performance_measurement(void); 407 408 /* Get the total time of all full collections since the start of the */ 409 /* performance measurements. The measurement unit is one millisecond. */ 410 /* Note that the returned value wraps around on overflow. */ 411 /* The function does not use any synchronization. Defined only if the */ 412 /* library has been compiled without NO_CLOCK. */ 413 GC_API unsigned long GC_CALL GC_get_full_gc_total_time(void); 414 415 /* Set whether the GC will allocate executable memory pages or not. */ 416 /* A non-zero argument instructs the collector to allocate memory with */ 417 /* the executable flag on. Must be called before the collector is */ 418 /* initialized. May have no effect on some platforms. The default */ 419 /* value is controlled by NO_EXECUTE_PERMISSION macro (if present then */ 420 /* the flag is off). Portable clients should have */ 421 /* GC_set_pages_executable(1) call (before GC_INIT) provided they are */ 422 /* going to execute code on any of the GC-allocated memory objects. */ 423 GC_API void GC_CALL GC_set_pages_executable(int); 424 425 /* Returns non-zero value if the GC is set to the allocate-executable */ 426 /* mode. The mode could be changed by GC_set_pages_executable (before */ 427 /* GC_INIT) unless the former has no effect on the platform. Does not */ 428 /* use or need synchronization (i.e. acquiring the allocator lock). */ 429 GC_API int GC_CALL GC_get_pages_executable(void); 430 431 /* The setter and getter of the minimum value returned by the internal */ 432 /* min_bytes_allocd(). The value should not be zero; the default value */ 433 /* is one. Not synchronized. */ 434 GC_API void GC_CALL GC_set_min_bytes_allocd(size_t); 435 GC_API size_t GC_CALL GC_get_min_bytes_allocd(void); 436 437 /* Set/get the size in pages of units operated by GC_collect_a_little. */ 438 /* The value should not be zero. Not synchronized. */ 439 GC_API void GC_CALL GC_set_rate(int); 440 GC_API int GC_CALL GC_get_rate(void); 441 442 /* Set/get the maximum number of prior attempts at the world-stop */ 443 /* marking. Not synchronized. */ 444 GC_API void GC_CALL GC_set_max_prior_attempts(int); 445 GC_API int GC_CALL GC_get_max_prior_attempts(void); 446 447 /* Overrides the default handle-fork mode. Non-zero value means GC */ 448 /* should install proper pthread_atfork handlers. Has effect only if */ 449 /* called before GC_INIT. Clients should invoke GC_set_handle_fork */ 450 /* with non-zero argument if going to use fork with GC functions called */ 451 /* in the forked child. (Note that such client and atfork handlers */ 452 /* activities are not fully POSIX-compliant.) GC_set_handle_fork */ 453 /* instructs GC_init to setup GC fork handlers using pthread_atfork, */ 454 /* the latter might fail (or, even, absent on some targets) causing */ 455 /* abort at GC initialization. Starting from 7.3alpha3, problems with */ 456 /* missing (or failed) pthread_atfork() could be avoided by invocation */ 457 /* of GC_set_handle_fork(-1) at application start-up and surrounding */ 458 /* each fork() with the relevant GC_atfork_prepare/parent/child calls. */ 459 GC_API void GC_CALL GC_set_handle_fork(int); 460 461 /* Routines to handle POSIX fork() manually (no-op if handled */ 462 /* automatically). GC_atfork_prepare should be called immediately */ 463 /* before fork(); GC_atfork_parent should be invoked just after fork in */ 464 /* the branch that corresponds to parent process (i.e., fork result is */ 465 /* non-zero); GC_atfork_child is to be called immediately in the child */ 466 /* branch (i.e., fork result is 0). Note that GC_atfork_child() call */ 467 /* should, of course, precede GC_start_mark_threads call (if any). */ 468 GC_API void GC_CALL GC_atfork_prepare(void); 469 GC_API void GC_CALL GC_atfork_parent(void); 470 GC_API void GC_CALL GC_atfork_child(void); 471 472 /* Initialize the collector. Portable clients should call GC_INIT() */ 473 /* from the main program instead. */ 474 GC_API void GC_CALL GC_init(void); 475 476 /* Returns non-zero (TRUE) if and only if the collector is initialized */ 477 /* (or, at least, the initialization is in progress). */ 478 GC_API int GC_CALL GC_is_init_called(void); 479 480 /* Perform the collector shutdown. (E.g. dispose critical sections on */ 481 /* Win32 target.) A duplicate invocation is a no-op. GC_INIT should */ 482 /* not be called after the shutdown. See also GC_win32_free_heap(). */ 483 GC_API void GC_CALL GC_deinit(void); 484 485 /* General purpose allocation routines, with roughly malloc calling */ 486 /* conv. The atomic versions promise that no relevant pointers are */ 487 /* contained in the object. The non-atomic versions guarantee that the */ 488 /* new object is cleared. GC_malloc_uncollectable allocates */ 489 /* an object that is scanned for pointers to collectible */ 490 /* objects, but is not itself collectible. The object is scanned even */ 491 /* if it does not appear to be reachable. GC_malloc_uncollectable and */ 492 /* GC_free called on the resulting object implicitly update */ 493 /* GC_non_gc_bytes appropriately. */ 494 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL 495 GC_malloc(size_t /* size_in_bytes */); 496 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL 497 GC_malloc_atomic(size_t /* size_in_bytes */); 498 GC_API GC_ATTR_MALLOC char * GC_CALL GC_strdup(const char *); 499 GC_API GC_ATTR_MALLOC char * GC_CALL 500 GC_strndup(const char *, size_t) GC_ATTR_NONNULL(1); 501 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL 502 GC_malloc_uncollectable(size_t /* size_in_bytes */); 503 GC_API GC_ATTR_DEPRECATED void * GC_CALL GC_malloc_stubborn(size_t); 504 505 /* GC_memalign() is not well tested. */ 506 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(2) void * GC_CALL 507 GC_memalign(size_t /* align */, size_t /* lb */); 508 GC_API int GC_CALL GC_posix_memalign(void ** /* memptr */, size_t /* align */, 509 size_t /* lb */) GC_ATTR_NONNULL(1); 510 511 /* Explicitly deallocate an object. Dangerous if used incorrectly. */ 512 /* Requires a pointer to the base of an object. */ 513 /* An object should not be enabled for finalization (and it should not */ 514 /* contain registered disappearing links of any kind) when it is */ 515 /* explicitly deallocated. */ 516 /* GC_free(0) is a no-op, as required by ANSI C for free. */ 517 GC_API void GC_CALL GC_free(void *); 518 519 /* The "stubborn" objects allocation is not supported anymore. Exists */ 520 /* only for the backward compatibility. */ 521 #define GC_MALLOC_STUBBORN(sz) GC_MALLOC(sz) 522 #define GC_NEW_STUBBORN(t) GC_NEW(t) 523 #define GC_CHANGE_STUBBORN(p) GC_change_stubborn(p) 524 GC_API GC_ATTR_DEPRECATED void GC_CALL GC_change_stubborn(const void *); 525 526 /* Inform the collector that the object has been changed. */ 527 /* Only non-NULL pointer stores into the object are considered to be */ 528 /* changes. Matters only if the incremental collection is enabled in */ 529 /* the manual VDB mode (otherwise the function does nothing). */ 530 /* Should be followed typically by GC_reachable_here called for each */ 531 /* of the stored pointers. */ 532 GC_API void GC_CALL GC_end_stubborn_change(const void *) GC_ATTR_NONNULL(1); 533 534 /* Return a pointer to the base (lowest address) of an object given */ 535 /* a pointer to a location within the object. */ 536 /* I.e., map an interior pointer to the corresponding base pointer. */ 537 /* Note that with debugging allocation, this returns a pointer to the */ 538 /* actual base of the object, i.e. the debug information, not to */ 539 /* the base of the user object. */ 540 /* Return 0 if displaced_pointer doesn't point to within a valid */ 541 /* object. */ 542 /* Note that a deallocated object in the garbage collected heap */ 543 /* may be considered valid, even if it has been deallocated with */ 544 /* GC_free. */ 545 GC_API void * GC_CALL GC_base(void * /* displaced_pointer */); 546 547 /* Return non-zero (TRUE) if and only if the argument points to */ 548 /* somewhere in GC heap. Primary use is as a fast alternative to */ 549 /* GC_base to check whether the pointed object is allocated by GC */ 550 /* or not. It is assumed that the collector is already initialized. */ 551 GC_API int GC_CALL GC_is_heap_ptr(const void *); 552 553 /* Given a pointer to the base of an object, return its size in bytes. */ 554 /* The returned size may be slightly larger than what was originally */ 555 /* requested. */ 556 GC_API size_t GC_CALL GC_size(const void * /* obj_addr */) GC_ATTR_NONNULL(1); 557 558 /* For compatibility with C library. This is occasionally faster than */ 559 /* a malloc followed by a bcopy. But if you rely on that, either here */ 560 /* or with the standard C library, your code is broken. In my */ 561 /* opinion, it shouldn't have been invented, but now we're stuck. -HB */ 562 /* The resulting object has the same kind as the original. */ 563 /* It is an error to have changes enabled for the original object. */ 564 /* It does not change the content of the object from its beginning to */ 565 /* the minimum of old size and new_size_in_bytes; the content above in */ 566 /* case of object size growth is initialized to zero (not guaranteed */ 567 /* for atomic object type). The function follows ANSI conventions for */ 568 /* NULL old_object (i.e., equivalent to GC_malloc regardless of new */ 569 /* size). If new size is zero (and old_object is non-NULL) then the */ 570 /* call is equivalent to GC_free (and NULL is returned). If old_object */ 571 /* is non-NULL, it must have been returned by an earlier call to */ 572 /* GC_malloc* or GC_realloc. In case of the allocation failure, the */ 573 /* memory pointed by old_object is untouched (and not freed). */ 574 /* If the returned pointer is not the same as old_object and both of */ 575 /* them are non-NULL then old_object is freed. Returns either NULL (in */ 576 /* case of the allocation failure or zero new size) or pointer to the */ 577 /* allocated memory. */ 578 GC_API void * GC_CALL GC_realloc(void * /* old_object */, 579 size_t /* new_size_in_bytes */) 580 /* 'realloc' attr */ GC_ATTR_ALLOC_SIZE(2); 581 582 /* Explicitly increase the heap size. */ 583 /* Returns 0 on failure, 1 on success. */ 584 GC_API int GC_CALL GC_expand_hp(size_t /* number_of_bytes */); 585 586 /* Limit the heap size to n bytes. Useful when you're debugging, */ 587 /* especially on systems that don't handle running out of memory well. */ 588 /* n == 0 ==> unbounded. This is the default. This setter function is */ 589 /* unsynchronized (so it might require GC_call_with_alloc_lock to avoid */ 590 /* data races). */ 591 GC_API void GC_CALL GC_set_max_heap_size(GC_word /* n */); 592 593 /* Inform the collector that a certain section of statically allocated */ 594 /* memory contains no pointers to garbage collected memory. Thus it */ 595 /* need not be scanned. This is sometimes important if the application */ 596 /* maps large read/write files into the address space, which could be */ 597 /* mistaken for dynamic library data segments on some systems. */ 598 /* Both section start and end are not needed to be pointer-aligned. */ 599 GC_API void GC_CALL GC_exclude_static_roots(void * /* low_address */, 600 void * /* high_address_plus_1 */); 601 602 /* Clear the set of root segments. Wizards only. */ 603 GC_API void GC_CALL GC_clear_roots(void); 604 605 /* Add a root segment. Wizards only. */ 606 /* Both segment start and end are not needed to be pointer-aligned. */ 607 /* low_address must not be greater than high_address_plus_1. */ 608 GC_API void GC_CALL GC_add_roots(void * /* low_address */, 609 void * /* high_address_plus_1 */); 610 611 /* Remove a root segment. Wizards only. */ 612 /* May be unimplemented on some platforms. */ 613 GC_API void GC_CALL GC_remove_roots(void * /* low_address */, 614 void * /* high_address_plus_1 */); 615 616 /* Add a displacement to the set of those considered valid by the */ 617 /* collector. GC_register_displacement(n) means that if p was returned */ 618 /* by GC_malloc, then (char *)p + n will be considered to be a valid */ 619 /* pointer to p. N must be small and less than the size of p. */ 620 /* (All pointers to the interior of objects from the stack are */ 621 /* considered valid in any case. This applies to heap objects and */ 622 /* static data.) */ 623 /* Preferably, this should be called before any other GC procedures. */ 624 /* Calling it later adds to the probability of excess memory */ 625 /* retention. */ 626 /* This is a no-op if the collector has recognition of */ 627 /* arbitrary interior pointers enabled, which is now the default. */ 628 GC_API void GC_CALL GC_register_displacement(size_t /* n */); 629 630 /* The following version should be used if any debugging allocation is */ 631 /* being done. */ 632 GC_API void GC_CALL GC_debug_register_displacement(size_t /* n */); 633 634 /* Explicitly trigger a full, world-stop collection. */ 635 GC_API void GC_CALL GC_gcollect(void); 636 637 /* Same as above but ignores the default stop_func setting and tries to */ 638 /* unmap as much memory as possible (regardless of the corresponding */ 639 /* switch setting). The recommended usage: on receiving a system */ 640 /* low-memory event; before retrying a system call failed because of */ 641 /* the system is running out of resources. */ 642 GC_API void GC_CALL GC_gcollect_and_unmap(void); 643 644 /* Trigger a full world-stopped collection. Abort the collection if */ 645 /* and when stop_func returns a nonzero value. Stop_func will be */ 646 /* called frequently, and should be reasonably fast. (stop_func is */ 647 /* called with the allocation lock held and the world might be stopped; */ 648 /* it's not allowed for stop_func to manipulate pointers to the garbage */ 649 /* collected heap or call most of GC functions.) This works even */ 650 /* if virtual dirty bits, and hence incremental collection is not */ 651 /* available for this architecture. Collections can be aborted faster */ 652 /* than normal pause times for incremental collection. However, */ 653 /* aborted collections do no useful work; the next collection needs */ 654 /* to start from the beginning. stop_func must not be 0. */ 655 /* GC_try_to_collect() returns 0 if the collection was aborted (or the */ 656 /* collections are disabled), 1 if it succeeded. */ 657 typedef int (GC_CALLBACK * GC_stop_func)(void); 658 GC_API int GC_CALL GC_try_to_collect(GC_stop_func /* stop_func */) 659 GC_ATTR_NONNULL(1); 660 661 /* Set and get the default stop_func. The default stop_func is used by */ 662 /* GC_gcollect() and by implicitly trigged collections (except for the */ 663 /* case when handling out of memory). Must not be 0. */ 664 /* Both the setter and getter acquire the GC lock to avoid data races. */ 665 GC_API void GC_CALL GC_set_stop_func(GC_stop_func /* stop_func */) 666 GC_ATTR_NONNULL(1); 667 GC_API GC_stop_func GC_CALL GC_get_stop_func(void); 668 669 /* Return the number of bytes in the heap. Excludes collector private */ 670 /* data structures. Excludes the unmapped memory (returned to the OS). */ 671 /* Includes empty blocks and fragmentation loss. Includes some pages */ 672 /* that were allocated but never written. */ 673 /* This is an unsynchronized getter, so it should be called typically */ 674 /* with the GC lock held to avoid data races on multiprocessors (the */ 675 /* alternative is to use GC_get_heap_usage_safe or GC_get_prof_stats */ 676 /* API calls instead). */ 677 /* This getter remains lock-free (unsynchronized) for compatibility */ 678 /* reason since some existing clients call it from a GC callback */ 679 /* holding the allocator lock. (This API function and the following */ 680 /* four ones below were made thread-safe in GC v7.2alpha1 and */ 681 /* reverted back in v7.2alpha7 for the reason described.) */ 682 GC_API size_t GC_CALL GC_get_heap_size(void); 683 684 /* Return a lower bound on the number of free bytes in the heap */ 685 /* (excluding the unmapped memory space). This is an unsynchronized */ 686 /* getter (see GC_get_heap_size comment regarding thread-safety). */ 687 GC_API size_t GC_CALL GC_get_free_bytes(void); 688 689 /* Return the size (in bytes) of the unmapped memory (which is returned */ 690 /* to the OS but could be remapped back by the collector later unless */ 691 /* the OS runs out of system/virtual memory). This is an unsynchronized */ 692 /* getter (see GC_get_heap_size comment regarding thread-safety). */ 693 GC_API size_t GC_CALL GC_get_unmapped_bytes(void); 694 695 /* Return the number of bytes allocated since the last collection. */ 696 /* This is an unsynchronized getter (see GC_get_heap_size comment */ 697 /* regarding thread-safety). */ 698 GC_API size_t GC_CALL GC_get_bytes_since_gc(void); 699 700 /* Return the number of explicitly deallocated bytes of memory since */ 701 /* the recent collection. This is an unsynchronized getter. */ 702 GC_API size_t GC_CALL GC_get_expl_freed_bytes_since_gc(void); 703 704 /* Return the total number of bytes allocated in this process. */ 705 /* Never decreases, except due to wrapping. This is an unsynchronized */ 706 /* getter (see GC_get_heap_size comment regarding thread-safety). */ 707 GC_API size_t GC_CALL GC_get_total_bytes(void); 708 709 /* Return the heap usage information. This is a thread-safe (atomic) */ 710 /* alternative for the five above getters. (This function acquires */ 711 /* the allocator lock thus preventing data racing and returning the */ 712 /* consistent result.) Passing NULL pointer is allowed for any */ 713 /* argument. Returned (filled in) values are of word type. */ 714 /* (This API function was introduced in GC v7.2alpha7 at the same time */ 715 /* when GC_get_heap_size and the friends were made lock-free again.) */ 716 GC_API void GC_CALL GC_get_heap_usage_safe(GC_word * /* pheap_size */, 717 GC_word * /* pfree_bytes */, 718 GC_word * /* punmapped_bytes */, 719 GC_word * /* pbytes_since_gc */, 720 GC_word * /* ptotal_bytes */); 721 722 /* Structure used to query GC statistics (profiling information). */ 723 /* More fields could be added in the future. To preserve compatibility */ 724 /* new fields should be added only to the end, and no deprecated fields */ 725 /* should be removed from. */ 726 struct GC_prof_stats_s { 727 GC_word heapsize_full; 728 /* Heap size in bytes (including the area unmapped to OS). */ 729 /* Same as GC_get_heap_size() + GC_get_unmapped_bytes(). */ 730 GC_word free_bytes_full; 731 /* Total bytes contained in free and unmapped blocks. */ 732 /* Same as GC_get_free_bytes() + GC_get_unmapped_bytes(). */ 733 GC_word unmapped_bytes; 734 /* Amount of memory unmapped to OS. Same as the value */ 735 /* returned by GC_get_unmapped_bytes(). */ 736 GC_word bytes_allocd_since_gc; 737 /* Number of bytes allocated since the recent collection. */ 738 /* Same as returned by GC_get_bytes_since_gc(). */ 739 GC_word allocd_bytes_before_gc; 740 /* Number of bytes allocated before the recent garbage */ 741 /* collection. The value may wrap. Same as the result of */ 742 /* GC_get_total_bytes() - GC_get_bytes_since_gc(). */ 743 GC_word non_gc_bytes; 744 /* Number of bytes not considered candidates for garbage */ 745 /* collection. Same as returned by GC_get_non_gc_bytes(). */ 746 GC_word gc_no; 747 /* Garbage collection cycle number. The value may wrap */ 748 /* (and could be -1). Same as returned by GC_get_gc_no(). */ 749 GC_word markers_m1; 750 /* Number of marker threads (excluding the initiating one). */ 751 /* Same as returned by GC_get_parallel (or 0 if the */ 752 /* collector is single-threaded). */ 753 GC_word bytes_reclaimed_since_gc; 754 /* Approximate number of reclaimed bytes after recent GC. */ 755 GC_word reclaimed_bytes_before_gc; 756 /* Approximate number of bytes reclaimed before the recent */ 757 /* garbage collection. The value may wrap. */ 758 GC_word expl_freed_bytes_since_gc; 759 /* Number of bytes freed explicitly since the recent GC. */ 760 /* Same as returned by GC_get_expl_freed_bytes_since_gc(). */ 761 }; 762 763 /* Atomically get GC statistics (various global counters). Clients */ 764 /* should pass the size of the buffer (of GC_prof_stats_s type) to fill */ 765 /* in the values - this is for interoperability between different GC */ 766 /* versions, an old client could have fewer fields, and vice versa, */ 767 /* client could use newer gc.h (with more entries declared in the */ 768 /* structure) than that of the linked libgc binary; in the latter case, */ 769 /* unsupported (unknown) fields are filled in with -1. Return the size */ 770 /* (in bytes) of the filled in part of the structure (excluding all */ 771 /* unknown fields, if any). */ 772 GC_API size_t GC_CALL GC_get_prof_stats(struct GC_prof_stats_s *, 773 size_t /* stats_sz */); 774 #ifdef GC_THREADS 775 /* Same as above but unsynchronized (i.e., not holding the allocation */ 776 /* lock). Clients should call it using GC_call_with_alloc_lock to */ 777 /* avoid data races on multiprocessors. */ 778 GC_API size_t GC_CALL GC_get_prof_stats_unsafe(struct GC_prof_stats_s *, 779 size_t /* stats_sz */); 780 #endif 781 782 /* Get the element value (converted to bytes) at a given index of */ 783 /* size_map table which provides requested-to-actual allocation size */ 784 /* mapping. Assumes the collector is initialized. Returns -1 if the */ 785 /* index is out of size_map table bounds. Does not use synchronization, */ 786 /* thus clients should call it using GC_call_with_alloc_lock typically */ 787 /* to avoid data races on multiprocessors. */ 788 GC_API size_t GC_CALL GC_get_size_map_at(int i); 789 790 /* Count total memory use in bytes by all allocated blocks. Acquires */ 791 /* the lock. */ 792 GC_API size_t GC_CALL GC_get_memory_use(void); 793 794 /* Disable garbage collection. Even GC_gcollect calls will be */ 795 /* ineffective. */ 796 GC_API void GC_CALL GC_disable(void); 797 798 /* Return non-zero (TRUE) if and only if garbage collection is disabled */ 799 /* (i.e., GC_dont_gc value is non-zero). Does not acquire the lock. */ 800 GC_API int GC_CALL GC_is_disabled(void); 801 802 /* Try to re-enable garbage collection. GC_disable() and GC_enable() */ 803 /* calls nest. Garbage collection is enabled if the number of calls to */ 804 /* both functions is equal. */ 805 GC_API void GC_CALL GC_enable(void); 806 807 /* Select whether to use the manual VDB mode for the incremental */ 808 /* collection. Has no effect if called after enabling the incremental */ 809 /* collection. The default value is off unless the collector is */ 810 /* compiled with MANUAL_VDB defined. The manual VDB mode should be */ 811 /* used only if the client has the appropriate GC_END_STUBBORN_CHANGE */ 812 /* and GC_reachable_here (or, alternatively, GC_PTR_STORE_AND_DIRTY) */ 813 /* calls (to ensure proper write barriers). Both the setter and getter */ 814 /* are not synchronized, and are defined only if the library has been */ 815 /* compiled without SMALL_CONFIG. */ 816 GC_API void GC_CALL GC_set_manual_vdb_allowed(int); 817 GC_API int GC_CALL GC_get_manual_vdb_allowed(void); 818 819 /* Enable incremental/generational collection. Not advisable unless */ 820 /* dirty bits are available or most heap objects are pointer-free */ 821 /* (atomic) or immutable. Don't use in leak finding mode. Ignored if */ 822 /* GC_dont_gc is non-zero. Only the generational piece of this is */ 823 /* functional if GC_parallel is non-zero or if GC_time_limit is */ 824 /* GC_TIME_UNLIMITED. Causes thread-local variant of GC_gcj_malloc() */ 825 /* to revert to locked allocation. Must be called before any such */ 826 /* GC_gcj_malloc() calls. For best performance, should be called as */ 827 /* early as possible. On some platforms, calling it later may have */ 828 /* adverse effects. */ 829 /* Safe to call before GC_INIT(). Includes a GC_init() call. */ 830 GC_API void GC_CALL GC_enable_incremental(void); 831 832 /* Return non-zero (TRUE) if and only if the incremental mode is on. */ 833 /* Does not acquire the lock. */ 834 GC_API int GC_CALL GC_is_incremental_mode(void); 835 836 /* Does incremental mode write-protect pages? Returns zero or */ 837 /* more of the following, or'ed together: */ 838 #define GC_PROTECTS_POINTER_HEAP 1 /* May protect non-atomic objects. */ 839 #define GC_PROTECTS_PTRFREE_HEAP 2 840 #define GC_PROTECTS_STATIC_DATA 4 /* Currently never. */ 841 #define GC_PROTECTS_STACK 8 /* Probably impractical. */ 842 843 #define GC_PROTECTS_NONE 0 844 /* The collector is assumed to be initialized before this call. */ 845 GC_API int GC_CALL GC_incremental_protection_needs(void); 846 847 /* Perform some garbage collection work, if appropriate. */ 848 /* Return 0 if there is no more work to be done (including the */ 849 /* case when garbage collection is not appropriate). */ 850 /* Typically performs an amount of work corresponding roughly */ 851 /* to marking from one page. May do more work if further */ 852 /* progress requires it, e.g. if incremental collection is */ 853 /* disabled. It is reasonable to call this in a wait loop */ 854 /* until it returns 0. */ 855 GC_API int GC_CALL GC_collect_a_little(void); 856 857 /* Allocate an object of size lb bytes. The client guarantees that */ 858 /* as long as the object is live, it will be referenced by a pointer */ 859 /* that points to somewhere within the first 256 bytes of the object. */ 860 /* (This should normally be declared volatile to prevent the compiler */ 861 /* from invalidating this assertion.) This routine is only useful */ 862 /* if a large array is being allocated. It reduces the chance of */ 863 /* accidentally retaining such an array as a result of scanning an */ 864 /* integer that happens to be an address inside the array. (Actually, */ 865 /* it reduces the chance of the allocator not finding space for such */ 866 /* an array, since it will try hard to avoid introducing such a false */ 867 /* reference.) On a SunOS 4.X or MS Windows system this is recommended */ 868 /* for arrays likely to be larger than 100K or so. For other systems, */ 869 /* or if the collector is not configured to recognize all interior */ 870 /* pointers, the threshold is normally much higher. */ 871 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL 872 GC_malloc_ignore_off_page(size_t /* lb */); 873 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL 874 GC_malloc_atomic_ignore_off_page(size_t /* lb */); 875 876 #ifdef GC_ADD_CALLER 877 # define GC_EXTRAS GC_RETURN_ADDR, __FILE__, __LINE__ 878 # define GC_EXTRA_PARAMS GC_word ra, const char * s, int i 879 #else 880 # define GC_EXTRAS __FILE__, __LINE__ 881 # define GC_EXTRA_PARAMS const char * s, int i 882 #endif 883 884 /* The following is only defined if the library has been suitably */ 885 /* compiled: */ 886 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL 887 GC_malloc_atomic_uncollectable(size_t /* size_in_bytes */); 888 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL 889 GC_debug_malloc_atomic_uncollectable(size_t, GC_EXTRA_PARAMS); 890 891 /* Debugging (annotated) allocation. GC_gcollect will check */ 892 /* objects allocated in this way for overwrites, etc. */ 893 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL 894 GC_debug_malloc(size_t /* size_in_bytes */, GC_EXTRA_PARAMS); 895 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL 896 GC_debug_malloc_atomic(size_t /* size_in_bytes */, GC_EXTRA_PARAMS); 897 GC_API GC_ATTR_MALLOC char * GC_CALL 898 GC_debug_strdup(const char *, GC_EXTRA_PARAMS); 899 GC_API GC_ATTR_MALLOC char * GC_CALL 900 GC_debug_strndup(const char *, size_t, GC_EXTRA_PARAMS) 901 GC_ATTR_NONNULL(1); 902 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL 903 GC_debug_malloc_uncollectable(size_t /* size_in_bytes */, 904 GC_EXTRA_PARAMS); 905 GC_API GC_ATTR_DEPRECATED void * GC_CALL 906 GC_debug_malloc_stubborn(size_t /* size_in_bytes */, GC_EXTRA_PARAMS); 907 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL 908 GC_debug_malloc_ignore_off_page(size_t /* size_in_bytes */, 909 GC_EXTRA_PARAMS); 910 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL 911 GC_debug_malloc_atomic_ignore_off_page(size_t /* size_in_bytes */, 912 GC_EXTRA_PARAMS); 913 GC_API void GC_CALL GC_debug_free(void *); 914 GC_API void * GC_CALL GC_debug_realloc(void * /* old_object */, 915 size_t /* new_size_in_bytes */, GC_EXTRA_PARAMS) 916 /* 'realloc' attr */ GC_ATTR_ALLOC_SIZE(2); 917 GC_API 918 #if !defined(CPPCHECK) 919 GC_ATTR_DEPRECATED 920 #endif 921 void GC_CALL GC_debug_change_stubborn(const void *); 922 GC_API void GC_CALL GC_debug_end_stubborn_change(const void *) 923 GC_ATTR_NONNULL(1); 924 925 /* Routines that allocate objects with debug information (like the */ 926 /* above), but just fill in dummy file and line number information. */ 927 /* Thus they can serve as drop-in malloc/realloc replacements. This */ 928 /* can be useful for two reasons: */ 929 /* 1) It allows the collector to be built with DBG_HDRS_ALL defined */ 930 /* even if some allocation calls come from 3rd party libraries */ 931 /* that can't be recompiled. */ 932 /* 2) On some platforms, the file and line information is redundant, */ 933 /* since it can be reconstructed from a stack trace. On such */ 934 /* platforms it may be more convenient not to recompile, e.g. for */ 935 /* leak detection. This can be accomplished by instructing the */ 936 /* linker to replace malloc/realloc with these. */ 937 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL 938 GC_debug_malloc_replacement(size_t /* size_in_bytes */); 939 GC_API /* 'realloc' attr */ GC_ATTR_ALLOC_SIZE(2) void * GC_CALL 940 GC_debug_realloc_replacement(void * /* object_addr */, 941 size_t /* size_in_bytes */); 942 943 #ifdef GC_DEBUG_REPLACEMENT 944 # define GC_MALLOC(sz) GC_debug_malloc_replacement(sz) 945 # define GC_REALLOC(old, sz) GC_debug_realloc_replacement(old, sz) 946 #elif defined(GC_DEBUG) 947 # define GC_MALLOC(sz) GC_debug_malloc(sz, GC_EXTRAS) 948 # define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, GC_EXTRAS) 949 #else 950 # define GC_MALLOC(sz) GC_malloc(sz) 951 # define GC_REALLOC(old, sz) GC_realloc(old, sz) 952 #endif /* !GC_DEBUG_REPLACEMENT && !GC_DEBUG */ 953 954 #ifdef GC_DEBUG 955 # define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, GC_EXTRAS) 956 # define GC_STRDUP(s) GC_debug_strdup(s, GC_EXTRAS) 957 # define GC_STRNDUP(s, sz) GC_debug_strndup(s, sz, GC_EXTRAS) 958 # define GC_MALLOC_ATOMIC_UNCOLLECTABLE(sz) \ 959 GC_debug_malloc_atomic_uncollectable(sz, GC_EXTRAS) 960 # define GC_MALLOC_UNCOLLECTABLE(sz) \ 961 GC_debug_malloc_uncollectable(sz, GC_EXTRAS) 962 # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \ 963 GC_debug_malloc_ignore_off_page(sz, GC_EXTRAS) 964 # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \ 965 GC_debug_malloc_atomic_ignore_off_page(sz, GC_EXTRAS) 966 # define GC_FREE(p) GC_debug_free(p) 967 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \ 968 GC_debug_register_finalizer(p, f, d, of, od) 969 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \ 970 GC_debug_register_finalizer_ignore_self(p, f, d, of, od) 971 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \ 972 GC_debug_register_finalizer_no_order(p, f, d, of, od) 973 # define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \ 974 GC_debug_register_finalizer_unreachable(p, f, d, of, od) 975 # define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p) 976 # define GC_PTR_STORE_AND_DIRTY(p, q) GC_debug_ptr_store_and_dirty(p, q) 977 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \ 978 GC_general_register_disappearing_link(link, \ 979 GC_base((/* no const */ void *)(obj))) 980 # define GC_REGISTER_LONG_LINK(link, obj) \ 981 GC_register_long_link(link, GC_base((/* no const */ void *)(obj))) 982 # define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n) 983 #else 984 # define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz) 985 # define GC_STRDUP(s) GC_strdup(s) 986 # define GC_STRNDUP(s, sz) GC_strndup(s, sz) 987 # define GC_MALLOC_ATOMIC_UNCOLLECTABLE(sz) GC_malloc_atomic_uncollectable(sz) 988 # define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz) 989 # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \ 990 GC_malloc_ignore_off_page(sz) 991 # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \ 992 GC_malloc_atomic_ignore_off_page(sz) 993 # define GC_FREE(p) GC_free(p) 994 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \ 995 GC_register_finalizer(p, f, d, of, od) 996 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \ 997 GC_register_finalizer_ignore_self(p, f, d, of, od) 998 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \ 999 GC_register_finalizer_no_order(p, f, d, of, od) 1000 # define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \ 1001 GC_register_finalizer_unreachable(p, f, d, of, od) 1002 # define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p) 1003 # define GC_PTR_STORE_AND_DIRTY(p, q) GC_ptr_store_and_dirty(p, q) 1004 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \ 1005 GC_general_register_disappearing_link(link, obj) 1006 # define GC_REGISTER_LONG_LINK(link, obj) \ 1007 GC_register_long_link(link, obj) 1008 # define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n) 1009 #endif /* !GC_DEBUG */ 1010 1011 /* The following are included because they are often convenient, and */ 1012 /* reduce the chance for a misspecified size argument. But calls may */ 1013 /* expand to something syntactically incorrect if t is a complicated */ 1014 /* type expression. Note that, unlike C++ new operator, these ones */ 1015 /* may return NULL (if out of memory). */ 1016 #define GC_NEW(t) ((t*)GC_MALLOC(sizeof(t))) 1017 #define GC_NEW_ATOMIC(t) ((t*)GC_MALLOC_ATOMIC(sizeof(t))) 1018 #define GC_NEW_UNCOLLECTABLE(t) ((t*)GC_MALLOC_UNCOLLECTABLE(sizeof(t))) 1019 1020 #ifdef GC_REQUIRE_WCSDUP 1021 /* This might be unavailable on some targets (or not needed). */ 1022 /* wchar_t should be defined in stddef.h */ 1023 GC_API GC_ATTR_MALLOC wchar_t * GC_CALL 1024 GC_wcsdup(const wchar_t *) GC_ATTR_NONNULL(1); 1025 GC_API GC_ATTR_MALLOC wchar_t * GC_CALL 1026 GC_debug_wcsdup(const wchar_t *, GC_EXTRA_PARAMS) GC_ATTR_NONNULL(1); 1027 # ifdef GC_DEBUG 1028 # define GC_WCSDUP(s) GC_debug_wcsdup(s, GC_EXTRAS) 1029 # else 1030 # define GC_WCSDUP(s) GC_wcsdup(s) 1031 # endif 1032 #endif /* GC_REQUIRE_WCSDUP */ 1033 1034 /* Finalization. Some of these primitives are grossly unsafe. */ 1035 /* The idea is to make them both cheap, and sufficient to build */ 1036 /* a safer layer, closer to Modula-3, Java, or PCedar finalization. */ 1037 /* The interface represents my conclusions from a long discussion */ 1038 /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes, */ 1039 /* Christian Jacobi, and Russ Atkinson. It's not perfect, and */ 1040 /* probably nobody else agrees with it. Hans-J. Boehm 3/13/92 */ 1041 typedef void (GC_CALLBACK * GC_finalization_proc)(void * /* obj */, 1042 void * /* client_data */); 1043 1044 GC_API void GC_CALL GC_register_finalizer(void * /* obj */, 1045 GC_finalization_proc /* fn */, void * /* cd */, 1046 GC_finalization_proc * /* ofn */, void ** /* ocd */) 1047 GC_ATTR_NONNULL(1); 1048 GC_API void GC_CALL GC_debug_register_finalizer(void * /* obj */, 1049 GC_finalization_proc /* fn */, void * /* cd */, 1050 GC_finalization_proc * /* ofn */, void ** /* ocd */) 1051 GC_ATTR_NONNULL(1); 1052 /* When obj is no longer accessible, invoke */ 1053 /* (*fn)(obj, cd). If a and b are inaccessible, and */ 1054 /* a points to b (after disappearing links have been */ 1055 /* made to disappear), then only a will be */ 1056 /* finalized. (If this does not create any new */ 1057 /* pointers to b, then b will be finalized after the */ 1058 /* next collection.) Any finalizable object that */ 1059 /* is reachable from itself by following one or more */ 1060 /* pointers will not be finalized (or collected). */ 1061 /* Thus cycles involving finalizable objects should */ 1062 /* be avoided, or broken by disappearing links. */ 1063 /* All but the last finalizer registered for an object */ 1064 /* is ignored. */ 1065 /* No-op in the leak-finding mode. */ 1066 /* Finalization may be removed by passing 0 as fn. */ 1067 /* Finalizers are implicitly unregistered when they are */ 1068 /* enqueued for finalization (i.e. become ready to be */ 1069 /* finalized). */ 1070 /* The old finalizer and client data are stored in */ 1071 /* *ofn and *ocd. (ofn and/or ocd may be NULL. */ 1072 /* The allocation lock is held while *ofn and *ocd are */ 1073 /* updated. In case of error (no memory to register */ 1074 /* new finalizer), *ofn and *ocd remain unchanged.) */ 1075 /* Fn is never invoked on an accessible object, */ 1076 /* provided hidden pointers are converted to real */ 1077 /* pointers only if the allocation lock is held, and */ 1078 /* such conversions are not performed by finalization */ 1079 /* routines. */ 1080 /* If GC_register_finalizer is aborted as a result of */ 1081 /* a signal, the object may be left with no */ 1082 /* finalization, even if neither the old nor new */ 1083 /* finalizer were NULL. */ 1084 /* Obj should be the starting address of an object */ 1085 /* allocated by GC_malloc or friends. Obj may also be */ 1086 /* NULL or point to something outside GC heap (in this */ 1087 /* case, fn is ignored, *ofn and *ocd are set to NULL). */ 1088 /* Note that any garbage collectible object referenced */ 1089 /* by cd will be considered accessible until the */ 1090 /* finalizer is invoked. */ 1091 1092 /* Another versions of the above follow. It ignores */ 1093 /* self-cycles, i.e. pointers from a finalizable object to */ 1094 /* itself. There is a stylistic argument that this is wrong, */ 1095 /* but it's unavoidable for C++, since the compiler may */ 1096 /* silently introduce these. It's also benign in that specific */ 1097 /* case. And it helps if finalizable objects are split to */ 1098 /* avoid cycles. */ 1099 /* Note that cd will still be viewed as accessible, even if it */ 1100 /* refers to the object itself. */ 1101 GC_API void GC_CALL GC_register_finalizer_ignore_self(void * /* obj */, 1102 GC_finalization_proc /* fn */, void * /* cd */, 1103 GC_finalization_proc * /* ofn */, void ** /* ocd */) 1104 GC_ATTR_NONNULL(1); 1105 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self(void * /* obj */, 1106 GC_finalization_proc /* fn */, void * /* cd */, 1107 GC_finalization_proc * /* ofn */, void ** /* ocd */) 1108 GC_ATTR_NONNULL(1); 1109 1110 /* Another version of the above. It ignores all cycles. */ 1111 /* It should probably only be used by Java implementations. */ 1112 /* Note that cd will still be viewed as accessible, even if it */ 1113 /* refers to the object itself. */ 1114 GC_API void GC_CALL GC_register_finalizer_no_order(void * /* obj */, 1115 GC_finalization_proc /* fn */, void * /* cd */, 1116 GC_finalization_proc * /* ofn */, void ** /* ocd */) 1117 GC_ATTR_NONNULL(1); 1118 GC_API void GC_CALL GC_debug_register_finalizer_no_order(void * /* obj */, 1119 GC_finalization_proc /* fn */, void * /* cd */, 1120 GC_finalization_proc * /* ofn */, void ** /* ocd */) 1121 GC_ATTR_NONNULL(1); 1122 1123 /* This is a special finalizer that is useful when an object's */ 1124 /* finalizer must be run when the object is known to be no */ 1125 /* longer reachable, not even from other finalizable objects. */ 1126 /* It behaves like "normal" finalization, except that the */ 1127 /* finalizer is not run while the object is reachable from */ 1128 /* other objects specifying unordered finalization. */ 1129 /* Effectively it allows an object referenced, possibly */ 1130 /* indirectly, from an unordered finalizable object to override */ 1131 /* the unordered finalization request. */ 1132 /* This can be used in combination with finalizer_no_order so */ 1133 /* as to release resources that must not be released while an */ 1134 /* object can still be brought back to life by other */ 1135 /* finalizers. */ 1136 /* Only works if GC_java_finalization is set. Probably only */ 1137 /* of interest when implementing a language that requires */ 1138 /* unordered finalization (e.g. Java, C#). */ 1139 GC_API void GC_CALL GC_register_finalizer_unreachable(void * /* obj */, 1140 GC_finalization_proc /* fn */, void * /* cd */, 1141 GC_finalization_proc * /* ofn */, void ** /* ocd */) 1142 GC_ATTR_NONNULL(1); 1143 GC_API void GC_CALL GC_debug_register_finalizer_unreachable(void * /* obj */, 1144 GC_finalization_proc /* fn */, void * /* cd */, 1145 GC_finalization_proc * /* ofn */, void ** /* ocd */) 1146 GC_ATTR_NONNULL(1); 1147 1148 #define GC_NO_MEMORY 2 /* Failure due to lack of memory. */ 1149 1150 /* The following routine may be used to break cycles between */ 1151 /* finalizable objects, thus causing cyclic finalizable */ 1152 /* objects to be finalized in the correct order. Standard */ 1153 /* use involves calling GC_register_disappearing_link(&p), */ 1154 /* where p is a pointer that is not followed by finalization */ 1155 /* code, and should not be considered in determining */ 1156 /* finalization order. */ 1157 GC_API int GC_CALL GC_register_disappearing_link(void ** /* link */) 1158 GC_ATTR_NONNULL(1); 1159 /* Link should point to a field of a heap allocated */ 1160 /* object obj. *link will be cleared when obj is */ 1161 /* found to be inaccessible. This happens BEFORE any */ 1162 /* finalization code is invoked, and BEFORE any */ 1163 /* decisions about finalization order are made. */ 1164 /* This is useful in telling the finalizer that */ 1165 /* some pointers are not essential for proper */ 1166 /* finalization. This may avoid finalization cycles. */ 1167 /* Note that obj may be resurrected by another */ 1168 /* finalizer, and thus the clearing of *link may */ 1169 /* be visible to non-finalization code. */ 1170 /* There's an argument that an arbitrary action should */ 1171 /* be allowed here, instead of just clearing a pointer. */ 1172 /* But this causes problems if that action alters, or */ 1173 /* examines connectivity. Returns GC_DUPLICATE if link */ 1174 /* was already registered, GC_SUCCESS if registration */ 1175 /* succeeded, GC_NO_MEMORY if it failed for lack of */ 1176 /* memory, and GC_oom_fn did not handle the problem. */ 1177 /* Only exists for backward compatibility. See below: */ 1178 1179 GC_API int GC_CALL GC_general_register_disappearing_link(void ** /* link */, 1180 const void * /* obj */) 1181 GC_ATTR_NONNULL(1) GC_ATTR_NONNULL(2); 1182 /* A slight generalization of the above. *link is */ 1183 /* cleared when obj first becomes inaccessible. This */ 1184 /* can be used to implement weak pointers easily and */ 1185 /* safely. Typically link will point to a location */ 1186 /* holding a disguised pointer to obj. (A pointer */ 1187 /* inside an "atomic" object is effectively disguised.) */ 1188 /* In this way, weak pointers are broken before any */ 1189 /* object reachable from them gets finalized. */ 1190 /* Each link may be registered only with one obj value, */ 1191 /* i.e. all objects but the last one (link registered */ 1192 /* with) are ignored. This was added after a long */ 1193 /* email discussion with John Ellis. */ 1194 /* link must be non-NULL (and be properly aligned). */ 1195 /* obj must be a pointer to the first word of an object */ 1196 /* allocated by GC_malloc or friends. A link */ 1197 /* disappears when it is unregistered manually, or when */ 1198 /* (*link) is cleared, or when the object containing */ 1199 /* this link is garbage collected. It is unsafe to */ 1200 /* explicitly deallocate the object containing link. */ 1201 /* Explicit deallocation of obj may or may not cause */ 1202 /* link to eventually be cleared. */ 1203 /* No-op in the leak-finding mode. */ 1204 /* This function can be used to implement certain types */ 1205 /* of weak pointers. Note, however, this generally */ 1206 /* requires that the allocation lock is held (see */ 1207 /* GC_call_with_alloc_lock() below) when the disguised */ 1208 /* pointer is accessed. Otherwise a strong pointer */ 1209 /* could be recreated between the time the collector */ 1210 /* decides to reclaim the object and the link is */ 1211 /* cleared. Returns GC_SUCCESS if registration */ 1212 /* succeeded (a new link is registered), GC_DUPLICATE */ 1213 /* if link was already registered (with some object), */ 1214 /* GC_NO_MEMORY if registration failed for lack of */ 1215 /* memory (and GC_oom_fn did not handle the problem), */ 1216 /* GC_UNIMPLEMENTED if GC_find_leak is true. */ 1217 1218 GC_API int GC_CALL GC_move_disappearing_link(void ** /* link */, 1219 void ** /* new_link */) 1220 GC_ATTR_NONNULL(2); 1221 /* Moves a link previously registered via */ 1222 /* GC_general_register_disappearing_link (or */ 1223 /* GC_register_disappearing_link). Does not change the */ 1224 /* target object of the weak reference. Does not */ 1225 /* change (*new_link) content. May be called with */ 1226 /* new_link equal to link (to check whether link has */ 1227 /* been registered). Returns GC_SUCCESS on success, */ 1228 /* GC_DUPLICATE if there is already another */ 1229 /* disappearing link at the new location (never */ 1230 /* returned if new_link is equal to link), GC_NOT_FOUND */ 1231 /* if no link is registered at the original location. */ 1232 1233 GC_API int GC_CALL GC_unregister_disappearing_link(void ** /* link */); 1234 /* Undoes a registration by either of the above two */ 1235 /* routines. Returns 0 if link was not actually */ 1236 /* registered (otherwise returns 1). */ 1237 1238 GC_API int GC_CALL GC_register_long_link(void ** /* link */, 1239 const void * /* obj */) 1240 GC_ATTR_NONNULL(1) GC_ATTR_NONNULL(2); 1241 /* Similar to GC_general_register_disappearing_link but */ 1242 /* *link only gets cleared when obj becomes truly */ 1243 /* inaccessible. An object becomes truly inaccessible */ 1244 /* when it can no longer be resurrected from its */ 1245 /* finalizer (e.g. by assigning itself to a pointer */ 1246 /* traceable from root). This can be used to implement */ 1247 /* long weak pointers easily and safely. */ 1248 1249 GC_API int GC_CALL GC_move_long_link(void ** /* link */, 1250 void ** /* new_link */) 1251 GC_ATTR_NONNULL(2); 1252 /* Similar to GC_move_disappearing_link but for a link */ 1253 /* previously registered via GC_register_long_link. */ 1254 1255 GC_API int GC_CALL GC_unregister_long_link(void ** /* link */); 1256 /* Similar to GC_unregister_disappearing_link but for a */ 1257 /* registration by either of the above two routines. */ 1258 1259 /* Support of toggle-ref style of external memory management */ 1260 /* without hooking up to the host retain/release machinery. */ 1261 /* The idea of toggle-ref is that an external reference to */ 1262 /* an object is kept and it can be either a strong or weak */ 1263 /* reference; a weak reference is used when the external peer */ 1264 /* has no interest in the object, and a strong otherwise. */ 1265 typedef enum { 1266 GC_TOGGLE_REF_DROP, 1267 GC_TOGGLE_REF_STRONG, 1268 GC_TOGGLE_REF_WEAK 1269 } GC_ToggleRefStatus; 1270 1271 /* The callback is to decide (return) the new state of a given */ 1272 /* object. Invoked by the collector for all objects registered */ 1273 /* for toggle-ref processing. Invoked with the allocation lock */ 1274 /* held (but the "world" is running). */ 1275 typedef GC_ToggleRefStatus (GC_CALLBACK *GC_toggleref_func)(void * /* obj */); 1276 1277 /* Set (register) a callback that decides the state of a given */ 1278 /* object (by, probably, inspecting its native state). */ 1279 /* The argument may be 0 (means no callback). Both the setter */ 1280 /* and the getter acquire the allocation lock (to avoid data */ 1281 /* races). */ 1282 GC_API void GC_CALL GC_set_toggleref_func(GC_toggleref_func); 1283 GC_API GC_toggleref_func GC_CALL GC_get_toggleref_func(void); 1284 1285 /* Register a given object for toggle-ref processing. It will */ 1286 /* be stored internally and the toggle-ref callback will be */ 1287 /* invoked on the object until the callback returns */ 1288 /* GC_TOGGLE_REF_DROP or the object is collected. If is_strong */ 1289 /* is true then the object is registered with a strong ref, */ 1290 /* a weak one otherwise. Returns GC_SUCCESS if registration */ 1291 /* succeeded (or no callback registered yet), GC_NO_MEMORY if */ 1292 /* it failed for lack of memory. */ 1293 GC_API int GC_CALL GC_toggleref_add(void * /* obj */, int /* is_strong */) 1294 GC_ATTR_NONNULL(1); 1295 1296 /* Finalizer callback support. Invoked by the collector (with */ 1297 /* the allocation lock held) for each unreachable object */ 1298 /* enqueued for finalization. */ 1299 typedef void (GC_CALLBACK * GC_await_finalize_proc)(void * /* obj */); 1300 GC_API void GC_CALL GC_set_await_finalize_proc(GC_await_finalize_proc); 1301 GC_API GC_await_finalize_proc GC_CALL GC_get_await_finalize_proc(void); 1302 /* Zero means no callback. The setter */ 1303 /* and getter acquire the lock too. */ 1304 1305 /* Returns !=0 if GC_invoke_finalizers has something to do. */ 1306 /* Does not use any synchronization. */ 1307 GC_API int GC_CALL GC_should_invoke_finalizers(void); 1308 1309 GC_API int GC_CALL GC_invoke_finalizers(void); 1310 /* Run finalizers for all objects that are ready to */ 1311 /* be finalized. Return the number of finalizers */ 1312 /* that were run. Normally this is also called */ 1313 /* implicitly during some allocations. If */ 1314 /* GC_finalize_on_demand is nonzero, it must be called */ 1315 /* explicitly. */ 1316 1317 /* Explicitly tell the collector that an object is reachable */ 1318 /* at a particular program point. This prevents the argument */ 1319 /* pointer from being optimized away, even it is otherwise no */ 1320 /* longer needed. It should have no visible effect in the */ 1321 /* absence of finalizers or disappearing links. But it may be */ 1322 /* needed to prevent finalizers from running while the */ 1323 /* associated external resource is still in use. */ 1324 /* The function is sometimes called keep_alive in other */ 1325 /* settings. */ 1326 #if defined(__GNUC__) && !defined(__INTEL_COMPILER) 1327 # define GC_reachable_here(ptr) \ 1328 __asm__ __volatile__(" " : : "X"(ptr) : "memory") 1329 #else 1330 GC_API void GC_CALL GC_noop1(GC_word); 1331 # ifdef LINT2 1332 # define GC_reachable_here(ptr) GC_noop1(~(GC_word)(ptr)^(~(GC_word)0)) 1333 /* The expression matches the one of COVERT_DATAFLOW(). */ 1334 # else 1335 # define GC_reachable_here(ptr) GC_noop1((GC_word)(ptr)) 1336 # endif 1337 #endif 1338 1339 /* GC_set_warn_proc can be used to redirect or filter warning messages. */ 1340 /* p may not be a NULL pointer. msg is printf format string (arg must */ 1341 /* match the format). Both the setter and the getter acquire the GC */ 1342 /* lock (to avoid data races). In version 7.1 (and before), the setter */ 1343 /* returned the old warn_proc value. */ 1344 typedef void (GC_CALLBACK * GC_warn_proc)(char * /* msg */, 1345 GC_word /* arg */); 1346 GC_API void GC_CALL GC_set_warn_proc(GC_warn_proc /* p */) GC_ATTR_NONNULL(1); 1347 /* GC_get_warn_proc returns the current warn_proc. */ 1348 GC_API GC_warn_proc GC_CALL GC_get_warn_proc(void); 1349 1350 /* GC_ignore_warn_proc may be used as an argument for GC_set_warn_proc */ 1351 /* to suppress all warnings (unless statistics printing is turned on). */ 1352 GC_API void GC_CALLBACK GC_ignore_warn_proc(char *, GC_word); 1353 1354 /* Change file descriptor of GC log. Unavailable on some targets. */ 1355 GC_API void GC_CALL GC_set_log_fd(int); 1356 1357 /* abort_func is invoked on GC fatal aborts (just before OS-dependent */ 1358 /* abort or exit(1) is called). Must be non-NULL. The default one */ 1359 /* outputs msg to stderr provided msg is non-NULL. msg is NULL if */ 1360 /* invoked before exit(1) otherwise msg is non-NULL (i.e., if invoked */ 1361 /* before abort). Both the setter and getter acquire the GC lock. */ 1362 /* Both the setter and getter are defined only if the library has been */ 1363 /* compiled without SMALL_CONFIG. */ 1364 typedef void (GC_CALLBACK * GC_abort_func)(const char * /* msg */); 1365 GC_API void GC_CALL GC_set_abort_func(GC_abort_func) GC_ATTR_NONNULL(1); 1366 GC_API GC_abort_func GC_CALL GC_get_abort_func(void); 1367 1368 /* A portable way to abort the application because of not enough memory.*/ 1369 GC_API void GC_CALL GC_abort_on_oom(void); 1370 1371 /* The following is intended to be used by a higher level */ 1372 /* (e.g. Java-like) finalization facility. It is expected */ 1373 /* that finalization code will arrange for hidden pointers to */ 1374 /* disappear. Otherwise objects can be accessed after they */ 1375 /* have been collected. */ 1376 /* Should not be used in the leak-finding mode. */ 1377 /* Note that putting pointers in atomic objects or in */ 1378 /* non-pointer slots of "typed" objects is equivalent to */ 1379 /* disguising them in this way, and may have other advantages. */ 1380 typedef GC_word GC_hidden_pointer; 1381 #define GC_HIDE_POINTER(p) (~(GC_hidden_pointer)(p)) 1382 /* Converting a hidden pointer to a real pointer requires verifying */ 1383 /* that the object still exists. This involves acquiring the */ 1384 /* allocator lock to avoid a race with the collector. */ 1385 #define GC_REVEAL_POINTER(p) ((void *)GC_HIDE_POINTER(p)) 1386 1387 #if defined(I_HIDE_POINTERS) || defined(GC_I_HIDE_POINTERS) 1388 /* This exists only for compatibility (the GC-prefixed symbols are */ 1389 /* preferred for new code). */ 1390 # define HIDE_POINTER(p) GC_HIDE_POINTER(p) 1391 # define REVEAL_POINTER(p) GC_REVEAL_POINTER(p) 1392 #endif 1393 1394 typedef void * (GC_CALLBACK * GC_fn_type)(void * /* client_data */); 1395 GC_API void * GC_CALL GC_call_with_alloc_lock(GC_fn_type /* fn */, 1396 void * /* client_data */) GC_ATTR_NONNULL(1); 1397 1398 /* These routines are intended to explicitly notify the collector */ 1399 /* of new threads. Often this is unnecessary because thread creation */ 1400 /* is implicitly intercepted by the collector, using header-file */ 1401 /* defines, or linker-based interception. In the long run the intent */ 1402 /* is to always make redundant registration safe. In the short run, */ 1403 /* this is being implemented a platform at a time. */ 1404 /* The interface is complicated by the fact that we probably will not */ 1405 /* ever be able to automatically determine the stack base for thread */ 1406 /* stacks on all platforms. */ 1407 1408 /* Structure representing the base of a thread stack. On most */ 1409 /* platforms this contains just a single address. */ 1410 struct GC_stack_base { 1411 void * mem_base; /* Base of memory stack. */ 1412 # if defined(__ia64) || defined(__ia64__) || defined(_M_IA64) 1413 void * reg_base; /* Base of separate register stack. */ 1414 # endif 1415 }; 1416 1417 typedef void * (GC_CALLBACK * GC_stack_base_func)( 1418 struct GC_stack_base * /* sb */, void * /* arg */); 1419 1420 /* Call a function with a stack base structure corresponding to */ 1421 /* somewhere in the GC_call_with_stack_base frame. This often can */ 1422 /* be used to provide a sufficiently accurate stack base. And we */ 1423 /* implement it everywhere. */ 1424 GC_API void * GC_CALL GC_call_with_stack_base(GC_stack_base_func /* fn */, 1425 void * /* arg */) GC_ATTR_NONNULL(1); 1426 1427 #define GC_SUCCESS 0 1428 #define GC_DUPLICATE 1 /* Was already registered. */ 1429 #define GC_NO_THREADS 2 /* No thread support in GC. */ 1430 /* GC_NO_THREADS is not returned by any GC function anymore. */ 1431 #define GC_UNIMPLEMENTED 3 /* Not yet implemented on this platform. */ 1432 #define GC_NOT_FOUND 4 /* Requested link not found (returned */ 1433 /* by GC_move_disappearing_link). */ 1434 1435 #if defined(GC_DARWIN_THREADS) || defined(GC_WIN32_THREADS) 1436 /* Use implicit thread registration and processing (via Win32 DllMain */ 1437 /* or Darwin task_threads). Deprecated. Must be called before */ 1438 /* GC_INIT() and other GC routines. Should be avoided if */ 1439 /* GC_pthread_create, GC_beginthreadex (or GC_CreateThread) could be */ 1440 /* called instead. Disables parallelized GC on Win32. */ 1441 GC_API void GC_CALL GC_use_threads_discovery(void); 1442 #endif 1443 1444 #ifdef GC_THREADS 1445 /* Suggest the GC to use the specific signal to suspend threads. */ 1446 /* Has no effect after GC_init and on non-POSIX systems. */ 1447 GC_API void GC_CALL GC_set_suspend_signal(int); 1448 1449 /* Suggest the GC to use the specific signal to resume threads. */ 1450 /* Has no effect after GC_init and on non-POSIX systems. */ 1451 GC_API void GC_CALL GC_set_thr_restart_signal(int); 1452 1453 /* Return the signal number (constant after initialization) used by */ 1454 /* the GC to suspend threads on POSIX systems. Return -1 otherwise. */ 1455 GC_API int GC_CALL GC_get_suspend_signal(void); 1456 1457 /* Return the signal number (constant after initialization) used by */ 1458 /* the garbage collector to restart (resume) threads on POSIX */ 1459 /* systems. Return -1 otherwise. */ 1460 GC_API int GC_CALL GC_get_thr_restart_signal(void); 1461 1462 /* Restart marker threads after POSIX fork in child. Meaningless in */ 1463 /* other situations. Should not be called if fork followed by exec. */ 1464 GC_API void GC_CALL GC_start_mark_threads(void); 1465 1466 /* Explicitly enable GC_register_my_thread() invocation. */ 1467 /* Done implicitly if a GC thread-creation function is called (or */ 1468 /* implicit thread registration is activated, or the collector is */ 1469 /* compiled with GC_ALWAYS_MULTITHREADED defined). Otherwise, it */ 1470 /* must be called from the main (or any previously registered) thread */ 1471 /* between the collector initialization and the first explicit */ 1472 /* registering of a thread (it should be called as late as possible). */ 1473 GC_API void GC_CALL GC_allow_register_threads(void); 1474 1475 /* Register the current thread, with the indicated stack base, as */ 1476 /* a new thread whose stack(s) should be traced by the GC. If it */ 1477 /* is not implicitly called by the GC, this must be called before a */ 1478 /* thread can allocate garbage collected memory, or assign pointers */ 1479 /* to the garbage collected heap. Once registered, a thread will be */ 1480 /* stopped during garbage collections. */ 1481 /* This call must be previously enabled (see above). */ 1482 /* This should never be called from the main thread, where it is */ 1483 /* always done implicitly. This is normally done implicitly if GC_ */ 1484 /* functions are called to create the thread, e.g. by including gc.h */ 1485 /* (which redefines some system functions) before calling the system */ 1486 /* thread creation function. Nonetheless, thread cleanup routines */ 1487 /* (e.g., pthread key destructor) typically require manual thread */ 1488 /* registering (and unregistering) if pointers to GC-allocated */ 1489 /* objects are manipulated inside. */ 1490 /* It is also always done implicitly on some platforms if */ 1491 /* GC_use_threads_discovery() is called at start-up. Except for the */ 1492 /* latter case, the explicit call is normally required for threads */ 1493 /* created by third-party libraries. */ 1494 /* A manually registered thread requires manual unregistering. */ 1495 /* Returns GC_SUCCESS on success, GC_DUPLICATE if already registered. */ 1496 GC_API int GC_CALL GC_register_my_thread(const struct GC_stack_base *) 1497 GC_ATTR_NONNULL(1); 1498 1499 /* Return non-zero (TRUE) if and only if the calling thread is */ 1500 /* registered with the garbage collector. */ 1501 GC_API int GC_CALL GC_thread_is_registered(void); 1502 1503 /* Notify the collector about the stack and the alt-stack of the */ 1504 /* current thread. stack_start/size is used to determine the stack */ 1505 /* boundaries when a thread is suspended while it is on an alt-stack. */ 1506 GC_API void GC_CALL GC_register_altstack(void * /* stack_start */, 1507 GC_word /* stack_size */, 1508 void * /* altstack_base */, 1509 GC_word /* altstack_size */); 1510 1511 /* Unregister the current thread. Only an explicitly registered */ 1512 /* thread (i.e. for which GC_register_my_thread() returns GC_SUCCESS) */ 1513 /* is allowed (and required) to call this function. (As a special */ 1514 /* exception, it is also allowed to once unregister the main thread.) */ 1515 /* The thread may no longer allocate garbage collected memory or */ 1516 /* manipulate pointers to the garbage collected heap after making */ 1517 /* this call. Specifically, if it wants to return or otherwise */ 1518 /* communicate a pointer to the garbage-collected heap to another */ 1519 /* thread, it must do this before calling GC_unregister_my_thread, */ 1520 /* most probably by saving it in a global data structure. Must not */ 1521 /* be called inside a GC callback function (except for */ 1522 /* GC_call_with_stack_base() one). */ 1523 GC_API int GC_CALL GC_unregister_my_thread(void); 1524 1525 /* Stop/start the world explicitly. Not recommended for general use. */ 1526 GC_API void GC_CALL GC_stop_world_external(void); 1527 GC_API void GC_CALL GC_start_world_external(void); 1528 #endif /* GC_THREADS */ 1529 1530 /* Wrapper for functions that are likely to block (or, at least, do not */ 1531 /* allocate garbage collected memory and/or manipulate pointers to the */ 1532 /* garbage collected heap) for an appreciable length of time. While fn */ 1533 /* is running, the collector is said to be in the "inactive" state for */ 1534 /* the current thread (this means that the thread is not suspended and */ 1535 /* the thread's stack frames "belonging" to the functions in the */ 1536 /* "inactive" state are not scanned during garbage collections). It is */ 1537 /* assumed that the collector is already initialized and the current */ 1538 /* thread is registered. It is allowed for fn to call */ 1539 /* GC_call_with_gc_active() (even recursively), thus temporarily */ 1540 /* toggling the collector's state back to "active". The latter */ 1541 /* technique might be used to make stack scanning more precise (i.e. */ 1542 /* scan only stack frames of functions that allocate garbage collected */ 1543 /* memory and/or manipulate pointers to the garbage collected heap). */ 1544 GC_API void * GC_CALL GC_do_blocking(GC_fn_type /* fn */, 1545 void * /* client_data */) GC_ATTR_NONNULL(1); 1546 1547 /* Call a function switching to the "active" state of the collector for */ 1548 /* the current thread (i.e. the user function is allowed to call any */ 1549 /* GC function and/or manipulate pointers to the garbage collected */ 1550 /* heap). GC_call_with_gc_active() has the functionality opposite to */ 1551 /* GC_do_blocking() one. It is assumed that the collector is already */ 1552 /* initialized and the current thread is registered. fn may toggle */ 1553 /* the collector thread's state temporarily to "inactive" one by using */ 1554 /* GC_do_blocking. GC_call_with_gc_active() often can be used to */ 1555 /* provide a sufficiently accurate stack base. */ 1556 GC_API void * GC_CALL GC_call_with_gc_active(GC_fn_type /* fn */, 1557 void * /* client_data */) GC_ATTR_NONNULL(1); 1558 1559 /* Attempt to fill in the GC_stack_base structure with the stack base */ 1560 /* for this thread. This appears to be required to implement anything */ 1561 /* like the JNI AttachCurrentThread in an environment in which new */ 1562 /* threads are not automatically registered with the collector. */ 1563 /* It is also unfortunately hard to implement well on many platforms. */ 1564 /* Returns GC_SUCCESS or GC_UNIMPLEMENTED. This function acquires the */ 1565 /* GC lock on some platforms. */ 1566 GC_API int GC_CALL GC_get_stack_base(struct GC_stack_base *) 1567 GC_ATTR_NONNULL(1); 1568 1569 /* The following routines are primarily intended for use with a */ 1570 /* preprocessor which inserts calls to check C pointer arithmetic. */ 1571 /* They indicate failure by invoking the corresponding _print_proc. */ 1572 1573 /* Check that p and q point to the same object. */ 1574 /* Fail conspicuously if they don't. */ 1575 /* Returns the first argument. */ 1576 /* Succeeds if neither p nor q points to the heap. */ 1577 /* May succeed if both p and q point to between heap objects. */ 1578 GC_API void * GC_CALL GC_same_obj(void * /* p */, void * /* q */); 1579 1580 /* Checked pointer pre- and post- increment operations. Note that */ 1581 /* the second argument is in units of bytes, not multiples of the */ 1582 /* object size. This should either be invoked from a macro, or the */ 1583 /* call should be automatically generated. */ 1584 GC_API void * GC_CALL GC_pre_incr(void **, ptrdiff_t /* how_much */) 1585 GC_ATTR_NONNULL(1); 1586 GC_API void * GC_CALL GC_post_incr(void **, ptrdiff_t /* how_much */) 1587 GC_ATTR_NONNULL(1); 1588 1589 /* Check that p is visible */ 1590 /* to the collector as a possibly pointer containing location. */ 1591 /* If it isn't fail conspicuously. */ 1592 /* Returns the argument in all cases. May erroneously succeed */ 1593 /* in hard cases. (This is intended for debugging use with */ 1594 /* untyped allocations. The idea is that it should be possible, though */ 1595 /* slow, to add such a call to all indirect pointer stores.) */ 1596 /* Currently useless for multi-threaded worlds. */ 1597 GC_API void * GC_CALL GC_is_visible(void * /* p */); 1598 1599 /* Check that if p is a pointer to a heap page, then it points to */ 1600 /* a valid displacement within a heap object. */ 1601 /* Fail conspicuously if this property does not hold. */ 1602 /* Uninteresting with GC_all_interior_pointers. */ 1603 /* Always returns its argument. */ 1604 GC_API void * GC_CALL GC_is_valid_displacement(void * /* p */); 1605 1606 /* Explicitly dump the GC state. This is most often called from the */ 1607 /* debugger, or by setting the GC_DUMP_REGULARLY environment variable, */ 1608 /* but it may be useful to call it from client code during debugging. */ 1609 /* The current collection number is printed in the header of the dump. */ 1610 /* Acquires the GC lock to avoid data races. */ 1611 /* Defined only if the library has been compiled without NO_DEBUGGING. */ 1612 GC_API void GC_CALL GC_dump(void); 1613 1614 /* The same as GC_dump but allows to specify the name of dump and does */ 1615 /* not acquire the lock. If name is non-NULL, it is printed to help */ 1616 /* identifying individual dumps. Otherwise the current collection */ 1617 /* number is used as the name. */ 1618 /* Defined only if the library has been compiled without NO_DEBUGGING. */ 1619 GC_API void GC_CALL GC_dump_named(const char * /* name */); 1620 1621 /* Dump information about each block of every GC memory section. */ 1622 /* Defined only if the library has been compiled without NO_DEBUGGING. */ 1623 GC_API void GC_CALL GC_dump_regions(void); 1624 1625 /* Dump information about every registered disappearing link and */ 1626 /* finalizable object. */ 1627 /* Defined only if the library has been compiled without NO_DEBUGGING. */ 1628 GC_API void GC_CALL GC_dump_finalization(void); 1629 1630 /* Safer, but slow, pointer addition. Probably useful mainly with */ 1631 /* a preprocessor. Useful only for heap pointers. */ 1632 /* Only the macros without trailing digits are meant to be used */ 1633 /* by clients. These are designed to model the available C pointer */ 1634 /* arithmetic expressions. */ 1635 /* Even then, these are probably more useful as */ 1636 /* documentation than as part of the API. */ 1637 /* Note that GC_PTR_ADD evaluates the first argument more than once. */ 1638 #if defined(GC_DEBUG) && defined(__GNUC__) 1639 # define GC_PTR_ADD3(x, n, type_of_result) \ 1640 ((type_of_result)GC_same_obj((x)+(n), (x))) 1641 # define GC_PRE_INCR3(x, n, type_of_result) \ 1642 ((type_of_result)GC_pre_incr((void **)(&(x)), (n)*sizeof(*x))) 1643 # define GC_POST_INCR3(x, n, type_of_result) \ 1644 ((type_of_result)GC_post_incr((void **)(&(x)), (n)*sizeof(*x))) 1645 # define GC_PTR_ADD(x, n) GC_PTR_ADD3(x, n, __typeof__(x)) 1646 # define GC_PRE_INCR(x, n) GC_PRE_INCR3(x, n, __typeof__(x)) 1647 # define GC_POST_INCR(x) GC_POST_INCR3(x, 1, __typeof__(x)) 1648 # define GC_POST_DECR(x) GC_POST_INCR3(x, -1, __typeof__(x)) 1649 #else /* !GC_DEBUG || !__GNUC__ */ 1650 /* We can't do this right without typeof, which ANSI decided was not */ 1651 /* sufficiently useful. Without it we resort to the non-debug version. */ 1652 /* TODO: This should eventually support C++0x decltype. */ 1653 # define GC_PTR_ADD(x, n) ((x)+(n)) 1654 # define GC_PRE_INCR(x, n) ((x) += (n)) 1655 # define GC_POST_INCR(x) ((x)++) 1656 # define GC_POST_DECR(x) ((x)--) 1657 #endif /* !GC_DEBUG || !__GNUC__ */ 1658 1659 /* Safer assignment of a pointer to a non-stack location. */ 1660 #ifdef GC_DEBUG 1661 # define GC_PTR_STORE(p, q) \ 1662 (*(void **)GC_is_visible((void *)(p)) = \ 1663 GC_is_valid_displacement((void *)(q))) 1664 #else 1665 # define GC_PTR_STORE(p, q) (*(void **)(p) = (void *)(q)) 1666 #endif 1667 1668 /* GC_PTR_STORE_AND_DIRTY(p,q) is equivalent to GC_PTR_STORE(p,q) */ 1669 /* followed by GC_END_STUBBORN_CHANGE(p) and GC_reachable_here(q) */ 1670 /* (assuming p and q do not have side effects). */ 1671 GC_API void GC_CALL GC_ptr_store_and_dirty(void * /* p */, 1672 const void * /* q */); 1673 GC_API void GC_CALL GC_debug_ptr_store_and_dirty(void * /* p */, 1674 const void * /* q */); 1675 1676 /* Functions called to report pointer checking errors */ 1677 GC_API void (GC_CALLBACK * GC_same_obj_print_proc)(void * /* p */, 1678 void * /* q */); 1679 GC_API void (GC_CALLBACK * GC_is_valid_displacement_print_proc)(void *); 1680 GC_API void (GC_CALLBACK * GC_is_visible_print_proc)(void *); 1681 1682 #ifdef GC_PTHREADS 1683 /* For pthread support, we generally need to intercept a number of */ 1684 /* thread library calls. We do that here by macro defining them. */ 1685 # ifdef __cplusplus 1686 } /* extern "C" */ 1687 # endif 1688 # include "gc_pthread_redirects.h" 1689 # ifdef __cplusplus 1690 extern "C" { 1691 # endif 1692 #endif 1693 1694 /* This returns a list of objects, linked through their first word. */ 1695 /* Its use can greatly reduce lock contention problems, since the */ 1696 /* allocation lock can be acquired and released many fewer times. */ 1697 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_many(size_t /* lb */); 1698 #define GC_NEXT(p) (*(void * *)(p)) /* Retrieve the next element */ 1699 /* in returned list. */ 1700 1701 /* A filter function to control the scanning of dynamic libraries. */ 1702 /* If implemented, called by GC before registering a dynamic library */ 1703 /* (discovered by GC) section as a static data root (called only as */ 1704 /* a last reason not to register). The filename of the library, the */ 1705 /* address and the length of the memory region (section) are passed. */ 1706 /* This routine should return nonzero if that region should be scanned. */ 1707 /* Always called with the allocation lock held. Depending on the */ 1708 /* platform, might be called with the "world" stopped. */ 1709 typedef int (GC_CALLBACK * GC_has_static_roots_func)( 1710 const char * /* dlpi_name */, 1711 void * /* section_start */, 1712 size_t /* section_size */); 1713 1714 /* Register a new callback (a user-supplied filter) to control the */ 1715 /* scanning of dynamic libraries. Replaces any previously registered */ 1716 /* callback. May be 0 (means no filtering). May be unused on some */ 1717 /* platforms (if the filtering is unimplemented or inappropriate). */ 1718 GC_API void GC_CALL GC_register_has_static_roots_callback( 1719 GC_has_static_roots_func); 1720 1721 #if !defined(CPPCHECK) && !defined(GC_WINDOWS_H_INCLUDED) && defined(WINAPI) 1722 /* windows.h is included before gc.h */ 1723 # define GC_WINDOWS_H_INCLUDED 1724 #endif 1725 1726 #if defined(GC_WIN32_THREADS) \ 1727 && (!defined(GC_PTHREADS) || defined(GC_BUILD) \ 1728 || defined(GC_WINDOWS_H_INCLUDED)) 1729 /* Note: for Cygwin and pthreads-win32, this is skipped */ 1730 /* unless windows.h is included before gc.h. */ 1731 1732 # if (!defined(GC_NO_THREAD_DECLS) || defined(GC_BUILD)) \ 1733 && !defined(GC_DONT_INCL_WINDOWS_H) 1734 1735 # ifdef __cplusplus 1736 } /* Including windows.h in an extern "C" context no longer works. */ 1737 # endif 1738 1739 # if !defined(_WIN32_WCE) && !defined(__CEGCC__) 1740 # include <process.h> /* For _beginthreadex, _endthreadex */ 1741 # endif 1742 1743 # if defined(GC_BUILD) || !defined(GC_DONT_INCLUDE_WINDOWS_H) 1744 # include <windows.h> 1745 # define GC_WINDOWS_H_INCLUDED 1746 # endif 1747 1748 # ifdef __cplusplus 1749 extern "C" { 1750 # endif 1751 1752 # ifdef GC_UNDERSCORE_STDCALL 1753 /* Explicitly prefix exported/imported WINAPI (__stdcall) symbols */ 1754 /* with '_' (underscore). Might be useful if MinGW/x86 is used. */ 1755 # define GC_CreateThread _GC_CreateThread 1756 # define GC_ExitThread _GC_ExitThread 1757 # endif 1758 1759 # ifndef DECLSPEC_NORETURN 1760 /* Typically defined in winnt.h. */ 1761 # ifdef GC_WINDOWS_H_INCLUDED 1762 # define DECLSPEC_NORETURN /* empty */ 1763 # else 1764 # define DECLSPEC_NORETURN __declspec(noreturn) 1765 # endif 1766 # endif 1767 1768 # if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED) \ 1769 && !defined(UINTPTR_MAX) 1770 typedef GC_word GC_uintptr_t; 1771 # else 1772 typedef uintptr_t GC_uintptr_t; 1773 # endif 1774 1775 # ifdef _WIN64 1776 # define GC_WIN32_SIZE_T GC_uintptr_t 1777 # elif defined(GC_WINDOWS_H_INCLUDED) 1778 # define GC_WIN32_SIZE_T DWORD 1779 # else 1780 # define GC_WIN32_SIZE_T unsigned long 1781 # endif 1782 1783 # ifdef GC_INSIDE_DLL 1784 /* Export GC DllMain to be invoked from client DllMain. */ 1785 # ifdef GC_UNDERSCORE_STDCALL 1786 # define GC_DllMain _GC_DllMain 1787 # endif 1788 # ifdef GC_WINDOWS_H_INCLUDED 1789 GC_API BOOL WINAPI GC_DllMain(HINSTANCE /* inst */, 1790 ULONG /* reason */, 1791 LPVOID /* reserved */); 1792 # else 1793 GC_API int __stdcall GC_DllMain(void *, unsigned long, void *); 1794 # endif 1795 # endif /* GC_INSIDE_DLL */ 1796 1797 /* All threads must be created using GC_CreateThread or */ 1798 /* GC_beginthreadex, or must explicitly call GC_register_my_thread */ 1799 /* (and call GC_unregister_my_thread before thread termination), so */ 1800 /* that they will be recorded in the thread table. For backward */ 1801 /* compatibility, it is possible to build the GC with GC_DLL */ 1802 /* defined, and to call GC_use_threads_discovery. This implicitly */ 1803 /* registers all created threads, but appears to be less robust. */ 1804 /* Currently the collector expects all threads to fall through and */ 1805 /* terminate normally, or call GC_endthreadex() or GC_ExitThread, */ 1806 /* so that the thread is properly unregistered. */ 1807 # ifdef GC_WINDOWS_H_INCLUDED 1808 GC_API HANDLE WINAPI GC_CreateThread( 1809 LPSECURITY_ATTRIBUTES /* lpThreadAttributes */, 1810 GC_WIN32_SIZE_T /* dwStackSize */, 1811 LPTHREAD_START_ROUTINE /* lpStartAddress */, 1812 LPVOID /* lpParameter */, DWORD /* dwCreationFlags */, 1813 LPDWORD /* lpThreadId */); 1814 1815 GC_API DECLSPEC_NORETURN void WINAPI GC_ExitThread( 1816 DWORD /* dwExitCode */); 1817 # else 1818 struct _SECURITY_ATTRIBUTES; 1819 GC_API void *__stdcall GC_CreateThread(struct _SECURITY_ATTRIBUTES *, 1820 GC_WIN32_SIZE_T, 1821 unsigned long (__stdcall *)(void *), 1822 void *, unsigned long, unsigned long *); 1823 GC_API DECLSPEC_NORETURN void __stdcall GC_ExitThread(unsigned long); 1824 # endif 1825 1826 # if !defined(_WIN32_WCE) && !defined(__CEGCC__) 1827 GC_API GC_uintptr_t GC_CALL GC_beginthreadex( 1828 void * /* security */, unsigned /* stack_size */, 1829 unsigned (__stdcall *)(void *), 1830 void * /* arglist */, unsigned /* initflag */, 1831 unsigned * /* thrdaddr */); 1832 1833 /* Note: _endthreadex() is not currently marked as no-return in */ 1834 /* VC++ and MinGW headers, so we don't mark it neither. */ 1835 GC_API void GC_CALL GC_endthreadex(unsigned /* retval */); 1836 # endif /* !_WIN32_WCE */ 1837 1838 # endif /* !GC_NO_THREAD_DECLS */ 1839 1840 # ifdef GC_WINMAIN_REDIRECT 1841 /* win32_threads.c implements the real WinMain(), which will start */ 1842 /* a new thread to call GC_WinMain() after initializing the garbage */ 1843 /* collector. */ 1844 # define WinMain GC_WinMain 1845 # endif 1846 1847 /* For compatibility only. */ 1848 # define GC_use_DllMain GC_use_threads_discovery 1849 1850 # ifndef GC_NO_THREAD_REDIRECTS 1851 # define CreateThread GC_CreateThread 1852 # define ExitThread GC_ExitThread 1853 # undef _beginthreadex 1854 # define _beginthreadex GC_beginthreadex 1855 # undef _endthreadex 1856 # define _endthreadex GC_endthreadex 1857 /* #define _beginthread { > "Please use _beginthreadex instead of _beginthread" < } */ 1858 # endif /* !GC_NO_THREAD_REDIRECTS */ 1859 1860 #endif /* GC_WIN32_THREADS */ 1861 1862 /* Public setter and getter for switching "unmap as much as possible" */ 1863 /* mode on(1) and off(0). Has no effect unless unmapping is turned on. */ 1864 /* Has no effect on implicitly-initiated garbage collections. Initial */ 1865 /* value is controlled by GC_FORCE_UNMAP_ON_GCOLLECT. The setter and */ 1866 /* getter are unsynchronized. */ 1867 GC_API void GC_CALL GC_set_force_unmap_on_gcollect(int); 1868 GC_API int GC_CALL GC_get_force_unmap_on_gcollect(void); 1869 1870 /* Fully portable code should call GC_INIT() from the main program */ 1871 /* before making any other GC_ calls. On most platforms this is a */ 1872 /* no-op and the collector self-initializes. But a number of */ 1873 /* platforms make that too hard. */ 1874 /* A GC_INIT call is required if the collector is built with */ 1875 /* THREAD_LOCAL_ALLOC defined and the initial allocation call is not */ 1876 /* to GC_malloc() or GC_malloc_atomic(). */ 1877 1878 #if defined(__CYGWIN32__) || defined(__CYGWIN__) 1879 /* Similarly gnu-win32 DLLs need explicit initialization from the */ 1880 /* main program, as does AIX. */ 1881 # ifdef __x86_64__ 1882 /* Cygwin/x64 does not add leading underscore to symbols anymore. */ 1883 extern int __data_start__[], __data_end__[]; 1884 extern int __bss_start__[], __bss_end__[]; 1885 # define GC_DATASTART ((GC_word)__data_start__ < (GC_word)__bss_start__ \ 1886 ? (void *)__data_start__ : (void *)__bss_start__) 1887 # define GC_DATAEND ((GC_word)__data_end__ > (GC_word)__bss_end__ \ 1888 ? (void *)__data_end__ : (void *)__bss_end__) 1889 # else 1890 extern int _data_start__[], _data_end__[], _bss_start__[], _bss_end__[]; 1891 # define GC_DATASTART ((GC_word)_data_start__ < (GC_word)_bss_start__ \ 1892 ? (void *)_data_start__ : (void *)_bss_start__) 1893 # define GC_DATAEND ((GC_word)_data_end__ > (GC_word)_bss_end__ \ 1894 ? (void *)_data_end__ : (void *)_bss_end__) 1895 # endif /* !__x86_64__ */ 1896 # define GC_INIT_CONF_ROOTS GC_add_roots(GC_DATASTART, GC_DATAEND); \ 1897 GC_gcollect() /* For blacklisting. */ 1898 /* Required at least if GC is in a DLL. And doesn't hurt. */ 1899 #elif defined(_AIX) 1900 extern int _data[], _end[]; 1901 # define GC_DATASTART ((void *)_data) 1902 # define GC_DATAEND ((void *)_end) 1903 # define GC_INIT_CONF_ROOTS GC_add_roots(GC_DATASTART, GC_DATAEND) 1904 #elif (defined(HOST_ANDROID) || defined(__ANDROID__)) \ 1905 && defined(IGNORE_DYNAMIC_LOADING) 1906 /* This is ugly but seems the only way to register data roots of the */ 1907 /* client shared library if the GC dynamic loading support is off. */ 1908 # pragma weak __dso_handle 1909 extern int __dso_handle[]; 1910 GC_API void * GC_CALL GC_find_limit(void * /* start */, int /* up */); 1911 # define GC_INIT_CONF_ROOTS (void)(__dso_handle != 0 \ 1912 ? (GC_add_roots(__dso_handle, \ 1913 GC_find_limit(__dso_handle, \ 1914 1 /*up*/)), 0) : 0) 1915 #else 1916 # define GC_INIT_CONF_ROOTS /* empty */ 1917 #endif 1918 1919 #ifdef GC_DONT_EXPAND 1920 /* Set GC_dont_expand to TRUE at start-up */ 1921 # define GC_INIT_CONF_DONT_EXPAND GC_set_dont_expand(1) 1922 #else 1923 # define GC_INIT_CONF_DONT_EXPAND /* empty */ 1924 #endif 1925 1926 #ifdef GC_FORCE_UNMAP_ON_GCOLLECT 1927 /* Turn on "unmap as much as possible on explicit GC" mode at start-up */ 1928 # define GC_INIT_CONF_FORCE_UNMAP_ON_GCOLLECT \ 1929 GC_set_force_unmap_on_gcollect(1) 1930 #else 1931 # define GC_INIT_CONF_FORCE_UNMAP_ON_GCOLLECT /* empty */ 1932 #endif 1933 1934 #ifdef GC_DONT_GC 1935 /* This is for debugging only (useful if environment variables are */ 1936 /* unsupported); cannot call GC_disable as goes before GC_init. */ 1937 # define GC_INIT_CONF_MAX_RETRIES (void)(GC_dont_gc = 1) 1938 #elif defined(GC_MAX_RETRIES) && !defined(CPPCHECK) 1939 /* Set GC_max_retries to the desired value at start-up */ 1940 # define GC_INIT_CONF_MAX_RETRIES GC_set_max_retries(GC_MAX_RETRIES) 1941 #else 1942 # define GC_INIT_CONF_MAX_RETRIES /* empty */ 1943 #endif 1944 1945 #if defined(GC_FREE_SPACE_DIVISOR) && !defined(CPPCHECK) 1946 /* Set GC_free_space_divisor to the desired value at start-up */ 1947 # define GC_INIT_CONF_FREE_SPACE_DIVISOR \ 1948 GC_set_free_space_divisor(GC_FREE_SPACE_DIVISOR) 1949 #else 1950 # define GC_INIT_CONF_FREE_SPACE_DIVISOR /* empty */ 1951 #endif 1952 1953 #if defined(GC_FULL_FREQ) && !defined(CPPCHECK) 1954 /* Set GC_full_freq to the desired value at start-up */ 1955 # define GC_INIT_CONF_FULL_FREQ GC_set_full_freq(GC_FULL_FREQ) 1956 #else 1957 # define GC_INIT_CONF_FULL_FREQ /* empty */ 1958 #endif 1959 1960 #if defined(GC_TIME_LIMIT) && !defined(CPPCHECK) 1961 /* Set GC_time_limit to the desired value at start-up */ 1962 # define GC_INIT_CONF_TIME_LIMIT GC_set_time_limit(GC_TIME_LIMIT) 1963 #else 1964 # define GC_INIT_CONF_TIME_LIMIT /* empty */ 1965 #endif 1966 1967 #if defined(GC_SIG_SUSPEND) && defined(GC_THREADS) && !defined(CPPCHECK) 1968 # define GC_INIT_CONF_SUSPEND_SIGNAL GC_set_suspend_signal(GC_SIG_SUSPEND) 1969 #else 1970 # define GC_INIT_CONF_SUSPEND_SIGNAL /* empty */ 1971 #endif 1972 1973 #if defined(GC_SIG_THR_RESTART) && defined(GC_THREADS) && !defined(CPPCHECK) 1974 # define GC_INIT_CONF_THR_RESTART_SIGNAL \ 1975 GC_set_thr_restart_signal(GC_SIG_THR_RESTART) 1976 #else 1977 # define GC_INIT_CONF_THR_RESTART_SIGNAL /* empty */ 1978 #endif 1979 1980 #if defined(GC_MAXIMUM_HEAP_SIZE) && !defined(CPPCHECK) 1981 /* Limit the heap size to the desired value (useful for debugging). */ 1982 /* The limit could be overridden either at the program start-up by */ 1983 /* the similar environment variable or anytime later by the */ 1984 /* corresponding API function call. */ 1985 # define GC_INIT_CONF_MAXIMUM_HEAP_SIZE \ 1986 GC_set_max_heap_size(GC_MAXIMUM_HEAP_SIZE) 1987 #else 1988 # define GC_INIT_CONF_MAXIMUM_HEAP_SIZE /* empty */ 1989 #endif 1990 1991 #ifdef GC_IGNORE_WARN 1992 /* Turn off all warnings at start-up (after GC initialization) */ 1993 # define GC_INIT_CONF_IGNORE_WARN GC_set_warn_proc(GC_ignore_warn_proc) 1994 #else 1995 # define GC_INIT_CONF_IGNORE_WARN /* empty */ 1996 #endif 1997 1998 #if defined(GC_INITIAL_HEAP_SIZE) && !defined(CPPCHECK) 1999 /* Set heap size to the desired value at start-up */ 2000 # define GC_INIT_CONF_INITIAL_HEAP_SIZE \ 2001 { size_t heap_size = GC_get_heap_size(); \ 2002 if (heap_size < (GC_INITIAL_HEAP_SIZE)) \ 2003 (void)GC_expand_hp((GC_INITIAL_HEAP_SIZE) - heap_size); } 2004 #else 2005 # define GC_INIT_CONF_INITIAL_HEAP_SIZE /* empty */ 2006 #endif 2007 2008 /* Portable clients should call this at the program start-up. More */ 2009 /* over, some platforms require this call to be done strictly from the */ 2010 /* primordial thread. Multiple invocations are harmless. */ 2011 #define GC_INIT() { GC_INIT_CONF_DONT_EXPAND; /* pre-init */ \ 2012 GC_INIT_CONF_FORCE_UNMAP_ON_GCOLLECT; \ 2013 GC_INIT_CONF_MAX_RETRIES; \ 2014 GC_INIT_CONF_FREE_SPACE_DIVISOR; \ 2015 GC_INIT_CONF_FULL_FREQ; \ 2016 GC_INIT_CONF_TIME_LIMIT; \ 2017 GC_INIT_CONF_SUSPEND_SIGNAL; \ 2018 GC_INIT_CONF_THR_RESTART_SIGNAL; \ 2019 GC_INIT_CONF_MAXIMUM_HEAP_SIZE; \ 2020 GC_init(); /* real GC initialization */ \ 2021 GC_INIT_CONF_ROOTS; /* post-init */ \ 2022 GC_INIT_CONF_IGNORE_WARN; \ 2023 GC_INIT_CONF_INITIAL_HEAP_SIZE; } 2024 2025 /* win32S may not free all resources on process exit. */ 2026 /* This explicitly deallocates the heap. */ 2027 GC_API void GC_CALL GC_win32_free_heap(void); 2028 2029 #if defined(__SYMBIAN32__) 2030 void GC_init_global_static_roots(void); 2031 #endif 2032 2033 #if defined(_AMIGA) && !defined(GC_AMIGA_MAKINGLIB) 2034 /* Allocation really goes through GC_amiga_allocwrapper_do. */ 2035 void *GC_amiga_realloc(void *, size_t); 2036 # define GC_realloc(a,b) GC_amiga_realloc(a,b) 2037 void GC_amiga_set_toany(void (*)(void)); 2038 extern int GC_amiga_free_space_divisor_inc; 2039 extern void *(*GC_amiga_allocwrapper_do)(size_t, void *(GC_CALL *)(size_t)); 2040 # define GC_malloc(a) \ 2041 (*GC_amiga_allocwrapper_do)(a,GC_malloc) 2042 # define GC_malloc_atomic(a) \ 2043 (*GC_amiga_allocwrapper_do)(a,GC_malloc_atomic) 2044 # define GC_malloc_uncollectable(a) \ 2045 (*GC_amiga_allocwrapper_do)(a,GC_malloc_uncollectable) 2046 # define GC_malloc_atomic_uncollectable(a) \ 2047 (*GC_amiga_allocwrapper_do)(a,GC_malloc_atomic_uncollectable) 2048 # define GC_malloc_ignore_off_page(a) \ 2049 (*GC_amiga_allocwrapper_do)(a,GC_malloc_ignore_off_page) 2050 # define GC_malloc_atomic_ignore_off_page(a) \ 2051 (*GC_amiga_allocwrapper_do)(a,GC_malloc_atomic_ignore_off_page) 2052 #endif /* _AMIGA && !GC_AMIGA_MAKINGLIB */ 2053 2054 #ifdef __cplusplus 2055 } /* extern "C" */ 2056 #endif 2057 2058 #endif /* GC_H */ 2059