1 // These are weird things we need to do to get this compiling on 2 // random systems (and on SWIG). 3 // 4 5 #ifndef DYNAMIC_DEPTH_INTERNAL_BASE_PORT_H_ // NOLINT 6 #define DYNAMIC_DEPTH_INTERNAL_BASE_PORT_H_ // NOLINT 7 8 #include <limits.h> // So we can set the bounds of our types 9 #include <stdlib.h> // for free() 10 #include <string.h> // for memcpy() 11 12 #if defined(__APPLE__) 13 // OSX has type names *_t, so we define these aliases. 14 #include <inttypes.h> 15 #include <stdint.h> 16 17 typedef uint64_t uint64; 18 typedef uint32_t uint32; 19 typedef uint16_t uint16; 20 typedef uint8_t uint8; 21 22 typedef int64_t int64; 23 typedef int32_t int32; 24 typedef int16_t int16; 25 typedef int8_t int8; 26 #endif 27 28 #define DYNAMIC_DEPTH_INTERNAL_EXPORT // NOLINT 29 30 #if defined(OS_CYGWIN) 31 #error "Cygwin is not supported." 32 #endif 33 34 #if defined(__CYGWIN__) 35 #error "Cygwin is not supported." 36 #endif 37 38 #if defined(__APPLE__) 39 // Currently, blaze supports iOS yet doesn't define a flag. Mac users have 40 // traditionally defined OS_MACOSX themselves via other build systems, since mac 41 // hasn't been supported by blaze. 42 #include <TargetConditionals.h> 43 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE 44 #ifndef OS_IOS // NOLINT 45 #define OS_IOS 1 46 #endif 47 #define SUPPRESS_MOBILE_IOS_BASE_PORT_H 48 #endif // defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE 49 #endif // defined(__APPLE__) 50 51 #if defined(OS_MACOSX) || defined(OS_IOS) 52 // This was added for getpagesize(), which is no longer used here. 53 // Clients incorrectly depend on this include. 54 #include <unistd.h> 55 #elif defined(OS_CYGWIN) || defined(__ANDROID__) 56 #include <malloc.h> // for memalign() 57 #elif defined(COMPILER_MSVC) 58 #include <stdio.h> // declare snprintf/vsnprintf before overriding 59 #endif 60 61 #include "base/integral_types.h" 62 63 // We support gcc 4.7 and later. 64 #if defined(__GNUC__) && !defined(__clang__) 65 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 7) 66 #error "This package requires gcc 4.7 or higher" 67 #endif 68 #endif 69 70 // We support MSVC++ 12.0 and later. 71 #if defined(_MSC_VER) && _MSC_VER < 1800 72 #error "This package requires _MSC_VER of 1800 or higher" 73 #endif 74 75 // We support Apple Xcode clang 4.2.1 (version 421.11.65) and later. 76 // This corresponds to Apple Xcode version 4.5. 77 #if defined(__apple_build_version__) && __apple_build_version__ < 4211165 78 #error "This package requires __apple_build_version__ of 4211165 or higher" 79 #endif 80 81 // Must happens before inttypes.h inclusion */ 82 #if defined(OS_MACOSX) 83 /* From MacOSX's inttypes.h: 84 * "C++ implementations should define these macros only when 85 * __STDC_FORMAT_MACROS is defined before <inttypes.h> is included." */ 86 #ifndef __STDC_FORMAT_MACROS // NOLINT 87 #define __STDC_FORMAT_MACROS 88 #endif /* __STDC_FORMAT_MACROS */ 89 #endif /* OS_MACOSX */ 90 91 /* Default for most OSes */ 92 /* We use SIGPWR since that seems unlikely to be used for other reasons. */ 93 #define GOOGLE_OBSCURE_SIGNAL SIGPWR 94 95 #if defined OS_LINUX || defined OS_CYGWIN || defined OS_ANDROID || \ 96 defined(__ANDROID__) 97 // _BIG_ENDIAN 98 #include <endian.h> 99 #endif 100 101 #if defined OS_LINUX || defined OS_CYGWIN 102 103 // GLIBC-related macros. 104 #include <features.h> 105 106 #ifndef __GLIBC_PREREQ // NOLINT 107 #define __GLIBC_PREREQ(a, b) 0 // not a GLIBC system 108 #endif 109 110 // The uint mess: 111 // mysql.h sets _GNU_SOURCE which sets __USE_MISC in <features.h> 112 // sys/types.h typedefs uint if __USE_MISC 113 // mysql typedefs uint if HAVE_UINT not set 114 // The following typedef is carefully considered, and should not cause 115 // any clashes 116 #if !defined(__USE_MISC) 117 #if !defined(HAVE_UINT) 118 #define HAVE_UINT 1 119 typedef unsigned int uint; 120 #endif 121 #if !defined(HAVE_USHORT) 122 #define HAVE_USHORT 1 123 typedef unsigned short ushort; 124 #endif 125 #if !defined(HAVE_ULONG) 126 #define HAVE_ULONG 1 127 typedef unsigned long ulong; 128 #endif 129 #endif 130 131 #if defined(__cplusplus) 132 #include <cstddef> // For _GLIBCXX macros 133 #endif 134 135 #if !defined(HAVE_TLS) && \ 136 (defined(_LIBCPP_VERSION) || defined(_GLIBCXX_HAVE_TLS)) && \ 137 (defined(ARCH_K8) || defined(ARCH_PPC) || defined(ARCH_ARM)) 138 #define HAVE_TLS 1 139 #endif 140 141 #elif defined OS_FREEBSD 142 143 // _BIG_ENDIAN 144 #include <machine/endian.h> 145 146 #elif defined(OS_MACOSX) || defined(OS_IOS) 147 148 // BIG_ENDIAN 149 #include <machine/endian.h> // NOLINT(build/include) 150 /* Let's try and follow the Linux convention */ 151 #define __BYTE_ORDER BYTE_ORDER 152 #define __LITTLE_ENDIAN LITTLE_ENDIAN 153 #define __BIG_ENDIAN BIG_ENDIAN 154 155 #endif 156 157 // The following guarantees declaration of the byte swap functions, and 158 // defines __BYTE_ORDER for MSVC 159 #ifdef COMPILER_MSVC 160 #include <stdlib.h> // NOLINT(build/include) 161 #define __BYTE_ORDER __LITTLE_ENDIAN 162 #define bswap_16(x) _byteswap_ushort(x) 163 #define bswap_32(x) _byteswap_ulong(x) 164 #define bswap_64(x) _byteswap_uint64(x) 165 166 #elif defined(OS_MACOSX) || defined(OS_IOS) 167 // Mac OS X / Darwin features 168 #include <libkern/OSByteOrder.h> 169 #define bswap_16(x) OSSwapInt16(x) 170 #define bswap_32(x) OSSwapInt32(x) 171 #define bswap_64(x) OSSwapInt64(x) 172 173 #elif defined(__GLIBC__) || defined(__CYGWIN__) 174 #include <byteswap.h> // IWYU pragma: export 175 176 #else 177 178 static inline uint16 bswap_16(uint16 x) { 179 return static_cast<uint16>(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8)); 180 } 181 #define bswap_16(x) bswap_16(x) 182 static inline uint32 bswap_32(uint32 x) { 183 return (((x & 0xFF) << 24) | ((x & 0xFF00) << 8) | ((x & 0xFF0000) >> 8) | 184 ((x & 0xFF000000) >> 24)); 185 } 186 #define bswap_32(x) bswap_32(x) 187 static inline uint64 bswap_64(uint64 x) { 188 return (((x & GG_ULONGLONG(0xFF)) << 56) | 189 ((x & GG_ULONGLONG(0xFF00)) << 40) | 190 ((x & GG_ULONGLONG(0xFF0000)) << 24) | 191 ((x & GG_ULONGLONG(0xFF000000)) << 8) | 192 ((x & GG_ULONGLONG(0xFF00000000)) >> 8) | 193 ((x & GG_ULONGLONG(0xFF0000000000)) >> 24) | 194 ((x & GG_ULONGLONG(0xFF000000000000)) >> 40) | 195 ((x & GG_ULONGLONG(0xFF00000000000000)) >> 56)); 196 } 197 #define bswap_64(x) bswap_64(x) 198 199 #endif 200 201 // define the macros IS_LITTLE_ENDIAN or IS_BIG_ENDIAN 202 // using the above endian definitions from endian.h if 203 // endian.h was included 204 #ifdef __BYTE_ORDER 205 #if __BYTE_ORDER == __LITTLE_ENDIAN 206 #define IS_LITTLE_ENDIAN 207 #endif 208 209 #if __BYTE_ORDER == __BIG_ENDIAN 210 #define IS_BIG_ENDIAN 211 #endif 212 213 #else 214 215 #if defined(__LITTLE_ENDIAN__) 216 #define IS_LITTLE_ENDIAN 217 #elif defined(__BIG_ENDIAN__) 218 #define IS_BIG_ENDIAN 219 #endif 220 221 // there is also PDP endian ... 222 223 #endif // __BYTE_ORDER 224 225 // Define the OS's path separator 226 #ifdef __cplusplus // C won't merge duplicate const variables at link time 227 // Some headers provide a macro for this (GCC's system.h), remove it so that we 228 // can use our own. 229 #undef PATH_SEPARATOR 230 #if defined(OS_WINDOWS) 231 const char PATH_SEPARATOR = '\\'; 232 #else 233 const char PATH_SEPARATOR = '/'; 234 #endif 235 #endif 236 237 // Windows has O_BINARY as a flag to open() (like "b" for fopen). 238 // Linux doesn't need make this distinction. 239 #if defined OS_LINUX && !defined O_BINARY 240 #define O_BINARY 0 241 #endif 242 243 #ifdef COMPILER_MSVC 244 // doesn't have uid_t 245 typedef int uid_t; 246 #endif 247 248 // Mac OS X / Darwin and iOS features 249 250 #if defined(OS_MACOSX) || defined(OS_IOS) 251 252 // For mmap, Linux defines both MAP_ANONYMOUS and MAP_ANON and says MAP_ANON is 253 // deprecated. In Darwin, MAP_ANON is all there is. 254 #if !defined MAP_ANONYMOUS 255 #define MAP_ANONYMOUS MAP_ANON 256 #endif 257 258 // Linux has this in <sys/cdefs.h> 259 #define __ptr_t void * 260 261 // Linux has this in <linux/errno.h> 262 #define EXFULL ENOMEM // not really that great a translation... 263 264 // Mach-O supports sections (albeit with small names), but doesn't have 265 // vars at the beginning and end. Instead you should call the function 266 // getsectdata("__DATA", name, &size). 267 #define HAVE_ATTRIBUTE_SECTION 1 268 269 // Any function with ATTRIBUTE_SECTION must not be inlined, or it will 270 // be placed into whatever section its caller is placed into. 271 #define ATTRIBUTE_SECTION(name) \ 272 __attribute__((section("__DATA, " #name))) __attribute__((noinline)) 273 274 #define ENUM_DYLD_BOOL // so that we don't pollute the global namespace 275 extern "C" { 276 #include <mach-o/dyld.h> 277 #include <mach-o/getsect.h> 278 } 279 class AssignAttributeStartEnd { 280 public: 281 AssignAttributeStartEnd(const char *name, char **pstart, char **pend) { 282 // Find out what dynamic library name is defined in 283 for (int i = _dyld_image_count() - 1; i >= 0; --i) { 284 const mach_header *hdr = _dyld_get_image_header(i); 285 uint32_t len; 286 *pstart = getsectdatafromheader(hdr, "__DATA", name, &len); 287 if (*pstart) { // NULL if not defined in this dynamic library 288 *pstart += _dyld_get_image_vmaddr_slide(i); // correct for reloc 289 *pend = *pstart + len; 290 return; 291 } 292 } 293 // If we get here, not defined in a dll at all. See if defined statically. 294 // don't ask me why this type isn't uint32_t too... 295 unsigned long len; // NOLINT 296 *pstart = getsectdata("__DATA", name, &len); 297 *pend = *pstart + len; 298 } 299 }; 300 301 // 1) DEFINE_ATTRIBUTE_SECTION_VARS: must be called once per unique 302 // name. You want to make sure this is executed before any 303 // DECLARE_ATTRIBUTE_SECTION_VARS; the easiest way is to put them 304 // in the same .cc file. Put this call at the global level. 305 // 2) INIT_ATTRIBUTE_SECTION_VARS: you can scatter calls to this in 306 // multiple places to help ensure execution before any 307 // DECLARE_ATTRIBUTE_SECTION_VARS. You must have at least one 308 // DEFINE, but you can have many INITs. Put each in its own scope. 309 // 3) DECLARE_ATTRIBUTE_SECTION_VARS: must be called before using 310 // ATTRIBUTE_SECTION_START or ATTRIBUTE_SECTION_STOP on a name. 311 // Put this call at the global level. 312 #define DECLARE_ATTRIBUTE_SECTION_VARS(name) \ 313 extern char *__start_##name; \ 314 extern char *__stop_##name; 315 316 #define INIT_ATTRIBUTE_SECTION_VARS(name) \ 317 DECLARE_ATTRIBUTE_SECTION_VARS(name); \ 318 static const AssignAttributeStartEnd __assign_##name(#name, &__start_##name, \ 319 &__stop_##name) 320 321 #define DEFINE_ATTRIBUTE_SECTION_VARS(name) \ 322 char *__start_##name, *__stop_##name; \ 323 INIT_ATTRIBUTE_SECTION_VARS(name) 324 325 // Darwin doesn't have strnlen. No comment. 326 inline size_t strnlen(const char *s, size_t maxlen) { 327 const char *end = (const char *)memchr(s, '\0', maxlen); 328 if (end) return end - s; 329 return maxlen; 330 } 331 332 // Doesn't exist on OSX. 333 #define MSG_NOSIGNAL 0 334 335 // No SIGPWR on MacOSX. SIGINFO seems suitably obscure. 336 #undef GOOGLE_OBSCURE_SIGNAL 337 #define GOOGLE_OBSCURE_SIGNAL SIGINFO 338 339 #elif defined(OS_CYGWIN) // Cygwin-specific behavior. 340 341 #if defined(__CYGWIN32__) 342 #define __WORDSIZE 32 343 #else 344 // It's probably possible to support 64-bit, but the #defines will need checked. 345 #error "Cygwin is currently only 32-bit." 346 #endif 347 348 // No signalling on Windows. 349 #undef GOOGLE_OBSCURE_SIGNAL 350 #define GOOGLE_OBSCURE_SIGNAL 0 351 352 struct stack_t { 353 void *ss_sp; 354 int ss_flags; 355 size_t ss_size; 356 }; 357 inline int sigaltstack(stack_t *ss, stack_t *oss) { return 0; } 358 359 #define PTHREAD_STACK_MIN 0 // Not provided by cygwin 360 361 // Scans memory for a character. 362 // memrchr is used in a few places, but it's linux-specific. 363 inline void *memrchr(const void *bytes, int find_char, size_t len) { 364 const unsigned char *cursor = 365 reinterpret_cast<const unsigned char *>(bytes) + len - 1; 366 unsigned char actual_char = find_char; 367 for (; cursor >= bytes; --cursor) { 368 if (*cursor == actual_char) { 369 return const_cast<void *>(reinterpret_cast<const void *>(cursor)); 370 } 371 } 372 return NULL; 373 } 374 375 #endif 376 377 // Klocwork static analysis tool's C/C++ compiler kwcc 378 #if defined(__KLOCWORK__) 379 #define STATIC_ANALYSIS 380 #endif // __KLOCWORK__ 381 382 // GCC-specific features 383 384 #if (defined(COMPILER_GCC3) || defined(OS_MACOSX) || defined(OS_IOS)) && \ 385 !defined(SWIG) 386 387 // 388 // Tell the compiler to do printf format string checking if the 389 // compiler supports it; see the 'format' attribute in 390 // <http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html>. 391 // 392 // N.B.: As the GCC manual states, "[s]ince non-static C++ methods 393 // have an implicit 'this' argument, the arguments of such methods 394 // should be counted from two, not one." 395 // 396 #define PRINTF_ATTRIBUTE(string_index, first_to_check) \ 397 __attribute__((__format__(__printf__, string_index, first_to_check))) 398 #define SCANF_ATTRIBUTE(string_index, first_to_check) \ 399 __attribute__((__format__(__scanf__, string_index, first_to_check))) 400 401 // Cache line alignment 402 #if defined(__i386__) || defined(__x86_64__) 403 #define CACHELINE_SIZE 64 404 #elif defined(__powerpc64__) 405 #define CACHELINE_SIZE 128 406 #elif defined(__aarch64__) 407 // We would need to read special regiter ctr_el0 to find out L1 dcache size. 408 // This value is a good estimate based on a real aarch64 machine. 409 #define CACHELINE_SIZE 64 410 #elif defined(__arm__) 411 // Cache line sizes for ARM: These values are not strictly correct since 412 // cache line sizes depend on implementations, not architectures. There 413 // are even implementations with cache line sizes configurable at boot 414 // time. 415 #if defined(__ARM_ARCH_5T__) 416 #define CACHELINE_SIZE 32 417 #elif defined(__ARM_ARCH_7A__) 418 #define CACHELINE_SIZE 64 419 #endif 420 #endif 421 422 #ifndef CACHELINE_SIZE // NOLINT 423 // A reasonable default guess. Note that overestimates tend to waste more 424 // space, while underestimates tend to waste more time. 425 #define CACHELINE_SIZE 64 426 #endif 427 428 #define CACHELINE_ALIGNED __attribute__((aligned(CACHELINE_SIZE))) 429 430 // 431 // Prevent the compiler from complaining about or optimizing away variables 432 // that appear unused 433 #ifndef DDEPTH_ATTRIBUTE_UNUSED // NOLINT 434 #undef DDEPTH_ATTRIBUTE_UNUSED 435 #define DDEPTH_ATTRIBUTE_UNUSED __attribute__((__unused__)) 436 #endif // DDEPTH_ATTRIBUTE_UNUSED 437 438 // 439 // For functions we want to force inline or not inline. 440 // Introduced in gcc 3.1. 441 #define ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline)) 442 #define HAVE_ATTRIBUTE_ALWAYS_INLINE 1 443 #define ATTRIBUTE_NOINLINE __attribute__((noinline)) 444 #define HAVE_ATTRIBUTE_NOINLINE 1 445 446 // For weak functions 447 #undef ATTRIBUTE_WEAK 448 #define ATTRIBUTE_WEAK __attribute__((weak)) 449 #define HAVE_ATTRIBUTE_WEAK 1 450 451 // Tell the compiler to use "initial-exec" mode for a thread-local variable. 452 // See http://people.redhat.com/drepper/tls.pdf for the gory details. 453 #define ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec"))) 454 455 // Tell the compiler either that a particular function parameter 456 // should be a non-null pointer, or that all pointer arguments should 457 // be non-null. 458 // 459 // Note: As the GCC manual states, "[s]ince non-static C++ methods 460 // have an implicit 'this' argument, the arguments of such methods 461 // should be counted from two, not one." 462 // 463 // Args are indexed starting at 1. 464 // For non-static class member functions, the implicit "this" argument 465 // is arg 1, and the first explicit argument is arg 2. 466 // For static class member functions, there is no implicit "this", and 467 // the first explicit argument is arg 1. 468 // 469 // /* arg_a cannot be NULL, but arg_b can */ 470 // void Function(void* arg_a, void* arg_b) ATTRIBUTE_NONNULL(1); 471 // 472 // class C { 473 // /* arg_a cannot be NULL, but arg_b can */ 474 // void Method(void* arg_a, void* arg_b) ATTRIBUTE_NONNULL(2); 475 // 476 // /* arg_a cannot be NULL, but arg_b can */ 477 // static void StaticMethod(void* arg_a, void* arg_b) ATTRIBUTE_NONNULL(1); 478 // }; 479 // 480 // If no arguments are provided, then all pointer arguments should be non-null. 481 // 482 // /* No pointer arguments may be null. */ 483 // void Function(void* arg_a, void* arg_b, int arg_c) ATTRIBUTE_NONNULL(); 484 // 485 // NOTE: The GCC nonnull attribute actually accepts a list of arguments, but 486 // ATTRIBUTE_NONNULL does not. 487 #define ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index))) 488 489 // 490 // Tell the compiler that a given function never returns 491 // 492 #define ATTRIBUTE_NORETURN __attribute__((noreturn)) 493 494 // Tell AddressSanitizer (or other memory testing tools) to ignore a given 495 // function. Useful for cases when a function reads random locations on stack, 496 // calls _exit from a cloned subprocess, deliberately accesses buffer 497 // out of bounds or does other scary things with memory. 498 #ifdef ADDRESS_SANITIZER 499 #define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) 500 #else 501 #define ATTRIBUTE_NO_SANITIZE_ADDRESS 502 #endif 503 504 // Tell MemorySanitizer to relax the handling of a given function. All "Use of 505 // uninitialized value" warnings from such functions will be suppressed, and all 506 // values loaded from memory will be considered fully initialized. 507 // This is similar to the ADDRESS_SANITIZER attribute above, but deals with 508 // initializedness rather than addressability issues. 509 #ifdef MEMORY_SANITIZER 510 #define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory)) 511 #else 512 #define ATTRIBUTE_NO_SANITIZE_MEMORY 513 #endif 514 515 // Tell ThreadSanitizer to not instrument a given function. 516 // If you are adding this attribute, please cc dynamic-tools@ on the cl. 517 #ifdef THREAD_SANITIZER 518 #define ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread)) 519 #else 520 #define ATTRIBUTE_NO_SANITIZE_THREAD 521 #endif 522 523 // Tell ControlFlowIntegrity sanitizer to not instrument a given function. 524 #ifdef CONTROL_FLOW_INTEGRITY 525 #define ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi"))) 526 #else 527 #define ATTRIBUTE_NO_SANITIZE_CFI 528 #endif 529 530 #ifndef HAVE_ATTRIBUTE_SECTION // may have been pre-set to 0, e.g. for Darwin 531 // // NOLINT 532 #define HAVE_ATTRIBUTE_SECTION 1 533 #endif 534 535 #if HAVE_ATTRIBUTE_SECTION // define section support for the case of GCC 536 537 // 538 // Tell the compiler/linker to put a given function into a section and define 539 // "__start_ ## name" and "__stop_ ## name" symbols to bracket the section. 540 // This functionality is supported by GNU linker. 541 // Any function with ATTRIBUTE_SECTION must not be inlined, or it will 542 // be placed into whatever section its caller is placed into. 543 // 544 #ifndef ATTRIBUTE_SECTION // NOLINT 545 #define ATTRIBUTE_SECTION(name) \ 546 __attribute__((section(#name))) __attribute__((noinline)) 547 #endif 548 549 // 550 // Weak section declaration to be used as a global declaration 551 // for ATTRIBUTE_SECTION_START|STOP(name) to compile and link 552 // even without functions with ATTRIBUTE_SECTION(name). 553 // DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's 554 // a no-op on ELF but not on Mach-O. 555 // 556 #ifndef DECLARE_ATTRIBUTE_SECTION_VARS // NOLINT 557 #define DECLARE_ATTRIBUTE_SECTION_VARS(name) \ 558 extern char __start_##name[] ATTRIBUTE_WEAK; \ 559 extern char __stop_##name[] ATTRIBUTE_WEAK 560 #endif 561 #ifndef DEFINE_ATTRIBUTE_SECTION_VARS // NOLINT 562 #define INIT_ATTRIBUTE_SECTION_VARS(name) 563 #define DEFINE_ATTRIBUTE_SECTION_VARS(name) 564 #endif 565 566 // 567 // Return void* pointers to start/end of a section of code with 568 // functions having ATTRIBUTE_SECTION(name). 569 // Returns 0 if no such functions exits. 570 // One must DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and link. 571 // 572 #define ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(__start_##name)) 573 #define ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(__stop_##name)) 574 575 #endif // HAVE_ATTRIBUTE_SECTION 576 577 // Support for aligning the stack on 32-bit x86. 578 579 #if defined(__i386__) && \ 580 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) 581 #define ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \ 582 __attribute__((force_align_arg_pointer)) 583 #define REQUIRE_STACK_ALIGN_TRAMPOLINE (0) 584 #elif defined(__i386__) || defined(__x86_64__) 585 #define REQUIRE_STACK_ALIGN_TRAMPOLINE (1) 586 #define ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC 587 #else 588 #define REQUIRE_STACK_ALIGN_TRAMPOLINE (0) 589 #define ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC 590 #endif 591 592 // Tell the compiler to warn about unused return values for functions declared 593 // with this macro. The macro must appear as the very first part of a function 594 // declaration or definition: 595 // 596 // ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket(); 597 // 598 // This placement has the broadest compatibility with GCC, Clang, and MSVC, with 599 // both defs and decls, and with GCC-style attributes, MSVC declspec, and C++11 600 // attributes. Note: past advice was to place the macro after the argument list. 601 #if defined(SWIG) 602 #define ABSL_MUST_USE_RESULT 603 #elif __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 604 #define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result)) 605 #else 606 #define ABSL_MUST_USE_RESULT 607 #endif 608 609 // 610 // Prevent the compiler from padding a structure to natural alignment 611 // 612 #if __GNUC__ && !defined(SWIG) 613 #define ATTRIBUTE_PACKED __attribute__((__packed__)) 614 #else 615 #define ATTRIBUTE_PACKED 616 #endif 617 618 #if defined(COMPILER_GCC3) || defined(__llvm__) 619 // Defined behavior on some of the uarchs: 620 // PREFETCH_HINT_T0: 621 // prefetch to all levels of the hierarchy (except on p4: prefetch to L2) 622 // PREFETCH_HINT_NTA: 623 // p4: fetch to L2, but limit to 1 way (out of the 8 ways) 624 // core: skip L2, go directly to L1 625 // k8 rev E and later: skip L2, can go to either of the 2-ways in L1 626 enum PrefetchHint { 627 PREFETCH_HINT_T0 = 3, // More temporal locality 628 PREFETCH_HINT_T1 = 2, 629 PREFETCH_HINT_T2 = 1, // Less temporal locality 630 PREFETCH_HINT_NTA = 0 // No temporal locality 631 }; 632 #else 633 // prefetch is a no-op for this target. Feel free to add more sections above. 634 #endif 635 636 // The default behavior of prefetch is to speculatively load for read only. This 637 // is safe for all currently supported platforms. However, prefetch for store 638 // may have problems depending on the target platform (x86, PPC, arm). Check 639 // with the platforms team (platforms-servers@) before introducing any changes 640 // to this function to identify potential impact on current and future servers. 641 extern inline void prefetch(const void *x, int hint) { 642 #if defined(__llvm__) 643 // In the gcc version of prefetch(), hint is only a constant _after_ inlining 644 // (assumed to have been successful). llvm views things differently, and 645 // checks constant-ness _before_ inlining. This leads to compilation errors 646 // with using the other version of this code with llvm. 647 // 648 // One way round this is to use a switch statement to explicitly match 649 // prefetch hint enumerations, and invoke __builtin_prefetch for each valid 650 // value. llvm's optimization removes the switch and unused case statements 651 // after inlining, so that this boils down in the end to the same as for gcc; 652 // that is, a single inlined prefetchX instruction. 653 // 654 // Note that this version of prefetch() cannot verify constant-ness of hint. 655 // If client code calls prefetch() with a variable value for hint, it will 656 // receive the full expansion of the switch below, perhaps also not inlined. 657 // This should however not be a problem in the general case of well behaved 658 // caller code that uses the supplied prefetch hint enumerations. 659 switch (hint) { 660 case PREFETCH_HINT_T0: 661 __builtin_prefetch(x, 0, PREFETCH_HINT_T0); 662 break; 663 case PREFETCH_HINT_T1: 664 __builtin_prefetch(x, 0, PREFETCH_HINT_T1); 665 break; 666 case PREFETCH_HINT_T2: 667 __builtin_prefetch(x, 0, PREFETCH_HINT_T2); 668 break; 669 case PREFETCH_HINT_NTA: 670 __builtin_prefetch(x, 0, PREFETCH_HINT_NTA); 671 break; 672 default: 673 __builtin_prefetch(x); 674 break; 675 } 676 #elif defined(COMPILER_GCC3) 677 if (__builtin_constant_p(hint)) { 678 __builtin_prefetch(x, 0, hint); 679 } else { 680 // Defaults to PREFETCH_HINT_T0 681 __builtin_prefetch(x); 682 } 683 #else 684 // You get no effect. Feel free to add more sections above. 685 #endif 686 } 687 688 #ifdef __cplusplus 689 // prefetch intrinsic (bring data to L1 without polluting L2 cache) 690 extern inline void prefetch(const void *x) { return prefetch(x, 0); } 691 #endif // ifdef __cplusplus 692 693 // 694 // GCC can be told that a certain branch is not likely to be taken (for 695 // instance, a CHECK failure), and use that information in static analysis. 696 // Giving it this information can help it optimize for the common case in 697 // the absence of better information (ie. -fprofile-arcs). 698 // 699 #if defined(COMPILER_GCC3) 700 #define PREDICT_FALSE(x) (__builtin_expect(x, 0)) 701 #define PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) 702 #else 703 #define PREDICT_FALSE(x) x 704 #define PREDICT_TRUE(x) x 705 #endif 706 707 // 708 // Tell GCC that a function is hot or cold. GCC can use this information to 709 // improve static analysis, i.e. a conditional branch to a cold function 710 // is likely to be not-taken. 711 // This annotation is used for function declarations, e.g.: 712 // int foo() ATTRIBUTE_HOT; 713 // 714 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) 715 #define ATTRIBUTE_HOT __attribute__((hot)) 716 #define ATTRIBUTE_COLD __attribute__((cold)) 717 #else 718 #define ATTRIBUTE_HOT 719 #define ATTRIBUTE_COLD 720 #endif 721 722 #define FTELLO ftello 723 #define FSEEKO fseeko 724 725 #else // not GCC 726 727 #define PRINTF_ATTRIBUTE(string_index, first_to_check) 728 #define SCANF_ATTRIBUTE(string_index, first_to_check) 729 #define CACHELINE_SIZE 64 730 #define CACHELINE_ALIGNED 731 732 #ifndef ATTRIBUTE_UNUSED // NOLINT 733 #define ATTRIBUTE_UNUSED 734 #endif // ATTRIBUTE_UNUSED 735 736 #define ATTRIBUTE_ALWAYS_INLINE 737 #define ATTRIBUTE_NOINLINE 738 #define ATTRIBUTE_HOT 739 #define ATTRIBUTE_COLD 740 #define ATTRIBUTE_WEAK 741 #define HAVE_ATTRIBUTE_WEAK 0 742 #define ATTRIBUTE_INITIAL_EXEC 743 #define ATTRIBUTE_NONNULL(arg_index) 744 #define ATTRIBUTE_NORETURN 745 #define ATTRIBUTE_NO_SANITIZE_ADDRESS 746 #define ATTRIBUTE_NO_SANITIZE_MEMORY 747 #define HAVE_ATTRIBUTE_SECTION 0 748 #define ATTRIBUTE_PACKED 749 #define ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC 750 #define REQUIRE_STACK_ALIGN_TRAMPOLINE (0) 751 #define ABSL_MUST_USE_RESULT 752 extern inline void prefetch(const void *) {} 753 #define PREDICT_FALSE(x) x 754 #define PREDICT_TRUE(x) x 755 756 // These should be redefined appropriately if better alternatives to 757 // ftell/fseek exist in the compiler 758 #define FTELLO ftell 759 #define FSEEKO fseek 760 761 #endif // GCC 762 763 #if ((defined(COMPILER_GCC3) || defined(OS_MACOSX) || defined(OS_IOS) || \ 764 defined(__NVCC__)) && \ 765 !defined(SWIG)) || \ 766 ((__GNUC__ >= 3 || defined(__clang__)) && defined(__ANDROID__)) 767 768 #if !defined(__cplusplus) && !defined(OS_MACOSX) && !defined(OS_IOS) && \ 769 !defined(OS_CYGWIN) 770 // stdlib.h only declares this in C++, not in C, so we declare it here. 771 // Also make sure to avoid declaring it on platforms which don't support it. 772 extern int posix_memalign(void **memptr, size_t alignment, size_t size); 773 #endif 774 775 inline void *aligned_malloc(size_t size, int minimum_alignment) { 776 #if defined(__ANDROID__) || defined(OS_ANDROID) || defined(OS_CYGWIN) 777 return memalign(minimum_alignment, size); 778 #else // !__ANDROID__ && !OS_ANDROID && !OS_CYGWIN 779 void *ptr = NULL; 780 // posix_memalign requires that the requested alignment be at least 781 // sizeof(void*). In this case, fall back on malloc which should return memory 782 // aligned to at least the size of a pointer. 783 const int required_alignment = sizeof(void *); 784 if (minimum_alignment < required_alignment) return malloc(size); 785 if (posix_memalign(&ptr, minimum_alignment, size) != 0) 786 return NULL; 787 else 788 return ptr; 789 #endif 790 } 791 792 inline void aligned_free(void *aligned_memory) { free(aligned_memory); } 793 794 #endif 795 // #if ((defined(COMPILER_GCC3) || defined(OS_MACOSX) || defined(OS_IOS) || 796 // defined(__NVCC__)) && !defined(SWIG)) || 797 // ((__GNUC__ >= 3 || defined(__clang__)) && defined(__ANDROID__)) 798 799 // 800 // Provides a char array with the exact same alignment as another type. The 801 // first parameter must be a complete type, the second parameter is how many 802 // of that type to provide space for. 803 // 804 // ALIGNED_CHAR_ARRAY(struct stat, 16) storage_; 805 // 806 #if defined(__cplusplus) 807 #undef ALIGNED_CHAR_ARRAY 808 // Because MSVC and older GCCs require that the argument to their alignment 809 // construct to be a literal constant integer, we use a template instantiated 810 // at all the possible powers of two. 811 #ifndef SWIG // NOLINT 812 template <int alignment, int size> 813 struct AlignType {}; 814 template <int size> 815 struct AlignType<0, size> { 816 typedef char result[size]; 817 }; 818 #if defined(COMPILER_MSVC) 819 #define BASE_PORT_H_ALIGN_ATTRIBUTE(X) __declspec(align(X)) 820 #define BASE_PORT_H_ALIGN_OF(T) __alignof(T) 821 #elif defined(COMPILER_GCC3) 822 #define BASE_PORT_H_ALIGN_ATTRIBUTE(X) __attribute__((aligned(X))) 823 #define BASE_PORT_H_ALIGN_OF(T) __alignof__(T) 824 #endif 825 826 #if defined(BASE_PORT_H_ALIGN_ATTRIBUTE) 827 828 #define BASE_PORT_H_ALIGNTYPE_TEMPLATE(X) \ 829 template <int size> \ 830 struct AlignType<X, size> { \ 831 typedef BASE_PORT_H_ALIGN_ATTRIBUTE(X) char result[size]; \ 832 } 833 834 BASE_PORT_H_ALIGNTYPE_TEMPLATE(1); 835 BASE_PORT_H_ALIGNTYPE_TEMPLATE(2); 836 BASE_PORT_H_ALIGNTYPE_TEMPLATE(4); 837 BASE_PORT_H_ALIGNTYPE_TEMPLATE(8); 838 BASE_PORT_H_ALIGNTYPE_TEMPLATE(16); 839 BASE_PORT_H_ALIGNTYPE_TEMPLATE(32); 840 BASE_PORT_H_ALIGNTYPE_TEMPLATE(64); 841 BASE_PORT_H_ALIGNTYPE_TEMPLATE(128); 842 BASE_PORT_H_ALIGNTYPE_TEMPLATE(256); 843 BASE_PORT_H_ALIGNTYPE_TEMPLATE(512); 844 BASE_PORT_H_ALIGNTYPE_TEMPLATE(1024); 845 BASE_PORT_H_ALIGNTYPE_TEMPLATE(2048); 846 BASE_PORT_H_ALIGNTYPE_TEMPLATE(4096); 847 BASE_PORT_H_ALIGNTYPE_TEMPLATE(8192); 848 // Any larger and MSVC++ will complain. 849 850 #define ALIGNED_CHAR_ARRAY(T, Size) \ 851 typename AlignType<BASE_PORT_H_ALIGN_OF(T), sizeof(T) * Size>::result 852 853 #undef BASE_PORT_H_ALIGNTYPE_TEMPLATE 854 #undef BASE_PORT_H_ALIGN_ATTRIBUTE 855 856 #else // defined(BASE_PORT_H_ALIGN_ATTRIBUTE) 857 #define ALIGNED_CHAR_ARRAY \ 858 you_must_define_ALIGNED_CHAR_ARRAY_for_your_compiler_in_base_port_h 859 #endif // defined(BASE_PORT_H_ALIGN_ATTRIBUTE) 860 861 #else // !SWIG 862 863 // SWIG can't represent alignment and doesn't care about alignment on data 864 // members (it works fine without it). 865 template <typename Size> 866 struct AlignType { 867 typedef char result[Size]; 868 }; 869 #define ALIGNED_CHAR_ARRAY(T, Size) AlignType<Size * sizeof(T)>::result 870 871 #endif // !SWIG 872 #else // __cpluscplus 873 #define ALIGNED_CHAR_ARRAY ALIGNED_CHAR_ARRAY_is_not_available_without_Cplusplus 874 #endif // __cplusplus 875 876 #if !HAVE_ATTRIBUTE_SECTION // provide dummy definitions 877 878 #define ATTRIBUTE_SECTION(name) 879 #define INIT_ATTRIBUTE_SECTION_VARS(name) 880 #define DEFINE_ATTRIBUTE_SECTION_VARS(name) 881 #define DECLARE_ATTRIBUTE_SECTION_VARS(name) 882 #define ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0)) 883 #define ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0)) 884 885 #endif // !HAVE_ATTRIBUTE_SECTION 886 887 #ifdef COMPILER_MSVC /* if Visual C++ */ 888 889 // This compiler flag can be easily overlooked on MSVC. 890 // _CHAR_UNSIGNED gets set with the /J flag. 891 #ifndef _CHAR_UNSIGNED // NOLINT 892 #error chars must be unsigned! Use the /J flag on the compiler command line. 893 #endif 894 895 // MSVC is a little hyper-active in its warnings 896 // Signed vs. unsigned comparison is ok. 897 #pragma warning(disable : 4018) 898 // We know casting from a long to a char may lose data 899 #pragma warning(disable : 4244) 900 // Don't need performance warnings about converting ints to bools 901 #pragma warning(disable : 4800) 902 // Integral constant overflow is apparently ok too 903 // for example: 904 // short k; int n; 905 // k = k + n; 906 #pragma warning(disable : 4307) 907 // It's ok to use this* in constructor 908 // Example: 909 // class C { 910 // Container cont_; 911 // C() : cont_(this) { ... 912 #pragma warning(disable : 4355) 913 // Truncating from double to float is ok 914 #pragma warning(disable : 4305) 915 916 #include <assert.h> 917 #include <process.h> // _getpid() 918 #include <windows.h> 919 #include <winsock2.h> 920 #undef ERROR 921 922 #include <float.h> // for nextafter functionality on windows 923 #include <math.h> // for HUGE_VAL 924 925 #ifndef HUGE_VALF // NOLINT 926 #define HUGE_VALF (static_cast<float>(HUGE_VAL)) 927 #endif 928 929 namespace std {} // namespace std 930 using namespace std; 931 932 // VC++ doesn't understand "uint" 933 #ifndef HAVE_UINT // NOLINT 934 #define HAVE_UINT 1 935 typedef unsigned int uint; 936 #endif 937 938 // VC++ doesn't understand "ssize_t" 939 // <windows.h> from above includes <BaseTsd.h> and <BaseTsd.h> defines SSIZE_T 940 #ifndef HAVE_SSIZET // NOLINT 941 #define HAVE_SSIZET 1 942 typedef SSIZE_T ssize_t; 943 #endif 944 945 #define strtoq _strtoi64 946 #define strtouq _strtoui64 947 #define strtoll _strtoi64 948 #define strtoull _strtoui64 949 #define atoll _atoi64 950 951 // You say tomato, I say atotom 952 #define PATH_MAX MAX_PATH 953 954 // Wrap Microsoft _snprintf/_vsnprintf calls so they nul-terminate on buffer 955 // overflow. 956 #define vsnprintf base_port_MSVC_vsnprintf 957 inline int base_port_MSVC_vsnprintf(char *str, size_t size, const char *format, 958 va_list ap) { 959 int count = _vsnprintf(str, size, format, ap); 960 if (count < 0) { 961 count = _vscprintf(format, ap); // Yields character count. 962 } 963 if (size > 0 && count >= size) { 964 str[size - 1] = '\0'; 965 } 966 return count; 967 } 968 969 #define snprintf base_port_MSVC_snprintf 970 inline int base_port_MSVC_snprintf(char *str, size_t size, const char *fmt, 971 ...) { 972 va_list ap; 973 va_start(ap, fmt); 974 int count = base_port_MSVC_vsnprintf(str, size, fmt, ap); 975 va_end(ap); 976 return count; 977 } 978 979 // You say tomato, I say _tomato 980 #define strcasecmp _stricmp 981 #define strncasecmp _strnicmp 982 #define nextafter _nextafter 983 #define strdup _strdup 984 #define tempnam _tempnam 985 #define chdir _chdir 986 #define getcwd _getcwd 987 #define putenv _putenv 988 #if _MSC_VER >= 1900 // Only needed for VS2015+ 989 #define getpid _getpid 990 #define timezone _timezone 991 #define tzname _tzname 992 #endif 993 994 // You say tomato, I say toma 995 inline int random() { return rand(); } 996 inline void srandom(unsigned int seed) { srand(seed); } 997 998 // You say juxtapose, I say transpose 999 #define bcopy(s, d, n) memcpy(d, s, n) 1000 1001 inline void *aligned_malloc(size_t size, int minimum_alignment) { 1002 return _aligned_malloc(size, minimum_alignment); 1003 } 1004 1005 inline void aligned_free(void *aligned_memory) { 1006 _aligned_free(aligned_memory); 1007 } 1008 1009 // ----- BEGIN VC++ STUBS & FAKE DEFINITIONS --------------------------------- 1010 1011 // See http://en.wikipedia.org/wiki/IEEE_754 for details of 1012 // floating point format. 1013 1014 inline int fpclassify_double(double x) { 1015 const int float_point_class = _fpclass(x); 1016 int c99_class; 1017 switch (float_point_class) { 1018 case _FPCLASS_SNAN: // Signaling NaN 1019 case _FPCLASS_QNAN: // Quiet NaN 1020 c99_class = FP_NAN; 1021 break; 1022 case _FPCLASS_NZ: // Negative zero ( -0) 1023 case _FPCLASS_PZ: // Positive 0 (+0) 1024 c99_class = FP_ZERO; 1025 break; 1026 case _FPCLASS_NINF: // Negative infinity ( -INF) 1027 case _FPCLASS_PINF: // Positive infinity (+INF) 1028 c99_class = FP_INFINITE; 1029 break; 1030 case _FPCLASS_ND: // Negative denormalized 1031 case _FPCLASS_PD: // Positive denormalized 1032 c99_class = FP_SUBNORMAL; 1033 break; 1034 case _FPCLASS_NN: // Negative normalized non-zero 1035 case _FPCLASS_PN: // Positive normalized non-zero 1036 c99_class = FP_NORMAL; 1037 break; 1038 default: 1039 c99_class = FP_NAN; // Should never happen 1040 break; 1041 } 1042 return c99_class; 1043 } 1044 1045 // This function handle the special subnormal case for float; it will 1046 // become a normal number while casting to double. 1047 // bit_cast is avoided to simplify dependency and to create a code that is 1048 // easy to deploy in C code 1049 inline int fpclassify_float(float x) { 1050 uint32 bitwise_representation; 1051 memcpy(&bitwise_representation, &x, 4); 1052 if ((bitwise_representation & 0x7f800000) == 0 && 1053 (bitwise_representation & 0x007fffff) != 0) 1054 return FP_SUBNORMAL; 1055 return fpclassify_double(x); 1056 } 1057 // 1058 // This define takes care of the denormalized float; the casting to 1059 // double make it a normal number 1060 #define fpclassify(x) \ 1061 ((sizeof(x) == sizeof(float)) ? fpclassify_float(x) : fpclassify_double(x)) 1062 1063 #define isnan _isnan 1064 1065 inline int isinf(double x) { 1066 const int float_point_class = _fpclass(x); 1067 if (float_point_class == _FPCLASS_PINF) return 1; 1068 if (float_point_class == _FPCLASS_NINF) return -1; 1069 return 0; 1070 } 1071 1072 typedef void (*sig_t)(int); 1073 1074 // This actually belongs in errno.h but there's a name conflict in errno 1075 // on WinNT. They (and a ton more) are also found in Winsock2.h, but 1076 // if'd out under NT. We need this subset at minimum. 1077 #define EXFULL ENOMEM // not really that great a translation... 1078 1079 // 1080 // Really from <string.h> 1081 // 1082 1083 inline void bzero(void *s, int n) { memset(s, 0, n); } 1084 1085 // From glob.h 1086 #define __ptr_t void * 1087 1088 // Defined all over the place. 1089 typedef int pid_t; 1090 1091 // From stat.h 1092 typedef unsigned int mode_t; 1093 1094 // u_int16_t, int16_t don't exist in MSVC 1095 typedef unsigned short u_int16_t; 1096 typedef short int16_t; 1097 1098 // ----- END VC++ STUBS & FAKE DEFINITIONS ---------------------------------- 1099 1100 #endif // COMPILER_MSVC 1101 1102 #ifdef STL_MSVC // not always the same as COMPILER_MSVC 1103 #include "base/port_hash.inc" 1104 #else 1105 struct PortableHashBase {}; 1106 #endif 1107 1108 #if defined(OS_WINDOWS) || defined(OS_MACOSX) || defined(OS_IOS) 1109 // gethostbyname() *is* thread-safe for Windows native threads. It is also 1110 // safe on Mac OS X and iOS, where it uses thread-local storage, even though the 1111 // manpages claim otherwise. For details, see 1112 // http://lists.apple.com/archives/Darwin-dev/2006/May/msg00008.html 1113 #else 1114 // gethostbyname() is not thread-safe. So disallow its use. People 1115 // should either use the HostLookup::Lookup*() methods, or gethostbyname_r() 1116 #define gethostbyname gethostbyname_is_not_thread_safe_DO_NOT_USE 1117 #endif 1118 1119 // Define the namespace for pre-C++11 functors for hash_map and hash_set. 1120 // This is not the namespace for C++11 functors (that namespace is "std"). 1121 // 1122 // We used to require that the build tool or Makefile provide this definition. 1123 // Now we usually get it from testing target macros. If the testing target 1124 // macros are different from an external definition, you will get a build 1125 // error. 1126 // 1127 1128 #if defined(__GNUC__) && defined(GOOGLE_GLIBCXX_VERSION) 1129 // Crosstool v17 or later. 1130 #define HASH_NAMESPACE __gnu_cxx 1131 #elif defined(__GNUC__) && defined(STLPORT) 1132 // A version of gcc with stlport. 1133 #define HASH_NAMESPACE std 1134 #elif defined(_MSC_VER) 1135 // MSVC. 1136 // http://msdn.microsoft.com/en-us/library/6x7w9f6z(v=vs.100).aspx 1137 #define HASH_NAMESPACE stdext 1138 #elif defined(__APPLE__) 1139 // Xcode. 1140 #define HASH_NAMESPACE __gnu_cxx 1141 #elif defined(__GNUC__) 1142 // Some other version of gcc. 1143 #define HASH_NAMESPACE __gnu_cxx 1144 #else 1145 // HASH_NAMESPACE defined externally. 1146 #endif 1147 1148 #ifndef HASH_NAMESPACE // NOLINT 1149 // I think gcc 2.95.3 was the last toolchain to use this. 1150 #define HASH_NAMESPACE_DECLARATION_START 1151 #define HASH_NAMESPACE_DECLARATION_END 1152 #else 1153 #define HASH_NAMESPACE_DECLARATION_START namespace HASH_NAMESPACE { 1154 #define HASH_NAMESPACE_DECLARATION_END } 1155 #endif 1156 1157 // Our STL-like classes use __STD. 1158 #if defined(COMPILER_GCC3) || defined(OS_MACOSX) || defined(OS_IOS) || \ 1159 defined(COMPILER_MSVC) 1160 #define __STD std 1161 #endif 1162 1163 #if defined COMPILER_GCC3 1164 #define STREAM_SET(s, bit) (s).setstate(ios_base::bit) 1165 #define STREAM_SETF(s, flag) (s).setf(ios_base::flag) 1166 #else 1167 #define STREAM_SET(s, bit) (s).set(ios::bit) 1168 #define STREAM_SETF(s, flag) (s).setf(ios::flag) 1169 #endif 1170 1171 // Portable handling of unaligned loads, stores, and copies. 1172 // On some platforms, like ARM, the copy functions can be more efficient 1173 // then a load and a store. 1174 // 1175 // It is possible to implement all of these these using constant-length memcpy 1176 // calls, which is portable and will usually be inlined into simple loads and 1177 // stores if the architecture supports it. However, such inlining usually 1178 // happens in a pass that's quite late in compilation, which means the resulting 1179 // loads and stores cannot participate in many other optimizations, leading to 1180 // overall worse code. 1181 1182 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \ 1183 defined(MEMORY_SANITIZER) 1184 // Consider we have an unaligned load/store of 4 bytes from address 0x...05. 1185 // AddressSanitizer will treat it as a 3-byte access to the range 05:07 and 1186 // will miss a bug if 08 is the first unaddressable byte. 1187 // ThreadSanitizer will also treat this as a 3-byte access to 05:07 and will 1188 // miss a race between this access and some other accesses to 08. 1189 // MemorySanitizer will correctly propagate the shadow on unaligned stores 1190 // and correctly report bugs on unaligned loads, but it may not properly 1191 // update and report the origin of the uninitialized memory. 1192 // For all three tools, replacing an unaligned access with a tool-specific 1193 // callback solves the problem. 1194 1195 // Make sure uint16_t/uint32_t/uint64_t are defined. 1196 #include <stdint.h> 1197 1198 #ifdef __cplusplus 1199 extern "C" { 1200 #endif // __cplusplus 1201 uint16_t __sanitizer_unaligned_load16(const void *p); 1202 uint32_t __sanitizer_unaligned_load32(const void *p); 1203 uint64_t __sanitizer_unaligned_load64(const void *p); 1204 void __sanitizer_unaligned_store16(void *p, uint16_t v); 1205 void __sanitizer_unaligned_store32(void *p, uint32_t v); 1206 void __sanitizer_unaligned_store64(void *p, uint64_t v); 1207 #ifdef __cplusplus 1208 } // extern "C" 1209 #endif // __cplusplus 1210 1211 inline uint16 UNALIGNED_LOAD16(const void *p) { 1212 return __sanitizer_unaligned_load16(p); 1213 } 1214 1215 inline uint32 UNALIGNED_LOAD32(const void *p) { 1216 return __sanitizer_unaligned_load32(p); 1217 } 1218 1219 inline uint64 UNALIGNED_LOAD64(const void *p) { 1220 return __sanitizer_unaligned_load64(p); 1221 } 1222 1223 inline void UNALIGNED_STORE16(void *p, uint16 v) { 1224 __sanitizer_unaligned_store16(p, v); 1225 } 1226 1227 inline void UNALIGNED_STORE32(void *p, uint32 v) { 1228 __sanitizer_unaligned_store32(p, v); 1229 } 1230 1231 inline void UNALIGNED_STORE64(void *p, uint64 v) { 1232 __sanitizer_unaligned_store64(p, v); 1233 } 1234 1235 #elif defined(__i386__) || defined(ARCH_K8) || defined(ARCH_PPC) 1236 1237 // x86 and x86-64 can perform unaligned loads/stores directly; 1238 // modern PowerPC hardware can also do unaligned integer loads and stores; 1239 // but note: the FPU still sends unaligned loads and stores to a trap handler! 1240 1241 #define UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p)) 1242 #define UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p)) 1243 #define UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p)) 1244 1245 #define UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val)) 1246 #define UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val)) 1247 #define UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val)) 1248 1249 #elif defined(__arm__) && !defined(__ARM_ARCH_5__) && \ 1250 !defined(__ARM_ARCH_5T__) && !defined(__ARM_ARCH_5TE__) && \ 1251 !defined(__ARM_ARCH_5TEJ__) && !defined(__ARM_ARCH_6__) && \ 1252 !defined(__ARM_ARCH_6J__) && !defined(__ARM_ARCH_6K__) && \ 1253 !defined(__ARM_ARCH_6Z__) && !defined(__ARM_ARCH_6ZK__) && \ 1254 !defined(__ARM_ARCH_6T2__) 1255 1256 // ARMv7 and newer support native unaligned accesses, but only of 16-bit 1257 // and 32-bit values (not 64-bit); older versions either raise a fatal signal, 1258 // do an unaligned read and rotate the words around a bit, or do the reads very 1259 // slowly (trip through kernel mode). There's no simple #define that says just 1260 // ARMv7 or higher, so we have to filter away all ARMv5 and ARMv6 1261 // sub-architectures. Newer gcc (>= 4.6) set an __ARM_FEATURE_ALIGNED #define, 1262 // so in time, maybe we can move on to that. 1263 // 1264 // This is a mess, but there's not much we can do about it. 1265 // 1266 // To further complicate matters, only LDR instructions (single reads) are 1267 // allowed to be unaligned, not LDRD (two reads) or LDM (many reads). Unless we 1268 // explicitly tell the compiler that these accesses can be unaligned, it can and 1269 // will combine accesses. On armcc, the way to signal this is done by accessing 1270 // through the type (uint32 __packed *), but GCC has no such attribute 1271 // (it ignores __attribute__((packed)) on individual variables). However, 1272 // we can tell it that a _struct_ is unaligned, which has the same effect, 1273 // so we do that. 1274 1275 namespace base { 1276 namespace internal { 1277 1278 struct Unaligned16Struct { 1279 uint16 value; 1280 uint8 dummy; // To make the size non-power-of-two. 1281 } ATTRIBUTE_PACKED; 1282 1283 struct Unaligned32Struct { 1284 uint32 value; 1285 uint8 dummy; // To make the size non-power-of-two. 1286 } ATTRIBUTE_PACKED; 1287 1288 } // namespace internal 1289 } // namespace base 1290 1291 #define UNALIGNED_LOAD16(_p) \ 1292 ((reinterpret_cast<const ::base::internal::Unaligned16Struct *>(_p))->value) 1293 #define UNALIGNED_LOAD32(_p) \ 1294 ((reinterpret_cast<const ::base::internal::Unaligned32Struct *>(_p))->value) 1295 1296 #define UNALIGNED_STORE16(_p, _val) \ 1297 ((reinterpret_cast< ::base::internal::Unaligned16Struct *>(_p))->value = \ 1298 (_val)) 1299 #define UNALIGNED_STORE32(_p, _val) \ 1300 ((reinterpret_cast< ::base::internal::Unaligned32Struct *>(_p))->value = \ 1301 (_val)) 1302 1303 // See if that would be more efficient on platforms supporting it, 1304 // at least for copies. 1305 1306 inline uint64 UNALIGNED_LOAD64(const void *p) { 1307 uint64 t; 1308 memcpy(&t, p, sizeof t); 1309 return t; 1310 } 1311 1312 inline void UNALIGNED_STORE64(void *p, uint64 v) { memcpy(p, &v, sizeof v); } 1313 1314 #else 1315 1316 #define NEED_ALIGNED_LOADS 1317 1318 // These functions are provided for architectures that don't support 1319 // unaligned loads and stores. 1320 1321 inline uint16 UNALIGNED_LOAD16(const void *p) { 1322 uint16 t; 1323 memcpy(&t, p, sizeof t); 1324 return t; 1325 } 1326 1327 inline uint32 UNALIGNED_LOAD32(const void *p) { 1328 uint32 t; 1329 memcpy(&t, p, sizeof t); 1330 return t; 1331 } 1332 1333 inline uint64 UNALIGNED_LOAD64(const void *p) { 1334 uint64 t; 1335 memcpy(&t, p, sizeof t); 1336 return t; 1337 } 1338 1339 inline void UNALIGNED_STORE16(void *p, uint16 v) { memcpy(p, &v, sizeof v); } 1340 1341 inline void UNALIGNED_STORE32(void *p, uint32 v) { memcpy(p, &v, sizeof v); } 1342 1343 inline void UNALIGNED_STORE64(void *p, uint64 v) { memcpy(p, &v, sizeof v); } 1344 1345 #endif 1346 1347 // The UNALIGNED_LOADW and UNALIGNED_STOREW macros load and store values 1348 // of type uword_t. 1349 #ifdef _LP64 1350 #define UNALIGNED_LOADW(_p) UNALIGNED_LOAD64(_p) 1351 #define UNALIGNED_STOREW(_p, _val) UNALIGNED_STORE64(_p, _val) 1352 #else 1353 #define UNALIGNED_LOADW(_p) UNALIGNED_LOAD32(_p) 1354 #define UNALIGNED_STOREW(_p, _val) UNALIGNED_STORE32(_p, _val) 1355 #endif 1356 1357 // NOTE(sesse): These are only exported to C++ because the macros they depend on 1358 // use C++-only syntax. This #ifdef can be removed if/when the macros are fixed. 1359 1360 #if defined(__cplusplus) 1361 1362 inline void UnalignedCopy16(const void *src, void *dst) { 1363 UNALIGNED_STORE16(dst, UNALIGNED_LOAD16(src)); 1364 } 1365 1366 inline void UnalignedCopy32(const void *src, void *dst) { 1367 UNALIGNED_STORE32(dst, UNALIGNED_LOAD32(src)); 1368 } 1369 1370 inline void UnalignedCopy64(const void *src, void *dst) { 1371 if (sizeof(void *) == 8) { 1372 UNALIGNED_STORE64(dst, UNALIGNED_LOAD64(src)); 1373 } else { 1374 const char *src_char = reinterpret_cast<const char *>(src); 1375 char *dst_char = reinterpret_cast<char *>(dst); 1376 1377 UNALIGNED_STORE32(dst_char, UNALIGNED_LOAD32(src_char)); 1378 UNALIGNED_STORE32(dst_char + 4, UNALIGNED_LOAD32(src_char + 4)); 1379 } 1380 } 1381 1382 #endif // defined(__cpluscplus) 1383 1384 // printf macros for size_t, in the style of inttypes.h 1385 #if defined(_LP64) || defined(OS_IOS) 1386 #define __PRIS_PREFIX "z" 1387 #else 1388 #define __PRIS_PREFIX 1389 #endif 1390 1391 // Use these macros after a % in a printf format string 1392 // to get correct 32/64 bit behavior, like this: 1393 // size_t size = records.size(); 1394 // printf("%" PRIuS "\n", size); 1395 1396 #define PRIdS __PRIS_PREFIX "d" 1397 #define PRIxS __PRIS_PREFIX "x" 1398 #define PRIuS __PRIS_PREFIX "u" 1399 #define PRIXS __PRIS_PREFIX "X" 1400 #define PRIoS __PRIS_PREFIX "o" 1401 1402 #define GPRIuPTHREAD "lu" 1403 #define GPRIxPTHREAD "lx" 1404 #ifdef OS_CYGWIN 1405 #define PRINTABLE_PTHREAD(pthreadt) reinterpret_cast<uintptr_t>(pthreadt) 1406 #else 1407 #define PRINTABLE_PTHREAD(pthreadt) pthreadt 1408 #endif 1409 1410 #define DDEPTH_SIZEOF_MEMBER(t, f) sizeof(((t *)4096)->f) 1411 1412 #define OFFSETOF_MEMBER(t, f) \ 1413 (reinterpret_cast<char *>(&reinterpret_cast<t *>(16)->f) - \ 1414 reinterpret_cast<char *>(16)) 1415 1416 #ifdef PTHREADS_REDHAT_WIN32 1417 #include <pthread.h> // NOLINT(build/include) 1418 #include <iosfwd> // NOLINT(build/include) 1419 // pthread_t is not a simple integer or pointer on Win32 1420 std::ostream &operator<<(std::ostream &out, const pthread_t &thread_id); 1421 #endif 1422 1423 // GXX_EXPERIMENTAL_CXX0X is defined by gcc and clang up to at least 1424 // gcc-4.7 and clang-3.1 (2011-12-13). __cplusplus was defined to 1 1425 // in gcc before 4.7 (Crosstool 16) and clang before 3.1, but is 1426 // defined according to the language version in effect thereafter. 1427 // Microsoft Visual Studio 14 (2015) sets __cplusplus==199711 despite 1428 // reasonably good C++11 support, so we set LANG_CXX for it and 1429 // newer versions (_MSC_VER >= 1900). Stlport is used by many Android 1430 // projects and does not have full C++11 STL support. 1431 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L || \ 1432 (defined(_MSC_VER) && _MSC_VER >= 1900)) && \ 1433 !defined(STLPORT) 1434 // Define this to 1 if the code is compiled in C++11 mode; leave it 1435 // undefined otherwise. Do NOT define it to 0 -- that causes 1436 // '#ifdef LANG_CXX11' to behave differently from '#if LANG_CXX11'. 1437 #define LANG_CXX11 1 1438 #endif 1439 1440 // On some platforms, a "function pointer" points to a function descriptor 1441 // rather than directly to the function itself. Use FUNC_PTR_TO_CHAR_PTR(func) 1442 // to get a char-pointer to the first instruction of the function func. 1443 #if (defined(__powerpc__) && !(_CALL_ELF > 1)) || defined(__ia64) 1444 // use opd section for function descriptors on these platforms, the function 1445 // address is the first word of the descriptor 1446 enum { kPlatformUsesOPDSections = 1 }; 1447 #define FUNC_PTR_TO_CHAR_PTR(func) (reinterpret_cast<char **>(func)[0]) 1448 #else 1449 enum { kPlatformUsesOPDSections = 0 }; 1450 #define FUNC_PTR_TO_CHAR_PTR(func) (reinterpret_cast<char *>(func)) 1451 #endif 1452 1453 // Private implementation detail: __has_extension is useful to implement 1454 // static_assert, and defining it for all toolchains avoids an extra level of 1455 // nesting of #if/#ifdef/#ifndef. 1456 #ifndef __has_extension // NOLINT 1457 #define __has_extension(x) 0 // MSVC 10's preprocessor can't handle 'false'. 1458 #endif 1459 1460 #ifdef __cplusplus 1461 // We support C++11's static_assert(expression, message) for all C++ 1462 // builds, though for some pre-C++11 toolchains we fall back to using 1463 // GG_PRIVATE_STATIC_ASSERT, which has two limitations: (1) the 1464 // expression argument will need to be parenthesized if it would 1465 // otherwise contain commas outside of parentheses, and (2) the 1466 // message is ignored (though the compiler error will likely mention 1467 // "static_assert_failed" and point to the line with the failing assertion). 1468 1469 // Something else (perhaps libc++) may have provided its own definition of 1470 // static_assert. 1471 #ifndef static_assert // NOLINT 1472 #if LANG_CXX11 || __has_extension(cxx_static_assert) || defined(_MSC_VER) 1473 // There's a native implementation of static_assert, no need to define our own. 1474 #elif __has_extension(c_static_assert) 1475 // C11's _Static_assert is available, and makes a great static_assert. 1476 #define static_assert _Static_assert 1477 #else 1478 // Fall back on our home-grown implementation, with its limitations. 1479 #define static_assert GG_PRIVATE_STATIC_ASSERT 1480 #endif 1481 #endif 1482 1483 // CompileAssert is an implementation detail of COMPILE_ASSERT and 1484 // GG_PRIVATE_STATIC_ASSERT. 1485 template <bool> 1486 struct CompileAssert {}; 1487 1488 // GG_PRIVATE_STATIC_ASSERT: A poor man's static_assert. This doesn't handle 1489 // condition expressions that contain unparenthesized top-level commas; 1490 // write GG_PRIVATE_STATIC_ASSERT((expr), "comment") when needed. 1491 #define GG_PRIVATE_CAT_IMMEDIATE(a, b) a##b 1492 #define GG_PRIVATE_CAT(a, b) GG_PRIVATE_CAT_IMMEDIATE(a, b) 1493 #define GG_PRIVATE_STATIC_ASSERT(expr, ignored) \ 1494 typedef CompileAssert<(static_cast<bool>(expr))> GG_PRIVATE_CAT( \ 1495 static_assert_failed_at_line, \ 1496 __LINE__)[bool(expr) ? 1 : -1] DDEPTH_ATTRIBUTE_UNUSED // NOLINT 1497 1498 #endif // __cplusplus 1499 1500 // Some platforms have a ::string class that is different from ::std::string 1501 // (although the interface is the same, of course). On other platforms, 1502 // ::string is the same as ::std::string. 1503 #if defined(__cplusplus) && !defined(SWIG) 1504 #include <string> 1505 #ifndef HAS_GLOBAL_STRING // NOLINT 1506 using std::basic_string; 1507 using std::string; 1508 #endif // HAS_GLOBAL_STRING 1509 #endif // SWIG, __cplusplus 1510 1511 #endif // DYNAMIC_DEPTH_INTERNAL_BASE_PORT_H_ // NOLINT 1512