OS.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. // This include file uses various macros and other tricks to auto-detect, define, and
  14. // canonicalize a bunch of macros and types used throughout the ZeroTier core.
  15. #ifndef ZT_OS_HPP
  16. #define ZT_OS_HPP
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #if defined(_WIN32) || defined(_WIN64)
  22. #ifdef _MSC_VER
  23. #pragma warning(disable : 4290)
  24. #pragma warning(disable : 4996)
  25. #pragma warning(disable : 4101)
  26. #endif
  27. #ifndef __WINDOWS__
  28. #define __WINDOWS__ 1
  29. #endif
  30. #ifndef NOMINMAX
  31. #define NOMINMAX
  32. #endif
  33. #undef __UNIX_LIKE__
  34. #undef __BSD__
  35. #include <WinSock2.h>
  36. #include <ws2tcpip.h>
  37. #include <Windows.h>
  38. #include <memoryapi.h>
  39. #include <shlwapi.h>
  40. #include <Shlobj.h>
  41. #include <sys/param.h>
  42. #endif
  43. #ifdef SOCKET
  44. #define ZT_SOCKET SOCKET
  45. #else
  46. #define ZT_SOCKET int
  47. #endif
  48. #ifdef INVALID_SOCKET
  49. #define ZT_INVALID_SOCKET INVALID_SOCKET
  50. #else
  51. #define ZT_INVALID_SOCKET (-1)
  52. #endif
  53. #if !defined(__GNUC__) && (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) || defined(__INTEL_COMPILER) || defined(__clang__))
  54. #define __GNUC__ 3
  55. #endif
  56. #ifdef __cplusplus
  57. #if __cplusplus > 199711L
  58. #include <atomic>
  59. #ifndef __CPP11__
  60. #define __CPP11__
  61. #endif
  62. #endif
  63. #ifndef __CPP11__
  64. // Beyond that defining nullptr, constexpr, and noexcept should allow us to still build on these. So far we've
  65. // avoided deeper C++11 features like lambdas in the core until we're 100% sure all the ancient targets are gone.
  66. #error TODO: to build on pre-c++11 compilers we will need to make a subset of std::atomic for integers
  67. #define nullptr (0)
  68. #define constexpr ZT_INLINE
  69. #define noexcept throw()
  70. #endif
  71. #endif
  72. #ifdef __GNUC__
  73. #ifndef ZT_DEBUG
  74. #ifdef __clang__
  75. #define ZT_INLINE __attribute__((always_inline)) inline
  76. #else
  77. #define ZT_INLINE inline
  78. #endif
  79. #endif
  80. #ifndef restrict
  81. #define restrict __restrict__
  82. #endif
  83. #ifndef likely
  84. #define likely(x) __builtin_expect((x),1)
  85. #endif
  86. #ifndef unlikely
  87. #define unlikely(x) __builtin_expect((x),0)
  88. #endif
  89. #else /* not GCC-like */
  90. #ifndef restrict
  91. #define restrict
  92. #endif
  93. #ifndef likely
  94. #define likely(x) (x)
  95. #endif
  96. #ifndef unlikely
  97. #define unlikely(x) (x)
  98. #endif
  99. #endif
  100. #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  101. #define ZT_ARCH_X64 1
  102. #include <xmmintrin.h>
  103. #include <emmintrin.h>
  104. #include <immintrin.h>
  105. #include <tmmintrin.h>
  106. #include <mmintrin.h>
  107. #endif
  108. #if (defined(__ARM_NEON) || defined(__ARM_NEON__) || defined(ZT_ARCH_ARM_HAS_NEON))
  109. #ifndef ZT_ARCH_ARM_HAS_NEON
  110. #define ZT_ARCH_ARM_HAS_NEON 1
  111. #endif
  112. #include <arm_neon.h>
  113. /*#include <arm_acle.h>*/
  114. #endif
  115. #if defined(ZT_ARCH_X64) || defined(i386) || defined(__i386) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(_M_IX86) || defined(__X86__) || defined(_X86_) || defined(__I86__) || defined(__INTEL__) || defined(__386)
  116. #define ZT_ARCH_X86 1
  117. #endif
  118. #if !defined(ZT_ARCH_X86)
  119. #ifndef ZT_NO_UNALIGNED_ACCESS
  120. #define ZT_NO_UNALIGNED_ACCESS 1
  121. #endif
  122. #endif
  123. #if defined(__SIZEOF_INT128__) || ((defined(ZT_ARCH_X64) || defined(__aarch64__)) && defined(__GNUC__))
  124. #ifdef __SIZEOF_INT128__
  125. #define ZT_HAVE_UINT128 1
  126. typedef unsigned __int128 uint128_t;
  127. #else
  128. #define ZT_HAVE_UINT128 1
  129. typedef unsigned uint128_t __attribute__((mode(TI)));
  130. #endif
  131. #endif
  132. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  133. #ifndef __LINUX__
  134. #define __LINUX__ 1
  135. #endif
  136. #ifndef __UNIX_LIKE__
  137. #define __UNIX_LIKE__ 1
  138. #endif
  139. #include <endian.h>
  140. #endif
  141. #ifdef __APPLE__
  142. #include <TargetConditionals.h>
  143. #include <machine/endian.h>
  144. #ifndef __UNIX_LIKE__
  145. #define __UNIX_LIKE__ 1
  146. #endif
  147. #ifndef __BSD__
  148. #define __BSD__ 1
  149. #endif
  150. #ifndef __BYTE_ORDER
  151. #define __BYTE_ORDER __DARWIN_BYTE_ORDER
  152. #define __BIG_ENDIAN __DARWIN_BIG_ENDIAN
  153. #define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
  154. #endif
  155. #endif
  156. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
  157. #ifndef __UNIX_LIKE__
  158. #define __UNIX_LIKE__ 1
  159. #endif
  160. #ifndef __BSD__
  161. #define __BSD__ 1
  162. #endif
  163. #include <sys/endian.h>
  164. #ifndef RTF_MULTICAST
  165. #define RTF_MULTICAST 0x20000000
  166. #endif
  167. #endif
  168. #ifdef __WINDOWS__
  169. #define ZT_PATH_SEPARATOR '\\'
  170. #define ZT_PATH_SEPARATOR_S "\\"
  171. #define ZT_EOL_S "\r\n"
  172. #else
  173. #define ZT_PATH_SEPARATOR '/'
  174. #define ZT_PATH_SEPARATOR_S "/"
  175. #define ZT_EOL_S "\n"
  176. #endif
  177. #if !defined(__BYTE_ORDER) && defined(__BYTE_ORDER__)
  178. #define __BYTE_ORDER __BYTE_ORDER__
  179. #define __LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
  180. #define __BIG_ENDIAN __ORDER_BIG_ENDIAN__
  181. #endif
  182. #if !defined(__BYTE_ORDER) && defined(BYTE_ORDER)
  183. #define __BYTE_ORDER BYTE_ORDER
  184. #define __LITTLE_ENDIAN LITTLE_ENDIAN
  185. #define __BIG_ENDIAN BIG_ENDIAN
  186. #endif
  187. #if !defined(__BYTE_ORDER) && defined(_BYTE_ORDER)
  188. #define __BYTE_ORDER _BYTE_ORDER
  189. #define __LITTLE_ENDIAN _LITTLE_ENDIAN
  190. #define __BIG_ENDIAN _BIG_ENDIAN
  191. #endif
  192. #ifndef ZT_INLINE
  193. #ifdef ZT_DEBUG
  194. #define ZT_INLINE
  195. #else
  196. #define ZT_INLINE inline
  197. #endif
  198. #endif
  199. // Macro to print very verbose tracing information to standard error.
  200. #define ZT_VA_ARGS(...) , ##__VA_ARGS__
  201. #define ZT_DEBUG_SPEW
  202. #ifdef ZT_DEBUG_SPEW
  203. #define ZT_SPEW(f,...) fprintf(stderr,"%s:%d(%s): " f ZT_EOL_S,__FILE__,__LINE__,__FUNCTION__ ZT_VA_ARGS(__VA_ARGS__))
  204. #else
  205. #define ZT_SPEW(f,...)
  206. #endif
  207. #endif