2
0

OS.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #ifndef ZT_OS_HPP
  14. #define ZT_OS_HPP
  15. /* Uncomment this to force a whole lot of debug output. */
  16. #define ZT_DEBUG_SPEW
  17. #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__))
  18. #define __GNUC__ 3
  19. #endif
  20. #if defined(_WIN32) || defined(_WIN64)
  21. #ifndef _WIN32_WINNT
  22. #define _WIN32_WINNT 0x06010000
  23. #endif
  24. #ifdef _MSC_VER
  25. #pragma warning(disable : 4290)
  26. #pragma warning(disable : 4996)
  27. #pragma warning(disable : 4101)
  28. #endif
  29. #ifndef __WINDOWS__
  30. #define __WINDOWS__ 1
  31. #endif
  32. #ifndef NOMINMAX
  33. #define NOMINMAX
  34. #endif
  35. #ifdef __UNIX_LIKE__
  36. #undef __UNIX_LIKE__
  37. #endif
  38. #ifdef __BSD__
  39. #undef __BSD__
  40. #endif
  41. #include <WinSock2.h>
  42. #include <ws2tcpip.h>
  43. #include <Windows.h>
  44. #include <memoryapi.h>
  45. #include <shlwapi.h>
  46. #include <Shlobj.h>
  47. #include <sys/param.h>
  48. #endif /* Microsoft Windows */
  49. #include <stdint.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <stdio.h>
  53. #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  54. #define ZT_ARCH_X64 1
  55. #include <xmmintrin.h>
  56. #include <emmintrin.h>
  57. #include <immintrin.h>
  58. #endif
  59. #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)
  60. #define ZT_ARCH_X86 1
  61. #endif
  62. #if !defined(ZT_ARCH_X86)
  63. #ifndef ZT_NO_UNALIGNED_ACCESS
  64. #define ZT_NO_UNALIGNED_ACCESS 1
  65. #endif
  66. #endif
  67. #if defined(__ARM_NEON) || defined(__ARM_NEON__) || defined(ZT_ARCH_ARM_HAS_NEON)
  68. #if (defined(__APPLE__) && !defined(__LP64__)) || (defined(__ANDROID__) && defined(__arm__))
  69. #ifdef ZT_ARCH_ARM_HAS_NEON
  70. #undef ZT_ARCH_ARM_HAS_NEON
  71. #endif
  72. #else
  73. #ifndef ZT_ARCH_ARM_HAS_NEON
  74. #define ZT_ARCH_ARM_HAS_NEON 1
  75. #endif
  76. #endif
  77. #include <arm_neon.h>
  78. /*#include <arm_acle.h>*/
  79. #endif
  80. #ifdef __APPLE__
  81. #include <TargetConditionals.h>
  82. #include <machine/endian.h>
  83. #ifndef __UNIX_LIKE__
  84. #define __UNIX_LIKE__ 1
  85. #endif
  86. #ifndef __BSD__
  87. #define __BSD__ 1
  88. #endif
  89. #ifndef __BYTE_ORDER
  90. #define __BYTE_ORDER __DARWIN_BYTE_ORDER
  91. #define __BIG_ENDIAN __DARWIN_BIG_ENDIAN
  92. #define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
  93. #endif
  94. #endif
  95. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  96. #ifndef __LINUX__
  97. #define __LINUX__ 1
  98. #endif
  99. #ifndef __UNIX_LIKE__
  100. #define __UNIX_LIKE__ 1
  101. #endif
  102. #include <endian.h>
  103. #endif
  104. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
  105. #ifndef __UNIX_LIKE__
  106. #define __UNIX_LIKE__ 1
  107. #endif
  108. #ifndef __BSD__
  109. #define __BSD__ 1
  110. #endif
  111. #include <sys/endian.h>
  112. #ifndef RTF_MULTICAST
  113. #define RTF_MULTICAST 0x20000000
  114. #endif
  115. #endif
  116. #ifdef __WINDOWS__
  117. #define ZT_PATH_SEPARATOR '\\'
  118. #define ZT_PATH_SEPARATOR_S "\\"
  119. #define ZT_EOL_S "\r\n"
  120. #else
  121. #define ZT_PATH_SEPARATOR '/'
  122. #define ZT_PATH_SEPARATOR_S "/"
  123. #define ZT_EOL_S "\n"
  124. #endif
  125. #ifdef SOCKET
  126. #define ZT_SOCKET SOCKET
  127. #else
  128. #define ZT_SOCKET int
  129. #endif
  130. #ifdef INVALID_SOCKET
  131. #define ZT_INVALID_SOCKET INVALID_SOCKET
  132. #else
  133. #define ZT_INVALID_SOCKET (-1)
  134. #endif
  135. #ifdef __cplusplus
  136. #if __cplusplus > 199711L
  137. #include <atomic>
  138. #ifndef __CPP11__
  139. #define __CPP11__ 1
  140. #endif
  141. #endif
  142. /* Right now we fail if no C++11. The core could be ported to old C++ compilers
  143. * if a shim for <atomic> were included. */
  144. #ifndef __CPP11__
  145. #error TODO: to build on pre-c++11 compilers we will need to make a subset of std::atomic for integers
  146. #define nullptr (0)
  147. #define constexpr ZT_INLINE
  148. #define noexcept throw()
  149. #endif
  150. #endif
  151. #ifndef ZT_INLINE
  152. #ifdef ZT_DEBUG
  153. #define ZT_INLINE
  154. #else
  155. #if defined(__GNUC__) || defined(__clang__)
  156. #define ZT_INLINE __attribute__((always_inline)) inline
  157. #else
  158. #define ZT_INLINE inline
  159. #endif
  160. #endif
  161. #endif
  162. #ifndef restrict
  163. #if defined(__GNUC__) || defined(__clang__)
  164. #define restrict __restrict__
  165. #else
  166. #define restrict
  167. #endif
  168. #endif
  169. #ifndef likely
  170. #if defined(__GNUC__) || defined(__clang__)
  171. #define likely(x) __builtin_expect((x),1)
  172. #else
  173. #define likely(x) x
  174. #endif
  175. #endif
  176. #ifndef unlikely
  177. #if defined(__GNUC__) || defined(__clang__)
  178. #define unlikely(x) __builtin_expect((x),0)
  179. #else
  180. #define unlikely(x) x
  181. #endif
  182. #endif
  183. #if defined(__SIZEOF_INT128__) || ((defined(ZT_ARCH_X64) || defined(__aarch64__)) && defined(__GNUC__))
  184. #ifdef __SIZEOF_INT128__
  185. #define ZT_HAVE_UINT128 1
  186. typedef unsigned __int128 uint128_t;
  187. #else
  188. #define ZT_HAVE_UINT128 1
  189. typedef unsigned uint128_t __attribute__((mode(TI)));
  190. #endif
  191. #endif
  192. #if !defined(__BYTE_ORDER) && defined(__BYTE_ORDER__)
  193. #define __BYTE_ORDER __BYTE_ORDER__
  194. #define __LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
  195. #define __BIG_ENDIAN __ORDER_BIG_ENDIAN__
  196. #endif
  197. #if !defined(__BYTE_ORDER) && defined(BYTE_ORDER)
  198. #define __BYTE_ORDER BYTE_ORDER
  199. #define __LITTLE_ENDIAN LITTLE_ENDIAN
  200. #define __BIG_ENDIAN BIG_ENDIAN
  201. #endif
  202. #if !defined(__BYTE_ORDER) && defined(_BYTE_ORDER)
  203. #define __BYTE_ORDER _BYTE_ORDER
  204. #define __LITTLE_ENDIAN _LITTLE_ENDIAN
  205. #define __BIG_ENDIAN _BIG_ENDIAN
  206. #endif
  207. #define ZT_VA_ARGS(...) , ##__VA_ARGS__
  208. #ifdef ZT_DEBUG_SPEW
  209. #define ZT_SPEW(f,...) fprintf(stderr,"%s:%d(%s): " f ZT_EOL_S,__FILE__,__LINE__,__FUNCTION__ ZT_VA_ARGS(__VA_ARGS__))
  210. #else
  211. #define ZT_SPEW(f,...)
  212. #endif
  213. #endif