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