* gccbug.in: Add libgcj and preprocessor categories.
[official-gcc.git] / boehm-gc / gc.h
blobd7cd8996085bff60fa60408aff0662f7e054b821
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
32 # define __GC
33 # include <stddef.h>
35 #if defined(__CYGWIN32__) && defined(GC_USE_DLL)
36 #include "libgc_globals.h"
37 #endif
39 #if defined(__MINGW32__) && defined(WIN32_THREADS)
40 # ifdef GC_BUILD
41 # define GC_API __declspec(dllexport)
42 # else
43 # define GC_API __declspec(dllimport)
44 # endif
45 #endif
47 #if defined(_MSC_VER) && defined(_DLL)
48 # ifdef GC_BUILD
49 # define GC_API __declspec(dllexport)
50 # else
51 # define GC_API __declspec(dllimport)
52 # endif
53 #endif
55 #if defined(__WATCOMC__) && defined(GC_DLL)
56 # ifdef GC_BUILD
57 # define GC_API extern __declspec(dllexport)
58 # else
59 # define GC_API extern __declspec(dllimport)
60 # endif
61 #endif
63 #ifndef GC_API
64 #define GC_API extern
65 #endif
67 # if defined(__STDC__) || defined(__cplusplus)
68 # define GC_PROTO(args) args
69 typedef void * GC_PTR;
70 # define GC_CONST const
71 # else
72 # define GC_PROTO(args) ()
73 typedef char * GC_PTR;
74 # define GC_CONST
75 # endif
77 # ifdef __cplusplus
78 extern "C" {
79 # endif
82 /* Define word and signed_word to be unsigned and signed types of the */
83 /* size as char * or void *. There seems to be no way to do this */
84 /* even semi-portably. The following is probably no better/worse */
85 /* than almost anything else. */
86 /* The ANSI standard suggests that size_t and ptr_diff_t might be */
87 /* better choices. But those appear to have incorrect definitions */
88 /* on may systems. Notably "typedef int size_t" seems to be both */
89 /* frequent and WRONG. */
90 typedef unsigned long GC_word;
91 typedef long GC_signed_word;
93 /* Public read-only variables */
95 GC_API GC_word GC_gc_no;/* Counter incremented per collection. */
96 /* Includes empty GCs at startup. */
99 /* Public R/W variables */
101 GC_API GC_PTR (*GC_oom_fn) GC_PROTO((size_t bytes_requested));
102 /* When there is insufficient memory to satisfy */
103 /* an allocation request, we return */
104 /* (*GC_oom_fn)(). By default this just */
105 /* returns 0. */
106 /* If it returns, it must return 0 or a valid */
107 /* pointer to a previously allocated heap */
108 /* object. */
110 GC_API int GC_find_leak;
111 /* Do not actually garbage collect, but simply */
112 /* report inaccessible memory that was not */
113 /* deallocated with GC_free. Initial value */
114 /* is determined by FIND_LEAK macro. */
116 GC_API int GC_quiet; /* Disable statistics output. Only matters if */
117 /* collector has been compiled with statistics */
118 /* enabled. This involves a performance cost, */
119 /* and is thus not the default. */
121 GC_API int GC_finalize_on_demand;
122 /* If nonzero, finalizers will only be run in */
123 /* response to an eplit GC_invoke_finalizers */
124 /* call. The default is determined by whether */
125 /* the FINALIZE_ON_DEMAND macro is defined */
126 /* when the collector is built. */
128 GC_API int GC_java_finalization;
129 /* Mark objects reachable from finalizable */
130 /* objects in a separate postpass. This makes */
131 /* it a bit safer to use non-topologically- */
132 /* ordered finalization. Default value is */
133 /* determined by JAVA_FINALIZATION macro. */
135 GC_API int GC_dont_gc; /* Dont collect unless explicitly requested, e.g. */
136 /* because it's not safe. */
138 GC_API int GC_dont_expand;
139 /* Dont expand heap unless explicitly requested */
140 /* or forced to. */
142 GC_API int GC_use_entire_heap;
143 /* Causes the nonincremental collector to use the */
144 /* entire heap before collecting. This was the only */
145 /* option for GC versions < 5.0. This sometimes */
146 /* results in more large block fragmentation, since */
147 /* very larg blocks will tend to get broken up */
148 /* during each GC cycle. It is likely to result in a */
149 /* larger working set, but lower collection */
150 /* frequencies, and hence fewer instructions executed */
151 /* in the collector. */
153 GC_API int GC_full_freq; /* Number of partial collections between */
154 /* full collections. Matters only if */
155 /* GC_incremental is set. */
156 /* Full collections are also triggered if */
157 /* the collector detects a substantial */
158 /* increase in the number of in-use heap */
159 /* blocks. Values in the tens are now */
160 /* perfectly reasonable, unlike for */
161 /* earlier GC versions. */
163 GC_API GC_word GC_non_gc_bytes;
164 /* Bytes not considered candidates for collection. */
165 /* Used only to control scheduling of collections. */
167 GC_API GC_word GC_free_space_divisor;
168 /* We try to make sure that we allocate at */
169 /* least N/GC_free_space_divisor bytes between */
170 /* collections, where N is the heap size plus */
171 /* a rough estimate of the root set size. */
172 /* Initially, GC_free_space_divisor = 4. */
173 /* Increasing its value will use less space */
174 /* but more collection time. Decreasing it */
175 /* will appreciably decrease collection time */
176 /* at the expense of space. */
177 /* GC_free_space_divisor = 1 will effectively */
178 /* disable collections. */
180 GC_API GC_word GC_max_retries;
181 /* The maximum number of GCs attempted before */
182 /* reporting out of memory after heap */
183 /* expansion fails. Initially 0. */
186 GC_API char *GC_stackbottom; /* Cool end of user stack. */
187 /* May be set in the client prior to */
188 /* calling any GC_ routines. This */
189 /* avoids some overhead, and */
190 /* potentially some signals that can */
191 /* confuse debuggers. Otherwise the */
192 /* collector attempts to set it */
193 /* automatically. */
194 /* For multithreaded code, this is the */
195 /* cold end of the stack for the */
196 /* primordial thread. */
198 /* Public procedures */
200 * general purpose allocation routines, with roughly malloc calling conv.
201 * The atomic versions promise that no relevant pointers are contained
202 * in the object. The nonatomic versions guarantee that the new object
203 * is cleared. GC_malloc_stubborn promises that no changes to the object
204 * will occur after GC_end_stubborn_change has been called on the
205 * result of GC_malloc_stubborn. GC_malloc_uncollectable allocates an object
206 * that is scanned for pointers to collectable objects, but is not itself
207 * collectable. GC_malloc_uncollectable and GC_free called on the resulting
208 * object implicitly update GC_non_gc_bytes appropriately.
210 GC_API GC_PTR GC_malloc GC_PROTO((size_t size_in_bytes));
211 GC_API GC_PTR GC_malloc_atomic GC_PROTO((size_t size_in_bytes));
212 GC_API GC_PTR GC_malloc_uncollectable GC_PROTO((size_t size_in_bytes));
213 GC_API GC_PTR GC_malloc_stubborn GC_PROTO((size_t size_in_bytes));
215 /* The following is only defined if the library has been suitably */
216 /* compiled: */
217 GC_API GC_PTR GC_malloc_atomic_uncollectable GC_PROTO((size_t size_in_bytes));
219 /* Explicitly deallocate an object. Dangerous if used incorrectly. */
220 /* Requires a pointer to the base of an object. */
221 /* If the argument is stubborn, it should not be changeable when freed. */
222 /* An object should not be enable for finalization when it is */
223 /* explicitly deallocated. */
224 /* GC_free(0) is a no-op, as required by ANSI C for free. */
225 GC_API void GC_free GC_PROTO((GC_PTR object_addr));
228 * Stubborn objects may be changed only if the collector is explicitly informed.
229 * The collector is implicitly informed of coming change when such
230 * an object is first allocated. The following routines inform the
231 * collector that an object will no longer be changed, or that it will
232 * once again be changed. Only nonNIL pointer stores into the object
233 * are considered to be changes. The argument to GC_end_stubborn_change
234 * must be exacly the value returned by GC_malloc_stubborn or passed to
235 * GC_change_stubborn. (In the second case it may be an interior pointer
236 * within 512 bytes of the beginning of the objects.)
237 * There is a performance penalty for allowing more than
238 * one stubborn object to be changed at once, but it is acceptable to
239 * do so. The same applies to dropping stubborn objects that are still
240 * changeable.
242 GC_API void GC_change_stubborn GC_PROTO((GC_PTR));
243 GC_API void GC_end_stubborn_change GC_PROTO((GC_PTR));
245 /* Return a pointer to the base (lowest address) of an object given */
246 /* a pointer to a location within the object. */
247 /* Return 0 if displaced_pointer doesn't point to within a valid */
248 /* object. */
249 GC_API GC_PTR GC_base GC_PROTO((GC_PTR displaced_pointer));
251 /* Given a pointer to the base of an object, return its size in bytes. */
252 /* The returned size may be slightly larger than what was originally */
253 /* requested. */
254 GC_API size_t GC_size GC_PROTO((GC_PTR object_addr));
256 /* For compatibility with C library. This is occasionally faster than */
257 /* a malloc followed by a bcopy. But if you rely on that, either here */
258 /* or with the standard C library, your code is broken. In my */
259 /* opinion, it shouldn't have been invented, but now we're stuck. -HB */
260 /* The resulting object has the same kind as the original. */
261 /* If the argument is stubborn, the result will have changes enabled. */
262 /* It is an error to have changes enabled for the original object. */
263 /* Follows ANSI comventions for NULL old_object. */
264 GC_API GC_PTR GC_realloc
265 GC_PROTO((GC_PTR old_object, size_t new_size_in_bytes));
267 /* Explicitly increase the heap size. */
268 /* Returns 0 on failure, 1 on success. */
269 GC_API int GC_expand_hp GC_PROTO((size_t number_of_bytes));
271 /* Limit the heap size to n bytes. Useful when you're debugging, */
272 /* especially on systems that don't handle running out of memory well. */
273 /* n == 0 ==> unbounded. This is the default. */
274 GC_API void GC_set_max_heap_size GC_PROTO((GC_word n));
276 /* Inform the collector that a certain section of statically allocated */
277 /* memory contains no pointers to garbage collected memory. Thus it */
278 /* need not be scanned. This is sometimes important if the application */
279 /* maps large read/write files into the address space, which could be */
280 /* mistaken for dynamic library data segments on some systems. */
281 GC_API void GC_exclude_static_roots GC_PROTO((GC_PTR start, GC_PTR finish));
283 /* Clear the set of root segments. Wizards only. */
284 GC_API void GC_clear_roots GC_PROTO((void));
286 /* Add a root segment. Wizards only. */
287 GC_API void GC_add_roots GC_PROTO((char * low_address,
288 char * high_address_plus_1));
290 /* Add a displacement to the set of those considered valid by the */
291 /* collector. GC_register_displacement(n) means that if p was returned */
292 /* by GC_malloc, then (char *)p + n will be considered to be a valid */
293 /* pointer to n. N must be small and less than the size of p. */
294 /* (All pointers to the interior of objects from the stack are */
295 /* considered valid in any case. This applies to heap objects and */
296 /* static data.) */
297 /* Preferably, this should be called before any other GC procedures. */
298 /* Calling it later adds to the probability of excess memory */
299 /* retention. */
300 /* This is a no-op if the collector was compiled with recognition of */
301 /* arbitrary interior pointers enabled, which is now the default. */
302 GC_API void GC_register_displacement GC_PROTO((GC_word n));
304 /* The following version should be used if any debugging allocation is */
305 /* being done. */
306 GC_API void GC_debug_register_displacement GC_PROTO((GC_word n));
308 /* Explicitly trigger a full, world-stop collection. */
309 GC_API void GC_gcollect GC_PROTO((void));
311 /* Trigger a full world-stopped collection. Abort the collection if */
312 /* and when stop_func returns a nonzero value. Stop_func will be */
313 /* called frequently, and should be reasonably fast. This works even */
314 /* if virtual dirty bits, and hence incremental collection is not */
315 /* available for this architecture. Collections can be aborted faster */
316 /* than normal pause times for incremental collection. However, */
317 /* aborted collections do no useful work; the next collection needs */
318 /* to start from the beginning. */
319 /* Return 0 if the collection was aborted, 1 if it succeeded. */
320 typedef int (* GC_stop_func) GC_PROTO((void));
321 GC_API int GC_try_to_collect GC_PROTO((GC_stop_func stop_func));
323 /* Return the number of bytes in the heap. Excludes collector private */
324 /* data structures. Includes empty blocks and fragmentation loss. */
325 /* Includes some pages that were allocated but never written. */
326 GC_API size_t GC_get_heap_size GC_PROTO((void));
328 /* Return a lower bound on the number of free bytes in the heap. */
329 GC_API size_t GC_get_free_bytes GC_PROTO((void));
331 /* Return the number of bytes allocated since the last collection. */
332 GC_API size_t GC_get_bytes_since_gc GC_PROTO((void));
334 /* Enable incremental/generational collection. */
335 /* Not advisable unless dirty bits are */
336 /* available or most heap objects are */
337 /* pointerfree(atomic) or immutable. */
338 /* Don't use in leak finding mode. */
339 /* Ignored if GC_dont_gc is true. */
340 GC_API void GC_enable_incremental GC_PROTO((void));
342 /* Perform some garbage collection work, if appropriate. */
343 /* Return 0 if there is no more work to be done. */
344 /* Typically performs an amount of work corresponding roughly */
345 /* to marking from one page. May do more work if further */
346 /* progress requires it, e.g. if incremental collection is */
347 /* disabled. It is reasonable to call this in a wait loop */
348 /* until it returns 0. */
349 GC_API int GC_collect_a_little GC_PROTO((void));
351 /* Allocate an object of size lb bytes. The client guarantees that */
352 /* as long as the object is live, it will be referenced by a pointer */
353 /* that points to somewhere within the first 256 bytes of the object. */
354 /* (This should normally be declared volatile to prevent the compiler */
355 /* from invalidating this assertion.) This routine is only useful */
356 /* if a large array is being allocated. It reduces the chance of */
357 /* accidentally retaining such an array as a result of scanning an */
358 /* integer that happens to be an address inside the array. (Actually, */
359 /* it reduces the chance of the allocator not finding space for such */
360 /* an array, since it will try hard to avoid introducing such a false */
361 /* reference.) On a SunOS 4.X or MS Windows system this is recommended */
362 /* for arrays likely to be larger than 100K or so. For other systems, */
363 /* or if the collector is not configured to recognize all interior */
364 /* pointers, the threshold is normally much higher. */
365 GC_API GC_PTR GC_malloc_ignore_off_page GC_PROTO((size_t lb));
366 GC_API GC_PTR GC_malloc_atomic_ignore_off_page GC_PROTO((size_t lb));
368 #if defined(__sgi) && !defined(__GNUC__) && _COMPILER_VERSION >= 720
369 # define GC_ADD_CALLER
370 # define GC_RETURN_ADDR (GC_word)__return_address
371 #endif
373 #ifdef GC_ADD_CALLER
374 # define GC_EXTRAS GC_RETURN_ADDR, __FILE__, __LINE__
375 # define GC_EXTRA_PARAMS GC_word ra, GC_CONST char * s, int i
376 #else
377 # define GC_EXTRAS __FILE__, __LINE__
378 # define GC_EXTRA_PARAMS GC_CONST char * s, int i
379 #endif
381 /* Debugging (annotated) allocation. GC_gcollect will check */
382 /* objects allocated in this way for overwrites, etc. */
383 GC_API GC_PTR GC_debug_malloc
384 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
385 GC_API GC_PTR GC_debug_malloc_atomic
386 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
387 GC_API GC_PTR GC_debug_malloc_uncollectable
388 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
389 GC_API GC_PTR GC_debug_malloc_stubborn
390 GC_PROTO((size_t size_in_bytes, GC_EXTRA_PARAMS));
391 GC_API void GC_debug_free GC_PROTO((GC_PTR object_addr));
392 GC_API GC_PTR GC_debug_realloc
393 GC_PROTO((GC_PTR old_object, size_t new_size_in_bytes,
394 GC_EXTRA_PARAMS));
396 GC_API void GC_debug_change_stubborn GC_PROTO((GC_PTR));
397 GC_API void GC_debug_end_stubborn_change GC_PROTO((GC_PTR));
398 # ifdef GC_DEBUG
399 # define GC_MALLOC(sz) GC_debug_malloc(sz, GC_EXTRAS)
400 # define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, GC_EXTRAS)
401 # define GC_MALLOC_UNCOLLECTABLE(sz) GC_debug_malloc_uncollectable(sz, \
402 GC_EXTRAS)
403 # define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, GC_EXTRAS)
404 # define GC_FREE(p) GC_debug_free(p)
405 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
406 GC_debug_register_finalizer(p, f, d, of, od)
407 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
408 GC_debug_register_finalizer_ignore_self(p, f, d, of, od)
409 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
410 GC_debug_register_finalizer_no_order(p, f, d, of, od)
411 # define GC_MALLOC_STUBBORN(sz) GC_debug_malloc_stubborn(sz, GC_EXTRAS);
412 # define GC_CHANGE_STUBBORN(p) GC_debug_change_stubborn(p)
413 # define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p)
414 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
415 GC_general_register_disappearing_link(link, GC_base(obj))
416 # define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n)
417 # else
418 # define GC_MALLOC(sz) GC_malloc(sz)
419 # define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz)
420 # define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz)
421 # define GC_REALLOC(old, sz) GC_realloc(old, sz)
422 # define GC_FREE(p) GC_free(p)
423 # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
424 GC_register_finalizer(p, f, d, of, od)
425 # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
426 GC_register_finalizer_ignore_self(p, f, d, of, od)
427 # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
428 GC_register_finalizer_no_order(p, f, d, of, od)
429 # define GC_MALLOC_STUBBORN(sz) GC_malloc_stubborn(sz)
430 # define GC_CHANGE_STUBBORN(p) GC_change_stubborn(p)
431 # define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p)
432 # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
433 GC_general_register_disappearing_link(link, obj)
434 # define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n)
435 # endif
436 /* The following are included because they are often convenient, and */
437 /* reduce the chance for a misspecifed size argument. But calls may */
438 /* expand to something syntactically incorrect if t is a complicated */
439 /* type expression. */
440 # define GC_NEW(t) (t *)GC_MALLOC(sizeof (t))
441 # define GC_NEW_ATOMIC(t) (t *)GC_MALLOC_ATOMIC(sizeof (t))
442 # define GC_NEW_STUBBORN(t) (t *)GC_MALLOC_STUBBORN(sizeof (t))
443 # define GC_NEW_UNCOLLECTABLE(t) (t *)GC_MALLOC_UNCOLLECTABLE(sizeof (t))
445 /* Finalization. Some of these primitives are grossly unsafe. */
446 /* The idea is to make them both cheap, and sufficient to build */
447 /* a safer layer, closer to PCedar finalization. */
448 /* The interface represents my conclusions from a long discussion */
449 /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes, */
450 /* Christian Jacobi, and Russ Atkinson. It's not perfect, and */
451 /* probably nobody else agrees with it. Hans-J. Boehm 3/13/92 */
452 typedef void (*GC_finalization_proc)
453 GC_PROTO((GC_PTR obj, GC_PTR client_data));
455 GC_API void GC_register_finalizer
456 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
457 GC_finalization_proc *ofn, GC_PTR *ocd));
458 GC_API void GC_debug_register_finalizer
459 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
460 GC_finalization_proc *ofn, GC_PTR *ocd));
461 /* When obj is no longer accessible, invoke */
462 /* (*fn)(obj, cd). If a and b are inaccessible, and */
463 /* a points to b (after disappearing links have been */
464 /* made to disappear), then only a will be */
465 /* finalized. (If this does not create any new */
466 /* pointers to b, then b will be finalized after the */
467 /* next collection.) Any finalizable object that */
468 /* is reachable from itself by following one or more */
469 /* pointers will not be finalized (or collected). */
470 /* Thus cycles involving finalizable objects should */
471 /* be avoided, or broken by disappearing links. */
472 /* All but the last finalizer registered for an object */
473 /* is ignored. */
474 /* Finalization may be removed by passing 0 as fn. */
475 /* Finalizers are implicitly unregistered just before */
476 /* they are invoked. */
477 /* The old finalizer and client data are stored in */
478 /* *ofn and *ocd. */
479 /* Fn is never invoked on an accessible object, */
480 /* provided hidden pointers are converted to real */
481 /* pointers only if the allocation lock is held, and */
482 /* such conversions are not performed by finalization */
483 /* routines. */
484 /* If GC_register_finalizer is aborted as a result of */
485 /* a signal, the object may be left with no */
486 /* finalization, even if neither the old nor new */
487 /* finalizer were NULL. */
488 /* Obj should be the nonNULL starting address of an */
489 /* object allocated by GC_malloc or friends. */
490 /* Note that any garbage collectable object referenced */
491 /* by cd will be considered accessible until the */
492 /* finalizer is invoked. */
494 /* Another versions of the above follow. It ignores */
495 /* self-cycles, i.e. pointers from a finalizable object to */
496 /* itself. There is a stylistic argument that this is wrong, */
497 /* but it's unavoidable for C++, since the compiler may */
498 /* silently introduce these. It's also benign in that specific */
499 /* case. */
500 GC_API void GC_register_finalizer_ignore_self
501 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
502 GC_finalization_proc *ofn, GC_PTR *ocd));
503 GC_API void GC_debug_register_finalizer_ignore_self
504 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
505 GC_finalization_proc *ofn, GC_PTR *ocd));
507 /* Another version of the above. It ignores all cycles. */
508 /* It should probably only be used by Java implementations. */
509 GC_API void GC_register_finalizer_no_order
510 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
511 GC_finalization_proc *ofn, GC_PTR *ocd));
512 GC_API void GC_debug_register_finalizer_no_order
513 GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
514 GC_finalization_proc *ofn, GC_PTR *ocd));
517 /* The following routine may be used to break cycles between */
518 /* finalizable objects, thus causing cyclic finalizable */
519 /* objects to be finalized in the correct order. Standard */
520 /* use involves calling GC_register_disappearing_link(&p), */
521 /* where p is a pointer that is not followed by finalization */
522 /* code, and should not be considered in determining */
523 /* finalization order. */
524 GC_API int GC_register_disappearing_link GC_PROTO((GC_PTR * /* link */));
525 /* Link should point to a field of a heap allocated */
526 /* object obj. *link will be cleared when obj is */
527 /* found to be inaccessible. This happens BEFORE any */
528 /* finalization code is invoked, and BEFORE any */
529 /* decisions about finalization order are made. */
530 /* This is useful in telling the finalizer that */
531 /* some pointers are not essential for proper */
532 /* finalization. This may avoid finalization cycles. */
533 /* Note that obj may be resurrected by another */
534 /* finalizer, and thus the clearing of *link may */
535 /* be visible to non-finalization code. */
536 /* There's an argument that an arbitrary action should */
537 /* be allowed here, instead of just clearing a pointer. */
538 /* But this causes problems if that action alters, or */
539 /* examines connectivity. */
540 /* Returns 1 if link was already registered, 0 */
541 /* otherwise. */
542 /* Only exists for backward compatibility. See below: */
544 GC_API int GC_general_register_disappearing_link
545 GC_PROTO((GC_PTR * /* link */, GC_PTR obj));
546 /* A slight generalization of the above. *link is */
547 /* cleared when obj first becomes inaccessible. This */
548 /* can be used to implement weak pointers easily and */
549 /* safely. Typically link will point to a location */
550 /* holding a disguised pointer to obj. (A pointer */
551 /* inside an "atomic" object is effectively */
552 /* disguised.) In this way soft */
553 /* pointers are broken before any object */
554 /* reachable from them are finalized. Each link */
555 /* May be registered only once, i.e. with one obj */
556 /* value. This was added after a long email discussion */
557 /* with John Ellis. */
558 /* Obj must be a pointer to the first word of an object */
559 /* we allocated. It is unsafe to explicitly deallocate */
560 /* the object containing link. Explicitly deallocating */
561 /* obj may or may not cause link to eventually be */
562 /* cleared. */
563 GC_API int GC_unregister_disappearing_link GC_PROTO((GC_PTR * /* link */));
564 /* Returns 0 if link was not actually registered. */
565 /* Undoes a registration by either of the above two */
566 /* routines. */
568 /* Auxiliary fns to make finalization work correctly with displaced */
569 /* pointers introduced by the debugging allocators. */
570 GC_API GC_PTR GC_make_closure GC_PROTO((GC_finalization_proc fn, GC_PTR data));
571 GC_API void GC_debug_invoke_finalizer GC_PROTO((GC_PTR obj, GC_PTR data));
573 /* Returns !=0 if GC_invoke_finalizers has something to do. */
574 GC_API int GC_should_invoke_finalizers GC_PROTO((void));
576 GC_API int GC_invoke_finalizers GC_PROTO((void));
577 /* Run finalizers for all objects that are ready to */
578 /* be finalized. Return the number of finalizers */
579 /* that were run. Normally this is also called */
580 /* implicitly during some allocations. If */
581 /* GC-finalize_on_demand is nonzero, it must be called */
582 /* explicitly. */
584 /* GC_set_warn_proc can be used to redirect or filter warning messages. */
585 /* p may not be a NULL pointer. */
586 typedef void (*GC_warn_proc) GC_PROTO((char *msg, GC_word arg));
587 GC_API GC_warn_proc GC_set_warn_proc GC_PROTO((GC_warn_proc p));
588 /* Returns old warning procedure. */
590 /* The following is intended to be used by a higher level */
591 /* (e.g. cedar-like) finalization facility. It is expected */
592 /* that finalization code will arrange for hidden pointers to */
593 /* disappear. Otherwise objects can be accessed after they */
594 /* have been collected. */
595 /* Note that putting pointers in atomic objects or in */
596 /* nonpointer slots of "typed" objects is equivalent to */
597 /* disguising them in this way, and may have other advantages. */
598 # if defined(I_HIDE_POINTERS) || defined(GC_I_HIDE_POINTERS)
599 typedef GC_word GC_hidden_pointer;
600 # define HIDE_POINTER(p) (~(GC_hidden_pointer)(p))
601 # define REVEAL_POINTER(p) ((GC_PTR)(HIDE_POINTER(p)))
602 /* Converting a hidden pointer to a real pointer requires verifying */
603 /* that the object still exists. This involves acquiring the */
604 /* allocator lock to avoid a race with the collector. */
605 # endif /* I_HIDE_POINTERS */
607 typedef GC_PTR (*GC_fn_type) GC_PROTO((GC_PTR client_data));
608 GC_API GC_PTR GC_call_with_alloc_lock
609 GC_PROTO((GC_fn_type fn, GC_PTR client_data));
611 /* Check that p and q point to the same object. */
612 /* Fail conspicuously if they don't. */
613 /* Returns the first argument. */
614 /* Succeeds if neither p nor q points to the heap. */
615 /* May succeed if both p and q point to between heap objects. */
616 GC_API GC_PTR GC_same_obj GC_PROTO((GC_PTR p, GC_PTR q));
618 /* Checked pointer pre- and post- increment operations. Note that */
619 /* the second argument is in units of bytes, not multiples of the */
620 /* object size. This should either be invoked from a macro, or the */
621 /* call should be automatically generated. */
622 GC_API GC_PTR GC_pre_incr GC_PROTO((GC_PTR *p, size_t how_much));
623 GC_API GC_PTR GC_post_incr GC_PROTO((GC_PTR *p, size_t how_much));
625 /* Check that p is visible */
626 /* to the collector as a possibly pointer containing location. */
627 /* If it isn't fail conspicuously. */
628 /* Returns the argument in all cases. May erroneously succeed */
629 /* in hard cases. (This is intended for debugging use with */
630 /* untyped allocations. The idea is that it should be possible, though */
631 /* slow, to add such a call to all indirect pointer stores.) */
632 /* Currently useless for multithreaded worlds. */
633 GC_API GC_PTR GC_is_visible GC_PROTO((GC_PTR p));
635 /* Check that if p is a pointer to a heap page, then it points to */
636 /* a valid displacement within a heap object. */
637 /* Fail conspicuously if this property does not hold. */
638 /* Uninteresting with ALL_INTERIOR_POINTERS. */
639 /* Always returns its argument. */
640 GC_API GC_PTR GC_is_valid_displacement GC_PROTO((GC_PTR p));
642 /* Safer, but slow, pointer addition. Probably useful mainly with */
643 /* a preprocessor. Useful only for heap pointers. */
644 #ifdef GC_DEBUG
645 # define GC_PTR_ADD3(x, n, type_of_result) \
646 ((type_of_result)GC_same_obj((x)+(n), (x)))
647 # define GC_PRE_INCR3(x, n, type_of_result) \
648 ((type_of_result)GC_pre_incr(&(x), (n)*sizeof(*x))
649 # define GC_POST_INCR2(x, type_of_result) \
650 ((type_of_result)GC_post_incr(&(x), sizeof(*x))
651 # ifdef __GNUC__
652 # define GC_PTR_ADD(x, n) \
653 GC_PTR_ADD3(x, n, typeof(x))
654 # define GC_PRE_INCR(x, n) \
655 GC_PRE_INCR3(x, n, typeof(x))
656 # define GC_POST_INCR(x, n) \
657 GC_POST_INCR3(x, typeof(x))
658 # else
659 /* We can't do this right without typeof, which ANSI */
660 /* decided was not sufficiently useful. Repeatedly */
661 /* mentioning the arguments seems too dangerous to be */
662 /* useful. So does not casting the result. */
663 # define GC_PTR_ADD(x, n) ((x)+(n))
664 # endif
665 #else /* !GC_DEBUG */
666 # define GC_PTR_ADD3(x, n, type_of_result) ((x)+(n))
667 # define GC_PTR_ADD(x, n) ((x)+(n))
668 # define GC_PRE_INCR3(x, n, type_of_result) ((x) += (n))
669 # define GC_PRE_INCR(x, n) ((x) += (n))
670 # define GC_POST_INCR2(x, n, type_of_result) ((x)++)
671 # define GC_POST_INCR(x, n) ((x)++)
672 #endif
674 /* Safer assignment of a pointer to a nonstack location. */
675 #ifdef GC_DEBUG
676 # ifdef __STDC__
677 # define GC_PTR_STORE(p, q) \
678 (*(void **)GC_is_visible(p) = GC_is_valid_displacement(q))
679 # else
680 # define GC_PTR_STORE(p, q) \
681 (*(char **)GC_is_visible(p) = GC_is_valid_displacement(q))
682 # endif
683 #else /* !GC_DEBUG */
684 # define GC_PTR_STORE(p, q) *((p) = (q))
685 #endif
687 /* Fynctions called to report pointer checking errors */
688 GC_API void (*GC_same_obj_print_proc) GC_PROTO((GC_PTR p, GC_PTR q));
690 GC_API void (*GC_is_valid_displacement_print_proc)
691 GC_PROTO((GC_PTR p));
693 GC_API void (*GC_is_visible_print_proc)
694 GC_PROTO((GC_PTR p));
696 #if defined(_SOLARIS_PTHREADS) && !defined(SOLARIS_THREADS)
697 # define SOLARIS_THREADS
698 #endif
700 #ifdef SOLARIS_THREADS
701 /* We need to intercept calls to many of the threads primitives, so */
702 /* that we can locate thread stacks and stop the world. */
703 /* Note also that the collector cannot see thread specific data. */
704 /* Thread specific data should generally consist of pointers to */
705 /* uncollectable objects, which are deallocated using the destructor */
706 /* facility in thr_keycreate. */
707 # include <thread.h>
708 # include <signal.h>
709 int GC_thr_create(void *stack_base, size_t stack_size,
710 void *(*start_routine)(void *), void *arg, long flags,
711 thread_t *new_thread);
712 int GC_thr_join(thread_t wait_for, thread_t *departed, void **status);
713 int GC_thr_suspend(thread_t target_thread);
714 int GC_thr_continue(thread_t target_thread);
715 void * GC_dlopen(const char *path, int mode);
717 # ifdef _SOLARIS_PTHREADS
718 # include <pthread.h>
719 extern int GC_pthread_create(pthread_t *new_thread,
720 const pthread_attr_t *attr,
721 void * (*thread_execp)(void *), void *arg);
722 extern int GC_pthread_join(pthread_t wait_for, void **status);
724 # undef thread_t
726 # define pthread_join GC_pthread_join
727 # define pthread_create GC_pthread_create
728 #endif
730 # define thr_create GC_thr_create
731 # define thr_join GC_thr_join
732 # define thr_suspend GC_thr_suspend
733 # define thr_continue GC_thr_continue
734 # define dlopen GC_dlopen
736 # endif /* SOLARIS_THREADS */
739 #if !defined(USE_LD_WRAP) && \
740 (defined(IRIX_THREADS) || defined(LINUX_THREADS) || defined(HPUX_THREADS))
741 /* We treat these similarly. */
742 # include <pthread.h>
743 # include <signal.h>
745 int GC_pthread_create(pthread_t *new_thread,
746 const pthread_attr_t *attr,
747 void *(*start_routine)(void *), void *arg);
748 int GC_pthread_sigmask(int how, const sigset_t *set, sigset_t *oset);
749 int GC_pthread_join(pthread_t thread, void **retval);
751 # define pthread_create GC_pthread_create
752 # define pthread_sigmask GC_pthread_sigmask
753 # define pthread_join GC_pthread_join
754 # define dlopen GC_dlopen
756 #endif /* xxxxx_THREADS */
758 # if defined(PCR) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || \
759 defined(IRIX_THREADS) || defined(LINUX_THREADS) || \
760 defined(IRIX_JDK_THREADS) || defined(HPUX_THREADS)
761 /* Any flavor of threads except SRC_M3. */
762 /* This returns a list of objects, linked through their first */
763 /* word. Its use can greatly reduce lock contention problems, since */
764 /* the allocation lock can be acquired and released many fewer times. */
765 /* lb must be large enough to hold the pointer field. */
766 GC_PTR GC_malloc_many(size_t lb);
767 #define GC_NEXT(p) (*(GC_PTR *)(p)) /* Retrieve the next element */
768 /* in returned list. */
769 extern void GC_thr_init(); /* Needed for Solaris/X86 */
771 #endif /* THREADS && !SRC_M3 */
774 * If you are planning on putting
775 * the collector in a SunOS 5 dynamic library, you need to call GC_INIT()
776 * from the statically loaded program section.
777 * This circumvents a Solaris 2.X (X<=4) linker bug.
779 #if defined(sparc) || defined(__sparc)
780 # define GC_INIT() { extern end, etext; \
781 GC_noop(&end, &etext); }
782 #else
783 # if defined(__CYGWIN32__) && defined(GC_USE_DLL)
785 * Similarly gnu-win32 DLLs need explicit initialization
787 # define GC_INIT() { GC_add_roots(DATASTART, DATAEND); }
788 # else
789 # define GC_INIT()
790 # endif
791 #endif
793 #if (defined(_MSDOS) || defined(_MSC_VER)) && (_M_IX86 >= 300) \
794 || defined(_WIN32)
795 /* win32S may not free all resources on process exit. */
796 /* This explicitly deallocates the heap. */
797 GC_API void GC_win32_free_heap ();
798 #endif
800 #ifdef __cplusplus
801 } /* end of extern "C" */
802 #endif
804 #endif /* _GC_H */