home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / SYSTEM / GC / INCLUDE / GC.H < prev   
C/C++ Source or Header  |  1995-01-09  |  27KB  |  620 lines

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