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
8 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
11 * Permission is hereby granted to use or copy this program
12 * for any purpose, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
19 * Note that this defines a large number of tuning hooks, which can
20 * safely be ignored in nearly all cases. For normal use it suffices
21 * to call only GC_MALLOC and perhaps GC_REALLOC.
22 * For better performance, also look at GC_MALLOC_ATOMIC, and
23 * GC_enable_incremental. If you need an action to be performed
24 * immediately before an object is collected, look at GC_register_finalizer.
25 * If you are using Solaris threads, look at the end of this file.
26 * Everything else is best ignored unless you encounter performance
34 # include "gc_version.h"
35 /* Define version numbers here to allow test on build machine */
36 /* for cross-builds. Note that this defines the header */
37 /* version number, which may or may not match that of the */
38 /* dynamic library. The GC_version variable can be used */
39 /* to obtain the latter. */
41 # include "gc_config_macros.h"
48 /* Define word and signed_word to be unsigned and signed types of the */
49 /* size as char * or void *. There seems to be no way to do this */
50 /* even semi-portably. The following is probably no better/worse */
51 /* than almost anything else. */
52 /* The ANSI standard suggests that size_t and ptrdiff_t might be */
53 /* better choices. But those had incorrect definitions on some older */
54 /* systems. Notably "typedef int size_t" is WRONG. */
56 typedef unsigned long GC_word;
57 typedef long GC_signed_word;
60 typedef unsigned __int64 GC_word;
61 typedef __int64 GC_signed_word;
63 typedef unsigned long long GC_word;
64 typedef long long GC_signed_word;
68 /* Public read-only variables */
69 /* Getter procedures are supplied in some cases and preferred for new */
72 GC_API GC_word GC_gc_no;/* Counter incremented per collection. */
73 /* Includes empty GCs at startup. */
74 GC_API GC_word GC_CALL GC_get_gc_no(void);
76 GC_API int GC_parallel; /* GC is parallelized for performance on */
77 /* multiprocessors. Currently set only */
78 /* implicitly if collector is built with */
79 /* -DPARALLEL_MARK and if either: */
80 /* Env variable GC_NPROC is set to > 1, or */
81 /* GC_NPROC is not set and this is an MP. */
82 /* If GC_parallel is set, incremental */
83 /* collection is only partially functional, */
84 /* and may not be desirable. */
85 GC_API int GC_CALL GC_get_parallel(void);
88 /* Public R/W variables */
90 typedef void * (GC_CALLBACK * GC_oom_func)(size_t /* bytes_requested */);
91 GC_API GC_oom_func GC_oom_fn;
92 /* When there is insufficient memory to satisfy */
93 /* an allocation request, we return */
94 /* (*GC_oom_fn)(). By default this just */
96 /* If it returns, it must return 0 or a valid */
97 /* pointer to a previously allocated heap */
99 GC_API void GC_CALL GC_set_oom_fn(GC_oom_func);
100 GC_API GC_oom_func GC_CALL GC_get_oom_fn(void);
102 GC_API int GC_find_leak;
103 /* Do not actually garbage collect, but simply */
104 /* report inaccessible memory that was not */
105 /* deallocated with GC_free. Initial value */
106 /* is determined by FIND_LEAK macro. */
107 GC_API void GC_CALL GC_set_find_leak(int);
108 GC_API int GC_CALL GC_get_find_leak(void);
110 GC_API int GC_all_interior_pointers;
111 /* Arrange for pointers to object interiors to */
112 /* be recognized as valid. May not be changed */
113 /* after GC initialization. */
114 /* Initial value is determined by */
115 /* -DALL_INTERIOR_POINTERS. */
116 /* Unless DONT_ADD_BYTE_AT_END is defined, this */
117 /* also affects whether sizes are increased by */
118 /* at least a byte to allow "off the end" */
119 /* pointer recognition. */
120 /* MUST BE 0 or 1. */
121 GC_API void GC_CALL GC_set_all_interior_pointers(int);
122 GC_API int GC_CALL GC_get_all_interior_pointers(void);
124 GC_API int GC_finalize_on_demand;
125 /* If nonzero, finalizers will only be run in */
126 /* response to an explicit GC_invoke_finalizers */
127 /* call. The default is determined by whether */
128 /* the FINALIZE_ON_DEMAND macro is defined */
129 /* when the collector is built. */
130 GC_API void GC_CALL GC_set_finalize_on_demand(int);
131 GC_API int GC_CALL GC_get_finalize_on_demand(void);
133 GC_API int GC_java_finalization;
134 /* Mark objects reachable from finalizable */
135 /* objects in a separate post-pass. This makes */
136 /* it a bit safer to use non-topologically- */
137 /* ordered finalization. Default value is */
138 /* determined by JAVA_FINALIZATION macro. */
139 /* Enables register_finalizer_unreachable to */
140 /* work correctly. */
141 GC_API void GC_CALL GC_set_java_finalization(int);
142 GC_API int GC_CALL GC_get_java_finalization(void);
144 typedef void (GC_CALLBACK * GC_finalizer_notifier_proc)(void);
145 GC_API GC_finalizer_notifier_proc GC_finalizer_notifier;
146 /* Invoked by the collector when there are */
147 /* objects to be finalized. Invoked at most */
148 /* once per GC cycle. Never invoked unless */
149 /* GC_finalize_on_demand is set. */
150 /* Typically this will notify a finalization */
151 /* thread, which will call GC_invoke_finalizers */
153 GC_API void GC_CALL GC_set_finalizer_notifier(GC_finalizer_notifier_proc);
154 GC_API GC_finalizer_notifier_proc GC_CALL GC_get_finalizer_notifier(void);
156 GC_API int GC_dont_gc; /* != 0 ==> Dont collect. In versions 6.2a1+, */
157 /* this overrides explicit GC_gcollect() calls. */
158 /* Used as a counter, so that nested enabling */
159 /* and disabling work correctly. Should */
160 /* normally be updated with GC_enable() and */
161 /* GC_disable() calls. */
162 /* Direct assignment to GC_dont_gc is */
165 GC_API int GC_dont_expand;
166 /* Dont expand heap unless explicitly requested */
168 GC_API void GC_CALL GC_set_dont_expand(int);
169 GC_API int GC_CALL GC_get_dont_expand(void);
171 GC_API int GC_use_entire_heap;
172 /* Causes the non-incremental collector to use the */
173 /* entire heap before collecting. This was the only */
174 /* option for GC versions < 5.0. This sometimes */
175 /* results in more large block fragmentation, since */
176 /* very large blocks will tend to get broken up */
177 /* during each GC cycle. It is likely to result in a */
178 /* larger working set, but lower collection */
179 /* frequencies, and hence fewer instructions executed */
180 /* in the collector. */
182 GC_API int GC_full_freq; /* Number of partial collections between */
183 /* full collections. Matters only if */
184 /* GC_incremental is set. */
185 /* Full collections are also triggered if */
186 /* the collector detects a substantial */
187 /* increase in the number of in-use heap */
188 /* blocks. Values in the tens are now */
189 /* perfectly reasonable, unlike for */
190 /* earlier GC versions. */
191 GC_API void GC_CALL GC_set_full_freq(int);
192 GC_API int GC_CALL GC_get_full_freq(void);
194 GC_API GC_word GC_non_gc_bytes;
195 /* Bytes not considered candidates for collection. */
196 /* Used only to control scheduling of collections. */
197 /* Updated by GC_malloc_uncollectable and GC_free. */
199 GC_API void GC_CALL GC_set_non_gc_bytes(GC_word);
200 GC_API GC_word GC_CALL GC_get_non_gc_bytes(void);
202 GC_API int GC_no_dls;
203 /* Don't register dynamic library data segments. */
204 /* Wizards only. Should be used only if the */
205 /* application explicitly registers all roots. */
206 /* In Microsoft Windows environments, this will */
207 /* usually also prevent registration of the */
208 /* main data segment as part of the root set. */
209 GC_API void GC_CALL GC_set_no_dls(int);
210 GC_API int GC_CALL GC_get_no_dls(void);
212 GC_API GC_word GC_free_space_divisor;
213 /* We try to make sure that we allocate at */
214 /* least N/GC_free_space_divisor bytes between */
215 /* collections, where N is twice the number */
216 /* of traced bytes, plus the number of untraced */
217 /* bytes (bytes in "atomic" objects), plus */
218 /* a rough estimate of the root set size. */
219 /* N approximates GC tracing work per GC. */
220 /* Initially, GC_free_space_divisor = 3. */
221 /* Increasing its value will use less space */
222 /* but more collection time. Decreasing it */
223 /* will appreciably decrease collection time */
224 /* at the expense of space. */
225 GC_API void GC_CALL GC_set_free_space_divisor(GC_word);
226 GC_API GC_word GC_CALL GC_get_free_space_divisor(void);
228 GC_API GC_word GC_max_retries;
229 /* The maximum number of GCs attempted before */
230 /* reporting out of memory after heap */
231 /* expansion fails. Initially 0. */
232 GC_API void GC_CALL GC_set_max_retries(GC_word);
233 GC_API GC_word GC_CALL GC_get_max_retries(void);
236 GC_API char *GC_stackbottom; /* Cool end of user stack. */
237 /* May be set in the client prior to */
238 /* calling any GC_ routines. This */
239 /* avoids some overhead, and */
240 /* potentially some signals that can */
241 /* confuse debuggers. Otherwise the */
242 /* collector attempts to set it */
244 /* For multithreaded code, this is the */
245 /* cold end of the stack for the */
246 /* primordial thread. */
248 GC_API int GC_dont_precollect; /* Don't collect as part of */
249 /* initialization. Should be set only */
250 /* if the client wants a chance to */
251 /* manually initialize the root set */
252 /* before the first collection. */
253 /* Interferes with blacklisting. */
255 GC_API void GC_CALL GC_set_dont_precollect(int);
256 GC_API int GC_CALL GC_get_dont_precollect(void);
258 GC_API unsigned long GC_time_limit;
259 /* If incremental collection is enabled, */
260 /* We try to terminate collections */
261 /* after this many milliseconds. Not a */
262 /* hard time bound. Setting this to */
263 /* GC_TIME_UNLIMITED will essentially */
264 /* disable incremental collection while */
265 /* leaving generational collection */
267 # define GC_TIME_UNLIMITED 999999
268 /* Setting GC_time_limit to this value */
269 /* will disable the "pause time exceeded"*/
271 GC_API void GC_CALL GC_set_time_limit(unsigned long);
272 GC_API unsigned long GC_CALL GC_get_time_limit(void);
274 /* Public procedures */
276 /* Initialize the collector. Portable clients should call GC_INIT() from
277 * the main program instead.
279 GC_API void GC_CALL GC_init(void);
282 * general purpose allocation routines, with roughly malloc calling conv.
283 * The atomic versions promise that no relevant pointers are contained
284 * in the object. The non-atomic versions guarantee that the new object
285 * is cleared. GC_malloc_stubborn promises that no changes to the object
286 * will occur after GC_end_stubborn_change has been called on the
287 * result of GC_malloc_stubborn. GC_malloc_uncollectable allocates an object
288 * that is scanned for pointers to collectable objects, but is not itself
289 * collectable. The object is scanned even if it does not appear to
290 * be reachable. GC_malloc_uncollectable and GC_free called on the resulting
291 * object implicitly update GC_non_gc_bytes appropriately.
293 * Note that the GC_malloc_stubborn support doesn't really
294 * exist anymore. MANUAL_VDB provides comparable functionality.
296 GC_API void * GC_CALL GC_malloc(size_t size_in_bytes);
297 GC_API void * GC_CALL GC_malloc_atomic(size_t size_in_bytes);
298 GC_API char * GC_CALL GC_strdup (const char *str);
299 GC_API void * GC_CALL GC_malloc_uncollectable(size_t size_in_bytes);
300 GC_API void * GC_CALL GC_malloc_stubborn(size_t size_in_bytes);
302 /* GC_memalign() is not well tested. */
303 GC_API void * GC_CALL GC_memalign(size_t align, size_t lb);
305 /* The following is only defined if the library has been suitably */
307 GC_API void * GC_CALL GC_malloc_atomic_uncollectable(size_t size_in_bytes);
309 /* Explicitly deallocate an object. Dangerous if used incorrectly. */
310 /* Requires a pointer to the base of an object. */
311 /* If the argument is stubborn, it should not be changeable when freed. */
312 /* An object should not be enabled for finalization when it is */
313 /* explicitly deallocated. */
314 /* GC_free(0) is a no-op, as required by ANSI C for free. */
315 GC_API void GC_CALL GC_free(void * object_addr);
318 * Stubborn objects may be changed only if the collector is explicitly informed.
319 * The collector is implicitly informed of coming change when such
320 * an object is first allocated. The following routines inform the
321 * collector that an object will no longer be changed, or that it will
322 * once again be changed. Only non-NULL pointer stores into the object
323 * are considered to be changes. The argument to GC_end_stubborn_change
324 * must be exactly the value returned by GC_malloc_stubborn or passed to
325 * GC_change_stubborn. (In the second case it may be an interior pointer
326 * within 512 bytes of the beginning of the objects.)
327 * There is a performance penalty for allowing more than
328 * one stubborn object to be changed at once, but it is acceptable to
329 * do so. The same applies to dropping stubborn objects that are still
332 GC_API void GC_CALL GC_change_stubborn(void *);
333 GC_API void GC_CALL GC_end_stubborn_change(void *);
335 /* Return a pointer to the base (lowest address) of an object given */
336 /* a pointer to a location within the object. */
337 /* I.e. map an interior pointer to the corresponding bas pointer. */
338 /* Note that with debugging allocation, this returns a pointer to the */
339 /* actual base of the object, i.e. the debug information, not to */
340 /* the base of the user object. */
341 /* Return 0 if displaced_pointer doesn't point to within a valid */
343 /* Note that a deallocated object in the garbage collected heap */
344 /* may be considered valid, even if it has been deallocated with */
346 GC_API void * GC_CALL GC_base(void * displaced_pointer);
348 /* Given a pointer to the base of an object, return its size in bytes. */
349 /* The returned size may be slightly larger than what was originally */
351 GC_API size_t GC_CALL GC_size(void * object_addr);
353 /* For compatibility with C library. This is occasionally faster than */
354 /* a malloc followed by a bcopy. But if you rely on that, either here */
355 /* or with the standard C library, your code is broken. In my */
356 /* opinion, it shouldn't have been invented, but now we're stuck. -HB */
357 /* The resulting object has the same kind as the original. */
358 /* If the argument is stubborn, the result will have changes enabled. */
359 /* It is an error to have changes enabled for the original object. */
360 /* Follows ANSI conventions for NULL old_object. */
361 GC_API void * GC_CALL GC_realloc(void * old_object, size_t new_size_in_bytes);
363 /* Explicitly increase the heap size. */
364 /* Returns 0 on failure, 1 on success. */
365 GC_API int GC_CALL GC_expand_hp(size_t number_of_bytes);
367 /* Limit the heap size to n bytes. Useful when you're debugging, */
368 /* especially on systems that don't handle running out of memory well. */
369 /* n == 0 ==> unbounded. This is the default. */
370 GC_API void GC_CALL GC_set_max_heap_size(GC_word n);
372 /* Inform the collector that a certain section of statically allocated */
373 /* memory contains no pointers to garbage collected memory. Thus it */
374 /* need not be scanned. This is sometimes important if the application */
375 /* maps large read/write files into the address space, which could be */
376 /* mistaken for dynamic library data segments on some systems. */
377 GC_API void GC_CALL GC_exclude_static_roots(void * low_address,
378 void * high_address_plus_1);
380 /* Clear the set of root segments. Wizards only. */
381 GC_API void GC_CALL GC_clear_roots(void);
383 /* Add a root segment. Wizards only. */
384 GC_API void GC_CALL GC_add_roots(void * low_address,
385 void * high_address_plus_1);
387 /* Remove a root segment. Wizards only. */
388 GC_API void GC_CALL GC_remove_roots(void * low_address,
389 void * high_address_plus_1);
391 /* Add a displacement to the set of those considered valid by the */
392 /* collector. GC_register_displacement(n) means that if p was returned */
393 /* by GC_malloc, then (char *)p + n will be considered to be a valid */
394 /* pointer to p. N must be small and less than the size of p. */
395 /* (All pointers to the interior of objects from the stack are */
396 /* considered valid in any case. This applies to heap objects and */
398 /* Preferably, this should be called before any other GC procedures. */
399 /* Calling it later adds to the probability of excess memory */
401 /* This is a no-op if the collector has recognition of */
402 /* arbitrary interior pointers enabled, which is now the default. */
403 GC_API void GC_CALL GC_register_displacement(size_t n);
405 /* The following version should be used if any debugging allocation is */
407 GC_API void GC_CALL GC_debug_register_displacement(size_t n);
409 /* Explicitly trigger a full, world-stop collection. */
410 GC_API void GC_CALL GC_gcollect(void);
412 /* Trigger a full world-stopped collection. Abort the collection if */
413 /* and when stop_func returns a nonzero value. Stop_func will be */
414 /* called frequently, and should be reasonably fast. This works even */
415 /* if virtual dirty bits, and hence incremental collection is not */
416 /* available for this architecture. Collections can be aborted faster */
417 /* than normal pause times for incremental collection. However, */
418 /* aborted collections do no useful work; the next collection needs */
419 /* to start from the beginning. */
420 /* Return 0 if the collection was aborted, 1 if it succeeded. */
421 typedef int (GC_CALLBACK * GC_stop_func)(void);
422 GC_API int GC_CALL GC_try_to_collect(GC_stop_func stop_func);
424 /* Return the number of bytes in the heap. Excludes collector private */
425 /* data structures. Excludes the unmapped memory (retuned to the OS). */
426 /* Includes empty blocks and fragmentation loss. Includes some pages */
427 /* that were allocated but never written. */
428 GC_API size_t GC_CALL GC_get_heap_size(void);
430 /* Return a lower bound on the number of free bytes in the heap */
431 /* (excluding the unmapped memory space). */
432 GC_API size_t GC_CALL GC_get_free_bytes(void);
434 /* Return the size (in bytes) of the unmapped memory (which is returned */
435 /* to the OS but could be remapped back by the collector later unless */
436 /* the OS runs out of system/virtual memory). */
437 GC_API size_t GC_CALL GC_get_unmapped_bytes(void);
439 /* Return the number of bytes allocated since the last collection. */
440 GC_API size_t GC_CALL GC_get_bytes_since_gc(void);
442 /* Return the total number of bytes allocated in this process. */
443 /* Never decreases, except due to wrapping. */
444 GC_API size_t GC_CALL GC_get_total_bytes(void);
446 /* Disable garbage collection. Even GC_gcollect calls will be */
448 GC_API void GC_CALL GC_disable(void);
450 /* Re-enable garbage collection. GC_disable() and GC_enable() calls */
451 /* nest. Garbage collection is enabled if the number of calls to both */
452 /* both functions is equal. */
453 GC_API void GC_CALL GC_enable(void);
455 /* Enable incremental/generational collection. */
456 /* Not advisable unless dirty bits are */
457 /* available or most heap objects are */
458 /* pointer-free (atomic) or immutable. */
459 /* Don't use in leak finding mode. */
460 /* Ignored if GC_dont_gc is true. */
461 /* Only the generational piece of this is */
462 /* functional if GC_parallel is TRUE */
463 /* or if GC_time_limit is GC_TIME_UNLIMITED. */
464 /* Causes thread-local variant of GC_gcj_malloc() to revert to */
465 /* locked allocation. Must be called before any such */
466 /* GC_gcj_malloc() calls. */
467 /* For best performance, should be called as early as possible. */
468 /* On some platforms, calling it later may have adverse effects.*/
469 /* Safe to call before GC_INIT(). Includes a GC_init() call. */
470 GC_API void GC_CALL GC_enable_incremental(void);
472 /* Does incremental mode write-protect pages? Returns zero or */
473 /* more of the following, or'ed together: */
474 #define GC_PROTECTS_POINTER_HEAP 1 /* May protect non-atomic objs. */
475 #define GC_PROTECTS_PTRFREE_HEAP 2
476 #define GC_PROTECTS_STATIC_DATA 4 /* Currently never. */
477 #define GC_PROTECTS_STACK 8 /* Probably impractical. */
479 #define GC_PROTECTS_NONE 0
480 GC_API int GC_CALL GC_incremental_protection_needs(void);
482 /* Perform some garbage collection work, if appropriate. */
483 /* Return 0 if there is no more work to be done. */
484 /* Typically performs an amount of work corresponding roughly */
485 /* to marking from one page. May do more work if further */
486 /* progress requires it, e.g. if incremental collection is */
487 /* disabled. It is reasonable to call this in a wait loop */
488 /* until it returns 0. */
489 GC_API int GC_CALL GC_collect_a_little(void);
491 /* Allocate an object of size lb bytes. The client guarantees that */
492 /* as long as the object is live, it will be referenced by a pointer */
493 /* that points to somewhere within the first 256 bytes of the object. */
494 /* (This should normally be declared volatile to prevent the compiler */
495 /* from invalidating this assertion.) This routine is only useful */
496 /* if a large array is being allocated. It reduces the chance of */
497 /* accidentally retaining such an array as a result of scanning an */
498 /* integer that happens to be an address inside the array. (Actually, */
499 /* it reduces the chance of the allocator not finding space for such */
500 /* an array, since it will try hard to avoid introducing such a false */
501 /* reference.) On a SunOS 4.X or MS Windows system this is recommended */
502 /* for arrays likely to be larger than 100K or so. For other systems, */
503 /* or if the collector is not configured to recognize all interior */
504 /* pointers, the threshold is normally much higher. */
505 GC_API void * GC_CALL GC_malloc_ignore_off_page(size_t lb);
506 GC_API void * GC_CALL GC_malloc_atomic_ignore_off_page(size_t lb);
508 #if defined(__sgi) && !defined(__GNUC__) && _COMPILER_VERSION >= 720
509 # define GC_ADD_CALLER
510 # define GC_RETURN_ADDR (GC_word)__return_address
513 #if defined(__linux__) || defined(__GLIBC__)
514 # include <features.h>
515 # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1 || __GLIBC__ > 2) \
516 && !defined(__ia64__) && !defined(__UCLIBC__)
517 # ifndef GC_HAVE_BUILTIN_BACKTRACE
518 # define GC_HAVE_BUILTIN_BACKTRACE
521 # if defined(__i386__) || defined(__x86_64__)
522 # define GC_CAN_SAVE_CALL_STACKS
526 #if defined(_MSC_VER) && _MSC_VER >= 1200 /* version 12.0+ (MSVC 6.0+) */ \
528 # ifndef GC_HAVE_NO_BUILTIN_BACKTRACE
529 # define GC_HAVE_BUILTIN_BACKTRACE
533 #if defined(GC_HAVE_BUILTIN_BACKTRACE) && !defined(GC_CAN_SAVE_CALL_STACKS)
534 # define GC_CAN_SAVE_CALL_STACKS
537 #if defined(__sparc__)
538 # define GC_CAN_SAVE_CALL_STACKS
541 /* If we're on an a platform on which we can't save call stacks, but */
542 /* gcc is normally used, we go ahead and define GC_ADD_CALLER. */
543 /* We make this decision independent of whether gcc is actually being */
544 /* used, in order to keep the interface consistent, and allow mixing */
546 /* This may also be desirable if it is possible but expensive to */
547 /* retrieve the call chain. */
548 #if (defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__) \
549 || defined(__FreeBSD__) || defined(__DragonFly__)) & !defined(GC_CAN_SAVE_CALL_STACKS)
550 # define GC_ADD_CALLER
551 # if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
552 /* gcc knows how to retrieve return address, but we don't know */
553 /* how to generate call stacks. */
554 # define GC_RETURN_ADDR (GC_word)__builtin_return_address(0)
556 /* Just pass 0 for gcc compatibility. */
557 # define GC_RETURN_ADDR 0
562 # define GC_EXTRAS GC_RETURN_ADDR, __FILE__, __LINE__
563 # define GC_EXTRA_PARAMS GC_word ra, const char * s, int i
565 # define GC_EXTRAS __FILE__, __LINE__
566 # define GC_EXTRA_PARAMS const char * s, int i
569 /* Debugging (annotated) allocation. GC_gcollect will check */
570 /* objects allocated in this way for overwrites, etc. */
571 GC_API void * GC_CALL GC_debug_malloc(size_t size_in_bytes, GC_EXTRA_PARAMS);
572 GC_API void * GC_CALL GC_debug_malloc_atomic
573 (size_t size_in_bytes, GC_EXTRA_PARAMS);
574 GC_API char * GC_CALL GC_debug_strdup(const char *str, GC_EXTRA_PARAMS);
575 GC_API void * GC_CALL GC_debug_malloc_uncollectable
576 (size_t size_in_bytes, GC_EXTRA_PARAMS);
577 GC_API void * GC_CALL GC_debug_malloc_stubborn
578 (size_t size_in_bytes, GC_EXTRA_PARAMS);
579 GC_API void * GC_CALL GC_debug_malloc_ignore_off_page
580 (size_t size_in_bytes, GC_EXTRA_PARAMS);
581 GC_API void * GC_CALL GC_debug_malloc_atomic_ignore_off_page
582 (size_t size_in_bytes, GC_EXTRA_PARAMS);
583 GC_API void GC_CALL GC_debug_free (void * object_addr);
584 GC_API void * GC_CALL GC_debug_realloc
585 (void * old_object, size_t new_size_in_bytes, GC_EXTRA_PARAMS);
586 GC_API void GC_CALL GC_debug_change_stubborn(void *);
587 GC_API void GC_CALL GC_debug_end_stubborn_change(void *);
589 /* Routines that allocate objects with debug information (like the */
590 /* above), but just fill in dummy file and line number information. */
591 /* Thus they can serve as drop-in malloc/realloc replacements. This */
592 /* can be useful for two reasons: */
593 /* 1) It allows the collector to be built with DBG_HDRS_ALL defined */
594 /* even if some allocation calls come from 3rd party libraries */
595 /* that can't be recompiled. */
596 /* 2) On some platforms, the file and line information is redundant, */
597 /* since it can be reconstructed from a stack trace. On such */
598 /* platforms it may be more convenient not to recompile, e.g. for */
599 /* leak detection. This can be accomplished by instructing the */
600 /* linker to replace malloc/realloc with these. */
601 GC_API void * GC_CALL GC_debug_malloc_replacement (size_t size_in_bytes);
602 GC_API void * GC_CALL GC_debug_realloc_replacement
603 (void * object_addr, size_t size_in_bytes);
606 # define GC_MALLOC(sz) GC_debug_malloc(sz, GC_EXTRAS)
607 # define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, GC_EXTRAS)
608 # define GC_STRDUP(s) GC_debug_strdup((s), GC_EXTRAS)
609 # define GC_MALLOC_UNCOLLECTABLE(sz) \
610 GC_debug_malloc_uncollectable(sz, GC_EXTRAS)
611 # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
612 GC_debug_malloc_ignore_off_page(sz, GC_EXTRAS)
613 # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
614 GC_debug_malloc_atomic_ignore_off_page(sz, GC_EXTRAS)
615 # define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, GC_EXTRAS)
616 # define GC_FREE(p) GC_debug_free(p)
617 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
618 GC_debug_register_finalizer(p, f, d, of, od)
619 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
620 GC_debug_register_finalizer_ignore_self(p, f, d, of, od)
621 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
622 GC_debug_register_finalizer_no_order(p, f, d, of, od)
623 # define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \
624 GC_debug_register_finalizer_unreachable(p, f, d, of, od)
625 # define GC_MALLOC_STUBBORN(sz) GC_debug_malloc_stubborn(sz, GC_EXTRAS);
626 # define GC_CHANGE_STUBBORN(p) GC_debug_change_stubborn(p)
627 # define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p)
628 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
629 GC_general_register_disappearing_link(link, GC_base(obj))
630 # define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n)
632 # define GC_MALLOC(sz) GC_malloc(sz)
633 # define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz)
634 # define GC_STRDUP(s) GC_strdup(s)
635 # define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz)
636 # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
637 GC_malloc_ignore_off_page(sz)
638 # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
639 GC_malloc_atomic_ignore_off_page(sz)
640 # define GC_REALLOC(old, sz) GC_realloc(old, sz)
641 # define GC_FREE(p) GC_free(p)
642 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
643 GC_register_finalizer(p, f, d, of, od)
644 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
645 GC_register_finalizer_ignore_self(p, f, d, of, od)
646 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
647 GC_register_finalizer_no_order(p, f, d, of, od)
648 # define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \
649 GC_register_finalizer_unreachable(p, f, d, of, od)
650 # define GC_MALLOC_STUBBORN(sz) GC_malloc_stubborn(sz)
651 # define GC_CHANGE_STUBBORN(p) GC_change_stubborn(p)
652 # define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p)
653 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
654 GC_general_register_disappearing_link(link, obj)
655 # define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n)
657 /* The following are included because they are often convenient, and */
658 /* reduce the chance for a misspecified size argument. But calls may */
659 /* expand to something syntactically incorrect if t is a complicated */
660 /* type expression. */
661 # define GC_NEW(t) (t *)GC_MALLOC(sizeof (t))
662 # define GC_NEW_ATOMIC(t) (t *)GC_MALLOC_ATOMIC(sizeof (t))
663 # define GC_NEW_STUBBORN(t) (t *)GC_MALLOC_STUBBORN(sizeof (t))
664 # define GC_NEW_UNCOLLECTABLE(t) (t *)GC_MALLOC_UNCOLLECTABLE(sizeof (t))
666 /* Finalization. Some of these primitives are grossly unsafe. */
667 /* The idea is to make them both cheap, and sufficient to build */
668 /* a safer layer, closer to Modula-3, Java, or PCedar finalization. */
669 /* The interface represents my conclusions from a long discussion */
670 /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes, */
671 /* Christian Jacobi, and Russ Atkinson. It's not perfect, and */
672 /* probably nobody else agrees with it. Hans-J. Boehm 3/13/92 */
673 typedef void (GC_CALLBACK * GC_finalization_proc) (void * obj,
676 GC_API void GC_CALL GC_register_finalizer(void * obj, GC_finalization_proc fn,
677 void * cd, GC_finalization_proc *ofn,
679 GC_API void GC_CALL GC_debug_register_finalizer
680 (void * obj, GC_finalization_proc fn, void * cd,
681 GC_finalization_proc *ofn, void * *ocd);
682 /* When obj is no longer accessible, invoke */
683 /* (*fn)(obj, cd). If a and b are inaccessible, and */
684 /* a points to b (after disappearing links have been */
685 /* made to disappear), then only a will be */
686 /* finalized. (If this does not create any new */
687 /* pointers to b, then b will be finalized after the */
688 /* next collection.) Any finalizable object that */
689 /* is reachable from itself by following one or more */
690 /* pointers will not be finalized (or collected). */
691 /* Thus cycles involving finalizable objects should */
692 /* be avoided, or broken by disappearing links. */
693 /* All but the last finalizer registered for an object */
695 /* Finalization may be removed by passing 0 as fn. */
696 /* Finalizers are implicitly unregistered when they are */
697 /* enqueued for finalization (i.e. become ready to be */
699 /* The old finalizer and client data are stored in */
700 /* *ofn and *ocd. (ofn and/or ocd may be NULL. */
701 /* The allocation lock is held while *ofn and *ocd are */
702 /* updated. In case of error (no memory to register */
703 /* new finalizer), *ofn and *ocd remain unchanged.) */
704 /* Fn is never invoked on an accessible object, */
705 /* provided hidden pointers are converted to real */
706 /* pointers only if the allocation lock is held, and */
707 /* such conversions are not performed by finalization */
709 /* If GC_register_finalizer is aborted as a result of */
710 /* a signal, the object may be left with no */
711 /* finalization, even if neither the old nor new */
712 /* finalizer were NULL. */
713 /* Obj should be the starting address of an object */
714 /* allocated by GC_malloc or friends. Obj may also be */
715 /* NULL or point to something outside GC heap (in this */
716 /* case, fn is ignored, *ofn and *ocd are set to NULL). */
717 /* Note that any garbage collectable object referenced */
718 /* by cd will be considered accessible until the */
719 /* finalizer is invoked. */
721 /* Another versions of the above follow. It ignores */
722 /* self-cycles, i.e. pointers from a finalizable object to */
723 /* itself. There is a stylistic argument that this is wrong, */
724 /* but it's unavoidable for C++, since the compiler may */
725 /* silently introduce these. It's also benign in that specific */
726 /* case. And it helps if finalizable objects are split to */
728 /* Note that cd will still be viewed as accessible, even if it */
729 /* refers to the object itself. */
730 GC_API void GC_CALL GC_register_finalizer_ignore_self
731 (void * obj, GC_finalization_proc fn, void * cd,
732 GC_finalization_proc *ofn, void * *ocd);
733 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self
734 (void * obj, GC_finalization_proc fn, void * cd,
735 GC_finalization_proc *ofn, void * *ocd);
737 /* Another version of the above. It ignores all cycles. */
738 /* It should probably only be used by Java implementations. */
739 /* Note that cd will still be viewed as accessible, even if it */
740 /* refers to the object itself. */
741 GC_API void GC_CALL GC_register_finalizer_no_order
742 (void * obj, GC_finalization_proc fn, void * cd,
743 GC_finalization_proc *ofn, void * *ocd);
744 GC_API void GC_CALL GC_debug_register_finalizer_no_order
745 (void * obj, GC_finalization_proc fn, void * cd,
746 GC_finalization_proc *ofn, void * *ocd);
748 /* This is a special finalizer that is useful when an object's */
749 /* finalizer must be run when the object is known to be no */
750 /* longer reachable, not even from other finalizable objects. */
751 /* It behaves like "normal" finalization, except that the */
752 /* finalizer is not run while the object is reachable from */
753 /* other objects specifying unordered finalization. */
754 /* Effectively it allows an object referenced, possibly */
755 /* indirectly, from an unordered finalizable object to override */
756 /* the unordered finalization request. */
757 /* This can be used in combination with finalizer_no_order so */
758 /* as to release resources that must not be released while an */
759 /* object can still be brought back to life by other */
761 /* Only works if GC_java_finalization is set. Probably only */
762 /* of interest when implementing a language that requires */
763 /* unordered finalization (e.g. Java, C#). */
764 GC_API void GC_CALL GC_register_finalizer_unreachable
765 (void * obj, GC_finalization_proc fn, void * cd,
766 GC_finalization_proc *ofn, void * *ocd);
767 GC_API void GC_CALL GC_debug_register_finalizer_unreachable
768 (void * obj, GC_finalization_proc fn, void * cd,
769 GC_finalization_proc *ofn, void * *ocd);
771 /* The following routine may be used to break cycles between */
772 /* finalizable objects, thus causing cyclic finalizable */
773 /* objects to be finalized in the correct order. Standard */
774 /* use involves calling GC_register_disappearing_link(&p), */
775 /* where p is a pointer that is not followed by finalization */
776 /* code, and should not be considered in determining */
777 /* finalization order. */
778 GC_API int GC_CALL GC_register_disappearing_link(void * * link );
779 /* Link should point to a field of a heap allocated */
780 /* object obj. *link will be cleared when obj is */
781 /* found to be inaccessible. This happens BEFORE any */
782 /* finalization code is invoked, and BEFORE any */
783 /* decisions about finalization order are made. */
784 /* This is useful in telling the finalizer that */
785 /* some pointers are not essential for proper */
786 /* finalization. This may avoid finalization cycles. */
787 /* Note that obj may be resurrected by another */
788 /* finalizer, and thus the clearing of *link may */
789 /* be visible to non-finalization code. */
790 /* There's an argument that an arbitrary action should */
791 /* be allowed here, instead of just clearing a pointer. */
792 /* But this causes problems if that action alters, or */
793 /* examines connectivity. */
794 /* Returns 1 if link was already registered, 0 if */
795 /* registration succeeded, 2 if it failed for lack of */
796 /* memory, and GC_oom_fn did not handle the problem. */
797 /* Only exists for backward compatibility. See below: */
799 GC_API int GC_CALL GC_general_register_disappearing_link (void * * link,
801 /* A slight generalization of the above. *link is */
802 /* cleared when obj first becomes inaccessible. This */
803 /* can be used to implement weak pointers easily and */
804 /* safely. Typically link will point to a location */
805 /* holding a disguised pointer to obj. (A pointer */
806 /* inside an "atomic" object is effectively */
807 /* disguised.) In this way soft */
808 /* pointers are broken before any object */
809 /* reachable from them are finalized. Each link */
810 /* May be registered only once, i.e. with one obj */
811 /* value. This was added after a long email discussion */
812 /* with John Ellis. */
813 /* Obj must be a pointer to the first word of an object */
814 /* we allocated. It is unsafe to explicitly deallocate */
815 /* the object containing link. Explicitly deallocating */
816 /* obj may or may not cause link to eventually be */
818 /* This can be used to implement certain types of */
819 /* weak pointers. Note however that this generally */
820 /* requires that the allocation lock is held (see */
821 /* GC_call_with_alloc_lock() below) when the disguised */
822 /* pointer is accessed. Otherwise a strong pointer */
823 /* could be recreated between the time the collector */
824 /* decides to reclaim the object and the link is */
827 GC_API int GC_CALL GC_unregister_disappearing_link (void * * link);
828 /* Undoes a registration by either of the above two */
829 /* routines. Returns 0 if link was not actually */
830 /* registered (otherwise returns 1). */
832 /* Returns !=0 if GC_invoke_finalizers has something to do. */
833 GC_API int GC_CALL GC_should_invoke_finalizers(void);
835 GC_API int GC_CALL GC_invoke_finalizers(void);
836 /* Run finalizers for all objects that are ready to */
837 /* be finalized. Return the number of finalizers */
838 /* that were run. Normally this is also called */
839 /* implicitly during some allocations. If */
840 /* GC_finalize_on_demand is nonzero, it must be called */
843 /* Explicitly tell the collector that an object is reachable */
844 /* at a particular program point. This prevents the argument */
845 /* pointer from being optimized away, even it is otherwise no */
846 /* longer needed. It should have no visible effect in the */
847 /* absence of finalizers or disappearing links. But it may be */
848 /* needed to prevent finalizers from running while the */
849 /* associated external resource is still in use. */
850 /* The function is sometimes called keep_alive in other */
852 # if defined(__GNUC__) && !defined(__INTEL_COMPILER)
853 # define GC_reachable_here(ptr) \
854 __asm__ volatile(" " : : "X"(ptr) : "memory");
856 GC_API void GC_CALL GC_noop1(GC_word x);
857 # define GC_reachable_here(ptr) GC_noop1((GC_word)(ptr));
860 /* GC_set_warn_proc can be used to redirect or filter warning messages. */
861 /* p may not be a NULL pointer. */
862 typedef void (GC_CALLBACK * GC_warn_proc) (char *msg, GC_word arg);
863 GC_API void GC_CALL GC_set_warn_proc(GC_warn_proc p);
864 /* GC_get_warn_proc returns the current warn_proc. */
865 GC_API GC_warn_proc GC_CALL GC_get_warn_proc(void);
867 /* GC_ignore_warn_proc may be used as an argument for */
868 /* GC_set_warn_proc() to suppress all warnings (unless */
869 /* statistics printing is turned on). */
870 GC_API void GC_CALLBACK GC_ignore_warn_proc(char *msg, GC_word arg);
872 /* The following is intended to be used by a higher level */
873 /* (e.g. Java-like) finalization facility. It is expected */
874 /* that finalization code will arrange for hidden pointers to */
875 /* disappear. Otherwise objects can be accessed after they */
876 /* have been collected. */
877 /* Note that putting pointers in atomic objects or in */
878 /* nonpointer slots of "typed" objects is equivalent to */
879 /* disguising them in this way, and may have other advantages. */
880 # if defined(I_HIDE_POINTERS) || defined(GC_I_HIDE_POINTERS)
881 typedef GC_word GC_hidden_pointer;
882 # define HIDE_POINTER(p) (~(GC_hidden_pointer)(p))
883 # define REVEAL_POINTER(p) ((void *)(HIDE_POINTER(p)))
884 /* Converting a hidden pointer to a real pointer requires verifying */
885 /* that the object still exists. This involves acquiring the */
886 /* allocator lock to avoid a race with the collector. */
887 # endif /* I_HIDE_POINTERS */
889 typedef void * (GC_CALLBACK * GC_fn_type) (void * client_data);
890 GC_API void * GC_CALL GC_call_with_alloc_lock (GC_fn_type fn,
893 /* These routines are intended to explicitly notify the collector */
894 /* of new threads. Often this is unnecessary because thread creation */
895 /* is implicitly intercepted by the collector, using header-file */
896 /* defines, or linker-based interception. In the long run the intent */
897 /* is to always make redundant registration safe. In the short run, */
898 /* this is being implemented a platform at a time. */
899 /* The interface is complicated by the fact that we probably will not */
900 /* ever be able to automatically determine the stack base for thread */
901 /* stacks on all platforms. */
903 /* Structure representing the base of a thread stack. On most */
904 /* platforms this contains just a single address. */
905 struct GC_stack_base {
906 void * mem_base; /* Base of memory stack. */
907 # if defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
908 void * reg_base; /* Base of separate register stack. */
912 typedef void * (GC_CALLBACK * GC_stack_base_func)(struct GC_stack_base *sb,
915 /* Call a function with a stack base structure corresponding to */
916 /* somewhere in the GC_call_with_stack_base frame. This often can */
917 /* be used to provide a sufficiently accurate stack base. And we */
918 /* implement it everywhere. */
919 GC_API void * GC_CALL GC_call_with_stack_base(GC_stack_base_func fn,
922 /* Explicitly enable GC_register_my_thread() invocation. */
923 /* Done implicitly if a GC thread-creation function is called (or */
924 /* DllMain-based thread registration is enabled). Otherwise, it must */
925 /* be called from the main (or any previously registered) thread */
926 /* between the collector initialization and the first explicit */
927 /* registering of a thread (it should be called as late as possible). */
928 GC_API void GC_CALL GC_allow_register_threads(void);
930 /* Register the current thread, with the indicated stack base, as */
931 /* a new thread whose stack(s) should be traced by the GC. If it */
932 /* is not implicitly called by the GC, this must be called before a */
933 /* thread can allocate garbage collected memory, or assign pointers */
934 /* to the garbage collected heap. Once registered, a thread will be */
935 /* stopped during garbage collections. */
936 /* This call must be previously enabled (see above). */
937 /* This should never be called from the main thread, where it is */
938 /* always done implicitly. This is normally done implicitly if GC_ */
939 /* functions are called to create the thread, e.g. by defining */
940 /* GC_THREADS and including gc.h (which redefines some system */
941 /* functions) before calling the system thread creation function. */
942 /* It is also always done implicitly under win32 with DllMain-based */
943 /* thread registration enabled. Except in this latter case, explicit */
944 /* calls are normally required for threads created by third-party */
947 #define GC_DUPLICATE 1 /* Was already registered. */
948 #define GC_NO_THREADS 2 /* No thread support in GC. */
949 #define GC_UNIMPLEMENTED 3 /* Not yet implemented on this platform. */
950 GC_API int GC_CALL GC_register_my_thread(struct GC_stack_base *);
952 /* Unregister the current thread. Only an explicity registered thread */
953 /* (i.e. for which GC_register_my_thread() returns GC_SUCCESS) is */
954 /* allowed (and required) to call this function. The thread may no */
955 /* longer allocate garbage collected memory or manipulate pointers to */
956 /* the garbage collected heap after making this call. */
957 /* Specifically, if it wants to return or otherwise communicate a */
958 /* pointer to the garbage-collected heap to another thread, it must */
959 /* do this before calling GC_unregister_my_thread, most probably */
960 /* by saving it in a global data structure. */
961 GC_API int GC_CALL GC_unregister_my_thread(void);
963 /* Attempt to fill in the GC_stack_base structure with the stack base */
964 /* for this thread. This appears to be required to implement anything */
965 /* like the JNI AttachCurrentThread in an environment in which new */
966 /* threads are not automatically registered with the collector. */
967 /* It is also unfortunately hard to implement well on many platforms. */
968 /* Returns GC_SUCCESS or GC_UNIMPLEMENTED. */
969 GC_API int GC_CALL GC_get_stack_base(struct GC_stack_base *);
971 /* The following routines are primarily intended for use with a */
972 /* preprocessor which inserts calls to check C pointer arithmetic. */
973 /* They indicate failure by invoking the corresponding _print_proc. */
975 /* Check that p and q point to the same object. */
976 /* Fail conspicuously if they don't. */
977 /* Returns the first argument. */
978 /* Succeeds if neither p nor q points to the heap. */
979 /* May succeed if both p and q point to between heap objects. */
980 GC_API void * GC_CALL GC_same_obj (void * p, void * q);
982 /* Checked pointer pre- and post- increment operations. Note that */
983 /* the second argument is in units of bytes, not multiples of the */
984 /* object size. This should either be invoked from a macro, or the */
985 /* call should be automatically generated. */
986 GC_API void * GC_CALL GC_pre_incr (void * *p, ptrdiff_t how_much);
987 GC_API void * GC_CALL GC_post_incr (void * *p, ptrdiff_t how_much);
989 /* Check that p is visible */
990 /* to the collector as a possibly pointer containing location. */
991 /* If it isn't fail conspicuously. */
992 /* Returns the argument in all cases. May erroneously succeed */
993 /* in hard cases. (This is intended for debugging use with */
994 /* untyped allocations. The idea is that it should be possible, though */
995 /* slow, to add such a call to all indirect pointer stores.) */
996 /* Currently useless for multithreaded worlds. */
997 GC_API void * GC_CALL GC_is_visible (void * p);
999 /* Check that if p is a pointer to a heap page, then it points to */
1000 /* a valid displacement within a heap object. */
1001 /* Fail conspicuously if this property does not hold. */
1002 /* Uninteresting with GC_all_interior_pointers. */
1003 /* Always returns its argument. */
1004 GC_API void * GC_CALL GC_is_valid_displacement (void * p);
1006 /* Explicitly dump the GC state. This is most often called from the */
1007 /* debugger, or by setting the GC_DUMP_REGULARLY environment variable, */
1008 /* but it may be useful to call it from client code during debugging. */
1009 /* Defined only if the library has been compiled without NO_DEBUGGING. */
1010 GC_API void GC_CALL GC_dump(void);
1012 /* Safer, but slow, pointer addition. Probably useful mainly with */
1013 /* a preprocessor. Useful only for heap pointers. */
1014 /* Only the macros without trailing digits are meant to be used */
1015 /* by clients. These are designed to model the available C pointer */
1016 /* arithmetic expressions. */
1017 /* Even then, these are probably more useful as */
1018 /* documentation than as part of the API. */
1019 /* Note that GC_PTR_ADD evaluates the first argument more than once. */
1020 #if defined(GC_DEBUG) && defined(__GNUC__)
1021 # define GC_PTR_ADD3(x, n, type_of_result) \
1022 ((type_of_result)GC_same_obj((x)+(n), (x)))
1023 # define GC_PRE_INCR3(x, n, type_of_result) \
1024 ((type_of_result)GC_pre_incr((void **)(&(x)), (n)*sizeof(*x)))
1025 # define GC_POST_INCR3(x, n, type_of_result) \
1026 ((type_of_result)GC_post_incr((void **)(&(x)), (n)*sizeof(*x)))
1027 # define GC_PTR_ADD(x, n) \
1028 GC_PTR_ADD3(x, n, typeof(x))
1029 # define GC_PRE_INCR(x, n) \
1030 GC_PRE_INCR3(x, n, typeof(x))
1031 # define GC_POST_INCR(x) \
1032 GC_POST_INCR3(x, 1, typeof(x))
1033 # define GC_POST_DECR(x) \
1034 GC_POST_INCR3(x, -1, typeof(x))
1035 #else /* !GC_DEBUG || !__GNUC__ */
1036 /* We can't do this right without typeof, which ANSI */
1037 /* decided was not sufficiently useful. Without it */
1038 /* we resort to the non-debug version. */
1039 /* FIXME: This should eventually support C++0x decltype */
1040 # define GC_PTR_ADD(x, n) ((x)+(n))
1041 # define GC_PRE_INCR(x, n) ((x) += (n))
1042 # define GC_POST_INCR(x) ((x)++)
1043 # define GC_POST_DECR(x) ((x)--)
1046 /* Safer assignment of a pointer to a non-stack location. */
1048 # define GC_PTR_STORE(p, q) \
1049 (*(void **)GC_is_visible(p) = GC_is_valid_displacement(q))
1050 #else /* !GC_DEBUG */
1051 # define GC_PTR_STORE(p, q) (*(p) = (q))
1054 /* Functions called to report pointer checking errors */
1055 GC_API void (GC_CALLBACK * GC_same_obj_print_proc) (void * p, void * q);
1057 GC_API void (GC_CALLBACK * GC_is_valid_displacement_print_proc) (void * p);
1059 GC_API void (GC_CALLBACK * GC_is_visible_print_proc) (void * p);
1062 /* For pthread support, we generally need to intercept a number of */
1063 /* thread library calls. We do that here by macro defining them. */
1065 #if !defined(GC_USE_LD_WRAP) && !defined(GC_NO_THREAD_REDIRECTS) \
1066 && defined(GC_PTHREADS)
1067 # include "gc_pthread_redirects.h"
1070 # if defined(PCR) || defined(GC_SOLARIS_THREADS) || \
1071 defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
1072 /* Any flavor of threads. */
1073 /* This returns a list of objects, linked through their first */
1074 /* word. Its use can greatly reduce lock contention problems, since */
1075 /* the allocation lock can be acquired and released many fewer times. */
1076 GC_API void * GC_CALL GC_malloc_many(size_t lb);
1077 #define GC_NEXT(p) (*(void * *)(p)) /* Retrieve the next element */
1078 /* in returned list. */
1080 #endif /* THREADS */
1082 /* Register a callback to control the scanning of dynamic libraries.
1083 When the GC scans the static data of a dynamic library, it will
1084 first call a user-supplied routine with filename of the library and
1085 the address and length of the memory region. This routine should
1086 return nonzero if that region should be scanned. */
1087 GC_API void GC_CALL GC_register_has_static_roots_callback
1088 (int (GC_CALLBACK * callback)(const char *, void *, size_t));
1091 #if defined(GC_WIN32_THREADS) && !defined(__CYGWIN32__) \
1092 && !defined(__CYGWIN__) \
1093 && !defined(GC_PTHREADS)
1095 #ifndef GC_NO_THREAD_DECLS
1098 } /* Including windows.h in an extern "C" context no longer works. */
1101 # if !defined(_WIN32_WCE)
1102 # include <process.h> /* For _beginthreadex, _endthreadex */
1105 # include <windows.h>
1111 * All threads must be created using GC_CreateThread or GC_beginthreadex,
1112 * or must explicitly call GC_register_my_thread
1113 * (and call GC_unregister_my_thread before thread termination),
1114 * so that they will be recorded in the thread table.
1115 * For backwards compatibility, it is possible to build the GC
1116 * with GC_DLL defined, and to call GC_use_DllMain().
1117 * This implicitly registers all created threads, but appears to be
1120 * Currently the collector expects all threads to fall through and
1121 * terminate normally, or call GC_endthreadex() or GC_ExitThread,
1122 * so that the thread is properly unregistered.
1124 GC_API HANDLE WINAPI GC_CreateThread(
1125 LPSECURITY_ATTRIBUTES lpThreadAttributes,
1126 DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress,
1127 LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId );
1129 # if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED) \
1130 && !defined(UINTPTR_MAX)
1131 typedef GC_word GC_uintptr_t;
1133 typedef uintptr_t GC_uintptr_t;
1136 GC_API GC_uintptr_t GC_CALL GC_beginthreadex(
1137 void *security, unsigned stack_size,
1138 unsigned ( __stdcall *start_address )( void * ),
1139 void *arglist, unsigned initflag, unsigned *thrdaddr);
1141 GC_API void GC_CALL GC_endthreadex(unsigned retval);
1143 GC_API void WINAPI GC_ExitThread(DWORD dwExitCode);
1145 # if defined(_WIN32_WCE)
1147 * win32_threads.c implements the real WinMain, which will start a new thread
1148 * to call GC_WinMain after initializing the garbage collector.
1150 GC_API int WINAPI GC_WinMain(
1151 HINSTANCE hInstance,
1152 HINSTANCE hPrevInstance,
1156 # define WinMain GC_WinMain
1158 # endif /* defined(_WIN32_WCE) */
1159 #endif /* !GC_NO_THREAD_DECLS */
1162 * Use implicit thread registration via DllMain.
1163 * Must be called before GC_INIT and other GC routines.
1164 * Should be avoided if GC_beginthreadex and friends can be called
1167 GC_API void GC_CALL GC_use_DllMain(void);
1169 # ifndef GC_NO_THREAD_REDIRECTS
1170 # define CreateThread GC_CreateThread
1171 # define ExitThread GC_ExitThread
1172 # undef _beginthreadex
1173 # define _beginthreadex GC_beginthreadex
1174 # undef _endthreadex
1175 # define _endthreadex GC_endthreadex
1176 /* # define _beginthread { > "Please use _beginthreadex instead of _beginthread" < } */
1177 # endif /* !GC_NO_THREAD_REDIRECTS */
1179 #endif /* defined(GC_WIN32_THREADS) && !cygwin */
1182 * Fully portable code should call GC_INIT() from the main program
1183 * before making any other GC_ calls. On most platforms this is a
1184 * no-op and the collector self-initializes. But a number of platforms
1185 * make that too hard.
1186 * A GC_INIT call is required if the collector is built with THREAD_LOCAL_ALLOC
1187 * defined and the initial allocation call is not to GC_malloc() or
1188 * GC_malloc_atomic().
1190 #if defined(__CYGWIN32__) || defined (_AIX)
1192 * Similarly gnu-win32 DLLs need explicit initialization from
1193 * the main program, as does AIX.
1195 # ifdef __CYGWIN32__
1196 extern int _data_start__[];
1197 extern int _data_end__[];
1198 extern int _bss_start__[];
1199 extern int _bss_end__[];
1200 # define GC_MAX(x,y) ((x) > (y) ? (x) : (y))
1201 # define GC_MIN(x,y) ((x) < (y) ? (x) : (y))
1202 # define GC_DATASTART ((void *) GC_MIN(_data_start__, _bss_start__))
1203 # define GC_DATAEND ((void *) GC_MAX(_data_end__, _bss_end__))
1204 # define GC_INIT() { GC_add_roots(GC_DATASTART, GC_DATAEND); \
1205 GC_gcollect(); /* For blacklisting. */}
1206 /* Required at least if GC is in dll. And doesn't hurt. */
1209 extern int _data[], _end[];
1210 # define GC_DATASTART ((void *)((ulong)_data))
1211 # define GC_DATAEND ((void *)((ulong)_end))
1212 # define GC_INIT() { GC_add_roots(GC_DATASTART, GC_DATAEND); }
1215 # define GC_INIT() { GC_init(); }
1218 /* win32S may not free all resources on process exit. */
1219 /* This explicitly deallocates the heap. */
1220 GC_API void GC_CALL GC_win32_free_heap(void);
1222 #if ( defined(_AMIGA) && !defined(GC_AMIGA_MAKINGLIB) )
1223 /* Allocation really goes through GC_amiga_allocwrapper_do */
1224 # include "gc_amiga_redirects.h"
1228 * GC_REDIRECT_TO_LOCAL is now redundant;
1229 * that's the default with THREAD_LOCAL_ALLOC.
1233 } /* end of extern "C" */