OS.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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: 2024-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. //
  16. // This include file also auto-detects and canonicalizes some environment
  17. // information defines:
  18. //
  19. // __LINUX__
  20. // __APPLE__
  21. // __BSD__ (OSX also defines this)
  22. // __UNIX_LIKE__ (Linux, BSD, etc.)
  23. // __WINDOWS__
  24. //
  25. // Also makes sure __BYTE_ORDER is defined reasonably.
  26. //
  27. #ifndef __GCC__
  28. #if 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__)
  29. #define __GCC__
  30. #endif
  31. #endif
  32. #if defined(__GCC__) && !defined(__GNUC__)
  33. #define __GNUC__
  34. #endif
  35. #if defined(__SIZEOF_INT128__) || ((defined(__GCC__) || defined(__GNUC__) || defined(__clang)) && (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64) || defined(__aarch64__)))
  36. #if defined(__SIZEOF_INT128__)
  37. #define ZT_HAVE_UINT128 1
  38. typedef unsigned __int128 uint128_t;
  39. #else
  40. #define ZT_HAVE_UINT128 1
  41. typedef unsigned uint128_t __attribute__((mode(TI)));
  42. #endif
  43. #endif
  44. #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  45. #define ZT_ARCH_X64 1
  46. #endif
  47. // As far as we know it's only generally safe to do unaligned type casts in all
  48. // cases on x86 and x64 architectures. Others such as ARM and MIPS will generate
  49. // a fault or exhibit undefined behavior that varies by vendor.
  50. #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)))
  51. #ifndef ZT_NO_UNALIGNED_ACCESS
  52. #define ZT_NO_UNALIGNED_ACCESS 1
  53. #endif
  54. #endif
  55. #if defined(_WIN32) || defined(_WIN64)
  56. #ifdef _MSC_VER
  57. #pragma warning(disable : 4290)
  58. #pragma warning(disable : 4996)
  59. #pragma warning(disable : 4101)
  60. #endif
  61. #ifndef __WINDOWS__
  62. #define __WINDOWS__ 1
  63. #endif
  64. #ifndef NOMINMAX
  65. #define NOMINMAX
  66. #endif
  67. #undef __UNIX_LIKE__
  68. #undef __BSD__
  69. #include <WinSock2.h>
  70. #include <Windows.h>
  71. #endif
  72. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  73. #ifndef __LINUX__
  74. #define __LINUX__ 1
  75. #endif
  76. #ifndef __UNIX_LIKE__
  77. #define __UNIX_LIKE__ 1
  78. #endif
  79. #include <endian.h>
  80. #endif
  81. #ifdef __APPLE__
  82. #include <TargetConditionals.h>
  83. #ifndef __UNIX_LIKE__
  84. #define __UNIX_LIKE__ 1
  85. #endif
  86. #ifndef __BSD__
  87. #define __BSD__ 1
  88. #endif
  89. #include <machine/endian.h>
  90. #ifndef __BYTE_ORDER
  91. #define __BYTE_ORDER __DARWIN_BYTE_ORDER
  92. #define __BIG_ENDIAN __DARWIN_BIG_ENDIAN
  93. #define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
  94. #endif
  95. #endif
  96. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
  97. #ifndef __UNIX_LIKE__
  98. #define __UNIX_LIKE__ 1
  99. #endif
  100. #ifndef __BSD__
  101. #define __BSD__ 1
  102. #endif
  103. #include <sys/endian.h>
  104. #ifndef RTF_MULTICAST
  105. #define RTF_MULTICAST 0x20000000
  106. #endif
  107. #endif
  108. // It would probably be safe to assume LE everywhere except on very specific architectures as there
  109. // are few BE chips remaining in the wild that are powerful enough to run this, but for now we'll
  110. // try to include endian.h and error out if it doesn't exist.
  111. #ifndef __BYTE_ORDER
  112. #ifdef _BYTE_ORDER
  113. #define __BYTE_ORDER _BYTE_ORDER
  114. #define __LITTLE_ENDIAN _LITTLE_ENDIAN
  115. #define __BIG_ENDIAN _BIG_ENDIAN
  116. #else
  117. #include <endian.h>
  118. #endif
  119. #endif
  120. #if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__)
  121. #define ZT_ALWAYS_INLINE __attribute__((always_inline)) inline
  122. #ifndef restrict
  123. #define restrict __restrict__
  124. #endif
  125. #ifndef likely
  126. #define likely(x) __builtin_expect((x),1)
  127. #endif
  128. #ifndef unlikely
  129. #define unlikely(x) __builtin_expect((x),0)
  130. #endif
  131. #else /* not GCC-like */
  132. #ifndef restrict
  133. #define restrict
  134. #endif
  135. #ifndef likely
  136. #define likely(x) (x)
  137. #endif
  138. #ifndef unlikely
  139. #define unlikely(x) (x)
  140. #endif
  141. #endif
  142. #if __cplusplus > 199711L
  143. #include <atomic>
  144. #ifndef __CPP11__
  145. #define __CPP11__
  146. #endif
  147. #endif
  148. #ifndef __CPP11__
  149. // TODO: we'll need to "polyfill" a subset of std::atomic for integers if we want to build on pre-C++11 compilers.
  150. // Beyond that defining nullptr, constexpr, and noexcept should allow us to still build on these. So far we've
  151. // avoided deeper C++11 features like lambdas in the core until we're 100% sure all the ancient targets are gone.
  152. #error need pre-c++11 std::atomic implementation
  153. #define nullptr (0)
  154. #define constexpr ZT_ALWAYS_INLINE
  155. #define noexcept throw()
  156. #endif
  157. #ifdef SOCKET
  158. #define ZT_SOCKET SOCKET
  159. #else
  160. #define ZT_SOCKET int
  161. #endif
  162. #ifdef INVALID_SOCKET
  163. #define ZT_INVALID_SOCKET INVALID_SOCKET
  164. #else
  165. #define ZT_INVALID_SOCKET -1
  166. #endif
  167. #ifdef __WINDOWS__
  168. #define ZT_PATH_SEPARATOR '\\'
  169. #define ZT_PATH_SEPARATOR_S "\\"
  170. #define ZT_EOL_S "\r\n"
  171. #else
  172. #define ZT_PATH_SEPARATOR '/'
  173. #define ZT_PATH_SEPARATOR_S "/"
  174. #define ZT_EOL_S "\n"
  175. #endif
  176. #ifndef ZT_ALWAYS_INLINE
  177. #define ZT_ALWAYS_INLINE inline
  178. #endif
  179. // Macro to avoid calling hton() on values known at compile time.
  180. #if __BYTE_ORDER == __LITTLE_ENDIAN
  181. #define ZT_CONST_TO_BE_UINT16(x) ((uint16_t)((uint16_t)((uint16_t)(x) << 8U) | (uint16_t)((uint16_t)(x) >> 8U)))
  182. #else
  183. #define ZT_CONST_TO_BE_UINT16(x) ((uint16_t)(x))
  184. #endif
  185. #endif