PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/headers/gc/gc.h

http://github.com/nddrylliog/ooc
C++ Header | 1152 lines | 390 code | 129 blank | 633 comment | 50 complexity | 6dee5f992e726cf54397f9dce632557d MD5 | raw file
  1. /*
  2. * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3. * Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
  4. * Copyright 1996-1999 by Silicon Graphics. All rights reserved.
  5. * Copyright 1999 by Hewlett-Packard Company. All rights reserved.
  6. * Copyright (C) 2007 Free Software Foundation, Inc
  7. *
  8. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  9. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  10. *
  11. * Permission is hereby granted to use or copy this program
  12. * for any purpose, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. */
  17. /*
  18. * Note that this defines a large number of tuning hooks, which can
  19. * safely be ignored in nearly all cases. For normal use it suffices
  20. * to call only GC_MALLOC and perhaps GC_REALLOC.
  21. * For better performance, also look at GC_MALLOC_ATOMIC, and
  22. * GC_enable_incremental. If you need an action to be performed
  23. * immediately before an object is collected, look at GC_register_finalizer.
  24. * If you are using Solaris threads, look at the end of this file.
  25. * Everything else is best ignored unless you encounter performance
  26. * problems.
  27. */
  28. #ifndef _GC_H
  29. # define _GC_H
  30. # include "gc_version.h"
  31. /* Define version numbers here to allow test on build machine */
  32. /* for cross-builds. Note that this defines the header */
  33. /* version number, which may or may not match that of the */
  34. /* dynamic library. The GC_version variable can be used */
  35. /* to obtain the latter. */
  36. # include "gc_config_macros.h"
  37. # ifdef __cplusplus
  38. extern "C" {
  39. # endif
  40. /* Define word and signed_word to be unsigned and signed types of the */
  41. /* size as char * or void *. There seems to be no way to do this */
  42. /* even semi-portably. The following is probably no better/worse */
  43. /* than almost anything else. */
  44. /* The ANSI standard suggests that size_t and ptr_diff_t might be */
  45. /* better choices. But those had incorrect definitions on some older */
  46. /* systems. Notably "typedef int size_t" is WRONG. */
  47. #ifndef _WIN64
  48. typedef unsigned long GC_word;
  49. typedef long GC_signed_word;
  50. #else
  51. /* Win64 isn't really supported yet, but this is the first step. And */
  52. /* it might cause error messages to show up in more plausible places. */
  53. /* This needs basetsd.h, which is included by windows.h. */
  54. typedef unsigned long long GC_word;
  55. typedef long long GC_signed_word;
  56. #endif
  57. /* Public read-only variables */
  58. GC_API GC_word GC_gc_no;/* Counter incremented per collection. */
  59. /* Includes empty GCs at startup. */
  60. GC_API int GC_parallel; /* GC is parallelized for performance on */
  61. /* multiprocessors. Currently set only */
  62. /* implicitly if collector is built with */
  63. /* -DPARALLEL_MARK and if either: */
  64. /* Env variable GC_NPROC is set to > 1, or */
  65. /* GC_NPROC is not set and this is an MP. */
  66. /* If GC_parallel is set, incremental */
  67. /* collection is only partially functional, */
  68. /* and may not be desirable. */
  69. /* Public R/W variables */
  70. GC_API void * (*GC_oom_fn) (size_t bytes_requested);
  71. /* When there is insufficient memory to satisfy */
  72. /* an allocation request, we return */
  73. /* (*GC_oom_fn)(). By default this just */
  74. /* returns 0. */
  75. /* If it returns, it must return 0 or a valid */
  76. /* pointer to a previously allocated heap */
  77. /* object. */
  78. GC_API int GC_find_leak;
  79. /* Do not actually garbage collect, but simply */
  80. /* report inaccessible memory that was not */
  81. /* deallocated with GC_free. Initial value */
  82. /* is determined by FIND_LEAK macro. */
  83. GC_API int GC_all_interior_pointers;
  84. /* Arrange for pointers to object interiors to */
  85. /* be recognized as valid. May not be changed */
  86. /* after GC initialization. */
  87. /* Initial value is determined by */
  88. /* -DALL_INTERIOR_POINTERS. */
  89. /* Unless DONT_ADD_BYTE_AT_END is defined, this */
  90. /* also affects whether sizes are increased by */
  91. /* at least a byte to allow "off the end" */
  92. /* pointer recognition. */
  93. /* MUST BE 0 or 1. */
  94. GC_API int GC_finalize_on_demand;
  95. /* If nonzero, finalizers will only be run in */
  96. /* response to an explicit GC_invoke_finalizers */
  97. /* call. The default is determined by whether */
  98. /* the FINALIZE_ON_DEMAND macro is defined */
  99. /* when the collector is built. */
  100. GC_API int GC_java_finalization;
  101. /* Mark objects reachable from finalizable */
  102. /* objects in a separate postpass. This makes */
  103. /* it a bit safer to use non-topologically- */
  104. /* ordered finalization. Default value is */
  105. /* determined by JAVA_FINALIZATION macro. */
  106. /* Enables register_finalizer_unreachable to */
  107. /* work correctly. */
  108. GC_API void (* GC_finalizer_notifier)(void);
  109. /* Invoked by the collector when there are */
  110. /* objects to be finalized. Invoked at most */
  111. /* once per GC cycle. Never invoked unless */
  112. /* GC_finalize_on_demand is set. */
  113. /* Typically this will notify a finalization */
  114. /* thread, which will call GC_invoke_finalizers */
  115. /* in response. */
  116. GC_API int GC_dont_gc; /* != 0 ==> Dont collect. In versions 6.2a1+, */
  117. /* this overrides explicit GC_gcollect() calls. */
  118. /* Used as a counter, so that nested enabling */
  119. /* and disabling work correctly. Should */
  120. /* normally be updated with GC_enable() and */
  121. /* GC_disable() calls. */
  122. /* Direct assignment to GC_dont_gc is */
  123. /* deprecated. */
  124. GC_API int GC_dont_expand;
  125. /* Dont expand heap unless explicitly requested */
  126. /* or forced to. */
  127. GC_API int GC_use_entire_heap;
  128. /* Causes the nonincremental collector to use the */
  129. /* entire heap before collecting. This was the only */
  130. /* option for GC versions < 5.0. This sometimes */
  131. /* results in more large block fragmentation, since */
  132. /* very larg blocks will tend to get broken up */
  133. /* during each GC cycle. It is likely to result in a */
  134. /* larger working set, but lower collection */
  135. /* frequencies, and hence fewer instructions executed */
  136. /* in the collector. */
  137. GC_API int GC_full_freq; /* Number of partial collections between */
  138. /* full collections. Matters only if */
  139. /* GC_incremental is set. */
  140. /* Full collections are also triggered if */
  141. /* the collector detects a substantial */
  142. /* increase in the number of in-use heap */
  143. /* blocks. Values in the tens are now */
  144. /* perfectly reasonable, unlike for */
  145. /* earlier GC versions. */
  146. GC_API GC_word GC_non_gc_bytes;
  147. /* Bytes not considered candidates for collection. */
  148. /* Used only to control scheduling of collections. */
  149. /* Updated by GC_malloc_uncollectable and GC_free. */
  150. /* Wizards only. */
  151. GC_API int GC_no_dls;
  152. /* Don't register dynamic library data segments. */
  153. /* Wizards only. Should be used only if the */
  154. /* application explicitly registers all roots. */
  155. /* In Microsoft Windows environments, this will */
  156. /* usually also prevent registration of the */
  157. /* main data segment as part of the root set. */
  158. GC_API GC_word GC_free_space_divisor;
  159. /* We try to make sure that we allocate at */
  160. /* least N/GC_free_space_divisor bytes between */
  161. /* collections, where N is twice the number */
  162. /* of traced bytes, plus the number of untraced */
  163. /* bytes (bytes in "atomic" objects), plus */
  164. /* a rough estimate of the root set size. */
  165. /* N approximates GC tracing work per GC. */
  166. /* Initially, GC_free_space_divisor = 3. */
  167. /* Increasing its value will use less space */
  168. /* but more collection time. Decreasing it */
  169. /* will appreciably decrease collection time */
  170. /* at the expense of space. */
  171. GC_API GC_word GC_max_retries;
  172. /* The maximum number of GCs attempted before */
  173. /* reporting out of memory after heap */
  174. /* expansion fails. Initially 0. */
  175. GC_API char *GC_stackbottom; /* Cool end of user stack. */
  176. /* May be set in the client prior to */
  177. /* calling any GC_ routines. This */
  178. /* avoids some overhead, and */
  179. /* potentially some signals that can */
  180. /* confuse debuggers. Otherwise the */
  181. /* collector attempts to set it */
  182. /* automatically. */
  183. /* For multithreaded code, this is the */
  184. /* cold end of the stack for the */
  185. /* primordial thread. */
  186. GC_API int GC_dont_precollect; /* Don't collect as part of */
  187. /* initialization. Should be set only */
  188. /* if the client wants a chance to */
  189. /* manually initialize the root set */
  190. /* before the first collection. */
  191. /* Interferes with blacklisting. */
  192. /* Wizards only. */
  193. GC_API unsigned long GC_time_limit;
  194. /* If incremental collection is enabled, */
  195. /* We try to terminate collections */
  196. /* after this many milliseconds. Not a */
  197. /* hard time bound. Setting this to */
  198. /* GC_TIME_UNLIMITED will essentially */
  199. /* disable incremental collection while */
  200. /* leaving generational collection */
  201. /* enabled. */
  202. # define GC_TIME_UNLIMITED 999999
  203. /* Setting GC_time_limit to this value */
  204. /* will disable the "pause time exceeded"*/
  205. /* tests. */
  206. /* Public procedures */
  207. /* Initialize the collector. Portable clients should call GC_INIT() from
  208. * the main program instead.
  209. */
  210. GC_API void GC_init(void);
  211. /*
  212. * general purpose allocation routines, with roughly malloc calling conv.
  213. * The atomic versions promise that no relevant pointers are contained
  214. * in the object. The nonatomic versions guarantee that the new object
  215. * is cleared. GC_malloc_stubborn promises that no changes to the object
  216. * will occur after GC_end_stubborn_change has been called on the
  217. * result of GC_malloc_stubborn. GC_malloc_uncollectable allocates an object
  218. * that is scanned for pointers to collectable objects, but is not itself
  219. * collectable. The object is scanned even if it does not appear to
  220. * be reachable. GC_malloc_uncollectable and GC_free called on the resulting
  221. * object implicitly update GC_non_gc_bytes appropriately.
  222. *
  223. * Note that the GC_malloc_stubborn support is stubbed out by default
  224. * starting in 6.0. GC_malloc_stubborn is an alias for GC_malloc unless
  225. * the collector is built with STUBBORN_ALLOC defined.
  226. */
  227. GC_API void * GC_malloc(size_t size_in_bytes);
  228. GC_API void * GC_malloc_atomic(size_t size_in_bytes);
  229. GC_API char * GC_strdup (const char *str);
  230. GC_API void * GC_malloc_uncollectable(size_t size_in_bytes);
  231. GC_API void * GC_malloc_stubborn(size_t size_in_bytes);
  232. /* The following is only defined if the library has been suitably */
  233. /* compiled: */
  234. GC_API void * GC_malloc_atomic_uncollectable(size_t size_in_bytes);
  235. /* Explicitly deallocate an object. Dangerous if used incorrectly. */
  236. /* Requires a pointer to the base of an object. */
  237. /* If the argument is stubborn, it should not be changeable when freed. */
  238. /* An object should not be enable for finalization when it is */
  239. /* explicitly deallocated. */
  240. /* GC_free(0) is a no-op, as required by ANSI C for free. */
  241. GC_API void GC_free(void * object_addr);
  242. /*
  243. * Stubborn objects may be changed only if the collector is explicitly informed.
  244. * The collector is implicitly informed of coming change when such
  245. * an object is first allocated. The following routines inform the
  246. * collector that an object will no longer be changed, or that it will
  247. * once again be changed. Only nonNIL pointer stores into the object
  248. * are considered to be changes. The argument to GC_end_stubborn_change
  249. * must be exacly the value returned by GC_malloc_stubborn or passed to
  250. * GC_change_stubborn. (In the second case it may be an interior pointer
  251. * within 512 bytes of the beginning of the objects.)
  252. * There is a performance penalty for allowing more than
  253. * one stubborn object to be changed at once, but it is acceptable to
  254. * do so. The same applies to dropping stubborn objects that are still
  255. * changeable.
  256. */
  257. GC_API void GC_change_stubborn(void *);
  258. GC_API void GC_end_stubborn_change(void *);
  259. /* Return a pointer to the base (lowest address) of an object given */
  260. /* a pointer to a location within the object. */
  261. /* I.e. map an interior pointer to the corresponding bas pointer. */
  262. /* Note that with debugging allocation, this returns a pointer to the */
  263. /* actual base of the object, i.e. the debug information, not to */
  264. /* the base of the user object. */
  265. /* Return 0 if displaced_pointer doesn't point to within a valid */
  266. /* object. */
  267. /* Note that a deallocated object in the garbage collected heap */
  268. /* may be considered valid, even if it has been deallocated with */
  269. /* GC_free. */
  270. GC_API void * GC_base(void * displaced_pointer);
  271. /* Given a pointer to the base of an object, return its size in bytes. */
  272. /* The returned size may be slightly larger than what was originally */
  273. /* requested. */
  274. GC_API size_t GC_size(void * object_addr);
  275. /* For compatibility with C library. This is occasionally faster than */
  276. /* a malloc followed by a bcopy. But if you rely on that, either here */
  277. /* or with the standard C library, your code is broken. In my */
  278. /* opinion, it shouldn't have been invented, but now we're stuck. -HB */
  279. /* The resulting object has the same kind as the original. */
  280. /* If the argument is stubborn, the result will have changes enabled. */
  281. /* It is an error to have changes enabled for the original object. */
  282. /* Follows ANSI comventions for NULL old_object. */
  283. GC_API void * GC_realloc(void * old_object, size_t new_size_in_bytes);
  284. /* Explicitly increase the heap size. */
  285. /* Returns 0 on failure, 1 on success. */
  286. GC_API int GC_expand_hp(size_t number_of_bytes);
  287. /* Limit the heap size to n bytes. Useful when you're debugging, */
  288. /* especially on systems that don't handle running out of memory well. */
  289. /* n == 0 ==> unbounded. This is the default. */
  290. GC_API void GC_set_max_heap_size(GC_word n);
  291. /* Inform the collector that a certain section of statically allocated */
  292. /* memory contains no pointers to garbage collected memory. Thus it */
  293. /* need not be scanned. This is sometimes important if the application */
  294. /* maps large read/write files into the address space, which could be */
  295. /* mistaken for dynamic library data segments on some systems. */
  296. GC_API void GC_exclude_static_roots(void * low_address,
  297. void * high_address_plus_1);
  298. /* Clear the set of root segments. Wizards only. */
  299. GC_API void GC_clear_roots(void);
  300. /* Add a root segment. Wizards only. */
  301. GC_API void GC_add_roots(void * low_address, void * high_address_plus_1);
  302. /* Remove a root segment. Wizards only. */
  303. GC_API void GC_remove_roots(void * low_address, void * high_address_plus_1);
  304. /* Add a displacement to the set of those considered valid by the */
  305. /* collector. GC_register_displacement(n) means that if p was returned */
  306. /* by GC_malloc, then (char *)p + n will be considered to be a valid */
  307. /* pointer to p. N must be small and less than the size of p. */
  308. /* (All pointers to the interior of objects from the stack are */
  309. /* considered valid in any case. This applies to heap objects and */
  310. /* static data.) */
  311. /* Preferably, this should be called before any other GC procedures. */
  312. /* Calling it later adds to the probability of excess memory */
  313. /* retention. */
  314. /* This is a no-op if the collector has recognition of */
  315. /* arbitrary interior pointers enabled, which is now the default. */
  316. GC_API void GC_register_displacement(size_t n);
  317. /* The following version should be used if any debugging allocation is */
  318. /* being done. */
  319. GC_API void GC_debug_register_displacement(size_t n);
  320. /* Explicitly trigger a full, world-stop collection. */
  321. GC_API void GC_gcollect(void);
  322. /* Trigger a full world-stopped collection. Abort the collection if */
  323. /* and when stop_func returns a nonzero value. Stop_func will be */
  324. /* called frequently, and should be reasonably fast. This works even */
  325. /* if virtual dirty bits, and hence incremental collection is not */
  326. /* available for this architecture. Collections can be aborted faster */
  327. /* than normal pause times for incremental collection. However, */
  328. /* aborted collections do no useful work; the next collection needs */
  329. /* to start from the beginning. */
  330. /* Return 0 if the collection was aborted, 1 if it succeeded. */
  331. typedef int (* GC_stop_func)(void);
  332. GC_API int GC_try_to_collect(GC_stop_func stop_func);
  333. /* Return the number of bytes in the heap. Excludes collector private */
  334. /* data structures. Includes empty blocks and fragmentation loss. */
  335. /* Includes some pages that were allocated but never written. */
  336. GC_API size_t GC_get_heap_size(void);
  337. /* Return a lower bound on the number of free bytes in the heap. */
  338. GC_API size_t GC_get_free_bytes(void);
  339. /* Return the number of bytes allocated since the last collection. */
  340. GC_API size_t GC_get_bytes_since_gc(void);
  341. /* Return the total number of bytes allocated in this process. */
  342. /* Never decreases, except due to wrapping. */
  343. GC_API size_t GC_get_total_bytes(void);
  344. /* Disable garbage collection. Even GC_gcollect calls will be */
  345. /* ineffective. */
  346. GC_API void GC_disable(void);
  347. /* Reenable garbage collection. GC_disable() and GC_enable() calls */
  348. /* nest. Garbage collection is enabled if the number of calls to both */
  349. /* both functions is equal. */
  350. GC_API void GC_enable(void);
  351. /* Enable incremental/generational collection. */
  352. /* Not advisable unless dirty bits are */
  353. /* available or most heap objects are */
  354. /* pointerfree(atomic) or immutable. */
  355. /* Don't use in leak finding mode. */
  356. /* Ignored if GC_dont_gc is true. */
  357. /* Only the generational piece of this is */
  358. /* functional if GC_parallel is TRUE */
  359. /* or if GC_time_limit is GC_TIME_UNLIMITED. */
  360. /* Causes GC_local_gcj_malloc() to revert to */
  361. /* locked allocation. Must be called */
  362. /* before any GC_local_gcj_malloc() calls. */
  363. /* For best performance, should be called as early as possible. */
  364. /* On some platforms, calling it later may have adverse effects.*/
  365. /* Safe to call before GC_INIT(). Includes a GC_init() call. */
  366. GC_API void GC_enable_incremental(void);
  367. /* Does incremental mode write-protect pages? Returns zero or */
  368. /* more of the following, or'ed together: */
  369. #define GC_PROTECTS_POINTER_HEAP 1 /* May protect non-atomic objs. */
  370. #define GC_PROTECTS_PTRFREE_HEAP 2
  371. #define GC_PROTECTS_STATIC_DATA 4 /* Currently never. */
  372. #define GC_PROTECTS_STACK 8 /* Probably impractical. */
  373. #define GC_PROTECTS_NONE 0
  374. GC_API int GC_incremental_protection_needs(void);
  375. /* Perform some garbage collection work, if appropriate. */
  376. /* Return 0 if there is no more work to be done. */
  377. /* Typically performs an amount of work corresponding roughly */
  378. /* to marking from one page. May do more work if further */
  379. /* progress requires it, e.g. if incremental collection is */
  380. /* disabled. It is reasonable to call this in a wait loop */
  381. /* until it returns 0. */
  382. GC_API int GC_collect_a_little(void);
  383. /* Allocate an object of size lb bytes. The client guarantees that */
  384. /* as long as the object is live, it will be referenced by a pointer */
  385. /* that points to somewhere within the first 256 bytes of the object. */
  386. /* (This should normally be declared volatile to prevent the compiler */
  387. /* from invalidating this assertion.) This routine is only useful */
  388. /* if a large array is being allocated. It reduces the chance of */
  389. /* accidentally retaining such an array as a result of scanning an */
  390. /* integer that happens to be an address inside the array. (Actually, */
  391. /* it reduces the chance of the allocator not finding space for such */
  392. /* an array, since it will try hard to avoid introducing such a false */
  393. /* reference.) On a SunOS 4.X or MS Windows system this is recommended */
  394. /* for arrays likely to be larger than 100K or so. For other systems, */
  395. /* or if the collector is not configured to recognize all interior */
  396. /* pointers, the threshold is normally much higher. */
  397. GC_API void * GC_malloc_ignore_off_page(size_t lb);
  398. GC_API void * GC_malloc_atomic_ignore_off_page(size_t lb);
  399. #if defined(__sgi) && !defined(__GNUC__) && _COMPILER_VERSION >= 720
  400. # define GC_ADD_CALLER
  401. # define GC_RETURN_ADDR (GC_word)__return_address
  402. #endif
  403. #if defined(__linux__) || defined(__GLIBC__)
  404. # include <features.h>
  405. # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1 || __GLIBC__ > 2) \
  406. && !defined(__ia64__) && !defined(__UCLIBC__)
  407. # ifndef GC_HAVE_BUILTIN_BACKTRACE
  408. # ifndef __UCLIBC__
  409. # define GC_HAVE_BUILTIN_BACKTRACE
  410. # endif
  411. # endif
  412. # endif
  413. # if defined(__i386__) || defined(__x86_64__)
  414. # define GC_CAN_SAVE_CALL_STACKS
  415. # endif
  416. #endif
  417. #if defined(_MSC_VER) && _MSC_VER >= 1200 /* version 12.0+ (MSVC 6.0+) */ \
  418. && !defined(_AMD64_)
  419. # ifndef GC_HAVE_NO_BUILTIN_BACKTRACE
  420. # define GC_HAVE_BUILTIN_BACKTRACE
  421. # endif
  422. #endif
  423. #if defined(GC_HAVE_BUILTIN_BACKTRACE) && !defined(GC_CAN_SAVE_CALL_STACKS)
  424. # define GC_CAN_SAVE_CALL_STACKS
  425. #endif
  426. #if defined(__sparc__)
  427. # define GC_CAN_SAVE_CALL_STACKS
  428. #endif
  429. /* If we're on an a platform on which we can't save call stacks, but */
  430. /* gcc is normally used, we go ahead and define GC_ADD_CALLER. */
  431. /* We make this decision independent of whether gcc is actually being */
  432. /* used, in order to keep the interface consistent, and allow mixing */
  433. /* of compilers. */
  434. /* This may also be desirable if it is possible but expensive to */
  435. /* retrieve the call chain. */
  436. #if (defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__) \
  437. || defined(__FreeBSD__) || defined(__DragonFly__)) & !defined(GC_CAN_SAVE_CALL_STACKS)
  438. # define GC_ADD_CALLER
  439. # if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
  440. /* gcc knows how to retrieve return address, but we don't know */
  441. /* how to generate call stacks. */
  442. # define GC_RETURN_ADDR (GC_word)__builtin_return_address(0)
  443. # else
  444. /* Just pass 0 for gcc compatibility. */
  445. # define GC_RETURN_ADDR 0
  446. # endif
  447. #endif
  448. #ifdef GC_ADD_CALLER
  449. # define GC_EXTRAS GC_RETURN_ADDR, __FILE__, __LINE__
  450. # define GC_EXTRA_PARAMS GC_word ra, const char * s, int i
  451. #else
  452. # define GC_EXTRAS __FILE__, __LINE__
  453. # define GC_EXTRA_PARAMS const char * s, int i
  454. #endif
  455. /* Debugging (annotated) allocation. GC_gcollect will check */
  456. /* objects allocated in this way for overwrites, etc. */
  457. GC_API void * GC_debug_malloc(size_t size_in_bytes, GC_EXTRA_PARAMS);
  458. GC_API void * GC_debug_malloc_atomic(size_t size_in_bytes, GC_EXTRA_PARAMS);
  459. GC_API char * GC_debug_strdup(const char *str, GC_EXTRA_PARAMS);
  460. GC_API void * GC_debug_malloc_uncollectable
  461. (size_t size_in_bytes, GC_EXTRA_PARAMS);
  462. GC_API void * GC_debug_malloc_stubborn
  463. (size_t size_in_bytes, GC_EXTRA_PARAMS);
  464. GC_API void * GC_debug_malloc_ignore_off_page
  465. (size_t size_in_bytes, GC_EXTRA_PARAMS);
  466. GC_API void * GC_debug_malloc_atomic_ignore_off_page
  467. (size_t size_in_bytes, GC_EXTRA_PARAMS);
  468. GC_API void GC_debug_free (void * object_addr);
  469. GC_API void * GC_debug_realloc
  470. (void * old_object, size_t new_size_in_bytes, GC_EXTRA_PARAMS);
  471. GC_API void GC_debug_change_stubborn(void *);
  472. GC_API void GC_debug_end_stubborn_change(void *);
  473. /* Routines that allocate objects with debug information (like the */
  474. /* above), but just fill in dummy file and line number information. */
  475. /* Thus they can serve as drop-in malloc/realloc replacements. This */
  476. /* can be useful for two reasons: */
  477. /* 1) It allows the collector to be built with DBG_HDRS_ALL defined */
  478. /* even if some allocation calls come from 3rd party libraries */
  479. /* that can't be recompiled. */
  480. /* 2) On some platforms, the file and line information is redundant, */
  481. /* since it can be reconstructed from a stack trace. On such */
  482. /* platforms it may be more convenient not to recompile, e.g. for */
  483. /* leak detection. This can be accomplished by instructing the */
  484. /* linker to replace malloc/realloc with these. */
  485. GC_API void * GC_debug_malloc_replacement (size_t size_in_bytes);
  486. GC_API void * GC_debug_realloc_replacement
  487. (void * object_addr, size_t size_in_bytes);
  488. # ifdef GC_DEBUG
  489. # define GC_MALLOC(sz) GC_debug_malloc(sz, GC_EXTRAS)
  490. # define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, GC_EXTRAS)
  491. # define GC_STRDUP(s) GC_debug_strdup((s), GC_EXTRAS)
  492. # define GC_MALLOC_UNCOLLECTABLE(sz) \
  493. GC_debug_malloc_uncollectable(sz, GC_EXTRAS)
  494. # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
  495. GC_debug_malloc_ignore_off_page(sz, GC_EXTRAS)
  496. # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
  497. GC_debug_malloc_atomic_ignore_off_page(sz, GC_EXTRAS)
  498. # define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, GC_EXTRAS)
  499. # define GC_FREE(p) GC_debug_free(p)
  500. # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
  501. GC_debug_register_finalizer(p, f, d, of, od)
  502. # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
  503. GC_debug_register_finalizer_ignore_self(p, f, d, of, od)
  504. # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
  505. GC_debug_register_finalizer_no_order(p, f, d, of, od)
  506. # define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \
  507. GC_debug_register_finalizer_unreachable(p, f, d, of, od)
  508. # define GC_MALLOC_STUBBORN(sz) GC_debug_malloc_stubborn(sz, GC_EXTRAS);
  509. # define GC_CHANGE_STUBBORN(p) GC_debug_change_stubborn(p)
  510. # define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p)
  511. # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
  512. GC_general_register_disappearing_link(link, GC_base(obj))
  513. # define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n)
  514. # else
  515. # define GC_MALLOC(sz) GC_malloc(sz)
  516. # define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz)
  517. # define GC_STRDUP(s) GC_strdup(s)
  518. # define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz)
  519. # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
  520. GC_malloc_ignore_off_page(sz)
  521. # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
  522. GC_malloc_atomic_ignore_off_page(sz)
  523. # define GC_REALLOC(old, sz) GC_realloc(old, sz)
  524. # define GC_FREE(p) GC_free(p)
  525. # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
  526. GC_register_finalizer(p, f, d, of, od)
  527. # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
  528. GC_register_finalizer_ignore_self(p, f, d, of, od)
  529. # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
  530. GC_register_finalizer_no_order(p, f, d, of, od)
  531. # define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \
  532. GC_register_finalizer_unreachable(p, f, d, of, od)
  533. # define GC_MALLOC_STUBBORN(sz) GC_malloc_stubborn(sz)
  534. # define GC_CHANGE_STUBBORN(p) GC_change_stubborn(p)
  535. # define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p)
  536. # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
  537. GC_general_register_disappearing_link(link, obj)
  538. # define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n)
  539. # endif
  540. /* The following are included because they are often convenient, and */
  541. /* reduce the chance for a misspecifed size argument. But calls may */
  542. /* expand to something syntactically incorrect if t is a complicated */
  543. /* type expression. */
  544. # define GC_NEW(t) (t *)GC_MALLOC(sizeof (t))
  545. # define GC_NEW_ATOMIC(t) (t *)GC_MALLOC_ATOMIC(sizeof (t))
  546. # define GC_NEW_STUBBORN(t) (t *)GC_MALLOC_STUBBORN(sizeof (t))
  547. # define GC_NEW_UNCOLLECTABLE(t) (t *)GC_MALLOC_UNCOLLECTABLE(sizeof (t))
  548. /* Finalization. Some of these primitives are grossly unsafe. */
  549. /* The idea is to make them both cheap, and sufficient to build */
  550. /* a safer layer, closer to Modula-3, Java, or PCedar finalization. */
  551. /* The interface represents my conclusions from a long discussion */
  552. /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes, */
  553. /* Christian Jacobi, and Russ Atkinson. It's not perfect, and */
  554. /* probably nobody else agrees with it. Hans-J. Boehm 3/13/92 */
  555. typedef void (*GC_finalization_proc) (void * obj, void * client_data);
  556. GC_API void GC_register_finalizer(void * obj, GC_finalization_proc fn,
  557. void * cd, GC_finalization_proc *ofn,
  558. void * *ocd);
  559. GC_API void GC_debug_register_finalizer
  560. (void * obj, GC_finalization_proc fn, void * cd,
  561. GC_finalization_proc *ofn, void * *ocd);
  562. /* When obj is no longer accessible, invoke */
  563. /* (*fn)(obj, cd). If a and b are inaccessible, and */
  564. /* a points to b (after disappearing links have been */
  565. /* made to disappear), then only a will be */
  566. /* finalized. (If this does not create any new */
  567. /* pointers to b, then b will be finalized after the */
  568. /* next collection.) Any finalizable object that */
  569. /* is reachable from itself by following one or more */
  570. /* pointers will not be finalized (or collected). */
  571. /* Thus cycles involving finalizable objects should */
  572. /* be avoided, or broken by disappearing links. */
  573. /* All but the last finalizer registered for an object */
  574. /* is ignored. */
  575. /* Finalization may be removed by passing 0 as fn. */
  576. /* Finalizers are implicitly unregistered just before */
  577. /* they are invoked. */
  578. /* The old finalizer and client data are stored in */
  579. /* *ofn and *ocd. */
  580. /* Fn is never invoked on an accessible object, */
  581. /* provided hidden pointers are converted to real */
  582. /* pointers only if the allocation lock is held, and */
  583. /* such conversions are not performed by finalization */
  584. /* routines. */
  585. /* If GC_register_finalizer is aborted as a result of */
  586. /* a signal, the object may be left with no */
  587. /* finalization, even if neither the old nor new */
  588. /* finalizer were NULL. */
  589. /* Obj should be the nonNULL starting address of an */
  590. /* object allocated by GC_malloc or friends. */
  591. /* Note that any garbage collectable object referenced */
  592. /* by cd will be considered accessible until the */
  593. /* finalizer is invoked. */
  594. /* Another versions of the above follow. It ignores */
  595. /* self-cycles, i.e. pointers from a finalizable object to */
  596. /* itself. There is a stylistic argument that this is wrong, */
  597. /* but it's unavoidable for C++, since the compiler may */
  598. /* silently introduce these. It's also benign in that specific */
  599. /* case. And it helps if finalizable objects are split to */
  600. /* avoid cycles. */
  601. /* Note that cd will still be viewed as accessible, even if it */
  602. /* refers to the object itself. */
  603. GC_API void GC_register_finalizer_ignore_self
  604. (void * obj, GC_finalization_proc fn, void * cd,
  605. GC_finalization_proc *ofn, void * *ocd);
  606. GC_API void GC_debug_register_finalizer_ignore_self
  607. (void * obj, GC_finalization_proc fn, void * cd,
  608. GC_finalization_proc *ofn, void * *ocd);
  609. /* Another version of the above. It ignores all cycles. */
  610. /* It should probably only be used by Java implementations. */
  611. /* Note that cd will still be viewed as accessible, even if it */
  612. /* refers to the object itself. */
  613. GC_API void GC_register_finalizer_no_order
  614. (void * obj, GC_finalization_proc fn, void * cd,
  615. GC_finalization_proc *ofn, void * *ocd);
  616. GC_API void GC_debug_register_finalizer_no_order
  617. (void * obj, GC_finalization_proc fn, void * cd,
  618. GC_finalization_proc *ofn, void * *ocd);
  619. /* This is a special finalizer that is useful when an object's */
  620. /* finalizer must be run when the object is known to be no */
  621. /* longer reachable, not even from other finalizable objects. */
  622. /* It behaves like "normal" finalization, except that the */
  623. /* finalizer is not run while the object is reachable from */
  624. /* other objects specifying unordered finalization. */
  625. /* Effectively it allows an object referenced, possibly */
  626. /* indirectly, from an unordered finalizable object to override */
  627. /* the unordered finalization request. */
  628. /* This can be used in combination with finalizer_no_order so */
  629. /* as to release resources that must not be released while an */
  630. /* object can still be brought back to life by other */
  631. /* finalizers. */
  632. /* Only works if GC_java_finalization is set. Probably only */
  633. /* of interest when implementing a language that requires */
  634. /* unordered finalization (e.g. Java, C#). */
  635. GC_API void GC_register_finalizer_unreachable
  636. (void * obj, GC_finalization_proc fn, void * cd,
  637. GC_finalization_proc *ofn, void * *ocd);
  638. GC_API void GC_debug_register_finalizer_unreachable
  639. (void * obj, GC_finalization_proc fn, void * cd,
  640. GC_finalization_proc *ofn, void * *ocd);
  641. /* The following routine may be used to break cycles between */
  642. /* finalizable objects, thus causing cyclic finalizable */
  643. /* objects to be finalized in the correct order. Standard */
  644. /* use involves calling GC_register_disappearing_link(&p), */
  645. /* where p is a pointer that is not followed by finalization */
  646. /* code, and should not be considered in determining */
  647. /* finalization order. */
  648. GC_API int GC_register_disappearing_link(void * * link );
  649. /* Link should point to a field of a heap allocated */
  650. /* object obj. *link will be cleared when obj is */
  651. /* found to be inaccessible. This happens BEFORE any */
  652. /* finalization code is invoked, and BEFORE any */
  653. /* decisions about finalization order are made. */
  654. /* This is useful in telling the finalizer that */
  655. /* some pointers are not essential for proper */
  656. /* finalization. This may avoid finalization cycles. */
  657. /* Note that obj may be resurrected by another */
  658. /* finalizer, and thus the clearing of *link may */
  659. /* be visible to non-finalization code. */
  660. /* There's an argument that an arbitrary action should */
  661. /* be allowed here, instead of just clearing a pointer. */
  662. /* But this causes problems if that action alters, or */
  663. /* examines connectivity. */
  664. /* Returns 1 if link was already registered, 0 if */
  665. /* registration succeeded, 2 if it failed for lack of */
  666. /* memory, and GC_oom_fn did not handle the problem. */
  667. /* Only exists for backward compatibility. See below: */
  668. GC_API int GC_general_register_disappearing_link (void * * link, void * obj);
  669. /* A slight generalization of the above. *link is */
  670. /* cleared when obj first becomes inaccessible. This */
  671. /* can be used to implement weak pointers easily and */
  672. /* safely. Typically link will point to a location */
  673. /* holding a disguised pointer to obj. (A pointer */
  674. /* inside an "atomic" object is effectively */
  675. /* disguised.) In this way soft */
  676. /* pointers are broken before any object */
  677. /* reachable from them are finalized. Each link */
  678. /* May be registered only once, i.e. with one obj */
  679. /* value. This was added after a long email discussion */
  680. /* with John Ellis. */
  681. /* Obj must be a pointer to the first word of an object */
  682. /* we allocated. It is unsafe to explicitly deallocate */
  683. /* the object containing link. Explicitly deallocating */
  684. /* obj may or may not cause link to eventually be */
  685. /* cleared. */
  686. /* This can be used to implement certain types of */
  687. /* weak pointers. Note however that this generally */
  688. /* requires that thje allocation lock is held (see */
  689. /* GC_call_with_allock_lock() below) when the disguised */
  690. /* pointer is accessed. Otherwise a strong pointer */
  691. /* could be recreated between the time the collector */
  692. /* decides to reclaim the object and the link is */
  693. /* cleared. */
  694. GC_API int GC_unregister_disappearing_link (void * * link);
  695. /* Returns 0 if link was not actually registered. */
  696. /* Undoes a registration by either of the above two */
  697. /* routines. */
  698. /* Returns !=0 if GC_invoke_finalizers has something to do. */
  699. GC_API int GC_should_invoke_finalizers(void);
  700. GC_API int GC_invoke_finalizers(void);
  701. /* Run finalizers for all objects that are ready to */
  702. /* be finalized. Return the number of finalizers */
  703. /* that were run. Normally this is also called */
  704. /* implicitly during some allocations. If */
  705. /* GC-finalize_on_demand is nonzero, it must be called */
  706. /* explicitly. */
  707. /* Explicitly tell the collector that an object is reachable */
  708. /* at a particular program point. This prevents the argument */
  709. /* pointer from being optimized away, even it is otherwise no */
  710. /* longer needed. It should have no visible effect in the */
  711. /* absence of finalizers or disappearing links. But it may be */
  712. /* needed to prevent finalizers from running while the */
  713. /* associated external resource is still in use. */
  714. /* The function is sometimes called keep_alive in other */
  715. /* settings. */
  716. # if defined(__GNUC__) && !defined(__INTEL_COMPILER)
  717. # define GC_reachable_here(ptr) \
  718. __asm__ volatile(" " : : "X"(ptr) : "memory");
  719. # else
  720. GC_API void GC_noop1(GC_word x);
  721. # define GC_reachable_here(ptr) GC_noop1((GC_word)(ptr));
  722. #endif
  723. /* GC_set_warn_proc can be used to redirect or filter warning messages. */
  724. /* p may not be a NULL pointer. */
  725. typedef void (*GC_warn_proc) (char *msg, GC_word arg);
  726. GC_API GC_warn_proc GC_set_warn_proc(GC_warn_proc p);
  727. /* Returns old warning procedure. */
  728. GC_API GC_word GC_set_free_space_divisor(GC_word value);
  729. /* Set free_space_divisor. See above for definition. */
  730. /* Returns old value. */
  731. /* The following is intended to be used by a higher level */
  732. /* (e.g. Java-like) finalization facility. It is expected */
  733. /* that finalization code will arrange for hidden pointers to */
  734. /* disappear. Otherwise objects can be accessed after they */
  735. /* have been collected. */
  736. /* Note that putting pointers in atomic objects or in */
  737. /* nonpointer slots of "typed" objects is equivalent to */
  738. /* disguising them in this way, and may have other advantages. */
  739. # if defined(I_HIDE_POINTERS) || defined(GC_I_HIDE_POINTERS)
  740. typedef GC_word GC_hidden_pointer;
  741. # define HIDE_POINTER(p) (~(GC_hidden_pointer)(p))
  742. # define REVEAL_POINTER(p) ((void *)(HIDE_POINTER(p)))
  743. /* Converting a hidden pointer to a real pointer requires verifying */
  744. /* that the object still exists. This involves acquiring the */
  745. /* allocator lock to avoid a race with the collector. */
  746. # endif /* I_HIDE_POINTERS */
  747. typedef void * (*GC_fn_type) (void * client_data);
  748. GC_API void * GC_call_with_alloc_lock (GC_fn_type fn, void * client_data);
  749. /* These routines are intended to explicitly notify the collector */
  750. /* of new threads. Often this is unnecessary because thread creation */
  751. /* is implicitly intercepted by the collector, using header-file */
  752. /* defines, or linker-based interception. In the long run the intent */
  753. /* is to always make redundant registration safe. In the short run, */
  754. /* this is being implemented a platform at a time. */
  755. /* The interface is complicated by the fact that we probably will not */
  756. /* ever be able to automatically determine the stack base for thread */
  757. /* stacks on all platforms. */
  758. /* Structure representing the base of a thread stack. On most */
  759. /* platforms this contains just a single address. */
  760. struct GC_stack_base {
  761. void * mem_base; /* Base of memory stack. */
  762. # if defined(__ia64) || defined(__ia64__)
  763. void * reg_base; /* Base of separate register stack. */
  764. # endif
  765. };
  766. typedef void * (*GC_stack_base_func)(struct GC_stack_base *sb, void *arg);
  767. /* Call a function with a stack base structure corresponding to */
  768. /* somewhere in the GC_call_with_stack_base frame. This often can */
  769. /* be used to provide a sufficiently accurate stack base. And we */
  770. /* implement it everywhere. */
  771. GC_API void * GC_call_with_stack_base(GC_stack_base_func fn, void *arg);
  772. /* Register the current thread, with the indicated stack base, as */
  773. /* a new thread whose stack(s) should be traced by the GC. If a */
  774. /* platform does not implicitly do so, this must be called before a */
  775. /* thread can allocate garbage collected memory, or assign pointers */
  776. /* to the garbage collected heap. Once registered, a thread will be */
  777. /* stopped during garbage collections. */
  778. /* Return codes: */
  779. #define GC_SUCCESS 0
  780. #define GC_DUPLICATE 1 /* Was already registered. */
  781. #define GC_NO_THREADS 2 /* No thread support in GC. */
  782. #define GC_UNIMPLEMENTED 3 /* Not yet implemented on this platform. */
  783. GC_API int GC_register_my_thread(struct GC_stack_base *);
  784. /* Unregister the current thread. The thread may no longer allocate */
  785. /* garbage collected memory or manipulate pointers to the */
  786. /* garbage collected heap after making this call. */
  787. /* Specifically, if it wants to return or otherwise communicate a */
  788. /* pointer to the garbage-collected heap to another thread, it must */
  789. /* do this before calling GC_unregister_my_thread, most probably */
  790. /* by saving it in a global data structure. */
  791. GC_API int GC_unregister_my_thread(void);
  792. /* Attempt to fill in the GC_stack_base structure with the stack base */
  793. /* for this thread. This appears to be required to implement anything */
  794. /* like the JNI AttachCurrentThread in an environment in which new */
  795. /* threads are not automatically registered with the collector. */
  796. /* It is also unfortunately hard to implement well on many platforms. */
  797. /* Returns GC_SUCCESS or GC_UNIMPLEMENTED. */
  798. GC_API int GC_get_stack_base(struct GC_stack_base *);
  799. /* The following routines are primarily intended for use with a */
  800. /* preprocessor which inserts calls to check C pointer arithmetic. */
  801. /* They indicate failure by invoking the corresponding _print_proc. */
  802. /* Check that p and q point to the same object. */
  803. /* Fail conspicuously if they don't. */
  804. /* Returns the first argument. */
  805. /* Succeeds if neither p nor q points to the heap. */
  806. /* May succeed if both p and q point to between heap objects. */
  807. GC_API void * GC_same_obj (void * p, void * q);
  808. /* Checked pointer pre- and post- increment operations. Note that */
  809. /* the second argument is in units of bytes, not multiples of the */
  810. /* object size. This should either be invoked from a macro, or the */
  811. /* call should be automatically generated. */
  812. GC_API void * GC_pre_incr (void * *p, size_t how_much);
  813. GC_API void * GC_post_incr (void * *p, size_t how_much);
  814. /* Check that p is visible */
  815. /* to the collector as a possibly pointer containing location. */
  816. /* If it isn't fail conspicuously. */
  817. /* Returns the argument in all cases. May erroneously succeed */
  818. /* in hard cases. (This is intended for debugging use with */
  819. /* untyped allocations. The idea is that it should be possible, though */
  820. /* slow, to add such a call to all indirect pointer stores.) */
  821. /* Currently useless for multithreaded worlds. */
  822. GC_API void * GC_is_visible (void * p);
  823. /* Check that if p is a pointer to a heap page, then it points to */
  824. /* a valid displacement within a heap object. */
  825. /* Fail conspicuously if this property does not hold. */
  826. /* Uninteresting with GC_all_interior_pointers. */
  827. /* Always returns its argument. */
  828. GC_API void * GC_is_valid_displacement (void * p);
  829. /* Explicitly dump the GC state. This is most often called from the */
  830. /* debugger, or by setting the GC_DUMP_REGULARLY environment variable, */
  831. /* but it may be useful to call it from client code during debugging. */
  832. void GC_dump(void);
  833. /* Safer, but slow, pointer addition. Probably useful mainly with */
  834. /* a preprocessor. Useful only for heap pointers. */
  835. #ifdef GC_DEBUG
  836. # define GC_PTR_ADD3(x, n, type_of_result) \
  837. ((type_of_result)GC_same_obj((x)+(n), (x)))
  838. # define GC_PRE_INCR3(x, n, type_of_result) \
  839. ((type_of_result)GC_pre_incr(&(x), (n)*sizeof(*x))
  840. # define GC_POST_INCR2(x, type_of_result) \
  841. ((type_of_result)GC_post_incr(&(x), sizeof(*x))
  842. # ifdef __GNUC__
  843. # define GC_PTR_ADD(x, n) \
  844. GC_PTR_ADD3(x, n, typeof(x))
  845. # define GC_PRE_INCR(x, n) \
  846. GC_PRE_INCR3(x, n, typeof(x))
  847. # define GC_POST_INCR(x, n) \
  848. GC_POST_INCR3(x, typeof(x))
  849. # else
  850. /* We can't do this right without typeof, which ANSI */
  851. /* decided was not sufficiently useful. Repeatedly */
  852. /* mentioning the arguments seems too dangerous to be */
  853. /* useful. So does not casting the result. */
  854. # define GC_PTR_ADD(x, n) ((x)+(n))
  855. # endif
  856. #else /* !GC_DEBUG */
  857. # define GC_PTR_ADD3(x, n, type_of_result) ((x)+(n))
  858. # define GC_PTR_ADD(x, n) ((x)+(n))
  859. # define GC_PRE_INCR3(x, n, type_of_result) ((x) += (n))
  860. # define GC_PRE_INCR(x, n) ((x) += (n))
  861. # define GC_POST_INCR2(x, n, type_of_result) ((x)++)
  862. # define GC_POST_INCR(x, n) ((x)++)
  863. #endif
  864. /* Safer assignment of a pointer to a nonstack location. */
  865. #ifdef GC_DEBUG
  866. # define GC_PTR_STORE(p, q) \
  867. (*(void **)GC_is_visible(p) = GC_is_valid_displacement(q))
  868. #else /* !GC_DEBUG */
  869. # define GC_PTR_STORE(p, q) (*(p) = (q))
  870. #endif
  871. /* Functions called to report pointer checking errors */
  872. GC_API void (*GC_same_obj_print_proc) (void * p, void * q);
  873. GC_API void (*GC_is_valid_displacement_print_proc) (void * p);
  874. GC_API void (*GC_is_visible_print_proc) (void * p);
  875. /* For pthread support, we generally need to intercept a number of */
  876. /* thread library calls. We do that here by macro defining them. */
  877. #if !defined(GC_USE_LD_WRAP) && !defined(GC_NO_THREAD_REDIRECTS) \
  878. && defined(GC_PTHREADS)
  879. # include "gc_pthread_redirects.h"
  880. #endif
  881. # if defined(PCR) || defined(GC_SOLARIS_THREADS) || \
  882. defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
  883. /* Any flavor of threads. */
  884. /* This returns a list of objects, linked through their first */
  885. /* word. Its use can greatly reduce lock contention problems, since */
  886. /* the allocation lock can be acquired and released many fewer times. */
  887. /* It is used internally by gc_local_alloc.h, which provides a simpler */
  888. /* programming interface on Linux. */
  889. void * GC_malloc_many(size_t lb);
  890. #define GC_NEXT(p) (*(void * *)(p)) /* Retrieve the next element */
  891. /* in returned list. */
  892. #endif /* THREADS */
  893. /* Register a callback to control the scanning of dynamic libraries.
  894. When the GC scans the static data of a dynamic library, it will
  895. first call a user-supplied routine with filename of the library and
  896. the address and length of the memory region. This routine should
  897. return nonzero if that region should be scanned. */
  898. GC_API void
  899. GC_register_has_static_roots_callback
  900. (int (*callback)(const char *, void *, size_t));
  901. #if defined(GC_WIN32_THREADS) && !defined(__CYGWIN32__) \
  902. && !defined(__CYGWIN__) \
  903. && !defined(GC_PTHREADS)
  904. #ifdef __cplusplus
  905. } /* Including windows.h in an extern "C" context no longer works. */
  906. #endif
  907. #ifndef GC_NO_THREAD_DECLS
  908. # include <windows.h>
  909. #ifdef __cplusplus
  910. extern "C" {
  911. #endif
  912. /*
  913. * All threads must be created using GC_CreateThread or GC_beginthreadex,
  914. * or must explicitly call GC_register_my_thread,
  915. * so that they will be recorded in the thread table.
  916. * For backwards compatibility, it is possible to build the GC
  917. * with GC_DLL defined, and to call GC_use_DllMain().
  918. * This implicitly registers all created threads, but appears to be
  919. * less robust.
  920. *
  921. * Currently the collector expects all threads to fall through and
  922. * terminate normally, or call GC_endthreadex() or GC_ExitThread,
  923. * so that the thread is properly unregistered. (An explicit call
  924. * to GC_unregister_my_thread() should also work, but risks unregistering
  925. * the thread twice.)
  926. */
  927. GC_API HANDLE WINAPI GC_CreateThread(
  928. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  929. DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress,
  930. LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId );
  931. # if defined(_MSC_VER) && _MSC_VER >= 1200 && !defined(_UINTPTR_T_DEFINED)
  932. typedef unsigned long uintptr_t;
  933. # endif
  934. GC_API uintptr_t GC_beginthreadex(
  935. void *security, unsigned stack_size,
  936. unsigned ( __stdcall *start_address )( void * ),
  937. void *arglist, unsigned initflag, unsigned *thrdaddr);
  938. GC_API void GC_endthreadex(unsigned retval);
  939. GC_API void WINAPI GC_ExitThread(DWORD dwExitCode);
  940. # if defined(_WIN32_WCE)
  941. /*
  942. * win32_threads.c implements the real WinMain, which will start a new thread
  943. * to call GC_WinMain after initializing the garbage collector.
  944. */
  945. GC_API int WINAPI GC_WinMain(
  946. HINSTANCE hInstance,
  947. HINSTANCE hPrevInstance,
  948. LPWSTR lpCmdLine,
  949. int nCmdShow );
  950. # ifndef GC_BUILD
  951. # define WinMain GC_WinMain
  952. # endif
  953. # endif /* defined(_WIN32_WCE) */
  954. #endif /* !GC_NO_THREAD_DECLS */
  955. /*
  956. * Use implicit thread registration via DllMain.
  957. * Must be called before GC_INIT and other GC routines.
  958. * Should be avoided if GC_beginthreadex and friends can be called
  959. * instead.
  960. */
  961. GC_API void GC_use_DllMain(void);
  962. # ifndef GC_NO_THREAD_REDIRECTS
  963. # define CreateThread GC_CreateThread
  964. # define ExitThread GC_ExitThread
  965. # define _beginthreadex GC_beginthreadex
  966. # define _endthreadex GC_endthreadex
  967. # define _beginthread { > "Please use _beginthreadex instead of _beginthread" < }
  968. # endif /* !GC_NO_THREAD_REDIRECTS */
  969. #endif /* defined(GC_WIN32_THREADS) && !cygwin */
  970. /*
  971. * Fully portable code should call GC_INIT() from the main program
  972. * before making any other GC_ calls. On most platforms this is a
  973. * no-op and the collector self-initializes. But a number of platforms
  974. * make that too hard.
  975. * A GC_INIT call is required if the collector is built with THREAD_LOCAL_ALLOC
  976. * defined and the initial allocation call is not to GC_malloc() or
  977. * GC_malloc_atomic().
  978. */
  979. #if defined(__CYGWIN32__) || defined (_AIX)
  980. /*
  981. * Similarly gnu-win32 DLLs need explicit initialization from
  982. * the main program, as does AIX.
  983. */
  984. # ifdef __CYGWIN32__
  985. extern int _data_start__[];
  986. extern int _data_end__[];
  987. extern int _bss_start__[];
  988. extern int _bss_end__[];
  989. # define GC_MAX(x,y) ((x) > (y) ? (x) : (y))
  990. # define GC_MIN(x,y) ((x) < (y) ? (x) : (y))
  991. # define GC_DATASTART ((void *) GC_MIN(_data_start__, _bss_start__))
  992. # define GC_DATAEND ((void *) GC_MAX(_data_end__, _bss_end__))
  993. # define GC_INIT() { GC_add_roots(GC_DATASTART, GC_DATAEND); \
  994. GC_gcollect(); /* For blacklisting. */}
  995. /* Required at least if GC is in dll. And doesn't hurt. */
  996. # endif
  997. # if defined(_AIX)
  998. extern int _data[], _end[];
  999. # define GC_DATASTART ((void *)((ulong)_data))
  1000. # define GC_DATAEND ((void *)((ulong)_end))
  1001. # define GC_INIT() { GC_add_roots(GC_DATASTART, GC_DATAEND); }
  1002. # endif
  1003. #else
  1004. # define GC_INIT() { GC_init(); }
  1005. #endif
  1006. #if !defined(_WIN32_WCE) \
  1007. && ((defined(_MSDOS) || defined(_MSC_VER)) && (_M_IX86 >= 300) \
  1008. || defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__))
  1009. /* win32S may not free all resources on process exit. */
  1010. /* This explicitly deallocates the heap. */
  1011. GC_API void GC_win32_free_heap ();
  1012. #endif
  1013. #if ( defined(_AMIGA) && !defined(GC_AMIGA_MAKINGLIB) )
  1014. /* Allocation really goes through GC_amiga_allocwrapper_do */
  1015. # include "gc_amiga_redirects.h"
  1016. #endif
  1017. #if defined(GC_REDIRECT_TO_LOCAL)
  1018. /* Now redundant; that's the default with THREAD_LOCAL_ALLOC */
  1019. #endif
  1020. #ifdef __cplusplus
  1021. } /* end of extern "C" */
  1022. #endif
  1023. #endif /* _GC_H */