OS.hpp 5.2 KB

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