lang.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #ifndef FALLTHROUGH_INTENDED
  7. #if defined(__clang__)
  8. #define FALLTHROUGH_INTENDED [[clang::fallthrough]]
  9. #elif defined(__GNUC__) && __GNUC__ >= 7
  10. #define FALLTHROUGH_INTENDED [[gnu::fallthrough]]
  11. #else
  12. #define FALLTHROUGH_INTENDED \
  13. do { \
  14. } while (0)
  15. #endif
  16. #endif
  17. #define DECLARE_DEFAULT_MOVES(Name) \
  18. Name(Name&&) noexcept = default; \
  19. Name& operator=(Name&&) = default
  20. // ASAN (Address sanitizer)
  21. #if defined(__clang__)
  22. #if defined(__has_feature)
  23. #if __has_feature(address_sanitizer)
  24. #define MUST_FREE_HEAP_ALLOCATIONS 1
  25. #endif // __has_feature(address_sanitizer)
  26. #endif // defined(__has_feature)
  27. #else // __clang__
  28. #ifdef __SANITIZE_ADDRESS__
  29. #define MUST_FREE_HEAP_ALLOCATIONS 1
  30. #endif // __SANITIZE_ADDRESS__
  31. #endif // __clang__
  32. #ifdef ROCKSDB_VALGRIND_RUN
  33. #define MUST_FREE_HEAP_ALLOCATIONS 1
  34. #endif // ROCKSDB_VALGRIND_RUN
  35. // Coding guidelines say to avoid static objects with non-trivial destructors,
  36. // because it's easy to cause trouble (UB) in static destruction. This
  37. // macro makes it easier to define static objects that are normally never
  38. // destructed, except are destructed when running under ASAN. This should
  39. // avoid unexpected, unnecessary destruction behavior in production.
  40. // Note that constructor arguments can be provided as in
  41. // STATIC_AVOID_DESTRUCTION(Foo, foo)(arg1, arg2);
  42. #ifdef MUST_FREE_HEAP_ALLOCATIONS
  43. #define STATIC_AVOID_DESTRUCTION(Type, name) static Type name
  44. constexpr bool kMustFreeHeapAllocations = true;
  45. #else
  46. #define STATIC_AVOID_DESTRUCTION(Type, name) static Type& name = *new Type
  47. constexpr bool kMustFreeHeapAllocations = false;
  48. #endif
  49. // TSAN (Thread sanitizer)
  50. // For simplicity, standardize on the GCC define
  51. #if defined(__clang__)
  52. #if defined(__has_feature) && __has_feature(thread_sanitizer)
  53. #define __SANITIZE_THREAD__ 1
  54. #endif // __has_feature(thread_sanitizer)
  55. #endif // __clang__
  56. #ifdef __SANITIZE_THREAD__
  57. #define TSAN_SUPPRESSION __attribute__((no_sanitize("thread")))
  58. #else
  59. #define TSAN_SUPPRESSION
  60. #endif // TSAN_SUPPRESSION
  61. // Compile-time CPU feature testing compatibility
  62. //
  63. // A way to be extra sure these defines have been included.
  64. #define ASSERT_FEATURE_COMPAT_HEADER() \
  65. static_assert(true, "Semicolon required") /* empty */
  66. // MSVC doesn't support the same defines that gcc and clang provide
  67. // but does some like __AVX__. Here we can infer some features from others.
  68. #ifdef __AVX__
  69. #define __SSE4_2__ 1
  70. #define __PCLMUL__ 1
  71. #endif // __AVX__
  72. // A way to disable PCLMUL
  73. #ifdef NO_PCLMUL
  74. #undef __PCLMUL__
  75. #endif
  76. // popcnt is generally implied by SSE4.2
  77. #if defined(__SSE4_2__)
  78. #define __POPCNT__ 1
  79. #endif
  80. // A way to disable POPCNT
  81. #ifdef NO_POPCNT
  82. #undef __POPCNT__
  83. #endif