gc_config_macros.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
  3. * Copyright (c) 1996 by Silicon Graphics. All rights reserved.
  4. * Copyright (c) 1998 by Fergus Henderson. All rights reserved.
  5. * Copyright (c) 2000-2009 by Hewlett-Packard Development Company.
  6. * All rights reserved.
  7. * Copyright (c) 2008-2020 Ivan Maidanski
  8. *
  9. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  10. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  11. *
  12. * Permission is hereby granted to use or copy this program
  13. * for any purpose, provided the above notices are retained on all copies.
  14. * Permission to modify the code and to distribute modified code is granted,
  15. * provided the above notices are retained, and a notice that the code was
  16. * modified is included with the above copyright notice.
  17. */
  18. /* This should never be included directly; it is included only from gc.h. */
  19. /* We separate it only to make gc.h more suitable as documentation. */
  20. #if defined(GC_H)
  21. /* Convenient internal macro to test version of gcc. */
  22. #if defined(__GNUC__) && defined(__GNUC_MINOR__)
  23. # define GC_GNUC_PREREQ(major, minor) \
  24. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((major) << 16) + (minor))
  25. #else
  26. # define GC_GNUC_PREREQ(major, minor) 0 /* FALSE */
  27. #endif
  28. /* Some tests for old macros. These violate our namespace rules and */
  29. /* will disappear shortly. Use the GC_ names. */
  30. #if defined(SOLARIS_THREADS) || defined(_SOLARIS_THREADS) \
  31. || defined(_SOLARIS_PTHREADS) || defined(GC_SOLARIS_PTHREADS)
  32. /* We no longer support old style Solaris threads. */
  33. /* GC_SOLARIS_THREADS now means pthreads. */
  34. # ifndef GC_SOLARIS_THREADS
  35. # define GC_SOLARIS_THREADS
  36. # endif
  37. #endif
  38. #if defined(IRIX_THREADS)
  39. # define GC_IRIX_THREADS
  40. #endif
  41. #if defined(DGUX_THREADS) && !defined(GC_DGUX386_THREADS)
  42. # define GC_DGUX386_THREADS
  43. #endif
  44. #if defined(AIX_THREADS)
  45. # define GC_AIX_THREADS
  46. #endif
  47. #if defined(HPUX_THREADS)
  48. # define GC_HPUX_THREADS
  49. #endif
  50. #if defined(OSF1_THREADS)
  51. # define GC_OSF1_THREADS
  52. #endif
  53. #if defined(LINUX_THREADS)
  54. # define GC_LINUX_THREADS
  55. #endif
  56. #if defined(WIN32_THREADS)
  57. # define GC_WIN32_THREADS
  58. #endif
  59. #if defined(RTEMS_THREADS)
  60. # define GC_RTEMS_PTHREADS
  61. #endif
  62. #if defined(USE_LD_WRAP)
  63. # define GC_USE_LD_WRAP
  64. #endif
  65. #if defined(GC_WIN32_PTHREADS) && !defined(GC_WIN32_THREADS)
  66. /* Using pthreads-win32 library (or other Win32 implementation). */
  67. # define GC_WIN32_THREADS
  68. #endif
  69. #if defined(GC_AIX_THREADS) || defined(GC_DARWIN_THREADS) \
  70. || defined(GC_DGUX386_THREADS) || defined(GC_FREEBSD_THREADS) \
  71. || defined(GC_HPUX_THREADS) \
  72. || defined(GC_IRIX_THREADS) || defined(GC_LINUX_THREADS) \
  73. || defined(GC_NETBSD_THREADS) || defined(GC_OPENBSD_THREADS) \
  74. || defined(GC_OSF1_THREADS) || defined(GC_SOLARIS_THREADS) \
  75. || defined(GC_WIN32_THREADS) || defined(GC_RTEMS_PTHREADS)
  76. # ifndef GC_THREADS
  77. # define GC_THREADS
  78. # endif
  79. #elif defined(GC_THREADS)
  80. # if defined(__linux__)
  81. # define GC_LINUX_THREADS
  82. # elif defined(__OpenBSD__)
  83. # define GC_OPENBSD_THREADS
  84. # elif defined(_PA_RISC1_1) || defined(_PA_RISC2_0) || defined(hppa) \
  85. || defined(__HPPA) || (defined(__ia64) && defined(_HPUX_SOURCE))
  86. # define GC_HPUX_THREADS
  87. # elif defined(__HAIKU__)
  88. # define GC_HAIKU_THREADS
  89. # elif (defined(__DragonFly__) || defined(__FreeBSD_kernel__) \
  90. || defined(__FreeBSD__)) && !defined(GC_NO_FREEBSD)
  91. # define GC_FREEBSD_THREADS
  92. # elif defined(__NetBSD__)
  93. # define GC_NETBSD_THREADS
  94. # elif defined(__alpha) || defined(__alpha__) /* && !Linux && !xBSD */
  95. # define GC_OSF1_THREADS
  96. # elif (defined(mips) || defined(__mips) || defined(_mips)) \
  97. && !(defined(nec_ews) || defined(_nec_ews) \
  98. || defined(ultrix) || defined(__ultrix))
  99. # define GC_IRIX_THREADS
  100. # elif defined(__sparc) /* && !Linux */ \
  101. || ((defined(sun) || defined(__sun)) \
  102. && (defined(i386) || defined(__i386__) \
  103. || defined(__amd64) || defined(__amd64__)))
  104. # define GC_SOLARIS_THREADS
  105. # elif defined(__APPLE__) && defined(__MACH__)
  106. # define GC_DARWIN_THREADS
  107. # endif
  108. # if defined(DGUX) && (defined(i386) || defined(__i386__))
  109. # define GC_DGUX386_THREADS
  110. # endif
  111. # if defined(_AIX)
  112. # define GC_AIX_THREADS
  113. # endif
  114. # if (defined(_WIN32) || defined(_MSC_VER) || defined(__BORLANDC__) \
  115. || defined(__CYGWIN32__) || defined(__CYGWIN__) || defined(__CEGCC__) \
  116. || defined(_WIN32_WCE) || defined(__MINGW32__)) \
  117. && !defined(GC_WIN32_THREADS)
  118. /* Either posix or native Win32 threads. */
  119. # define GC_WIN32_THREADS
  120. # endif
  121. # if defined(__rtems__) && (defined(i386) || defined(__i386__))
  122. # define GC_RTEMS_PTHREADS
  123. # endif
  124. #endif /* GC_THREADS */
  125. #undef GC_PTHREADS
  126. #if (!defined(GC_WIN32_THREADS) || defined(GC_WIN32_PTHREADS) \
  127. || defined(__CYGWIN32__) || defined(__CYGWIN__)) && defined(GC_THREADS) \
  128. && !defined(NN_PLATFORM_CTR) && !defined(NN_BUILD_TARGET_PLATFORM_NX)
  129. /* Posix threads. */
  130. # define GC_PTHREADS
  131. #endif
  132. #if !defined(_PTHREADS) && defined(GC_NETBSD_THREADS)
  133. # define _PTHREADS
  134. #endif
  135. #if defined(GC_DGUX386_THREADS) && !defined(_POSIX4A_DRAFT10_SOURCE)
  136. # define _POSIX4A_DRAFT10_SOURCE 1
  137. #endif
  138. #if !defined(_REENTRANT) && defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS)
  139. /* Better late than never. This fails if system headers that depend */
  140. /* on this were previously included. */
  141. # define _REENTRANT 1
  142. #endif
  143. #define __GC
  144. #if !defined(_WIN32_WCE) || defined(__GNUC__)
  145. # include <stddef.h>
  146. # if defined(__MINGW32__) && !defined(_WIN32_WCE)
  147. # include <stdint.h>
  148. /* We mention uintptr_t. */
  149. /* Perhaps this should be included in pure msft environments */
  150. /* as well? */
  151. # endif
  152. #else /* _WIN32_WCE */
  153. /* Yet more kludges for WinCE. */
  154. # include <stdlib.h> /* size_t is defined here */
  155. # ifndef _PTRDIFF_T_DEFINED
  156. /* ptrdiff_t is not defined */
  157. # define _PTRDIFF_T_DEFINED
  158. typedef long ptrdiff_t;
  159. # endif
  160. #endif /* _WIN32_WCE */
  161. #if !defined(GC_NOT_DLL) && !defined(GC_DLL) \
  162. && ((defined(_DLL) && !defined(__GNUC__)) \
  163. || (defined(DLL_EXPORT) && defined(GC_BUILD)))
  164. # define GC_DLL
  165. #endif
  166. #if defined(GC_DLL) && !defined(GC_API)
  167. # if defined(__CEGCC__)
  168. # if defined(GC_BUILD)
  169. # define GC_API __declspec(dllexport)
  170. # else
  171. # define GC_API __declspec(dllimport)
  172. # endif
  173. # elif defined(__MINGW32__)
  174. # if defined(__cplusplus) && defined(GC_BUILD)
  175. # define GC_API extern __declspec(dllexport)
  176. # elif defined(GC_BUILD) || defined(__MINGW32_DELAY_LOAD__)
  177. # define GC_API __declspec(dllexport)
  178. # else
  179. # define GC_API extern __declspec(dllimport)
  180. # endif
  181. # elif defined(_MSC_VER) || defined(__DMC__) || defined(__BORLANDC__) \
  182. || defined(__CYGWIN__)
  183. # ifdef GC_BUILD
  184. # define GC_API extern __declspec(dllexport)
  185. # else
  186. # define GC_API __declspec(dllimport)
  187. # endif
  188. # elif defined(__WATCOMC__)
  189. # ifdef GC_BUILD
  190. # define GC_API extern __declspec(dllexport)
  191. # else
  192. # define GC_API extern __declspec(dllimport)
  193. # endif
  194. # elif defined(__SYMBIAN32__)
  195. # ifdef GC_BUILD
  196. # define GC_API extern EXPORT_C
  197. # else
  198. # define GC_API extern IMPORT_C
  199. # endif
  200. # elif defined(__GNUC__)
  201. /* Only matters if used in conjunction with -fvisibility=hidden option. */
  202. # if defined(GC_BUILD) && !defined(GC_NO_VISIBILITY) \
  203. && (GC_GNUC_PREREQ(4, 0) || defined(GC_VISIBILITY_HIDDEN_SET))
  204. # define GC_API extern __attribute__((__visibility__("default")))
  205. # endif
  206. # endif
  207. #endif /* GC_DLL */
  208. #ifndef GC_API
  209. # define GC_API extern
  210. #endif
  211. #ifndef GC_CALL
  212. # define GC_CALL
  213. #endif
  214. #ifndef GC_CALLBACK
  215. # define GC_CALLBACK GC_CALL
  216. #endif
  217. #ifndef GC_ATTR_MALLOC
  218. /* 'malloc' attribute should be used for all malloc-like functions */
  219. /* (to tell the compiler that a function may be treated as if any */
  220. /* non-NULL pointer it returns cannot alias any other pointer valid */
  221. /* when the function returns). If the client code violates this rule */
  222. /* by using custom GC_oom_func then define GC_OOM_FUNC_RETURNS_ALIAS. */
  223. # ifdef GC_OOM_FUNC_RETURNS_ALIAS
  224. # define GC_ATTR_MALLOC /* empty */
  225. # elif GC_GNUC_PREREQ(3, 1)
  226. # define GC_ATTR_MALLOC __attribute__((__malloc__))
  227. # elif defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(__EDG__)
  228. # define GC_ATTR_MALLOC \
  229. __declspec(allocator) __declspec(noalias) __declspec(restrict)
  230. # elif defined(_MSC_VER) && _MSC_VER >= 1400
  231. # define GC_ATTR_MALLOC __declspec(noalias) __declspec(restrict)
  232. # else
  233. # define GC_ATTR_MALLOC
  234. # endif
  235. #endif
  236. #ifndef GC_ATTR_ALLOC_SIZE
  237. /* 'alloc_size' attribute improves __builtin_object_size correctness. */
  238. # undef GC_ATTR_CALLOC_SIZE
  239. # ifdef __clang__
  240. # if __has_attribute(__alloc_size__)
  241. # define GC_ATTR_ALLOC_SIZE(argnum) __attribute__((__alloc_size__(argnum)))
  242. # define GC_ATTR_CALLOC_SIZE(n, s) __attribute__((__alloc_size__(n, s)))
  243. # else
  244. # define GC_ATTR_ALLOC_SIZE(argnum) /* empty */
  245. # endif
  246. # elif GC_GNUC_PREREQ(4, 3) && !defined(__ICC)
  247. # define GC_ATTR_ALLOC_SIZE(argnum) __attribute__((__alloc_size__(argnum)))
  248. # define GC_ATTR_CALLOC_SIZE(n, s) __attribute__((__alloc_size__(n, s)))
  249. # else
  250. # define GC_ATTR_ALLOC_SIZE(argnum) /* empty */
  251. # endif
  252. #endif
  253. #ifndef GC_ATTR_CALLOC_SIZE
  254. # define GC_ATTR_CALLOC_SIZE(n, s) /* empty */
  255. #endif
  256. #ifndef GC_ATTR_NONNULL
  257. # if GC_GNUC_PREREQ(4, 0)
  258. # define GC_ATTR_NONNULL(argnum) __attribute__((__nonnull__(argnum)))
  259. # else
  260. # define GC_ATTR_NONNULL(argnum) /* empty */
  261. # endif
  262. #endif
  263. #ifndef GC_ATTR_CONST
  264. # if GC_GNUC_PREREQ(4, 0)
  265. # define GC_ATTR_CONST __attribute__((__const__))
  266. # else
  267. # define GC_ATTR_CONST /* empty */
  268. # endif
  269. #endif
  270. #ifndef GC_ATTR_DEPRECATED
  271. # ifdef GC_BUILD
  272. # undef GC_ATTR_DEPRECATED
  273. # define GC_ATTR_DEPRECATED /* empty */
  274. # elif GC_GNUC_PREREQ(4, 0)
  275. # define GC_ATTR_DEPRECATED __attribute__((__deprecated__))
  276. # elif defined(_MSC_VER) && _MSC_VER >= 1200
  277. # define GC_ATTR_DEPRECATED __declspec(deprecated)
  278. # else
  279. # define GC_ATTR_DEPRECATED /* empty */
  280. # endif
  281. #endif
  282. #if defined(__sgi) && !defined(__GNUC__) && _COMPILER_VERSION >= 720
  283. # define GC_ADD_CALLER
  284. # define GC_RETURN_ADDR (GC_word)__return_address
  285. #endif
  286. #if defined(__linux__) || defined(__GLIBC__)
  287. # if !defined(__native_client__)
  288. # include <features.h>
  289. # endif
  290. # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1 || __GLIBC__ > 2) \
  291. && !defined(__ia64__) \
  292. && !defined(GC_MISSING_EXECINFO_H) \
  293. && !defined(GC_HAVE_BUILTIN_BACKTRACE)
  294. # define GC_HAVE_BUILTIN_BACKTRACE
  295. # endif
  296. # if defined(__i386__) || defined(__amd64__) || defined(__x86_64__)
  297. # define GC_CAN_SAVE_CALL_STACKS
  298. # endif
  299. #endif /* GLIBC */
  300. #if defined(_MSC_VER) && _MSC_VER >= 1200 /* version 12.0+ (MSVC 6.0+) */ \
  301. && !defined(_M_ARM) && !defined(_M_ARM64) \
  302. && !defined(_AMD64_) && !defined(_M_X64) && !defined(_WIN32_WCE) \
  303. && !defined(GC_HAVE_NO_BUILTIN_BACKTRACE) \
  304. && !defined(GC_HAVE_BUILTIN_BACKTRACE)
  305. # define GC_HAVE_BUILTIN_BACKTRACE
  306. #endif
  307. #if defined(GC_HAVE_BUILTIN_BACKTRACE) && !defined(GC_CAN_SAVE_CALL_STACKS)
  308. # define GC_CAN_SAVE_CALL_STACKS
  309. #endif
  310. #if defined(__sparc__)
  311. # define GC_CAN_SAVE_CALL_STACKS
  312. #endif
  313. /* If we're on a platform on which we can't save call stacks, but */
  314. /* gcc is normally used, we go ahead and define GC_ADD_CALLER. */
  315. /* We make this decision independent of whether gcc is actually being */
  316. /* used, in order to keep the interface consistent, and allow mixing */
  317. /* of compilers. */
  318. /* This may also be desirable if it is possible but expensive to */
  319. /* retrieve the call chain. */
  320. #if (defined(__linux__) || defined(__DragonFly__) || defined(__FreeBSD__) \
  321. || defined(__FreeBSD_kernel__) || defined(__HAIKU__) \
  322. || defined(__NetBSD__) || defined(__OpenBSD__) \
  323. || defined(HOST_ANDROID) || defined(__ANDROID__)) \
  324. && !defined(GC_CAN_SAVE_CALL_STACKS)
  325. # define GC_ADD_CALLER
  326. # if GC_GNUC_PREREQ(2, 95)
  327. /* gcc knows how to retrieve return address, but we don't know */
  328. /* how to generate call stacks. */
  329. # define GC_RETURN_ADDR (GC_word)__builtin_return_address(0)
  330. # if GC_GNUC_PREREQ(4, 0) && (defined(__i386__) || defined(__amd64__) \
  331. || defined(__x86_64__) /* and probably others... */) \
  332. && !defined(GC_NO_RETURN_ADDR_PARENT)
  333. # define GC_HAVE_RETURN_ADDR_PARENT
  334. # define GC_RETURN_ADDR_PARENT \
  335. (GC_word)__builtin_extract_return_addr(__builtin_return_address(1))
  336. /* Note: a compiler might complain that calling */
  337. /* __builtin_return_address with a nonzero argument is unsafe. */
  338. # endif
  339. # else
  340. /* Just pass 0 for gcc compatibility. */
  341. # define GC_RETURN_ADDR 0
  342. # endif
  343. #endif /* !GC_CAN_SAVE_CALL_STACKS */
  344. #ifdef GC_PTHREADS
  345. # if (defined(GC_DARWIN_THREADS) || defined(GC_WIN32_PTHREADS) \
  346. || defined(__native_client__) || defined(GC_RTEMS_PTHREADS)) \
  347. && !defined(GC_NO_DLOPEN)
  348. /* Either there is no dlopen() or we do not need to intercept it. */
  349. # define GC_NO_DLOPEN
  350. # endif
  351. # if (defined(GC_DARWIN_THREADS) || defined(GC_WIN32_PTHREADS) \
  352. || defined(GC_OPENBSD_THREADS) || defined(__native_client__)) \
  353. && !defined(GC_NO_PTHREAD_SIGMASK)
  354. /* Either there is no pthread_sigmask() or no need to intercept it. */
  355. # define GC_NO_PTHREAD_SIGMASK
  356. # endif
  357. # if defined(__native_client__)
  358. /* At present, NaCl pthread_create() prototype does not have */
  359. /* "const" for its "attr" argument; also, NaCl pthread_exit() one */
  360. /* does not have "noreturn" attribute. */
  361. # ifndef GC_PTHREAD_CREATE_CONST
  362. # define GC_PTHREAD_CREATE_CONST /* empty */
  363. # endif
  364. # ifndef GC_HAVE_PTHREAD_EXIT
  365. # define GC_HAVE_PTHREAD_EXIT
  366. # define GC_PTHREAD_EXIT_ATTRIBUTE /* empty */
  367. # endif
  368. # endif
  369. # if !defined(GC_HAVE_PTHREAD_EXIT) \
  370. && !defined(HOST_ANDROID) && !defined(__ANDROID__) \
  371. && (defined(GC_LINUX_THREADS) || defined(GC_SOLARIS_THREADS))
  372. # define GC_HAVE_PTHREAD_EXIT
  373. /* Intercept pthread_exit on Linux and Solaris. */
  374. # if GC_GNUC_PREREQ(2, 7)
  375. # define GC_PTHREAD_EXIT_ATTRIBUTE __attribute__((__noreturn__))
  376. # elif defined(__NORETURN) /* used in Solaris */
  377. # define GC_PTHREAD_EXIT_ATTRIBUTE __NORETURN
  378. # else
  379. # define GC_PTHREAD_EXIT_ATTRIBUTE /* empty */
  380. # endif
  381. # endif
  382. # if (!defined(GC_HAVE_PTHREAD_EXIT) || defined(__native_client__)) \
  383. && !defined(GC_NO_PTHREAD_CANCEL)
  384. /* Either there is no pthread_cancel() or no need to intercept it. */
  385. # define GC_NO_PTHREAD_CANCEL
  386. # endif
  387. #endif /* GC_PTHREADS */
  388. #ifdef __cplusplus
  389. #ifndef GC_ATTR_EXPLICIT
  390. # if __cplusplus >= 201103L && !defined(__clang__) || _MSVC_LANG >= 201103L \
  391. || defined(CPPCHECK)
  392. # define GC_ATTR_EXPLICIT explicit
  393. # else
  394. # define GC_ATTR_EXPLICIT /* empty */
  395. # endif
  396. #endif
  397. #ifndef GC_NOEXCEPT
  398. # if defined(__DMC__) || (defined(__BORLANDC__) \
  399. && (defined(_RWSTD_NO_EXCEPTIONS) || defined(_RWSTD_NO_EX_SPEC))) \
  400. || (defined(_MSC_VER) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS) \
  401. || (defined(__WATCOMC__) && !defined(_CPPUNWIND))
  402. # define GC_NOEXCEPT /* empty */
  403. # ifndef GC_NEW_ABORTS_ON_OOM
  404. # define GC_NEW_ABORTS_ON_OOM
  405. # endif
  406. # elif __cplusplus >= 201103L || _MSVC_LANG >= 201103L
  407. # define GC_NOEXCEPT noexcept
  408. # else
  409. # define GC_NOEXCEPT throw()
  410. # endif
  411. #endif
  412. #endif /* __cplusplus */
  413. #endif