Debian Codegraph @

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