OS.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #ifdef _MSC_VER
  33. #pragma warning(disable : 4290)
  34. #pragma warning(disable : 4996)
  35. #pragma warning(disable : 4101)
  36. #endif
  37. #if defined(_WIN32) || defined(_WIN64)
  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. #ifndef __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. // Avoid unaligned type casts on all but x86/x64 architecture.
  88. #if (!(defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_AMD64) || defined(_M_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)))
  89. #ifndef ZT_NO_UNALIGNED_ACCESS
  90. #define ZT_NO_UNALIGNED_ACCESS
  91. #endif
  92. #endif
  93. // Assume little endian if not defined on Mac and Windows as these don't run on any BE architectures.
  94. #if (defined(__APPLE__) || defined(__WINDOWS__)) && (!defined(__BYTE_ORDER))
  95. #undef __BYTE_ORDER
  96. #undef __LITTLE_ENDIAN
  97. #undef __BIG_ENDIAN
  98. #define __BIG_ENDIAN 4321
  99. #define __LITTLE_ENDIAN 1234
  100. #define __BYTE_ORDER 1234
  101. #endif
  102. #ifndef __BYTE_ORDER
  103. #include <endian.h>
  104. #endif
  105. #if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__)
  106. #define ZT_ALWAYS_INLINE __attribute__((always_inline)) inline
  107. #ifndef restrict
  108. #define restrict __restrict__
  109. #endif
  110. #ifndef likely
  111. #define likely(x) __builtin_expect((x),1)
  112. #endif
  113. #ifndef unlikely
  114. #define unlikely(x) __builtin_expect((x),0)
  115. #endif
  116. #else /* not GCC-like */
  117. #ifndef restrict
  118. #define restrict
  119. #endif
  120. #ifndef likely
  121. #define inline inline
  122. #define likely(x) (x)
  123. #endif
  124. #ifndef unlikely
  125. #define unlikely(x) (x)
  126. #endif
  127. #endif
  128. #if __cplusplus > 199711L
  129. #ifndef __CPP11__
  130. #define __CPP11__
  131. #endif
  132. #endif
  133. #ifndef __CPP11__
  134. #define nullptr (0)
  135. #endif
  136. #ifdef SOCKET
  137. #define ZT_SOCKET SOCKET
  138. #else
  139. #define ZT_SOCKET int
  140. #endif
  141. #ifdef INVALID_SOCKET
  142. #define ZT_INVALID_SOCKET INVALID_SOCKET
  143. #else
  144. #define ZT_INVALID_SOCKET -1
  145. #endif
  146. #ifdef __WINDOWS__
  147. #define ZT_PATH_SEPARATOR '\\'
  148. #define ZT_PATH_SEPARATOR_S "\\"
  149. #define ZT_EOL_S "\r\n"
  150. #else
  151. #define ZT_PATH_SEPARATOR '/'
  152. #define ZT_PATH_SEPARATOR_S "/"
  153. #define ZT_EOL_S "\n"
  154. #endif
  155. #ifndef ZT_ALWAYS_INLINE
  156. #define ZT_ALWAYS_INLINE inline
  157. #endif
  158. #endif