OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) | 53 #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) |
54 #define PROTOBUF_LITTLE_ENDIAN 1 | 54 #define PROTOBUF_LITTLE_ENDIAN 1 |
55 #endif | 55 #endif |
56 #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER) | 56 #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER) |
57 // If MSVC has "/RTCc" set, it will complain about truncating casts at | 57 // If MSVC has "/RTCc" set, it will complain about truncating casts at |
58 // runtime. This file contains some intentional truncating casts. | 58 // runtime. This file contains some intentional truncating casts. |
59 #pragma runtime_checks("c", off) | 59 #pragma runtime_checks("c", off) |
60 #endif | 60 #endif |
61 #else | 61 #else |
62 #include <sys/param.h> // __BYTE_ORDER | 62 #include <sys/param.h> // __BYTE_ORDER |
| 63 #if defined(__OpenBSD__) |
| 64 #include <endian.h> |
| 65 #endif |
63 #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \ | 66 #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \ |
64 (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \ | 67 (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \ |
| 68 (defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN)) && \ |
65 !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) | 69 !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) |
66 #define PROTOBUF_LITTLE_ENDIAN 1 | 70 #define PROTOBUF_LITTLE_ENDIAN 1 |
67 #endif | 71 #endif |
68 #endif | 72 #endif |
69 | 73 |
70 // The macros defined below are required in order to make protobuf_lite a | 74 // The macros defined below are required in order to make protobuf_lite a |
71 // component on all platforms. See http://crbug.com/172800. | 75 // component on all platforms. See http://crbug.com/172800. |
72 #if defined(COMPONENT_BUILD) && defined(PROTOBUF_USE_DLLS) | 76 #if defined(COMPONENT_BUILD) && defined(PROTOBUF_USE_DLLS) |
73 #if defined(_MSC_VER) | 77 #if defined(_MSC_VER) |
74 #ifdef LIBPROTOBUF_EXPORTS | 78 #ifdef LIBPROTOBUF_EXPORTS |
(...skipping 27 matching lines...) Expand all Loading... |
102 #ifdef _MSC_VER | 106 #ifdef _MSC_VER |
103 #include <stdlib.h> // NOLINT(build/include) | 107 #include <stdlib.h> // NOLINT(build/include) |
104 #elif defined(__APPLE__) | 108 #elif defined(__APPLE__) |
105 #include <libkern/OSByteOrder.h> | 109 #include <libkern/OSByteOrder.h> |
106 #elif defined(__GLIBC__) || defined(__CYGWIN__) | 110 #elif defined(__GLIBC__) || defined(__CYGWIN__) |
107 #include <byteswap.h> // IWYU pragma: export | 111 #include <byteswap.h> // IWYU pragma: export |
108 #endif | 112 #endif |
109 | 113 |
110 // =================================================================== | 114 // =================================================================== |
111 // from google3/base/port.h | 115 // from google3/base/port.h |
| 116 |
| 117 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L || \ |
| 118 (defined(_MSC_VER) && _MSC_VER >= 1900)) |
| 119 // Define this to 1 if the code is compiled in C++11 mode; leave it |
| 120 // undefined otherwise. Do NOT define it to 0 -- that causes |
| 121 // '#ifdef LANG_CXX11' to behave differently from '#if LANG_CXX11'. |
| 122 #define LANG_CXX11 1 |
| 123 #endif |
| 124 |
112 namespace google { | 125 namespace google { |
113 namespace protobuf { | 126 namespace protobuf { |
114 | 127 |
115 typedef unsigned int uint; | 128 typedef unsigned int uint; |
116 | 129 |
117 #ifdef _MSC_VER | 130 #ifdef _MSC_VER |
118 typedef signed __int8 int8; | 131 typedef signed __int8 int8; |
119 typedef __int16 int16; | 132 typedef __int16 int16; |
120 typedef __int32 int32; | 133 typedef __int32 int32; |
121 typedef __int64 int64; | 134 typedef __int64 int64; |
122 | 135 |
123 typedef unsigned __int8 uint8; | 136 typedef unsigned __int8 uint8; |
124 typedef unsigned __int16 uint16; | 137 typedef unsigned __int16 uint16; |
125 typedef unsigned __int32 uint32; | 138 typedef unsigned __int32 uint32; |
126 typedef unsigned __int64 uint64; | 139 typedef unsigned __int64 uint64; |
127 #else | 140 #else |
128 typedef int8_t int8; | 141 typedef int8_t int8; |
129 typedef int16_t int16; | 142 typedef int16_t int16; |
130 typedef int32_t int32; | 143 typedef int32_t int32; |
131 typedef int64_t int64; | 144 typedef int64_t int64; |
132 | 145 |
133 typedef uint8_t uint8; | 146 typedef uint8_t uint8; |
134 typedef uint16_t uint16; | 147 typedef uint16_t uint16; |
135 typedef uint32_t uint32; | 148 typedef uint32_t uint32; |
136 typedef uint64_t uint64; | 149 typedef uint64_t uint64; |
137 #endif | 150 #endif |
138 | 151 |
139 // long long macros to be used because gcc and vc++ use different suffixes, | 152 // long long macros to be used because gcc and vc++ use different suffixes, |
140 // and different size specifiers in format strings | 153 // and different size specifiers in format strings |
141 #undef GOOGLE_LONGLONG | 154 #undef GOOGLE_LONGLONG |
142 #undef GOOGLE_ULONGLONG | 155 #undef GOOGLE_ULONGLONG |
143 #undef GOOGLE_LL_FORMAT | 156 #undef GOOGLE_LL_FORMAT |
144 | 157 |
145 #ifdef _MSC_VER | 158 #ifdef _MSC_VER |
146 #define GOOGLE_LONGLONG(x) x##I64 | 159 #define GOOGLE_LONGLONG(x) x##I64 |
147 #define GOOGLE_ULONGLONG(x) x##UI64 | 160 #define GOOGLE_ULONGLONG(x) x##UI64 |
148 #define GOOGLE_LL_FORMAT "I64" // As in printf("%I64d", ...) | 161 #define GOOGLE_LL_FORMAT "I64" // As in printf("%I64d", ...) |
149 #else | 162 #else |
| 163 // By long long, we actually mean int64. |
150 #define GOOGLE_LONGLONG(x) INT64_C(x) | 164 #define GOOGLE_LONGLONG(x) INT64_C(x) |
151 #define GOOGLE_ULONGLONG(x) UINT64_C(x) | 165 #define GOOGLE_ULONGLONG(x) UINT64_C(x) |
| 166 // Used to format real long long integers. |
152 #define GOOGLE_LL_FORMAT "ll" // As in "%lld". Note that "q" is poor form also. | 167 #define GOOGLE_LL_FORMAT "ll" // As in "%lld". Note that "q" is poor form also. |
153 #endif | 168 #endif |
154 | 169 |
155 static const int32 kint32max = 0x7FFFFFFF; | 170 static const int32 kint32max = 0x7FFFFFFF; |
156 static const int32 kint32min = -kint32max - 1; | 171 static const int32 kint32min = -kint32max - 1; |
157 static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF); | 172 static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF); |
158 static const int64 kint64min = -kint64max - 1; | 173 static const int64 kint64min = -kint64max - 1; |
159 static const uint32 kuint32max = 0xFFFFFFFFu; | 174 static const uint32 kuint32max = 0xFFFFFFFFu; |
160 static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF); | 175 static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF); |
161 | 176 |
(...skipping 21 matching lines...) Expand all Loading... |
183 #define GOOGLE_ATTRIBUTE_NOINLINE __attribute__ ((noinline)) | 198 #define GOOGLE_ATTRIBUTE_NOINLINE __attribute__ ((noinline)) |
184 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) | 199 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) |
185 // Seems to have been around since at least Visual Studio 2005 | 200 // Seems to have been around since at least Visual Studio 2005 |
186 #define GOOGLE_ATTRIBUTE_NOINLINE __declspec(noinline) | 201 #define GOOGLE_ATTRIBUTE_NOINLINE __declspec(noinline) |
187 #else | 202 #else |
188 // Other compilers will have to figure it out for themselves. | 203 // Other compilers will have to figure it out for themselves. |
189 #define GOOGLE_ATTRIBUTE_NOINLINE | 204 #define GOOGLE_ATTRIBUTE_NOINLINE |
190 #endif | 205 #endif |
191 #endif | 206 #endif |
192 | 207 |
| 208 #ifndef GOOGLE_ATTRIBUTE_NORETURN |
| 209 #ifdef __GNUC__ |
| 210 // Tell the compiler that a given function never returns. |
| 211 #define GOOGLE_ATTRIBUTE_NORETURN __attribute__((noreturn)) |
| 212 #else |
| 213 #define GOOGLE_ATTRIBUTE_NORETURN |
| 214 #endif |
| 215 #endif |
| 216 |
193 #ifndef GOOGLE_ATTRIBUTE_DEPRECATED | 217 #ifndef GOOGLE_ATTRIBUTE_DEPRECATED |
194 #ifdef __GNUC__ | 218 #ifdef __GNUC__ |
195 // If the method/variable/type is used anywhere, produce a warning. | 219 // If the method/variable/type is used anywhere, produce a warning. |
196 #define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated)) | 220 #define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated)) |
197 #else | 221 #else |
198 #define GOOGLE_ATTRIBUTE_DEPRECATED | 222 #define GOOGLE_ATTRIBUTE_DEPRECATED |
199 #endif | 223 #endif |
200 #endif | 224 #endif |
201 | 225 |
202 #ifndef GOOGLE_PREDICT_TRUE | 226 #ifndef GOOGLE_PREDICT_TRUE |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 memcpy(p, &v, sizeof v); | 309 memcpy(p, &v, sizeof v); |
286 } | 310 } |
287 #endif | 311 #endif |
288 | 312 |
289 #if defined(_MSC_VER) | 313 #if defined(_MSC_VER) |
290 #define GOOGLE_THREAD_LOCAL __declspec(thread) | 314 #define GOOGLE_THREAD_LOCAL __declspec(thread) |
291 #else | 315 #else |
292 #define GOOGLE_THREAD_LOCAL __thread | 316 #define GOOGLE_THREAD_LOCAL __thread |
293 #endif | 317 #endif |
294 | 318 |
295 // The following guarantees declaration of the byte swap functions, and | 319 // The following guarantees declaration of the byte swap functions. |
296 // defines __BYTE_ORDER for MSVC | |
297 #ifdef _MSC_VER | 320 #ifdef _MSC_VER |
298 #define __BYTE_ORDER __LITTLE_ENDIAN | |
299 #define bswap_16(x) _byteswap_ushort(x) | 321 #define bswap_16(x) _byteswap_ushort(x) |
300 #define bswap_32(x) _byteswap_ulong(x) | 322 #define bswap_32(x) _byteswap_ulong(x) |
301 #define bswap_64(x) _byteswap_uint64(x) | 323 #define bswap_64(x) _byteswap_uint64(x) |
302 | 324 |
303 #elif defined(__APPLE__) | 325 #elif defined(__APPLE__) |
304 // Mac OS X / Darwin features | 326 // Mac OS X / Darwin features |
305 #define bswap_16(x) OSSwapInt16(x) | 327 #define bswap_16(x) OSSwapInt16(x) |
306 #define bswap_32(x) OSSwapInt32(x) | 328 #define bswap_32(x) OSSwapInt32(x) |
307 #define bswap_64(x) OSSwapInt64(x) | 329 #define bswap_64(x) OSSwapInt64(x) |
308 | 330 |
(...skipping 18 matching lines...) Expand all Loading... |
327 ((x & GOOGLE_ULONGLONG(0xFF00000000)) >> 8) | | 349 ((x & GOOGLE_ULONGLONG(0xFF00000000)) >> 8) | |
328 ((x & GOOGLE_ULONGLONG(0xFF0000000000)) >> 24) | | 350 ((x & GOOGLE_ULONGLONG(0xFF0000000000)) >> 24) | |
329 ((x & GOOGLE_ULONGLONG(0xFF000000000000)) >> 40) | | 351 ((x & GOOGLE_ULONGLONG(0xFF000000000000)) >> 40) | |
330 ((x & GOOGLE_ULONGLONG(0xFF00000000000000)) >> 56)); | 352 ((x & GOOGLE_ULONGLONG(0xFF00000000000000)) >> 56)); |
331 } | 353 } |
332 #define bswap_64(x) bswap_64(x) | 354 #define bswap_64(x) bswap_64(x) |
333 | 355 |
334 #endif | 356 #endif |
335 | 357 |
336 // =================================================================== | 358 // =================================================================== |
| 359 // from google3/util/bits/bits.h |
| 360 |
| 361 class Bits { |
| 362 public: |
| 363 static uint32 Log2FloorNonZero(uint32 n) { |
| 364 #if defined(__GNUC__) |
| 365 return 31 ^ __builtin_clz(n); |
| 366 #elif defined(COMPILER_MSVC) && defined(_M_IX86) |
| 367 _asm { |
| 368 bsr ebx, n |
| 369 mov n, ebx |
| 370 } |
| 371 return n; |
| 372 #else |
| 373 return Log2FloorNonZero_Portable(n); |
| 374 #endif |
| 375 } |
| 376 |
| 377 static uint64 Log2FloorNonZero64(uint64 n) { |
| 378 #if defined(__GNUC__) |
| 379 return 63 ^ __builtin_clzll(n); |
| 380 #else |
| 381 return Log2FloorNonZero64_Portable(n); |
| 382 #endif |
| 383 } |
| 384 private: |
| 385 static int Log2FloorNonZero_Portable(uint32 n) { |
| 386 if (n == 0) |
| 387 return -1; |
| 388 int log = 0; |
| 389 uint32 value = n; |
| 390 for (int i = 4; i >= 0; --i) { |
| 391 int shift = (1 << i); |
| 392 uint32 x = value >> shift; |
| 393 if (x != 0) { |
| 394 value = x; |
| 395 log += shift; |
| 396 } |
| 397 } |
| 398 assert(value == 1); |
| 399 return log; |
| 400 } |
| 401 |
| 402 static int Log2FloorNonZero64_Portable(uint64 n) { |
| 403 const uint32 topbits = static_cast<uint32>(n >> 32); |
| 404 if (topbits == 0) { |
| 405 // Top bits are zero, so scan in bottom bits |
| 406 return Log2FloorNonZero(static_cast<uint32>(n)); |
| 407 } else { |
| 408 return 32 + Log2FloorNonZero(topbits); |
| 409 } |
| 410 } |
| 411 }; |
| 412 |
| 413 // =================================================================== |
337 // from google3/util/endian/endian.h | 414 // from google3/util/endian/endian.h |
338 LIBPROTOBUF_EXPORT uint32 ghtonl(uint32 x); | 415 LIBPROTOBUF_EXPORT uint32 ghtonl(uint32 x); |
339 | 416 |
340 class BigEndian { | 417 class BigEndian { |
341 public: | 418 public: |
342 #ifdef PROTOBUF_LITTLE_ENDIAN | 419 #ifdef PROTOBUF_LITTLE_ENDIAN |
343 | 420 |
344 static uint16 FromHost16(uint16 x) { return bswap_16(x); } | 421 static uint16 FromHost16(uint16 x) { return bswap_16(x); } |
345 static uint16 ToHost16(uint16 x) { return bswap_16(x); } | 422 static uint16 ToHost16(uint16 x) { return bswap_16(x); } |
346 | 423 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 static void Store64(void *p, uint64 v) { | 468 static void Store64(void *p, uint64 v) { |
392 GOOGLE_UNALIGNED_STORE64(p, FromHost64(v)); | 469 GOOGLE_UNALIGNED_STORE64(p, FromHost64(v)); |
393 } | 470 } |
394 }; | 471 }; |
395 | 472 |
396 | 473 |
397 } // namespace protobuf | 474 } // namespace protobuf |
398 } // namespace google | 475 } // namespace google |
399 | 476 |
400 #endif // GOOGLE_PROTOBUF_STUBS_PORT_H_ | 477 #endif // GOOGLE_PROTOBUF_STUBS_PORT_H_ |
OLD | NEW |