features.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*************************************************************************
  2. *
  3. * Copyright 2016 Realm Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. **************************************************************************/
  18. #ifndef REALM_UTIL_FEATURES_H
  19. #define REALM_UTIL_FEATURES_H
  20. #ifdef _MSC_VER
  21. #pragma warning(disable : 4800) // Visual Studio int->bool performance warnings
  22. #endif
  23. #if defined(_WIN32) && !defined(NOMINMAX)
  24. #define NOMINMAX
  25. #endif
  26. #ifndef REALM_NO_CONFIG
  27. #include <realm/util/config.h>
  28. #endif
  29. /* The maximum number of elements in a B+-tree node. Applies to inner nodes and
  30. * to leaves. The minimum allowable value is 2.
  31. */
  32. #ifndef REALM_MAX_BPNODE_SIZE
  33. #define REALM_MAX_BPNODE_SIZE 1000
  34. #endif
  35. #define REALM_QUOTE_2(x) #x
  36. #define REALM_QUOTE(x) REALM_QUOTE_2(x)
  37. /* See these links for information about feature check macroes in GCC,
  38. * Clang, and MSVC:
  39. *
  40. * http://gcc.gnu.org/projects/cxx0x.html
  41. * http://clang.llvm.org/cxx_status.html
  42. * http://clang.llvm.org/docs/LanguageExtensions.html#checks-for-standard-language-features
  43. * http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx
  44. * http://sourceforge.net/p/predef/wiki/Compilers
  45. */
  46. /* Compiler is GCC and version is greater than or equal to the specified version */
  47. #define REALM_HAVE_AT_LEAST_GCC(maj, min) \
  48. (__GNUC__ > (maj) || __GNUC__ == (maj) && __GNUC_MINOR__ >= (min))
  49. #if defined(__clang__)
  50. #define REALM_HAVE_CLANG_FEATURE(feature) __has_feature(feature)
  51. #define REALM_HAVE_CLANG_WARNING(warning) __has_warning(warning)
  52. #else
  53. #define REALM_HAVE_CLANG_FEATURE(feature) 0
  54. #define REALM_HAVE_CLANG_WARNING(warning) 0
  55. #endif
  56. #ifdef __has_cpp_attribute
  57. #define REALM_HAS_CPP_ATTRIBUTE(attr) __has_cpp_attribute(attr)
  58. #else
  59. #define REALM_HAS_CPP_ATTRIBUTE(attr) 0
  60. #endif
  61. #if REALM_HAS_CPP_ATTRIBUTE(clang::fallthrough)
  62. #define REALM_FALLTHROUGH [[clang::fallthrough]]
  63. #elif REALM_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
  64. #define REALM_FALLTHROUGH [[gnu::fallthrough]]
  65. #elif REALM_HAS_CPP_ATTRIBUTE(fallthrough)
  66. #define REALM_FALLTHROUGH [[fallthrough]]
  67. #else
  68. #define REALM_FALLTHROUGH
  69. #endif
  70. // This should be renamed to REALM_UNREACHABLE as soon as REALM_UNREACHABLE is renamed to
  71. // REALM_ASSERT_NOT_REACHED which will better reflect its nature
  72. #if defined(__GNUC__) || defined(__clang__)
  73. #define REALM_COMPILER_HINT_UNREACHABLE __builtin_unreachable
  74. #else
  75. #define REALM_COMPILER_HINT_UNREACHABLE abort
  76. #endif
  77. #if defined(__GNUC__) // clang or GCC
  78. #define REALM_PRAGMA(v) _Pragma(REALM_QUOTE_2(v))
  79. #elif defined(_MSC_VER) // VS
  80. #define REALM_PRAGMA(v) __pragma(v)
  81. #else
  82. #define REALM_PRAGMA(v)
  83. #endif
  84. #if defined(__clang__)
  85. #define REALM_DIAG(v) REALM_PRAGMA(clang diagnostic v)
  86. #elif defined(__GNUC__)
  87. #define REALM_DIAG(v) REALM_PRAGMA(GCC diagnostic v)
  88. #else
  89. #define REALM_DIAG(v)
  90. #endif
  91. #define REALM_DIAG_PUSH() REALM_DIAG(push)
  92. #define REALM_DIAG_POP() REALM_DIAG(pop)
  93. #ifdef _MSC_VER
  94. #define REALM_VS_WARNING_DISABLE #pragma warning (default: 4297)
  95. #endif
  96. #if REALM_HAVE_CLANG_WARNING("-Wtautological-compare") || REALM_HAVE_AT_LEAST_GCC(6, 0)
  97. #define REALM_DIAG_IGNORE_TAUTOLOGICAL_COMPARE() REALM_DIAG(ignored "-Wtautological-compare")
  98. #else
  99. #define REALM_DIAG_IGNORE_TAUTOLOGICAL_COMPARE()
  100. #endif
  101. #ifdef _MSC_VER
  102. # define REALM_DIAG_IGNORE_UNSIGNED_MINUS() REALM_PRAGMA(warning(disable:4146))
  103. #else
  104. #define REALM_DIAG_IGNORE_UNSIGNED_MINUS()
  105. #endif
  106. /* The way to specify that a function never returns. */
  107. #if REALM_HAVE_AT_LEAST_GCC(4, 8) || REALM_HAVE_CLANG_FEATURE(cxx_attributes)
  108. #define REALM_NORETURN [[noreturn]]
  109. #elif __GNUC__
  110. #define REALM_NORETURN __attribute__((noreturn))
  111. #elif defined(_MSC_VER)
  112. #define REALM_NORETURN __declspec(noreturn)
  113. #else
  114. #define REALM_NORETURN
  115. #endif
  116. /* The way to specify that a variable or type is intended to possibly
  117. * not be used. Use it to suppress a warning from the compiler. */
  118. #if __GNUC__
  119. #define REALM_UNUSED __attribute__((unused))
  120. #else
  121. #define REALM_UNUSED
  122. #endif
  123. /* The way to specify that a function is deprecated
  124. * not be used. Use it to suppress a warning from the compiler. */
  125. #if __GNUC__
  126. #define REALM_DEPRECATED(x) [[deprecated(x)]]
  127. #else
  128. #define REALM_DEPRECATED(x) __declspec(deprecated(x))
  129. #endif
  130. #if __GNUC__ || defined __INTEL_COMPILER
  131. #define REALM_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  132. #define REALM_LIKELY(expr) __builtin_expect(!!(expr), 1)
  133. #else
  134. #define REALM_UNLIKELY(expr) (expr)
  135. #define REALM_LIKELY(expr) (expr)
  136. #endif
  137. #if defined(__GNUC__) || defined(__HP_aCC)
  138. #define REALM_FORCEINLINE inline __attribute__((always_inline))
  139. #elif defined(_MSC_VER)
  140. #define REALM_FORCEINLINE __forceinline
  141. #else
  142. #define REALM_FORCEINLINE inline
  143. #endif
  144. #if REALM_HAS_CPP_ATTRIBUTE(gnu::cold)
  145. #define REALM_COLD [[gnu::cold]]
  146. #else
  147. #define REALM_COLD
  148. #endif
  149. #if REALM_HAS_CPP_ATTRIBUTE(gnu::noinline)
  150. #define REALM_NOINLINE [[gnu::noinline]]
  151. #elif defined(__GNUC__) || defined(__HP_aCC)
  152. #define REALM_NOINLINE __attribute__((noinline))
  153. #elif defined(_MSC_VER)
  154. #define REALM_NOINLINE __declspec(noinline)
  155. #else
  156. #define REALM_NOINLINE
  157. #endif
  158. #if REALM_HAS_CPP_ATTRIBUTE(nodiscard)
  159. #define REALM_NODISCARD [[nodiscard]]
  160. #else
  161. #if defined(__GNUC__) || defined(__HP_aCC)
  162. #define REALM_NODISCARD __attribute__((warn_unused_result))
  163. #elif defined(_MSC_VER)
  164. #define REALM_NODISCARD _Check_return_
  165. #else
  166. #define REALM_NODISCARD
  167. #endif
  168. #endif
  169. /* Thread specific data (only for POD types) */
  170. #if defined __clang__
  171. #define REALM_THREAD_LOCAL __thread
  172. #else
  173. #define REALM_THREAD_LOCAL thread_local
  174. #endif
  175. #if defined ANDROID || defined __ANDROID_API__
  176. #define REALM_ANDROID 1
  177. #else
  178. #define REALM_ANDROID 0
  179. #endif
  180. #if defined _WIN32
  181. #include <winapifamily.h>
  182. #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
  183. #define REALM_WINDOWS 1
  184. #define REALM_UWP 0
  185. #elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
  186. #define REALM_WINDOWS 0
  187. #define REALM_UWP 1
  188. #endif
  189. #else
  190. #define REALM_WINDOWS 0
  191. #define REALM_UWP 0
  192. #endif
  193. // Some documentation of the defines provided by Apple:
  194. // http://developer.apple.com/library/mac/documentation/Porting/Conceptual/PortingUnix/compiling/compiling.html#//apple_ref/doc/uid/TP40002850-SW13
  195. #if defined __APPLE__ && defined __MACH__
  196. #define REALM_PLATFORM_APPLE 1
  197. /* Apple OSX and iOS (Darwin). */
  198. #include <Availability.h>
  199. #include <TargetConditionals.h>
  200. #if TARGET_OS_IPHONE == 1 && TARGET_OS_IOS == 1
  201. /* Device (iPhone or iPad) or simulator. */
  202. #define REALM_IOS 1
  203. #define REALM_APPLE_DEVICE !TARGET_OS_SIMULATOR
  204. #define REALM_MACCATALYST TARGET_OS_MACCATALYST
  205. #else
  206. #define REALM_IOS 0
  207. #define REALM_MACCATALYST 0
  208. #endif
  209. #if TARGET_OS_WATCH == 1
  210. /* Device (Apple Watch) or simulator. */
  211. #define REALM_WATCHOS 1
  212. #define REALM_APPLE_DEVICE !TARGET_OS_SIMULATOR
  213. #else
  214. #define REALM_WATCHOS 0
  215. #endif
  216. #if TARGET_OS_TV
  217. /* Device (Apple TV) or simulator. */
  218. #define REALM_TVOS 1
  219. #define REALM_APPLE_DEVICE !TARGET_OS_SIMULATOR
  220. #else
  221. #define REALM_TVOS 0
  222. #endif
  223. #else
  224. #define REALM_PLATFORM_APPLE 0
  225. #define REALM_MACCATALYST 0
  226. #define REALM_IOS 0
  227. #define REALM_WATCHOS 0
  228. #define REALM_TVOS 0
  229. #endif
  230. #ifndef REALM_APPLE_DEVICE
  231. #define REALM_APPLE_DEVICE 0
  232. #endif
  233. #if REALM_ANDROID || REALM_IOS || REALM_WATCHOS || REALM_TVOS || REALM_UWP
  234. #define REALM_MOBILE 1
  235. #else
  236. #define REALM_MOBILE 0
  237. #endif
  238. #if defined(REALM_DEBUG) && !defined(REALM_COOKIE_CHECK)
  239. #define REALM_COOKIE_CHECK
  240. #endif
  241. // We're in i686 mode
  242. #if defined(__i386) || defined(__i386__) || defined(__i686__) || defined(_M_I86) || defined(_M_IX86)
  243. #define REALM_ARCHITECTURE_X86_32 1
  244. #else
  245. #define REALM_ARCHITECTURE_X86_32 0
  246. #endif
  247. // We're in amd64 mode
  248. #if defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
  249. defined(_M_AMD64)
  250. #define REALM_ARCHITECTURE_X86_64 1
  251. #else
  252. #define REALM_ARCHITECTURE_X86_64 0
  253. #endif
  254. #if defined(__arm__) || defined(_M_ARM)
  255. #define REALM_ARCHITECTURE_ARM32 1
  256. #else
  257. #define REALM_ARCHITECTURE_ARM32 0
  258. #endif
  259. #if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
  260. #define REALM_ARCHITECTURE_ARM64 1
  261. #else
  262. #define REALM_ARCHITECTURE_ARM64 0
  263. #endif
  264. // Address Sanitizer
  265. #if defined(__has_feature) // Clang
  266. # if __has_feature(address_sanitizer)
  267. # define REALM_SANITIZE_ADDRESS 1
  268. # else
  269. # define REALM_SANITIZE_ADDRESS 0
  270. # endif
  271. #elif defined(__SANITIZE_ADDRESS__) && __SANITIZE_ADDRESS__ // GCC
  272. # define REALM_SANITIZE_ADDRESS 1
  273. #else
  274. # define REALM_SANITIZE_ADDRESS 0
  275. #endif
  276. // Thread Sanitizer
  277. #if defined(__has_feature) // Clang
  278. # if __has_feature(thread_sanitizer)
  279. # define REALM_SANITIZE_THREAD 1
  280. # else
  281. # define REALM_SANITIZE_THREAD 0
  282. # endif
  283. #elif defined(__SANITIZE_THREAD__) && __SANITIZE_THREAD__ // GCC
  284. # define REALM_SANITIZE_THREAD 1
  285. #else
  286. # define REALM_SANITIZE_THREAD 0
  287. #endif
  288. #endif /* REALM_UTIL_FEATURES_H */