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 | |
66 #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \ | 63 #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \ |
67 (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \ | 64 (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \ |
68 (defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN)) && \ | |
69 !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) | 65 !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) |
70 #define PROTOBUF_LITTLE_ENDIAN 1 | 66 #define PROTOBUF_LITTLE_ENDIAN 1 |
71 #endif | 67 #endif |
72 #endif | 68 #endif |
73 | 69 |
74 // The macros defined below are required in order to make protobuf_lite a | 70 // The macros defined below are required in order to make protobuf_lite a |
75 // component on all platforms. See http://crbug.com/172800. | 71 // component on all platforms. See http://crbug.com/172800. |
76 #if defined(COMPONENT_BUILD) && defined(PROTOBUF_USE_DLLS) | 72 #if defined(COMPONENT_BUILD) && defined(PROTOBUF_USE_DLLS) |
77 #if defined(_MSC_VER) | 73 #if defined(_MSC_VER) |
78 #ifdef LIBPROTOBUF_EXPORTS | 74 #ifdef LIBPROTOBUF_EXPORTS |
(...skipping 27 matching lines...) Expand all Loading... |
106 #ifdef _MSC_VER | 102 #ifdef _MSC_VER |
107 #include <stdlib.h> // NOLINT(build/include) | 103 #include <stdlib.h> // NOLINT(build/include) |
108 #elif defined(__APPLE__) | 104 #elif defined(__APPLE__) |
109 #include <libkern/OSByteOrder.h> | 105 #include <libkern/OSByteOrder.h> |
110 #elif defined(__GLIBC__) || defined(__CYGWIN__) | 106 #elif defined(__GLIBC__) || defined(__CYGWIN__) |
111 #include <byteswap.h> // IWYU pragma: export | 107 #include <byteswap.h> // IWYU pragma: export |
112 #endif | 108 #endif |
113 | 109 |
114 // =================================================================== | 110 // =================================================================== |
115 // from google3/base/port.h | 111 // 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 | |
125 namespace google { | 112 namespace google { |
126 namespace protobuf { | 113 namespace protobuf { |
127 | 114 |
128 typedef unsigned int uint; | 115 typedef unsigned int uint; |
129 | 116 |
130 #ifdef _MSC_VER | 117 #ifdef _MSC_VER |
131 typedef signed __int8 int8; | 118 typedef signed __int8 int8; |
132 typedef __int16 int16; | 119 typedef __int16 int16; |
133 typedef __int32 int32; | 120 typedef __int32 int32; |
134 typedef __int64 int64; | 121 typedef __int64 int64; |
135 | 122 |
136 typedef unsigned __int8 uint8; | 123 typedef unsigned __int8 uint8; |
137 typedef unsigned __int16 uint16; | 124 typedef unsigned __int16 uint16; |
138 typedef unsigned __int32 uint32; | 125 typedef unsigned __int32 uint32; |
139 typedef unsigned __int64 uint64; | 126 typedef unsigned __int64 uint64; |
140 #else | 127 #else |
141 typedef int8_t int8; | 128 typedef int8_t int8; |
142 typedef int16_t int16; | 129 typedef int16_t int16; |
143 typedef int32_t int32; | 130 typedef int32_t int32; |
144 typedef int64_t int64; | 131 typedef int64_t int64; |
145 | 132 |
146 typedef uint8_t uint8; | 133 typedef uint8_t uint8; |
147 typedef uint16_t uint16; | 134 typedef uint16_t uint16; |
148 typedef uint32_t uint32; | 135 typedef uint32_t uint32; |
149 typedef uint64_t uint64; | 136 typedef uint64_t uint64; |
150 #endif | 137 #endif |
151 | 138 |
152 // long long macros to be used because gcc and vc++ use different suffixes, | 139 // long long macros to be used because gcc and vc++ use different suffixes, |
153 // and different size specifiers in format strings | 140 // and different size specifiers in format strings |
154 #undef GOOGLE_LONGLONG | 141 #undef GOOGLE_LONGLONG |
155 #undef GOOGLE_ULONGLONG | 142 #undef GOOGLE_ULONGLONG |
156 #undef GOOGLE_LL_FORMAT | 143 #undef GOOGLE_LL_FORMAT |
157 | 144 |
158 #ifdef _MSC_VER | 145 #ifdef _MSC_VER |
159 #define GOOGLE_LONGLONG(x) x##I64 | 146 #define GOOGLE_LONGLONG(x) x##I64 |
160 #define GOOGLE_ULONGLONG(x) x##UI64 | 147 #define GOOGLE_ULONGLONG(x) x##UI64 |
161 #define GOOGLE_LL_FORMAT "I64" // As in printf("%I64d", ...) | 148 #define GOOGLE_LL_FORMAT "I64" // As in printf("%I64d", ...) |
162 #else | 149 #else |
163 // By long long, we actually mean int64. | |
164 #define GOOGLE_LONGLONG(x) INT64_C(x) | 150 #define GOOGLE_LONGLONG(x) INT64_C(x) |
165 #define GOOGLE_ULONGLONG(x) UINT64_C(x) | 151 #define GOOGLE_ULONGLONG(x) UINT64_C(x) |
166 // Used to format real long long integers. | |
167 #define GOOGLE_LL_FORMAT "ll" // As in "%lld". Note that "q" is poor form also. | 152 #define GOOGLE_LL_FORMAT "ll" // As in "%lld". Note that "q" is poor form also. |
168 #endif | 153 #endif |
169 | 154 |
170 static const int32 kint32max = 0x7FFFFFFF; | 155 static const int32 kint32max = 0x7FFFFFFF; |
171 static const int32 kint32min = -kint32max - 1; | 156 static const int32 kint32min = -kint32max - 1; |
172 static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF); | 157 static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF); |
173 static const int64 kint64min = -kint64max - 1; | 158 static const int64 kint64min = -kint64max - 1; |
174 static const uint32 kuint32max = 0xFFFFFFFFu; | 159 static const uint32 kuint32max = 0xFFFFFFFFu; |
175 static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF); | 160 static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF); |
176 | 161 |
(...skipping 21 matching lines...) Expand all Loading... |
198 #define GOOGLE_ATTRIBUTE_NOINLINE __attribute__ ((noinline)) | 183 #define GOOGLE_ATTRIBUTE_NOINLINE __attribute__ ((noinline)) |
199 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) | 184 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) |
200 // Seems to have been around since at least Visual Studio 2005 | 185 // Seems to have been around since at least Visual Studio 2005 |
201 #define GOOGLE_ATTRIBUTE_NOINLINE __declspec(noinline) | 186 #define GOOGLE_ATTRIBUTE_NOINLINE __declspec(noinline) |
202 #else | 187 #else |
203 // Other compilers will have to figure it out for themselves. | 188 // Other compilers will have to figure it out for themselves. |
204 #define GOOGLE_ATTRIBUTE_NOINLINE | 189 #define GOOGLE_ATTRIBUTE_NOINLINE |
205 #endif | 190 #endif |
206 #endif | 191 #endif |
207 | 192 |
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 | |
217 #ifndef GOOGLE_ATTRIBUTE_DEPRECATED | 193 #ifndef GOOGLE_ATTRIBUTE_DEPRECATED |
218 #ifdef __GNUC__ | 194 #ifdef __GNUC__ |
219 // If the method/variable/type is used anywhere, produce a warning. | 195 // If the method/variable/type is used anywhere, produce a warning. |
220 #define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated)) | 196 #define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated)) |
221 #else | 197 #else |
222 #define GOOGLE_ATTRIBUTE_DEPRECATED | 198 #define GOOGLE_ATTRIBUTE_DEPRECATED |
223 #endif | 199 #endif |
224 #endif | 200 #endif |
225 | 201 |
226 #ifndef GOOGLE_PREDICT_TRUE | 202 #ifndef GOOGLE_PREDICT_TRUE |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 memcpy(p, &v, sizeof v); | 285 memcpy(p, &v, sizeof v); |
310 } | 286 } |
311 #endif | 287 #endif |
312 | 288 |
313 #if defined(_MSC_VER) | 289 #if defined(_MSC_VER) |
314 #define GOOGLE_THREAD_LOCAL __declspec(thread) | 290 #define GOOGLE_THREAD_LOCAL __declspec(thread) |
315 #else | 291 #else |
316 #define GOOGLE_THREAD_LOCAL __thread | 292 #define GOOGLE_THREAD_LOCAL __thread |
317 #endif | 293 #endif |
318 | 294 |
319 // The following guarantees declaration of the byte swap functions. | 295 // The following guarantees declaration of the byte swap functions, and |
| 296 // defines __BYTE_ORDER for MSVC |
320 #ifdef _MSC_VER | 297 #ifdef _MSC_VER |
| 298 #define __BYTE_ORDER __LITTLE_ENDIAN |
321 #define bswap_16(x) _byteswap_ushort(x) | 299 #define bswap_16(x) _byteswap_ushort(x) |
322 #define bswap_32(x) _byteswap_ulong(x) | 300 #define bswap_32(x) _byteswap_ulong(x) |
323 #define bswap_64(x) _byteswap_uint64(x) | 301 #define bswap_64(x) _byteswap_uint64(x) |
324 | 302 |
325 #elif defined(__APPLE__) | 303 #elif defined(__APPLE__) |
326 // Mac OS X / Darwin features | 304 // Mac OS X / Darwin features |
327 #define bswap_16(x) OSSwapInt16(x) | 305 #define bswap_16(x) OSSwapInt16(x) |
328 #define bswap_32(x) OSSwapInt32(x) | 306 #define bswap_32(x) OSSwapInt32(x) |
329 #define bswap_64(x) OSSwapInt64(x) | 307 #define bswap_64(x) OSSwapInt64(x) |
330 | 308 |
(...skipping 18 matching lines...) Expand all Loading... |
349 ((x & GOOGLE_ULONGLONG(0xFF00000000)) >> 8) | | 327 ((x & GOOGLE_ULONGLONG(0xFF00000000)) >> 8) | |
350 ((x & GOOGLE_ULONGLONG(0xFF0000000000)) >> 24) | | 328 ((x & GOOGLE_ULONGLONG(0xFF0000000000)) >> 24) | |
351 ((x & GOOGLE_ULONGLONG(0xFF000000000000)) >> 40) | | 329 ((x & GOOGLE_ULONGLONG(0xFF000000000000)) >> 40) | |
352 ((x & GOOGLE_ULONGLONG(0xFF00000000000000)) >> 56)); | 330 ((x & GOOGLE_ULONGLONG(0xFF00000000000000)) >> 56)); |
353 } | 331 } |
354 #define bswap_64(x) bswap_64(x) | 332 #define bswap_64(x) bswap_64(x) |
355 | 333 |
356 #endif | 334 #endif |
357 | 335 |
358 // =================================================================== | 336 // =================================================================== |
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 // =================================================================== | |
414 // from google3/util/endian/endian.h | 337 // from google3/util/endian/endian.h |
415 LIBPROTOBUF_EXPORT uint32 ghtonl(uint32 x); | 338 LIBPROTOBUF_EXPORT uint32 ghtonl(uint32 x); |
416 | 339 |
417 class BigEndian { | 340 class BigEndian { |
418 public: | 341 public: |
419 #ifdef PROTOBUF_LITTLE_ENDIAN | 342 #ifdef PROTOBUF_LITTLE_ENDIAN |
420 | 343 |
421 static uint16 FromHost16(uint16 x) { return bswap_16(x); } | 344 static uint16 FromHost16(uint16 x) { return bswap_16(x); } |
422 static uint16 ToHost16(uint16 x) { return bswap_16(x); } | 345 static uint16 ToHost16(uint16 x) { return bswap_16(x); } |
423 | 346 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 static void Store64(void *p, uint64 v) { | 391 static void Store64(void *p, uint64 v) { |
469 GOOGLE_UNALIGNED_STORE64(p, FromHost64(v)); | 392 GOOGLE_UNALIGNED_STORE64(p, FromHost64(v)); |
470 } | 393 } |
471 }; | 394 }; |
472 | 395 |
473 | 396 |
474 } // namespace protobuf | 397 } // namespace protobuf |
475 } // namespace google | 398 } // namespace google |
476 | 399 |
477 #endif // GOOGLE_PROTOBUF_STUBS_PORT_H_ | 400 #endif // GOOGLE_PROTOBUF_STUBS_PORT_H_ |
OLD | NEW |