OSDN Git Service

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