Coverage Report

Created: 2025-06-19 22:08

/root/doris/be/src/gutil/port.h
Line
Count
Source (jump to first uncovered line)
1
//
2
// Copyright (C) 1999 and onwards Google, Inc.
3
//
4
//
5
// These are weird things we need to do to get this compiling on
6
// random systems (and on SWIG).
7
8
#pragma once
9
10
#include <limits.h> // So we can set the bounds of our types
11
#include <stdlib.h> // for free()
12
#include <string.h> // for memcpy()
13
14
#if defined(__APPLE__)
15
// for getpagesize() on mac
16
#ifndef _POSIX_C_SOURCE
17
#include <unistd.h>
18
#else
19
#include <mach/vm_page_size.h>
20
#endif
21
22
#elif defined(OS_CYGWIN)
23
#include <malloc.h> // for memalign()
24
#endif
25
26
#include "gutil/integral_types.h"
27
28
// Must happens before inttypes.h inclusion */
29
#if defined(__APPLE__)
30
/* From MacOSX's inttypes.h:
31
 * "C++ implementations should define these macros only when
32
 *  __STDC_FORMAT_MACROS is defined before <inttypes.h> is included." */
33
#ifndef __STDC_FORMAT_MACROS
34
#define __STDC_FORMAT_MACROS
35
#endif /* __STDC_FORMAT_MACROS */
36
#endif /* __APPLE__ */
37
38
/* Default for most OSes */
39
/* We use SIGPWR since that seems unlikely to be used for other reasons. */
40
#define GOOGLE_OBSCURE_SIGNAL SIGPWR
41
42
#if defined OS_LINUX || defined OS_CYGWIN
43
44
// _BIG_ENDIAN
45
#include <endian.h>
46
47
// The uint mess:
48
// mysql.h sets _GNU_SOURCE which sets __USE_MISC in <features.h>
49
// sys/types.h typedefs uint if __USE_MISC
50
// mysql typedefs uint if HAVE_UINT not set
51
// The following typedef is carefully considered, and should not cause
52
//  any clashes
53
#if !defined(__USE_MISC)
54
#if !defined(HAVE_UINT)
55
#define HAVE_UINT 1
56
typedef unsigned int uint;
57
#endif
58
#if !defined(HAVE_USHORT)
59
#define HAVE_USHORT 1
60
typedef unsigned short ushort;
61
#endif
62
#if !defined(HAVE_ULONG)
63
#define HAVE_ULONG 1
64
typedef unsigned long ulong;
65
#endif
66
#endif
67
68
#if defined(__cplusplus)
69
#include <cstddef> // For _GLIBCXX macros
70
#endif
71
72
#if !defined(HAVE_TLS) && defined(_GLIBCXX_HAVE_TLS) && defined(__x86_64__)
73
#define HAVE_TLS 1
74
#endif
75
76
#elif defined OS_FREEBSD
77
78
// _BIG_ENDIAN
79
#include <machine/endian.h>
80
81
#elif defined OS_SOLARIS
82
83
// _BIG_ENDIAN
84
#include <sys/isa_defs.h>
85
86
// Solaris doesn't define sig_t (function taking an int, returning void)
87
typedef void (*sig_t)(int);
88
89
// Solaris only defines strtoll, not strtoq
90
#define strtoq strtoll
91
#define strtouq strtoull
92
93
// It doesn't define the posix-standard(?) u_int_16
94
#include <sys/int_types.h> // NOLINT(build/include)
95
typedef uint16_t u_int16_t;
96
97
#elif defined __APPLE__
98
99
// BIG_ENDIAN
100
#include <machine/endian.h> // NOLINT(build/include)
101
/* Let's try and follow the Linux convention */
102
#define __BYTE_ORDER BYTE_ORDER
103
#define __LITTLE_ENDIAN LITTLE_ENDIAN
104
#define __BIG_ENDIAN BIG_ENDIAN
105
106
#endif
107
108
// The following guarenty declaration of the byte swap functions, and
109
// define __BYTE_ORDER for MSVC
110
#ifdef _MSC_VER
111
#include <stdlib.h> // NOLINT(build/include)
112
#define __BYTE_ORDER __LITTLE_ENDIAN
113
#define bswap_16(x) _byteswap_ushort(x)
114
#define bswap_32(x) _byteswap_ulong(x)
115
#define bswap_64(x) _byteswap_uint64(x)
116
117
#elif defined(__APPLE__)
118
// Mac OS X / Darwin features
119
#include <libkern/OSByteOrder.h>
120
#define bswap_16(x) OSSwapInt16(x)
121
#define bswap_32(x) OSSwapInt32(x)
122
#define bswap_64(x) OSSwapInt64(x)
123
124
#elif defined(__GLIBC__)
125
#include <byteswap.h> // IWYU pragma: export
126
127
#else
128
129
static inline uint16 bswap_16(uint16 x) {
130
    return ((x & 0xFF) << 8) | ((x & 0xFF00) >> 8);
131
}
132
#define bswap_16(x) bswap_16(x)
133
static inline uint32 bswap_32(uint32 x) {
134
    return (((x & 0xFF) << 24) | ((x & 0xFF00) << 8) | ((x & 0xFF0000) >> 8) |
135
            ((x & 0xFF000000) >> 24));
136
}
137
#define bswap_32(x) bswap_32(x)
138
static inline uint64 bswap_64(uint64 x) {
139
    return (((x & GG_ULONGLONG(0xFF)) << 56) | ((x & GG_ULONGLONG(0xFF00)) << 40) |
140
            ((x & GG_ULONGLONG(0xFF0000)) << 24) | ((x & GG_ULONGLONG(0xFF000000)) << 8) |
141
            ((x & GG_ULONGLONG(0xFF00000000)) >> 8) | ((x & GG_ULONGLONG(0xFF0000000000)) >> 24) |
142
            ((x & GG_ULONGLONG(0xFF000000000000)) >> 40) |
143
            ((x & GG_ULONGLONG(0xFF00000000000000)) >> 56));
144
}
145
#define bswap_64(x) bswap_64(x)
146
147
#endif
148
149
// define the macros IS_LITTLE_ENDIAN or IS_BIG_ENDIAN
150
// using the above endian definitions from endian.h if
151
// endian.h was included
152
#ifdef __BYTE_ORDER
153
#if __BYTE_ORDER == __LITTLE_ENDIAN
154
#define IS_LITTLE_ENDIAN
155
#endif
156
157
#if __BYTE_ORDER == __BIG_ENDIAN
158
#define IS_BIG_ENDIAN
159
#endif
160
161
#else
162
163
#if defined(__LITTLE_ENDIAN__)
164
#define IS_LITTLE_ENDIAN
165
#elif defined(__BIG_ENDIAN__)
166
#define IS_BIG_ENDIAN
167
#endif
168
169
// there is also PDP endian ...
170
171
#endif // __BYTE_ORDER
172
173
// Define the OS's path separator
174
#ifdef __cplusplus // C won't merge duplicate const variables at link time
175
// Some headers provide a macro for this (GCC's system.h), remove it so that we
176
// can use our own.
177
#undef PATH_SEPARATOR
178
#if defined(OS_WINDOWS)
179
const char PATH_SEPARATOR = '\\';
180
#else
181
const char PATH_SEPARATOR = '/';
182
#endif
183
#endif
184
185
// Windows has O_BINARY as a flag to open() (like "b" for fopen).
186
// Linux doesn't need make this distinction.
187
#if defined OS_LINUX && !defined O_BINARY
188
#define O_BINARY 0
189
#endif
190
191
// va_copy portability definitions
192
#ifdef _MSC_VER
193
// MSVC doesn't have va_copy yet.
194
// This is believed to work for 32-bit msvc.  This may not work at all for
195
// other platforms.
196
// If va_list uses the single-element-array trick, you will probably get
197
// a compiler error here.
198
//
199
#include <stdarg.h>
200
void va_copy(va_list& a, va_list& b) {
201
    a = b;
202
}
203
204
// Nor does it have uid_t
205
typedef int uid_t;
206
207
#endif
208
209
// Mac OS X / Darwin features
210
211
#if defined(__APPLE__)
212
213
// For mmap, Linux defines both MAP_ANONYMOUS and MAP_ANON and says MAP_ANON is
214
// deprecated. In Darwin, MAP_ANON is all there is.
215
#if !defined MAP_ANONYMOUS
216
#define MAP_ANONYMOUS MAP_ANON
217
#endif
218
219
// Linux has this in <sys/cdefs.h>
220
#define __ptr_t void*
221
222
// Linux has this in <linux/errno.h>
223
#define EXFULL ENOMEM // not really that great a translation...
224
225
// Darwin doesn't have strnlen. No comment.
226
inline size_t strnlen(const char* s, size_t maxlen) {
227
    const char* end = (const char*)memchr(s, '\0', maxlen);
228
    if (end) return end - s;
229
    return maxlen;
230
}
231
232
namespace std {}     // namespace std
233
using namespace std; // Just like VC++, we need a using here.
234
235
// Doesn't exist on OSX; used in google.cc for send() to mean "no flags".
236
#ifndef MSG_NOSIGNAL
237
#define MSG_NOSIGNAL 0
238
#endif
239
240
// No SIGPWR on MacOSX.  SIGINFO seems suitably obscure.
241
#undef GOOGLE_OBSCURE_SIGNAL
242
#define GOOGLE_OBSCURE_SIGNAL SIGINFO
243
244
#elif defined(OS_CYGWIN) // Cygwin-specific behavior.
245
246
#if defined(__CYGWIN32__)
247
#define __WORDSIZE 32
248
#else
249
// It's probably possible to support 64-bit, but the #defines will need checked.
250
#error "Cygwin is currently only 32-bit."
251
#endif
252
253
// No signalling on Windows.
254
#undef GOOGLE_OBSCURE_SIGNAL
255
#define GOOGLE_OBSCURE_SIGNAL 0
256
257
struct stack_t {
258
    void* ss_sp = nullptr;
259
    int ss_flags;
260
    size_t ss_size;
261
};
262
inline int sigaltstack(stack_t* ss, stack_t* oss) {
263
    return 0;
264
}
265
266
#define PTHREAD_STACK_MIN 0 // Not provided by cygwin
267
268
// Scans memory for a character.
269
// memrchr is used in a few places, but it's linux-specific.
270
inline void* memrchr(const void* bytes, int find_char, size_t len) {
271
    const unsigned char* cursor = reinterpret_cast<const unsigned char*>(bytes) + len - 1;
272
    unsigned char actual_char = find_char;
273
    for (; cursor >= bytes; --cursor) {
274
        if (*cursor == actual_char) {
275
            return const_cast<void*>(reinterpret_cast<const void*>(cursor));
276
        }
277
    }
278
    return NULL;
279
}
280
281
#endif
282
283
// Klocwork static analysis tool's C/C++ compiler kwcc
284
#if defined(__KLOCWORK__)
285
#define STATIC_ANALYSIS
286
#endif // __KLOCWORK__
287
288
// GCC-specific features
289
290
#if (defined(__GNUC__) || defined(__APPLE__)) && !defined(SWIG)
291
292
//
293
// Tell the compiler to do printf format string checking if the
294
// compiler supports it; see the 'format' attribute in
295
// <http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html>.
296
//
297
// N.B.: As the GCC manual states, "[s]ince non-static C++ methods
298
// have an implicit 'this' argument, the arguments of such methods
299
// should be counted from two, not one."
300
//
301
#define PRINTF_ATTRIBUTE(string_index, first_to_check) \
302
    __attribute__((__format__(__printf__, string_index, first_to_check)))
303
#define SCANF_ATTRIBUTE(string_index, first_to_check) \
304
    __attribute__((__format__(__scanf__, string_index, first_to_check)))
305
306
//
307
// Prevent the compiler from padding a structure to natural alignment
308
//
309
#define PACKED __attribute__((packed))
310
311
// Cache line alignment
312
#if defined(__i386__) || defined(__x86_64__)
313
#define CACHELINE_SIZE 64
314
#elif defined(__powerpc64__)
315
// TODO(user) This is the L1 D-cache line size of our Power7 machines.
316
// Need to check if this is appropriate for other PowerPC64 systems.
317
#define CACHELINE_SIZE 128
318
#elif defined(__arm__)
319
// Cache line sizes for ARM: These values are not strictly correct since
320
// cache line sizes depend on implementations, not architectures.  There
321
// are even implementations with cache line sizes configurable at boot
322
// time.
323
#if defined(__ARM_ARCH_5T__)
324
#define CACHELINE_SIZE 32
325
#elif defined(__ARM_ARCH_7A__)
326
#define CACHELINE_SIZE 64
327
#endif
328
#endif
329
330
// This is a NOP if CACHELINE_SIZE is not defined.
331
#ifdef CACHELINE_SIZE
332
#define CACHELINE_ALIGNED __attribute__((aligned(CACHELINE_SIZE)))
333
#else
334
#define CACHELINE_ALIGNED
335
#endif
336
337
//
338
// Prevent the compiler from complaining about or optimizing away variables
339
// that appear unused
340
// (careful, others e.g. third_party/libxml/xmlversion.h also define this)
341
#undef ATTRIBUTE_UNUSED
342
#define ATTRIBUTE_UNUSED __attribute__((unused))
343
344
// Same as above, but for class members.
345
// As of 10/2013 this appears to only be supported in Clang/LLVM.
346
// See http://patchwork.ozlabs.org/patch/232594/ which is not yet committed
347
// in gcc trunk.
348
#if defined(__llvm__)
349
#define ATTRIBUTE_MEMBER_UNUSED ATTRIBUTE_UNUSED
350
#else
351
#define ATTRIBUTE_MEMBER_UNUSED
352
#endif
353
354
// For weak functions
355
#undef ATTRIBUTE_WEAK
356
#define ATTRIBUTE_WEAK __attribute__((weak))
357
#define HAVE_ATTRIBUTE_WEAK 1
358
359
// For deprecated functions or variables, generate a warning at usage sites.
360
// Verified to work as early as GCC 3.1.1 and clang 3.2 (so we'll assume any
361
// clang is new enough).
362
#if defined(__clang__) || \
363
        (defined(COMPILER_GCC) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 30200)
364
#define ATTRIBUTE_DEPRECATED(msg) __attribute__((deprecated(msg)))
365
#else
366
#define ATTRIBUTE_DEPRECATED(msg)
367
#endif
368
369
// Tell the compiler to use "initial-exec" mode for a thread-local variable.
370
// See http://people.redhat.com/drepper/tls.pdf for the gory details.
371
#define ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec")))
372
373
//
374
// Tell the compiler that some function parameters should be non-null pointers.
375
// Note: As the GCC manual states, "[s]ince non-static C++ methods
376
// have an implicit 'this' argument, the arguments of such methods
377
// should be counted from two, not one."
378
//
379
#define ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index)))
380
381
//
382
// Tell the compiler that a given function never returns
383
//
384
#define ATTRIBUTE_NORETURN __attribute__((noreturn))
385
386
// Tell AddressSanitizer (or other memory testing tools) to ignore a given
387
// function. Useful for cases when a function reads random locations on stack,
388
// calls _exit from a cloned subprocess, deliberately accesses buffer
389
// out of bounds or does other scary things with memory.
390
#ifdef ADDRESS_SANITIZER
391
#define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS __attribute__((no_address_safety_analysis))
392
#else
393
#define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS
394
#endif
395
396
// Tell ThreadSanitizer to ignore a given function. This can dramatically reduce
397
// the running time and memory requirements for racy code when TSAN is active.
398
// GCC does not support this attribute at the time of this writing (GCC 4.8).
399
#if defined(__llvm__)
400
#define ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
401
#else
402
#define ATTRIBUTE_NO_SANITIZE_THREAD
403
#endif
404
405
#ifndef HAVE_ATTRIBUTE_SECTION // may have been pre-set to 0, e.g. for Darwin
406
#define HAVE_ATTRIBUTE_SECTION 1
407
#endif
408
409
//
410
// The legacy prod71 libc does not provide the stack alignment required for use
411
// of SSE intrinsics.  In order to properly use the intrinsics you need to use
412
// a trampoline function which aligns the stack prior to calling your code,
413
// or as of crosstool v10 with gcc 4.2.0 there is an attribute which asks
414
// gcc to do this for you.
415
//
416
// It has also been discovered that crosstool up to and including v10 does not
417
// provide proper alignment for pthread_once() functions in x86-64 code either.
418
// Unfortunately gcc does not provide force_align_arg_pointer as an option in
419
// x86-64 code, so this requires us to always have a trampoline.
420
//
421
// For an example of using this see util/hash/adler32*
422
423
#if defined(__i386__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
424
#define ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC __attribute__((force_align_arg_pointer))
425
#define REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
426
#elif defined(__i386__) || defined(__x86_64__)
427
#define REQUIRE_STACK_ALIGN_TRAMPOLINE (1)
428
#define ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
429
#else
430
#define REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
431
#define ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
432
#endif
433
434
//
435
// Tell the compiler to warn about unused return values for functions declared
436
// with this macro.  The macro should be used on function declarations
437
// following the argument list:
438
//
439
//   Sprocket* AllocateSprocket() MUST_USE_RESULT;
440
//
441
#if defined(SWIG)
442
#define MUST_USE_RESULT
443
#elif __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
444
#define MUST_USE_RESULT __attribute__((warn_unused_result))
445
#else
446
#define MUST_USE_RESULT
447
#endif
448
449
#if defined(__GNUC__)
450
// Defined behavior on some of the uarchs:
451
// PREFETCH_HINT_T0:
452
//   prefetch to all levels of the hierarchy (except on p4: prefetch to L2)
453
// PREFETCH_HINT_NTA:
454
//   p4: fetch to L2, but limit to 1 way (out of the 8 ways)
455
//   core: skip L2, go directly to L1
456
//   k8 rev E and later: skip L2, can go to either of the 2-ways in L1
457
enum PrefetchHint {
458
    PREFETCH_HINT_T0 = 3, // More temporal locality
459
    PREFETCH_HINT_T1 = 2,
460
    PREFETCH_HINT_T2 = 1, // Less temporal locality
461
    PREFETCH_HINT_NTA = 0 // No temporal locality
462
};
463
#else
464
// prefetch is a no-op for this target. Feel free to add more sections above.
465
#endif
466
467
0
extern inline void prefetch(const char* x, int hint) {
468
0
#if defined(__llvm__)
469
0
    // In the gcc version of prefetch(), hint is only a constant _after_ inlining
470
0
    // (assumed to have been successful).  llvm views things differently, and
471
0
    // checks constant-ness _before_ inlining.  This leads to compilation errors
472
0
    // with using the other version of this code with llvm.
473
0
    //
474
0
    // One way round this is to use a switch statement to explicitly match
475
0
    // prefetch hint enumerations, and invoke __builtin_prefetch for each valid
476
0
    // value.  llvm's optimization removes the switch and unused case statements
477
0
    // after inlining, so that this boils down in the end to the same as for gcc;
478
0
    // that is, a single inlined prefetchX instruction.
479
0
    //
480
0
    // Note that this version of prefetch() cannot verify constant-ness of hint.
481
0
    // If client code calls prefetch() with a variable value for hint, it will
482
0
    // receive the full expansion of the switch below, perhaps also not inlined.
483
0
    // This should however not be a problem in the general case of well behaved
484
0
    // caller code that uses the supplied prefetch hint enumerations.
485
0
    switch (hint) {
486
0
    case PREFETCH_HINT_T0:
487
0
        __builtin_prefetch(x, 0, PREFETCH_HINT_T0);
488
0
        break;
489
0
    case PREFETCH_HINT_T1:
490
0
        __builtin_prefetch(x, 0, PREFETCH_HINT_T1);
491
0
        break;
492
0
    case PREFETCH_HINT_T2:
493
0
        __builtin_prefetch(x, 0, PREFETCH_HINT_T2);
494
0
        break;
495
0
    case PREFETCH_HINT_NTA:
496
0
        __builtin_prefetch(x, 0, PREFETCH_HINT_NTA);
497
0
        break;
498
0
    default:
499
0
        __builtin_prefetch(x);
500
0
        break;
501
0
    }
502
0
#elif defined(__GNUC__)
503
0
#if !defined(__i386) || defined(__SSE__)
504
0
    if (__builtin_constant_p(hint)) {
505
0
        __builtin_prefetch(x, 0, hint);
506
0
    } else {
507
0
        // Defaults to PREFETCH_HINT_T0
508
0
        __builtin_prefetch(x);
509
0
    }
510
0
#else
511
0
    // We want a __builtin_prefetch, but we build with the default -march=i386
512
0
    // where __builtin_prefetch quietly turns into nothing.
513
0
    // Once we crank up to -march=pentium3 or higher the __SSE__
514
0
    // clause above will kick in with the builtin.
515
0
    // -- mec 2006-06-06
516
0
    if (hint == PREFETCH_HINT_NTA) __asm__ __volatile__("prefetchnta (%0)" : : "r"(x));
517
0
#endif
518
0
#else
519
0
    // You get no effect.  Feel free to add more sections above.
520
0
#endif
521
0
}
522
523
#ifdef __cplusplus
524
// prefetch intrinsic (bring data to L1 without polluting L2 cache)
525
0
extern inline void prefetch(const char* x) {
526
0
    return prefetch(x, 0);
527
0
}
528
#endif // ifdef __cplusplus
529
530
//
531
// Tell GCC that a function is hot or cold. GCC can use this information to
532
// improve static analysis, i.e. a conditional branch to a cold function
533
// is likely to be not-taken.
534
// This annotation is used for function declarations, e.g.:
535
//   int foo() ATTRIBUTE_HOT;
536
//
537
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
538
#define ATTRIBUTE_HOT __attribute__((hot))
539
#define ATTRIBUTE_COLD __attribute__((cold))
540
#else
541
#define ATTRIBUTE_HOT
542
#define ATTRIBUTE_COLD
543
#endif
544
545
#define FTELLO ftello
546
#define FSEEKO fseeko
547
548
#if !defined(__cplusplus) && !defined(__APPLE__) && !defined(OS_CYGWIN)
549
// stdlib.h only declares this in C++, not in C, so we declare it here.
550
// Also make sure to avoid declaring it on platforms which don't support it.
551
extern int posix_memalign(void** memptr, size_t alignment, size_t size);
552
#endif
553
554
0
inline void* aligned_malloc(size_t size, int minimum_alignment) {
555
0
#if defined(__APPLE__)
556
0
    // mac lacks memalign(), posix_memalign(), however, according to
557
0
    // http://stackoverflow.com/questions/196329/osx-lacks-memalign
558
0
    // mac allocs are already 16-byte aligned.
559
0
    if (minimum_alignment <= 16) return malloc(size);
560
0
// next, try to return page-aligned memory. perhaps overkill
561
0
#ifndef _POSIX_C_SOURCE
562
0
    int page_size = getpagesize();
563
0
#else
564
0
    int page_size = vm_page_size;
565
0
#endif
566
0
    if (minimum_alignment <= page_size) return valloc(size);
567
0
    // give up
568
0
    return NULL;
569
0
#elif defined(OS_CYGWIN)
570
0
    return memalign(minimum_alignment, size);
571
0
#else // !__APPLE__ && !OS_CYGWIN
572
0
    void* ptr = NULL;
573
0
    if (posix_memalign(&ptr, minimum_alignment, size) != 0)
574
0
        return NULL;
575
0
    else
576
0
        return ptr;
577
0
#endif
578
0
}
579
580
#else // not GCC
581
582
#define PRINTF_ATTRIBUTE(string_index, first_to_check)
583
#define SCANF_ATTRIBUTE(string_index, first_to_check)
584
#define PACKED
585
#define CACHELINE_ALIGNED
586
#define ATTRIBUTE_UNUSED
587
#define ATTRIBUTE_ALWAYS_INLINE
588
#define ATTRIBUTE_NOINLINE
589
#define ATTRIBUTE_HOT
590
#define ATTRIBUTE_COLD
591
#define ATTRIBUTE_WEAK
592
#define HAVE_ATTRIBUTE_WEAK 0
593
#define ATTRIBUTE_INITIAL_EXEC
594
#define ATTRIBUTE_NONNULL(arg_index)
595
#define ATTRIBUTE_NORETURN
596
#define ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
597
#define REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
598
#define MUST_USE_RESULT
599
extern inline void prefetch(const char* x) {}
600
601
// These should be redefined appropriately if better alternatives to
602
// ftell/fseek exist in the compiler
603
#define FTELLO ftell
604
#define FSEEKO fseek
605
606
#endif // GCC
607
608
//
609
// Provides a char array with the exact same alignment as another type. The
610
// first parameter must be a complete type, the second parameter is how many
611
// of that type to provide space for.
612
//
613
//   ALIGNED_CHAR_ARRAY(struct stat, 16) storage_;
614
//
615
#if defined(__cplusplus)
616
#undef ALIGNED_CHAR_ARRAY
617
// Because MSVC and older GCCs require that the argument to their alignment
618
// construct to be a literal constant integer, we use a template instantiated
619
// at all the possible powers of two.
620
#ifndef SWIG
621
template <int alignment, int size>
622
struct AlignType {};
623
template <int size>
624
struct AlignType<0, size> {
625
    typedef char result[size];
626
};
627
#if defined(_MSC_VER)
628
#define BASE_PORT_H_ALIGN_ATTRIBUTE(X) __declspec(align(X))
629
#define BASE_PORT_H_ALIGN_OF(T) __alignof(T)
630
#elif defined(__GNUC__)
631
#define BASE_PORT_H_ALIGN_ATTRIBUTE(X) __attribute__((aligned(X)))
632
#define BASE_PORT_H_ALIGN_OF(T) __alignof__(T)
633
#endif
634
635
#if defined(BASE_PORT_H_ALIGN_ATTRIBUTE)
636
637
#define BASE_PORT_H_ALIGNTYPE_TEMPLATE(X)                         \
638
    template <int size>                                           \
639
    struct AlignType<X, size> {                                   \
640
        typedef BASE_PORT_H_ALIGN_ATTRIBUTE(X) char result[size]; \
641
    }
642
643
BASE_PORT_H_ALIGNTYPE_TEMPLATE(1);
644
BASE_PORT_H_ALIGNTYPE_TEMPLATE(2);
645
BASE_PORT_H_ALIGNTYPE_TEMPLATE(4);
646
BASE_PORT_H_ALIGNTYPE_TEMPLATE(8);
647
BASE_PORT_H_ALIGNTYPE_TEMPLATE(16);
648
BASE_PORT_H_ALIGNTYPE_TEMPLATE(32);
649
BASE_PORT_H_ALIGNTYPE_TEMPLATE(64);
650
BASE_PORT_H_ALIGNTYPE_TEMPLATE(128);
651
BASE_PORT_H_ALIGNTYPE_TEMPLATE(256);
652
BASE_PORT_H_ALIGNTYPE_TEMPLATE(512);
653
BASE_PORT_H_ALIGNTYPE_TEMPLATE(1024);
654
BASE_PORT_H_ALIGNTYPE_TEMPLATE(2048);
655
BASE_PORT_H_ALIGNTYPE_TEMPLATE(4096);
656
BASE_PORT_H_ALIGNTYPE_TEMPLATE(8192);
657
// Any larger and MSVC++ will complain.
658
659
#define ALIGNED_CHAR_ARRAY(T, Size) \
660
    typename AlignType<BASE_PORT_H_ALIGN_OF(T), sizeof(T) * Size>::result
661
662
#undef BASE_PORT_H_ALIGNTYPE_TEMPLATE
663
#undef BASE_PORT_H_ALIGN_ATTRIBUTE
664
665
#else // defined(BASE_PORT_H_ALIGN_ATTRIBUTE)
666
#define ALIGNED_CHAR_ARRAY you_must_define_ALIGNED_CHAR_ARRAY_for_your_compiler_in_base_port_h
667
#endif // defined(BASE_PORT_H_ALIGN_ATTRIBUTE)
668
669
#else // !SWIG
670
671
// SWIG can't represent alignment and doesn't care about alignment on data
672
// members (it works fine without it).
673
template <typename Size>
674
struct AlignType {
675
    typedef char result[Size];
676
};
677
#define ALIGNED_CHAR_ARRAY(T, Size) AlignType<Size * sizeof(T)>::result
678
679
#endif // !SWIG
680
#else  // __cpluscplus
681
#define ALIGNED_CHAR_ARRAY ALIGNED_CHAR_ARRAY_is_not_available_without_Cplusplus
682
#endif // __cplusplus
683
684
#ifdef _MSC_VER /* if Visual C++ */
685
686
// This compiler flag can be easily overlooked on MSVC.
687
// _CHAR_UNSIGNED gets set with the /J flag.
688
#ifndef _CHAR_UNSIGNED
689
#error chars must be unsigned!  Use the /J flag on the compiler command line.
690
#endif
691
692
// MSVC is a little hyper-active in its warnings
693
// Signed vs. unsigned comparison is ok.
694
#pragma warning(disable : 4018)
695
// We know casting from a long to a char may lose data
696
#pragma warning(disable : 4244)
697
// Don't need performance warnings about converting ints to bools
698
#pragma warning(disable : 4800)
699
// Integral constant overflow is apparently ok too
700
// for example:
701
//  short k;  int n;
702
//  k = k + n;
703
#pragma warning(disable : 4307)
704
// It's ok to use this* in constructor
705
// Example:
706
//  class C {
707
//   Container cont_;
708
//   C() : cont_(this) { ...
709
#pragma warning(disable : 4355)
710
// Truncating from double to float is ok
711
#pragma warning(disable : 4305)
712
713
#include <assert.h>
714
#include <windows.h>
715
#include <winsock2.h>
716
#undef ERROR
717
718
#include <float.h> // for nextafter functionality on windows
719
#include <math.h>  // for HUGE_VAL
720
721
#ifndef HUGE_VALF
722
#define HUGE_VALF (static_cast<float>(HUGE_VAL))
723
#endif
724
725
namespace std {} // namespace std
726
using namespace std;
727
728
// VC++ doesn't understand "uint"
729
#ifndef HAVE_UINT
730
#define HAVE_UINT 1
731
typedef unsigned int uint;
732
#endif
733
734
// VC++ doesn't understand "ssize_t"
735
#ifndef HAVE_SSIZET
736
#define HAVE_SSIZET 1
737
// The following correctly defines ssize_t on most (all?) VC++ versions:
738
//   #include <BaseTsd.h>
739
//   typedef SSIZE_T ssize_t;
740
// However, several projects in googleclient already use plain 'int', e.g.,
741
//   googleclient/posix/unistd.h
742
//   googleclient/earth/client/libs/base/types.h
743
// so to avoid conflicts with those definitions, we do the same here.
744
typedef int ssize_t;
745
#endif
746
747
#define strtoq _strtoi64
748
#define strtouq _strtoui64
749
#define strtoll _strtoi64
750
#define strtoull _strtoui64
751
#define atoll _atoi64
752
753
// VC++ 6 and before ship without an ostream << operator for 64-bit ints
754
#if (_MSC_VER <= 1200)
755
#include <iosfwd>
756
using std::ostream;
757
inline ostream& operator<<(ostream& os, const unsigned __int64& num) {
758
    // Fake operator; doesn't actually do anything.
759
    LOG(FATAL) << "64-bit ostream operator << not supported in VC++ 6";
760
    return os;
761
}
762
#endif
763
764
// You say tomato, I say atotom
765
#define PATH_MAX MAX_PATH
766
767
// You say tomato, I say _tomato
768
#define vsnprintf _vsnprintf
769
#define snprintf _snprintf
770
#define strcasecmp _stricmp
771
#define strncasecmp _strnicmp
772
773
#define nextafter _nextafter
774
775
#define hypot _hypot
776
#define hypotf _hypotf
777
778
#define strdup _strdup
779
#define tempnam _tempnam
780
#define chdir _chdir
781
#define getcwd _getcwd
782
#define putenv _putenv
783
784
// You say tomato, I say toma
785
#define random() rand()
786
#define srandom(x) srand(x)
787
788
// You say juxtapose, I say transpose
789
#define bcopy(s, d, n) memcpy(d, s, n)
790
791
inline void* aligned_malloc(size_t size, int minimum_alignment) {
792
    return _aligned_malloc(size, minimum_alignment);
793
}
794
795
// ----- BEGIN VC++ STUBS & FAKE DEFINITIONS ---------------------------------
796
797
// See http://en.wikipedia.org/wiki/IEEE_754 for details of
798
// floating point format.
799
800
enum {
801
    FP_NAN,      //  is "Not a Number"
802
    FP_INFINITE, //  is either plus or minus infinity.
803
    FP_ZERO,
804
    FP_SUBNORMAL, // is too small to be represented in normalized format.
805
    FP_NORMAL     // if nothing of the above is correct that it must be a
806
                  // normal floating-point number.
807
};
808
809
inline int fpclassify_double(double x) {
810
    const int float_point_class = _fpclass(x);
811
    int c99_class;
812
    switch (float_point_class) {
813
    case _FPCLASS_SNAN: // Signaling NaN
814
    case _FPCLASS_QNAN: // Quiet NaN
815
        c99_class = FP_NAN;
816
        break;
817
    case _FPCLASS_NZ: // Negative zero ( -0)
818
    case _FPCLASS_PZ: // Positive 0 (+0)
819
        c99_class = FP_ZERO;
820
        break;
821
    case _FPCLASS_NINF: // Negative infinity ( -INF)
822
    case _FPCLASS_PINF: // Positive infinity (+INF)
823
        c99_class = FP_INFINITE;
824
        break;
825
    case _FPCLASS_ND: // Negative denormalized
826
    case _FPCLASS_PD: // Positive denormalized
827
        c99_class = FP_SUBNORMAL;
828
        break;
829
    case _FPCLASS_NN: // Negative normalized non-zero
830
    case _FPCLASS_PN: // Positive normalized non-zero
831
        c99_class = FP_NORMAL;
832
        break;
833
    default:
834
        c99_class = FP_NAN; // Should never happen
835
        break;
836
    }
837
    return c99_class;
838
}
839
840
// This function handle the special subnormal case for float; it will
841
// become a normal number while casting to double.
842
// bit_cast is avoided to simplify dependency and to create a code that is
843
// easy to deploy in C code
844
inline int fpclassify_float(float x) {
845
    uint32 bitwise_representation;
846
    memcpy(&bitwise_representation, &x, 4);
847
    if ((bitwise_representation & 0x7f800000) == 0 && (bitwise_representation & 0x007fffff) != 0)
848
        return FP_SUBNORMAL;
849
    return fpclassify_double(x);
850
}
851
//
852
// This define takes care of the denormalized float; the casting to
853
// double make it a normal number
854
#define fpclassify(x) ((sizeof(x) == sizeof(float)) ? fpclassify_float(x) : fpclassify_double(x))
855
856
#define isnan _isnan
857
858
inline int isinf(double x) {
859
    const int float_point_class = _fpclass(x);
860
    if (float_point_class == _FPCLASS_PINF) return 1;
861
    if (float_point_class == _FPCLASS_NINF) return -1;
862
    return 0;
863
}
864
865
// #include "conflict-signal.h"
866
typedef void (*sig_t)(int);
867
868
// These actually belong in errno.h but there's a name confilict in errno
869
// on WinNT. They (and a ton more) are also found in Winsock2.h, but
870
// if'd out under NT. We need this subset at minimum.
871
#define EXFULL ENOMEM // not really that great a translation...
872
// The following are already defined in VS2010.
873
#if (_MSC_VER < 1600)
874
#define EWOULDBLOCK WSAEWOULDBLOCK
875
#ifndef PTHREADS_REDHAT_WIN32
876
#define ETIMEDOUT WSAETIMEDOUT
877
#endif
878
#define ENOTSOCK WSAENOTSOCK
879
#define EINPROGRESS WSAEINPROGRESS
880
#define ECONNRESET WSAECONNRESET
881
#endif
882
883
//
884
// Really from <string.h>
885
//
886
887
inline void bzero(void* s, int n) {
888
    memset(s, 0, n);
889
}
890
891
// From glob.h
892
#define __ptr_t void*
893
894
// Defined all over the place.
895
typedef int pid_t;
896
897
// From stat.h
898
typedef unsigned int mode_t;
899
900
// u_int16_t, int16_t don't exist in MSVC
901
typedef unsigned short u_int16_t;
902
typedef short int16_t;
903
904
// ----- END VC++ STUBS & FAKE DEFINITIONS ----------------------------------
905
906
#endif // _MSC_VER
907
908
#ifdef STL_MSVC // not always the same as _MSC_VER
909
#include "base/port_hash.h"
910
#else
911
struct PortableHashBase {};
912
#endif
913
914
#if defined(OS_WINDOWS) || defined(__APPLE__)
915
// gethostbyname() *is* thread-safe for Windows native threads. It is also
916
// safe on Mac OS X, where it uses thread-local storage, even though the
917
// manpages claim otherwise. For details, see
918
// http://lists.apple.com/archives/Darwin-dev/2006/May/msg00008.html
919
#else
920
// gethostbyname() is not thread-safe.  So disallow its use.  People
921
// should either use the HostLookup::Lookup*() methods, or gethostbyname_r()
922
#define gethostbyname gethostbyname_is_not_thread_safe_DO_NOT_USE
923
#endif
924
925
// create macros in which the programmer should enclose all specializations
926
// for hash_maps and hash_sets. This is necessary since these classes are not
927
// STL standardized. Depending on the STL implementation they are in different
928
// namespaces. Right now the right namespace is passed by the Makefile
929
// Examples: gcc3: -DHASH_NAMESPACE=__gnu_cxx
930
//           icc:  -DHASH_NAMESPACE=std
931
//           gcc2: empty
932
933
#ifndef HASH_NAMESPACE
934
#define HASH_NAMESPACE_DECLARATION_START
935
#define HASH_NAMESPACE_DECLARATION_END
936
#else
937
#define HASH_NAMESPACE_DECLARATION_START namespace HASH_NAMESPACE {
938
#define HASH_NAMESPACE_DECLARATION_END }
939
#endif
940
941
// Our STL-like classes use __STD.
942
#if defined(__GNUC__) || defined(__APPLE__) || defined(_MSC_VER)
943
#define __STD std
944
#endif
945
946
#if defined __GNUC__
947
#define STREAM_SET(s, bit) (s).setstate(ios_base::bit)
948
#define STREAM_SETF(s, flag) (s).setf(ios_base::flag)
949
#else
950
#define STREAM_SET(s, bit) (s).set(ios::bit)
951
#define STREAM_SETF(s, flag) (s).setf(ios::flag)
952
#endif
953
954
// Portable handling of unaligned loads, stores, and copies.
955
// On some platforms, like ARM, the copy functions can be more efficient
956
// then a load and a store.
957
958
#if defined(__i386) || defined(ARCH_ATHLON) || defined(__x86_64__) || defined(_ARCH_PPC)
959
960
// x86 and x86-64 can perform unaligned loads/stores directly;
961
// modern PowerPC hardware can also do unaligned integer loads and stores;
962
// but note: the FPU still sends unaligned loads and stores to a trap handler!
963
964
#define UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16*>(_p))
965
256k
#define UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32*>(_p))
966
5.38k
#define UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64*>(_p))
967
968
#define UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16*>(_p) = (_val))
969
0
#define UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32*>(_p) = (_val))
970
#define UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64*>(_p) = (_val))
971
972
#elif defined(__arm__) && !defined(__ARM_ARCH_5__) && !defined(__ARM_ARCH_5T__) &&               \
973
        !defined(__ARM_ARCH_5TE__) && !defined(__ARM_ARCH_5TEJ__) && !defined(__ARM_ARCH_6__) && \
974
        !defined(__ARM_ARCH_6J__) && !defined(__ARM_ARCH_6K__) && !defined(__ARM_ARCH_6Z__) &&   \
975
        !defined(__ARM_ARCH_6ZK__) && !defined(__ARM_ARCH_6T2__)
976
977
// ARMv7 and newer support native unaligned accesses, but only of 16-bit
978
// and 32-bit values (not 64-bit); older versions either raise a fatal signal,
979
// do an unaligned read and rotate the words around a bit, or do the reads very
980
// slowly (trip through kernel mode). There's no simple #define that says just
981
// “ARMv7 or higher”, so we have to filter away all ARMv5 and ARMv6
982
// sub-architectures. Newer gcc (>= 4.6) set an __ARM_FEATURE_ALIGNED #define,
983
// so in time, maybe we can move on to that.
984
//
985
// This is a mess, but there's not much we can do about it.
986
987
#define UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16*>(_p))
988
#define UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32*>(_p))
989
990
#define UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16*>(_p) = (_val))
991
#define UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32*>(_p) = (_val))
992
993
// TODO(user): NEON supports unaligned 64-bit loads and stores.
994
// See if that would be more efficient on platforms supporting it,
995
// at least for copies.
996
997
inline uint64 UNALIGNED_LOAD64(const void* p) {
998
    uint64 t;
999
    memcpy(&t, p, sizeof t);
1000
    return t;
1001
}
1002
1003
inline void UNALIGNED_STORE64(void* p, uint64 v) {
1004
    memcpy(p, &v, sizeof v);
1005
}
1006
1007
#else
1008
1009
#define NEED_ALIGNED_LOADS
1010
1011
// These functions are provided for architectures that don't support
1012
// unaligned loads and stores.
1013
1014
inline uint16 UNALIGNED_LOAD16(const void* p) {
1015
    uint16 t;
1016
    memcpy(&t, p, sizeof t);
1017
    return t;
1018
}
1019
1020
inline uint32 UNALIGNED_LOAD32(const void* p) {
1021
    uint32 t;
1022
    memcpy(&t, p, sizeof t);
1023
    return t;
1024
}
1025
1026
inline uint64 UNALIGNED_LOAD64(const void* p) {
1027
    uint64 t;
1028
    memcpy(&t, p, sizeof t);
1029
    return t;
1030
}
1031
1032
inline void UNALIGNED_STORE16(void* p, uint16 v) {
1033
    memcpy(p, &v, sizeof v);
1034
}
1035
1036
inline void UNALIGNED_STORE32(void* p, uint32 v) {
1037
    memcpy(p, &v, sizeof v);
1038
}
1039
1040
inline void UNALIGNED_STORE64(void* p, uint64 v) {
1041
    memcpy(p, &v, sizeof v);
1042
}
1043
1044
#endif
1045
1046
#ifdef _LP64
1047
#define UNALIGNED_LOADW(_p) UNALIGNED_LOAD64(_p)
1048
#define UNALIGNED_STOREW(_p, _val) UNALIGNED_STORE64(_p, _val)
1049
#else
1050
#define UNALIGNED_LOADW(_p) UNALIGNED_LOAD32(_p)
1051
#define UNALIGNED_STOREW(_p, _val) UNALIGNED_STORE32(_p, _val)
1052
#endif
1053
1054
// NOTE(user): These are only exported to C++ because the macros they depend on
1055
// use C++-only syntax. This #ifdef can be removed if/when the macros are fixed.
1056
1057
#if defined(__cplusplus)
1058
1059
0
inline void UnalignedCopy16(const void* src, void* dst) {
1060
0
    UNALIGNED_STORE16(dst, UNALIGNED_LOAD16(src));
1061
0
}
1062
1063
0
inline void UnalignedCopy32(const void* src, void* dst) {
1064
0
    UNALIGNED_STORE32(dst, UNALIGNED_LOAD32(src));
1065
0
}
1066
1067
0
inline void UnalignedCopy64(const void* src, void* dst) {
1068
0
    if (sizeof(void*) == 8) {
1069
0
        UNALIGNED_STORE64(dst, UNALIGNED_LOAD64(src));
1070
0
    } else {
1071
0
        const char* src_char = reinterpret_cast<const char*>(src);
1072
0
        char* dst_char = reinterpret_cast<char*>(dst);
1073
0
1074
0
        UNALIGNED_STORE32(dst_char, UNALIGNED_LOAD32(src_char));
1075
0
        UNALIGNED_STORE32(dst_char + 4, UNALIGNED_LOAD32(src_char + 4));
1076
0
    }
1077
0
}
1078
1079
#endif // defined(__cpluscplus)
1080
1081
// printf macros for size_t, in the style of inttypes.h
1082
#ifdef _LP64
1083
#define __PRIS_PREFIX "z"
1084
#else
1085
#define __PRIS_PREFIX
1086
#endif
1087
1088
// Use these macros after a % in a printf format string
1089
// to get correct 32/64 bit behavior, like this:
1090
// size_t size = records.size();
1091
// printf("%" PRIuS "\n", size);
1092
1093
#define PRIdS __PRIS_PREFIX "d"
1094
#define PRIxS __PRIS_PREFIX "x"
1095
#define PRIuS __PRIS_PREFIX "u"
1096
#define PRIXS __PRIS_PREFIX "X"
1097
#define PRIoS __PRIS_PREFIX "o"
1098
1099
#define GPRIuPTHREAD "lu"
1100
#define GPRIxPTHREAD "lx"
1101
#ifdef OS_CYGWIN
1102
#define PRINTABLE_PTHREAD(pthreadt) reinterpret_cast<uintptr_t>(pthreadt)
1103
#else
1104
#define PRINTABLE_PTHREAD(pthreadt) pthreadt
1105
#endif
1106
1107
#define SIZEOF_MEMBER(t, f) sizeof(((t*)4096)->f)
1108
1109
#define OFFSETOF_MEMBER(t, f) \
1110
    (reinterpret_cast<char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<char*>(16))
1111
1112
#ifdef PTHREADS_REDHAT_WIN32
1113
#include <iosfwd>
1114
using std::ostream;  // NOLINT(build/include)
1115
#include <pthread.h> // NOLINT(build/include)
1116
// pthread_t is not a simple integer or pointer on Win32
1117
std::ostream& operator<<(std::ostream& out, const pthread_t& thread_id);
1118
#endif
1119
1120
// GXX_EXPERIMENTAL_CXX0X is defined by gcc and clang up to at least
1121
// gcc-4.7 and clang-3.1 (2011-12-13).  __cplusplus was defined to 1
1122
// in gcc before 4.7 (Crosstool 16) and clang before 3.1, but is
1123
// defined according to the language version in effect thereafter.  I
1124
// believe MSVC will also define __cplusplus according to the language
1125
// version, but haven't checked that.
1126
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
1127
// Define this to 1 if the code is compiled in C++11 mode; leave it
1128
// undefined otherwise.  Do NOT define it to 0 -- that causes
1129
// '#ifdef LANG_CXX11' to behave differently from '#if LANG_CXX11'.
1130
#define LANG_CXX11 1
1131
#endif
1132
1133
// On some platforms, a "function pointer" points to a function descriptor
1134
// rather than directly to the function itself.  Use FUNC_PTR_TO_CHAR_PTR(func)
1135
// to get a char-pointer to the first instruction of the function func.
1136
#if defined(__powerpc__) || defined(__ia64)
1137
// use opd section for function descriptors on these platforms, the function
1138
// address is the first word of the descriptor
1139
enum { kPlatformUsesOPDSections = 1 };
1140
#define FUNC_PTR_TO_CHAR_PTR(func) (reinterpret_cast<char**>(func)[0])
1141
#else
1142
enum { kPlatformUsesOPDSections = 0 };
1143
#define FUNC_PTR_TO_CHAR_PTR(func) (reinterpret_cast<char*>(func))
1144
#endif