Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / boehm-gc / include / gc.h
blob0781a611bb09290f5b39a8b11ecd23db58fd0fcc
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.
7 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 * Permission is hereby granted to use or copy this program
11 * for any purpose, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
18 * Note that this defines a large number of tuning hooks, which can
19 * safely be ignored in nearly all cases. For normal use it suffices
20 * to call only GC_MALLOC and perhaps GC_REALLOC.
21 * For better performance, also look at GC_MALLOC_ATOMIC, and
22 * GC_enable_incremental. If you need an action to be performed
23 * immediately before an object is collected, look at GC_register_finalizer.
24 * If you are using Solaris threads, look at the end of this file.
25 * Everything else is best ignored unless you encounter performance
26 * problems.
29 #ifndef _GC_H
31 # define _GC_H
33 /*
34 * As this header includes gc_config.h, preprocessor conflicts can occur with
35 * clients that include their own autoconf headers. The following #undef's
36 * work around some likely conflicts.
39 # ifdef PACKAGE_NAME
40 # undef PACKAGE_NAME
41 # endif
42 # ifdef PACKAGE_BUGREPORT
43 # undef PACKAGE_BUGREPORT
44 # endif
45 # ifdef PACKAGE_STRING
46 # undef PACKAGE_STRING
47 # endif
48 # ifdef PACKAGE_TARNAME
49 # undef PACKAGE_TARNAME
50 # endif
51 # ifdef PACKAGE_VERSION
52 # undef PACKAGE_VERSION
53 # endif
55 # include <gc_config.h>
56 # include "gc_config_macros.h"
58 # if defined(__STDC__) || defined(__cplusplus)
59 # define GC_PROTO(args) args
60 typedef void * GC_PTR;
61 # define GC_CONST const
62 # else
63 # define GC_PROTO(args) ()
64 typedef char * GC_PTR;
65 # define GC_CONST
66 # endif
68 # ifdef __cplusplus
69 extern "C" {
70 # endif
73 /* Define word and signed_word to be unsigned and signed types of the */
74 /* size as char * or void *. There seems to be no way to do this */
75 /* even semi-portably. The following is probably no better/worse */
76 /* than almost anything else. */
77 /* The ANSI standard suggests that size_t and ptr_diff_t might be */
78 /* better choices. But those had incorrect definitions on some older */
79 /* systems. Notably "typedef int size_t" is WRONG. */
80 #ifndef _WIN64
81 typedef unsigned long GC_word;
82 typedef long GC_signed_word;
83 #else
84 /* Win64 isn't really supported yet, but this is the first step. And */
85 /* it might cause error messages to show up in more plausible places. */
86 /* This needs basetsd.h, which is included by windows.h. */
87 typedef ULONG_PTR GC_word;
88 typedef LONG_PTR GC_word;
89 #endif
91 /* Public read-only variables */
93 GC_API GC_word GC_gc_no;/* Counter incremented per collection. */
94 /* Includes empty GCs at startup. */
96 GC_API int GC_parallel; /* GC is parallelized for performance on */
97 /* multiprocessors. Currently set only */
98 /* implicitly if collector is built with */
99 /* -DPARALLEL_MARK and if either: */
100 /* Env variable GC_NPROC is set to > 1, or */
101 /* GC_NPROC is not set and this is an MP. */
102 /* If GC_parallel is set, incremental */
103 /* collection is only partially functional, */
104 /* and may not be desirable. */
107 /* Public R/W variables */
109 GC_API GC_PTR (*GC_oom_fn) GC_PROTO((size_t bytes_requested));
110 /* When there is insufficient memory to satisfy */
111 /* an allocation request, we return */
112 /* (*GC_oom_fn)(). By default this just */
113 /* returns 0. */
114 /* If it returns, it must return 0 or a valid */
115 /* pointer to a previously allocated heap */
116 /* object. */
118 GC_API int GC_find_leak;
119 /* Do not actually garbage collect, but simply */
120 /* report inaccessible memory that was not */
121 /* deallocated with GC_free. Initial value */
122 /* is determined by FIND_LEAK macro. */
124 GC_API int GC_all_interior_pointers;
125 /* Arrange for pointers to object interiors to */
126 /* be recognized as valid. May not be changed */
127 /* after GC initialization. */
128 /* Initial value is determined by */
129 /* -DALL_INTERIOR_POINTERS. */
130 /* Unless DONT_ADD_BYTE_AT_END is defined, this */
131 /* also affects whether sizes are increased by */
132 /* at least a byte to allow "off the end" */
133 /* pointer recognition. */
134 /* MUST BE 0 or 1. */
136 GC_API int GC_quiet; /* Disable statistics output. Only matters if */
137 /* collector has been compiled with statistics */
138 /* enabled. This involves a performance cost, */
139 /* and is thus not the default. */
141 GC_API int GC_finalize_on_demand;
142 /* If nonzero, finalizers will only be run in */
143 /* response to an explicit GC_invoke_finalizers */
144 /* call. The default is determined by whether */
145 /* the FINALIZE_ON_DEMAND macro is defined */
146 /* when the collector is built. */
148 GC_API int GC_java_finalization;
149 /* Mark objects reachable from finalizable */
150 /* objects in a separate postpass. This makes */
151 /* it a bit safer to use non-topologically- */
152 /* ordered finalization. Default value is */
153 /* determined by JAVA_FINALIZATION macro. */
155 GC_API void (* GC_finalizer_notifier)();
156 /* Invoked by the collector when there are */
157 /* objects to be finalized. Invoked at most */
158 /* once per GC cycle. Never invoked unless */
159 /* GC_finalize_on_demand is set. */
160 /* Typically this will notify a finalization */
161 /* thread, which will call GC_invoke_finalizers */
162 /* in response. */
164 GC_API int GC_dont_gc; /* != 0 ==> Dont collect. In versions 6.2a1+, */
165 /* this overrides explicit GC_gcollect() calls. */
166 /* Used as a counter, so that nested enabling */
167 /* and disabling work correctly. Should */
168 /* normally be updated with GC_enable() and */
169 /* GC_disable() calls. */
170 /* Direct assignment to GC_dont_gc is */
171 /* deprecated. */
173 GC_API int GC_dont_expand;
174 /* Dont expand heap unless explicitly requested */
175 /* or forced to. */
177 GC_API int GC_use_entire_heap;
178 /* Causes the nonincremental collector to use the */
179 /* entire heap before collecting. This was the only */
180 /* option for GC versions < 5.0. This sometimes */
181 /* results in more large block fragmentation, since */
182 /* very larg blocks will tend to get broken up */
183 /* during each GC cycle. It is likely to result in a */
184 /* larger working set, but lower collection */
185 /* frequencies, and hence fewer instructions executed */
186 /* in the collector. */
188 GC_API int GC_full_freq; /* Number of partial collections between */
189 /* full collections. Matters only if */
190 /* GC_incremental is set. */
191 /* Full collections are also triggered if */
192 /* the collector detects a substantial */
193 /* increase in the number of in-use heap */
194 /* blocks. Values in the tens are now */
195 /* perfectly reasonable, unlike for */
196 /* earlier GC versions. */
198 GC_API GC_word GC_non_gc_bytes;
199 /* Bytes not considered candidates for collection. */
200 /* Used only to control scheduling of collections. */
201 /* Updated by GC_malloc_uncollectable and GC_free. */
202 /* Wizards only. */
204 GC_API int GC_no_dls;
205 /* Don't register dynamic library data segments. */
206 /* Wizards only. Should be used only if the */
207 /* application explicitly registers all roots. */
208 /* In Microsoft Windows environments, this will */
209 /* usually also prevent registration of the */
210 /* main data segment as part of the root set. */
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 the heap size plus */
216 /* a rough estimate of the root set size. */
217 /* Initially, GC_free_space_divisor = 4. */
218 /* Increasing its value will use less space */
219 /* but more collection time. Decreasing it */
220 /* will appreciably decrease collection time */
221 /* at the expense of space. */
222 /* GC_free_space_divisor = 1 will effectively */
223 /* disable collections. */
225 GC_API GC_word GC_max_retries;
226 /* The maximum number of GCs attempted before */
227 /* reporting out of memory after heap */
228 /* expansion fails. Initially 0. */
231 GC_API char *GC_stackbottom; /* Cool end of user stack. */
232 /* May be set in the client prior to */
233 /* calling any GC_ routines. This */
234 /* avoids some overhead, and */
235 /* potentially some signals that can */
236 /* confuse debuggers. Otherwise the */
237 /* collector attempts to set it */
238 /* automatically. */
239 /* For multithreaded code, this is the */
240 /* cold end of the stack for the */
241 /* primordial thread. */
243 GC_API int GC_dont_precollect; /* Don't collect as part of */
244 /* initialization. Should be set only */
245 /* if the client wants a chance to */
246 /* manually initialize the root set */
247 /* before the first collection. */
248 /* Interferes with blacklisting. */
249 /* Wizards only. */
251 /* Public procedures */
253 /* Initialize the collector. This is only required when using thread-local
254 * allocation, since unlike the regular allocation routines, GC_local_malloc
255 * is not self-initializing. If you use GC_local_malloc you should arrange
256 * to call this somehow (e.g. from a constructor) before doing any allocation.
258 GC_API void GC_init GC_PROTO((void));
260 GC_API unsigned long GC_time_limit;
261 /* If incremental collection is enabled, */
262 /* We try to terminate collections */
263 /* after this many milliseconds. Not a */
264 /* hard time bound. Setting this to */
265 /* GC_TIME_UNLIMITED will essentially */
266 /* disable incremental collection while */
267 /* leaving generational collection */
268 /* enabled. */
269 # define GC_TIME_UNLIMITED 999999
270 /* Setting GC_time_limit to this value */
271 /* will disable the "pause time exceeded"*/
272 /* tests. */
274 /* Public procedures */
276 /* Initialize the collector. This is only required when using thread-local
277 * allocation, since unlike the regular allocation routines, GC_local_malloc
278 * is not self-initializing. If you use GC_local_malloc you should arrange
279 * to call this somehow (e.g. from a constructor) before doing any allocation.
280 * For win32 threads, it needs to be called explicitly.
282 GC_API void GC_init GC_PROTO((void));
285 * general purpose allocation routines, with roughly malloc calling conv.
286 * The atomic versions promise that no relevant pointers are contained
287 * in the object. The nonatomic versions guarantee that the new object
288 * is cleared. GC_malloc_stubborn promises that no changes to the object
289 * will occur after GC_end_stubborn_change has been called on the
290 * result of GC_malloc_stubborn. GC_malloc_uncollectable allocates an object
291 * that is scanned for pointers to collectable objects, but is not itself
292 * collectable. The object is scanned even if it does not appear to
293 * be reachable. GC_malloc_uncollectable and GC_free called on the resulting
294 * object implicitly update GC_non_gc_bytes appropriately.
296 * Note that the GC_malloc_stubborn support is stubbed out by default
297 * starting in 6.0. GC_malloc_stubborn is an alias for GC_malloc unless
298 * the collector is built with STUBBORN_ALLOC defined.
300 GC_API GC_PTR GC_malloc GC_PROTO((size_t size_in_bytes));
301 GC_API GC_PTR GC_malloc_atomic GC_PROTO((size_t size_in_bytes));
302 GC_API GC_PTR GC_malloc_uncollectable GC_PROTO((size_t size_in_bytes));
303 GC_API GC_PTR GC_malloc_stubborn GC_PROTO((size_t size_in_bytes));
305 /* The following is only defined if the library has been suitably */
306 /* compiled: */
307 GC_API GC_PTR GC_malloc_atomic_uncollectable GC_PROTO((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 enable 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_free GC_PROTO((GC_PTR 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 nonNIL pointer stores into the object
323 * are considered to be changes. The argument to GC_end_stubborn_change
324 * must be exacly 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
330 * changeable.
332 GC_API void GC_change_stubborn GC_PROTO((GC_PTR));
333 GC_API void GC_end_stubborn_change GC_PROTO((GC_PTR));
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 */
342 /* object. */
343 GC_API GC_PTR GC_base GC_PROTO((GC_PTR displaced_pointer));
345 /* Given a pointer to the base of an object, return its size in bytes. */
346 /* The returned size may be slightly larger than what was originally */
347 /* requested. */
348 GC_API size_t GC_size GC_PROTO((GC_PTR object_addr));
350 /* For compatibility with C library. This is occasionally faster than */
351 /* a malloc followed by a bcopy. But if you rely on that, either here */
352 /* or with the standard C library, your code is broken. In my */
353 /* opinion, it shouldn't have been invented, but now we're stuck. -HB */
354 /* The resulting object has the same kind as the original. */
355 /* If the argument is stubborn, the result will have changes enabled. */
356 /* It is an error to have changes enabled for the original object. */
357 /* Follows ANSI comventions for NULL old_object. */
358 GC_API GC_PTR GC_realloc
359 GC_PROTO((GC_PTR old_object, size_t new_size_in_bytes));
361 /* Explicitly increase the heap size. */
362 /* Returns 0 on failure, 1 on success. */
363 GC_API int GC_expand_hp GC_PROTO((size_t number_of_bytes));
365 /* Limit the heap size to n bytes. Useful when you're debugging, */
366 /* especially on systems that don't handle running out of memory well. */
367 /* n == 0 ==> unbounded. This is the default. */
368 GC_API void GC_set_max_heap_size GC_PROTO((GC_word n));
370 /* Inform the collector that a certain section of statically allocated */
371 /* memory contains no pointers to garbage collected memory. Thus it */
372 /* need not be scanned. This is sometimes important if the application */
373 /* maps large read/write files into the address space, which could be */
374 /* mistaken for dynamic library data segments on some systems. */
375 GC_API void GC_exclude_static_roots GC_PROTO((GC_PTR start, GC_PTR finish));
377 /* Clear the set of root segments. Wizards only. */
378 GC_API void GC_clear_roots GC_PROTO((void));
380 /* Add a root segment. Wizards only. */
381 GC_API void GC_add_roots GC_PROTO((char * low_address,
382 char * high_address_plus_1));
384 /* Remove a root segment. Wizards only. */
385 GC_API void GC_remove_roots GC_PROTO((char * low_address,
386 char * high_address_plus_1));
388 /* Add a displacement to the set of those considered valid by the */
389 /* collector. GC_register_displacement(n) means that if p was returned */
390 /* by GC_malloc, then (char *)p + n will be considered to be a valid */
391 /* pointer to p. N must be small and less than the size of p. */
392 /* (All pointers to the interior of objects from the stack are */
393 /* considered valid in any case. This applies to heap objects and */
394 /* static data.) */
395 /* Preferably, this should be called before any other GC procedures. */
396 /* Calling it later adds to the probability of excess memory */
397 /* retention. */
398 /* This is a no-op if the collector has recognition of */
399 /* arbitrary interior pointers enabled, which is now the default. */
400 GC_API void GC_register_displacement GC_PROTO((GC_word n));
402 /* The following version should be used if any debugging allocation is */
403 /* being done. */
404 GC_API void GC_debug_register_displacement GC_PROTO((GC_word n));
406 /* Explicitly trigger a full, world-stop collection. */
407 GC_API void GC_gcollect GC_PROTO((void));
409 /* Trigger a full world-stopped collection. Abort the collection if */
410 /* and when stop_func returns a nonzero value. Stop_func will be */
411 /* called frequently, and should be reasonably fast. This works even */
412 /* if virtual dirty bits, and hence incremental collection is not */
413 /* available for this architecture. Collections can be aborted faster */
414 /* than normal pause times for incremental collection. However, */
415 /* aborted collections do no useful work; the next collection needs */
416 /* to start from the beginning. */
417 /* Return 0 if the collection was aborted, 1 if it succeeded. */
418 typedef int (* GC_stop_func) GC_PROTO((void));
419 GC_API int GC_try_to_collect GC_PROTO((GC_stop_func stop_func));
421 /* Return the number of bytes in the heap. Excludes collector private */
422 /* data structures. Includes empty blocks and fragmentation loss. */
423 /* Includes some pages that were allocated but never written. */
424 GC_API size_t GC_get_heap_size GC_PROTO((void));
426 /* Return a lower bound on the number of free bytes in the heap. */
427 GC_API size_t GC_get_free_bytes GC_PROTO((void));
429 /* Return the number of bytes allocated since the last collection. */
430 GC_API size_t GC_get_bytes_since_gc GC_PROTO((void));
432 /* Return the total number of bytes allocated in this process. */
433 /* Never decreases, except due to wrapping. */
434 GC_API size_t GC_get_total_bytes GC_PROTO((void));
436 /* Disable garbage collection. Even GC_gcollect calls will be */
437 /* ineffective. */
438 GC_API void GC_disable GC_PROTO((void));
440 /* Reenable garbage collection. GC_disable() and GC_enable() calls */
441 /* nest. Garbage collection is enabled if the number of calls to both */
442 /* both functions is equal. */
443 GC_API void GC_enable GC_PROTO((void));
445 /* Enable incremental/generational collection. */
446 /* Not advisable unless dirty bits are */
447 /* available or most heap objects are */
448 /* pointerfree(atomic) or immutable. */
449 /* Don't use in leak finding mode. */
450 /* Ignored if GC_dont_gc is true. */
451 /* Only the generational piece of this is */
452 /* functional if GC_parallel is TRUE */
453 /* or if GC_time_limit is GC_TIME_UNLIMITED. */
454 /* Causes GC_local_gcj_malloc() to revert to */
455 /* locked allocation. Must be called */
456 /* before any GC_local_gcj_malloc() calls. */
457 GC_API void GC_enable_incremental GC_PROTO((void));
459 /* Does incremental mode write-protect pages? Returns zero or */
460 /* more of the following, or'ed together: */
461 #define GC_PROTECTS_POINTER_HEAP 1 /* May protect non-atomic objs. */
462 #define GC_PROTECTS_PTRFREE_HEAP 2
463 #define GC_PROTECTS_STATIC_DATA 4 /* Curently never. */
464 #define GC_PROTECTS_STACK 8 /* Probably impractical. */
466 #define GC_PROTECTS_NONE 0
467 GC_API int GC_incremental_protection_needs GC_PROTO((void));
469 /* Perform some garbage collection work, if appropriate. */
470 /* Return 0 if there is no more work to be done. */
471 /* Typically performs an amount of work corresponding roughly */
472 /* to marking from one page. May do more work if further */
473 /* progress requires it, e.g. if incremental collection is */
474 /* disabled. It is reasonable to call this in a wait loop */
475 /* until it returns 0. */
476 GC_API int GC_collect_a_little GC_PROTO((void));
478 /* Allocate an object of size lb bytes. The client guarantees that */
479 /* as long as the object is live, it will be referenced by a pointer */
480 /* that points to somewhere within the first 256 bytes of the object. */
481 /* (This should normally be declared volatile to prevent the compiler */
482 /* from invalidating this assertion.) This routine is only useful */
483 /* if a large array is being allocated. It reduces the chance of */
484 /* accidentally retaining such an array as a result of scanning an */
485 /* integer that happens to be an address inside the array. (Actually, */
486 /* it reduces the chance of the allocator not finding space for such */
487 /* an array, since it will try hard to avoid introducing such a false */
488 /* reference.) On a SunOS 4.X or MS Windows system this is recommended */
489 /* for arrays likely to be larger than 100K or so. For other systems, */
490 /* or if the collector is not configured to recognize all interior */
491 /* pointers, the threshold is normally much higher. */
492 GC_API GC_PTR GC_malloc_ignore_off_page GC_PROTO((size_t lb));
493 GC_API GC_PTR GC_malloc_atomic_ignore_off_page GC_PROTO((size_t lb));
495 #if defined(__sgi) && !defined(__GNUC__) && _COMPILER_VERSION >= 720
496 # define GC_ADD_CALLER
497 # define GC_RETURN_ADDR (GC_word)__return_address
498 #endif
500 #ifdef __linux__
501 # include <features.h>
502 # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1 || __GLIBC__ > 2) \
503 && !defined(__ia64__)
504 # ifndef GC_HAVE_BUILTIN_BACKTRACE
505 # define GC_HAVE_BUILTIN_BACKTRACE
506 # endif
507 # endif
508 # if defined(__i386__) || defined(__x86_64__)
509 # define GC_CAN_SAVE_CALL_STACKS
510 # endif
511 #endif
513 #if defined(GC_HAVE_BUILTIN_BACKTRACE) && !defined(GC_CAN_SAVE_CALL_STACKS)
514 # define GC_CAN_SAVE_CALL_STACKS
515 #endif
517 #if defined(__sparc__)
518 # define GC_CAN_SAVE_CALL_STACKS
519 #endif
521 /* If we're on an a platform on which we can't save call stacks, but */
522 /* gcc is normally used, we go ahead and define GC_ADD_CALLER. */
523 /* We make this decision independent of whether gcc is actually being */
524 /* used, in order to keep the interface consistent, and allow mixing */
525 /* of compilers. */
526 /* This may also be desirable if it is possible but expensive to */
527 /* retrieve the call chain. */
528 #if (defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__) \
529 || defined(__FreeBSD__)) & !defined(GC_CAN_SAVE_CALL_STACKS)
530 # define GC_ADD_CALLER
531 # if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
532 /* gcc knows how to retrieve return address, but we don't know */
533 /* how to generate call stacks. */
534 # define GC_RETURN_ADDR (GC_word)__builtin_return_address(0)
535 # else
536 /* Just pass 0 for gcc compatibility. */
537 # define GC_RETURN_ADDR 0
538 # endif
539 #endif
541 #ifdef GC_ADD_CALLER
542 # define GC_EXTRAS GC_RETURN_ADDR, __FILE__, __LINE__
543 # define GC_EXTRA_PARAMS GC_word ra, GC_CONST char * s, int i
544 #else
545 # define GC_EXTRAS __FILE__, __LINE__
546 # define GC_EXTRA_PARAMS GC_CONST char * s, int i
547 #endif
549 /* Debugging (annotated) allocation. GC_gcollect will check */
550 /* objects allocated in this way for overwrites, etc. */
551 GC_API GC_PTR GC_debug_malloc
552 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
553 GC_API GC_PTR GC_debug_malloc_atomic
554 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
555 GC_API GC_PTR GC_debug_malloc_uncollectable
556 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
557 GC_API GC_PTR GC_debug_malloc_stubborn
558 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
559 GC_API GC_PTR GC_debug_malloc_ignore_off_page
560 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
561 GC_API GC_PTR GC_debug_malloc_atomic_ignore_off_page
562 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
563 GC_API void GC_debug_free GC_PROTO((GC_PTR object_addr));
564 GC_API GC_PTR GC_debug_realloc
565 GC_PROTO((GC_PTR old_object, size_t new_size_in_bytes,
566 GC_EXTRA_PARAMS));
567 GC_API void GC_debug_change_stubborn GC_PROTO((GC_PTR));
568 GC_API void GC_debug_end_stubborn_change GC_PROTO((GC_PTR));
570 /* Routines that allocate objects with debug information (like the */
571 /* above), but just fill in dummy file and line number information. */
572 /* Thus they can serve as drop-in malloc/realloc replacements. This */
573 /* can be useful for two reasons: */
574 /* 1) It allows the collector to be built with DBG_HDRS_ALL defined */
575 /* even if some allocation calls come from 3rd party libraries */
576 /* that can't be recompiled. */
577 /* 2) On some platforms, the file and line information is redundant, */
578 /* since it can be reconstructed from a stack trace. On such */
579 /* platforms it may be more convenient not to recompile, e.g. for */
580 /* leak detection. This can be accomplished by instructing the */
581 /* linker to replace malloc/realloc with these. */
582 GC_API GC_PTR GC_debug_malloc_replacement GC_PROTO((size_t size_in_bytes));
583 GC_API GC_PTR GC_debug_realloc_replacement
584 GC_PROTO((GC_PTR object_addr, size_t size_in_bytes));
586 # ifdef GC_DEBUG
587 # define GC_MALLOC(sz) GC_debug_malloc(sz, GC_EXTRAS)
588 # define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, GC_EXTRAS)
589 # define GC_MALLOC_UNCOLLECTABLE(sz) \
590 GC_debug_malloc_uncollectable(sz, GC_EXTRAS)
591 # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
592 GC_debug_malloc_ignore_off_page(sz, GC_EXTRAS)
593 # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
594 GC_debug_malloc_atomic_ignore_off_page(sz, GC_EXTRAS)
595 # define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, GC_EXTRAS)
596 # define GC_FREE(p) GC_debug_free(p)
597 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
598 GC_debug_register_finalizer(p, f, d, of, od)
599 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
600 GC_debug_register_finalizer_ignore_self(p, f, d, of, od)
601 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
602 GC_debug_register_finalizer_no_order(p, f, d, of, od)
603 # define GC_MALLOC_STUBBORN(sz) GC_debug_malloc_stubborn(sz, GC_EXTRAS);
604 # define GC_CHANGE_STUBBORN(p) GC_debug_change_stubborn(p)
605 # define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p)
606 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
607 GC_general_register_disappearing_link(link, GC_base(obj))
608 # define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n)
609 # else
610 # define GC_MALLOC(sz) GC_malloc(sz)
611 # define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz)
612 # define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz)
613 # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
614 GC_malloc_ignore_off_page(sz)
615 # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
616 GC_malloc_atomic_ignore_off_page(sz)
617 # define GC_REALLOC(old, sz) GC_realloc(old, sz)
618 # define GC_FREE(p) GC_free(p)
619 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
620 GC_register_finalizer(p, f, d, of, od)
621 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
622 GC_register_finalizer_ignore_self(p, f, d, of, od)
623 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
624 GC_register_finalizer_no_order(p, f, d, of, od)
625 # define GC_MALLOC_STUBBORN(sz) GC_malloc_stubborn(sz)
626 # define GC_CHANGE_STUBBORN(p) GC_change_stubborn(p)
627 # define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p)
628 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
629 GC_general_register_disappearing_link(link, obj)
630 # define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n)
631 # endif
632 /* The following are included because they are often convenient, and */
633 /* reduce the chance for a misspecifed size argument. But calls may */
634 /* expand to something syntactically incorrect if t is a complicated */
635 /* type expression. */
636 # define GC_NEW(t) (t *)GC_MALLOC(sizeof (t))
637 # define GC_NEW_ATOMIC(t) (t *)GC_MALLOC_ATOMIC(sizeof (t))
638 # define GC_NEW_STUBBORN(t) (t *)GC_MALLOC_STUBBORN(sizeof (t))
639 # define GC_NEW_UNCOLLECTABLE(t) (t *)GC_MALLOC_UNCOLLECTABLE(sizeof (t))
641 /* Finalization. Some of these primitives are grossly unsafe. */
642 /* The idea is to make them both cheap, and sufficient to build */
643 /* a safer layer, closer to Modula-3, Java, or PCedar finalization. */
644 /* The interface represents my conclusions from a long discussion */
645 /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes, */
646 /* Christian Jacobi, and Russ Atkinson. It's not perfect, and */
647 /* probably nobody else agrees with it. Hans-J. Boehm 3/13/92 */
648 typedef void (*GC_finalization_proc)
649 GC_PROTO((GC_PTR obj, GC_PTR client_data));
651 GC_API void GC_register_finalizer
652 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
653 GC_finalization_proc *ofn, GC_PTR *ocd));
654 GC_API void GC_debug_register_finalizer
655 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
656 GC_finalization_proc *ofn, GC_PTR *ocd));
657 /* When obj is no longer accessible, invoke */
658 /* (*fn)(obj, cd). If a and b are inaccessible, and */
659 /* a points to b (after disappearing links have been */
660 /* made to disappear), then only a will be */
661 /* finalized. (If this does not create any new */
662 /* pointers to b, then b will be finalized after the */
663 /* next collection.) Any finalizable object that */
664 /* is reachable from itself by following one or more */
665 /* pointers will not be finalized (or collected). */
666 /* Thus cycles involving finalizable objects should */
667 /* be avoided, or broken by disappearing links. */
668 /* All but the last finalizer registered for an object */
669 /* is ignored. */
670 /* Finalization may be removed by passing 0 as fn. */
671 /* Finalizers are implicitly unregistered just before */
672 /* they are invoked. */
673 /* The old finalizer and client data are stored in */
674 /* *ofn and *ocd. */
675 /* Fn is never invoked on an accessible object, */
676 /* provided hidden pointers are converted to real */
677 /* pointers only if the allocation lock is held, and */
678 /* such conversions are not performed by finalization */
679 /* routines. */
680 /* If GC_register_finalizer is aborted as a result of */
681 /* a signal, the object may be left with no */
682 /* finalization, even if neither the old nor new */
683 /* finalizer were NULL. */
684 /* Obj should be the nonNULL starting address of an */
685 /* object allocated by GC_malloc or friends. */
686 /* Note that any garbage collectable object referenced */
687 /* by cd will be considered accessible until the */
688 /* finalizer is invoked. */
690 /* Another versions of the above follow. It ignores */
691 /* self-cycles, i.e. pointers from a finalizable object to */
692 /* itself. There is a stylistic argument that this is wrong, */
693 /* but it's unavoidable for C++, since the compiler may */
694 /* silently introduce these. It's also benign in that specific */
695 /* case. And it helps if finalizable objects are split to */
696 /* avoid cycles. */
697 /* Note that cd will still be viewed as accessible, even if it */
698 /* refers to the object itself. */
699 GC_API void GC_register_finalizer_ignore_self
700 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
701 GC_finalization_proc *ofn, GC_PTR *ocd));
702 GC_API void GC_debug_register_finalizer_ignore_self
703 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
704 GC_finalization_proc *ofn, GC_PTR *ocd));
706 /* Another version of the above. It ignores all cycles. */
707 /* It should probably only be used by Java implementations. */
708 /* Note that cd will still be viewed as accessible, even if it */
709 /* refers to the object itself. */
710 GC_API void GC_register_finalizer_no_order
711 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
712 GC_finalization_proc *ofn, GC_PTR *ocd));
713 GC_API void GC_debug_register_finalizer_no_order
714 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
715 GC_finalization_proc *ofn, GC_PTR *ocd));
718 /* The following routine may be used to break cycles between */
719 /* finalizable objects, thus causing cyclic finalizable */
720 /* objects to be finalized in the correct order. Standard */
721 /* use involves calling GC_register_disappearing_link(&p), */
722 /* where p is a pointer that is not followed by finalization */
723 /* code, and should not be considered in determining */
724 /* finalization order. */
725 GC_API int GC_register_disappearing_link GC_PROTO((GC_PTR * /* link */));
726 /* Link should point to a field of a heap allocated */
727 /* object obj. *link will be cleared when obj is */
728 /* found to be inaccessible. This happens BEFORE any */
729 /* finalization code is invoked, and BEFORE any */
730 /* decisions about finalization order are made. */
731 /* This is useful in telling the finalizer that */
732 /* some pointers are not essential for proper */
733 /* finalization. This may avoid finalization cycles. */
734 /* Note that obj may be resurrected by another */
735 /* finalizer, and thus the clearing of *link may */
736 /* be visible to non-finalization code. */
737 /* There's an argument that an arbitrary action should */
738 /* be allowed here, instead of just clearing a pointer. */
739 /* But this causes problems if that action alters, or */
740 /* examines connectivity. */
741 /* Returns 1 if link was already registered, 0 */
742 /* otherwise. */
743 /* Only exists for backward compatibility. See below: */
745 GC_API int GC_general_register_disappearing_link
746 GC_PROTO((GC_PTR * /* link */, GC_PTR obj));
747 /* A slight generalization of the above. *link is */
748 /* cleared when obj first becomes inaccessible. This */
749 /* can be used to implement weak pointers easily and */
750 /* safely. Typically link will point to a location */
751 /* holding a disguised pointer to obj. (A pointer */
752 /* inside an "atomic" object is effectively */
753 /* disguised.) In this way soft */
754 /* pointers are broken before any object */
755 /* reachable from them are finalized. Each link */
756 /* May be registered only once, i.e. with one obj */
757 /* value. This was added after a long email discussion */
758 /* with John Ellis. */
759 /* Obj must be a pointer to the first word of an object */
760 /* we allocated. It is unsafe to explicitly deallocate */
761 /* the object containing link. Explicitly deallocating */
762 /* obj may or may not cause link to eventually be */
763 /* cleared. */
764 GC_API int GC_unregister_disappearing_link GC_PROTO((GC_PTR * /* link */));
765 /* Returns 0 if link was not actually registered. */
766 /* Undoes a registration by either of the above two */
767 /* routines. */
769 /* Returns !=0 if GC_invoke_finalizers has something to do. */
770 GC_API int GC_should_invoke_finalizers GC_PROTO((void));
772 GC_API int GC_invoke_finalizers GC_PROTO((void));
773 /* Run finalizers for all objects that are ready to */
774 /* be finalized. Return the number of finalizers */
775 /* that were run. Normally this is also called */
776 /* implicitly during some allocations. If */
777 /* GC-finalize_on_demand is nonzero, it must be called */
778 /* explicitly. */
780 /* GC_set_warn_proc can be used to redirect or filter warning messages. */
781 /* p may not be a NULL pointer. */
782 typedef void (*GC_warn_proc) GC_PROTO((char *msg, GC_word arg));
783 GC_API GC_warn_proc GC_set_warn_proc GC_PROTO((GC_warn_proc p));
784 /* Returns old warning procedure. */
786 GC_API GC_word GC_set_free_space_divisor GC_PROTO((GC_word value));
787 /* Set free_space_divisor. See above for definition. */
788 /* Returns old value. */
790 /* The following is intended to be used by a higher level */
791 /* (e.g. Java-like) finalization facility. It is expected */
792 /* that finalization code will arrange for hidden pointers to */
793 /* disappear. Otherwise objects can be accessed after they */
794 /* have been collected. */
795 /* Note that putting pointers in atomic objects or in */
796 /* nonpointer slots of "typed" objects is equivalent to */
797 /* disguising them in this way, and may have other advantages. */
798 # if defined(I_HIDE_POINTERS) || defined(GC_I_HIDE_POINTERS)
799 typedef GC_word GC_hidden_pointer;
800 # define HIDE_POINTER(p) (~(GC_hidden_pointer)(p))
801 # define REVEAL_POINTER(p) ((GC_PTR)(HIDE_POINTER(p)))
802 /* Converting a hidden pointer to a real pointer requires verifying */
803 /* that the object still exists. This involves acquiring the */
804 /* allocator lock to avoid a race with the collector. */
805 # endif /* I_HIDE_POINTERS */
807 typedef GC_PTR (*GC_fn_type) GC_PROTO((GC_PTR client_data));
808 GC_API GC_PTR GC_call_with_alloc_lock
809 GC_PROTO((GC_fn_type fn, GC_PTR client_data));
811 /* The following routines are primarily intended for use with a */
812 /* preprocessor which inserts calls to check C pointer arithmetic. */
813 /* They indicate failure by invoking the corresponding _print_proc. */
815 /* Check that p and q point to the same object. */
816 /* Fail conspicuously if they don't. */
817 /* Returns the first argument. */
818 /* Succeeds if neither p nor q points to the heap. */
819 /* May succeed if both p and q point to between heap objects. */
820 GC_API GC_PTR GC_same_obj GC_PROTO((GC_PTR p, GC_PTR q));
822 /* Checked pointer pre- and post- increment operations. Note that */
823 /* the second argument is in units of bytes, not multiples of the */
824 /* object size. This should either be invoked from a macro, or the */
825 /* call should be automatically generated. */
826 GC_API GC_PTR GC_pre_incr GC_PROTO((GC_PTR *p, size_t how_much));
827 GC_API GC_PTR GC_post_incr GC_PROTO((GC_PTR *p, size_t how_much));
829 /* Check that p is visible */
830 /* to the collector as a possibly pointer containing location. */
831 /* If it isn't fail conspicuously. */
832 /* Returns the argument in all cases. May erroneously succeed */
833 /* in hard cases. (This is intended for debugging use with */
834 /* untyped allocations. The idea is that it should be possible, though */
835 /* slow, to add such a call to all indirect pointer stores.) */
836 /* Currently useless for multithreaded worlds. */
837 GC_API GC_PTR GC_is_visible GC_PROTO((GC_PTR p));
839 /* Check that if p is a pointer to a heap page, then it points to */
840 /* a valid displacement within a heap object. */
841 /* Fail conspicuously if this property does not hold. */
842 /* Uninteresting with GC_all_interior_pointers. */
843 /* Always returns its argument. */
844 GC_API GC_PTR GC_is_valid_displacement GC_PROTO((GC_PTR p));
846 /* Safer, but slow, pointer addition. Probably useful mainly with */
847 /* a preprocessor. Useful only for heap pointers. */
848 #ifdef GC_DEBUG
849 # define GC_PTR_ADD3(x, n, type_of_result) \
850 ((type_of_result)GC_same_obj((x)+(n), (x)))
851 # define GC_PRE_INCR3(x, n, type_of_result) \
852 ((type_of_result)GC_pre_incr(&(x), (n)*sizeof(*x))
853 # define GC_POST_INCR2(x, type_of_result) \
854 ((type_of_result)GC_post_incr(&(x), sizeof(*x))
855 # ifdef __GNUC__
856 # define GC_PTR_ADD(x, n) \
857 GC_PTR_ADD3(x, n, typeof(x))
858 # define GC_PRE_INCR(x, n) \
859 GC_PRE_INCR3(x, n, typeof(x))
860 # define GC_POST_INCR(x, n) \
861 GC_POST_INCR3(x, typeof(x))
862 # else
863 /* We can't do this right without typeof, which ANSI */
864 /* decided was not sufficiently useful. Repeatedly */
865 /* mentioning the arguments seems too dangerous to be */
866 /* useful. So does not casting the result. */
867 # define GC_PTR_ADD(x, n) ((x)+(n))
868 # endif
869 #else /* !GC_DEBUG */
870 # define GC_PTR_ADD3(x, n, type_of_result) ((x)+(n))
871 # define GC_PTR_ADD(x, n) ((x)+(n))
872 # define GC_PRE_INCR3(x, n, type_of_result) ((x) += (n))
873 # define GC_PRE_INCR(x, n) ((x) += (n))
874 # define GC_POST_INCR2(x, n, type_of_result) ((x)++)
875 # define GC_POST_INCR(x, n) ((x)++)
876 #endif
878 /* Safer assignment of a pointer to a nonstack location. */
879 #ifdef GC_DEBUG
880 # ifdef __STDC__
881 # define GC_PTR_STORE(p, q) \
882 (*(void **)GC_is_visible(p) = GC_is_valid_displacement(q))
883 # else
884 # define GC_PTR_STORE(p, q) \
885 (*(char **)GC_is_visible(p) = GC_is_valid_displacement(q))
886 # endif
887 #else /* !GC_DEBUG */
888 # define GC_PTR_STORE(p, q) *((p) = (q))
889 #endif
891 /* Functions called to report pointer checking errors */
892 GC_API void (*GC_same_obj_print_proc) GC_PROTO((GC_PTR p, GC_PTR q));
894 GC_API void (*GC_is_valid_displacement_print_proc)
895 GC_PROTO((GC_PTR p));
897 GC_API void (*GC_is_visible_print_proc)
898 GC_PROTO((GC_PTR p));
901 /* For pthread support, we generally need to intercept a number of */
902 /* thread library calls. We do that here by macro defining them. */
904 #if !defined(GC_USE_LD_WRAP) && \
905 (defined(GC_PTHREADS) || defined(GC_SOLARIS_THREADS))
906 # include "gc_pthread_redirects.h"
907 #endif
909 # if defined(PCR) || defined(GC_SOLARIS_THREADS) || \
910 defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
911 /* Any flavor of threads except SRC_M3. */
912 /* This returns a list of objects, linked through their first */
913 /* word. Its use can greatly reduce lock contention problems, since */
914 /* the allocation lock can be acquired and released many fewer times. */
915 /* lb must be large enough to hold the pointer field. */
916 /* It is used internally by gc_local_alloc.h, which provides a simpler */
917 /* programming interface on Linux. */
918 GC_PTR GC_malloc_many(size_t lb);
919 #define GC_NEXT(p) (*(GC_PTR *)(p)) /* Retrieve the next element */
920 /* in returned list. */
921 extern void GC_thr_init(); /* Needed for Solaris/X86 */
923 #endif /* THREADS && !SRC_M3 */
925 #if defined(GC_WIN32_THREADS) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)
926 # include <windows.h>
929 * All threads must be created using GC_CreateThread, so that they will be
930 * recorded in the thread table. For backwards compatibility, this is not
931 * technically true if the GC is built as a dynamic library, since it can
932 * and does then use DllMain to keep track of thread creations. But new code
933 * should be built to call GC_CreateThread.
935 GC_API HANDLE WINAPI GC_CreateThread(
936 LPSECURITY_ATTRIBUTES lpThreadAttributes,
937 DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress,
938 LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId );
940 # if defined(_WIN32_WCE)
942 * win32_threads.c implements the real WinMain, which will start a new thread
943 * to call GC_WinMain after initializing the garbage collector.
945 int WINAPI GC_WinMain(
946 HINSTANCE hInstance,
947 HINSTANCE hPrevInstance,
948 LPWSTR lpCmdLine,
949 int nCmdShow );
951 # ifndef GC_BUILD
952 # define WinMain GC_WinMain
953 # define CreateThread GC_CreateThread
954 # endif
955 # endif /* defined(_WIN32_WCE) */
957 #endif /* defined(GC_WIN32_THREADS) && !cygwin */
960 * Fully portable code should call GC_INIT() from the main program
961 * before making any other GC_ calls. On most platforms this is a
962 * no-op and the collector self-initializes. But a number of platforms
963 * make that too hard.
965 #if (defined(sparc) || defined(__sparc)) && defined(sun)
967 * If you are planning on putting
968 * the collector in a SunOS 5 dynamic library, you need to call GC_INIT()
969 * from the statically loaded program section.
970 * This circumvents a Solaris 2.X (X<=4) linker bug.
972 # define GC_INIT() { extern end, etext; \
973 GC_noop(&end, &etext); }
974 #else
975 # if defined(__CYGWIN32__) && defined(GC_DLL) || defined (_AIX)
977 * Similarly gnu-win32 DLLs need explicit initialization from
978 * the main program, as does AIX.
980 # define GC_INIT() { GC_add_roots(DATASTART, DATAEND); }
981 # else
982 # if defined(__APPLE__) && defined(__MACH__) || defined(GC_WIN32_THREADS)
983 # define GC_INIT() { GC_init(); }
984 # else
985 # define GC_INIT()
986 # endif /* !__MACH && !GC_WIN32_THREADS */
987 # endif /* !AIX && !cygwin */
988 #endif /* !sparc */
990 #if !defined(_WIN32_WCE) \
991 && ((defined(_MSDOS) || defined(_MSC_VER)) && (_M_IX86 >= 300) \
992 || defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__))
993 /* win32S may not free all resources on process exit. */
994 /* This explicitly deallocates the heap. */
995 GC_API void GC_win32_free_heap ();
996 #endif
998 #if ( defined(_AMIGA) && !defined(GC_AMIGA_MAKINGLIB) )
999 /* Allocation really goes through GC_amiga_allocwrapper_do */
1000 # include "gc_amiga_redirects.h"
1001 #endif
1003 #if defined(GC_REDIRECT_TO_LOCAL) && !defined(GC_LOCAL_ALLOC_H)
1004 # include "gc_local_alloc.h"
1005 #endif
1007 #ifdef __cplusplus
1008 } /* end of extern "C" */
1009 #endif
1011 #endif /* _GC_H */