gc.h 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  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. * Copyright (c) 2000-2011 by Hewlett-Packard Development Company.
  8. *
  9. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  10. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  11. *
  12. * Permission is hereby granted to use or copy this program
  13. * for any purpose, provided the above notices are retained on all copies.
  14. * Permission to modify the code and to distribute modified code is granted,
  15. * provided the above notices are retained, and a notice that the code was
  16. * modified is included with the above copyright notice.
  17. */
  18. /*
  19. * Note that this defines a large number of tuning hooks, which can
  20. * safely be ignored in nearly all cases. For normal use it suffices
  21. * to call only GC_MALLOC and perhaps GC_REALLOC.
  22. * For better performance, also look at GC_MALLOC_ATOMIC, and
  23. * GC_enable_incremental. If you need an action to be performed
  24. * immediately before an object is collected, look at GC_register_finalizer.
  25. * If you are using Solaris threads, look at the end of this file.
  26. * Everything else is best ignored unless you encounter performance
  27. * problems.
  28. */
  29. #ifndef GC_H
  30. #define GC_H
  31. /* Help debug mixed up preprocessor symbols. */
  32. #if (defined(WIN64) && !defined(_WIN64)) && defined(_MSC_VER)
  33. #pragma message("Warning: Expecting _WIN64 for x64 targets! Notice the leading underscore!")
  34. #endif
  35. #include "gc_version.h"
  36. /* Define version numbers here to allow test on build machine */
  37. /* for cross-builds. Note that this defines the header */
  38. /* version number, which may or may not match that of the */
  39. /* dynamic library. GC_get_version() can be used to obtain */
  40. /* the latter. */
  41. #include "gc_config_macros.h"
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. typedef void * GC_PTR; /* preserved only for backward compatibility */
  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 ptrdiff_t might be */
  51. /* better choices. But those had incorrect definitions on some older */
  52. /* systems. Notably "typedef int size_t" is WRONG. */
  53. #ifdef _WIN64
  54. # if defined(__int64) && !defined(CPPCHECK)
  55. typedef unsigned __int64 GC_word;
  56. typedef __int64 GC_signed_word;
  57. # else
  58. typedef unsigned long long GC_word;
  59. typedef long long GC_signed_word;
  60. # endif
  61. #else
  62. typedef unsigned long GC_word;
  63. typedef long GC_signed_word;
  64. #endif
  65. /* Get the GC library version. The returned value is a constant in the */
  66. /* form: ((version_major<<16) | (version_minor<<8) | version_micro). */
  67. GC_API unsigned GC_CALL GC_get_version(void);
  68. /* Public read-only variables */
  69. /* The supplied getter functions are preferred for new code. */
  70. GC_API GC_ATTR_DEPRECATED GC_word GC_gc_no;
  71. /* Counter incremented per collection. */
  72. /* Includes empty GCs at startup. */
  73. GC_API GC_word GC_CALL GC_get_gc_no(void);
  74. /* GC_get_gc_no() is unsynchronized, so */
  75. /* it requires GC_call_with_alloc_lock() to */
  76. /* avoid data races on multiprocessors. */
  77. #ifdef GC_THREADS
  78. GC_API GC_ATTR_DEPRECATED int GC_parallel;
  79. /* GC is parallelized for performance on */
  80. /* multiprocessors. Currently set only */
  81. /* implicitly if collector is built with */
  82. /* PARALLEL_MARK defined and if either: */
  83. /* Env variable GC_NPROC is set to > 1, or */
  84. /* GC_NPROC is not set and this is an MP. */
  85. /* If GC_parallel is on (non-zero), incremental */
  86. /* collection is only partially functional, */
  87. /* and may not be desirable. The getter does */
  88. /* not use or need synchronization (i.e. */
  89. /* acquiring the GC lock). Starting from */
  90. /* GC v7.3, GC_parallel value is equal to the */
  91. /* number of marker threads minus one (i.e. */
  92. /* number of existing parallel marker threads */
  93. /* excluding the initiating one). */
  94. GC_API int GC_CALL GC_get_parallel(void);
  95. #endif
  96. /* Public R/W variables */
  97. /* The supplied setter and getter functions are preferred for new code. */
  98. typedef void * (GC_CALLBACK * GC_oom_func)(size_t /* bytes_requested */);
  99. GC_API GC_ATTR_DEPRECATED GC_oom_func GC_oom_fn;
  100. /* When there is insufficient memory to satisfy */
  101. /* an allocation request, we return */
  102. /* (*GC_oom_fn)(size). By default this just */
  103. /* returns NULL. */
  104. /* If it returns, it must return 0 or a valid */
  105. /* pointer to a previously allocated heap */
  106. /* object. GC_oom_fn must not be 0. */
  107. /* Both the supplied setter and the getter */
  108. /* acquire the GC lock (to avoid data races). */
  109. GC_API void GC_CALL GC_set_oom_fn(GC_oom_func) GC_ATTR_NONNULL(1);
  110. GC_API GC_oom_func GC_CALL GC_get_oom_fn(void);
  111. typedef void (GC_CALLBACK * GC_on_heap_resize_proc)(GC_word /* new_size */);
  112. GC_API GC_ATTR_DEPRECATED GC_on_heap_resize_proc GC_on_heap_resize;
  113. /* Invoked when the heap grows or shrinks. */
  114. /* Called with the world stopped (and the */
  115. /* allocation lock held). May be 0. */
  116. GC_API void GC_CALL GC_set_on_heap_resize(GC_on_heap_resize_proc);
  117. GC_API GC_on_heap_resize_proc GC_CALL GC_get_on_heap_resize(void);
  118. /* Both the supplied setter and the getter */
  119. /* acquire the GC lock (to avoid data races). */
  120. typedef enum {
  121. GC_EVENT_START /* COLLECTION */,
  122. GC_EVENT_MARK_START,
  123. GC_EVENT_MARK_END,
  124. GC_EVENT_RECLAIM_START,
  125. GC_EVENT_RECLAIM_END,
  126. GC_EVENT_END /* COLLECTION */,
  127. GC_EVENT_PRE_STOP_WORLD /* STOPWORLD_BEGIN */,
  128. GC_EVENT_POST_STOP_WORLD /* STOPWORLD_END */,
  129. GC_EVENT_PRE_START_WORLD /* STARTWORLD_BEGIN */,
  130. GC_EVENT_POST_START_WORLD /* STARTWORLD_END */,
  131. GC_EVENT_THREAD_SUSPENDED,
  132. GC_EVENT_THREAD_UNSUSPENDED
  133. } GC_EventType;
  134. typedef void (GC_CALLBACK * GC_on_collection_event_proc)(GC_EventType);
  135. /* Invoked to indicate progress through the */
  136. /* collection process. Not used for thread */
  137. /* suspend/resume notifications. Called with */
  138. /* the GC lock held (or, even, the world */
  139. /* stopped). May be 0 (means no notifier). */
  140. GC_API void GC_CALL GC_set_on_collection_event(GC_on_collection_event_proc);
  141. GC_API GC_on_collection_event_proc GC_CALL GC_get_on_collection_event(void);
  142. /* Both the supplied setter and the getter */
  143. /* acquire the GC lock (to avoid data races). */
  144. #if defined(GC_THREADS) || defined(NN_PLATFORM_CTR) || defined(NN_BUILD_TARGET_PLATFORM_NX)
  145. typedef void (GC_CALLBACK * GC_on_thread_event_proc)(GC_EventType,
  146. void * /* thread_id */);
  147. /* Invoked when a thread is suspended or */
  148. /* resumed during collection. Called with the */
  149. /* GC lock held (and the world stopped */
  150. /* partially). May be 0 (means no notifier). */
  151. GC_API void GC_CALL GC_set_on_thread_event(GC_on_thread_event_proc);
  152. GC_API GC_on_thread_event_proc GC_CALL GC_get_on_thread_event(void);
  153. /* Both the supplied setter and the getter */
  154. /* acquire the GC lock (to avoid data races). */
  155. #endif
  156. GC_API GC_ATTR_DEPRECATED int GC_find_leak;
  157. /* Set to true to turn on the leak-finding mode */
  158. /* (do not actually garbage collect, but simply */
  159. /* report inaccessible memory that was not */
  160. /* deallocated with GC_FREE). Initial value */
  161. /* is determined by FIND_LEAK macro. */
  162. /* The value should not typically be modified */
  163. /* after GC initialization (and, thus, it does */
  164. /* not use or need synchronization). */
  165. GC_API void GC_CALL GC_set_find_leak(int);
  166. GC_API int GC_CALL GC_get_find_leak(void);
  167. GC_API GC_ATTR_DEPRECATED int GC_all_interior_pointers;
  168. /* Arrange for pointers to object interiors to */
  169. /* be recognized as valid. Typically should */
  170. /* not be changed after GC initialization (in */
  171. /* case of calling it after the GC is */
  172. /* initialized, the setter acquires the GC lock */
  173. /* (to avoid data races). The initial value */
  174. /* depends on whether the GC is built with */
  175. /* ALL_INTERIOR_POINTERS macro defined or not. */
  176. /* Unless DONT_ADD_BYTE_AT_END is defined, this */
  177. /* also affects whether sizes are increased by */
  178. /* at least a byte to allow "off the end" */
  179. /* pointer recognition. Must be only 0 or 1. */
  180. GC_API void GC_CALL GC_set_all_interior_pointers(int);
  181. GC_API int GC_CALL GC_get_all_interior_pointers(void);
  182. GC_API GC_ATTR_DEPRECATED int GC_finalize_on_demand;
  183. /* If nonzero, finalizers will only be run in */
  184. /* response to an explicit GC_invoke_finalizers */
  185. /* call. The default is determined by whether */
  186. /* the FINALIZE_ON_DEMAND macro is defined */
  187. /* when the collector is built. */
  188. /* The setter and getter are unsynchronized. */
  189. GC_API void GC_CALL GC_set_finalize_on_demand(int);
  190. GC_API int GC_CALL GC_get_finalize_on_demand(void);
  191. GC_API GC_ATTR_DEPRECATED int GC_java_finalization;
  192. /* Mark objects reachable from finalizable */
  193. /* objects in a separate post-pass. This makes */
  194. /* it a bit safer to use non-topologically- */
  195. /* ordered finalization. Default value is */
  196. /* determined by JAVA_FINALIZATION macro. */
  197. /* Enables register_finalizer_unreachable to */
  198. /* work correctly. */
  199. /* The setter and getter are unsynchronized. */
  200. GC_API void GC_CALL GC_set_java_finalization(int);
  201. GC_API int GC_CALL GC_get_java_finalization(void);
  202. typedef void (GC_CALLBACK * GC_finalizer_notifier_proc)(void);
  203. GC_API GC_ATTR_DEPRECATED GC_finalizer_notifier_proc GC_finalizer_notifier;
  204. /* Invoked by the collector when there are */
  205. /* objects to be finalized. Invoked at most */
  206. /* once per GC cycle. Never invoked unless */
  207. /* GC_finalize_on_demand is set. */
  208. /* Typically this will notify a finalization */
  209. /* thread, which will call GC_invoke_finalizers */
  210. /* in response. May be 0 (means no notifier). */
  211. /* Both the supplied setter and the getter */
  212. /* acquire the GC lock (to avoid data races). */
  213. GC_API void GC_CALL GC_set_finalizer_notifier(GC_finalizer_notifier_proc);
  214. GC_API GC_finalizer_notifier_proc GC_CALL GC_get_finalizer_notifier(void);
  215. GC_API
  216. # ifndef GC_DONT_GC
  217. GC_ATTR_DEPRECATED
  218. # endif
  219. int GC_dont_gc; /* != 0 ==> Don't collect. In versions 6.2a1+, */
  220. /* this overrides explicit GC_gcollect() calls. */
  221. /* Used as a counter, so that nested enabling */
  222. /* and disabling work correctly. Should */
  223. /* normally be updated with GC_enable() and */
  224. /* GC_disable() calls. Direct assignment to */
  225. /* GC_dont_gc is deprecated. To check whether */
  226. /* GC is disabled, GC_is_disabled() is */
  227. /* preferred for new code. */
  228. GC_API GC_ATTR_DEPRECATED int GC_dont_expand;
  229. /* Do not expand the heap unless explicitly */
  230. /* requested or forced to. The setter and */
  231. /* getter are unsynchronized. */
  232. GC_API void GC_CALL GC_set_dont_expand(int);
  233. GC_API int GC_CALL GC_get_dont_expand(void);
  234. GC_API GC_ATTR_DEPRECATED int GC_use_entire_heap;
  235. /* Causes the non-incremental collector to use the */
  236. /* entire heap before collecting. This was the only */
  237. /* option for GC versions < 5.0. This sometimes */
  238. /* results in more large block fragmentation, since */
  239. /* very large blocks will tend to get broken up */
  240. /* during each GC cycle. It is likely to result in a */
  241. /* larger working set, but lower collection */
  242. /* frequencies, and hence fewer instructions executed */
  243. /* in the collector. */
  244. GC_API GC_ATTR_DEPRECATED int GC_full_freq;
  245. /* Number of partial collections between */
  246. /* full collections. Matters only if */
  247. /* GC_is_incremental_mode(). */
  248. /* Full collections are also triggered if */
  249. /* the collector detects a substantial */
  250. /* increase in the number of in-use heap */
  251. /* blocks. Values in the tens are now */
  252. /* perfectly reasonable, unlike for */
  253. /* earlier GC versions. */
  254. /* The setter and getter are unsynchronized, so */
  255. /* GC_call_with_alloc_lock() is required to */
  256. /* avoid data races (if the value is modified */
  257. /* after the GC is put to multi-threaded mode). */
  258. GC_API void GC_CALL GC_set_full_freq(int);
  259. GC_API int GC_CALL GC_get_full_freq(void);
  260. GC_API GC_ATTR_DEPRECATED GC_word GC_non_gc_bytes;
  261. /* Bytes not considered candidates for */
  262. /* collection. Used only to control scheduling */
  263. /* of collections. Updated by */
  264. /* GC_malloc_uncollectable and GC_free. */
  265. /* Wizards only. */
  266. /* The setter and getter are unsynchronized, so */
  267. /* GC_call_with_alloc_lock() is required to */
  268. /* avoid data races (if the value is modified */
  269. /* after the GC is put to multi-threaded mode). */
  270. GC_API void GC_CALL GC_set_non_gc_bytes(GC_word);
  271. GC_API GC_word GC_CALL GC_get_non_gc_bytes(void);
  272. GC_API GC_ATTR_DEPRECATED int GC_no_dls;
  273. /* Don't register dynamic library data segments. */
  274. /* Wizards only. Should be used only if the */
  275. /* application explicitly registers all roots. */
  276. /* (In some environments like Microsoft Windows */
  277. /* and Apple's Darwin, this may also prevent */
  278. /* registration of the main data segment as part */
  279. /* of the root set.) */
  280. /* The setter and getter are unsynchronized. */
  281. GC_API void GC_CALL GC_set_no_dls(int);
  282. GC_API int GC_CALL GC_get_no_dls(void);
  283. GC_API GC_ATTR_DEPRECATED GC_word GC_free_space_divisor;
  284. /* We try to make sure that we allocate at */
  285. /* least N/GC_free_space_divisor bytes between */
  286. /* collections, where N is twice the number */
  287. /* of traced bytes, plus the number of untraced */
  288. /* bytes (bytes in "atomic" objects), plus */
  289. /* a rough estimate of the root set size. */
  290. /* N approximates GC tracing work per GC. */
  291. /* The initial value is GC_FREE_SPACE_DIVISOR. */
  292. /* Increasing its value will use less space */
  293. /* but more collection time. Decreasing it */
  294. /* will appreciably decrease collection time */
  295. /* at the expense of space. */
  296. /* The setter and getter are unsynchronized, so */
  297. /* GC_call_with_alloc_lock() is required to */
  298. /* avoid data races (if the value is modified */
  299. /* after the GC is put to multi-threaded mode). */
  300. /* In version 7.1 (and before), the setter */
  301. /* returned the old value. */
  302. GC_API void GC_CALL GC_set_free_space_divisor(GC_word);
  303. GC_API GC_word GC_CALL GC_get_free_space_divisor(void);
  304. GC_API GC_ATTR_DEPRECATED GC_word GC_max_retries;
  305. /* The maximum number of GCs attempted before */
  306. /* reporting out of memory after heap */
  307. /* expansion fails. Initially 0. */
  308. /* The setter and getter are unsynchronized, so */
  309. /* GC_call_with_alloc_lock() is required to */
  310. /* avoid data races (if the value is modified */
  311. /* after the GC is put to multi-threaded mode). */
  312. GC_API void GC_CALL GC_set_max_retries(GC_word);
  313. GC_API GC_word GC_CALL GC_get_max_retries(void);
  314. GC_API GC_ATTR_DEPRECATED char *GC_stackbottom;
  315. /* Cool end of user stack. */
  316. /* May be set in the client prior to */
  317. /* calling any GC_ routines. This */
  318. /* avoids some overhead, and */
  319. /* potentially some signals that can */
  320. /* confuse debuggers. Otherwise the */
  321. /* collector attempts to set it */
  322. /* automatically. */
  323. /* For multi-threaded code, this is the */
  324. /* cold end of the stack for the */
  325. /* primordial thread. Portable clients */
  326. /* should use GC_get_stack_base(), */
  327. /* GC_call_with_gc_active() and */
  328. /* GC_register_my_thread() instead. */
  329. GC_API GC_ATTR_DEPRECATED int GC_dont_precollect;
  330. /* Do not collect as part of GC */
  331. /* initialization. Should be set only */
  332. /* if the client wants a chance to */
  333. /* manually initialize the root set */
  334. /* before the first collection. */
  335. /* Interferes with blacklisting. */
  336. /* Wizards only. The setter and getter */
  337. /* are unsynchronized (and no external */
  338. /* locking is needed since the value is */
  339. /* accessed at GC initialization only). */
  340. GC_API void GC_CALL GC_set_dont_precollect(int);
  341. GC_API int GC_CALL GC_get_dont_precollect(void);
  342. GC_API GC_ATTR_DEPRECATED unsigned long long GC_time_limit;
  343. /* If incremental collection is enabled, */
  344. /* We try to terminate collections */
  345. /* after this many milliseconds. Not a */
  346. /* hard time bound. Setting this to */
  347. /* GC_TIME_UNLIMITED will essentially */
  348. /* disable incremental collection while */
  349. /* leaving generational collection */
  350. /* enabled. */
  351. #define GC_TIME_UNLIMITED 999999999999
  352. /* Setting GC_time_limit to this value */
  353. /* will disable the "pause time exceeded"*/
  354. /* tests. */
  355. /* The setter and getter are unsynchronized, so */
  356. /* GC_call_with_alloc_lock() is required to */
  357. /* avoid data races (if the value is modified */
  358. /* after the GC is put to multi-threaded mode). */
  359. GC_API void GC_CALL GC_set_time_limit(unsigned long);
  360. GC_API void GC_CALL GC_set_time_limit_ns(unsigned long long);
  361. GC_API unsigned long GC_CALL GC_get_time_limit(void);
  362. GC_API unsigned long long GC_CALL GC_get_time_limit_ns(void);
  363. /* Public procedures */
  364. /* Tell the collector to start various performance measurements. */
  365. /* Only the total time taken by full collections is calculated, as */
  366. /* of now. And, currently, there is no way to stop the measurements. */
  367. /* The function does not use any synchronization. Defined only if the */
  368. /* library has been compiled without NO_CLOCK. */
  369. GC_API void GC_CALL GC_start_performance_measurement(void);
  370. /* Get the total time of all full collections since the start of the */
  371. /* performance measurements. The measurement unit is one millisecond. */
  372. /* Note that the returned value wraps around on overflow. */
  373. /* The function does not use any synchronization. Defined only if the */
  374. /* library has been compiled without NO_CLOCK. */
  375. GC_API unsigned long GC_CALL GC_get_full_gc_total_time(void);
  376. /* Set whether the GC will allocate executable memory pages or not. */
  377. /* A non-zero argument instructs the collector to allocate memory with */
  378. /* the executable flag on. Must be called before the collector is */
  379. /* initialized. May have no effect on some platforms. The default */
  380. /* value is controlled by NO_EXECUTE_PERMISSION macro (if present then */
  381. /* the flag is off). Portable clients should have */
  382. /* GC_set_pages_executable(1) call (before GC_INIT) provided they are */
  383. /* going to execute code on any of the GC-allocated memory objects. */
  384. GC_API void GC_CALL GC_set_pages_executable(int);
  385. /* Returns non-zero value if the GC is set to the allocate-executable */
  386. /* mode. The mode could be changed by GC_set_pages_executable (before */
  387. /* GC_INIT) unless the former has no effect on the platform. Does not */
  388. /* use or need synchronization (i.e. acquiring the allocator lock). */
  389. GC_API int GC_CALL GC_get_pages_executable(void);
  390. /* The setter and getter of the minimum value returned by the internal */
  391. /* min_bytes_allocd(). The value should not be zero; the default value */
  392. /* is one. Not synchronized. */
  393. GC_API void GC_CALL GC_set_min_bytes_allocd(size_t);
  394. GC_API size_t GC_CALL GC_get_min_bytes_allocd(void);
  395. /* Set/get the size in pages of units operated by GC_collect_a_little. */
  396. /* The value should not be zero. Not synchronized. */
  397. GC_API void GC_CALL GC_set_rate(int);
  398. GC_API int GC_CALL GC_get_rate(void);
  399. /* Set/get the maximum number of prior attempts at the world-stop */
  400. /* marking. Not synchronized. */
  401. GC_API void GC_CALL GC_set_max_prior_attempts(int);
  402. GC_API int GC_CALL GC_get_max_prior_attempts(void);
  403. /* Overrides the default handle-fork mode. Non-zero value means GC */
  404. /* should install proper pthread_atfork handlers. Has effect only if */
  405. /* called before GC_INIT. Clients should invoke GC_set_handle_fork */
  406. /* with non-zero argument if going to use fork with GC functions called */
  407. /* in the forked child. (Note that such client and atfork handlers */
  408. /* activities are not fully POSIX-compliant.) GC_set_handle_fork */
  409. /* instructs GC_init to setup GC fork handlers using pthread_atfork, */
  410. /* the latter might fail (or, even, absent on some targets) causing */
  411. /* abort at GC initialization. Starting from 7.3alpha3, problems with */
  412. /* missing (or failed) pthread_atfork() could be avoided by invocation */
  413. /* of GC_set_handle_fork(-1) at application start-up and surrounding */
  414. /* each fork() with the relevant GC_atfork_prepare/parent/child calls. */
  415. GC_API void GC_CALL GC_set_handle_fork(int);
  416. /* Routines to handle POSIX fork() manually (no-op if handled */
  417. /* automatically). GC_atfork_prepare should be called immediately */
  418. /* before fork(); GC_atfork_parent should be invoked just after fork in */
  419. /* the branch that corresponds to parent process (i.e., fork result is */
  420. /* non-zero); GC_atfork_child is to be called immediately in the child */
  421. /* branch (i.e., fork result is 0). Note that GC_atfork_child() call */
  422. /* should, of course, precede GC_start_mark_threads call (if any). */
  423. GC_API void GC_CALL GC_atfork_prepare(void);
  424. GC_API void GC_CALL GC_atfork_parent(void);
  425. GC_API void GC_CALL GC_atfork_child(void);
  426. /* Initialize the collector. Portable clients should call GC_INIT() */
  427. /* from the main program instead. */
  428. GC_API void GC_CALL GC_init(void);
  429. /* Returns non-zero (TRUE) if and only if the collector is initialized */
  430. /* (or, at least, the initialization is in progress). */
  431. GC_API int GC_CALL GC_is_init_called(void);
  432. /* Perform the collector shutdown. (E.g. dispose critical sections on */
  433. /* Win32 target.) A duplicate invocation is a no-op. GC_INIT should */
  434. /* not be called after the shutdown. See also GC_win32_free_heap(). */
  435. GC_API void GC_CALL GC_deinit(void);
  436. /* General purpose allocation routines, with roughly malloc calling */
  437. /* conv. The atomic versions promise that no relevant pointers are */
  438. /* contained in the object. The non-atomic versions guarantee that the */
  439. /* new object is cleared. GC_malloc_uncollectable allocates */
  440. /* an object that is scanned for pointers to collectible */
  441. /* objects, but is not itself collectible. The object is scanned even */
  442. /* if it does not appear to be reachable. GC_malloc_uncollectable and */
  443. /* GC_free called on the resulting object implicitly update */
  444. /* GC_non_gc_bytes appropriately. */
  445. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  446. GC_malloc(size_t /* size_in_bytes */);
  447. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  448. GC_malloc_atomic(size_t /* size_in_bytes */);
  449. GC_API GC_ATTR_MALLOC char * GC_CALL GC_strdup(const char *);
  450. GC_API GC_ATTR_MALLOC char * GC_CALL
  451. GC_strndup(const char *, size_t) GC_ATTR_NONNULL(1);
  452. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  453. GC_malloc_uncollectable(size_t /* size_in_bytes */);
  454. GC_API GC_ATTR_DEPRECATED void * GC_CALL GC_malloc_stubborn(size_t);
  455. /* GC_memalign() is not well tested. */
  456. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(2) void * GC_CALL
  457. GC_memalign(size_t /* align */, size_t /* lb */);
  458. GC_API int GC_CALL GC_posix_memalign(void ** /* memptr */, size_t /* align */,
  459. size_t /* lb */) GC_ATTR_NONNULL(1);
  460. /* Explicitly deallocate an object. Dangerous if used incorrectly. */
  461. /* Requires a pointer to the base of an object. */
  462. /* An object should not be enabled for finalization (and it should not */
  463. /* contain registered disappearing links of any kind) when it is */
  464. /* explicitly deallocated. */
  465. /* GC_free(0) is a no-op, as required by ANSI C for free. */
  466. GC_API void GC_CALL GC_free(void *);
  467. /* The "stubborn" objects allocation is not supported anymore. Exists */
  468. /* only for the backward compatibility. */
  469. #define GC_MALLOC_STUBBORN(sz) GC_MALLOC(sz)
  470. #define GC_NEW_STUBBORN(t) GC_NEW(t)
  471. #define GC_CHANGE_STUBBORN(p) GC_change_stubborn(p)
  472. GC_API GC_ATTR_DEPRECATED void GC_CALL GC_change_stubborn(const void *);
  473. /* Inform the collector that the object has been changed. */
  474. /* Only non-NULL pointer stores into the object are considered to be */
  475. /* changes. Matters only if the library has been compiled with */
  476. /* MANUAL_VDB defined (otherwise the function does nothing). */
  477. GC_API void GC_CALL GC_end_stubborn_change(const void *) GC_ATTR_NONNULL(1);
  478. /* Return a pointer to the base (lowest address) of an object given */
  479. /* a pointer to a location within the object. */
  480. /* I.e., map an interior pointer to the corresponding base pointer. */
  481. /* Note that with debugging allocation, this returns a pointer to the */
  482. /* actual base of the object, i.e. the debug information, not to */
  483. /* the base of the user object. */
  484. /* Return 0 if displaced_pointer doesn't point to within a valid */
  485. /* object. */
  486. /* Note that a deallocated object in the garbage collected heap */
  487. /* may be considered valid, even if it has been deallocated with */
  488. /* GC_free. */
  489. GC_API void * GC_CALL GC_base(void * /* displaced_pointer */);
  490. /* Return non-zero (TRUE) if and only if the argument points to */
  491. /* somewhere in GC heap. Primary use is as a fast alternative to */
  492. /* GC_base to check whether the pointed object is allocated by GC */
  493. /* or not. It is assumed that the collector is already initialized. */
  494. GC_API int GC_CALL GC_is_heap_ptr(const void *);
  495. /* Given a pointer to the base of an object, return its size in bytes. */
  496. /* The returned size may be slightly larger than what was originally */
  497. /* requested. */
  498. GC_API size_t GC_CALL GC_size(const void * /* obj_addr */) GC_ATTR_NONNULL(1);
  499. /* For compatibility with C library. This is occasionally faster than */
  500. /* a malloc followed by a bcopy. But if you rely on that, either here */
  501. /* or with the standard C library, your code is broken. In my */
  502. /* opinion, it shouldn't have been invented, but now we're stuck. -HB */
  503. /* The resulting object has the same kind as the original. */
  504. /* It is an error to have changes enabled for the original object. */
  505. /* It does not change the content of the object from its beginning to */
  506. /* the minimum of old size and new_size_in_bytes; the content above in */
  507. /* case of object size growth is initialized to zero (not guaranteed */
  508. /* for atomic object type). The function follows ANSI conventions for */
  509. /* NULL old_object (i.e., equivalent to GC_malloc regardless of new */
  510. /* size). If new size is zero (and old_object is non-NULL) then the */
  511. /* call is equivalent to GC_free (and NULL is returned). If old_object */
  512. /* is non-NULL, it must have been returned by an earlier call to */
  513. /* GC_malloc* or GC_realloc. In case of the allocation failure, the */
  514. /* memory pointed by old_object is untouched (and not freed). */
  515. /* If the returned pointer is not the same as old_object and both of */
  516. /* them are non-NULL then old_object is freed. Returns either NULL (in */
  517. /* case of the allocation failure or zero new size) or pointer to the */
  518. /* allocated memory. */
  519. GC_API void * GC_CALL GC_realloc(void * /* old_object */,
  520. size_t /* new_size_in_bytes */)
  521. /* 'realloc' attr */ GC_ATTR_ALLOC_SIZE(2);
  522. /* Explicitly increase the heap size. */
  523. /* Returns 0 on failure, 1 on success. */
  524. GC_API int GC_CALL GC_expand_hp(size_t /* number_of_bytes */);
  525. /* Limit the heap size to n bytes. Useful when you're debugging, */
  526. /* especially on systems that don't handle running out of memory well. */
  527. /* n == 0 ==> unbounded. This is the default. This setter function is */
  528. /* unsynchronized (so it might require GC_call_with_alloc_lock to avoid */
  529. /* data races). */
  530. GC_API void GC_CALL GC_set_max_heap_size(GC_word /* n */);
  531. /* Inform the collector that a certain section of statically allocated */
  532. /* memory contains no pointers to garbage collected memory. Thus it */
  533. /* need not be scanned. This is sometimes important if the application */
  534. /* maps large read/write files into the address space, which could be */
  535. /* mistaken for dynamic library data segments on some systems. */
  536. /* Both section start and end are not needed to be pointer-aligned. */
  537. GC_API void GC_CALL GC_exclude_static_roots(void * /* low_address */,
  538. void * /* high_address_plus_1 */);
  539. /* Clear the number of entries in the exclustion table. Wizards only. */
  540. GC_API void GC_CALL GC_clear_exclusion_table(void);
  541. /* Clear the set of root segments. Wizards only. */
  542. GC_API void GC_CALL GC_clear_roots(void);
  543. /* Add a root segment. Wizards only. */
  544. /* Both segment start and end are not needed to be pointer-aligned. */
  545. /* low_address must not be greater than high_address_plus_1. */
  546. GC_API void GC_CALL GC_add_roots(void * /* low_address */,
  547. void * /* high_address_plus_1 */);
  548. /* Remove a root segment. Wizards only. */
  549. /* May be unimplemented on some platforms. */
  550. GC_API void GC_CALL GC_remove_roots(void * /* low_address */,
  551. void * /* high_address_plus_1 */);
  552. /* Add a displacement to the set of those considered valid by the */
  553. /* collector. GC_register_displacement(n) means that if p was returned */
  554. /* by GC_malloc, then (char *)p + n will be considered to be a valid */
  555. /* pointer to p. N must be small and less than the size of p. */
  556. /* (All pointers to the interior of objects from the stack are */
  557. /* considered valid in any case. This applies to heap objects and */
  558. /* static data.) */
  559. /* Preferably, this should be called before any other GC procedures. */
  560. /* Calling it later adds to the probability of excess memory */
  561. /* retention. */
  562. /* This is a no-op if the collector has recognition of */
  563. /* arbitrary interior pointers enabled, which is now the default. */
  564. GC_API void GC_CALL GC_register_displacement(size_t /* n */);
  565. /* The following version should be used if any debugging allocation is */
  566. /* being done. */
  567. GC_API void GC_CALL GC_debug_register_displacement(size_t /* n */);
  568. /* Explicitly trigger a full, world-stop collection. */
  569. GC_API void GC_CALL GC_gcollect(void);
  570. /* Same as above but ignores the default stop_func setting and tries to */
  571. /* unmap as much memory as possible (regardless of the corresponding */
  572. /* switch setting). The recommended usage: on receiving a system */
  573. /* low-memory event; before retrying a system call failed because of */
  574. /* the system is running out of resources. */
  575. GC_API void GC_CALL GC_gcollect_and_unmap(void);
  576. /* Trigger a full world-stopped collection. Abort the collection if */
  577. /* and when stop_func returns a nonzero value. Stop_func will be */
  578. /* called frequently, and should be reasonably fast. (stop_func is */
  579. /* called with the allocation lock held and the world might be stopped; */
  580. /* it's not allowed for stop_func to manipulate pointers to the garbage */
  581. /* collected heap or call most of GC functions.) This works even */
  582. /* if virtual dirty bits, and hence incremental collection is not */
  583. /* available for this architecture. Collections can be aborted faster */
  584. /* than normal pause times for incremental collection. However, */
  585. /* aborted collections do no useful work; the next collection needs */
  586. /* to start from the beginning. stop_func must not be 0. */
  587. /* GC_try_to_collect() returns 0 if the collection was aborted (or the */
  588. /* collections are disabled), 1 if it succeeded. */
  589. typedef int (GC_CALLBACK * GC_stop_func)(void);
  590. GC_API int GC_CALL GC_try_to_collect(GC_stop_func /* stop_func */)
  591. GC_ATTR_NONNULL(1);
  592. /* Set and get the default stop_func. The default stop_func is used by */
  593. /* GC_gcollect() and by implicitly trigged collections (except for the */
  594. /* case when handling out of memory). Must not be 0. */
  595. /* Both the setter and getter acquire the GC lock to avoid data races. */
  596. GC_API void GC_CALL GC_set_stop_func(GC_stop_func /* stop_func */)
  597. GC_ATTR_NONNULL(1);
  598. GC_API GC_stop_func GC_CALL GC_get_stop_func(void);
  599. /* Return the number of bytes in the heap. Excludes collector private */
  600. /* data structures. Excludes the unmapped memory (returned to the OS). */
  601. /* Includes empty blocks and fragmentation loss. Includes some pages */
  602. /* that were allocated but never written. */
  603. /* This is an unsynchronized getter, so it should be called typically */
  604. /* with the GC lock held to avoid data races on multiprocessors (the */
  605. /* alternative is to use GC_get_heap_usage_safe or GC_get_prof_stats */
  606. /* API calls instead). */
  607. /* This getter remains lock-free (unsynchronized) for compatibility */
  608. /* reason since some existing clients call it from a GC callback */
  609. /* holding the allocator lock. (This API function and the following */
  610. /* four ones below were made thread-safe in GC v7.2alpha1 and */
  611. /* reverted back in v7.2alpha7 for the reason described.) */
  612. GC_API size_t GC_CALL GC_get_heap_size(void);
  613. /* Return a lower bound on the number of free bytes in the heap */
  614. /* (excluding the unmapped memory space). This is an unsynchronized */
  615. /* getter (see GC_get_heap_size comment regarding thread-safety). */
  616. GC_API size_t GC_CALL GC_get_free_bytes(void);
  617. /* Return the size (in bytes) of the unmapped memory (which is returned */
  618. /* to the OS but could be remapped back by the collector later unless */
  619. /* the OS runs out of system/virtual memory). This is an unsynchronized */
  620. /* getter (see GC_get_heap_size comment regarding thread-safety). */
  621. GC_API size_t GC_CALL GC_get_unmapped_bytes(void);
  622. /* Return the number of bytes allocated since the last collection. */
  623. /* This is an unsynchronized getter (see GC_get_heap_size comment */
  624. /* regarding thread-safety). */
  625. GC_API size_t GC_CALL GC_get_bytes_since_gc(void);
  626. /* Return the number of explicitly deallocated bytes of memory since */
  627. /* the recent collection. This is an unsynchronized getter. */
  628. GC_API size_t GC_CALL GC_get_expl_freed_bytes_since_gc(void);
  629. /* Return the total number of bytes allocated in this process. */
  630. /* Never decreases, except due to wrapping. This is an unsynchronized */
  631. /* getter (see GC_get_heap_size comment regarding thread-safety). */
  632. GC_API size_t GC_CALL GC_get_total_bytes(void);
  633. /* Return the heap usage information. This is a thread-safe (atomic) */
  634. /* alternative for the five above getters. (This function acquires */
  635. /* the allocator lock thus preventing data racing and returning the */
  636. /* consistent result.) Passing NULL pointer is allowed for any */
  637. /* argument. Returned (filled in) values are of word type. */
  638. /* (This API function was introduced in GC v7.2alpha7 at the same time */
  639. /* when GC_get_heap_size and the friends were made lock-free again.) */
  640. GC_API void GC_CALL GC_get_heap_usage_safe(GC_word * /* pheap_size */,
  641. GC_word * /* pfree_bytes */,
  642. GC_word * /* punmapped_bytes */,
  643. GC_word * /* pbytes_since_gc */,
  644. GC_word * /* ptotal_bytes */);
  645. /* Structure used to query GC statistics (profiling information). */
  646. /* More fields could be added in the future. To preserve compatibility */
  647. /* new fields should be added only to the end, and no deprecated fields */
  648. /* should be removed from. */
  649. struct GC_prof_stats_s {
  650. GC_word heapsize_full;
  651. /* Heap size in bytes (including the area unmapped to OS). */
  652. /* Same as GC_get_heap_size() + GC_get_unmapped_bytes(). */
  653. GC_word free_bytes_full;
  654. /* Total bytes contained in free and unmapped blocks. */
  655. /* Same as GC_get_free_bytes() + GC_get_unmapped_bytes(). */
  656. GC_word unmapped_bytes;
  657. /* Amount of memory unmapped to OS. Same as the value */
  658. /* returned by GC_get_unmapped_bytes(). */
  659. GC_word bytes_allocd_since_gc;
  660. /* Number of bytes allocated since the recent collection. */
  661. /* Same as returned by GC_get_bytes_since_gc(). */
  662. GC_word allocd_bytes_before_gc;
  663. /* Number of bytes allocated before the recent garbage */
  664. /* collection. The value may wrap. Same as the result of */
  665. /* GC_get_total_bytes() - GC_get_bytes_since_gc(). */
  666. GC_word non_gc_bytes;
  667. /* Number of bytes not considered candidates for garbage */
  668. /* collection. Same as returned by GC_get_non_gc_bytes(). */
  669. GC_word gc_no;
  670. /* Garbage collection cycle number. The value may wrap */
  671. /* (and could be -1). Same as returned by GC_get_gc_no(). */
  672. GC_word markers_m1;
  673. /* Number of marker threads (excluding the initiating one). */
  674. /* Same as returned by GC_get_parallel (or 0 if the */
  675. /* collector is single-threaded). */
  676. GC_word bytes_reclaimed_since_gc;
  677. /* Approximate number of reclaimed bytes after recent GC. */
  678. GC_word reclaimed_bytes_before_gc;
  679. /* Approximate number of bytes reclaimed before the recent */
  680. /* garbage collection. The value may wrap. */
  681. GC_word expl_freed_bytes_since_gc;
  682. /* Number of bytes freed explicitly since the recent GC. */
  683. /* Same as returned by GC_get_expl_freed_bytes_since_gc(). */
  684. };
  685. /* Atomically get GC statistics (various global counters). Clients */
  686. /* should pass the size of the buffer (of GC_prof_stats_s type) to fill */
  687. /* in the values - this is for interoperability between different GC */
  688. /* versions, an old client could have fewer fields, and vice versa, */
  689. /* client could use newer gc.h (with more entries declared in the */
  690. /* structure) than that of the linked libgc binary; in the latter case, */
  691. /* unsupported (unknown) fields are filled in with -1. Return the size */
  692. /* (in bytes) of the filled in part of the structure (excluding all */
  693. /* unknown fields, if any). */
  694. GC_API size_t GC_CALL GC_get_prof_stats(struct GC_prof_stats_s *,
  695. size_t /* stats_sz */);
  696. #ifdef GC_THREADS
  697. /* Same as above but unsynchronized (i.e., not holding the allocation */
  698. /* lock). Clients should call it using GC_call_with_alloc_lock to */
  699. /* avoid data races on multiprocessors. */
  700. GC_API size_t GC_CALL GC_get_prof_stats_unsafe(struct GC_prof_stats_s *,
  701. size_t /* stats_sz */);
  702. #endif
  703. /* Get the element value (converted to bytes) at a given index of */
  704. /* size_map table which provides requested-to-actual allocation size */
  705. /* mapping. Assumes the collector is initialized. Returns -1 if the */
  706. /* index is out of size_map table bounds. Does not use synchronization, */
  707. /* thus clients should call it using GC_call_with_alloc_lock typically */
  708. /* to avoid data races on multiprocessors. */
  709. GC_API size_t GC_CALL GC_get_size_map_at(int i);
  710. /* Count total memory use in bytes by all allocated blocks. Acquires */
  711. /* the lock. */
  712. GC_API size_t GC_CALL GC_get_memory_use(void);
  713. /* Disable garbage collection. Even GC_gcollect calls will be */
  714. /* ineffective. */
  715. GC_API void GC_CALL GC_disable(void);
  716. /* Return non-zero (TRUE) if and only if garbage collection is disabled */
  717. /* (i.e., GC_dont_gc value is non-zero). Does not acquire the lock. */
  718. GC_API int GC_CALL GC_is_disabled(void);
  719. /* Try to re-enable garbage collection. GC_disable() and GC_enable() */
  720. /* calls nest. Garbage collection is enabled if the number of calls to */
  721. /* both functions is equal. */
  722. GC_API void GC_CALL GC_enable(void);
  723. /* Enable incremental/generational collection. Not advisable unless */
  724. /* dirty bits are available or most heap objects are pointer-free */
  725. /* (atomic) or immutable. Don't use in leak finding mode. Ignored if */
  726. /* GC_dont_gc is non-zero. Only the generational piece of this is */
  727. /* functional if GC_parallel is non-zero or if GC_time_limit is */
  728. /* GC_TIME_UNLIMITED. Causes thread-local variant of GC_gcj_malloc() */
  729. /* to revert to locked allocation. Must be called before any such */
  730. /* GC_gcj_malloc() calls. For best performance, should be called as */
  731. /* early as possible. On some platforms, calling it later may have */
  732. /* adverse effects. */
  733. /* Safe to call before GC_INIT(). Includes a GC_init() call. */
  734. GC_API void GC_CALL GC_enable_incremental(void);
  735. /* Return non-zero (TRUE) if and only if the incremental mode is on. */
  736. /* Does not acquire the lock. */
  737. GC_API int GC_CALL GC_is_incremental_mode(void);
  738. /* Does incremental mode write-protect pages? Returns zero or */
  739. /* more of the following, or'ed together: */
  740. #define GC_PROTECTS_POINTER_HEAP 1 /* May protect non-atomic objects. */
  741. #define GC_PROTECTS_PTRFREE_HEAP 2
  742. #define GC_PROTECTS_STATIC_DATA 4 /* Currently never. */
  743. #define GC_PROTECTS_STACK 8 /* Probably impractical. */
  744. #define GC_PROTECTS_NONE 0
  745. /* The collector is assumed to be initialized before this call. */
  746. GC_API int GC_CALL GC_incremental_protection_needs(void);
  747. /* Perform some garbage collection work, if appropriate. */
  748. /* Return 0 if there is no more work to be done (including the */
  749. /* case when garbage collection is not appropriate). */
  750. /* Typically performs an amount of work corresponding roughly */
  751. /* to marking from one page. May do more work if further */
  752. /* progress requires it, e.g. if incremental collection is */
  753. /* disabled. It is reasonable to call this in a wait loop */
  754. /* until it returns 0. */
  755. GC_API int GC_CALL GC_collect_a_little(void);
  756. /* Allocate an object of size lb bytes. The client guarantees that */
  757. /* as long as the object is live, it will be referenced by a pointer */
  758. /* that points to somewhere within the first 256 bytes of the object. */
  759. /* (This should normally be declared volatile to prevent the compiler */
  760. /* from invalidating this assertion.) This routine is only useful */
  761. /* if a large array is being allocated. It reduces the chance of */
  762. /* accidentally retaining such an array as a result of scanning an */
  763. /* integer that happens to be an address inside the array. (Actually, */
  764. /* it reduces the chance of the allocator not finding space for such */
  765. /* an array, since it will try hard to avoid introducing such a false */
  766. /* reference.) On a SunOS 4.X or MS Windows system this is recommended */
  767. /* for arrays likely to be larger than 100K or so. For other systems, */
  768. /* or if the collector is not configured to recognize all interior */
  769. /* pointers, the threshold is normally much higher. */
  770. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  771. GC_malloc_ignore_off_page(size_t /* lb */);
  772. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  773. GC_malloc_atomic_ignore_off_page(size_t /* lb */);
  774. #ifdef GC_ADD_CALLER
  775. # define GC_EXTRAS GC_RETURN_ADDR, __FILE__, __LINE__
  776. # define GC_EXTRA_PARAMS GC_word ra, const char * s, int i
  777. #else
  778. # define GC_EXTRAS __FILE__, __LINE__
  779. # define GC_EXTRA_PARAMS const char * s, int i
  780. #endif
  781. /* The following is only defined if the library has been suitably */
  782. /* compiled: */
  783. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  784. GC_malloc_atomic_uncollectable(size_t /* size_in_bytes */);
  785. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  786. GC_debug_malloc_atomic_uncollectable(size_t, GC_EXTRA_PARAMS);
  787. /* Debugging (annotated) allocation. GC_gcollect will check */
  788. /* objects allocated in this way for overwrites, etc. */
  789. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  790. GC_debug_malloc(size_t /* size_in_bytes */, GC_EXTRA_PARAMS);
  791. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  792. GC_debug_malloc_atomic(size_t /* size_in_bytes */, GC_EXTRA_PARAMS);
  793. GC_API GC_ATTR_MALLOC char * GC_CALL
  794. GC_debug_strdup(const char *, GC_EXTRA_PARAMS);
  795. GC_API GC_ATTR_MALLOC char * GC_CALL
  796. GC_debug_strndup(const char *, size_t, GC_EXTRA_PARAMS)
  797. GC_ATTR_NONNULL(1);
  798. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  799. GC_debug_malloc_uncollectable(size_t /* size_in_bytes */,
  800. GC_EXTRA_PARAMS);
  801. GC_API GC_ATTR_DEPRECATED void * GC_CALL
  802. GC_debug_malloc_stubborn(size_t /* size_in_bytes */, GC_EXTRA_PARAMS);
  803. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  804. GC_debug_malloc_ignore_off_page(size_t /* size_in_bytes */,
  805. GC_EXTRA_PARAMS);
  806. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  807. GC_debug_malloc_atomic_ignore_off_page(size_t /* size_in_bytes */,
  808. GC_EXTRA_PARAMS);
  809. GC_API void GC_CALL GC_debug_free(void *);
  810. GC_API void * GC_CALL GC_debug_realloc(void * /* old_object */,
  811. size_t /* new_size_in_bytes */, GC_EXTRA_PARAMS)
  812. /* 'realloc' attr */ GC_ATTR_ALLOC_SIZE(2);
  813. GC_API
  814. #if !defined(CPPCHECK)
  815. GC_ATTR_DEPRECATED
  816. #endif
  817. void GC_CALL GC_debug_change_stubborn(const void *);
  818. GC_API void GC_CALL GC_debug_end_stubborn_change(const void *)
  819. GC_ATTR_NONNULL(1);
  820. /* Routines that allocate objects with debug information (like the */
  821. /* above), but just fill in dummy file and line number information. */
  822. /* Thus they can serve as drop-in malloc/realloc replacements. This */
  823. /* can be useful for two reasons: */
  824. /* 1) It allows the collector to be built with DBG_HDRS_ALL defined */
  825. /* even if some allocation calls come from 3rd party libraries */
  826. /* that can't be recompiled. */
  827. /* 2) On some platforms, the file and line information is redundant, */
  828. /* since it can be reconstructed from a stack trace. On such */
  829. /* platforms it may be more convenient not to recompile, e.g. for */
  830. /* leak detection. This can be accomplished by instructing the */
  831. /* linker to replace malloc/realloc with these. */
  832. GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
  833. GC_debug_malloc_replacement(size_t /* size_in_bytes */);
  834. GC_API /* 'realloc' attr */ GC_ATTR_ALLOC_SIZE(2) void * GC_CALL
  835. GC_debug_realloc_replacement(void * /* object_addr */,
  836. size_t /* size_in_bytes */);
  837. #ifdef GC_DEBUG_REPLACEMENT
  838. # define GC_MALLOC(sz) GC_debug_malloc_replacement(sz)
  839. # define GC_REALLOC(old, sz) GC_debug_realloc_replacement(old, sz)
  840. #elif defined(GC_DEBUG)
  841. # define GC_MALLOC(sz) GC_debug_malloc(sz, GC_EXTRAS)
  842. # define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, GC_EXTRAS)
  843. #else
  844. # define GC_MALLOC(sz) GC_malloc(sz)
  845. # define GC_REALLOC(old, sz) GC_realloc(old, sz)
  846. #endif /* !GC_DEBUG_REPLACEMENT && !GC_DEBUG */
  847. #ifdef GC_DEBUG
  848. # define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, GC_EXTRAS)
  849. # define GC_STRDUP(s) GC_debug_strdup(s, GC_EXTRAS)
  850. # define GC_STRNDUP(s, sz) GC_debug_strndup(s, sz, GC_EXTRAS)
  851. # define GC_MALLOC_ATOMIC_UNCOLLECTABLE(sz) \
  852. GC_debug_malloc_atomic_uncollectable(sz, GC_EXTRAS)
  853. # define GC_MALLOC_UNCOLLECTABLE(sz) \
  854. GC_debug_malloc_uncollectable(sz, GC_EXTRAS)
  855. # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
  856. GC_debug_malloc_ignore_off_page(sz, GC_EXTRAS)
  857. # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
  858. GC_debug_malloc_atomic_ignore_off_page(sz, GC_EXTRAS)
  859. # define GC_FREE(p) GC_debug_free(p)
  860. # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
  861. GC_debug_register_finalizer(p, f, d, of, od)
  862. # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
  863. GC_debug_register_finalizer_ignore_self(p, f, d, of, od)
  864. # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
  865. GC_debug_register_finalizer_no_order(p, f, d, of, od)
  866. # define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \
  867. GC_debug_register_finalizer_unreachable(p, f, d, of, od)
  868. # define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p)
  869. # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
  870. GC_general_register_disappearing_link(link, \
  871. GC_base((/* no const */ void *)(obj)))
  872. # define GC_REGISTER_LONG_LINK(link, obj) \
  873. GC_register_long_link(link, GC_base((/* no const */ void *)(obj)))
  874. # define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n)
  875. #else
  876. # define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz)
  877. # define GC_STRDUP(s) GC_strdup(s)
  878. # define GC_STRNDUP(s, sz) GC_strndup(s, sz)
  879. # define GC_MALLOC_ATOMIC_UNCOLLECTABLE(sz) GC_malloc_atomic_uncollectable(sz)
  880. # define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz)
  881. # define GC_MALLOC_IGNORE_OFF_PAGE(sz) \
  882. GC_malloc_ignore_off_page(sz)
  883. # define GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(sz) \
  884. GC_malloc_atomic_ignore_off_page(sz)
  885. # define GC_FREE(p) GC_free(p)
  886. # define GC_REGISTER_FINALIZER(p, f, d, of, od) \
  887. GC_register_finalizer(p, f, d, of, od)
  888. # define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
  889. GC_register_finalizer_ignore_self(p, f, d, of, od)
  890. # define GC_REGISTER_FINALIZER_NO_ORDER(p, f, d, of, od) \
  891. GC_register_finalizer_no_order(p, f, d, of, od)
  892. # define GC_REGISTER_FINALIZER_UNREACHABLE(p, f, d, of, od) \
  893. GC_register_finalizer_unreachable(p, f, d, of, od)
  894. # define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p)
  895. # define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
  896. GC_general_register_disappearing_link(link, obj)
  897. # define GC_REGISTER_LONG_LINK(link, obj) \
  898. GC_register_long_link(link, obj)
  899. # define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n)
  900. #endif /* !GC_DEBUG */
  901. /* The following are included because they are often convenient, and */
  902. /* reduce the chance for a misspecified size argument. But calls may */
  903. /* expand to something syntactically incorrect if t is a complicated */
  904. /* type expression. Note that, unlike C++ new operator, these ones */
  905. /* may return NULL (if out of memory). */
  906. #define GC_NEW(t) ((t*)GC_MALLOC(sizeof(t)))
  907. #define GC_NEW_ATOMIC(t) ((t*)GC_MALLOC_ATOMIC(sizeof(t)))
  908. #define GC_NEW_UNCOLLECTABLE(t) ((t*)GC_MALLOC_UNCOLLECTABLE(sizeof(t)))
  909. #ifdef GC_REQUIRE_WCSDUP
  910. /* This might be unavailable on some targets (or not needed). */
  911. /* wchar_t should be defined in stddef.h */
  912. GC_API GC_ATTR_MALLOC wchar_t * GC_CALL
  913. GC_wcsdup(const wchar_t *) GC_ATTR_NONNULL(1);
  914. GC_API GC_ATTR_MALLOC wchar_t * GC_CALL
  915. GC_debug_wcsdup(const wchar_t *, GC_EXTRA_PARAMS) GC_ATTR_NONNULL(1);
  916. # ifdef GC_DEBUG
  917. # define GC_WCSDUP(s) GC_debug_wcsdup(s, GC_EXTRAS)
  918. # else
  919. # define GC_WCSDUP(s) GC_wcsdup(s)
  920. # endif
  921. #endif /* GC_REQUIRE_WCSDUP */
  922. /* Finalization. Some of these primitives are grossly unsafe. */
  923. /* The idea is to make them both cheap, and sufficient to build */
  924. /* a safer layer, closer to Modula-3, Java, or PCedar finalization. */
  925. /* The interface represents my conclusions from a long discussion */
  926. /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes, */
  927. /* Christian Jacobi, and Russ Atkinson. It's not perfect, and */
  928. /* probably nobody else agrees with it. Hans-J. Boehm 3/13/92 */
  929. typedef void (GC_CALLBACK * GC_finalization_proc)(void * /* obj */,
  930. void * /* client_data */);
  931. GC_API void GC_CALL GC_register_finalizer(void * /* obj */,
  932. GC_finalization_proc /* fn */, void * /* cd */,
  933. GC_finalization_proc * /* ofn */, void ** /* ocd */)
  934. GC_ATTR_NONNULL(1);
  935. GC_API void GC_CALL GC_debug_register_finalizer(void * /* obj */,
  936. GC_finalization_proc /* fn */, void * /* cd */,
  937. GC_finalization_proc * /* ofn */, void ** /* ocd */)
  938. GC_ATTR_NONNULL(1);
  939. /* When obj is no longer accessible, invoke */
  940. /* (*fn)(obj, cd). If a and b are inaccessible, and */
  941. /* a points to b (after disappearing links have been */
  942. /* made to disappear), then only a will be */
  943. /* finalized. (If this does not create any new */
  944. /* pointers to b, then b will be finalized after the */
  945. /* next collection.) Any finalizable object that */
  946. /* is reachable from itself by following one or more */
  947. /* pointers will not be finalized (or collected). */
  948. /* Thus cycles involving finalizable objects should */
  949. /* be avoided, or broken by disappearing links. */
  950. /* All but the last finalizer registered for an object */
  951. /* is ignored. */
  952. /* No-op in the leak-finding mode. */
  953. /* Finalization may be removed by passing 0 as fn. */
  954. /* Finalizers are implicitly unregistered when they are */
  955. /* enqueued for finalization (i.e. become ready to be */
  956. /* finalized). */
  957. /* The old finalizer and client data are stored in */
  958. /* *ofn and *ocd. (ofn and/or ocd may be NULL. */
  959. /* The allocation lock is held while *ofn and *ocd are */
  960. /* updated. In case of error (no memory to register */
  961. /* new finalizer), *ofn and *ocd remain unchanged.) */
  962. /* Fn is never invoked on an accessible object, */
  963. /* provided hidden pointers are converted to real */
  964. /* pointers only if the allocation lock is held, and */
  965. /* such conversions are not performed by finalization */
  966. /* routines. */
  967. /* If GC_register_finalizer is aborted as a result of */
  968. /* a signal, the object may be left with no */
  969. /* finalization, even if neither the old nor new */
  970. /* finalizer were NULL. */
  971. /* Obj should be the starting address of an object */
  972. /* allocated by GC_malloc or friends. Obj may also be */
  973. /* NULL or point to something outside GC heap (in this */
  974. /* case, fn is ignored, *ofn and *ocd are set to NULL). */
  975. /* Note that any garbage collectible object referenced */
  976. /* by cd will be considered accessible until the */
  977. /* finalizer is invoked. */
  978. /* Another versions of the above follow. It ignores */
  979. /* self-cycles, i.e. pointers from a finalizable object to */
  980. /* itself. There is a stylistic argument that this is wrong, */
  981. /* but it's unavoidable for C++, since the compiler may */
  982. /* silently introduce these. It's also benign in that specific */
  983. /* case. And it helps if finalizable objects are split to */
  984. /* avoid cycles. */
  985. /* Note that cd will still be viewed as accessible, even if it */
  986. /* refers to the object itself. */
  987. GC_API void GC_CALL GC_register_finalizer_ignore_self(void * /* obj */,
  988. GC_finalization_proc /* fn */, void * /* cd */,
  989. GC_finalization_proc * /* ofn */, void ** /* ocd */)
  990. GC_ATTR_NONNULL(1);
  991. GC_API void GC_CALL GC_debug_register_finalizer_ignore_self(void * /* obj */,
  992. GC_finalization_proc /* fn */, void * /* cd */,
  993. GC_finalization_proc * /* ofn */, void ** /* ocd */)
  994. GC_ATTR_NONNULL(1);
  995. /* Another version of the above. It ignores all cycles. */
  996. /* It should probably only be used by Java implementations. */
  997. /* Note that cd will still be viewed as accessible, even if it */
  998. /* refers to the object itself. */
  999. GC_API void GC_CALL GC_register_finalizer_no_order(void * /* obj */,
  1000. GC_finalization_proc /* fn */, void * /* cd */,
  1001. GC_finalization_proc * /* ofn */, void ** /* ocd */)
  1002. GC_ATTR_NONNULL(1);
  1003. GC_API void GC_CALL GC_debug_register_finalizer_no_order(void * /* obj */,
  1004. GC_finalization_proc /* fn */, void * /* cd */,
  1005. GC_finalization_proc * /* ofn */, void ** /* ocd */)
  1006. GC_ATTR_NONNULL(1);
  1007. /* This is a special finalizer that is useful when an object's */
  1008. /* finalizer must be run when the object is known to be no */
  1009. /* longer reachable, not even from other finalizable objects. */
  1010. /* It behaves like "normal" finalization, except that the */
  1011. /* finalizer is not run while the object is reachable from */
  1012. /* other objects specifying unordered finalization. */
  1013. /* Effectively it allows an object referenced, possibly */
  1014. /* indirectly, from an unordered finalizable object to override */
  1015. /* the unordered finalization request. */
  1016. /* This can be used in combination with finalizer_no_order so */
  1017. /* as to release resources that must not be released while an */
  1018. /* object can still be brought back to life by other */
  1019. /* finalizers. */
  1020. /* Only works if GC_java_finalization is set. Probably only */
  1021. /* of interest when implementing a language that requires */
  1022. /* unordered finalization (e.g. Java, C#). */
  1023. GC_API void GC_CALL GC_register_finalizer_unreachable(void * /* obj */,
  1024. GC_finalization_proc /* fn */, void * /* cd */,
  1025. GC_finalization_proc * /* ofn */, void ** /* ocd */)
  1026. GC_ATTR_NONNULL(1);
  1027. GC_API void GC_CALL GC_debug_register_finalizer_unreachable(void * /* obj */,
  1028. GC_finalization_proc /* fn */, void * /* cd */,
  1029. GC_finalization_proc * /* ofn */, void ** /* ocd */)
  1030. GC_ATTR_NONNULL(1);
  1031. #define GC_NO_MEMORY 2 /* Failure due to lack of memory. */
  1032. /* The following routine may be used to break cycles between */
  1033. /* finalizable objects, thus causing cyclic finalizable */
  1034. /* objects to be finalized in the correct order. Standard */
  1035. /* use involves calling GC_register_disappearing_link(&p), */
  1036. /* where p is a pointer that is not followed by finalization */
  1037. /* code, and should not be considered in determining */
  1038. /* finalization order. */
  1039. GC_API int GC_CALL GC_register_disappearing_link(void ** /* link */)
  1040. GC_ATTR_NONNULL(1);
  1041. /* Link should point to a field of a heap allocated */
  1042. /* object obj. *link will be cleared when obj is */
  1043. /* found to be inaccessible. This happens BEFORE any */
  1044. /* finalization code is invoked, and BEFORE any */
  1045. /* decisions about finalization order are made. */
  1046. /* This is useful in telling the finalizer that */
  1047. /* some pointers are not essential for proper */
  1048. /* finalization. This may avoid finalization cycles. */
  1049. /* Note that obj may be resurrected by another */
  1050. /* finalizer, and thus the clearing of *link may */
  1051. /* be visible to non-finalization code. */
  1052. /* There's an argument that an arbitrary action should */
  1053. /* be allowed here, instead of just clearing a pointer. */
  1054. /* But this causes problems if that action alters, or */
  1055. /* examines connectivity. Returns GC_DUPLICATE if link */
  1056. /* was already registered, GC_SUCCESS if registration */
  1057. /* succeeded, GC_NO_MEMORY if it failed for lack of */
  1058. /* memory, and GC_oom_fn did not handle the problem. */
  1059. /* Only exists for backward compatibility. See below: */
  1060. GC_API int GC_CALL GC_general_register_disappearing_link(void ** /* link */,
  1061. const void * /* obj */)
  1062. GC_ATTR_NONNULL(1) GC_ATTR_NONNULL(2);
  1063. /* A slight generalization of the above. *link is */
  1064. /* cleared when obj first becomes inaccessible. This */
  1065. /* can be used to implement weak pointers easily and */
  1066. /* safely. Typically link will point to a location */
  1067. /* holding a disguised pointer to obj. (A pointer */
  1068. /* inside an "atomic" object is effectively disguised.) */
  1069. /* In this way, weak pointers are broken before any */
  1070. /* object reachable from them gets finalized. */
  1071. /* Each link may be registered only with one obj value, */
  1072. /* i.e. all objects but the last one (link registered */
  1073. /* with) are ignored. This was added after a long */
  1074. /* email discussion with John Ellis. */
  1075. /* link must be non-NULL (and be properly aligned). */
  1076. /* obj must be a pointer to the first word of an object */
  1077. /* allocated by GC_malloc or friends. A link */
  1078. /* disappears when it is unregistered manually, or when */
  1079. /* (*link) is cleared, or when the object containing */
  1080. /* this link is garbage collected. It is unsafe to */
  1081. /* explicitly deallocate the object containing link. */
  1082. /* Explicit deallocation of obj may or may not cause */
  1083. /* link to eventually be cleared. */
  1084. /* No-op in the leak-finding mode. */
  1085. /* This function can be used to implement certain types */
  1086. /* of weak pointers. Note, however, this generally */
  1087. /* requires that the allocation lock is held (see */
  1088. /* GC_call_with_alloc_lock() below) when the disguised */
  1089. /* pointer is accessed. Otherwise a strong pointer */
  1090. /* could be recreated between the time the collector */
  1091. /* decides to reclaim the object and the link is */
  1092. /* cleared. Returns GC_SUCCESS if registration */
  1093. /* succeeded (a new link is registered), GC_DUPLICATE */
  1094. /* if link was already registered (with some object), */
  1095. /* GC_NO_MEMORY if registration failed for lack of */
  1096. /* memory (and GC_oom_fn did not handle the problem), */
  1097. /* GC_UNIMPLEMENTED if GC_find_leak is true. */
  1098. GC_API int GC_CALL GC_move_disappearing_link(void ** /* link */,
  1099. void ** /* new_link */)
  1100. GC_ATTR_NONNULL(2);
  1101. /* Moves a link previously registered via */
  1102. /* GC_general_register_disappearing_link (or */
  1103. /* GC_register_disappearing_link). Does not change the */
  1104. /* target object of the weak reference. Does not */
  1105. /* change (*new_link) content. May be called with */
  1106. /* new_link equal to link (to check whether link has */
  1107. /* been registered). Returns GC_SUCCESS on success, */
  1108. /* GC_DUPLICATE if there is already another */
  1109. /* disappearing link at the new location (never */
  1110. /* returned if new_link is equal to link), GC_NOT_FOUND */
  1111. /* if no link is registered at the original location. */
  1112. GC_API int GC_CALL GC_unregister_disappearing_link(void ** /* link */);
  1113. /* Undoes a registration by either of the above two */
  1114. /* routines. Returns 0 if link was not actually */
  1115. /* registered (otherwise returns 1). */
  1116. GC_API int GC_CALL GC_register_long_link(void ** /* link */,
  1117. const void * /* obj */)
  1118. GC_ATTR_NONNULL(1) GC_ATTR_NONNULL(2);
  1119. /* Similar to GC_general_register_disappearing_link but */
  1120. /* *link only gets cleared when obj becomes truly */
  1121. /* inaccessible. An object becomes truly inaccessible */
  1122. /* when it can no longer be resurrected from its */
  1123. /* finalizer (e.g. by assigning itself to a pointer */
  1124. /* traceable from root). This can be used to implement */
  1125. /* long weak pointers easily and safely. */
  1126. GC_API int GC_CALL GC_move_long_link(void ** /* link */,
  1127. void ** /* new_link */)
  1128. GC_ATTR_NONNULL(2);
  1129. /* Similar to GC_move_disappearing_link but for a link */
  1130. /* previously registered via GC_register_long_link. */
  1131. GC_API int GC_CALL GC_unregister_long_link(void ** /* link */);
  1132. /* Similar to GC_unregister_disappearing_link but for a */
  1133. /* registration by either of the above two routines. */
  1134. /* Support of toggle-ref style of external memory management */
  1135. /* without hooking up to the host retain/release machinery. */
  1136. /* The idea of toggle-ref is that an external reference to */
  1137. /* an object is kept and it can be either a strong or weak */
  1138. /* reference; a weak reference is used when the external peer */
  1139. /* has no interest in the object, and a strong otherwise. */
  1140. typedef enum {
  1141. GC_TOGGLE_REF_DROP,
  1142. GC_TOGGLE_REF_STRONG,
  1143. GC_TOGGLE_REF_WEAK
  1144. } GC_ToggleRefStatus;
  1145. /* The callback is to decide (return) the new state of a given */
  1146. /* object. Invoked by the collector for all objects registered */
  1147. /* for toggle-ref processing. Invoked with the allocation lock */
  1148. /* held (but the "world" is running). */
  1149. typedef GC_ToggleRefStatus (GC_CALLBACK *GC_toggleref_func)(void * /* obj */);
  1150. /* Set (register) a callback that decides the state of a given */
  1151. /* object (by, probably, inspecting its native state). */
  1152. /* The argument may be 0 (means no callback). Both the setter */
  1153. /* and the getter acquire the allocation lock (to avoid data */
  1154. /* races). */
  1155. GC_API void GC_CALL GC_set_toggleref_func(GC_toggleref_func);
  1156. GC_API GC_toggleref_func GC_CALL GC_get_toggleref_func(void);
  1157. /* Register a given object for toggle-ref processing. It will */
  1158. /* be stored internally and the toggle-ref callback will be */
  1159. /* invoked on the object until the callback returns */
  1160. /* GC_TOGGLE_REF_DROP or the object is collected. If is_strong */
  1161. /* is true then the object is registered with a strong ref, */
  1162. /* a weak one otherwise. Returns GC_SUCCESS if registration */
  1163. /* succeeded (or no callback registered yet), GC_NO_MEMORY if */
  1164. /* it failed for lack of memory. */
  1165. GC_API int GC_CALL GC_toggleref_add(void * /* obj */, int /* is_strong */)
  1166. GC_ATTR_NONNULL(1);
  1167. /* Finalizer callback support. Invoked by the collector (with */
  1168. /* the allocation lock held) for each unreachable object */
  1169. /* enqueued for finalization. */
  1170. typedef void (GC_CALLBACK * GC_await_finalize_proc)(void * /* obj */);
  1171. GC_API void GC_CALL GC_set_await_finalize_proc(GC_await_finalize_proc);
  1172. GC_API GC_await_finalize_proc GC_CALL GC_get_await_finalize_proc(void);
  1173. /* Zero means no callback. The setter */
  1174. /* and getter acquire the lock too. */
  1175. /* Returns !=0 if GC_invoke_finalizers has something to do. */
  1176. /* Does not use any synchronization. */
  1177. GC_API int GC_CALL GC_should_invoke_finalizers(void);
  1178. /* Sets flag to signal GC_invoke_finalizers to break */
  1179. /* Does not use any synchronization. */
  1180. GC_API void GC_CALL GC_set_interrupt_finalizers(void);
  1181. GC_API int GC_CALL GC_invoke_finalizers(void);
  1182. /* Run finalizers for all objects that are ready to */
  1183. /* be finalized. Return the number of finalizers */
  1184. /* that were run. Normally this is also called */
  1185. /* implicitly during some allocations. If */
  1186. /* GC_finalize_on_demand is nonzero, it must be called */
  1187. /* explicitly. */
  1188. /* Explicitly tell the collector that an object is reachable */
  1189. /* at a particular program point. This prevents the argument */
  1190. /* pointer from being optimized away, even it is otherwise no */
  1191. /* longer needed. It should have no visible effect in the */
  1192. /* absence of finalizers or disappearing links. But it may be */
  1193. /* needed to prevent finalizers from running while the */
  1194. /* associated external resource is still in use. */
  1195. /* The function is sometimes called keep_alive in other */
  1196. /* settings. */
  1197. #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
  1198. # define GC_reachable_here(ptr) \
  1199. __asm__ __volatile__(" " : : "X"(ptr) : "memory")
  1200. #else
  1201. GC_API void GC_CALL GC_noop1(GC_word);
  1202. # define GC_reachable_here(ptr) GC_noop1((GC_word)(ptr))
  1203. #endif
  1204. /* GC_set_warn_proc can be used to redirect or filter warning messages. */
  1205. /* p may not be a NULL pointer. msg is printf format string (arg must */
  1206. /* match the format). Both the setter and the getter acquire the GC */
  1207. /* lock (to avoid data races). In version 7.1 (and before), the setter */
  1208. /* returned the old warn_proc value. */
  1209. typedef void (GC_CALLBACK * GC_warn_proc)(char * /* msg */,
  1210. GC_word /* arg */);
  1211. GC_API void GC_CALL GC_set_warn_proc(GC_warn_proc /* p */) GC_ATTR_NONNULL(1);
  1212. /* GC_get_warn_proc returns the current warn_proc. */
  1213. GC_API GC_warn_proc GC_CALL GC_get_warn_proc(void);
  1214. /* GC_ignore_warn_proc may be used as an argument for GC_set_warn_proc */
  1215. /* to suppress all warnings (unless statistics printing is turned on). */
  1216. GC_API void GC_CALLBACK GC_ignore_warn_proc(char *, GC_word);
  1217. /* Change file descriptor of GC log. Unavailable on some targets. */
  1218. GC_API void GC_CALL GC_set_log_fd(int);
  1219. /* abort_func is invoked on GC fatal aborts (just before OS-dependent */
  1220. /* abort or exit(1) is called). Must be non-NULL. The default one */
  1221. /* outputs msg to stderr provided msg is non-NULL. msg is NULL if */
  1222. /* invoked before exit(1) otherwise msg is non-NULL (i.e., if invoked */
  1223. /* before abort). Both the setter and getter acquire the GC lock. */
  1224. /* Both the setter and getter are defined only if the library has been */
  1225. /* compiled without SMALL_CONFIG. */
  1226. typedef void (GC_CALLBACK * GC_abort_func)(const char * /* msg */);
  1227. GC_API void GC_CALL GC_set_abort_func(GC_abort_func) GC_ATTR_NONNULL(1);
  1228. GC_API GC_abort_func GC_CALL GC_get_abort_func(void);
  1229. /* The following is intended to be used by a higher level */
  1230. /* (e.g. Java-like) finalization facility. It is expected */
  1231. /* that finalization code will arrange for hidden pointers to */
  1232. /* disappear. Otherwise objects can be accessed after they */
  1233. /* have been collected. */
  1234. /* Note that putting pointers in atomic objects or in */
  1235. /* non-pointer slots of "typed" objects is equivalent to */
  1236. /* disguising them in this way, and may have other advantages. */
  1237. typedef GC_word GC_hidden_pointer;
  1238. #define GC_HIDE_POINTER(p) (~(GC_hidden_pointer)(p))
  1239. /* Converting a hidden pointer to a real pointer requires verifying */
  1240. /* that the object still exists. This involves acquiring the */
  1241. /* allocator lock to avoid a race with the collector. */
  1242. #define GC_REVEAL_POINTER(p) ((void *)GC_HIDE_POINTER(p))
  1243. #if defined(I_HIDE_POINTERS) || defined(GC_I_HIDE_POINTERS)
  1244. /* This exists only for compatibility (the GC-prefixed symbols are */
  1245. /* preferred for new code). */
  1246. # define HIDE_POINTER(p) GC_HIDE_POINTER(p)
  1247. # define REVEAL_POINTER(p) GC_REVEAL_POINTER(p)
  1248. #endif
  1249. typedef void * (GC_CALLBACK * GC_fn_type)(void * /* client_data */);
  1250. GC_API void * GC_CALL GC_call_with_alloc_lock(GC_fn_type /* fn */,
  1251. void * /* client_data */) GC_ATTR_NONNULL(1);
  1252. /* These routines are intended to explicitly notify the collector */
  1253. /* of new threads. Often this is unnecessary because thread creation */
  1254. /* is implicitly intercepted by the collector, using header-file */
  1255. /* defines, or linker-based interception. In the long run the intent */
  1256. /* is to always make redundant registration safe. In the short run, */
  1257. /* this is being implemented a platform at a time. */
  1258. /* The interface is complicated by the fact that we probably will not */
  1259. /* ever be able to automatically determine the stack base for thread */
  1260. /* stacks on all platforms. */
  1261. /* Structure representing the base of a thread stack. On most */
  1262. /* platforms this contains just a single address. */
  1263. struct GC_stack_base {
  1264. void * mem_base; /* Base of memory stack. */
  1265. # if defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
  1266. void * reg_base; /* Base of separate register stack. */
  1267. # endif
  1268. };
  1269. typedef void * (GC_CALLBACK * GC_stack_base_func)(
  1270. struct GC_stack_base * /* sb */, void * /* arg */);
  1271. /* Call a function with a stack base structure corresponding to */
  1272. /* somewhere in the GC_call_with_stack_base frame. This often can */
  1273. /* be used to provide a sufficiently accurate stack base. And we */
  1274. /* implement it everywhere. */
  1275. GC_API void * GC_CALL GC_call_with_stack_base(GC_stack_base_func /* fn */,
  1276. void * /* arg */) GC_ATTR_NONNULL(1);
  1277. #define GC_SUCCESS 0
  1278. #define GC_DUPLICATE 1 /* Was already registered. */
  1279. #define GC_NO_THREADS 2 /* No thread support in GC. */
  1280. /* GC_NO_THREADS is not returned by any GC function anymore. */
  1281. #define GC_UNIMPLEMENTED 3 /* Not yet implemented on this platform. */
  1282. #define GC_NOT_FOUND 4 /* Requested link not found (returned */
  1283. /* by GC_move_disappearing_link). */
  1284. #if defined(GC_DARWIN_THREADS) || defined(GC_WIN32_THREADS)
  1285. /* Use implicit thread registration and processing (via Win32 DllMain */
  1286. /* or Darwin task_threads). Deprecated. Must be called before */
  1287. /* GC_INIT() and other GC routines. Should be avoided if */
  1288. /* GC_pthread_create, GC_beginthreadex (or GC_CreateThread) could be */
  1289. /* called instead. Disables parallelized GC on Win32. */
  1290. GC_API void GC_CALL GC_use_threads_discovery(void);
  1291. #endif
  1292. #ifdef GC_THREADS
  1293. /* Suggest the GC to use the specific signal to suspend threads. */
  1294. /* Has no effect after GC_init and on non-POSIX systems. */
  1295. GC_API void GC_CALL GC_set_suspend_signal(int);
  1296. /* Suggest the GC to use the specific signal to resume threads. */
  1297. /* Has no effect after GC_init and on non-POSIX systems. */
  1298. GC_API void GC_CALL GC_set_thr_restart_signal(int);
  1299. /* Return the signal number (constant after initialization) used by */
  1300. /* the GC to suspend threads on POSIX systems. Return -1 otherwise. */
  1301. GC_API int GC_CALL GC_get_suspend_signal(void);
  1302. /* Return the signal number (constant after initialization) used by */
  1303. /* the garbage collector to restart (resume) threads on POSIX */
  1304. /* systems. Return -1 otherwise. */
  1305. GC_API int GC_CALL GC_get_thr_restart_signal(void);
  1306. /* Restart marker threads after POSIX fork in child. Meaningless in */
  1307. /* other situations. Should not be called if fork followed by exec. */
  1308. GC_API void GC_CALL GC_start_mark_threads(void);
  1309. /* Explicitly enable GC_register_my_thread() invocation. */
  1310. /* Done implicitly if a GC thread-creation function is called (or */
  1311. /* implicit thread registration is activated, or the collector is */
  1312. /* compiled with GC_ALWAYS_MULTITHREADED defined). Otherwise, it */
  1313. /* must be called from the main (or any previously registered) thread */
  1314. /* between the collector initialization and the first explicit */
  1315. /* registering of a thread (it should be called as late as possible). */
  1316. GC_API void GC_CALL GC_allow_register_threads(void);
  1317. /* Register the current thread, with the indicated stack base, as */
  1318. /* a new thread whose stack(s) should be traced by the GC. If it */
  1319. /* is not implicitly called by the GC, this must be called before a */
  1320. /* thread can allocate garbage collected memory, or assign pointers */
  1321. /* to the garbage collected heap. Once registered, a thread will be */
  1322. /* stopped during garbage collections. */
  1323. /* This call must be previously enabled (see above). */
  1324. /* This should never be called from the main thread, where it is */
  1325. /* always done implicitly. This is normally done implicitly if GC_ */
  1326. /* functions are called to create the thread, e.g. by including gc.h */
  1327. /* (which redefines some system functions) before calling the system */
  1328. /* thread creation function. Nonetheless, thread cleanup routines */
  1329. /* (e.g., pthread key destructor) typically require manual thread */
  1330. /* registering (and unregistering) if pointers to GC-allocated */
  1331. /* objects are manipulated inside. */
  1332. /* It is also always done implicitly on some platforms if */
  1333. /* GC_use_threads_discovery() is called at start-up. Except for the */
  1334. /* latter case, the explicit call is normally required for threads */
  1335. /* created by third-party libraries. */
  1336. /* A manually registered thread requires manual unregistering. */
  1337. /* Returns GC_SUCCESS on success, GC_DUPLICATE if already registered. */
  1338. GC_API int GC_CALL GC_register_my_thread(const struct GC_stack_base *)
  1339. GC_ATTR_NONNULL(1);
  1340. /* Return non-zero (TRUE) if and only if the calling thread is */
  1341. /* registered with the garbage collector. */
  1342. GC_API int GC_CALL GC_thread_is_registered(void);
  1343. /* Notify the collector about the stack and the alt-stack of the */
  1344. /* current thread. stack_start/size is used to determine the stack */
  1345. /* boundaries when a thread is suspended while it is on an alt-stack. */
  1346. GC_API void GC_CALL GC_register_altstack(void * /* stack_start */,
  1347. GC_word /* stack_size */,
  1348. void * /* altstack_base */,
  1349. GC_word /* altstack_size */);
  1350. /* Unregister the current thread. Only an explicitly registered */
  1351. /* thread (i.e. for which GC_register_my_thread() returns GC_SUCCESS) */
  1352. /* is allowed (and required) to call this function. (As a special */
  1353. /* exception, it is also allowed to once unregister the main thread.) */
  1354. /* The thread may no longer allocate garbage collected memory or */
  1355. /* manipulate pointers to the garbage collected heap after making */
  1356. /* this call. Specifically, if it wants to return or otherwise */
  1357. /* communicate a pointer to the garbage-collected heap to another */
  1358. /* thread, it must do this before calling GC_unregister_my_thread, */
  1359. /* most probably by saving it in a global data structure. Must not */
  1360. /* be called inside a GC callback function (except for */
  1361. /* GC_call_with_stack_base() one). */
  1362. GC_API int GC_CALL GC_unregister_my_thread(void);
  1363. #endif /* GC_THREADS */
  1364. /* Wrapper for functions that are likely to block (or, at least, do not */
  1365. /* allocate garbage collected memory and/or manipulate pointers to the */
  1366. /* garbage collected heap) for an appreciable length of time. While fn */
  1367. /* is running, the collector is said to be in the "inactive" state for */
  1368. /* the current thread (this means that the thread is not suspended and */
  1369. /* the thread's stack frames "belonging" to the functions in the */
  1370. /* "inactive" state are not scanned during garbage collections). It is */
  1371. /* allowed for fn to call GC_call_with_gc_active() (even recursively), */
  1372. /* thus temporarily toggling the collector's state back to "active". */
  1373. GC_API void * GC_CALL GC_do_blocking(GC_fn_type /* fn */,
  1374. void * /* client_data */) GC_ATTR_NONNULL(1);
  1375. /* Call a function switching to the "active" state of the collector for */
  1376. /* the current thread (i.e. the user function is allowed to call any */
  1377. /* GC function and/or manipulate pointers to the garbage collected */
  1378. /* heap). GC_call_with_gc_active() has the functionality opposite to */
  1379. /* GC_do_blocking() one. It is assumed that the collector is already */
  1380. /* initialized and the current thread is registered. fn may toggle */
  1381. /* the collector thread's state temporarily to "inactive" one by using */
  1382. /* GC_do_blocking. GC_call_with_gc_active() often can be used to */
  1383. /* provide a sufficiently accurate stack base. */
  1384. GC_API void * GC_CALL GC_call_with_gc_active(GC_fn_type /* fn */,
  1385. void * /* client_data */) GC_ATTR_NONNULL(1);
  1386. /* Attempt to fill in the GC_stack_base structure with the stack base */
  1387. /* for this thread. This appears to be required to implement anything */
  1388. /* like the JNI AttachCurrentThread in an environment in which new */
  1389. /* threads are not automatically registered with the collector. */
  1390. /* It is also unfortunately hard to implement well on many platforms. */
  1391. /* Returns GC_SUCCESS or GC_UNIMPLEMENTED. This function acquires the */
  1392. /* GC lock on some platforms. */
  1393. GC_API int GC_CALL GC_get_stack_base(struct GC_stack_base *)
  1394. GC_ATTR_NONNULL(1);
  1395. /* The following routines are primarily intended for use with a */
  1396. /* preprocessor which inserts calls to check C pointer arithmetic. */
  1397. /* They indicate failure by invoking the corresponding _print_proc. */
  1398. /* Check that p and q point to the same object. */
  1399. /* Fail conspicuously if they don't. */
  1400. /* Returns the first argument. */
  1401. /* Succeeds if neither p nor q points to the heap. */
  1402. /* May succeed if both p and q point to between heap objects. */
  1403. GC_API void * GC_CALL GC_same_obj(void * /* p */, void * /* q */);
  1404. /* Checked pointer pre- and post- increment operations. Note that */
  1405. /* the second argument is in units of bytes, not multiples of the */
  1406. /* object size. This should either be invoked from a macro, or the */
  1407. /* call should be automatically generated. */
  1408. GC_API void * GC_CALL GC_pre_incr(void **, ptrdiff_t /* how_much */)
  1409. GC_ATTR_NONNULL(1);
  1410. GC_API void * GC_CALL GC_post_incr(void **, ptrdiff_t /* how_much */)
  1411. GC_ATTR_NONNULL(1);
  1412. /* Check that p is visible */
  1413. /* to the collector as a possibly pointer containing location. */
  1414. /* If it isn't fail conspicuously. */
  1415. /* Returns the argument in all cases. May erroneously succeed */
  1416. /* in hard cases. (This is intended for debugging use with */
  1417. /* untyped allocations. The idea is that it should be possible, though */
  1418. /* slow, to add such a call to all indirect pointer stores.) */
  1419. /* Currently useless for multi-threaded worlds. */
  1420. GC_API void * GC_CALL GC_is_visible(void * /* p */);
  1421. /* Check that if p is a pointer to a heap page, then it points to */
  1422. /* a valid displacement within a heap object. */
  1423. /* Fail conspicuously if this property does not hold. */
  1424. /* Uninteresting with GC_all_interior_pointers. */
  1425. /* Always returns its argument. */
  1426. GC_API void * GC_CALL GC_is_valid_displacement(void * /* p */);
  1427. /* Explicitly dump the GC state. This is most often called from the */
  1428. /* debugger, or by setting the GC_DUMP_REGULARLY environment variable, */
  1429. /* but it may be useful to call it from client code during debugging. */
  1430. /* The current collection number is printed in the header of the dump. */
  1431. /* Acquires the GC lock to avoid data races. */
  1432. /* Defined only if the library has been compiled without NO_DEBUGGING. */
  1433. GC_API void GC_CALL GC_dump(void);
  1434. /* The same as GC_dump but allows to specify the name of dump and does */
  1435. /* not acquire the lock. If name is non-NULL, it is printed to help */
  1436. /* identifying individual dumps. Otherwise the current collection */
  1437. /* number is used as the name. */
  1438. /* Defined only if the library has been compiled without NO_DEBUGGING. */
  1439. GC_API void GC_CALL GC_dump_named(const char * /* name */);
  1440. /* Dump information about each block of every GC memory section. */
  1441. /* Defined only if the library has been compiled without NO_DEBUGGING. */
  1442. GC_API void GC_CALL GC_dump_regions(void);
  1443. /* Dump information about every registered disappearing link and */
  1444. /* finalizable object. */
  1445. /* Defined only if the library has been compiled without NO_DEBUGGING. */
  1446. GC_API void GC_CALL GC_dump_finalization(void);
  1447. /* Safer, but slow, pointer addition. Probably useful mainly with */
  1448. /* a preprocessor. Useful only for heap pointers. */
  1449. /* Only the macros without trailing digits are meant to be used */
  1450. /* by clients. These are designed to model the available C pointer */
  1451. /* arithmetic expressions. */
  1452. /* Even then, these are probably more useful as */
  1453. /* documentation than as part of the API. */
  1454. /* Note that GC_PTR_ADD evaluates the first argument more than once. */
  1455. #if defined(GC_DEBUG) && defined(__GNUC__)
  1456. # define GC_PTR_ADD3(x, n, type_of_result) \
  1457. ((type_of_result)GC_same_obj((x)+(n), (x)))
  1458. # define GC_PRE_INCR3(x, n, type_of_result) \
  1459. ((type_of_result)GC_pre_incr((void **)(&(x)), (n)*sizeof(*x)))
  1460. # define GC_POST_INCR3(x, n, type_of_result) \
  1461. ((type_of_result)GC_post_incr((void **)(&(x)), (n)*sizeof(*x)))
  1462. # define GC_PTR_ADD(x, n) GC_PTR_ADD3(x, n, typeof(x))
  1463. # define GC_PRE_INCR(x, n) GC_PRE_INCR3(x, n, typeof(x))
  1464. # define GC_POST_INCR(x) GC_POST_INCR3(x, 1, typeof(x))
  1465. # define GC_POST_DECR(x) GC_POST_INCR3(x, -1, typeof(x))
  1466. #else /* !GC_DEBUG || !__GNUC__ */
  1467. /* We can't do this right without typeof, which ANSI decided was not */
  1468. /* sufficiently useful. Without it we resort to the non-debug version. */
  1469. /* FIXME: This should eventually support C++0x decltype. */
  1470. # define GC_PTR_ADD(x, n) ((x)+(n))
  1471. # define GC_PRE_INCR(x, n) ((x) += (n))
  1472. # define GC_POST_INCR(x) ((x)++)
  1473. # define GC_POST_DECR(x) ((x)--)
  1474. #endif /* !GC_DEBUG || !__GNUC__ */
  1475. /* Safer assignment of a pointer to a non-stack location. */
  1476. #ifdef GC_DEBUG
  1477. # define GC_PTR_STORE(p, q) \
  1478. (*(void **)GC_is_visible(p) = GC_is_valid_displacement(q))
  1479. #else
  1480. # define GC_PTR_STORE(p, q) (*(p) = (q))
  1481. #endif
  1482. /* Functions called to report pointer checking errors */
  1483. GC_API void (GC_CALLBACK * GC_same_obj_print_proc)(void * /* p */,
  1484. void * /* q */);
  1485. GC_API void (GC_CALLBACK * GC_is_valid_displacement_print_proc)(void *);
  1486. GC_API void (GC_CALLBACK * GC_is_visible_print_proc)(void *);
  1487. #ifdef GC_PTHREADS
  1488. /* For pthread support, we generally need to intercept a number of */
  1489. /* thread library calls. We do that here by macro defining them. */
  1490. # ifdef __cplusplus
  1491. } /* extern "C" */
  1492. # endif
  1493. # include "gc_pthread_redirects.h"
  1494. # ifdef __cplusplus
  1495. extern "C" {
  1496. # endif
  1497. #endif
  1498. /* This returns a list of objects, linked through their first word. */
  1499. /* Its use can greatly reduce lock contention problems, since the */
  1500. /* allocation lock can be acquired and released many fewer times. */
  1501. GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_many(size_t /* lb */);
  1502. #define GC_NEXT(p) (*(void * *)(p)) /* Retrieve the next element */
  1503. /* in returned list. */
  1504. /* A filter function to control the scanning of dynamic libraries. */
  1505. /* If implemented, called by GC before registering a dynamic library */
  1506. /* (discovered by GC) section as a static data root (called only as */
  1507. /* a last reason not to register). The filename of the library, the */
  1508. /* address and the length of the memory region (section) are passed. */
  1509. /* This routine should return nonzero if that region should be scanned. */
  1510. /* Always called with the allocation lock held. Depending on the */
  1511. /* platform, might be called with the "world" stopped. */
  1512. typedef int (GC_CALLBACK * GC_has_static_roots_func)(
  1513. const char * /* dlpi_name */,
  1514. void * /* section_start */,
  1515. size_t /* section_size */);
  1516. /* Register a new callback (a user-supplied filter) to control the */
  1517. /* scanning of dynamic libraries. Replaces any previously registered */
  1518. /* callback. May be 0 (means no filtering). May be unused on some */
  1519. /* platforms (if the filtering is unimplemented or inappropriate). */
  1520. GC_API void GC_CALL GC_register_has_static_roots_callback(
  1521. GC_has_static_roots_func);
  1522. #if !defined(CPPCHECK) && !defined(GC_WINDOWS_H_INCLUDED) && defined(WINAPI)
  1523. /* windows.h is included before gc.h */
  1524. # define GC_WINDOWS_H_INCLUDED
  1525. #endif
  1526. #if defined(GC_WIN32_THREADS) \
  1527. && (!defined(GC_PTHREADS) || defined(GC_BUILD) \
  1528. || defined(GC_WINDOWS_H_INCLUDED))
  1529. /* Note: for Cygwin and pthreads-win32, this is skipped */
  1530. /* unless windows.h is included before gc.h. */
  1531. # if !defined(GC_NO_THREAD_DECLS) || defined(GC_BUILD)
  1532. # ifdef __cplusplus
  1533. } /* Including windows.h in an extern "C" context no longer works. */
  1534. # endif
  1535. # if !defined(_WIN32_WCE) && !defined(__CEGCC__)
  1536. # include <process.h> /* For _beginthreadex, _endthreadex */
  1537. # endif
  1538. # if defined(GC_BUILD) || !defined(GC_DONT_INCLUDE_WINDOWS_H)
  1539. # ifndef WIN32_LEAN_AND_MEAN
  1540. # define WIN32_LEAN_AND_MEAN
  1541. # endif
  1542. # include <windows.h>
  1543. # define GC_WINDOWS_H_INCLUDED
  1544. # endif
  1545. # ifdef __cplusplus
  1546. extern "C" {
  1547. # endif
  1548. # ifdef GC_UNDERSCORE_STDCALL
  1549. /* Explicitly prefix exported/imported WINAPI (__stdcall) symbols */
  1550. /* with '_' (underscore). Might be useful if MinGW/x86 is used. */
  1551. # define GC_CreateThread _GC_CreateThread
  1552. # define GC_ExitThread _GC_ExitThread
  1553. # endif
  1554. # ifndef DECLSPEC_NORETURN
  1555. /* Typically defined in winnt.h. */
  1556. # ifdef GC_WINDOWS_H_INCLUDED
  1557. # define DECLSPEC_NORETURN /* empty */
  1558. # else
  1559. # define DECLSPEC_NORETURN __declspec(noreturn)
  1560. # endif
  1561. # endif
  1562. # if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED) \
  1563. && !defined(UINTPTR_MAX)
  1564. typedef GC_word GC_uintptr_t;
  1565. # else
  1566. typedef uintptr_t GC_uintptr_t;
  1567. # endif
  1568. # ifdef _WIN64
  1569. # define GC_WIN32_SIZE_T GC_uintptr_t
  1570. # elif defined(GC_WINDOWS_H_INCLUDED)
  1571. # define GC_WIN32_SIZE_T DWORD
  1572. # else
  1573. # define GC_WIN32_SIZE_T unsigned long
  1574. # endif
  1575. # ifdef GC_INSIDE_DLL
  1576. /* Export GC DllMain to be invoked from client DllMain. */
  1577. # ifdef GC_UNDERSCORE_STDCALL
  1578. # define GC_DllMain _GC_DllMain
  1579. # endif
  1580. # ifdef GC_WINDOWS_H_INCLUDED
  1581. GC_API BOOL WINAPI GC_DllMain(HINSTANCE /* inst */,
  1582. ULONG /* reason */,
  1583. LPVOID /* reserved */);
  1584. # else
  1585. GC_API int __stdcall GC_DllMain(void *, unsigned long, void *);
  1586. # endif
  1587. # endif /* GC_INSIDE_DLL */
  1588. /* All threads must be created using GC_CreateThread or */
  1589. /* GC_beginthreadex, or must explicitly call GC_register_my_thread */
  1590. /* (and call GC_unregister_my_thread before thread termination), so */
  1591. /* that they will be recorded in the thread table. For backward */
  1592. /* compatibility, it is possible to build the GC with GC_DLL */
  1593. /* defined, and to call GC_use_threads_discovery. This implicitly */
  1594. /* registers all created threads, but appears to be less robust. */
  1595. /* Currently the collector expects all threads to fall through and */
  1596. /* terminate normally, or call GC_endthreadex() or GC_ExitThread, */
  1597. /* so that the thread is properly unregistered. */
  1598. # ifdef GC_WINDOWS_H_INCLUDED
  1599. GC_API HANDLE WINAPI GC_CreateThread(
  1600. LPSECURITY_ATTRIBUTES /* lpThreadAttributes */,
  1601. GC_WIN32_SIZE_T /* dwStackSize */,
  1602. LPTHREAD_START_ROUTINE /* lpStartAddress */,
  1603. LPVOID /* lpParameter */, DWORD /* dwCreationFlags */,
  1604. LPDWORD /* lpThreadId */);
  1605. GC_API DECLSPEC_NORETURN void WINAPI GC_ExitThread(
  1606. DWORD /* dwExitCode */);
  1607. # else
  1608. struct _SECURITY_ATTRIBUTES;
  1609. GC_API void *__stdcall GC_CreateThread(struct _SECURITY_ATTRIBUTES *,
  1610. GC_WIN32_SIZE_T,
  1611. unsigned long (__stdcall *)(void *),
  1612. void *, unsigned long, unsigned long *);
  1613. GC_API DECLSPEC_NORETURN void __stdcall GC_ExitThread(unsigned long);
  1614. # endif
  1615. # if !defined(_WIN32_WCE) && !defined(__CEGCC__)
  1616. GC_API GC_uintptr_t GC_CALL GC_beginthreadex(
  1617. void * /* security */, unsigned /* stack_size */,
  1618. unsigned (__stdcall *)(void *),
  1619. void * /* arglist */, unsigned /* initflag */,
  1620. unsigned * /* thrdaddr */);
  1621. /* Note: _endthreadex() is not currently marked as no-return in */
  1622. /* VC++ and MinGW headers, so we don't mark it neither. */
  1623. GC_API void GC_CALL GC_endthreadex(unsigned /* retval */);
  1624. # endif /* !_WIN32_WCE */
  1625. # endif /* !GC_NO_THREAD_DECLS */
  1626. # ifdef GC_WINMAIN_REDIRECT
  1627. /* win32_threads.c implements the real WinMain(), which will start */
  1628. /* a new thread to call GC_WinMain() after initializing the garbage */
  1629. /* collector. */
  1630. # define WinMain GC_WinMain
  1631. # endif
  1632. /* For compatibility only. */
  1633. # define GC_use_DllMain GC_use_threads_discovery
  1634. # ifndef GC_NO_THREAD_REDIRECTS
  1635. # define CreateThread GC_CreateThread
  1636. # define ExitThread GC_ExitThread
  1637. # undef _beginthreadex
  1638. # define _beginthreadex GC_beginthreadex
  1639. # undef _endthreadex
  1640. # define _endthreadex GC_endthreadex
  1641. /* #define _beginthread { > "Please use _beginthreadex instead of _beginthread" < } */
  1642. # endif /* !GC_NO_THREAD_REDIRECTS */
  1643. #endif /* GC_WIN32_THREADS */
  1644. /* Public setter and getter for switching "unmap as much as possible" */
  1645. /* mode on(1) and off(0). Has no effect unless unmapping is turned on. */
  1646. /* Has no effect on implicitly-initiated garbage collections. Initial */
  1647. /* value is controlled by GC_FORCE_UNMAP_ON_GCOLLECT. The setter and */
  1648. /* getter are unsynchronized. */
  1649. GC_API void GC_CALL GC_set_force_unmap_on_gcollect(int);
  1650. GC_API int GC_CALL GC_get_force_unmap_on_gcollect(void);
  1651. /* Fully portable code should call GC_INIT() from the main program */
  1652. /* before making any other GC_ calls. On most platforms this is a */
  1653. /* no-op and the collector self-initializes. But a number of */
  1654. /* platforms make that too hard. */
  1655. /* A GC_INIT call is required if the collector is built with */
  1656. /* THREAD_LOCAL_ALLOC defined and the initial allocation call is not */
  1657. /* to GC_malloc() or GC_malloc_atomic(). */
  1658. #if defined(__CYGWIN32__) || defined(__CYGWIN__)
  1659. /* Similarly gnu-win32 DLLs need explicit initialization from the */
  1660. /* main program, as does AIX. */
  1661. # ifdef __x86_64__
  1662. /* Cygwin/x64 does not add leading underscore to symbols anymore. */
  1663. extern int __data_start__[], __data_end__[];
  1664. extern int __bss_start__[], __bss_end__[];
  1665. # define GC_DATASTART ((GC_word)__data_start__ < (GC_word)__bss_start__ \
  1666. ? (void *)__data_start__ : (void *)__bss_start__)
  1667. # define GC_DATAEND ((GC_word)__data_end__ > (GC_word)__bss_end__ \
  1668. ? (void *)__data_end__ : (void *)__bss_end__)
  1669. # else
  1670. extern int _data_start__[], _data_end__[], _bss_start__[], _bss_end__[];
  1671. # define GC_DATASTART ((GC_word)_data_start__ < (GC_word)_bss_start__ \
  1672. ? (void *)_data_start__ : (void *)_bss_start__)
  1673. # define GC_DATAEND ((GC_word)_data_end__ > (GC_word)_bss_end__ \
  1674. ? (void *)_data_end__ : (void *)_bss_end__)
  1675. # endif /* !__x86_64__ */
  1676. # define GC_INIT_CONF_ROOTS GC_add_roots(GC_DATASTART, GC_DATAEND); \
  1677. GC_gcollect() /* For blacklisting. */
  1678. /* Required at least if GC is in a DLL. And doesn't hurt. */
  1679. #elif defined(_AIX)
  1680. extern int _data[], _end[];
  1681. # define GC_DATASTART ((void *)((ulong)_data))
  1682. # define GC_DATAEND ((void *)((ulong)_end))
  1683. # define GC_INIT_CONF_ROOTS GC_add_roots(GC_DATASTART, GC_DATAEND)
  1684. #elif (defined(HOST_ANDROID) || defined(__ANDROID__)) \
  1685. && !defined(GC_NOT_DLL) && defined(IGNORE_DYNAMIC_LOADING)
  1686. /* It causes the entire binary section of memory be pushed as a root. */
  1687. /* This might be a bad idea though because on some Android devices */
  1688. /* some of the binary data might become unmapped thus causing SIGSEGV */
  1689. /* with code SEGV_MAPERR. */
  1690. # pragma weak _etext
  1691. # pragma weak __data_start
  1692. # pragma weak __dso_handle
  1693. extern int _etext[], __data_start[], __dso_handle[];
  1694. # pragma weak __end__
  1695. extern int __end__[], _end[];
  1696. /* Explicitly register caller static data roots. Workaround for */
  1697. /* __data_start: NDK "gold" linker might miss it or place it */
  1698. /* incorrectly, __dso_handle is an alternative data start reference. */
  1699. /* Workaround for _end: NDK Clang 3.5+ does not place it at correct */
  1700. /* offset (as of NDK r10e) but "bfd" linker provides __end__ symbol */
  1701. /* that could be used instead. */
  1702. # define GC_INIT_CONF_ROOTS \
  1703. (void)((GC_word)__data_start < (GC_word)_etext \
  1704. && (GC_word)_etext < (GC_word)__dso_handle \
  1705. ? (__end__ != 0 \
  1706. ? (GC_add_roots(__dso_handle, __end__), 0) \
  1707. : (GC_word)__dso_handle < (GC_word)_end \
  1708. ? (GC_add_roots(__dso_handle, _end), 0) : 0) \
  1709. : __data_start != 0 ? (__end__ != 0 \
  1710. ? (GC_add_roots(__data_start, __end__), 0) \
  1711. : (GC_word)__data_start < (GC_word)_end \
  1712. ? (GC_add_roots(__data_start, _end), 0) : 0) : 0)
  1713. #else
  1714. # define GC_INIT_CONF_ROOTS /* empty */
  1715. #endif
  1716. #ifdef GC_DONT_EXPAND
  1717. /* Set GC_dont_expand to TRUE at start-up */
  1718. # define GC_INIT_CONF_DONT_EXPAND GC_set_dont_expand(1)
  1719. #else
  1720. # define GC_INIT_CONF_DONT_EXPAND /* empty */
  1721. #endif
  1722. #ifdef GC_FORCE_UNMAP_ON_GCOLLECT
  1723. /* Turn on "unmap as much as possible on explicit GC" mode at start-up */
  1724. # define GC_INIT_CONF_FORCE_UNMAP_ON_GCOLLECT \
  1725. GC_set_force_unmap_on_gcollect(1)
  1726. #else
  1727. # define GC_INIT_CONF_FORCE_UNMAP_ON_GCOLLECT /* empty */
  1728. #endif
  1729. #ifdef GC_DONT_GC
  1730. /* This is for debugging only (useful if environment variables are */
  1731. /* unsupported); cannot call GC_disable as goes before GC_init. */
  1732. # define GC_INIT_CONF_MAX_RETRIES (void)(GC_dont_gc = 1)
  1733. #elif defined(GC_MAX_RETRIES) && !defined(CPPCHECK)
  1734. /* Set GC_max_retries to the desired value at start-up */
  1735. # define GC_INIT_CONF_MAX_RETRIES GC_set_max_retries(GC_MAX_RETRIES)
  1736. #else
  1737. # define GC_INIT_CONF_MAX_RETRIES /* empty */
  1738. #endif
  1739. #if defined(GC_FREE_SPACE_DIVISOR) && !defined(CPPCHECK)
  1740. /* Set GC_free_space_divisor to the desired value at start-up */
  1741. # define GC_INIT_CONF_FREE_SPACE_DIVISOR \
  1742. GC_set_free_space_divisor(GC_FREE_SPACE_DIVISOR)
  1743. #else
  1744. # define GC_INIT_CONF_FREE_SPACE_DIVISOR /* empty */
  1745. #endif
  1746. #if defined(GC_FULL_FREQ) && !defined(CPPCHECK)
  1747. /* Set GC_full_freq to the desired value at start-up */
  1748. # define GC_INIT_CONF_FULL_FREQ GC_set_full_freq(GC_FULL_FREQ)
  1749. #else
  1750. # define GC_INIT_CONF_FULL_FREQ /* empty */
  1751. #endif
  1752. #if defined(GC_TIME_LIMIT) && !defined(CPPCHECK)
  1753. /* Set GC_time_limit to the desired value at start-up */
  1754. # define GC_INIT_CONF_TIME_LIMIT GC_set_time_limit(GC_TIME_LIMIT)
  1755. #else
  1756. # define GC_INIT_CONF_TIME_LIMIT /* empty */
  1757. #endif
  1758. #if defined(GC_SIG_SUSPEND) && defined(GC_THREADS) && !defined(CPPCHECK)
  1759. # define GC_INIT_CONF_SUSPEND_SIGNAL GC_set_suspend_signal(GC_SIG_SUSPEND)
  1760. #else
  1761. # define GC_INIT_CONF_SUSPEND_SIGNAL /* empty */
  1762. #endif
  1763. #if defined(GC_SIG_THR_RESTART) && defined(GC_THREADS) && !defined(CPPCHECK)
  1764. # define GC_INIT_CONF_THR_RESTART_SIGNAL \
  1765. GC_set_thr_restart_signal(GC_SIG_THR_RESTART)
  1766. #else
  1767. # define GC_INIT_CONF_THR_RESTART_SIGNAL /* empty */
  1768. #endif
  1769. #if defined(GC_MAXIMUM_HEAP_SIZE) && !defined(CPPCHECK)
  1770. /* Limit the heap size to the desired value (useful for debugging). */
  1771. /* The limit could be overridden either at the program start-up by */
  1772. /* the similar environment variable or anytime later by the */
  1773. /* corresponding API function call. */
  1774. # define GC_INIT_CONF_MAXIMUM_HEAP_SIZE \
  1775. GC_set_max_heap_size(GC_MAXIMUM_HEAP_SIZE)
  1776. #else
  1777. # define GC_INIT_CONF_MAXIMUM_HEAP_SIZE /* empty */
  1778. #endif
  1779. #ifdef GC_IGNORE_WARN
  1780. /* Turn off all warnings at start-up (after GC initialization) */
  1781. # define GC_INIT_CONF_IGNORE_WARN GC_set_warn_proc(GC_ignore_warn_proc)
  1782. #else
  1783. # define GC_INIT_CONF_IGNORE_WARN /* empty */
  1784. #endif
  1785. #if defined(GC_INITIAL_HEAP_SIZE) && !defined(CPPCHECK)
  1786. /* Set heap size to the desired value at start-up */
  1787. # define GC_INIT_CONF_INITIAL_HEAP_SIZE \
  1788. { size_t heap_size = GC_get_heap_size(); \
  1789. if (heap_size < (GC_INITIAL_HEAP_SIZE)) \
  1790. (void)GC_expand_hp((GC_INITIAL_HEAP_SIZE) - heap_size); }
  1791. #else
  1792. # define GC_INIT_CONF_INITIAL_HEAP_SIZE /* empty */
  1793. #endif
  1794. /* Portable clients should call this at the program start-up. More */
  1795. /* over, some platforms require this call to be done strictly from the */
  1796. /* primordial thread. */
  1797. #define GC_INIT() { GC_INIT_CONF_DONT_EXPAND; /* pre-init */ \
  1798. GC_INIT_CONF_FORCE_UNMAP_ON_GCOLLECT; \
  1799. GC_INIT_CONF_MAX_RETRIES; \
  1800. GC_INIT_CONF_FREE_SPACE_DIVISOR; \
  1801. GC_INIT_CONF_FULL_FREQ; \
  1802. GC_INIT_CONF_TIME_LIMIT; \
  1803. GC_INIT_CONF_SUSPEND_SIGNAL; \
  1804. GC_INIT_CONF_THR_RESTART_SIGNAL; \
  1805. GC_INIT_CONF_MAXIMUM_HEAP_SIZE; \
  1806. GC_init(); /* real GC initialization */ \
  1807. GC_INIT_CONF_ROOTS; /* post-init */ \
  1808. GC_INIT_CONF_IGNORE_WARN; \
  1809. GC_INIT_CONF_INITIAL_HEAP_SIZE; }
  1810. /* win32S may not free all resources on process exit. */
  1811. /* This explicitly deallocates the heap. */
  1812. GC_API void GC_CALL GC_win32_free_heap(void);
  1813. #if defined(__SYMBIAN32__)
  1814. void GC_init_global_static_roots(void);
  1815. #endif
  1816. #if defined(_AMIGA) && !defined(GC_AMIGA_MAKINGLIB)
  1817. /* Allocation really goes through GC_amiga_allocwrapper_do. */
  1818. void *GC_amiga_realloc(void *, size_t);
  1819. # define GC_realloc(a,b) GC_amiga_realloc(a,b)
  1820. void GC_amiga_set_toany(void (*)(void));
  1821. extern int GC_amiga_free_space_divisor_inc;
  1822. extern void *(*GC_amiga_allocwrapper_do)(size_t, void *(GC_CALL *)(size_t));
  1823. # define GC_malloc(a) \
  1824. (*GC_amiga_allocwrapper_do)(a,GC_malloc)
  1825. # define GC_malloc_atomic(a) \
  1826. (*GC_amiga_allocwrapper_do)(a,GC_malloc_atomic)
  1827. # define GC_malloc_uncollectable(a) \
  1828. (*GC_amiga_allocwrapper_do)(a,GC_malloc_uncollectable)
  1829. # define GC_malloc_atomic_uncollectable(a) \
  1830. (*GC_amiga_allocwrapper_do)(a,GC_malloc_atomic_uncollectable)
  1831. # define GC_malloc_ignore_off_page(a) \
  1832. (*GC_amiga_allocwrapper_do)(a,GC_malloc_ignore_off_page)
  1833. # define GC_malloc_atomic_ignore_off_page(a) \
  1834. (*GC_amiga_allocwrapper_do)(a,GC_malloc_atomic_ignore_off_page)
  1835. #endif /* _AMIGA && !GC_AMIGA_MAKINGLIB */
  1836. /* Unity specific APIs */
  1837. GC_API void GC_CALL GC_stop_world_external(void);
  1838. GC_API void GC_CALL GC_start_world_external(void);
  1839. GC_API void GC_CALL GC_disable_incremental(void);
  1840. GC_API void GC_CALL GC_start_incremental_collection (void);
  1841. GC_API void GC_CALL GC_set_disable_automatic_collection(int);
  1842. /* APIs for getting access to raw GC heap */
  1843. /* These are NOT thread safe, so should be called with GC lock held */
  1844. typedef void (*GC_heap_section_proc)(void* user_data, GC_PTR start, GC_PTR end);
  1845. GC_API void GC_foreach_heap_section(void* user_data, GC_heap_section_proc callback);
  1846. GC_API GC_word GC_get_heap_section_count(void);
  1847. #ifdef __cplusplus
  1848. } /* extern "C" */
  1849. #endif
  1850. #endif /* GC_H */