gc_priv.h 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712
  1. /*
  2. * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3. * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
  4. * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
  5. * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
  6. *
  7. *
  8. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  9. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  10. *
  11. * Permission is hereby granted to use or copy this program
  12. * for any purpose, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. */
  17. #ifndef GC_PRIVATE_H
  18. #define GC_PRIVATE_H
  19. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #ifndef GC_BUILD
  23. # define GC_BUILD
  24. #endif
  25. #if (defined(__linux__) || defined(__GLIBC__) || defined(__GNU__)) \
  26. && !defined(_GNU_SOURCE)
  27. /* Can't test LINUX, since this must be defined before other includes. */
  28. # define _GNU_SOURCE 1
  29. #endif
  30. #if (defined(DGUX) && defined(GC_THREADS) || defined(DGUX386_THREADS) \
  31. || defined(GC_DGUX386_THREADS)) && !defined(_USING_POSIX4A_DRAFT10)
  32. # define _USING_POSIX4A_DRAFT10 1
  33. #endif
  34. #if defined(__MINGW32__) && !defined(__MINGW_EXCPT_DEFINE_PSDK) \
  35. && defined(__i386__) && defined(GC_EXTERN) /* defined in gc.c */
  36. /* See the description in mark.c. */
  37. # define __MINGW_EXCPT_DEFINE_PSDK 1
  38. #endif
  39. # if defined(NO_DEBUGGING) && !defined(GC_ASSERTIONS) && !defined(NDEBUG)
  40. /* To turn off assertion checking (in atomic_ops.h). */
  41. # define NDEBUG 1
  42. # endif
  43. #ifndef GC_H
  44. # include "../gc.h"
  45. #endif
  46. #include <stdlib.h>
  47. #if !defined(sony_news)
  48. # include <stddef.h>
  49. #endif
  50. #ifdef DGUX
  51. # include <sys/types.h>
  52. # include <sys/time.h>
  53. # include <sys/resource.h>
  54. #endif /* DGUX */
  55. #ifdef BSD_TIME
  56. # include <sys/types.h>
  57. # include <sys/time.h>
  58. # include <sys/resource.h>
  59. #endif /* BSD_TIME */
  60. #ifdef PARALLEL_MARK
  61. # define AO_REQUIRE_CAS
  62. # if !defined(__GNUC__) && !defined(AO_ASSUME_WINDOWS98)
  63. # define AO_ASSUME_WINDOWS98
  64. # endif
  65. #endif
  66. #ifndef GC_TINY_FL_H
  67. # include "../gc_tiny_fl.h"
  68. #endif
  69. #ifndef GC_MARK_H
  70. # include "../gc_mark.h"
  71. #endif
  72. typedef GC_word word;
  73. typedef GC_signed_word signed_word;
  74. typedef unsigned int unsigned32;
  75. typedef int GC_bool;
  76. #define TRUE 1
  77. #define FALSE 0
  78. typedef char * ptr_t; /* A generic pointer to which we can add */
  79. /* byte displacements and which can be used */
  80. /* for address comparisons. */
  81. #ifndef SIZE_MAX
  82. # include <limits.h>
  83. #endif
  84. #if defined(SIZE_MAX) && !defined(CPPCHECK)
  85. # define GC_SIZE_MAX ((size_t)SIZE_MAX)
  86. /* Extra cast to workaround some buggy SIZE_MAX definitions. */
  87. #else
  88. # define GC_SIZE_MAX (~(size_t)0)
  89. #endif
  90. #if GC_GNUC_PREREQ(3, 0) && !defined(LINT2)
  91. # define EXPECT(expr, outcome) __builtin_expect(expr,outcome)
  92. /* Equivalent to (expr), but predict that usually (expr)==outcome. */
  93. #else
  94. # define EXPECT(expr, outcome) (expr)
  95. #endif /* __GNUC__ */
  96. /* Saturated addition of size_t values. Used to avoid value wrap */
  97. /* around on overflow. The arguments should have no side effects. */
  98. #define SIZET_SAT_ADD(a, b) \
  99. (EXPECT((a) < GC_SIZE_MAX - (b), TRUE) ? (a) + (b) : GC_SIZE_MAX)
  100. #ifndef GCCONFIG_H
  101. # include "gcconfig.h"
  102. #endif
  103. #if !defined(GC_ATOMIC_UNCOLLECTABLE) && defined(ATOMIC_UNCOLLECTABLE)
  104. /* For compatibility with old-style naming. */
  105. # define GC_ATOMIC_UNCOLLECTABLE
  106. #endif
  107. #ifndef GC_INNER
  108. /* This tagging macro must be used at the start of every variable */
  109. /* definition which is declared with GC_EXTERN. Should be also used */
  110. /* for the GC-scope function definitions and prototypes. Must not be */
  111. /* used in gcconfig.h. Shouldn't be used for the debugging-only */
  112. /* functions. Currently, not used for the functions declared in or */
  113. /* called from the "dated" source files (located in "extra" folder). */
  114. # if defined(GC_DLL) && defined(__GNUC__) && !defined(MSWIN32) \
  115. && !defined(MSWINCE) && !defined(CYGWIN32)
  116. # if GC_GNUC_PREREQ(4, 0) && !defined(GC_NO_VISIBILITY)
  117. /* See the corresponding GC_API definition. */
  118. # define GC_INNER __attribute__((__visibility__("hidden")))
  119. # else
  120. /* The attribute is unsupported. */
  121. # define GC_INNER /* empty */
  122. # endif
  123. # else
  124. # define GC_INNER /* empty */
  125. # endif
  126. # define GC_EXTERN extern GC_INNER
  127. /* Used only for the GC-scope variables (prefixed with "GC_") */
  128. /* declared in the header files. Must not be used for thread-local */
  129. /* variables. Must not be used in gcconfig.h. Shouldn't be used for */
  130. /* the debugging-only or profiling-only variables. Currently, not */
  131. /* used for the variables accessed from the "dated" source files */
  132. /* (specific.c/h, and in the "extra" folder). */
  133. /* The corresponding variable definition must start with GC_INNER. */
  134. #endif /* !GC_INNER */
  135. #ifdef __cplusplus
  136. /* Register storage specifier is deprecated in C++11. */
  137. # define REGISTER /* empty */
  138. #else
  139. /* Used only for several local variables in the performance-critical */
  140. /* functions. Should not be used for new code. */
  141. # define REGISTER register
  142. #endif
  143. #ifndef HEADERS_H
  144. # include "gc_hdrs.h"
  145. #endif
  146. #ifndef GC_ATTR_NO_SANITIZE_ADDR
  147. # ifndef ADDRESS_SANITIZER
  148. # define GC_ATTR_NO_SANITIZE_ADDR /* empty */
  149. # elif GC_CLANG_PREREQ(3, 8)
  150. # define GC_ATTR_NO_SANITIZE_ADDR __attribute__((no_sanitize("address")))
  151. # else
  152. # define GC_ATTR_NO_SANITIZE_ADDR __attribute__((no_sanitize_address))
  153. # endif
  154. #endif /* !GC_ATTR_NO_SANITIZE_ADDR */
  155. #ifndef GC_ATTR_NO_SANITIZE_MEMORY
  156. # ifndef MEMORY_SANITIZER
  157. # define GC_ATTR_NO_SANITIZE_MEMORY /* empty */
  158. # elif GC_CLANG_PREREQ(3, 8)
  159. # define GC_ATTR_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
  160. # else
  161. # define GC_ATTR_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
  162. # endif
  163. #endif /* !GC_ATTR_NO_SANITIZE_MEMORY */
  164. #ifndef GC_ATTR_NO_SANITIZE_THREAD
  165. # ifndef THREAD_SANITIZER
  166. # define GC_ATTR_NO_SANITIZE_THREAD /* empty */
  167. # elif GC_CLANG_PREREQ(3, 8)
  168. # define GC_ATTR_NO_SANITIZE_THREAD __attribute__((no_sanitize("thread")))
  169. # else
  170. # define GC_ATTR_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
  171. # endif
  172. #endif /* !GC_ATTR_NO_SANITIZE_THREAD */
  173. #ifndef GC_ATTR_UNUSED
  174. # if GC_GNUC_PREREQ(3, 4)
  175. # define GC_ATTR_UNUSED __attribute__((__unused__))
  176. # else
  177. # define GC_ATTR_UNUSED /* empty */
  178. # endif
  179. #endif /* !GC_ATTR_UNUSED */
  180. #ifdef HAVE_CONFIG_H
  181. /* The "inline" keyword is determined by Autoconf AC_C_INLINE. */
  182. # define GC_INLINE static inline
  183. #elif defined(_MSC_VER) || defined(__INTEL_COMPILER) || defined(__DMC__) \
  184. || (GC_GNUC_PREREQ(3, 0) && defined(__STRICT_ANSI__)) \
  185. || defined(__WATCOMC__)
  186. # define GC_INLINE static __inline
  187. #elif GC_GNUC_PREREQ(3, 0) || defined(__sun)
  188. # define GC_INLINE static inline
  189. #else
  190. # define GC_INLINE static
  191. #endif
  192. #ifndef GC_ATTR_NOINLINE
  193. # if GC_GNUC_PREREQ(4, 0)
  194. # define GC_ATTR_NOINLINE __attribute__((__noinline__))
  195. # elif _MSC_VER >= 1400
  196. # define GC_ATTR_NOINLINE __declspec(noinline)
  197. # else
  198. # define GC_ATTR_NOINLINE /* empty */
  199. # endif
  200. #endif
  201. #ifndef GC_API_OSCALL
  202. /* This is used to identify GC routines called by name from OS. */
  203. # if defined(__GNUC__)
  204. # if GC_GNUC_PREREQ(4, 0) && !defined(GC_NO_VISIBILITY)
  205. /* Same as GC_API if GC_DLL. */
  206. # define GC_API_OSCALL extern __attribute__((__visibility__("default")))
  207. # else
  208. /* The attribute is unsupported. */
  209. # define GC_API_OSCALL extern
  210. # endif
  211. # else
  212. # define GC_API_OSCALL GC_API
  213. # endif
  214. #endif
  215. #ifndef GC_API_PRIV
  216. # define GC_API_PRIV GC_API
  217. #endif
  218. #if defined(THREADS)
  219. # include "gc_atomic_ops.h"
  220. #endif
  221. #ifndef GC_LOCKS_H
  222. # include "gc_locks.h"
  223. #endif
  224. #define ONES ((word)(signed_word)(-1))
  225. # ifdef STACK_GROWS_DOWN
  226. # define COOLER_THAN >
  227. # define HOTTER_THAN <
  228. # define MAKE_COOLER(x,y) if ((word)((x) + (y)) > (word)(x)) {(x) += (y);} \
  229. else (x) = (ptr_t)ONES
  230. # define MAKE_HOTTER(x,y) (x) -= (y)
  231. # else
  232. # define COOLER_THAN <
  233. # define HOTTER_THAN >
  234. # define MAKE_COOLER(x,y) if ((word)((x) - (y)) < (word)(x)) {(x) -= (y);} \
  235. else (x) = 0
  236. # define MAKE_HOTTER(x,y) (x) += (y)
  237. # endif
  238. #if defined(AMIGA) && defined(__SASC)
  239. # define GC_FAR __far
  240. #else
  241. # define GC_FAR
  242. #endif
  243. /*********************************/
  244. /* */
  245. /* Definitions for conservative */
  246. /* collector */
  247. /* */
  248. /*********************************/
  249. /*********************************/
  250. /* */
  251. /* Easily changeable parameters */
  252. /* */
  253. /*********************************/
  254. /* #define ALL_INTERIOR_POINTERS */
  255. /* Forces all pointers into the interior of an */
  256. /* object to be considered valid. Also causes the */
  257. /* sizes of all objects to be inflated by at least */
  258. /* one byte. This should suffice to guarantee */
  259. /* that in the presence of a compiler that does */
  260. /* not perform garbage-collector-unsafe */
  261. /* optimizations, all portable, strictly ANSI */
  262. /* conforming C programs should be safely usable */
  263. /* with malloc replaced by GC_malloc and free */
  264. /* calls removed. There are several disadvantages: */
  265. /* 1. There are probably no interesting, portable, */
  266. /* strictly ANSI conforming C programs. */
  267. /* 2. This option makes it hard for the collector */
  268. /* to allocate space that is not "pointed to" */
  269. /* by integers, etc. Under SunOS 4.X with a */
  270. /* statically linked libc, we empirically */
  271. /* observed that it would be difficult to */
  272. /* allocate individual objects larger than 100K. */
  273. /* Even if only smaller objects are allocated, */
  274. /* more swap space is likely to be needed. */
  275. /* Fortunately, much of this will never be */
  276. /* touched. */
  277. /* If you can easily avoid using this option, do. */
  278. /* If not, try to keep individual objects small. */
  279. /* This is now really controlled at startup, */
  280. /* through GC_all_interior_pointers. */
  281. EXTERN_C_BEGIN
  282. #ifndef GC_NO_FINALIZATION
  283. # define GC_INVOKE_FINALIZERS() GC_notify_or_invoke_finalizers()
  284. GC_INNER void GC_notify_or_invoke_finalizers(void);
  285. /* If GC_finalize_on_demand is not set, invoke */
  286. /* eligible finalizers. Otherwise: */
  287. /* Call *GC_finalizer_notifier if there are */
  288. /* finalizers to be run, and we haven't called */
  289. /* this procedure yet this GC cycle. */
  290. GC_INNER void GC_finalize(void);
  291. /* Perform all indicated finalization actions */
  292. /* on unmarked objects. */
  293. /* Unreachable finalizable objects are enqueued */
  294. /* for processing by GC_invoke_finalizers. */
  295. /* Invoked with lock. */
  296. # ifndef GC_TOGGLE_REFS_NOT_NEEDED
  297. GC_INNER void GC_process_togglerefs(void);
  298. /* Process the toggle-refs before GC starts. */
  299. # endif
  300. # ifndef SMALL_CONFIG
  301. GC_INNER void GC_print_finalization_stats(void);
  302. # endif
  303. #else
  304. # define GC_INVOKE_FINALIZERS() (void)0
  305. #endif /* GC_NO_FINALIZATION */
  306. #if !defined(DONT_ADD_BYTE_AT_END)
  307. # ifdef LINT2
  308. /* Explicitly instruct the code analysis tool that */
  309. /* GC_all_interior_pointers is assumed to have only 0 or 1 value. */
  310. # define EXTRA_BYTES ((size_t)(GC_all_interior_pointers? 1 : 0))
  311. # else
  312. # define EXTRA_BYTES (size_t)GC_all_interior_pointers
  313. # endif
  314. # define MAX_EXTRA_BYTES 1
  315. #else
  316. # define EXTRA_BYTES 0
  317. # define MAX_EXTRA_BYTES 0
  318. #endif
  319. # ifndef LARGE_CONFIG
  320. # define MINHINCR 16 /* Minimum heap increment, in blocks of HBLKSIZE */
  321. /* Must be multiple of largest page size. */
  322. # define MAXHINCR 2048 /* Maximum heap increment, in blocks */
  323. # else
  324. # define MINHINCR 64
  325. # define MAXHINCR 4096
  326. # endif
  327. # define BL_LIMIT GC_black_list_spacing
  328. /* If we need a block of N bytes, and we have */
  329. /* a block of N + BL_LIMIT bytes available, */
  330. /* and N > BL_LIMIT, */
  331. /* but all possible positions in it are */
  332. /* blacklisted, we just use it anyway (and */
  333. /* print a warning, if warnings are enabled). */
  334. /* This risks subsequently leaking the block */
  335. /* due to a false reference. But not using */
  336. /* the block risks unreasonable immediate */
  337. /* heap growth. */
  338. /*********************************/
  339. /* */
  340. /* Stack saving for debugging */
  341. /* */
  342. /*********************************/
  343. #ifdef NEED_CALLINFO
  344. struct callinfo {
  345. word ci_pc; /* Caller, not callee, pc */
  346. # if NARGS > 0
  347. word ci_arg[NARGS]; /* bit-wise complement to avoid retention */
  348. # endif
  349. # if (NFRAMES * (NARGS + 1)) % 2 == 1
  350. /* Likely alignment problem. */
  351. word ci_dummy;
  352. # endif
  353. };
  354. #endif
  355. #ifdef SAVE_CALL_CHAIN
  356. /* Fill in the pc and argument information for up to NFRAMES of my */
  357. /* callers. Ignore my frame and my callers frame. */
  358. GC_INNER void GC_save_callers(struct callinfo info[NFRAMES]);
  359. GC_INNER void GC_print_callers(struct callinfo info[NFRAMES]);
  360. #endif
  361. EXTERN_C_END
  362. /*********************************/
  363. /* */
  364. /* OS interface routines */
  365. /* */
  366. /*********************************/
  367. #ifndef NO_CLOCK
  368. #ifdef BSD_TIME
  369. # undef CLOCK_TYPE
  370. # undef GET_TIME
  371. # undef MS_TIME_DIFF
  372. # define CLOCK_TYPE struct timeval
  373. # define GET_TIME(x) \
  374. do { \
  375. struct rusage rusage; \
  376. getrusage(RUSAGE_SELF, &rusage); \
  377. x = rusage.ru_utime; \
  378. } while (0)
  379. # define MS_TIME_DIFF(a,b) ((unsigned long)(a.tv_sec - b.tv_sec) * 1000 \
  380. + (unsigned long)(a.tv_usec - b.tv_usec) / 1000)
  381. # define NS_TIME_DIFF(a,b) ((unsigned long long)(a.tv_sec - b.tv_sec) * 1000000000 \
  382. + (unsigned long long)(a.tv_usec - b.tv_usec) * 1000)
  383. #elif defined(MSWIN32) || defined(MSWINCE)
  384. # ifndef WIN32_LEAN_AND_MEAN
  385. # define WIN32_LEAN_AND_MEAN 1
  386. # endif
  387. # define NOSERVICE
  388. # include <windows.h>
  389. # include <winbase.h>
  390. # define CLOCK_TYPE LONGLONG
  391. # define GET_TIME(x) \
  392. do { \
  393. LARGE_INTEGER freq, t; \
  394. QueryPerformanceFrequency(&freq); \
  395. QueryPerformanceCounter(&t); \
  396. x = (LONGLONG)(t.QuadPart * (1000000000.0 / (double)freq.QuadPart)); \
  397. } while (0)
  398. # define MS_TIME_DIFF(a,b) ((unsigned long)(((a) - (b)) / 1000000))
  399. # define NS_TIME_DIFF(a,b) ((a) - (b))
  400. #elif defined(NN_PLATFORM_CTR)
  401. # define CLOCK_TYPE long long
  402. EXTERN_C_BEGIN
  403. CLOCK_TYPE n3ds_get_system_tick(void);
  404. CLOCK_TYPE n3ds_convert_tick_to_ms(CLOCK_TYPE tick);
  405. EXTERN_C_END
  406. # define GET_TIME(x) (void)(x = n3ds_get_system_tick())
  407. # define MS_TIME_DIFF(a,b) ((long)n3ds_convert_tick_to_ms((a)-(b)))
  408. # define NS_TIME_DIFF(a,b) ((long long)n3ds_convert_tick_to_ms((a)-(b)) * 1000000)
  409. #elif defined(NINTENDO_SWITCH)
  410. #include <time.h>
  411. # define CLOCK_TYPE long long
  412. # define GET_TIME(x) \
  413. do { \
  414. struct timespec t;\
  415. int r = clock_gettime(CLOCK_REALTIME, &t);\
  416. x=-1;\
  417. if (r != -1) {\
  418. x = (t.tv_sec * 1000000000) + t.tv_nsec;\
  419. }\
  420. } while (0)
  421. # define MS_TIME_DIFF(a,b) ((unsigned long)(((a) - (b)) / 1000000))
  422. # define NS_TIME_DIFF(a,b) ((a)-(b))
  423. #else /* !BSD_TIME && !NN_PLATFORM_CTR && !MSWIN32 && !MSWINCE && !NINTENDO_SWITCH */
  424. # include <time.h>
  425. # if defined(FREEBSD) && !defined(CLOCKS_PER_SEC)
  426. # include <machine/limits.h>
  427. # define CLOCKS_PER_SEC CLK_TCK
  428. # endif
  429. # if !defined(CLOCKS_PER_SEC)
  430. # define CLOCKS_PER_SEC 1000000
  431. /* This is technically a bug in the implementation. */
  432. /* ANSI requires that CLOCKS_PER_SEC be defined. But at least */
  433. /* under SunOS 4.1.1, it isn't. Also note that the combination of */
  434. /* ANSI C and POSIX is incredibly gross here. The type clock_t */
  435. /* is used by both clock() and times(). But on some machines */
  436. /* these use different notions of a clock tick, CLOCKS_PER_SEC */
  437. /* seems to apply only to clock. Hence we use it here. On many */
  438. /* machines, including SunOS, clock actually uses units of */
  439. /* microseconds (which are not really clock ticks). */
  440. # endif
  441. # define CLOCK_TYPE clock_t
  442. # define GET_TIME(x) (void)(x = clock())
  443. # define MS_TIME_DIFF(a,b) (CLOCKS_PER_SEC % 1000 == 0 ? \
  444. (unsigned long)((a) - (b)) / (unsigned long)(CLOCKS_PER_SEC / 1000) \
  445. : ((unsigned long)((a) - (b)) * 1000) / (unsigned long)CLOCKS_PER_SEC)
  446. # define NS_TIME_DIFF(a,b) (((unsigned long long)((a) - (b)) * 1000000000) / (unsigned long long)CLOCKS_PER_SEC)
  447. /* Avoid using double type since some targets (like ARM) might */
  448. /* require -lm option for double-to-long conversion. */
  449. #endif /* !BSD_TIME && !MSWIN32 */
  450. #endif /* !NO_CLOCK */
  451. /* We use bzero and bcopy internally. They may not be available. */
  452. # if defined(SPARC) && defined(SUNOS4) \
  453. || (defined(M68K) && defined(NEXT)) || defined(VAX)
  454. # define BCOPY_EXISTS
  455. # elif defined(AMIGA) || defined(DARWIN)
  456. # include <string.h>
  457. # define BCOPY_EXISTS
  458. # elif defined(MACOS) && defined(POWERPC)
  459. # include <MacMemory.h>
  460. # define bcopy(x,y,n) BlockMoveData(x, y, n)
  461. # define bzero(x,n) BlockZero(x, n)
  462. # define BCOPY_EXISTS
  463. # endif
  464. # if !defined(BCOPY_EXISTS) || defined(CPPCHECK)
  465. # include <string.h>
  466. # define BCOPY(x,y,n) memcpy(y, x, (size_t)(n))
  467. # define BZERO(x,n) memset(x, 0, (size_t)(n))
  468. # else
  469. # define BCOPY(x,y,n) bcopy((void *)(x),(void *)(y),(size_t)(n))
  470. # define BZERO(x,n) bzero((void *)(x),(size_t)(n))
  471. # endif
  472. #ifdef PCR
  473. # include "th/PCR_ThCtl.h"
  474. #endif
  475. EXTERN_C_BEGIN
  476. /*
  477. * Stop and restart mutator threads.
  478. */
  479. # ifdef PCR
  480. # define STOP_WORLD() \
  481. PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_stopNormal, \
  482. PCR_allSigsBlocked, \
  483. PCR_waitForever)
  484. # define START_WORLD() \
  485. PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_null, \
  486. PCR_allSigsBlocked, \
  487. PCR_waitForever)
  488. # else
  489. # if defined(NN_PLATFORM_CTR) || defined(NINTENDO_SWITCH) \
  490. || defined(GC_WIN32_THREADS) || defined(GC_PTHREADS)
  491. GC_INNER void GC_stop_world(void);
  492. GC_INNER void GC_start_world(void);
  493. # define STOP_WORLD() GC_stop_world()
  494. # define START_WORLD() GC_start_world()
  495. # else
  496. /* Just do a sanity check: we are not inside GC_do_blocking(). */
  497. # define STOP_WORLD() GC_ASSERT(GC_blocked_sp == NULL)
  498. # define START_WORLD()
  499. # endif
  500. # endif
  501. #ifdef THREADS
  502. GC_EXTERN GC_on_thread_event_proc GC_on_thread_event;
  503. #endif
  504. /* Abandon ship */
  505. # if defined(SMALL_CONFIG) || defined(PCR)
  506. # define GC_on_abort(msg) (void)0 /* be silent on abort */
  507. # else
  508. GC_API_PRIV GC_abort_func GC_on_abort;
  509. # endif
  510. # if defined(CPPCHECK)
  511. # define ABORT(msg) { GC_on_abort(msg); abort(); }
  512. # elif defined(PCR)
  513. # define ABORT(s) PCR_Base_Panic(s)
  514. # else
  515. # if defined(MSWINCE) && !defined(DebugBreak) \
  516. && (!defined(UNDER_CE) || (defined(__MINGW32CE__) && !defined(ARM32)))
  517. /* This simplifies linking for WinCE (and, probably, doesn't */
  518. /* hurt debugging much); use -DDebugBreak=DebugBreak to override */
  519. /* this behavior if really needed. This is also a workaround for */
  520. /* x86mingw32ce toolchain (if it is still declaring DebugBreak() */
  521. /* instead of defining it as a macro). */
  522. # define DebugBreak() _exit(-1) /* there is no abort() in WinCE */
  523. # endif
  524. # if defined(MSWIN32) && (defined(NO_DEBUGGING) || defined(LINT2))
  525. /* A more user-friendly abort after showing fatal message. */
  526. # define ABORT(msg) (GC_on_abort(msg), _exit(-1))
  527. /* Exit on error without running "at-exit" callbacks. */
  528. # elif defined(MSWINCE) && defined(NO_DEBUGGING)
  529. # define ABORT(msg) (GC_on_abort(msg), ExitProcess(-1))
  530. # elif defined(MSWIN32) || defined(MSWINCE)
  531. # if defined(_CrtDbgBreak) && defined(_DEBUG) && defined(_MSC_VER)
  532. # define ABORT(msg) { GC_on_abort(msg); \
  533. _CrtDbgBreak() /* __debugbreak() */; }
  534. # else
  535. # define ABORT(msg) { GC_on_abort(msg); __debugbreak(); }
  536. /* Note that: on a WinCE box, this could be silently */
  537. /* ignored (i.e., the program is not aborted); */
  538. /* DebugBreak is a statement in some toolchains. */
  539. # endif
  540. # else
  541. # define ABORT(msg) (GC_on_abort(msg), abort())
  542. # endif /* !MSWIN32 */
  543. # endif /* !PCR */
  544. /* For abort message with 1-3 arguments. C_msg and C_fmt should be */
  545. /* literals. C_msg should not contain format specifiers. Arguments */
  546. /* should match their format specifiers. */
  547. #define ABORT_ARG1(C_msg, C_fmt, arg1) \
  548. do { \
  549. GC_INFOLOG_PRINTF(C_msg /* + */ C_fmt "\n", arg1); \
  550. ABORT(C_msg); \
  551. } while (0)
  552. #define ABORT_ARG2(C_msg, C_fmt, arg1, arg2) \
  553. do { \
  554. GC_INFOLOG_PRINTF(C_msg /* + */ C_fmt "\n", arg1, arg2); \
  555. ABORT(C_msg); \
  556. } while (0)
  557. #define ABORT_ARG3(C_msg, C_fmt, arg1, arg2, arg3) \
  558. do { \
  559. GC_INFOLOG_PRINTF(C_msg /* + */ C_fmt "\n", \
  560. arg1, arg2, arg3); \
  561. ABORT(C_msg); \
  562. } while (0)
  563. /* Same as ABORT but does not have 'no-return' attribute. */
  564. /* ABORT on a dummy condition (which is always true). */
  565. #define ABORT_RET(msg) \
  566. if ((signed_word)GC_current_warn_proc == -1) {} else ABORT(msg)
  567. /* Exit abnormally, but without making a mess (e.g. out of memory) */
  568. # ifdef PCR
  569. # define EXIT() PCR_Base_Exit(1,PCR_waitForever)
  570. # else
  571. # define EXIT() (GC_on_abort(NULL), exit(1 /* EXIT_FAILURE */))
  572. # endif
  573. /* Print warning message, e.g. almost out of memory. */
  574. /* The argument (if any) format specifier should be: */
  575. /* "%s", "%p" or "%"WARN_PRIdPTR. */
  576. #define WARN(msg, arg) \
  577. (*GC_current_warn_proc)((/* no const */ char *)("GC Warning: " msg), \
  578. (word)(arg))
  579. GC_EXTERN GC_warn_proc GC_current_warn_proc;
  580. /* Print format type macro for decimal signed_word value passed WARN(). */
  581. /* This could be redefined for Win64 or LLP64, but typically should */
  582. /* not be done as the WARN format string is, possibly, processed on the */
  583. /* client side, so non-standard print type modifiers (like MS "I64d") */
  584. /* should be avoided here if possible. */
  585. #ifndef WARN_PRIdPTR
  586. /* Assume sizeof(void *) == sizeof(long) (or a little-endian machine) */
  587. # define WARN_PRIdPTR "ld"
  588. #endif
  589. /* A tagging macro (for a code static analyzer) to indicate that the */
  590. /* string obtained from an untrusted source (e.g., argv[], getenv) is */
  591. /* safe to use in a vulnerable operation (e.g., open, exec). */
  592. #define TRUSTED_STRING(s) (char*)COVERT_DATAFLOW(s)
  593. /* Get environment entry */
  594. #ifdef GC_READ_ENV_FILE
  595. GC_INNER char * GC_envfile_getenv(const char *name);
  596. # define GETENV(name) GC_envfile_getenv(name)
  597. #elif defined(NO_GETENV) && !defined(CPPCHECK)
  598. # define GETENV(name) NULL
  599. #elif defined(EMPTY_GETENV_RESULTS)
  600. /* Workaround for a reputed Wine bug. */
  601. GC_INLINE char * fixed_getenv(const char *name)
  602. {
  603. char *value = getenv(name);
  604. return value != NULL && *value != '\0' ? value : NULL;
  605. }
  606. # define GETENV(name) fixed_getenv(name)
  607. #else
  608. # define GETENV(name) getenv(name)
  609. #endif
  610. EXTERN_C_END
  611. #if defined(DARWIN)
  612. # include <mach/thread_status.h>
  613. # ifndef MAC_OS_X_VERSION_MAX_ALLOWED
  614. # include <AvailabilityMacros.h>
  615. /* Include this header just to import the above macro. */
  616. # endif
  617. # if defined(POWERPC)
  618. # if CPP_WORDSZ == 32
  619. # define GC_THREAD_STATE_T ppc_thread_state_t
  620. # else
  621. # define GC_THREAD_STATE_T ppc_thread_state64_t
  622. # define GC_MACH_THREAD_STATE PPC_THREAD_STATE64
  623. # define GC_MACH_THREAD_STATE_COUNT PPC_THREAD_STATE64_COUNT
  624. # endif
  625. # elif defined(I386) || defined(X86_64)
  626. # if CPP_WORDSZ == 32
  627. # if defined(i386_THREAD_STATE_COUNT) && !defined(x86_THREAD_STATE32_COUNT)
  628. /* Use old naming convention for 32-bit x86. */
  629. # define GC_THREAD_STATE_T i386_thread_state_t
  630. # define GC_MACH_THREAD_STATE i386_THREAD_STATE
  631. # define GC_MACH_THREAD_STATE_COUNT i386_THREAD_STATE_COUNT
  632. # else
  633. # define GC_THREAD_STATE_T x86_thread_state32_t
  634. # define GC_MACH_THREAD_STATE x86_THREAD_STATE32
  635. # define GC_MACH_THREAD_STATE_COUNT x86_THREAD_STATE32_COUNT
  636. # endif
  637. # else
  638. # define GC_THREAD_STATE_T x86_thread_state64_t
  639. # define GC_MACH_THREAD_STATE x86_THREAD_STATE64
  640. # define GC_MACH_THREAD_STATE_COUNT x86_THREAD_STATE64_COUNT
  641. # endif
  642. # elif defined(ARM32) && defined(ARM_UNIFIED_THREAD_STATE) \
  643. && !defined(CPPCHECK)
  644. # define GC_THREAD_STATE_T arm_unified_thread_state_t
  645. # define GC_MACH_THREAD_STATE ARM_UNIFIED_THREAD_STATE
  646. # define GC_MACH_THREAD_STATE_COUNT ARM_UNIFIED_THREAD_STATE_COUNT
  647. # elif defined(ARM32)
  648. # define GC_THREAD_STATE_T arm_thread_state_t
  649. # ifdef ARM_MACHINE_THREAD_STATE_COUNT
  650. # define GC_MACH_THREAD_STATE ARM_MACHINE_THREAD_STATE
  651. # define GC_MACH_THREAD_STATE_COUNT ARM_MACHINE_THREAD_STATE_COUNT
  652. # endif
  653. # elif defined(AARCH64)
  654. # define GC_THREAD_STATE_T arm_thread_state64_t
  655. # define GC_MACH_THREAD_STATE ARM_THREAD_STATE64
  656. # define GC_MACH_THREAD_STATE_COUNT ARM_THREAD_STATE64_COUNT
  657. # elif !defined(CPPCHECK)
  658. # error define GC_THREAD_STATE_T
  659. # endif
  660. # ifndef GC_MACH_THREAD_STATE
  661. # define GC_MACH_THREAD_STATE MACHINE_THREAD_STATE
  662. # define GC_MACH_THREAD_STATE_COUNT MACHINE_THREAD_STATE_COUNT
  663. # endif
  664. # if CPP_WORDSZ == 32
  665. # define GC_MACH_HEADER mach_header
  666. # define GC_MACH_SECTION section
  667. # define GC_GETSECTBYNAME getsectbynamefromheader
  668. # else
  669. # define GC_MACH_HEADER mach_header_64
  670. # define GC_MACH_SECTION section_64
  671. # define GC_GETSECTBYNAME getsectbynamefromheader_64
  672. # endif
  673. /* Try to work out the right way to access thread state structure */
  674. /* members. The structure has changed its definition in different */
  675. /* Darwin versions. This now defaults to the (older) names */
  676. /* without __, thus hopefully, not breaking any existing */
  677. /* Makefile.direct builds. */
  678. # if __DARWIN_UNIX03
  679. # define THREAD_FLD_NAME(x) __ ## x
  680. # else
  681. # define THREAD_FLD_NAME(x) x
  682. # endif
  683. # if defined(ARM32) && defined(ARM_UNIFIED_THREAD_STATE)
  684. # define THREAD_FLD(x) ts_32.THREAD_FLD_NAME(x)
  685. # else
  686. # define THREAD_FLD(x) THREAD_FLD_NAME(x)
  687. # endif
  688. #endif /* DARWIN */
  689. #include "../gc_tiny_fl.h"
  690. #include <setjmp.h>
  691. EXTERN_C_BEGIN
  692. /*********************************/
  693. /* */
  694. /* Word-size-dependent defines */
  695. /* */
  696. /*********************************/
  697. #if CPP_WORDSZ == 32
  698. # define WORDS_TO_BYTES(x) ((x)<<2)
  699. # define BYTES_TO_WORDS(x) ((x)>>2)
  700. # define LOGWL ((word)5) /* log[2] of CPP_WORDSZ */
  701. # define modWORDSZ(n) ((n) & 0x1f) /* n mod size of word */
  702. # if ALIGNMENT != 4
  703. # define UNALIGNED_PTRS
  704. # endif
  705. #endif
  706. #if CPP_WORDSZ == 64
  707. # define WORDS_TO_BYTES(x) ((x)<<3)
  708. # define BYTES_TO_WORDS(x) ((x)>>3)
  709. # define LOGWL ((word)6) /* log[2] of CPP_WORDSZ */
  710. # define modWORDSZ(n) ((n) & 0x3f) /* n mod size of word */
  711. # if ALIGNMENT != 8
  712. # define UNALIGNED_PTRS
  713. # endif
  714. #endif
  715. /* The first TINY_FREELISTS free lists correspond to the first */
  716. /* TINY_FREELISTS multiples of GRANULE_BYTES, i.e. we keep */
  717. /* separate free lists for each multiple of GRANULE_BYTES */
  718. /* up to (TINY_FREELISTS-1) * GRANULE_BYTES. After that they */
  719. /* may be spread out further. */
  720. #define GRANULE_BYTES GC_GRANULE_BYTES
  721. #define TINY_FREELISTS GC_TINY_FREELISTS
  722. #define WORDSZ ((word)CPP_WORDSZ)
  723. #define SIGNB ((word)1 << (WORDSZ-1))
  724. #define BYTES_PER_WORD ((word)(sizeof (word)))
  725. #define divWORDSZ(n) ((n) >> LOGWL) /* divide n by size of word */
  726. #if GRANULE_BYTES == 8
  727. # define BYTES_TO_GRANULES(n) ((n)>>3)
  728. # define GRANULES_TO_BYTES(n) ((n)<<3)
  729. # if CPP_WORDSZ == 64
  730. # define GRANULES_TO_WORDS(n) (n)
  731. # elif CPP_WORDSZ == 32
  732. # define GRANULES_TO_WORDS(n) ((n)<<1)
  733. # else
  734. # define GRANULES_TO_WORDS(n) BYTES_TO_WORDS(GRANULES_TO_BYTES(n))
  735. # endif
  736. #elif GRANULE_BYTES == 16
  737. # define BYTES_TO_GRANULES(n) ((n)>>4)
  738. # define GRANULES_TO_BYTES(n) ((n)<<4)
  739. # if CPP_WORDSZ == 64
  740. # define GRANULES_TO_WORDS(n) ((n)<<1)
  741. # elif CPP_WORDSZ == 32
  742. # define GRANULES_TO_WORDS(n) ((n)<<2)
  743. # else
  744. # define GRANULES_TO_WORDS(n) BYTES_TO_WORDS(GRANULES_TO_BYTES(n))
  745. # endif
  746. #else
  747. # error Bad GRANULE_BYTES value
  748. #endif
  749. /*********************/
  750. /* */
  751. /* Size Parameters */
  752. /* */
  753. /*********************/
  754. /* Heap block size, bytes. Should be power of 2. */
  755. /* Incremental GC with MPROTECT_VDB currently requires the */
  756. /* page size to be a multiple of HBLKSIZE. Since most modern */
  757. /* architectures support variable page sizes down to 4K, and */
  758. /* X86 is generally 4K, we now default to 4K, except for */
  759. /* Alpha: Seems to be used with 8K pages. */
  760. /* SMALL_CONFIG: Want less block-level fragmentation. */
  761. #ifndef HBLKSIZE
  762. # if defined(LARGE_CONFIG) || !defined(SMALL_CONFIG)
  763. # ifdef ALPHA
  764. # define CPP_LOG_HBLKSIZE 13
  765. # elif defined(SN_TARGET_PSP2)
  766. # define CPP_LOG_HBLKSIZE 16 /* page size is set to 64K */
  767. # else
  768. # define CPP_LOG_HBLKSIZE 12
  769. # endif
  770. # else
  771. # define CPP_LOG_HBLKSIZE 10
  772. # endif
  773. #else
  774. # if HBLKSIZE == 512
  775. # define CPP_LOG_HBLKSIZE 9
  776. # elif HBLKSIZE == 1024
  777. # define CPP_LOG_HBLKSIZE 10
  778. # elif HBLKSIZE == 2048
  779. # define CPP_LOG_HBLKSIZE 11
  780. # elif HBLKSIZE == 4096
  781. # define CPP_LOG_HBLKSIZE 12
  782. # elif HBLKSIZE == 8192
  783. # define CPP_LOG_HBLKSIZE 13
  784. # elif HBLKSIZE == 16384
  785. # define CPP_LOG_HBLKSIZE 14
  786. # else
  787. # error fix HBLKSIZE
  788. # endif
  789. # undef HBLKSIZE
  790. #endif
  791. # define CPP_HBLKSIZE (1 << CPP_LOG_HBLKSIZE)
  792. # define LOG_HBLKSIZE ((size_t)CPP_LOG_HBLKSIZE)
  793. # define HBLKSIZE ((size_t)CPP_HBLKSIZE)
  794. #define GC_SQRT_SIZE_MAX ((((size_t)1) << (WORDSZ / 2)) - 1)
  795. /* Max size objects supported by freelist (larger objects are */
  796. /* allocated directly with allchblk(), by rounding to the next */
  797. /* multiple of HBLKSIZE). */
  798. #define CPP_MAXOBJBYTES (CPP_HBLKSIZE/2)
  799. #define MAXOBJBYTES ((size_t)CPP_MAXOBJBYTES)
  800. #define CPP_MAXOBJWORDS BYTES_TO_WORDS(CPP_MAXOBJBYTES)
  801. #define MAXOBJWORDS ((size_t)CPP_MAXOBJWORDS)
  802. #define CPP_MAXOBJGRANULES BYTES_TO_GRANULES(CPP_MAXOBJBYTES)
  803. #define MAXOBJGRANULES ((size_t)CPP_MAXOBJGRANULES)
  804. # define divHBLKSZ(n) ((n) >> LOG_HBLKSIZE)
  805. # define HBLK_PTR_DIFF(p,q) divHBLKSZ((ptr_t)p - (ptr_t)q)
  806. /* Equivalent to subtracting 2 hblk pointers. */
  807. /* We do it this way because a compiler should */
  808. /* find it hard to use an integer division */
  809. /* instead of a shift. The bundled SunOS 4.1 */
  810. /* o.w. sometimes pessimizes the subtraction to */
  811. /* involve a call to .div. */
  812. # define modHBLKSZ(n) ((n) & (HBLKSIZE-1))
  813. # define HBLKPTR(objptr) ((struct hblk *)(((word)(objptr)) \
  814. & ~(word)(HBLKSIZE-1)))
  815. # define HBLKDISPL(objptr) (((size_t) (objptr)) & (HBLKSIZE-1))
  816. /* Round up allocation size (in bytes) to a multiple of a granule. */
  817. #define ROUNDUP_GRANULE_SIZE(lb) /* lb should have no side-effect */ \
  818. (SIZET_SAT_ADD(lb, GRANULE_BYTES - 1) & ~(GRANULE_BYTES - 1))
  819. /* Round up byte allocation requests to integral number of words, etc. */
  820. # define ROUNDED_UP_GRANULES(lb) /* lb should have no side-effect */ \
  821. BYTES_TO_GRANULES(SIZET_SAT_ADD(lb, GRANULE_BYTES - 1 + EXTRA_BYTES))
  822. # if MAX_EXTRA_BYTES == 0
  823. # define SMALL_OBJ(bytes) EXPECT((bytes) <= (MAXOBJBYTES), TRUE)
  824. # else
  825. # define SMALL_OBJ(bytes) \
  826. (EXPECT((bytes) <= (MAXOBJBYTES - MAX_EXTRA_BYTES), TRUE) \
  827. || (bytes) <= MAXOBJBYTES - EXTRA_BYTES)
  828. /* This really just tests bytes <= MAXOBJBYTES - EXTRA_BYTES. */
  829. /* But we try to avoid looking up EXTRA_BYTES. */
  830. # endif
  831. # define ADD_SLOP(lb) /* lb should have no side-effect */ \
  832. SIZET_SAT_ADD(lb, EXTRA_BYTES)
  833. # ifndef MIN_WORDS
  834. # define MIN_WORDS 2 /* FIXME: obsolete */
  835. # endif
  836. /*
  837. * Hash table representation of sets of pages.
  838. * Implements a map from aligned HBLKSIZE chunks of the address space to one
  839. * bit each.
  840. * This assumes it is OK to spuriously set bits, e.g. because multiple
  841. * addresses are represented by a single location.
  842. * Used by black-listing code, and perhaps by dirty bit maintenance code.
  843. */
  844. # ifdef LARGE_CONFIG
  845. # if CPP_WORDSZ == 32
  846. # define LOG_PHT_ENTRIES 20 /* Collisions likely at 1M blocks, */
  847. /* which is >= 4GB. Each table takes */
  848. /* 128KB, some of which may never be */
  849. /* touched. */
  850. # else
  851. # define LOG_PHT_ENTRIES 21 /* Collisions likely at 2M blocks, */
  852. /* which is >= 8GB. Each table takes */
  853. /* 256KB, some of which may never be */
  854. /* touched. */
  855. # endif
  856. # elif !defined(SMALL_CONFIG)
  857. # define LOG_PHT_ENTRIES 18 /* Collisions are likely if heap grows */
  858. /* to more than 256K hblks >= 1GB. */
  859. /* Each hash table occupies 32K bytes. */
  860. /* Even for somewhat smaller heaps, */
  861. /* say half that, collisions may be an */
  862. /* issue because we blacklist */
  863. /* addresses outside the heap. */
  864. # else
  865. # define LOG_PHT_ENTRIES 15 /* Collisions are likely if heap grows */
  866. /* to more than 32K hblks = 128MB. */
  867. /* Each hash table occupies 4K bytes. */
  868. # endif
  869. # define PHT_ENTRIES ((word)1 << LOG_PHT_ENTRIES)
  870. # define PHT_SIZE (PHT_ENTRIES >> LOGWL)
  871. typedef word page_hash_table[PHT_SIZE];
  872. # define PHT_HASH(addr) ((((word)(addr)) >> LOG_HBLKSIZE) & (PHT_ENTRIES - 1))
  873. # define get_pht_entry_from_index(bl, index) \
  874. (((bl)[divWORDSZ(index)] >> modWORDSZ(index)) & 1)
  875. # define set_pht_entry_from_index(bl, index) \
  876. (void)((bl)[divWORDSZ(index)] |= (word)1 << modWORDSZ(index))
  877. #if defined(THREADS) && defined(AO_HAVE_or)
  878. /* And, one more version for GC_add_to_black_list_normal/stack */
  879. /* (invoked indirectly by GC_do_local_mark) and */
  880. /* async_set_pht_entry_from_index (invoked by GC_dirty or the write */
  881. /* fault handler). */
  882. # define set_pht_entry_from_index_concurrent(bl, index) \
  883. AO_or((volatile AO_t *)&(bl)[divWORDSZ(index)], \
  884. (AO_t)((word)1 << modWORDSZ(index)))
  885. #endif
  886. /********************************************/
  887. /* */
  888. /* H e a p B l o c k s */
  889. /* */
  890. /********************************************/
  891. /* heap block header */
  892. #define HBLKMASK (HBLKSIZE-1)
  893. #define MARK_BITS_PER_HBLK (HBLKSIZE/GRANULE_BYTES)
  894. /* upper bound */
  895. /* We allocate 1 bit per allocation granule. */
  896. /* If MARK_BIT_PER_GRANULE is defined, we use */
  897. /* every nth bit, where n is the number of */
  898. /* allocation granules per object. If */
  899. /* MARK_BIT_PER_OBJ is defined, we only use the */
  900. /* initial group of mark bits, and it is safe */
  901. /* to allocate smaller header for large objects. */
  902. union word_ptr_ao_u {
  903. word w;
  904. signed_word sw;
  905. void *vp;
  906. # ifdef PARALLEL_MARK
  907. volatile AO_t ao;
  908. # endif
  909. };
  910. /* We maintain layout maps for heap blocks containing objects of a given */
  911. /* size. Each entry in this map describes a byte offset and has the */
  912. /* following type. */
  913. struct hblkhdr {
  914. struct hblk * hb_next; /* Link field for hblk free list */
  915. /* and for lists of chunks waiting to be */
  916. /* reclaimed. */
  917. struct hblk * hb_prev; /* Backwards link for free list. */
  918. struct hblk * hb_block; /* The corresponding block. */
  919. unsigned char hb_obj_kind;
  920. /* Kind of objects in the block. Each kind */
  921. /* identifies a mark procedure and a set of */
  922. /* list headers. Sometimes called regions. */
  923. unsigned char hb_flags;
  924. # define IGNORE_OFF_PAGE 1 /* Ignore pointers that do not */
  925. /* point to the first page of */
  926. /* this object. */
  927. # define WAS_UNMAPPED 2 /* This is a free block, which has */
  928. /* been unmapped from the address */
  929. /* space. */
  930. /* GC_remap must be invoked on it */
  931. /* before it can be reallocated. */
  932. /* Only set with USE_MUNMAP. */
  933. # define FREE_BLK 4 /* Block is free, i.e. not in use. */
  934. # ifdef ENABLE_DISCLAIM
  935. # define HAS_DISCLAIM 8
  936. /* This kind has a callback on reclaim. */
  937. # define MARK_UNCONDITIONALLY 0x10
  938. /* Mark from all objects, marked or */
  939. /* not. Used to mark objects needed by */
  940. /* reclaim notifier. */
  941. # endif
  942. # ifdef MARK_BIT_PER_GRANULE
  943. # define LARGE_BLOCK 0x20
  944. # endif
  945. unsigned short hb_last_reclaimed;
  946. /* Value of GC_gc_no when block was */
  947. /* last allocated or swept. May wrap. */
  948. /* For a free block, this is maintained */
  949. /* only for USE_MUNMAP, and indicates */
  950. /* when the header was allocated, or */
  951. /* when the size of the block last */
  952. /* changed. */
  953. # ifdef MARK_BIT_PER_OBJ
  954. unsigned32 hb_inv_sz; /* A good upper bound for 2**32/hb_sz. */
  955. /* For large objects, we use */
  956. /* LARGE_INV_SZ. */
  957. # define LARGE_INV_SZ (1 << 16)
  958. # endif
  959. word hb_sz; /* If in use, size in bytes, of objects in the block. */
  960. /* if free, the size in bytes of the whole block. */
  961. /* We assume that this is convertible to signed_word */
  962. /* without generating a negative result. We avoid */
  963. /* generating free blocks larger than that. */
  964. word hb_descr; /* object descriptor for marking. See */
  965. /* gc_mark.h. */
  966. # ifdef MARK_BIT_PER_GRANULE
  967. unsigned short * hb_map; /* Essentially a table of remainders */
  968. /* mod BYTES_TO_GRANULES(hb_sz), except */
  969. /* for large blocks. See GC_obj_map. */
  970. # endif
  971. # ifdef PARALLEL_MARK
  972. volatile AO_t hb_n_marks; /* Number of set mark bits, excluding */
  973. /* the one always set at the end. */
  974. /* Currently it is concurrently */
  975. /* updated and hence only approximate. */
  976. /* But a zero value does guarantee that */
  977. /* the block contains no marked */
  978. /* objects. */
  979. /* Ensuring this property means that we */
  980. /* never decrement it to zero during a */
  981. /* collection, and hence the count may */
  982. /* be one too high. Due to concurrent */
  983. /* updates, an arbitrary number of */
  984. /* increments, but not all of them (!) */
  985. /* may be lost, hence it may in theory */
  986. /* be much too low. */
  987. /* The count may also be too high if */
  988. /* multiple mark threads mark the */
  989. /* same object due to a race. */
  990. # else
  991. size_t hb_n_marks; /* Without parallel marking, the count */
  992. /* is accurate. */
  993. # endif
  994. # ifdef USE_MARK_BYTES
  995. # define MARK_BITS_SZ (MARK_BITS_PER_HBLK + 1)
  996. /* Unlike the other case, this is in units of bytes. */
  997. /* Since we force double-word alignment, we need at most one */
  998. /* mark bit per 2 words. But we do allocate and set one */
  999. /* extra mark bit to avoid an explicit check for the */
  1000. /* partial object at the end of each block. */
  1001. union {
  1002. char _hb_marks[MARK_BITS_SZ];
  1003. /* The i'th byte is 1 if the object */
  1004. /* starting at granule i or object i is */
  1005. /* marked, 0 o.w. */
  1006. /* The mark bit for the "one past the */
  1007. /* end" object is always set to avoid a */
  1008. /* special case test in the marker. */
  1009. word dummy; /* Force word alignment of mark bytes. */
  1010. } _mark_byte_union;
  1011. # define hb_marks _mark_byte_union._hb_marks
  1012. # else
  1013. # define MARK_BITS_SZ (MARK_BITS_PER_HBLK/CPP_WORDSZ + 1)
  1014. word hb_marks[MARK_BITS_SZ];
  1015. # endif /* !USE_MARK_BYTES */
  1016. };
  1017. # define ANY_INDEX 23 /* "Random" mark bit index for assertions */
  1018. /* heap block body */
  1019. # define HBLK_WORDS (HBLKSIZE/sizeof(word))
  1020. # define HBLK_GRANULES (HBLKSIZE/GRANULE_BYTES)
  1021. /* The number of objects in a block dedicated to a certain size. */
  1022. /* may erroneously yield zero (instead of one) for large objects. */
  1023. # define HBLK_OBJS(sz_in_bytes) (HBLKSIZE/(sz_in_bytes))
  1024. struct hblk {
  1025. char hb_body[HBLKSIZE];
  1026. };
  1027. # define HBLK_IS_FREE(hdr) (((hdr) -> hb_flags & FREE_BLK) != 0)
  1028. # define OBJ_SZ_TO_BLOCKS(lb) divHBLKSZ((lb) + HBLKSIZE-1)
  1029. # define OBJ_SZ_TO_BLOCKS_CHECKED(lb) /* lb should have no side-effect */ \
  1030. divHBLKSZ(SIZET_SAT_ADD(lb, HBLKSIZE - 1))
  1031. /* Size of block (in units of HBLKSIZE) needed to hold objects of */
  1032. /* given lb (in bytes). The checked variant prevents wrap around. */
  1033. /* Object free list link */
  1034. # define obj_link(p) (*(void **)(p))
  1035. # define LOG_MAX_MARK_PROCS 6
  1036. # define MAX_MARK_PROCS (1 << LOG_MAX_MARK_PROCS)
  1037. /* Root sets. Logically private to mark_rts.c. But we don't want the */
  1038. /* tables scanned, so we put them here. */
  1039. /* MAX_ROOT_SETS is the maximum number of ranges that can be */
  1040. /* registered as static roots. */
  1041. # ifdef LARGE_CONFIG
  1042. # define MAX_ROOT_SETS 8192
  1043. # elif !defined(SMALL_CONFIG)
  1044. # define MAX_ROOT_SETS 2048
  1045. # else
  1046. # define MAX_ROOT_SETS 512
  1047. # endif
  1048. # define MAX_EXCLUSIONS (MAX_ROOT_SETS/4)
  1049. /* Maximum number of segments that can be excluded from root sets. */
  1050. /*
  1051. * Data structure for excluded static roots.
  1052. */
  1053. struct exclusion {
  1054. ptr_t e_start;
  1055. ptr_t e_end;
  1056. };
  1057. /* Data structure for list of root sets. */
  1058. /* We keep a hash table, so that we can filter out duplicate additions. */
  1059. /* Under Win32, we need to do a better job of filtering overlaps, so */
  1060. /* we resort to sequential search, and pay the price. */
  1061. struct roots {
  1062. ptr_t r_start;/* multiple of word size */
  1063. ptr_t r_end; /* multiple of word size and greater than r_start */
  1064. # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
  1065. struct roots * r_next;
  1066. # endif
  1067. GC_bool r_tmp;
  1068. /* Delete before registering new dynamic libraries */
  1069. };
  1070. #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
  1071. /* Size of hash table index to roots. */
  1072. # define LOG_RT_SIZE 6
  1073. # define RT_SIZE (1 << LOG_RT_SIZE) /* Power of 2, may be != MAX_ROOT_SETS */
  1074. #endif
  1075. #ifndef MAX_HEAP_SECTS
  1076. # ifdef LARGE_CONFIG
  1077. # if CPP_WORDSZ > 32
  1078. # define MAX_HEAP_SECTS 81920
  1079. # else
  1080. # define MAX_HEAP_SECTS 7680
  1081. # endif
  1082. # elif defined(SMALL_CONFIG) && !defined(USE_PROC_FOR_LIBRARIES)
  1083. # if defined(PARALLEL_MARK) && (defined(MSWIN32) || defined(CYGWIN32))
  1084. # define MAX_HEAP_SECTS 384
  1085. # else
  1086. # define MAX_HEAP_SECTS 128 /* Roughly 256MB (128*2048*1K) */
  1087. # endif
  1088. # elif CPP_WORDSZ > 32
  1089. # define MAX_HEAP_SECTS 1024 /* Roughly 8GB */
  1090. # else
  1091. # define MAX_HEAP_SECTS 512 /* Roughly 4GB */
  1092. # endif
  1093. #endif /* !MAX_HEAP_SECTS */
  1094. typedef struct GC_ms_entry {
  1095. ptr_t mse_start; /* First word of object, word aligned. */
  1096. union word_ptr_ao_u mse_descr;
  1097. /* Descriptor; low order two bits are tags, */
  1098. /* as described in gc_mark.h. */
  1099. } mse;
  1100. /* Lists of all heap blocks and free lists */
  1101. /* as well as other random data structures */
  1102. /* that should not be scanned by the */
  1103. /* collector. */
  1104. /* These are grouped together in a struct */
  1105. /* so that they can be easily skipped by the */
  1106. /* GC_mark routine. */
  1107. /* The ordering is weird to make GC_malloc */
  1108. /* faster by keeping the important fields */
  1109. /* sufficiently close together that a */
  1110. /* single load of a base register will do. */
  1111. /* Scalars that could easily appear to */
  1112. /* be pointers are also put here. */
  1113. /* The main fields should precede any */
  1114. /* conditionally included fields, so that */
  1115. /* gc_inl.h will work even if a different set */
  1116. /* of macros is defined when the client is */
  1117. /* compiled. */
  1118. struct _GC_arrays {
  1119. word _heapsize; /* Heap size in bytes (value never goes down). */
  1120. word _requested_heapsize; /* Heap size due to explicit expansion. */
  1121. ptr_t _last_heap_addr;
  1122. ptr_t _prev_heap_addr;
  1123. word _large_free_bytes;
  1124. /* Total bytes contained in blocks on large object free */
  1125. /* list. */
  1126. word _large_allocd_bytes;
  1127. /* Total number of bytes in allocated large objects blocks. */
  1128. /* For the purposes of this counter and the next one only, a */
  1129. /* large object is one that occupies a block of at least */
  1130. /* 2*HBLKSIZE. */
  1131. word _max_large_allocd_bytes;
  1132. /* Maximum number of bytes that were ever allocated in */
  1133. /* large object blocks. This is used to help decide when it */
  1134. /* is safe to split up a large block. */
  1135. word _bytes_allocd_before_gc;
  1136. /* Number of bytes allocated before this */
  1137. /* collection cycle. */
  1138. # ifndef SEPARATE_GLOBALS
  1139. # define GC_bytes_allocd GC_arrays._bytes_allocd
  1140. word _bytes_allocd;
  1141. /* Number of bytes allocated during this collection cycle. */
  1142. # endif
  1143. word _bytes_dropped;
  1144. /* Number of black-listed bytes dropped during GC cycle */
  1145. /* as a result of repeated scanning during allocation */
  1146. /* attempts. These are treated largely as allocated, */
  1147. /* even though they are not useful to the client. */
  1148. word _bytes_finalized;
  1149. /* Approximate number of bytes in objects (and headers) */
  1150. /* that became ready for finalization in the last */
  1151. /* collection. */
  1152. word _bytes_freed;
  1153. /* Number of explicitly deallocated bytes of memory */
  1154. /* since last collection. */
  1155. word _finalizer_bytes_freed;
  1156. /* Bytes of memory explicitly deallocated while */
  1157. /* finalizers were running. Used to approximate memory */
  1158. /* explicitly deallocated by finalizers. */
  1159. ptr_t _scratch_end_ptr;
  1160. ptr_t _scratch_last_end_ptr;
  1161. /* Used by headers.c, and can easily appear to point to */
  1162. /* heap. Also used by GC_register_dynamic_libraries(). */
  1163. mse *_mark_stack;
  1164. /* Limits of stack for GC_mark routine. All ranges */
  1165. /* between GC_mark_stack (incl.) and GC_mark_stack_top */
  1166. /* (incl.) still need to be marked from. */
  1167. mse *_mark_stack_limit;
  1168. # ifdef PARALLEL_MARK
  1169. mse *volatile _mark_stack_top;
  1170. /* Updated only with mark lock held, but read asynchronously. */
  1171. /* TODO: Use union to avoid casts to AO_t */
  1172. # else
  1173. mse *_mark_stack_top;
  1174. # endif
  1175. word _composite_in_use; /* Number of bytes in the accessible */
  1176. /* composite objects. */
  1177. word _atomic_in_use; /* Number of bytes in the accessible */
  1178. /* atomic objects. */
  1179. # ifdef USE_MUNMAP
  1180. # define GC_unmapped_bytes GC_arrays._unmapped_bytes
  1181. word _unmapped_bytes;
  1182. # else
  1183. # define GC_unmapped_bytes 0
  1184. # endif
  1185. bottom_index * _all_nils;
  1186. # ifdef ENABLE_TRACE
  1187. # define GC_trace_addr GC_arrays._trace_addr
  1188. ptr_t _trace_addr;
  1189. # endif
  1190. GC_mark_proc _mark_procs[MAX_MARK_PROCS];
  1191. /* Table of user-defined mark procedures. There is */
  1192. /* a small number of these, which can be referenced */
  1193. /* by DS_PROC mark descriptors. See gc_mark.h. */
  1194. char _modws_valid_offsets[sizeof(word)];
  1195. /* GC_valid_offsets[i] ==> */
  1196. /* GC_modws_valid_offsets[i%sizeof(word)] */
  1197. # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
  1198. # define GC_root_index GC_arrays._root_index
  1199. struct roots * _root_index[RT_SIZE];
  1200. # endif
  1201. # ifdef SAVE_CALL_CHAIN
  1202. # define GC_last_stack GC_arrays._last_stack
  1203. struct callinfo _last_stack[NFRAMES];
  1204. /* Stack at last garbage collection. Useful for */
  1205. /* debugging mysterious object disappearances. In the */
  1206. /* multi-threaded case, we currently only save the */
  1207. /* calling stack. */
  1208. # endif
  1209. # ifndef SEPARATE_GLOBALS
  1210. # define GC_objfreelist GC_arrays._objfreelist
  1211. void *_objfreelist[MAXOBJGRANULES+1];
  1212. /* free list for objects */
  1213. # define GC_aobjfreelist GC_arrays._aobjfreelist
  1214. void *_aobjfreelist[MAXOBJGRANULES+1];
  1215. /* free list for atomic objects */
  1216. # endif
  1217. void *_uobjfreelist[MAXOBJGRANULES+1];
  1218. /* Uncollectible but traced objects. */
  1219. /* Objects on this and _auobjfreelist */
  1220. /* are always marked, except during */
  1221. /* garbage collections. */
  1222. # ifdef GC_ATOMIC_UNCOLLECTABLE
  1223. # define GC_auobjfreelist GC_arrays._auobjfreelist
  1224. void *_auobjfreelist[MAXOBJGRANULES+1];
  1225. /* Atomic uncollectible but traced objects. */
  1226. # endif
  1227. size_t _size_map[MAXOBJBYTES+1];
  1228. /* Number of granules to allocate when asked for a certain */
  1229. /* number of bytes. Should be accessed with the allocation */
  1230. /* lock held. */
  1231. # ifdef MARK_BIT_PER_GRANULE
  1232. # define GC_obj_map GC_arrays._obj_map
  1233. unsigned short * _obj_map[MAXOBJGRANULES + 1];
  1234. /* If not NULL, then a pointer to a map of valid */
  1235. /* object addresses. */
  1236. /* _obj_map[sz_in_granules][i] is */
  1237. /* i % sz_in_granules. */
  1238. /* This is now used purely to replace a */
  1239. /* division in the marker by a table lookup. */
  1240. /* _obj_map[0] is used for large objects and */
  1241. /* contains all nonzero entries. This gets us */
  1242. /* out of the marker fast path without an extra */
  1243. /* test. */
  1244. # define MAP_LEN BYTES_TO_GRANULES(HBLKSIZE)
  1245. # endif
  1246. # define VALID_OFFSET_SZ HBLKSIZE
  1247. char _valid_offsets[VALID_OFFSET_SZ];
  1248. /* GC_valid_offsets[i] == TRUE ==> i */
  1249. /* is registered as a displacement. */
  1250. # if defined(PROC_VDB) || defined(MPROTECT_VDB) \
  1251. || defined(GWW_VDB) || defined(MANUAL_VDB)
  1252. # define GC_grungy_pages GC_arrays._grungy_pages
  1253. page_hash_table _grungy_pages; /* Pages that were dirty at last */
  1254. /* GC_read_dirty. */
  1255. # endif
  1256. # if defined(MPROTECT_VDB) || defined(MANUAL_VDB)
  1257. # define GC_dirty_pages GC_arrays._dirty_pages
  1258. volatile page_hash_table _dirty_pages;
  1259. /* Pages dirtied since last GC_read_dirty. */
  1260. # endif
  1261. # if (defined(CHECKSUMS) && defined(GWW_VDB)) || defined(PROC_VDB)
  1262. # define GC_written_pages GC_arrays._written_pages
  1263. page_hash_table _written_pages; /* Pages ever dirtied */
  1264. # endif
  1265. # define GC_heap_sects GC_arrays._heap_sects
  1266. struct HeapSect {
  1267. ptr_t hs_start;
  1268. size_t hs_bytes;
  1269. } _heap_sects[MAX_HEAP_SECTS]; /* Heap segments potentially */
  1270. /* client objects. */
  1271. # if defined(USE_PROC_FOR_LIBRARIES)
  1272. # define GC_our_memory GC_arrays._our_memory
  1273. struct HeapSect _our_memory[MAX_HEAP_SECTS];
  1274. /* All GET_MEM allocated */
  1275. /* memory. Includes block */
  1276. /* headers and the like. */
  1277. # endif
  1278. # if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
  1279. # define GC_heap_bases GC_arrays._heap_bases
  1280. ptr_t _heap_bases[MAX_HEAP_SECTS];
  1281. /* Start address of memory regions obtained from kernel. */
  1282. # endif
  1283. # ifdef MSWINCE
  1284. # define GC_heap_lengths GC_arrays._heap_lengths
  1285. word _heap_lengths[MAX_HEAP_SECTS];
  1286. /* Committed lengths of memory regions obtained from kernel. */
  1287. # endif
  1288. struct roots _static_roots[MAX_ROOT_SETS];
  1289. struct exclusion _excl_table[MAX_EXCLUSIONS];
  1290. /* Block header index; see gc_headers.h */
  1291. bottom_index * _top_index[TOP_SZ];
  1292. };
  1293. GC_API_PRIV GC_FAR struct _GC_arrays GC_arrays;
  1294. #define GC_all_nils GC_arrays._all_nils
  1295. #define GC_atomic_in_use GC_arrays._atomic_in_use
  1296. #define GC_bytes_allocd_before_gc GC_arrays._bytes_allocd_before_gc
  1297. #define GC_bytes_dropped GC_arrays._bytes_dropped
  1298. #define GC_bytes_finalized GC_arrays._bytes_finalized
  1299. #define GC_bytes_freed GC_arrays._bytes_freed
  1300. #define GC_composite_in_use GC_arrays._composite_in_use
  1301. #define GC_excl_table GC_arrays._excl_table
  1302. #define GC_finalizer_bytes_freed GC_arrays._finalizer_bytes_freed
  1303. #define GC_heapsize GC_arrays._heapsize
  1304. #define GC_large_allocd_bytes GC_arrays._large_allocd_bytes
  1305. #define GC_large_free_bytes GC_arrays._large_free_bytes
  1306. #define GC_last_heap_addr GC_arrays._last_heap_addr
  1307. #define GC_mark_stack GC_arrays._mark_stack
  1308. #define GC_mark_stack_limit GC_arrays._mark_stack_limit
  1309. #define GC_mark_stack_top GC_arrays._mark_stack_top
  1310. #define GC_mark_procs GC_arrays._mark_procs
  1311. #define GC_max_large_allocd_bytes GC_arrays._max_large_allocd_bytes
  1312. #define GC_modws_valid_offsets GC_arrays._modws_valid_offsets
  1313. #define GC_prev_heap_addr GC_arrays._prev_heap_addr
  1314. #define GC_requested_heapsize GC_arrays._requested_heapsize
  1315. #define GC_scratch_end_ptr GC_arrays._scratch_end_ptr
  1316. #define GC_scratch_last_end_ptr GC_arrays._scratch_last_end_ptr
  1317. #define GC_size_map GC_arrays._size_map
  1318. #define GC_static_roots GC_arrays._static_roots
  1319. #define GC_top_index GC_arrays._top_index
  1320. #define GC_uobjfreelist GC_arrays._uobjfreelist
  1321. #define GC_valid_offsets GC_arrays._valid_offsets
  1322. #define beginGC_arrays ((ptr_t)(&GC_arrays))
  1323. #define endGC_arrays (((ptr_t)(&GC_arrays)) + (sizeof GC_arrays))
  1324. #define USED_HEAP_SIZE (GC_heapsize - GC_large_free_bytes)
  1325. /* Object kinds: */
  1326. #ifndef MAXOBJKINDS
  1327. # define MAXOBJKINDS 16
  1328. #endif
  1329. GC_EXTERN struct obj_kind {
  1330. void **ok_freelist; /* Array of free list headers for this kind of */
  1331. /* object. Point either to GC_arrays or to */
  1332. /* storage allocated with GC_scratch_alloc. */
  1333. struct hblk **ok_reclaim_list;
  1334. /* List headers for lists of blocks waiting to */
  1335. /* be swept. Indexed by object size in */
  1336. /* granules. */
  1337. word ok_descriptor; /* Descriptor template for objects in this */
  1338. /* block. */
  1339. GC_bool ok_relocate_descr;
  1340. /* Add object size in bytes to descriptor */
  1341. /* template to obtain descriptor. Otherwise */
  1342. /* template is used as is. */
  1343. GC_bool ok_init; /* Clear objects before putting them on the free list. */
  1344. # ifdef ENABLE_DISCLAIM
  1345. GC_bool ok_mark_unconditionally;
  1346. /* Mark from all, including unmarked, objects */
  1347. /* in block. Used to protect objects reachable */
  1348. /* from reclaim notifiers. */
  1349. int (GC_CALLBACK *ok_disclaim_proc)(void * /*obj*/);
  1350. /* The disclaim procedure is called before obj */
  1351. /* is reclaimed, but must also tolerate being */
  1352. /* called with object from freelist. Non-zero */
  1353. /* exit prevents object from being reclaimed. */
  1354. # define OK_DISCLAIM_INITZ /* comma */, FALSE, 0
  1355. # else
  1356. # define OK_DISCLAIM_INITZ /* empty */
  1357. # endif /* !ENABLE_DISCLAIM */
  1358. } GC_obj_kinds[MAXOBJKINDS];
  1359. #define beginGC_obj_kinds ((ptr_t)(&GC_obj_kinds))
  1360. #define endGC_obj_kinds (beginGC_obj_kinds + (sizeof GC_obj_kinds))
  1361. /* Variables that used to be in GC_arrays, but need to be accessed by */
  1362. /* inline allocation code. If they were in GC_arrays, the inlined */
  1363. /* allocation code would include GC_arrays offsets (as it did), which */
  1364. /* introduce maintenance problems. */
  1365. #ifdef SEPARATE_GLOBALS
  1366. extern word GC_bytes_allocd;
  1367. /* Number of bytes allocated during this collection cycle. */
  1368. extern ptr_t GC_objfreelist[MAXOBJGRANULES+1];
  1369. /* free list for NORMAL objects */
  1370. # define beginGC_objfreelist ((ptr_t)(&GC_objfreelist))
  1371. # define endGC_objfreelist (beginGC_objfreelist + sizeof(GC_objfreelist))
  1372. extern ptr_t GC_aobjfreelist[MAXOBJGRANULES+1];
  1373. /* free list for atomic (PTRFREE) objects */
  1374. # define beginGC_aobjfreelist ((ptr_t)(&GC_aobjfreelist))
  1375. # define endGC_aobjfreelist (beginGC_aobjfreelist + sizeof(GC_aobjfreelist))
  1376. #endif /* SEPARATE_GLOBALS */
  1377. /* Predefined kinds: */
  1378. #define PTRFREE 0
  1379. #define NORMAL 1
  1380. #define UNCOLLECTABLE 2
  1381. #ifdef GC_ATOMIC_UNCOLLECTABLE
  1382. # define AUNCOLLECTABLE 3
  1383. # define IS_UNCOLLECTABLE(k) (((k) & ~1) == UNCOLLECTABLE)
  1384. # define GC_N_KINDS_INITIAL_VALUE 4
  1385. #else
  1386. # define IS_UNCOLLECTABLE(k) ((k) == UNCOLLECTABLE)
  1387. # define GC_N_KINDS_INITIAL_VALUE 3
  1388. #endif
  1389. GC_EXTERN unsigned GC_n_kinds;
  1390. GC_EXTERN word GC_n_heap_sects; /* Number of separately added heap */
  1391. /* sections. */
  1392. #ifdef USE_PROC_FOR_LIBRARIES
  1393. GC_EXTERN word GC_n_memory; /* Number of GET_MEM allocated memory */
  1394. /* sections. */
  1395. #endif
  1396. GC_EXTERN size_t GC_page_size;
  1397. /* Round up allocation size to a multiple of a page size. */
  1398. /* GC_setpagesize() is assumed to be already invoked. */
  1399. #define ROUNDUP_PAGESIZE(lb) /* lb should have no side-effect */ \
  1400. (SIZET_SAT_ADD(lb, GC_page_size - 1) & ~(GC_page_size - 1))
  1401. /* Same as above but used to make GET_MEM() argument safe. */
  1402. #ifdef MMAP_SUPPORTED
  1403. # define ROUNDUP_PAGESIZE_IF_MMAP(lb) ROUNDUP_PAGESIZE(lb)
  1404. #else
  1405. # define ROUNDUP_PAGESIZE_IF_MMAP(lb) (lb)
  1406. #endif
  1407. #if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
  1408. struct _SYSTEM_INFO;
  1409. GC_EXTERN struct _SYSTEM_INFO GC_sysinfo;
  1410. GC_INNER GC_bool GC_is_heap_base(void *p);
  1411. #endif
  1412. GC_EXTERN word GC_black_list_spacing;
  1413. /* Average number of bytes between blacklisted */
  1414. /* blocks. Approximate. */
  1415. /* Counts only blocks that are */
  1416. /* "stack-blacklisted", i.e. that are */
  1417. /* problematic in the interior of an object. */
  1418. #ifdef GC_GCJ_SUPPORT
  1419. extern struct hblk * GC_hblkfreelist[];
  1420. extern word GC_free_bytes[]; /* Both remain visible to GNU GCJ. */
  1421. #endif
  1422. #ifdef GC_DISABLE_INCREMENTAL
  1423. # define GC_incremental FALSE
  1424. /* Hopefully allow optimizer to remove some code. */
  1425. # define TRUE_INCREMENTAL FALSE
  1426. #else
  1427. GC_EXTERN GC_bool GC_incremental;
  1428. /* Using incremental/generational collection. */
  1429. /* Assumes dirty bits are being maintained. */
  1430. # define TRUE_INCREMENTAL \
  1431. (GC_incremental && GC_time_limit != GC_TIME_UNLIMITED)
  1432. /* True incremental, not just generational, mode */
  1433. #endif /* !GC_DISABLE_INCREMENTAL */
  1434. GC_EXTERN word GC_root_size; /* Total size of registered root sections. */
  1435. GC_EXTERN GC_bool GC_debugging_started;
  1436. /* GC_debug_malloc has been called. */
  1437. /* This is used by GC_do_blocking[_inner](). */
  1438. struct blocking_data {
  1439. GC_fn_type fn;
  1440. void * client_data; /* and result */
  1441. };
  1442. /* This is used by GC_call_with_gc_active(), GC_push_all_stack_sections(). */
  1443. struct GC_traced_stack_sect_s {
  1444. ptr_t saved_stack_ptr;
  1445. # ifdef IA64
  1446. ptr_t saved_backing_store_ptr;
  1447. ptr_t backing_store_end;
  1448. # endif
  1449. struct GC_traced_stack_sect_s *prev;
  1450. };
  1451. #ifdef THREADS
  1452. /* Process all "traced stack sections" - scan entire stack except for */
  1453. /* frames belonging to the user functions invoked by GC_do_blocking. */
  1454. GC_INNER void GC_push_all_stack_sections(ptr_t lo, ptr_t hi,
  1455. struct GC_traced_stack_sect_s *traced_stack_sect);
  1456. GC_EXTERN word GC_total_stacksize; /* updated on every push_all_stacks */
  1457. #else
  1458. GC_EXTERN ptr_t GC_blocked_sp;
  1459. GC_EXTERN struct GC_traced_stack_sect_s *GC_traced_stack_sect;
  1460. /* Points to the "frame" data held in stack by */
  1461. /* the innermost GC_call_with_gc_active(). */
  1462. /* NULL if no such "frame" active. */
  1463. #endif /* !THREADS */
  1464. #ifdef IA64
  1465. /* Similar to GC_push_all_stack_sections() but for IA-64 registers store. */
  1466. GC_INNER void GC_push_all_register_sections(ptr_t bs_lo, ptr_t bs_hi,
  1467. int eager, struct GC_traced_stack_sect_s *traced_stack_sect);
  1468. #endif
  1469. /* Marks are in a reserved area in */
  1470. /* each heap block. Each word has one mark bit associated */
  1471. /* with it. Only those corresponding to the beginning of an */
  1472. /* object are used. */
  1473. /* Mark bit operations */
  1474. /*
  1475. * Retrieve, set, clear the nth mark bit in a given heap block.
  1476. *
  1477. * (Recall that bit n corresponds to nth object or allocation granule
  1478. * relative to the beginning of the block, including unused words)
  1479. */
  1480. #ifdef USE_MARK_BYTES
  1481. # define mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[n])
  1482. # define set_mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[n] = 1)
  1483. # define clear_mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[n] = 0)
  1484. #else
  1485. /* Set mark bit correctly, even if mark bits may be concurrently */
  1486. /* accessed. */
  1487. # if defined(PARALLEL_MARK) || (defined(THREAD_SANITIZER) && defined(THREADS))
  1488. /* Workaround TSan false positive: there is no race between */
  1489. /* mark_bit_from_hdr and set_mark_bit_from_hdr when n is different */
  1490. /* (alternatively, USE_MARK_BYTES could be used). If TSan is off, */
  1491. /* AO_or() is used only if we set USE_MARK_BITS explicitly. */
  1492. # define OR_WORD(addr, bits) AO_or((volatile AO_t *)(addr), (AO_t)(bits))
  1493. # else
  1494. # define OR_WORD(addr, bits) (void)(*(addr) |= (bits))
  1495. # endif
  1496. # define mark_bit_from_hdr(hhdr,n) \
  1497. (((hhdr)->hb_marks[divWORDSZ(n)] >> modWORDSZ(n)) & (word)1)
  1498. # define set_mark_bit_from_hdr(hhdr,n) \
  1499. OR_WORD((hhdr)->hb_marks+divWORDSZ(n), (word)1 << modWORDSZ(n))
  1500. # define clear_mark_bit_from_hdr(hhdr,n) \
  1501. ((hhdr)->hb_marks[divWORDSZ(n)] &= ~((word)1 << modWORDSZ(n)))
  1502. #endif /* !USE_MARK_BYTES */
  1503. #ifdef MARK_BIT_PER_OBJ
  1504. # define MARK_BIT_NO(offset, sz) (((word)(offset))/(sz))
  1505. /* Get the mark bit index corresponding to the given byte */
  1506. /* offset and size (in bytes). */
  1507. # define MARK_BIT_OFFSET(sz) 1
  1508. /* Spacing between useful mark bits. */
  1509. # define IF_PER_OBJ(x) x
  1510. # define FINAL_MARK_BIT(sz) ((sz) > MAXOBJBYTES? 1 : HBLK_OBJS(sz))
  1511. /* Position of final, always set, mark bit. */
  1512. #else /* MARK_BIT_PER_GRANULE */
  1513. # define MARK_BIT_NO(offset, sz) BYTES_TO_GRANULES((word)(offset))
  1514. # define MARK_BIT_OFFSET(sz) BYTES_TO_GRANULES(sz)
  1515. # define IF_PER_OBJ(x)
  1516. # define FINAL_MARK_BIT(sz) \
  1517. ((sz) > MAXOBJBYTES ? MARK_BITS_PER_HBLK \
  1518. : BYTES_TO_GRANULES((sz) * HBLK_OBJS(sz)))
  1519. #endif
  1520. /* Important internal collector routines */
  1521. GC_INNER ptr_t GC_approx_sp(void);
  1522. GC_INNER GC_bool GC_should_collect(void);
  1523. void GC_apply_to_all_blocks(void (*fn)(struct hblk *h, word client_data),
  1524. word client_data);
  1525. /* Invoke fn(hbp, client_data) for each */
  1526. /* allocated heap block. */
  1527. GC_INNER struct hblk * GC_next_used_block(struct hblk * h);
  1528. /* Return first in-use block >= h */
  1529. GC_INNER struct hblk * GC_prev_block(struct hblk * h);
  1530. /* Return last block <= h. Returned block */
  1531. /* is managed by GC, but may or may not be in */
  1532. /* use. */
  1533. GC_INNER void GC_mark_init(void);
  1534. GC_INNER void GC_clear_marks(void);
  1535. /* Clear mark bits for all heap objects. */
  1536. GC_INNER void GC_invalidate_mark_state(void);
  1537. /* Tell the marker that marked */
  1538. /* objects may point to unmarked */
  1539. /* ones, and roots may point to */
  1540. /* unmarked objects. Reset mark stack. */
  1541. GC_INNER GC_bool GC_mark_some(ptr_t cold_gc_frame);
  1542. /* Perform about one pages worth of marking */
  1543. /* work of whatever kind is needed. Returns */
  1544. /* quickly if no collection is in progress. */
  1545. /* Return TRUE if mark phase finished. */
  1546. GC_INNER void GC_initiate_gc(void);
  1547. /* initiate collection. */
  1548. /* If the mark state is invalid, this */
  1549. /* becomes full collection. Otherwise */
  1550. /* it's partial. */
  1551. GC_INNER GC_bool GC_collection_in_progress(void);
  1552. /* Collection is in progress, or was abandoned. */
  1553. #define GC_PUSH_ALL_SYM(sym) \
  1554. GC_push_all((/* no volatile */ void *)&(sym), \
  1555. (/* no volatile */ void *)(&(sym) + 1))
  1556. GC_INNER void GC_push_all_stack(ptr_t b, ptr_t t);
  1557. /* As GC_push_all but consider */
  1558. /* interior pointers as valid. */
  1559. #if defined(WRAP_MARK_SOME) && defined(PARALLEL_MARK)
  1560. /* GC_mark_local does not handle memory protection faults yet. So, */
  1561. /* the static data regions are scanned immediately by GC_push_roots. */
  1562. GC_INNER void GC_push_conditional_eager(void *bottom, void *top,
  1563. GC_bool all);
  1564. #endif
  1565. /* In the threads case, we push part of the current thread stack */
  1566. /* with GC_push_all_eager when we push the registers. This gets the */
  1567. /* callee-save registers that may disappear. The remainder of the */
  1568. /* stacks are scheduled for scanning in *GC_push_other_roots, which */
  1569. /* is thread-package-specific. */
  1570. GC_INNER void GC_push_roots(GC_bool all, ptr_t cold_gc_frame);
  1571. /* Push all or dirty roots. */
  1572. GC_API_PRIV GC_push_other_roots_proc GC_push_other_roots;
  1573. /* Push system or application specific roots */
  1574. /* onto the mark stack. In some environments */
  1575. /* (e.g. threads environments) this is */
  1576. /* predefined to be non-zero. A client */
  1577. /* supplied replacement should also call the */
  1578. /* original function. Remains externally */
  1579. /* visible as used by some well-known 3rd-party */
  1580. /* software (e.g., ECL) currently. */
  1581. #ifdef THREADS
  1582. void GC_push_thread_structures(void);
  1583. #endif
  1584. GC_EXTERN void (*GC_push_typed_structures)(void);
  1585. /* A pointer such that we can avoid linking in */
  1586. /* the typed allocation support if unused. */
  1587. GC_INNER void GC_with_callee_saves_pushed(void (*fn)(ptr_t, void *),
  1588. volatile ptr_t arg);
  1589. #if defined(SPARC) || defined(IA64)
  1590. /* Cause all stacked registers to be saved in memory. Return a */
  1591. /* pointer to the top of the corresponding memory stack. */
  1592. ptr_t GC_save_regs_in_stack(void);
  1593. #endif
  1594. /* Push register contents onto mark stack. */
  1595. #if defined(MSWIN32) || defined(MSWINCE)
  1596. void __cdecl GC_push_one(word p);
  1597. #else
  1598. void GC_push_one(word p);
  1599. /* If p points to an object, mark it */
  1600. /* and push contents on the mark stack */
  1601. /* Pointer recognition test always */
  1602. /* accepts interior pointers, i.e. this */
  1603. /* is appropriate for pointers found on */
  1604. /* stack. */
  1605. #endif
  1606. #if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
  1607. GC_INNER void GC_mark_and_push_stack(ptr_t p, ptr_t source);
  1608. /* Ditto, omits plausibility test */
  1609. #else
  1610. GC_INNER void GC_mark_and_push_stack(ptr_t p);
  1611. #endif
  1612. GC_INNER void GC_clear_hdr_marks(hdr * hhdr);
  1613. /* Clear the mark bits in a header */
  1614. GC_INNER void GC_set_hdr_marks(hdr * hhdr);
  1615. /* Set the mark bits in a header */
  1616. GC_INNER void GC_set_fl_marks(ptr_t p);
  1617. /* Set all mark bits associated with */
  1618. /* a free list. */
  1619. #if defined(GC_ASSERTIONS) && defined(THREAD_LOCAL_ALLOC)
  1620. void GC_check_fl_marks(void **);
  1621. /* Check that all mark bits */
  1622. /* associated with a free list are */
  1623. /* set. Abort if not. */
  1624. #endif
  1625. void GC_add_roots_inner(ptr_t b, ptr_t e, GC_bool tmp);
  1626. #ifdef USE_PROC_FOR_LIBRARIES
  1627. GC_INNER void GC_remove_roots_subregion(ptr_t b, ptr_t e);
  1628. #endif
  1629. GC_INNER void GC_exclude_static_roots_inner(void *start, void *finish);
  1630. #if defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \
  1631. || defined(CYGWIN32) || defined(PCR)
  1632. GC_INNER void GC_register_dynamic_libraries(void);
  1633. /* Add dynamic library data sections to the root set. */
  1634. #endif
  1635. GC_INNER void GC_cond_register_dynamic_libraries(void);
  1636. /* Remove and reregister dynamic libraries if we're */
  1637. /* configured to do that at each GC. */
  1638. /* Machine dependent startup routines */
  1639. ptr_t GC_get_main_stack_base(void); /* Cold end of stack. */
  1640. #ifdef IA64
  1641. GC_INNER ptr_t GC_get_register_stack_base(void);
  1642. /* Cold end of register stack. */
  1643. #endif
  1644. void GC_register_data_segments(void);
  1645. #ifdef THREADS
  1646. GC_INNER void GC_thr_init(void);
  1647. GC_INNER void GC_init_parallel(void);
  1648. #else
  1649. GC_INNER GC_bool GC_is_static_root(void *p);
  1650. /* Is the address p in one of the registered static */
  1651. /* root sections? */
  1652. # ifdef TRACE_BUF
  1653. void GC_add_trace_entry(char *kind, word arg1, word arg2);
  1654. # endif
  1655. #endif /* !THREADS */
  1656. /* Black listing: */
  1657. #ifdef PRINT_BLACK_LIST
  1658. GC_INNER void GC_add_to_black_list_normal(word p, ptr_t source);
  1659. /* Register bits as a possible future false */
  1660. /* reference from the heap or static data */
  1661. # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
  1662. if (GC_all_interior_pointers) { \
  1663. GC_add_to_black_list_stack((word)(bits), (source)); \
  1664. } else \
  1665. GC_add_to_black_list_normal((word)(bits), (source))
  1666. GC_INNER void GC_add_to_black_list_stack(word p, ptr_t source);
  1667. # define GC_ADD_TO_BLACK_LIST_STACK(bits, source) \
  1668. GC_add_to_black_list_stack((word)(bits), (source))
  1669. #else
  1670. GC_INNER void GC_add_to_black_list_normal(word p);
  1671. # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
  1672. if (GC_all_interior_pointers) { \
  1673. GC_add_to_black_list_stack((word)(bits)); \
  1674. } else \
  1675. GC_add_to_black_list_normal((word)(bits))
  1676. GC_INNER void GC_add_to_black_list_stack(word p);
  1677. # define GC_ADD_TO_BLACK_LIST_STACK(bits, source) \
  1678. GC_add_to_black_list_stack((word)(bits))
  1679. #endif /* PRINT_BLACK_LIST */
  1680. struct hblk * GC_is_black_listed(struct hblk * h, word len);
  1681. /* If there are likely to be false references */
  1682. /* to a block starting at h of the indicated */
  1683. /* length, then return the next plausible */
  1684. /* starting location for h that might avoid */
  1685. /* these false references. Remains externally */
  1686. /* visible as used by GNU GCJ currently. */
  1687. GC_INNER void GC_promote_black_lists(void);
  1688. /* Declare an end to a black listing phase. */
  1689. GC_INNER void GC_unpromote_black_lists(void);
  1690. /* Approximately undo the effect of the above. */
  1691. /* This actually loses some information, but */
  1692. /* only in a reasonably safe way. */
  1693. GC_INNER ptr_t GC_scratch_alloc(size_t bytes);
  1694. /* GC internal memory allocation for */
  1695. /* small objects. Deallocation is not */
  1696. /* possible. May return NULL. */
  1697. #ifdef GWW_VDB
  1698. /* GC_scratch_recycle_no_gww() not used. */
  1699. #else
  1700. # define GC_scratch_recycle_no_gww GC_scratch_recycle_inner
  1701. #endif
  1702. GC_INNER void GC_scratch_recycle_inner(void *ptr, size_t bytes);
  1703. /* Reuse the memory region by the heap. */
  1704. /* Heap block layout maps: */
  1705. GC_INNER GC_bool GC_add_map_entry(size_t sz);
  1706. /* Add a heap block map for objects of */
  1707. /* size sz to obj_map. */
  1708. /* Return FALSE on failure. */
  1709. GC_INNER void GC_register_displacement_inner(size_t offset);
  1710. /* Version of GC_register_displacement */
  1711. /* that assumes lock is already held. */
  1712. /* hblk allocation: */
  1713. GC_INNER void GC_new_hblk(size_t size_in_granules, int kind);
  1714. /* Allocate a new heap block, and build */
  1715. /* a free list in it. */
  1716. GC_INNER ptr_t GC_build_fl(struct hblk *h, size_t words, GC_bool clear,
  1717. ptr_t list);
  1718. /* Build a free list for objects of */
  1719. /* size sz in block h. Append list to */
  1720. /* end of the free lists. Possibly */
  1721. /* clear objects on the list. Normally */
  1722. /* called by GC_new_hblk, but also */
  1723. /* called explicitly without GC lock. */
  1724. GC_INNER struct hblk * GC_allochblk(size_t size_in_bytes, int kind,
  1725. unsigned flags);
  1726. /* Allocate a heap block, inform */
  1727. /* the marker that block is valid */
  1728. /* for objects of indicated size. */
  1729. GC_INNER ptr_t GC_alloc_large(size_t lb, int k, unsigned flags);
  1730. /* Allocate a large block of size lb bytes. */
  1731. /* The block is not cleared. */
  1732. /* Flags is 0 or IGNORE_OFF_PAGE. */
  1733. /* Calls GC_allchblk to do the actual */
  1734. /* allocation, but also triggers GC and/or */
  1735. /* heap expansion as appropriate. */
  1736. /* Does not update GC_bytes_allocd, but does */
  1737. /* other accounting. */
  1738. GC_INNER void GC_freehblk(struct hblk * p);
  1739. /* Deallocate a heap block and mark it */
  1740. /* as invalid. */
  1741. /* Misc GC: */
  1742. GC_INNER GC_bool GC_expand_hp_inner(word n);
  1743. GC_INNER void GC_start_reclaim(GC_bool abort_if_found);
  1744. /* Restore unmarked objects to free */
  1745. /* lists, or (if abort_if_found is */
  1746. /* TRUE) report them. */
  1747. /* Sweeping of small object pages is */
  1748. /* largely deferred. */
  1749. GC_INNER void GC_continue_reclaim(word sz, int kind);
  1750. /* Sweep pages of the given size and */
  1751. /* kind, as long as possible, and */
  1752. /* as long as the corr. free list is */
  1753. /* empty. Sz is in granules. */
  1754. GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old);
  1755. /* Reclaim all blocks. Abort (in a */
  1756. /* consistent state) if f returns TRUE. */
  1757. GC_INNER ptr_t GC_reclaim_generic(struct hblk * hbp, hdr *hhdr, size_t sz,
  1758. GC_bool init, ptr_t list,
  1759. signed_word *count);
  1760. /* Rebuild free list in hbp with */
  1761. /* header hhdr, with objects of size sz */
  1762. /* bytes. Add list to the end of the */
  1763. /* free list. Add the number of */
  1764. /* reclaimed bytes to *count. */
  1765. GC_INNER GC_bool GC_block_empty(hdr * hhdr);
  1766. /* Block completely unmarked? */
  1767. GC_INNER int GC_CALLBACK GC_never_stop_func(void);
  1768. /* Always returns 0 (FALSE). */
  1769. GC_INNER GC_bool GC_try_to_collect_inner(GC_stop_func f);
  1770. /* Collect; caller must have acquired */
  1771. /* lock. Collection is aborted if f */
  1772. /* returns TRUE. Returns TRUE if it */
  1773. /* completes successfully. */
  1774. #define GC_gcollect_inner() \
  1775. (void)GC_try_to_collect_inner(GC_never_stop_func)
  1776. GC_EXTERN GC_bool GC_is_initialized; /* GC_init() has been run. */
  1777. GC_INNER void GC_collect_a_little_inner(int n);
  1778. /* Do n units worth of garbage */
  1779. /* collection work, if appropriate. */
  1780. /* A unit is an amount appropriate for */
  1781. /* HBLKSIZE bytes of allocation. */
  1782. GC_INNER void * GC_generic_malloc_inner(size_t lb, int k);
  1783. /* Allocate an object of the given */
  1784. /* kind but assuming lock already held. */
  1785. #if defined(DBG_HDRS_ALL) || defined(GC_GCJ_SUPPORT) \
  1786. || !defined(GC_NO_FINALIZATION)
  1787. GC_INNER void * GC_generic_malloc_inner_ignore_off_page(size_t lb, int k);
  1788. /* Allocate an object, where */
  1789. /* the client guarantees that there */
  1790. /* will always be a pointer to the */
  1791. /* beginning of the object while the */
  1792. /* object is live. */
  1793. #endif
  1794. GC_INNER GC_bool GC_collect_or_expand(word needed_blocks,
  1795. GC_bool ignore_off_page, GC_bool retry);
  1796. GC_INNER ptr_t GC_allocobj(size_t sz, int kind);
  1797. /* Make the indicated */
  1798. /* free list nonempty, and return its */
  1799. /* head. Sz is in granules. */
  1800. #ifdef GC_ADD_CALLER
  1801. /* GC_DBG_EXTRAS is used by GC debug API functions (unlike GC_EXTRAS */
  1802. /* used by GC debug API macros) thus GC_RETURN_ADDR_PARENT (pointing */
  1803. /* to client caller) should be used if possible. */
  1804. # ifdef GC_HAVE_RETURN_ADDR_PARENT
  1805. # define GC_DBG_EXTRAS GC_RETURN_ADDR_PARENT, NULL, 0
  1806. # else
  1807. # define GC_DBG_EXTRAS GC_RETURN_ADDR, NULL, 0
  1808. # endif
  1809. #else
  1810. # define GC_DBG_EXTRAS "unknown", 0
  1811. #endif
  1812. #ifdef GC_COLLECT_AT_MALLOC
  1813. extern size_t GC_dbg_collect_at_malloc_min_lb;
  1814. /* variable visible outside for debugging */
  1815. # define GC_DBG_COLLECT_AT_MALLOC(lb) \
  1816. (void)((lb) >= GC_dbg_collect_at_malloc_min_lb ? \
  1817. (GC_gcollect(), 0) : 0)
  1818. #else
  1819. # define GC_DBG_COLLECT_AT_MALLOC(lb) (void)0
  1820. #endif /* !GC_COLLECT_AT_MALLOC */
  1821. /* Allocation routines that bypass the thread local cache. */
  1822. #if defined(THREAD_LOCAL_ALLOC) && defined(GC_GCJ_SUPPORT)
  1823. GC_INNER void * GC_core_gcj_malloc(size_t, void *);
  1824. #endif
  1825. GC_INNER void GC_init_headers(void);
  1826. GC_INNER struct hblkhdr * GC_install_header(struct hblk *h);
  1827. /* Install a header for block h. */
  1828. /* Return 0 on failure, or the header */
  1829. /* otherwise. */
  1830. GC_INNER GC_bool GC_install_counts(struct hblk * h, size_t sz);
  1831. /* Set up forwarding counts for block */
  1832. /* h of size sz. */
  1833. /* Return FALSE on failure. */
  1834. GC_INNER void GC_remove_header(struct hblk * h);
  1835. /* Remove the header for block h. */
  1836. GC_INNER void GC_remove_counts(struct hblk * h, size_t sz);
  1837. /* Remove forwarding counts for h. */
  1838. GC_INNER hdr * GC_find_header(ptr_t h);
  1839. GC_INNER void GC_add_to_heap(struct hblk *p, size_t bytes);
  1840. /* Add a HBLKSIZE aligned chunk to the heap. */
  1841. #ifdef USE_PROC_FOR_LIBRARIES
  1842. GC_INNER void GC_add_to_our_memory(ptr_t p, size_t bytes);
  1843. /* Add a chunk to GC_our_memory. */
  1844. /* If p == 0, do nothing. */
  1845. #else
  1846. # define GC_add_to_our_memory(p, bytes)
  1847. #endif
  1848. GC_INNER void GC_print_all_errors(void);
  1849. /* Print smashed and leaked objects, if any. */
  1850. /* Clear the lists of such objects. */
  1851. GC_EXTERN void (*GC_check_heap)(void);
  1852. /* Check that all objects in the heap with */
  1853. /* debugging info are intact. */
  1854. /* Add any that are not to GC_smashed list. */
  1855. GC_EXTERN void (*GC_print_all_smashed)(void);
  1856. /* Print GC_smashed if it's not empty. */
  1857. /* Clear GC_smashed list. */
  1858. GC_EXTERN void (*GC_print_heap_obj)(ptr_t p);
  1859. /* If possible print (using GC_err_printf) */
  1860. /* a more detailed description (terminated with */
  1861. /* "\n") of the object referred to by p. */
  1862. #if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
  1863. void GC_print_address_map(void);
  1864. /* Print an address map of the process. */
  1865. #endif
  1866. #ifndef SHORT_DBG_HDRS
  1867. GC_EXTERN GC_bool GC_findleak_delay_free;
  1868. /* Do not immediately deallocate object on */
  1869. /* free() in the leak-finding mode, just mark */
  1870. /* it as freed (and deallocate it after GC). */
  1871. GC_INNER GC_bool GC_check_leaked(ptr_t base); /* from dbg_mlc.c */
  1872. #endif
  1873. GC_EXTERN GC_bool GC_have_errors; /* We saw a smashed or leaked object. */
  1874. /* Call error printing routine */
  1875. /* occasionally. It is OK to read it */
  1876. /* without acquiring the lock. */
  1877. #define VERBOSE 2
  1878. #if !defined(NO_CLOCK) || !defined(SMALL_CONFIG)
  1879. /* GC_print_stats should be visible to extra/MacOS.c. */
  1880. extern int GC_print_stats; /* Nonzero generates basic GC log. */
  1881. /* VERBOSE generates add'l messages. */
  1882. #else /* SMALL_CONFIG */
  1883. # define GC_print_stats 0
  1884. /* Will this remove the message character strings from the executable? */
  1885. /* With a particular level of optimizations, it should... */
  1886. #endif
  1887. #ifdef KEEP_BACK_PTRS
  1888. GC_EXTERN long GC_backtraces;
  1889. GC_INNER void GC_generate_random_backtrace_no_gc(void);
  1890. #endif
  1891. #ifdef LINT2
  1892. # define GC_RAND_MAX (~0U >> 1)
  1893. GC_API_PRIV long GC_random(void);
  1894. #endif
  1895. GC_EXTERN GC_bool GC_print_back_height;
  1896. #ifdef MAKE_BACK_GRAPH
  1897. void GC_print_back_graph_stats(void);
  1898. #endif
  1899. #ifdef THREADS
  1900. GC_INNER void GC_free_inner(void * p);
  1901. #endif
  1902. /* Macros used for collector internal allocation. */
  1903. /* These assume the collector lock is held. */
  1904. #ifdef DBG_HDRS_ALL
  1905. GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k);
  1906. GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
  1907. int k);
  1908. # define GC_INTERNAL_MALLOC GC_debug_generic_malloc_inner
  1909. # define GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE \
  1910. GC_debug_generic_malloc_inner_ignore_off_page
  1911. # ifdef THREADS
  1912. GC_INNER void GC_debug_free_inner(void * p);
  1913. # define GC_INTERNAL_FREE GC_debug_free_inner
  1914. # else
  1915. # define GC_INTERNAL_FREE GC_debug_free
  1916. # endif
  1917. #else
  1918. # define GC_INTERNAL_MALLOC GC_generic_malloc_inner
  1919. # define GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE \
  1920. GC_generic_malloc_inner_ignore_off_page
  1921. # ifdef THREADS
  1922. # define GC_INTERNAL_FREE GC_free_inner
  1923. # else
  1924. # define GC_INTERNAL_FREE GC_free
  1925. # endif
  1926. #endif /* !DBG_HDRS_ALL */
  1927. #ifdef USE_MUNMAP
  1928. /* Memory unmapping: */
  1929. GC_INNER void GC_unmap_old(void);
  1930. GC_INNER void GC_merge_unmapped(void);
  1931. GC_INNER void GC_unmap(ptr_t start, size_t bytes);
  1932. GC_INNER void GC_remap(ptr_t start, size_t bytes);
  1933. GC_INNER void GC_unmap_gap(ptr_t start1, size_t bytes1, ptr_t start2,
  1934. size_t bytes2);
  1935. #endif
  1936. #ifdef CAN_HANDLE_FORK
  1937. GC_EXTERN int GC_handle_fork;
  1938. /* Fork-handling mode: */
  1939. /* 0 means no fork handling requested (but client could */
  1940. /* anyway call fork() provided it is surrounded with */
  1941. /* GC_atfork_prepare/parent/child calls); */
  1942. /* -1 means GC tries to use pthread_at_fork if it is */
  1943. /* available (if it succeeds then GC_handle_fork value */
  1944. /* is changed to 1), client should nonetheless surround */
  1945. /* fork() with GC_atfork_prepare/parent/child (for the */
  1946. /* case of pthread_at_fork failure or absence); */
  1947. /* 1 (or other values) means client fully relies on */
  1948. /* pthread_at_fork (so if it is missing or failed then */
  1949. /* abort occurs in GC_init), GC_atfork_prepare and the */
  1950. /* accompanying routines are no-op in such a case. */
  1951. #endif
  1952. #ifndef GC_DISABLE_INCREMENTAL
  1953. /* Virtual dirty bit implementation: */
  1954. /* Each implementation exports the following: */
  1955. GC_INNER void GC_read_dirty(GC_bool output_unneeded);
  1956. /* Retrieve dirty bits. Set output_unneeded to */
  1957. /* indicate that reading of the retrieved dirty */
  1958. /* bits is not planned till the next retrieval. */
  1959. GC_INNER GC_bool GC_page_was_dirty(struct hblk *h);
  1960. /* Read retrieved dirty bits. */
  1961. GC_INNER void GC_remove_protection(struct hblk *h, word nblocks,
  1962. GC_bool pointerfree);
  1963. /* h is about to be written or allocated. Ensure that */
  1964. /* it is not write protected by the virtual dirty bit */
  1965. /* implementation. I.e., this is a call that: */
  1966. /* - hints that [h, h+nblocks) is about to be written; */
  1967. /* - guarantees that protection is removed; */
  1968. /* - may speed up some dirty bit implementations; */
  1969. /* - may be essential if we need to ensure that */
  1970. /* pointer-free system call buffers in the heap are */
  1971. /* not protected. */
  1972. GC_INNER GC_bool GC_dirty_init(void);
  1973. /* Returns true if dirty bits are maintained (otherwise */
  1974. /* it is OK to be called again if the client invokes */
  1975. /* GC_enable_incremental once more). */
  1976. #endif /* !GC_DISABLE_INCREMENTAL */
  1977. #ifdef MANUAL_VDB
  1978. GC_API void GC_dirty_inner(const void *p); /* does not require locking */
  1979. # define GC_dirty(p) (GC_incremental ? GC_dirty_inner(p) : (void)0)
  1980. #else
  1981. # define GC_dirty(p) (void)(p)
  1982. #endif
  1983. /* Same as GC_base but excepts and returns a pointer to const object. */
  1984. #define GC_base_C(p) ((const void *)GC_base((/* no const */ void *)(p)))
  1985. /* Debugging print routines: */
  1986. void GC_print_block_list(void);
  1987. void GC_print_hblkfreelist(void);
  1988. void GC_print_heap_sects(void);
  1989. void GC_print_static_roots(void);
  1990. extern word GC_fo_entries; /* should be visible in extra/MacOS.c */
  1991. #ifdef KEEP_BACK_PTRS
  1992. GC_INNER void GC_store_back_pointer(ptr_t source, ptr_t dest);
  1993. GC_INNER void GC_marked_for_finalization(ptr_t dest);
  1994. # define GC_STORE_BACK_PTR(source, dest) GC_store_back_pointer(source, dest)
  1995. # define GC_MARKED_FOR_FINALIZATION(dest) GC_marked_for_finalization(dest)
  1996. #else
  1997. # define GC_STORE_BACK_PTR(source, dest)
  1998. # define GC_MARKED_FOR_FINALIZATION(dest)
  1999. #endif
  2000. /* Make arguments appear live to compiler */
  2001. void GC_noop6(word, word, word, word, word, word);
  2002. GC_API void GC_CALL GC_noop1(word);
  2003. #ifndef GC_ATTR_FORMAT_PRINTF
  2004. # if GC_GNUC_PREREQ(3, 0)
  2005. # define GC_ATTR_FORMAT_PRINTF(spec_argnum, first_checked) \
  2006. __attribute__((__format__(__printf__, spec_argnum, first_checked)))
  2007. # else
  2008. # define GC_ATTR_FORMAT_PRINTF(spec_argnum, first_checked)
  2009. # endif
  2010. #endif
  2011. /* Logging and diagnostic output: */
  2012. /* GC_printf is used typically on client explicit print requests. */
  2013. /* For all GC_X_printf routines, it is recommended to put "\n" at */
  2014. /* 'format' string end (for output atomicity). */
  2015. GC_API_PRIV void GC_printf(const char * format, ...)
  2016. GC_ATTR_FORMAT_PRINTF(1, 2);
  2017. /* A version of printf that doesn't allocate, */
  2018. /* 1K total output length. */
  2019. /* (We use sprintf. Hopefully that doesn't */
  2020. /* allocate for long arguments.) */
  2021. GC_API_PRIV void GC_err_printf(const char * format, ...)
  2022. GC_ATTR_FORMAT_PRINTF(1, 2);
  2023. /* Basic logging routine. Typically, GC_log_printf is called directly */
  2024. /* only inside various DEBUG_x blocks. */
  2025. GC_API_PRIV void GC_log_printf(const char * format, ...)
  2026. GC_ATTR_FORMAT_PRINTF(1, 2);
  2027. #ifndef GC_ANDROID_LOG
  2028. # define GC_PRINT_STATS_FLAG (GC_print_stats != 0)
  2029. # define GC_INFOLOG_PRINTF GC_COND_LOG_PRINTF
  2030. /* GC_verbose_log_printf is called only if GC_print_stats is VERBOSE. */
  2031. # define GC_verbose_log_printf GC_log_printf
  2032. #else
  2033. extern GC_bool GC_quiet;
  2034. # define GC_PRINT_STATS_FLAG (!GC_quiet)
  2035. /* INFO/DBG loggers are enabled even if GC_print_stats is off. */
  2036. # ifndef GC_INFOLOG_PRINTF
  2037. # define GC_INFOLOG_PRINTF if (GC_quiet) {} else GC_info_log_printf
  2038. # endif
  2039. GC_INNER void GC_info_log_printf(const char *format, ...)
  2040. GC_ATTR_FORMAT_PRINTF(1, 2);
  2041. GC_INNER void GC_verbose_log_printf(const char *format, ...)
  2042. GC_ATTR_FORMAT_PRINTF(1, 2);
  2043. #endif /* GC_ANDROID_LOG */
  2044. /* Convenient macros for GC_[verbose_]log_printf invocation. */
  2045. #define GC_COND_LOG_PRINTF \
  2046. if (EXPECT(!GC_print_stats, TRUE)) {} else GC_log_printf
  2047. #define GC_VERBOSE_LOG_PRINTF \
  2048. if (EXPECT(GC_print_stats != VERBOSE, TRUE)) {} else GC_verbose_log_printf
  2049. #ifndef GC_DBGLOG_PRINTF
  2050. # define GC_DBGLOG_PRINTF if (!GC_PRINT_STATS_FLAG) {} else GC_log_printf
  2051. #endif
  2052. void GC_err_puts(const char *s);
  2053. /* Write s to stderr, don't buffer, don't add */
  2054. /* newlines, don't ... */
  2055. /* Handy macro for logging size values (of word type) in KiB (rounding */
  2056. /* to nearest value). */
  2057. #define TO_KiB_UL(v) ((unsigned long)(((v) + ((1 << 9) - 1)) >> 10))
  2058. GC_EXTERN unsigned GC_fail_count;
  2059. /* How many consecutive GC/expansion failures? */
  2060. /* Reset by GC_allochblk(); defined in alloc.c. */
  2061. GC_EXTERN long GC_large_alloc_warn_interval; /* defined in misc.c */
  2062. GC_EXTERN signed_word GC_bytes_found;
  2063. /* Number of reclaimed bytes after garbage collection; */
  2064. /* protected by GC lock; defined in reclaim.c. */
  2065. #ifndef GC_GET_HEAP_USAGE_NOT_NEEDED
  2066. GC_EXTERN word GC_reclaimed_bytes_before_gc;
  2067. /* Number of bytes reclaimed before this */
  2068. /* collection cycle; used for statistics only. */
  2069. #endif
  2070. #ifdef USE_MUNMAP
  2071. GC_EXTERN int GC_unmap_threshold; /* defined in allchblk.c */
  2072. GC_EXTERN GC_bool GC_force_unmap_on_gcollect; /* defined in misc.c */
  2073. #endif
  2074. #ifdef MSWIN32
  2075. GC_EXTERN GC_bool GC_no_win32_dlls; /* defined in os_dep.c */
  2076. GC_EXTERN GC_bool GC_wnt; /* Is Windows NT derivative; */
  2077. /* defined and set in os_dep.c. */
  2078. #endif
  2079. #ifdef THREADS
  2080. # if defined(MSWIN32) || defined(MSWINCE) || defined(MSWIN_XBOX1)
  2081. GC_EXTERN CRITICAL_SECTION GC_write_cs; /* defined in misc.c */
  2082. # endif
  2083. # if defined(GC_ASSERTIONS) && (defined(MSWIN32) || defined(MSWINCE))
  2084. GC_EXTERN GC_bool GC_write_disabled;
  2085. /* defined in win32_threads.c; */
  2086. /* protected by GC_write_cs. */
  2087. # endif
  2088. # if defined(GC_DISABLE_INCREMENTAL) \
  2089. || defined(set_pht_entry_from_index_concurrent)
  2090. # define GC_acquire_dirty_lock() (void)0
  2091. # define GC_release_dirty_lock() (void)0
  2092. # else
  2093. /* Acquire the spin lock we use to update dirty bits. */
  2094. /* Threads should not get stopped holding it. But we may */
  2095. /* acquire and release it during GC_remove_protection call. */
  2096. # define GC_acquire_dirty_lock() \
  2097. do { /* empty */ \
  2098. } while (AO_test_and_set_acquire(&GC_fault_handler_lock) == AO_TS_SET)
  2099. # define GC_release_dirty_lock() AO_CLEAR(&GC_fault_handler_lock)
  2100. GC_EXTERN volatile AO_TS_t GC_fault_handler_lock;
  2101. /* defined in os_dep.c */
  2102. # endif
  2103. # ifdef MSWINCE
  2104. GC_EXTERN GC_bool GC_dont_query_stack_min;
  2105. /* Defined and set in os_dep.c. */
  2106. # endif
  2107. #elif defined(IA64)
  2108. GC_EXTERN ptr_t GC_save_regs_ret_val; /* defined in mach_dep.c. */
  2109. /* Previously set to backing store pointer. */
  2110. #endif /* !THREADS */
  2111. #ifdef THREAD_LOCAL_ALLOC
  2112. GC_EXTERN GC_bool GC_world_stopped; /* defined in alloc.c */
  2113. GC_INNER void GC_mark_thread_local_free_lists(void);
  2114. #endif
  2115. #ifdef GC_GCJ_SUPPORT
  2116. # ifdef GC_ASSERTIONS
  2117. GC_EXTERN GC_bool GC_gcj_malloc_initialized; /* defined in gcj_mlc.c */
  2118. # endif
  2119. GC_EXTERN ptr_t * GC_gcjobjfreelist;
  2120. #endif
  2121. #ifdef MPROTECT_VDB
  2122. # ifdef GWW_VDB
  2123. GC_INNER GC_bool GC_gww_dirty_init(void);
  2124. /* Returns TRUE if GetWriteWatch is available. */
  2125. /* May be called repeatedly. */
  2126. # endif
  2127. # ifdef USE_MUNMAP
  2128. GC_INNER GC_bool GC_mprotect_dirty_init(void);
  2129. GC_INNER GC_bool GC_has_unmapped_memory(void);
  2130. # endif
  2131. #endif /* MPROTECT_VDB */
  2132. #if defined(CHECKSUMS) || defined(PROC_VDB)
  2133. GC_INNER GC_bool GC_page_was_ever_dirty(struct hblk * h);
  2134. /* Could the page contain valid heap pointers? */
  2135. #endif
  2136. #ifdef CHECKSUMS
  2137. # if defined(MPROTECT_VDB) && !defined(DARWIN)
  2138. void GC_record_fault(struct hblk * h);
  2139. # endif
  2140. void GC_check_dirty(void);
  2141. #endif
  2142. GC_INNER void GC_default_print_heap_obj_proc(ptr_t p);
  2143. GC_INNER void GC_setpagesize(void);
  2144. GC_INNER void GC_initialize_offsets(void); /* defined in obj_map.c */
  2145. GC_INNER void GC_bl_init(void);
  2146. GC_INNER void GC_bl_init_no_interiors(void); /* defined in blacklst.c */
  2147. GC_INNER void GC_start_debugging_inner(void); /* defined in dbg_mlc.c. */
  2148. /* Should not be called if GC_debugging_started. */
  2149. /* Store debugging info into p. Return displaced pointer. */
  2150. /* Assumes we hold the allocation lock. */
  2151. GC_INNER void *GC_store_debug_info_inner(void *p, word sz, const char *str,
  2152. int linenum);
  2153. #ifdef REDIRECT_MALLOC
  2154. # ifdef GC_LINUX_THREADS
  2155. GC_INNER GC_bool GC_text_mapping(char *nm, ptr_t *startp, ptr_t *endp);
  2156. /* from os_dep.c */
  2157. # endif
  2158. #elif defined(USE_WINALLOC)
  2159. GC_INNER void GC_add_current_malloc_heap(void);
  2160. #endif /* !REDIRECT_MALLOC */
  2161. #ifdef MAKE_BACK_GRAPH
  2162. GC_INNER void GC_build_back_graph(void);
  2163. GC_INNER void GC_traverse_back_graph(void);
  2164. #endif
  2165. #ifdef MSWIN32
  2166. GC_INNER void GC_init_win32(void);
  2167. #endif
  2168. #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
  2169. GC_INNER void * GC_roots_present(ptr_t);
  2170. /* The type is a lie, since the real type doesn't make sense here, */
  2171. /* and we only test for NULL. */
  2172. #endif
  2173. #ifdef GC_WIN32_THREADS
  2174. GC_INNER void GC_get_next_stack(char *start, char * limit, char **lo,
  2175. char **hi);
  2176. # ifdef MPROTECT_VDB
  2177. GC_INNER void GC_set_write_fault_handler(void);
  2178. # endif
  2179. # if defined(WRAP_MARK_SOME) && !defined(GC_PTHREADS)
  2180. GC_INNER GC_bool GC_started_thread_while_stopped(void);
  2181. /* Did we invalidate mark phase with an unexpected thread start? */
  2182. # endif
  2183. #endif /* GC_WIN32_THREADS */
  2184. #ifdef THREADS
  2185. GC_INNER void GC_reset_finalizer_nested(void);
  2186. GC_INNER unsigned char *GC_check_finalizer_nested(void);
  2187. GC_INNER void GC_do_blocking_inner(ptr_t data, void * context);
  2188. GC_INNER void GC_push_all_stacks(void);
  2189. # ifdef USE_PROC_FOR_LIBRARIES
  2190. GC_INNER GC_bool GC_segment_is_thread_stack(ptr_t lo, ptr_t hi);
  2191. # endif
  2192. # ifdef IA64
  2193. GC_INNER ptr_t GC_greatest_stack_base_below(ptr_t bound);
  2194. # endif
  2195. #endif /* THREADS */
  2196. #ifdef DYNAMIC_LOADING
  2197. GC_INNER GC_bool GC_register_main_static_data(void);
  2198. # ifdef DARWIN
  2199. GC_INNER void GC_init_dyld(void);
  2200. # endif
  2201. #endif /* DYNAMIC_LOADING */
  2202. #ifdef SEARCH_FOR_DATA_START
  2203. GC_INNER void GC_init_linux_data_start(void);
  2204. ptr_t GC_find_limit(ptr_t, GC_bool);
  2205. #endif
  2206. #if defined(NETBSD) && defined(__ELF__)
  2207. GC_INNER void GC_init_netbsd_elf(void);
  2208. ptr_t GC_find_limit(ptr_t, GC_bool);
  2209. #endif
  2210. #ifdef UNIX_LIKE
  2211. GC_INNER void GC_set_and_save_fault_handler(void (*handler)(int));
  2212. #endif
  2213. #ifdef NEED_PROC_MAPS
  2214. # if defined(DYNAMIC_LOADING) && defined(USE_PROC_FOR_LIBRARIES)
  2215. GC_INNER char *GC_parse_map_entry(char *buf_ptr, ptr_t *start, ptr_t *end,
  2216. char **prot, unsigned int *maj_dev,
  2217. char **mapping_name);
  2218. # endif
  2219. # if defined(IA64) || defined(INCLUDE_LINUX_THREAD_DESCR)
  2220. GC_INNER GC_bool GC_enclosing_mapping(ptr_t addr,
  2221. ptr_t *startp, ptr_t *endp);
  2222. # endif
  2223. GC_INNER char *GC_get_maps(void); /* from os_dep.c */
  2224. #endif /* NEED_PROC_MAPS */
  2225. #ifdef GC_ASSERTIONS
  2226. # define GC_ASSERT(expr) \
  2227. do { \
  2228. if (!(expr)) { \
  2229. GC_err_printf("Assertion failure: %s:%d\n", \
  2230. __FILE__, __LINE__); \
  2231. ABORT("assertion failure"); \
  2232. } \
  2233. } while (0)
  2234. GC_INNER word GC_compute_large_free_bytes(void);
  2235. GC_INNER word GC_compute_root_size(void);
  2236. #else
  2237. # define GC_ASSERT(expr)
  2238. #endif
  2239. /* Check a compile time assertion at compile time. */
  2240. #if defined(static_assert) && (__STDC_VERSION__ >= 201112L)
  2241. # define GC_STATIC_ASSERT(expr) static_assert(expr, "")
  2242. #elif defined(mips) && !defined(__GNUC__)
  2243. /* DOB: MIPSPro C gets an internal error taking the sizeof an array type.
  2244. This code works correctly (ugliness is to avoid "unused var" warnings) */
  2245. # define GC_STATIC_ASSERT(expr) \
  2246. do { if (0) { char j[(expr)? 1 : -1]; j[0]='\0'; j[0]=j[0]; } } while(0)
  2247. #else
  2248. /* The error message for failure is a bit baroque, but ... */
  2249. # define GC_STATIC_ASSERT(expr) (void)sizeof(char[(expr)? 1 : -1])
  2250. #endif
  2251. /* Runtime check for an argument declared as non-null is actually not null. */
  2252. #if GC_GNUC_PREREQ(4, 0)
  2253. /* Workaround tautological-pointer-compare Clang warning. */
  2254. # define NONNULL_ARG_NOT_NULL(arg) (*(volatile void **)&(arg) != NULL)
  2255. #else
  2256. # define NONNULL_ARG_NOT_NULL(arg) (NULL != (arg))
  2257. #endif
  2258. #define COND_DUMP_CHECKS \
  2259. do { \
  2260. GC_ASSERT(GC_compute_large_free_bytes() == GC_large_free_bytes); \
  2261. GC_ASSERT(GC_compute_root_size() == GC_root_size); \
  2262. } while (0)
  2263. #ifndef NO_DEBUGGING
  2264. GC_EXTERN GC_bool GC_dump_regularly;
  2265. /* Generate regular debugging dumps. */
  2266. # define COND_DUMP if (EXPECT(GC_dump_regularly, FALSE)) { \
  2267. GC_dump_named(NULL); \
  2268. } else COND_DUMP_CHECKS
  2269. #else
  2270. # define COND_DUMP COND_DUMP_CHECKS
  2271. #endif
  2272. #if defined(PARALLEL_MARK)
  2273. /* We need additional synchronization facilities from the thread */
  2274. /* support. We believe these are less performance critical */
  2275. /* than the main garbage collector lock; standard pthreads-based */
  2276. /* implementations should be sufficient. */
  2277. # define GC_markers_m1 GC_parallel
  2278. /* Number of mark threads we would like to have */
  2279. /* excluding the initiating thread. */
  2280. /* The mark lock and condition variable. If the GC lock is also */
  2281. /* acquired, the GC lock must be acquired first. The mark lock is */
  2282. /* used to both protect some variables used by the parallel */
  2283. /* marker, and to protect GC_fl_builder_count, below. */
  2284. /* GC_notify_all_marker() is called when */
  2285. /* the state of the parallel marker changes */
  2286. /* in some significant way (see gc_mark.h for details). The */
  2287. /* latter set of events includes incrementing GC_mark_no. */
  2288. /* GC_notify_all_builder() is called when GC_fl_builder_count */
  2289. /* reaches 0. */
  2290. GC_INNER void GC_wait_for_markers_init(void);
  2291. GC_INNER void GC_acquire_mark_lock(void);
  2292. GC_INNER void GC_release_mark_lock(void);
  2293. GC_INNER void GC_notify_all_builder(void);
  2294. GC_INNER void GC_wait_for_reclaim(void);
  2295. GC_EXTERN signed_word GC_fl_builder_count; /* Protected by mark lock. */
  2296. GC_INNER void GC_notify_all_marker(void);
  2297. GC_INNER void GC_wait_marker(void);
  2298. GC_EXTERN word GC_mark_no; /* Protected by mark lock. */
  2299. GC_INNER void GC_help_marker(word my_mark_no);
  2300. /* Try to help out parallel marker for mark cycle */
  2301. /* my_mark_no. Returns if the mark cycle finishes or */
  2302. /* was already done, or there was nothing to do for */
  2303. /* some other reason. */
  2304. GC_INNER void GC_start_mark_threads_inner(void);
  2305. #endif /* PARALLEL_MARK */
  2306. #if defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS) && !defined(NACL) \
  2307. && !defined(GC_DARWIN_THREADS) && !defined(SIG_SUSPEND)
  2308. /* We define the thread suspension signal here, so that we can refer */
  2309. /* to it in the dirty bit implementation, if necessary. Ideally we */
  2310. /* would allocate a (real-time?) signal using the standard mechanism. */
  2311. /* unfortunately, there is no standard mechanism. (There is one */
  2312. /* in Linux glibc, but it's not exported.) Thus we continue to use */
  2313. /* the same hard-coded signals we've always used. */
  2314. # if (defined(GC_LINUX_THREADS) || defined(GC_DGUX386_THREADS)) \
  2315. && !defined(GC_USESIGRT_SIGNALS)
  2316. # if defined(SPARC) && !defined(SIGPWR)
  2317. /* SPARC/Linux doesn't properly define SIGPWR in <signal.h>. */
  2318. /* It is aliased to SIGLOST in asm/signal.h, though. */
  2319. # define SIG_SUSPEND SIGLOST
  2320. # else
  2321. /* Linuxthreads itself uses SIGUSR1 and SIGUSR2. */
  2322. # define SIG_SUSPEND SIGPWR
  2323. # endif
  2324. # elif defined(GC_OPENBSD_THREADS)
  2325. # ifndef GC_OPENBSD_UTHREADS
  2326. # define SIG_SUSPEND SIGXFSZ
  2327. # endif
  2328. # elif defined(_SIGRTMIN) && !defined(CPPCHECK)
  2329. # define SIG_SUSPEND _SIGRTMIN + 6
  2330. # else
  2331. # define SIG_SUSPEND SIGRTMIN + 6
  2332. # endif
  2333. #endif /* GC_PTHREADS && !SIG_SUSPEND */
  2334. #if defined(GC_PTHREADS) && !defined(GC_SEM_INIT_PSHARED)
  2335. # define GC_SEM_INIT_PSHARED 0
  2336. #endif
  2337. /* Some macros for setjmp that works across signal handlers */
  2338. /* were possible, and a couple of routines to facilitate */
  2339. /* catching accesses to bad addresses when that's */
  2340. /* possible/needed. */
  2341. #if (defined(UNIX_LIKE) || (defined(NEED_FIND_LIMIT) && defined(CYGWIN32))) \
  2342. && !defined(GC_NO_SIGSETJMP)
  2343. # if defined(SUNOS5SIGS) && !defined(FREEBSD) && !defined(LINUX)
  2344. EXTERN_C_END
  2345. # include <sys/siginfo.h>
  2346. EXTERN_C_BEGIN
  2347. # endif
  2348. /* Define SETJMP and friends to be the version that restores */
  2349. /* the signal mask. */
  2350. # define SETJMP(env) sigsetjmp(env, 1)
  2351. # define LONGJMP(env, val) siglongjmp(env, val)
  2352. # define JMP_BUF sigjmp_buf
  2353. #else
  2354. # ifdef ECOS
  2355. # define SETJMP(env) hal_setjmp(env)
  2356. # else
  2357. # define SETJMP(env) setjmp(env)
  2358. # endif
  2359. # define LONGJMP(env, val) longjmp(env, val)
  2360. # define JMP_BUF jmp_buf
  2361. #endif /* !UNIX_LIKE || GC_NO_SIGSETJMP */
  2362. /* Do we need the GC_find_limit machinery to find the end of a */
  2363. /* data segment. */
  2364. #if defined(HEURISTIC2) || defined(SEARCH_FOR_DATA_START) \
  2365. || (!defined(STACKBOTTOM) && defined(HEURISTIC2)) \
  2366. || ((defined(SVR4) || defined(AIX) || defined(DGUX) \
  2367. || (defined(LINUX) && defined(SPARC))) && !defined(PCR))
  2368. # define NEED_FIND_LIMIT
  2369. #endif
  2370. #if defined(DATASTART_USES_BSDGETDATASTART)
  2371. EXTERN_C_END
  2372. # include <machine/trap.h>
  2373. EXTERN_C_BEGIN
  2374. # if !defined(PCR)
  2375. # define NEED_FIND_LIMIT
  2376. # endif
  2377. GC_INNER ptr_t GC_FreeBSDGetDataStart(size_t, ptr_t);
  2378. # define DATASTART_IS_FUNC
  2379. #endif /* DATASTART_USES_BSDGETDATASTART */
  2380. #if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__) \
  2381. && !defined(NEED_FIND_LIMIT)
  2382. /* Used by GC_init_netbsd_elf() in os_dep.c. */
  2383. # define NEED_FIND_LIMIT
  2384. #endif
  2385. #if defined(IA64) && !defined(NEED_FIND_LIMIT)
  2386. # define NEED_FIND_LIMIT
  2387. /* May be needed for register backing store base. */
  2388. #endif
  2389. #if defined(NEED_FIND_LIMIT) \
  2390. || (defined(USE_PROC_FOR_LIBRARIES) && defined(THREADS))
  2391. GC_EXTERN JMP_BUF GC_jmp_buf;
  2392. /* Set up a handler for address faults which will longjmp to */
  2393. /* GC_jmp_buf. */
  2394. GC_INNER void GC_setup_temporary_fault_handler(void);
  2395. /* Undo the effect of GC_setup_temporary_fault_handler. */
  2396. GC_INNER void GC_reset_fault_handler(void);
  2397. #endif /* NEED_FIND_LIMIT || USE_PROC_FOR_LIBRARIES */
  2398. /* Some convenience macros for cancellation support. */
  2399. #if defined(CANCEL_SAFE)
  2400. # if defined(GC_ASSERTIONS) \
  2401. && (defined(USE_COMPILER_TLS) \
  2402. || (defined(LINUX) && !defined(ARM32) && GC_GNUC_PREREQ(3, 3) \
  2403. || defined(HPUX) /* and probably others ... */))
  2404. extern __thread unsigned char GC_cancel_disable_count;
  2405. # define NEED_CANCEL_DISABLE_COUNT
  2406. # define INCR_CANCEL_DISABLE() ++GC_cancel_disable_count
  2407. # define DECR_CANCEL_DISABLE() --GC_cancel_disable_count
  2408. # define ASSERT_CANCEL_DISABLED() GC_ASSERT(GC_cancel_disable_count > 0)
  2409. # else
  2410. # define INCR_CANCEL_DISABLE()
  2411. # define DECR_CANCEL_DISABLE()
  2412. # define ASSERT_CANCEL_DISABLED() (void)0
  2413. # endif /* GC_ASSERTIONS & ... */
  2414. # define DISABLE_CANCEL(state) \
  2415. do { pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &state); \
  2416. INCR_CANCEL_DISABLE(); } while (0)
  2417. # define RESTORE_CANCEL(state) \
  2418. do { ASSERT_CANCEL_DISABLED(); \
  2419. pthread_setcancelstate(state, NULL); \
  2420. DECR_CANCEL_DISABLE(); } while (0)
  2421. #else /* !CANCEL_SAFE */
  2422. # define DISABLE_CANCEL(state) (void)0
  2423. # define RESTORE_CANCEL(state) (void)0
  2424. # define ASSERT_CANCEL_DISABLED() (void)0
  2425. #endif /* !CANCEL_SAFE */
  2426. EXTERN_C_END
  2427. #endif /* GC_PRIVATE_H */