OS.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // 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. #if defined(_WIN32) || defined(_WIN64)
  21. #ifdef _MSC_VER
  22. #pragma warning(disable : 4290)
  23. #pragma warning(disable : 4996)
  24. #pragma warning(disable : 4101)
  25. #endif
  26. #ifndef __WINDOWS__
  27. #define __WINDOWS__ 1
  28. #endif
  29. #ifndef NOMINMAX
  30. #define NOMINMAX
  31. #endif
  32. #undef __UNIX_LIKE__
  33. #undef __BSD__
  34. #include <WinSock2.h>
  35. #include <Windows.h>
  36. #include <sys/param.h>
  37. #endif
  38. #ifdef SOCKET
  39. #define ZT_SOCKET SOCKET
  40. #else
  41. #define ZT_SOCKET int
  42. #endif
  43. #ifdef INVALID_SOCKET
  44. #define ZT_INVALID_SOCKET INVALID_SOCKET
  45. #else
  46. #define ZT_INVALID_SOCKET (-1)
  47. #endif
  48. #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__))
  49. #define __GNUC__ 3
  50. #endif
  51. #ifdef __cplusplus
  52. #if __cplusplus > 199711L
  53. #include <atomic>
  54. #ifndef __CPP11__
  55. #define __CPP11__
  56. #endif
  57. #endif
  58. #ifndef __CPP11__
  59. // Beyond that defining nullptr, constexpr, and noexcept should allow us to still build on these. So far we've
  60. // avoided deeper C++11 features like lambdas in the core until we're 100% sure all the ancient targets are gone.
  61. #error TODO: to build on pre-c++11 compilers we will need to make a subset of std::atomic for integers
  62. #define nullptr (0)
  63. #define constexpr ZT_INLINE
  64. #define noexcept throw()
  65. #endif
  66. #endif
  67. #ifdef __GNUC__
  68. #ifndef ZT_DEBUG
  69. #define ZT_INLINE __attribute__((always_inline)) inline
  70. #endif
  71. #ifndef restrict
  72. #define restrict __restrict__
  73. #endif
  74. #ifndef likely
  75. #define likely(x) __builtin_expect((x),1)
  76. #endif
  77. #ifndef unlikely
  78. #define unlikely(x) __builtin_expect((x),0)
  79. #endif
  80. #else /* not GCC-like */
  81. #ifndef restrict
  82. #define restrict
  83. #endif
  84. #ifndef likely
  85. #define likely(x) (x)
  86. #endif
  87. #ifndef unlikely
  88. #define unlikely(x) (x)
  89. #endif
  90. #endif
  91. #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  92. #define ZT_ARCH_X64 1
  93. #endif
  94. #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)
  95. #define ZT_ARCH_X86 1
  96. #endif
  97. #if !defined(ZT_ARCH_X86)
  98. #ifndef ZT_NO_UNALIGNED_ACCESS
  99. #define ZT_NO_UNALIGNED_ACCESS 1
  100. #endif
  101. #endif
  102. #if defined(__SIZEOF_INT128__) || ((defined(ZT_ARCH_X64) || defined(__aarch64__)) && defined(__GNUC__))
  103. #ifdef __SIZEOF_INT128__
  104. #define ZT_HAVE_UINT128 1
  105. typedef unsigned __int128 uint128_t;
  106. #else
  107. #define ZT_HAVE_UINT128 1
  108. typedef unsigned uint128_t __attribute__((mode(TI)));
  109. #endif
  110. #endif
  111. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  112. #ifndef __LINUX__
  113. #define __LINUX__ 1
  114. #endif
  115. #ifndef __UNIX_LIKE__
  116. #define __UNIX_LIKE__ 1
  117. #endif
  118. #include <endian.h>
  119. #endif
  120. #ifdef __APPLE__
  121. #include <TargetConditionals.h>
  122. #include <machine/endian.h>
  123. #ifndef __UNIX_LIKE__
  124. #define __UNIX_LIKE__ 1
  125. #endif
  126. #ifndef __BSD__
  127. #define __BSD__ 1
  128. #endif
  129. #ifndef __BYTE_ORDER
  130. #define __BYTE_ORDER __DARWIN_BYTE_ORDER
  131. #define __BIG_ENDIAN __DARWIN_BIG_ENDIAN
  132. #define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
  133. #endif
  134. #endif
  135. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
  136. #ifndef __UNIX_LIKE__
  137. #define __UNIX_LIKE__ 1
  138. #endif
  139. #ifndef __BSD__
  140. #define __BSD__ 1
  141. #endif
  142. #include <sys/endian.h>
  143. #ifndef RTF_MULTICAST
  144. #define RTF_MULTICAST 0x20000000
  145. #endif
  146. #endif
  147. #ifdef __WINDOWS__
  148. #define ZT_PATH_SEPARATOR '\\'
  149. #define ZT_PATH_SEPARATOR_S "\\"
  150. #define ZT_EOL_S "\r\n"
  151. #else
  152. #define ZT_PATH_SEPARATOR '/'
  153. #define ZT_PATH_SEPARATOR_S "/"
  154. #define ZT_EOL_S "\n"
  155. #endif
  156. #if !defined(__BYTE_ORDER) && defined(__BYTE_ORDER__)
  157. #define __BYTE_ORDER __BYTE_ORDER__
  158. #define __LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
  159. #define __BIG_ENDIAN __ORDER_BIG_ENDIAN__
  160. #endif
  161. #if !defined(__BYTE_ORDER) && defined(BYTE_ORDER)
  162. #define __BYTE_ORDER BYTE_ORDER
  163. #define __LITTLE_ENDIAN LITTLE_ENDIAN
  164. #define __BIG_ENDIAN BIG_ENDIAN
  165. #endif
  166. #if !defined(__BYTE_ORDER) && defined(_BYTE_ORDER)
  167. #define __BYTE_ORDER _BYTE_ORDER
  168. #define __LITTLE_ENDIAN _LITTLE_ENDIAN
  169. #define __BIG_ENDIAN _BIG_ENDIAN
  170. #endif
  171. #ifndef ZT_INLINE
  172. #ifdef ZT_DEBUG
  173. #define ZT_INLINE
  174. #else
  175. #define ZT_INLINE inline
  176. #endif
  177. #endif
  178. #endif