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