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