Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: third_party/protobuf/src/google/protobuf/stubs/port.h

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pull whole protobuf Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
31 #ifndef GOOGLE_PROTOBUF_STUBS_PORT_H_
32 #define GOOGLE_PROTOBUF_STUBS_PORT_H_
33
34 #include <assert.h>
35 #include <stdlib.h>
36 #include <cstddef>
37 #include <string>
38 #include <string.h>
39 #if defined(__osf__)
40 // Tru64 lacks stdint.h, but has inttypes.h which defines a superset of
41 // what stdint.h would define.
42 #include <inttypes.h>
43 #elif !defined(_MSC_VER)
44 #include <stdint.h>
45 #endif
46
47 #undef PROTOBUF_LITTLE_ENDIAN
48 #ifdef _WIN32
49 // Assuming windows is always little-endian.
50 // TODO(xiaofeng): The PROTOBUF_LITTLE_ENDIAN is not only used for
51 // optimization but also for correctness. We should define an
52 // different macro to test the big-endian code path in coded_stream.
53 #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
54 #define PROTOBUF_LITTLE_ENDIAN 1
55 #endif
56 #if defined(_MSC_VER) && _MSC_VER >= 1300
57 // If MSVC has "/RTCc" set, it will complain about truncating casts at
58 // runtime. This file contains some intentional truncating casts.
59 #pragma runtime_checks("c", off)
60 #endif
61 #else
62 #include <sys/param.h> // __BYTE_ORDER
63 #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
64 (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \
65 !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
66 #define PROTOBUF_LITTLE_ENDIAN 1
67 #endif
68 #endif
69
70 // The macros defined below are required in order to make protobuf_lite a
71 // component on all platforms. See http://crbug.com/172800.
72 #if defined(COMPONENT_BUILD) && defined(PROTOBUF_USE_DLLS)
73 #if defined(_MSC_VER)
74 #ifdef LIBPROTOBUF_EXPORTS
75 #define LIBPROTOBUF_EXPORT __declspec(dllexport)
76 #else
77 #define LIBPROTOBUF_EXPORT __declspec(dllimport)
78 #endif
79 #ifdef LIBPROTOC_EXPORTS
80 #define LIBPROTOC_EXPORT __declspec(dllexport)
81 #else
82 #define LIBPROTOC_EXPORT __declspec(dllimport)
83 #endif
84 #else // defined(_MSC_VER)
85 #ifdef LIBPROTOBUF_EXPORTS
86 #define LIBPROTOBUF_EXPORT __attribute__((visibility("default")))
87 #else
88 #define LIBPROTOBUF_EXPORT
89 #endif
90 #ifdef LIBPROTOC_EXPORTS
91 #define LIBPROTOC_EXPORT __attribute__((visibility("default")))
92 #else
93 #define LIBPROTOC_EXPORT
94 #endif
95 #endif
96 #else // defined(COMPONENT_BUILD) && defined(PROTOBUF_USE_DLLS)
97 #define LIBPROTOBUF_EXPORT
98 #define LIBPROTOC_EXPORT
99 #endif
100
101 // These #includes are for the byte swap functions declared later on.
102 #ifdef _MSC_VER
103 #include <stdlib.h> // NOLINT(build/include)
104 #elif defined(__APPLE__)
105 #include <libkern/OSByteOrder.h>
106 #elif defined(__GLIBC__) || defined(__CYGWIN__)
107 #include <byteswap.h> // IWYU pragma: export
108 #endif
109
110 // ===================================================================
111 // from google3/base/port.h
112 namespace google {
113 namespace protobuf {
114
115 typedef unsigned int uint;
116
117 #ifdef _MSC_VER
118 typedef signed __int8 int8;
119 typedef __int16 int16;
120 typedef __int32 int32;
121 typedef __int64 int64;
122
123 typedef unsigned __int8 uint8;
124 typedef unsigned __int16 uint16;
125 typedef unsigned __int32 uint32;
126 typedef unsigned __int64 uint64;
127 #else
128 typedef int8_t int8;
129 typedef int16_t int16;
130 typedef int32_t int32;
131 typedef int64_t int64;
132
133 typedef uint8_t uint8;
134 typedef uint16_t uint16;
135 typedef uint32_t uint32;
136 typedef uint64_t uint64;
137 #endif
138
139 // long long macros to be used because gcc and vc++ use different suffixes,
140 // and different size specifiers in format strings
141 #undef GOOGLE_LONGLONG
142 #undef GOOGLE_ULONGLONG
143 #undef GOOGLE_LL_FORMAT
144
145 #ifdef _MSC_VER
146 #define GOOGLE_LONGLONG(x) x##I64
147 #define GOOGLE_ULONGLONG(x) x##UI64
148 #define GOOGLE_LL_FORMAT "I64" // As in printf("%I64d", ...)
149 #else
150 #define GOOGLE_LONGLONG(x) INT64_C(x)
151 #define GOOGLE_ULONGLONG(x) UINT64_C(x)
152 #define GOOGLE_LL_FORMAT "ll" // As in "%lld". Note that "q" is poor form also.
153 #endif
154
155 static const int32 kint32max = 0x7FFFFFFF;
156 static const int32 kint32min = -kint32max - 1;
157 static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF);
158 static const int64 kint64min = -kint64max - 1;
159 static const uint32 kuint32max = 0xFFFFFFFFu;
160 static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF);
161
162 // -------------------------------------------------------------------
163 // Annotations: Some parts of the code have been annotated in ways that might
164 // be useful to some compilers or tools, but are not supported universally.
165 // You can #define these annotations yourself if the default implementation
166 // is not right for you.
167
168 #ifndef GOOGLE_ATTRIBUTE_ALWAYS_INLINE
169 #if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
170 // For functions we want to force inline.
171 // Introduced in gcc 3.1.
172 #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline))
173 #else
174 // Other compilers will have to figure it out for themselves.
175 #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE
176 #endif
177 #endif
178
179 #ifndef GOOGLE_ATTRIBUTE_NOINLINE
180 #if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
181 // For functions we want to force not inline.
182 // Introduced in gcc 3.1.
183 #define GOOGLE_ATTRIBUTE_NOINLINE __attribute__ ((noinline))
184 #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
185 // Seems to have been around since at least Visual Studio 2005
186 #define GOOGLE_ATTRIBUTE_NOINLINE __declspec(noinline)
187 #else
188 // Other compilers will have to figure it out for themselves.
189 #define GOOGLE_ATTRIBUTE_NOINLINE
190 #endif
191 #endif
192
193 #ifndef GOOGLE_ATTRIBUTE_DEPRECATED
194 #ifdef __GNUC__
195 // If the method/variable/type is used anywhere, produce a warning.
196 #define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated))
197 #else
198 #define GOOGLE_ATTRIBUTE_DEPRECATED
199 #endif
200 #endif
201
202 #ifndef GOOGLE_PREDICT_TRUE
203 #ifdef __GNUC__
204 // Provided at least since GCC 3.0.
205 #define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
206 #else
207 #define GOOGLE_PREDICT_TRUE(x) (x)
208 #endif
209 #endif
210
211 #ifndef GOOGLE_PREDICT_FALSE
212 #ifdef __GNUC__
213 // Provided at least since GCC 3.0.
214 #define GOOGLE_PREDICT_FALSE(x) (__builtin_expect(x, 0))
215 #else
216 #define GOOGLE_PREDICT_FALSE(x) (x)
217 #endif
218 #endif
219
220 // Delimits a block of code which may write to memory which is simultaneously
221 // written by other threads, but which has been determined to be thread-safe
222 // (e.g. because it is an idempotent write).
223 #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN
224 #define GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN()
225 #endif
226 #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_END
227 #define GOOGLE_SAFE_CONCURRENT_WRITES_END()
228 #endif
229
230 #if defined(__clang__) && defined(__has_cpp_attribute) \
231 && !defined(GOOGLE_PROTOBUF_OS_APPLE)
232 # if defined(GOOGLE_PROTOBUF_OS_NACL) || defined(EMSCRIPTEN) || \
233 __has_cpp_attribute(clang::fallthrough)
234 # define GOOGLE_FALLTHROUGH_INTENDED [[clang::fallthrough]]
235 # endif
236 #endif
237
238 #ifndef GOOGLE_FALLTHROUGH_INTENDED
239 # define GOOGLE_FALLTHROUGH_INTENDED
240 #endif
241
242 #define GOOGLE_GUARDED_BY(x)
243 #define GOOGLE_ATTRIBUTE_COLD
244
245 // x86 and x86-64 can perform unaligned loads/stores directly.
246 #if defined(_M_X64) || defined(__x86_64__) || \
247 defined(_M_IX86) || defined(__i386__)
248
249 #define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
250 #define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
251 #define GOOGLE_UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p))
252
253 #define GOOGLE_UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_ val))
254 #define GOOGLE_UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_ val))
255 #define GOOGLE_UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_ val))
256
257 #else
258 inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
259 uint16 t;
260 memcpy(&t, p, sizeof t);
261 return t;
262 }
263
264 inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
265 uint32 t;
266 memcpy(&t, p, sizeof t);
267 return t;
268 }
269
270 inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
271 uint64 t;
272 memcpy(&t, p, sizeof t);
273 return t;
274 }
275
276 inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
277 memcpy(p, &v, sizeof v);
278 }
279
280 inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
281 memcpy(p, &v, sizeof v);
282 }
283
284 inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
285 memcpy(p, &v, sizeof v);
286 }
287 #endif
288
289 #if defined(_MSC_VER)
290 #define GOOGLE_THREAD_LOCAL __declspec(thread)
291 #else
292 #define GOOGLE_THREAD_LOCAL __thread
293 #endif
294
295 // The following guarantees declaration of the byte swap functions, and
296 // defines __BYTE_ORDER for MSVC
297 #ifdef _MSC_VER
298 #define __BYTE_ORDER __LITTLE_ENDIAN
299 #define bswap_16(x) _byteswap_ushort(x)
300 #define bswap_32(x) _byteswap_ulong(x)
301 #define bswap_64(x) _byteswap_uint64(x)
302
303 #elif defined(__APPLE__)
304 // Mac OS X / Darwin features
305 #define bswap_16(x) OSSwapInt16(x)
306 #define bswap_32(x) OSSwapInt32(x)
307 #define bswap_64(x) OSSwapInt64(x)
308
309 #elif !defined(__GLIBC__) && !defined(__CYGWIN__)
310
311 static inline uint16 bswap_16(uint16 x) {
312 return static_cast<uint16>(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8));
313 }
314 #define bswap_16(x) bswap_16(x)
315 static inline uint32 bswap_32(uint32 x) {
316 return (((x & 0xFF) << 24) |
317 ((x & 0xFF00) << 8) |
318 ((x & 0xFF0000) >> 8) |
319 ((x & 0xFF000000) >> 24));
320 }
321 #define bswap_32(x) bswap_32(x)
322 static inline uint64 bswap_64(uint64 x) {
323 return (((x & GOOGLE_ULONGLONG(0xFF)) << 56) |
324 ((x & GOOGLE_ULONGLONG(0xFF00)) << 40) |
325 ((x & GOOGLE_ULONGLONG(0xFF0000)) << 24) |
326 ((x & GOOGLE_ULONGLONG(0xFF000000)) << 8) |
327 ((x & GOOGLE_ULONGLONG(0xFF00000000)) >> 8) |
328 ((x & GOOGLE_ULONGLONG(0xFF0000000000)) >> 24) |
329 ((x & GOOGLE_ULONGLONG(0xFF000000000000)) >> 40) |
330 ((x & GOOGLE_ULONGLONG(0xFF00000000000000)) >> 56));
331 }
332 #define bswap_64(x) bswap_64(x)
333
334 #endif
335
336 // ===================================================================
337 // from google3/util/endian/endian.h
338 LIBPROTOBUF_EXPORT uint32 ghtonl(uint32 x);
339
340 class BigEndian {
341 public:
342 #ifdef PROTOBUF_LITTLE_ENDIAN
343
344 static uint16 FromHost16(uint16 x) { return bswap_16(x); }
345 static uint16 ToHost16(uint16 x) { return bswap_16(x); }
346
347 static uint32 FromHost32(uint32 x) { return bswap_32(x); }
348 static uint32 ToHost32(uint32 x) { return bswap_32(x); }
349
350 static uint64 FromHost64(uint64 x) { return bswap_64(x); }
351 static uint64 ToHost64(uint64 x) { return bswap_64(x); }
352
353 static bool IsLittleEndian() { return true; }
354
355 #else
356
357 static uint16 FromHost16(uint16 x) { return x; }
358 static uint16 ToHost16(uint16 x) { return x; }
359
360 static uint32 FromHost32(uint32 x) { return x; }
361 static uint32 ToHost32(uint32 x) { return x; }
362
363 static uint64 FromHost64(uint64 x) { return x; }
364 static uint64 ToHost64(uint64 x) { return x; }
365
366 static bool IsLittleEndian() { return false; }
367
368 #endif /* ENDIAN */
369
370 // Functions to do unaligned loads and stores in big-endian order.
371 static uint16 Load16(const void *p) {
372 return ToHost16(GOOGLE_UNALIGNED_LOAD16(p));
373 }
374
375 static void Store16(void *p, uint16 v) {
376 GOOGLE_UNALIGNED_STORE16(p, FromHost16(v));
377 }
378
379 static uint32 Load32(const void *p) {
380 return ToHost32(GOOGLE_UNALIGNED_LOAD32(p));
381 }
382
383 static void Store32(void *p, uint32 v) {
384 GOOGLE_UNALIGNED_STORE32(p, FromHost32(v));
385 }
386
387 static uint64 Load64(const void *p) {
388 return ToHost64(GOOGLE_UNALIGNED_LOAD64(p));
389 }
390
391 static void Store64(void *p, uint64 v) {
392 GOOGLE_UNALIGNED_STORE64(p, FromHost64(v));
393 }
394 };
395
396
397 } // namespace protobuf
398 } // namespace google
399
400 #endif // GOOGLE_PROTOBUF_STUBS_PORT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698