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
33 #if defined(_SOLARIS_PTHREADS) && !defined(SOLARIS_THREADS)
34 # define SOLARIS_THREADS
38 * Some tests for old macros. These violate our namespace rules and will
41 #if defined(SOLARIS_THREADS) || defined(_SOLARIS_THREADS)
42 # define GC_SOLARIS_THREADS
44 #if defined(_SOLARIS_PTHREADS)
45 # define GC_SOLARIS_PTHREADS
47 #if defined(IRIX_THREADS)
48 # define GC_IRIX_THREADS
50 #if defined(HPUX_THREADS)
51 # define GC_HPUX_THREADS
53 #if defined(OSF1_THREADS)
54 # define GC_OSF1_THREADS
56 #if defined(LINUX_THREADS)
57 # define GC_LINUX_THREADS
59 #if defined(WIN32_THREADS)
60 # define GC_WIN32_THREADS
62 #if defined(USE_LD_WRAP)
63 # define GC_USE_LD_WRAP
66 #if !defined(_REENTRANT) && (defined(GC_SOLARIS_THREADS) \
67 || defined(GC_SOLARIS_PTHREADS) \
68 || defined(GC_HPUX_THREADS) \
69 || defined(GC_LINUX_THREADS))
71 /* Better late than never. This fails if system headers that */
72 /* depend on this were previously included. */
78 /* Yet more kluges for WinCE */
79 # include <stdlib.h> /* size_t is defined here */
80 typedef long ptrdiff_t; /* ptrdiff_t is not defined */
83 #if defined(__CYGWIN32__) && defined(GC_USE_DLL)
84 #include "libgc_globals.h"
87 #if defined(__MINGW32__) && defined(WIN32_THREADS)
89 # define GC_API __declspec(dllexport)
91 # define GC_API __declspec(dllimport)
95 #if defined(_MSC_VER) && (defined(_DLL) && !defined(NOT_GC_DLL) \
98 # define GC_API extern __declspec(dllexport)
100 # define GC_API __declspec(dllimport)
104 #if defined(__WATCOMC__) && defined(GC_DLL)
106 # define GC_API extern __declspec(dllexport)
108 # define GC_API extern __declspec(dllimport)
113 #define GC_API extern
116 # if defined(__STDC__) || defined(__cplusplus)
117 # define GC_PROTO(args) args
118 typedef void * GC_PTR;
119 # define GC_CONST const
121 # define GC_PROTO(args) ()
122 typedef char * GC_PTR;
131 /* Define word and signed_word to be unsigned and signed types of the */
132 /* size as char * or void *. There seems to be no way to do this */
133 /* even semi-portably. The following is probably no better/worse */
134 /* than almost anything else. */
135 /* The ANSI standard suggests that size_t and ptr_diff_t might be */
136 /* better choices. But those appear to have incorrect definitions */
137 /* on may systems. Notably "typedef int size_t" seems to be both */
138 /* frequent and WRONG. */
139 typedef unsigned long GC_word;
140 typedef long GC_signed_word;
142 /* Public read-only variables */
144 GC_API GC_word GC_gc_no;/* Counter incremented per collection. */
145 /* Includes empty GCs at startup. */
147 GC_API int GC_parallel; /* GC is parallelized for performance on */
148 /* multiprocessors. Currently set only */
149 /* implicitly if collector is built with */
150 /* -DPARALLEL_MARK and if either: */
151 /* Env variable GC_NPROC is set to > 1, or */
152 /* GC_NPROC is not set and this is an MP. */
153 /* If GC_parallel is set, incremental */
154 /* collection is aonly partially functional, */
155 /* and may not be desirable. */
158 /* Public R/W variables */
160 GC_API GC_PTR (*GC_oom_fn) GC_PROTO((size_t bytes_requested));
161 /* When there is insufficient memory to satisfy */
162 /* an allocation request, we return */
163 /* (*GC_oom_fn)(). By default this just */
165 /* If it returns, it must return 0 or a valid */
166 /* pointer to a previously allocated heap */
169 GC_API int GC_find_leak;
170 /* Do not actually garbage collect, but simply */
171 /* report inaccessible memory that was not */
172 /* deallocated with GC_free. Initial value */
173 /* is determined by FIND_LEAK macro. */
175 GC_API int GC_all_interior_pointers;
176 /* Arrange for pointers to object interiors to */
177 /* be recognized as valid. May not be changed */
178 /* after GC initialization. */
179 /* Initial value is determined by */
180 /* -DALL_INTERIOR_POINTERS. */
181 /* Unless DONT_ADD_BYTE_AT_END is defined, this */
182 /* also affects whether sizes are increased by */
183 /* at least a byte to allow "off the end" */
184 /* pointer recognition. */
185 /* MUST BE 0 or 1. */
187 GC_API int GC_quiet; /* Disable statistics output. Only matters if */
188 /* collector has been compiled with statistics */
189 /* enabled. This involves a performance cost, */
190 /* and is thus not the default. */
192 GC_API int GC_finalize_on_demand;
193 /* If nonzero, finalizers will only be run in */
194 /* response to an explicit GC_invoke_finalizers */
195 /* call. The default is determined by whether */
196 /* the FINALIZE_ON_DEMAND macro is defined */
197 /* when the collector is built. */
199 GC_API int GC_java_finalization;
200 /* Mark objects reachable from finalizable */
201 /* objects in a separate postpass. This makes */
202 /* it a bit safer to use non-topologically- */
203 /* ordered finalization. Default value is */
204 /* determined by JAVA_FINALIZATION macro. */
206 GC_API void (* GC_finalizer_notifier)();
207 /* Invoked by the collector when there are */
208 /* objects to be finalized. Invoked at most */
209 /* once per GC cycle. Never invoked unless */
210 /* GC_finalize_on_demand is set. */
211 /* Typically this will notify a finalization */
212 /* thread, which will call GC_invoke_finalizers */
215 GC_API int GC_dont_gc; /* Dont collect unless explicitly requested, e.g. */
216 /* because it's not safe. */
218 GC_API int GC_dont_expand;
219 /* Dont expand heap unless explicitly requested */
222 GC_API int GC_use_entire_heap;
223 /* Causes the nonincremental collector to use the */
224 /* entire heap before collecting. This was the only */
225 /* option for GC versions < 5.0. This sometimes */
226 /* results in more large block fragmentation, since */
227 /* very larg blocks will tend to get broken up */
228 /* during each GC cycle. It is likely to result in a */
229 /* larger working set, but lower collection */
230 /* frequencies, and hence fewer instructions executed */
231 /* in the collector. */
233 GC_API int GC_full_freq; /* Number of partial collections between */
234 /* full collections. Matters only if */
235 /* GC_incremental is set. */
236 /* Full collections are also triggered if */
237 /* the collector detects a substantial */
238 /* increase in the number of in-use heap */
239 /* blocks. Values in the tens are now */
240 /* perfectly reasonable, unlike for */
241 /* earlier GC versions. */
243 GC_API GC_word GC_non_gc_bytes;
244 /* Bytes not considered candidates for collection. */
245 /* Used only to control scheduling of collections. */
246 /* Updated by GC_malloc_uncollectable and GC_free. */
249 GC_API int GC_no_dls;
250 /* Don't register dynamic library data segments. */
251 /* Wizards only. Should be used only if the */
252 /* application explicitly registers all roots. */
253 /* In Microsoft Windows environments, this will */
254 /* usually also prevent registration of the */
255 /* main data segment as part of the root set. */
257 GC_API GC_word GC_free_space_divisor;
258 /* We try to make sure that we allocate at */
259 /* least N/GC_free_space_divisor bytes between */
260 /* collections, where N is the heap size plus */
261 /* a rough estimate of the root set size. */
262 /* Initially, GC_free_space_divisor = 4. */
263 /* Increasing its value will use less space */
264 /* but more collection time. Decreasing it */
265 /* will appreciably decrease collection time */
266 /* at the expense of space. */
267 /* GC_free_space_divisor = 1 will effectively */
268 /* disable collections. */
270 GC_API GC_word GC_max_retries;
271 /* The maximum number of GCs attempted before */
272 /* reporting out of memory after heap */
273 /* expansion fails. Initially 0. */
276 GC_API char *GC_stackbottom; /* Cool end of user stack. */
277 /* May be set in the client prior to */
278 /* calling any GC_ routines. This */
279 /* avoids some overhead, and */
280 /* potentially some signals that can */
281 /* confuse debuggers. Otherwise the */
282 /* collector attempts to set it */
284 /* For multithreaded code, this is the */
285 /* cold end of the stack for the */
286 /* primordial thread. */
288 GC_API int GC_dont_precollect; /* Don't collect as part of */
289 /* initialization. Should be set only */
290 /* if the client wants a chance to */
291 /* manually initialize the root set */
292 /* before the first collection. */
293 /* Interferes with blacklisting. */
296 /* Public procedures */
298 * general purpose allocation routines, with roughly malloc calling conv.
299 * The atomic versions promise that no relevant pointers are contained
300 * in the object. The nonatomic versions guarantee that the new object
301 * is cleared. GC_malloc_stubborn promises that no changes to the object
302 * will occur after GC_end_stubborn_change has been called on the
303 * result of GC_malloc_stubborn. GC_malloc_uncollectable allocates an object
304 * that is scanned for pointers to collectable objects, but is not itself
305 * collectable. The object is scanned even if it does not appear to
306 * be reachable. GC_malloc_uncollectable and GC_free called on the resulting
307 * object implicitly update GC_non_gc_bytes appropriately.
309 * Note that the GC_malloc_stubborn support is stubbed out by default
310 * starting in 6.0. GC_malloc_stubborn is an alias for GC_malloc unless
311 * the collector is built with STUBBORN_ALLOC defined.
313 GC_API GC_PTR GC_malloc GC_PROTO((size_t size_in_bytes));
314 GC_API GC_PTR GC_malloc_atomic GC_PROTO((size_t size_in_bytes));
315 GC_API GC_PTR GC_malloc_uncollectable GC_PROTO((size_t size_in_bytes));
316 GC_API GC_PTR GC_malloc_stubborn GC_PROTO((size_t size_in_bytes));
318 /* The following is only defined if the library has been suitably */
320 GC_API GC_PTR GC_malloc_atomic_uncollectable GC_PROTO((size_t size_in_bytes));
322 /* Explicitly deallocate an object. Dangerous if used incorrectly. */
323 /* Requires a pointer to the base of an object. */
324 /* If the argument is stubborn, it should not be changeable when freed. */
325 /* An object should not be enable for finalization when it is */
326 /* explicitly deallocated. */
327 /* GC_free(0) is a no-op, as required by ANSI C for free. */
328 GC_API void GC_free GC_PROTO((GC_PTR object_addr));
331 * Stubborn objects may be changed only if the collector is explicitly informed.
332 * The collector is implicitly informed of coming change when such
333 * an object is first allocated. The following routines inform the
334 * collector that an object will no longer be changed, or that it will
335 * once again be changed. Only nonNIL pointer stores into the object
336 * are considered to be changes. The argument to GC_end_stubborn_change
337 * must be exacly the value returned by GC_malloc_stubborn or passed to
338 * GC_change_stubborn. (In the second case it may be an interior pointer
339 * within 512 bytes of the beginning of the objects.)
340 * There is a performance penalty for allowing more than
341 * one stubborn object to be changed at once, but it is acceptable to
342 * do so. The same applies to dropping stubborn objects that are still
345 GC_API void GC_change_stubborn GC_PROTO((GC_PTR));
346 GC_API void GC_end_stubborn_change GC_PROTO((GC_PTR));
348 /* Return a pointer to the base (lowest address) of an object given */
349 /* a pointer to a location within the object. */
350 /* Return 0 if displaced_pointer doesn't point to within a valid */
352 GC_API GC_PTR GC_base GC_PROTO((GC_PTR displaced_pointer));
354 /* Given a pointer to the base of an object, return its size in bytes. */
355 /* The returned size may be slightly larger than what was originally */
357 GC_API size_t GC_size GC_PROTO((GC_PTR object_addr));
359 /* For compatibility with C library. This is occasionally faster than */
360 /* a malloc followed by a bcopy. But if you rely on that, either here */
361 /* or with the standard C library, your code is broken. In my */
362 /* opinion, it shouldn't have been invented, but now we're stuck. -HB */
363 /* The resulting object has the same kind as the original. */
364 /* If the argument is stubborn, the result will have changes enabled. */
365 /* It is an error to have changes enabled for the original object. */
366 /* Follows ANSI comventions for NULL old_object. */
367 GC_API GC_PTR GC_realloc
368 GC_PROTO((GC_PTR old_object, size_t new_size_in_bytes));
370 /* Explicitly increase the heap size. */
371 /* Returns 0 on failure, 1 on success. */
372 GC_API int GC_expand_hp GC_PROTO((size_t number_of_bytes));
374 /* Limit the heap size to n bytes. Useful when you're debugging, */
375 /* especially on systems that don't handle running out of memory well. */
376 /* n == 0 ==> unbounded. This is the default. */
377 GC_API void GC_set_max_heap_size GC_PROTO((GC_word n));
379 /* Inform the collector that a certain section of statically allocated */
380 /* memory contains no pointers to garbage collected memory. Thus it */
381 /* need not be scanned. This is sometimes important if the application */
382 /* maps large read/write files into the address space, which could be */
383 /* mistaken for dynamic library data segments on some systems. */
384 GC_API void GC_exclude_static_roots GC_PROTO((GC_PTR start, GC_PTR finish));
386 /* Clear the set of root segments. Wizards only. */
387 GC_API void GC_clear_roots GC_PROTO((void));
389 /* Add a root segment. Wizards only. */
390 GC_API void GC_add_roots GC_PROTO((char * low_address,
391 char * high_address_plus_1));
393 /* Add a displacement to the set of those considered valid by the */
394 /* collector. GC_register_displacement(n) means that if p was returned */
395 /* by GC_malloc, then (char *)p + n will be considered to be a valid */
396 /* pointer to n. N must be small and less than the size of p. */
397 /* (All pointers to the interior of objects from the stack are */
398 /* considered valid in any case. This applies to heap objects and */
400 /* Preferably, this should be called before any other GC procedures. */
401 /* Calling it later adds to the probability of excess memory */
403 /* This is a no-op if the collector was compiled with recognition of */
404 /* arbitrary interior pointers enabled, which is now the default. */
405 GC_API void GC_register_displacement GC_PROTO((GC_word n));
407 /* The following version should be used if any debugging allocation is */
409 GC_API void GC_debug_register_displacement GC_PROTO((GC_word n));
411 /* Explicitly trigger a full, world-stop collection. */
412 GC_API void GC_gcollect GC_PROTO((void));
414 /* Trigger a full world-stopped collection. Abort the collection if */
415 /* and when stop_func returns a nonzero value. Stop_func will be */
416 /* called frequently, and should be reasonably fast. This works even */
417 /* if virtual dirty bits, and hence incremental collection is not */
418 /* available for this architecture. Collections can be aborted faster */
419 /* than normal pause times for incremental collection. However, */
420 /* aborted collections do no useful work; the next collection needs */
421 /* to start from the beginning. */
422 /* Return 0 if the collection was aborted, 1 if it succeeded. */
423 typedef int (* GC_stop_func) GC_PROTO((void));
424 GC_API int GC_try_to_collect GC_PROTO((GC_stop_func stop_func));
426 /* Return the number of bytes in the heap. Excludes collector private */
427 /* data structures. Includes empty blocks and fragmentation loss. */
428 /* Includes some pages that were allocated but never written. */
429 GC_API size_t GC_get_heap_size GC_PROTO((void));
431 /* Return a lower bound on the number of free bytes in the heap. */
432 GC_API size_t GC_get_free_bytes GC_PROTO((void));
434 /* Return the number of bytes allocated since the last collection. */
435 GC_API size_t GC_get_bytes_since_gc GC_PROTO((void));
437 /* Return the total number of bytes allocated in this process. */
438 /* Never decreases. */
439 GC_API size_t GC_get_total_bytes GC_PROTO((void));
441 /* Enable incremental/generational collection. */
442 /* Not advisable unless dirty bits are */
443 /* available or most heap objects are */
444 /* pointerfree(atomic) or immutable. */
445 /* Don't use in leak finding mode. */
446 /* Ignored if GC_dont_gc is true. */
447 /* Only the generational piece of this is */
448 /* functional if GC_parallel is TRUE. */
449 GC_API void GC_enable_incremental GC_PROTO((void));
451 /* Perform some garbage collection work, if appropriate. */
452 /* Return 0 if there is no more work to be done. */
453 /* Typically performs an amount of work corresponding roughly */
454 /* to marking from one page. May do more work if further */
455 /* progress requires it, e.g. if incremental collection is */
456 /* disabled. It is reasonable to call this in a wait loop */
457 /* until it returns 0. */
458 GC_API int GC_collect_a_little GC_PROTO((void));
460 /* Allocate an object of size lb bytes. The client guarantees that */
461 /* as long as the object is live, it will be referenced by a pointer */
462 /* that points to somewhere within the first 256 bytes of the object. */
463 /* (This should normally be declared volatile to prevent the compiler */
464 /* from invalidating this assertion.) This routine is only useful */
465 /* if a large array is being allocated. It reduces the chance of */
466 /* accidentally retaining such an array as a result of scanning an */
467 /* integer that happens to be an address inside the array. (Actually, */
468 /* it reduces the chance of the allocator not finding space for such */
469 /* an array, since it will try hard to avoid introducing such a false */
470 /* reference.) On a SunOS 4.X or MS Windows system this is recommended */
471 /* for arrays likely to be larger than 100K or so. For other systems, */
472 /* or if the collector is not configured to recognize all interior */
473 /* pointers, the threshold is normally much higher. */
474 GC_API GC_PTR GC_malloc_ignore_off_page GC_PROTO((size_t lb));
475 GC_API GC_PTR GC_malloc_atomic_ignore_off_page GC_PROTO((size_t lb));
477 #if defined(__sgi) && !defined(__GNUC__) && _COMPILER_VERSION >= 720
478 # define GC_ADD_CALLER
479 # define GC_RETURN_ADDR (GC_word)__return_address
483 # define GC_EXTRAS GC_RETURN_ADDR, __FILE__, __LINE__
484 # define GC_EXTRA_PARAMS GC_word ra, GC_CONST char * s, int i
486 # define GC_EXTRAS __FILE__, __LINE__
487 # define GC_EXTRA_PARAMS GC_CONST char * s, int i
490 /* Debugging (annotated) allocation. GC_gcollect will check */
491 /* objects allocated in this way for overwrites, etc. */
492 GC_API GC_PTR GC_debug_malloc
493 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
494 GC_API GC_PTR GC_debug_malloc_atomic
495 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
496 GC_API GC_PTR GC_debug_malloc_uncollectable
497 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
498 GC_API GC_PTR GC_debug_malloc_stubborn
499 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
500 GC_API void GC_debug_free GC_PROTO((GC_PTR object_addr));
501 GC_API GC_PTR GC_debug_realloc
502 GC_PROTO((GC_PTR old_object, size_t new_size_in_bytes,
505 GC_API void GC_debug_change_stubborn GC_PROTO((GC_PTR));
506 GC_API void GC_debug_end_stubborn_change GC_PROTO((GC_PTR));
508 # define GC_MALLOC(sz) GC_debug_malloc(sz, GC_EXTRAS)
509 # define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, GC_EXTRAS)
510 # define GC_MALLOC_UNCOLLECTABLE(sz) GC_debug_malloc_uncollectable(sz, \
512 # define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, GC_EXTRAS)
513 # define GC_FREE(p) GC_debug_free(p)
514 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
515 GC_debug_register_finalizer(p, f, d, of, od)
516 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
517 GC_debug_register_finalizer_ignore_self(p, f, d, of, od)
518 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
519 GC_debug_register_finalizer_no_order(p, f, d, of, od)
520 # define GC_MALLOC_STUBBORN(sz) GC_debug_malloc_stubborn(sz, GC_EXTRAS);
521 # define GC_CHANGE_STUBBORN(p) GC_debug_change_stubborn(p)
522 # define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p)
523 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
524 GC_general_register_disappearing_link(link, GC_base(obj))
525 # define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n)
527 # define GC_MALLOC(sz) GC_malloc(sz)
528 # define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz)
529 # define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz)
530 # define GC_REALLOC(old, sz) GC_realloc(old, sz)
531 # define GC_FREE(p) GC_free(p)
532 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
533 GC_register_finalizer(p, f, d, of, od)
534 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
535 GC_register_finalizer_ignore_self(p, f, d, of, od)
536 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
537 GC_register_finalizer_no_order(p, f, d, of, od)
538 # define GC_MALLOC_STUBBORN(sz) GC_malloc_stubborn(sz)
539 # define GC_CHANGE_STUBBORN(p) GC_change_stubborn(p)
540 # define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p)
541 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
542 GC_general_register_disappearing_link(link, obj)
543 # define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n)
545 /* The following are included because they are often convenient, and */
546 /* reduce the chance for a misspecifed size argument. But calls may */
547 /* expand to something syntactically incorrect if t is a complicated */
548 /* type expression. */
549 # define GC_NEW(t) (t *)GC_MALLOC(sizeof (t))
550 # define GC_NEW_ATOMIC(t) (t *)GC_MALLOC_ATOMIC(sizeof (t))
551 # define GC_NEW_STUBBORN(t) (t *)GC_MALLOC_STUBBORN(sizeof (t))
552 # define GC_NEW_UNCOLLECTABLE(t) (t *)GC_MALLOC_UNCOLLECTABLE(sizeof (t))
554 /* Finalization. Some of these primitives are grossly unsafe. */
555 /* The idea is to make them both cheap, and sufficient to build */
556 /* a safer layer, closer to PCedar finalization. */
557 /* The interface represents my conclusions from a long discussion */
558 /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes, */
559 /* Christian Jacobi, and Russ Atkinson. It's not perfect, and */
560 /* probably nobody else agrees with it. Hans-J. Boehm 3/13/92 */
561 typedef void (*GC_finalization_proc)
562 GC_PROTO((GC_PTR obj, GC_PTR client_data));
564 GC_API void GC_register_finalizer
565 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
566 GC_finalization_proc *ofn, GC_PTR *ocd));
567 GC_API void GC_debug_register_finalizer
568 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
569 GC_finalization_proc *ofn, GC_PTR *ocd));
570 /* When obj is no longer accessible, invoke */
571 /* (*fn)(obj, cd). If a and b are inaccessible, and */
572 /* a points to b (after disappearing links have been */
573 /* made to disappear), then only a will be */
574 /* finalized. (If this does not create any new */
575 /* pointers to b, then b will be finalized after the */
576 /* next collection.) Any finalizable object that */
577 /* is reachable from itself by following one or more */
578 /* pointers will not be finalized (or collected). */
579 /* Thus cycles involving finalizable objects should */
580 /* be avoided, or broken by disappearing links. */
581 /* All but the last finalizer registered for an object */
583 /* Finalization may be removed by passing 0 as fn. */
584 /* Finalizers are implicitly unregistered just before */
585 /* they are invoked. */
586 /* The old finalizer and client data are stored in */
588 /* Fn is never invoked on an accessible object, */
589 /* provided hidden pointers are converted to real */
590 /* pointers only if the allocation lock is held, and */
591 /* such conversions are not performed by finalization */
593 /* If GC_register_finalizer is aborted as a result of */
594 /* a signal, the object may be left with no */
595 /* finalization, even if neither the old nor new */
596 /* finalizer were NULL. */
597 /* Obj should be the nonNULL starting address of an */
598 /* object allocated by GC_malloc or friends. */
599 /* Note that any garbage collectable object referenced */
600 /* by cd will be considered accessible until the */
601 /* finalizer is invoked. */
603 /* Another versions of the above follow. It ignores */
604 /* self-cycles, i.e. pointers from a finalizable object to */
605 /* itself. There is a stylistic argument that this is wrong, */
606 /* but it's unavoidable for C++, since the compiler may */
607 /* silently introduce these. It's also benign in that specific */
609 /* Note that cd will still be viewed as accessible, even if it */
610 /* refers to the object itself. */
611 GC_API void GC_register_finalizer_ignore_self
612 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
613 GC_finalization_proc *ofn, GC_PTR *ocd));
614 GC_API void GC_debug_register_finalizer_ignore_self
615 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
616 GC_finalization_proc *ofn, GC_PTR *ocd));
618 /* Another version of the above. It ignores all cycles. */
619 /* It should probably only be used by Java implementations. */
620 /* Note that cd will still be viewed as accessible, even if it */
621 /* refers to the object itself. */
622 GC_API void GC_register_finalizer_no_order
623 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
624 GC_finalization_proc *ofn, GC_PTR *ocd));
625 GC_API void GC_debug_register_finalizer_no_order
626 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
627 GC_finalization_proc *ofn, GC_PTR *ocd));
630 /* The following routine may be used to break cycles between */
631 /* finalizable objects, thus causing cyclic finalizable */
632 /* objects to be finalized in the correct order. Standard */
633 /* use involves calling GC_register_disappearing_link(&p), */
634 /* where p is a pointer that is not followed by finalization */
635 /* code, and should not be considered in determining */
636 /* finalization order. */
637 GC_API int GC_register_disappearing_link GC_PROTO((GC_PTR * /* link */));
638 /* Link should point to a field of a heap allocated */
639 /* object obj. *link will be cleared when obj is */
640 /* found to be inaccessible. This happens BEFORE any */
641 /* finalization code is invoked, and BEFORE any */
642 /* decisions about finalization order are made. */
643 /* This is useful in telling the finalizer that */
644 /* some pointers are not essential for proper */
645 /* finalization. This may avoid finalization cycles. */
646 /* Note that obj may be resurrected by another */
647 /* finalizer, and thus the clearing of *link may */
648 /* be visible to non-finalization code. */
649 /* There's an argument that an arbitrary action should */
650 /* be allowed here, instead of just clearing a pointer. */
651 /* But this causes problems if that action alters, or */
652 /* examines connectivity. */
653 /* Returns 1 if link was already registered, 0 */
655 /* Only exists for backward compatibility. See below: */
657 GC_API int GC_general_register_disappearing_link
658 GC_PROTO((GC_PTR * /* link */, GC_PTR obj));
659 /* A slight generalization of the above. *link is */
660 /* cleared when obj first becomes inaccessible. This */
661 /* can be used to implement weak pointers easily and */
662 /* safely. Typically link will point to a location */
663 /* holding a disguised pointer to obj. (A pointer */
664 /* inside an "atomic" object is effectively */
665 /* disguised.) In this way soft */
666 /* pointers are broken before any object */
667 /* reachable from them are finalized. Each link */
668 /* May be registered only once, i.e. with one obj */
669 /* value. This was added after a long email discussion */
670 /* with John Ellis. */
671 /* Obj must be a pointer to the first word of an object */
672 /* we allocated. It is unsafe to explicitly deallocate */
673 /* the object containing link. Explicitly deallocating */
674 /* obj may or may not cause link to eventually be */
676 GC_API int GC_unregister_disappearing_link GC_PROTO((GC_PTR * /* link */));
677 /* Returns 0 if link was not actually registered. */
678 /* Undoes a registration by either of the above two */
681 /* Auxiliary fns to make finalization work correctly with displaced */
682 /* pointers introduced by the debugging allocators. */
683 GC_API GC_PTR GC_make_closure GC_PROTO((GC_finalization_proc fn, GC_PTR data));
684 GC_API void GC_debug_invoke_finalizer GC_PROTO((GC_PTR obj, GC_PTR data));
686 /* Returns !=0 if GC_invoke_finalizers has something to do. */
687 GC_API int GC_should_invoke_finalizers GC_PROTO((void));
689 GC_API int GC_invoke_finalizers GC_PROTO((void));
690 /* Run finalizers for all objects that are ready to */
691 /* be finalized. Return the number of finalizers */
692 /* that were run. Normally this is also called */
693 /* implicitly during some allocations. If */
694 /* GC-finalize_on_demand is nonzero, it must be called */
697 /* GC_set_warn_proc can be used to redirect or filter warning messages. */
698 /* p may not be a NULL pointer. */
699 typedef void (*GC_warn_proc) GC_PROTO((char *msg, GC_word arg));
700 GC_API GC_warn_proc GC_set_warn_proc GC_PROTO((GC_warn_proc p));
701 /* Returns old warning procedure. */
703 /* The following is intended to be used by a higher level */
704 /* (e.g. cedar-like) finalization facility. It is expected */
705 /* that finalization code will arrange for hidden pointers to */
706 /* disappear. Otherwise objects can be accessed after they */
707 /* have been collected. */
708 /* Note that putting pointers in atomic objects or in */
709 /* nonpointer slots of "typed" objects is equivalent to */
710 /* disguising them in this way, and may have other advantages. */
711 # if defined(I_HIDE_POINTERS) || defined(GC_I_HIDE_POINTERS)
712 typedef GC_word GC_hidden_pointer;
713 # define HIDE_POINTER(p) (~(GC_hidden_pointer)(p))
714 # define REVEAL_POINTER(p) ((GC_PTR)(HIDE_POINTER(p)))
715 /* Converting a hidden pointer to a real pointer requires verifying */
716 /* that the object still exists. This involves acquiring the */
717 /* allocator lock to avoid a race with the collector. */
718 # endif /* I_HIDE_POINTERS */
720 typedef GC_PTR (*GC_fn_type) GC_PROTO((GC_PTR client_data));
721 GC_API GC_PTR GC_call_with_alloc_lock
722 GC_PROTO((GC_fn_type fn, GC_PTR client_data));
724 /* The following routines are primarily intended for use with a */
725 /* preprocessor which inserts calls to check C pointer arithmetic. */
727 /* Check that p and q point to the same object. */
728 /* Fail conspicuously if they don't. */
729 /* Returns the first argument. */
730 /* Succeeds if neither p nor q points to the heap. */
731 /* May succeed if both p and q point to between heap objects. */
732 GC_API GC_PTR GC_same_obj GC_PROTO((GC_PTR p, GC_PTR q));
734 /* Checked pointer pre- and post- increment operations. Note that */
735 /* the second argument is in units of bytes, not multiples of the */
736 /* object size. This should either be invoked from a macro, or the */
737 /* call should be automatically generated. */
738 GC_API GC_PTR GC_pre_incr GC_PROTO((GC_PTR *p, size_t how_much));
739 GC_API GC_PTR GC_post_incr GC_PROTO((GC_PTR *p, size_t how_much));
741 /* Check that p is visible */
742 /* to the collector as a possibly pointer containing location. */
743 /* If it isn't fail conspicuously. */
744 /* Returns the argument in all cases. May erroneously succeed */
745 /* in hard cases. (This is intended for debugging use with */
746 /* untyped allocations. The idea is that it should be possible, though */
747 /* slow, to add such a call to all indirect pointer stores.) */
748 /* Currently useless for multithreaded worlds. */
749 GC_API GC_PTR GC_is_visible GC_PROTO((GC_PTR p));
751 /* Check that if p is a pointer to a heap page, then it points to */
752 /* a valid displacement within a heap object. */
753 /* Fail conspicuously if this property does not hold. */
754 /* Uninteresting with GC_all_interior_pointers. */
755 /* Always returns its argument. */
756 GC_API GC_PTR GC_is_valid_displacement GC_PROTO((GC_PTR p));
758 /* Safer, but slow, pointer addition. Probably useful mainly with */
759 /* a preprocessor. Useful only for heap pointers. */
761 # define GC_PTR_ADD3(x, n, type_of_result) \
762 ((type_of_result)GC_same_obj((x)+(n), (x)))
763 # define GC_PRE_INCR3(x, n, type_of_result) \
764 ((type_of_result)GC_pre_incr(&(x), (n)*sizeof(*x))
765 # define GC_POST_INCR2(x, type_of_result) \
766 ((type_of_result)GC_post_incr(&(x), sizeof(*x))
768 # define GC_PTR_ADD(x, n) \
769 GC_PTR_ADD3(x, n, typeof(x))
770 # define GC_PRE_INCR(x, n) \
771 GC_PRE_INCR3(x, n, typeof(x))
772 # define GC_POST_INCR(x, n) \
773 GC_POST_INCR3(x, typeof(x))
775 /* We can't do this right without typeof, which ANSI */
776 /* decided was not sufficiently useful. Repeatedly */
777 /* mentioning the arguments seems too dangerous to be */
778 /* useful. So does not casting the result. */
779 # define GC_PTR_ADD(x, n) ((x)+(n))
781 #else /* !GC_DEBUG */
782 # define GC_PTR_ADD3(x, n, type_of_result) ((x)+(n))
783 # define GC_PTR_ADD(x, n) ((x)+(n))
784 # define GC_PRE_INCR3(x, n, type_of_result) ((x) += (n))
785 # define GC_PRE_INCR(x, n) ((x) += (n))
786 # define GC_POST_INCR2(x, n, type_of_result) ((x)++)
787 # define GC_POST_INCR(x, n) ((x)++)
790 /* Safer assignment of a pointer to a nonstack location. */
793 # define GC_PTR_STORE(p, q) \
794 (*(void **)GC_is_visible(p) = GC_is_valid_displacement(q))
796 # define GC_PTR_STORE(p, q) \
797 (*(char **)GC_is_visible(p) = GC_is_valid_displacement(q))
799 #else /* !GC_DEBUG */
800 # define GC_PTR_STORE(p, q) *((p) = (q))
803 /* Fynctions called to report pointer checking errors */
804 GC_API void (*GC_same_obj_print_proc) GC_PROTO((GC_PTR p, GC_PTR q));
806 GC_API void (*GC_is_valid_displacement_print_proc)
807 GC_PROTO((GC_PTR p));
809 GC_API void (*GC_is_visible_print_proc)
810 GC_PROTO((GC_PTR p));
813 /* For pthread support, we generally need to intercept a number of */
814 /* thread library calls. We do that here by macro defining them. */
816 #if !defined(GC_USE_LD_WRAP) && \
817 (defined(GC_LINUX_THREADS) || defined(GC_HPUX_THREADS) || \
818 defined(GC_IRIX_THREADS) || defined(GC_SOLARIS_PTHREADS) || \
819 defined(GC_SOLARIS_THREADS) || defined(GC_OSF1_THREADS))
820 # include "gc_pthread_redirects.h"
823 # if defined(PCR) || defined(GC_SOLARIS_THREADS) || \
824 defined(GC_SOLARIS_PTHREADS) || defined(GC_WIN32_THREADS) || \
825 defined(GC_IRIX_THREADS) || defined(GC_LINUX_THREADS) || \
826 defined(GC_HPUX_THREADS)
827 /* Any flavor of threads except SRC_M3. */
828 /* This returns a list of objects, linked through their first */
829 /* word. Its use can greatly reduce lock contention problems, since */
830 /* the allocation lock can be acquired and released many fewer times. */
831 /* lb must be large enough to hold the pointer field. */
832 /* It is used internally by gc_local_alloc.h, which provides a simpler */
833 /* programming interface on Linux. */
834 GC_PTR GC_malloc_many(size_t lb);
835 #define GC_NEXT(p) (*(GC_PTR *)(p)) /* Retrieve the next element */
836 /* in returned list. */
837 extern void GC_thr_init(); /* Needed for Solaris/X86 */
839 #endif /* THREADS && !SRC_M3 */
841 #if defined(WIN32_THREADS) && defined(_WIN32_WCE)
842 # include <windows.h>
845 * win32_threads.c implements the real WinMain, which will start a new thread
846 * to call GC_WinMain after initializing the garbage collector.
848 int WINAPI GC_WinMain(
850 HINSTANCE hPrevInstance,
855 * All threads must be created using GC_CreateThread, so that they will be
856 * recorded in the thread table.
858 HANDLE WINAPI GC_CreateThread(
859 LPSECURITY_ATTRIBUTES lpThreadAttributes,
860 DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress,
861 LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId );
864 # define WinMain GC_WinMain
865 # define CreateThread GC_CreateThread
871 * If you are planning on putting
872 * the collector in a SunOS 5 dynamic library, you need to call GC_INIT()
873 * from the statically loaded program section.
874 * This circumvents a Solaris 2.X (X<=4) linker bug.
876 #if defined(sparc) || defined(__sparc)
877 # define GC_INIT() { extern end, etext; \
878 GC_noop(&end, &etext); }
880 # if defined(__CYGWIN32__) && defined(GC_USE_DLL)
882 * Similarly gnu-win32 DLLs need explicit initialization
884 # define GC_INIT() { GC_add_roots(DATASTART, DATAEND); }
890 #if !defined(_WIN32_WCE) \
891 && ((defined(_MSDOS) || defined(_MSC_VER)) && (_M_IX86 >= 300) \
892 || defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__))
893 /* win32S may not free all resources on process exit. */
894 /* This explicitly deallocates the heap. */
895 GC_API void GC_win32_free_heap ();
898 #if ( defined(_AMIGA) && !defined(GC_AMIGA_MAKINGLIB) )
899 /* Allocation really goes through GC_amiga_allocwrapper_do */
900 # include "gc_amiga_redirects.h"
903 #if defined(GC_REDIRECT_TO_LOCAL) && !defined(GC_LOCAL_ALLOC_H)
904 # include "gc_local_alloc.h"
908 } /* end of extern "C" */