Debian Codegraph @

external/bdwgc/include/gc.h

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