PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/google/protobuf/stubs/port.h

https://gitlab.com/yenny.prathivi/protobuf
C Header | 391 lines | 265 code | 59 blank | 67 comment | 24 complexity | 06763e45c103b9eacb371e25034f9753 MD5 | raw file
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #ifndef GOOGLE_PROTOBUF_STUBS_PORT_H_
  31. #define GOOGLE_PROTOBUF_STUBS_PORT_H_
  32. #include <assert.h>
  33. #include <stdlib.h>
  34. #include <cstddef>
  35. #include <string>
  36. #include <string.h>
  37. #if defined(__osf__)
  38. // Tru64 lacks stdint.h, but has inttypes.h which defines a superset of
  39. // what stdint.h would define.
  40. #include <inttypes.h>
  41. #elif !defined(_MSC_VER)
  42. #include <stdint.h>
  43. #endif
  44. #undef PROTOBUF_LITTLE_ENDIAN
  45. #ifdef _WIN32
  46. // Assuming windows is always little-endian.
  47. // TODO(xiaofeng): The PROTOBUF_LITTLE_ENDIAN is not only used for
  48. // optimization but also for correctness. We should define an
  49. // different macro to test the big-endian code path in coded_stream.
  50. #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
  51. #define PROTOBUF_LITTLE_ENDIAN 1
  52. #endif
  53. #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
  54. // If MSVC has "/RTCc" set, it will complain about truncating casts at
  55. // runtime. This file contains some intentional truncating casts.
  56. #pragma runtime_checks("c", off)
  57. #endif
  58. #else
  59. #include <sys/param.h> // __BYTE_ORDER
  60. #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
  61. (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \
  62. !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
  63. #define PROTOBUF_LITTLE_ENDIAN 1
  64. #endif
  65. #endif
  66. #if defined(_MSC_VER) && defined(PROTOBUF_USE_DLLS)
  67. #ifdef LIBPROTOBUF_EXPORTS
  68. #define LIBPROTOBUF_EXPORT __declspec(dllexport)
  69. #else
  70. #define LIBPROTOBUF_EXPORT __declspec(dllimport)
  71. #endif
  72. #ifdef LIBPROTOC_EXPORTS
  73. #define LIBPROTOC_EXPORT __declspec(dllexport)
  74. #else
  75. #define LIBPROTOC_EXPORT __declspec(dllimport)
  76. #endif
  77. #else
  78. #define LIBPROTOBUF_EXPORT
  79. #define LIBPROTOC_EXPORT
  80. #endif
  81. // These #includes are for the byte swap functions declared later on.
  82. #ifdef _MSC_VER
  83. #include <stdlib.h> // NOLINT(build/include)
  84. #elif defined(__APPLE__)
  85. #include <libkern/OSByteOrder.h>
  86. #elif defined(__GLIBC__) || defined(__CYGWIN__)
  87. #include <byteswap.h> // IWYU pragma: export
  88. #endif
  89. // ===================================================================
  90. // from google3/base/port.h
  91. namespace google {
  92. namespace protobuf {
  93. typedef unsigned int uint;
  94. #ifdef _MSC_VER
  95. typedef signed __int8 int8;
  96. typedef __int16 int16;
  97. typedef __int32 int32;
  98. typedef __int64 int64;
  99. typedef unsigned __int8 uint8;
  100. typedef unsigned __int16 uint16;
  101. typedef unsigned __int32 uint32;
  102. typedef unsigned __int64 uint64;
  103. #else
  104. typedef signed char int8;
  105. typedef short int16;
  106. typedef int int32;
  107. typedef long long int64;
  108. typedef unsigned char uint8;
  109. typedef unsigned short uint16;
  110. typedef unsigned int uint32;
  111. typedef unsigned long long uint64;
  112. #endif
  113. // long long macros to be used because gcc and vc++ use different suffixes,
  114. // and different size specifiers in format strings
  115. #undef GOOGLE_LONGLONG
  116. #undef GOOGLE_ULONGLONG
  117. #undef GOOGLE_LL_FORMAT
  118. #ifdef _MSC_VER
  119. #define GOOGLE_LONGLONG(x) x##I64
  120. #define GOOGLE_ULONGLONG(x) x##UI64
  121. #define GOOGLE_LL_FORMAT "I64" // As in printf("%I64d", ...)
  122. #else
  123. #define GOOGLE_LONGLONG(x) x##LL
  124. #define GOOGLE_ULONGLONG(x) x##ULL
  125. #define GOOGLE_LL_FORMAT "ll" // As in "%lld". Note that "q" is poor form also.
  126. #endif
  127. static const int32 kint32max = 0x7FFFFFFF;
  128. static const int32 kint32min = -kint32max - 1;
  129. static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF);
  130. static const int64 kint64min = -kint64max - 1;
  131. static const uint32 kuint32max = 0xFFFFFFFFu;
  132. static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF);
  133. // -------------------------------------------------------------------
  134. // Annotations: Some parts of the code have been annotated in ways that might
  135. // be useful to some compilers or tools, but are not supported universally.
  136. // You can #define these annotations yourself if the default implementation
  137. // is not right for you.
  138. #ifndef GOOGLE_ATTRIBUTE_ALWAYS_INLINE
  139. #if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
  140. // For functions we want to force inline.
  141. // Introduced in gcc 3.1.
  142. #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline))
  143. #else
  144. // Other compilers will have to figure it out for themselves.
  145. #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE
  146. #endif
  147. #endif
  148. #ifndef GOOGLE_ATTRIBUTE_NOINLINE
  149. #if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
  150. // For functions we want to force not inline.
  151. // Introduced in gcc 3.1.
  152. #define GOOGLE_ATTRIBUTE_NOINLINE __attribute__ ((noinline))
  153. #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
  154. // Seems to have been around since at least Visual Studio 2005
  155. #define GOOGLE_ATTRIBUTE_NOINLINE __declspec(noinline)
  156. #else
  157. // Other compilers will have to figure it out for themselves.
  158. #define GOOGLE_ATTRIBUTE_NOINLINE
  159. #endif
  160. #endif
  161. #ifndef GOOGLE_ATTRIBUTE_NORETURN
  162. #ifdef __GNUC__
  163. // Tell the compiler that a given function never returns.
  164. #define GOOGLE_ATTRIBUTE_NORETURN __attribute__((noreturn))
  165. #else
  166. #define GOOGLE_ATTRIBUTE_NORETURN
  167. #endif
  168. #endif
  169. #ifndef GOOGLE_ATTRIBUTE_DEPRECATED
  170. #ifdef __GNUC__
  171. // If the method/variable/type is used anywhere, produce a warning.
  172. #define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated))
  173. #else
  174. #define GOOGLE_ATTRIBUTE_DEPRECATED
  175. #endif
  176. #endif
  177. #ifndef GOOGLE_PREDICT_TRUE
  178. #ifdef __GNUC__
  179. // Provided at least since GCC 3.0.
  180. #define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
  181. #else
  182. #define GOOGLE_PREDICT_TRUE(x) (x)
  183. #endif
  184. #endif
  185. #ifndef GOOGLE_PREDICT_FALSE
  186. #ifdef __GNUC__
  187. // Provided at least since GCC 3.0.
  188. #define GOOGLE_PREDICT_FALSE(x) (__builtin_expect(x, 0))
  189. #else
  190. #define GOOGLE_PREDICT_FALSE(x) (x)
  191. #endif
  192. #endif
  193. // Delimits a block of code which may write to memory which is simultaneously
  194. // written by other threads, but which has been determined to be thread-safe
  195. // (e.g. because it is an idempotent write).
  196. #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN
  197. #define GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN()
  198. #endif
  199. #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_END
  200. #define GOOGLE_SAFE_CONCURRENT_WRITES_END()
  201. #endif
  202. #if defined(__clang__) && defined(__has_cpp_attribute) \
  203. && !defined(GOOGLE_PROTOBUF_OS_APPLE)
  204. # if defined(GOOGLE_PROTOBUF_OS_NACL) || defined(EMSCRIPTEN) || \
  205. __has_cpp_attribute(clang::fallthrough)
  206. # define GOOGLE_FALLTHROUGH_INTENDED [[clang::fallthrough]]
  207. # endif
  208. #endif
  209. #ifndef GOOGLE_FALLTHROUGH_INTENDED
  210. # define GOOGLE_FALLTHROUGH_INTENDED
  211. #endif
  212. #define GOOGLE_GUARDED_BY(x)
  213. #define GOOGLE_ATTRIBUTE_COLD
  214. // x86 and x86-64 can perform unaligned loads/stores directly.
  215. #if defined(_M_X64) || defined(__x86_64__) || \
  216. defined(_M_IX86) || defined(__i386__)
  217. #define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
  218. #define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
  219. #define GOOGLE_UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p))
  220. #define GOOGLE_UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val))
  221. #define GOOGLE_UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val))
  222. #define GOOGLE_UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val))
  223. #else
  224. inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
  225. uint16 t;
  226. memcpy(&t, p, sizeof t);
  227. return t;
  228. }
  229. inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
  230. uint32 t;
  231. memcpy(&t, p, sizeof t);
  232. return t;
  233. }
  234. inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
  235. uint64 t;
  236. memcpy(&t, p, sizeof t);
  237. return t;
  238. }
  239. inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
  240. memcpy(p, &v, sizeof v);
  241. }
  242. inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
  243. memcpy(p, &v, sizeof v);
  244. }
  245. inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
  246. memcpy(p, &v, sizeof v);
  247. }
  248. #endif
  249. #if defined(_MSC_VER)
  250. #define GOOGLE_THREAD_LOCAL __declspec(thread)
  251. #else
  252. #define GOOGLE_THREAD_LOCAL __thread
  253. #endif
  254. // The following guarantees declaration of the byte swap functions.
  255. #ifdef _MSC_VER
  256. #define bswap_16(x) _byteswap_ushort(x)
  257. #define bswap_32(x) _byteswap_ulong(x)
  258. #define bswap_64(x) _byteswap_uint64(x)
  259. #elif defined(__APPLE__)
  260. // Mac OS X / Darwin features
  261. #define bswap_16(x) OSSwapInt16(x)
  262. #define bswap_32(x) OSSwapInt32(x)
  263. #define bswap_64(x) OSSwapInt64(x)
  264. #elif !defined(__GLIBC__) && !defined(__CYGWIN__)
  265. static inline uint16 bswap_16(uint16 x) {
  266. return static_cast<uint16>(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8));
  267. }
  268. #define bswap_16(x) bswap_16(x)
  269. static inline uint32 bswap_32(uint32 x) {
  270. return (((x & 0xFF) << 24) |
  271. ((x & 0xFF00) << 8) |
  272. ((x & 0xFF0000) >> 8) |
  273. ((x & 0xFF000000) >> 24));
  274. }
  275. #define bswap_32(x) bswap_32(x)
  276. static inline uint64 bswap_64(uint64 x) {
  277. return (((x & GOOGLE_ULONGLONG(0xFF)) << 56) |
  278. ((x & GOOGLE_ULONGLONG(0xFF00)) << 40) |
  279. ((x & GOOGLE_ULONGLONG(0xFF0000)) << 24) |
  280. ((x & GOOGLE_ULONGLONG(0xFF000000)) << 8) |
  281. ((x & GOOGLE_ULONGLONG(0xFF00000000)) >> 8) |
  282. ((x & GOOGLE_ULONGLONG(0xFF0000000000)) >> 24) |
  283. ((x & GOOGLE_ULONGLONG(0xFF000000000000)) >> 40) |
  284. ((x & GOOGLE_ULONGLONG(0xFF00000000000000)) >> 56));
  285. }
  286. #define bswap_64(x) bswap_64(x)
  287. #endif
  288. // ===================================================================
  289. // from google3/util/endian/endian.h
  290. LIBPROTOBUF_EXPORT uint32 ghtonl(uint32 x);
  291. class BigEndian {
  292. public:
  293. #ifdef PROTOBUF_LITTLE_ENDIAN
  294. static uint16 FromHost16(uint16 x) { return bswap_16(x); }
  295. static uint16 ToHost16(uint16 x) { return bswap_16(x); }
  296. static uint32 FromHost32(uint32 x) { return bswap_32(x); }
  297. static uint32 ToHost32(uint32 x) { return bswap_32(x); }
  298. static uint64 FromHost64(uint64 x) { return bswap_64(x); }
  299. static uint64 ToHost64(uint64 x) { return bswap_64(x); }
  300. static bool IsLittleEndian() { return true; }
  301. #else
  302. static uint16 FromHost16(uint16 x) { return x; }
  303. static uint16 ToHost16(uint16 x) { return x; }
  304. static uint32 FromHost32(uint32 x) { return x; }
  305. static uint32 ToHost32(uint32 x) { return x; }
  306. static uint64 FromHost64(uint64 x) { return x; }
  307. static uint64 ToHost64(uint64 x) { return x; }
  308. static bool IsLittleEndian() { return false; }
  309. #endif /* ENDIAN */
  310. // Functions to do unaligned loads and stores in big-endian order.
  311. static uint16 Load16(const void *p) {
  312. return ToHost16(GOOGLE_UNALIGNED_LOAD16(p));
  313. }
  314. static void Store16(void *p, uint16 v) {
  315. GOOGLE_UNALIGNED_STORE16(p, FromHost16(v));
  316. }
  317. static uint32 Load32(const void *p) {
  318. return ToHost32(GOOGLE_UNALIGNED_LOAD32(p));
  319. }
  320. static void Store32(void *p, uint32 v) {
  321. GOOGLE_UNALIGNED_STORE32(p, FromHost32(v));
  322. }
  323. static uint64 Load64(const void *p) {
  324. return ToHost64(GOOGLE_UNALIGNED_LOAD64(p));
  325. }
  326. static void Store64(void *p, uint64 v) {
  327. GOOGLE_UNALIGNED_STORE64(p, FromHost64(v));
  328. }
  329. };
  330. } // namespace protobuf
  331. } // namespace google
  332. #endif // GOOGLE_PROTOBUF_STUBS_PORT_H_