port_stdcxx.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2018 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #ifndef STORAGE_LEVELDB_PORT_PORT_STDCXX_H_
  5. #define STORAGE_LEVELDB_PORT_PORT_STDCXX_H_
  6. // port/port_config.h availability is automatically detected via __has_include
  7. // in newer compilers. If LEVELDB_HAS_PORT_CONFIG_H is defined, it overrides the
  8. // configuration detection.
  9. #if defined(LEVELDB_HAS_PORT_CONFIG_H)
  10. #if LEVELDB_HAS_PORT_CONFIG_H
  11. #include "port/port_config.h"
  12. #endif // LEVELDB_HAS_PORT_CONFIG_H
  13. #elif defined(__has_include)
  14. #if __has_include("port/port_config.h")
  15. #include "port/port_config.h"
  16. #endif // __has_include("port/port_config.h")
  17. #endif // defined(LEVELDB_HAS_PORT_CONFIG_H)
  18. #if HAVE_CRC32C
  19. #include <crc32c/crc32c.h>
  20. #endif // HAVE_CRC32C
  21. #if HAVE_SNAPPY
  22. #include <snappy.h>
  23. #endif // HAVE_SNAPPY
  24. #include <cassert>
  25. #include <cstddef>
  26. #include <cstdint>
  27. #include <condition_variable> // NOLINT
  28. #include <mutex> // NOLINT
  29. #include <string>
  30. #include "port/thread_annotations.h"
  31. namespace leveldb {
  32. namespace port {
  33. static const bool kLittleEndian = !LEVELDB_IS_BIG_ENDIAN;
  34. class CondVar;
  35. // Thinly wraps std::mutex.
  36. class LOCKABLE Mutex {
  37. public:
  38. Mutex() = default;
  39. ~Mutex() = default;
  40. Mutex(const Mutex&) = delete;
  41. Mutex& operator=(const Mutex&) = delete;
  42. void Lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.lock(); }
  43. void Unlock() UNLOCK_FUNCTION() { mu_.unlock(); }
  44. void AssertHeld() ASSERT_EXCLUSIVE_LOCK() { }
  45. private:
  46. friend class CondVar;
  47. std::mutex mu_;
  48. };
  49. // Thinly wraps std::condition_variable.
  50. class CondVar {
  51. public:
  52. explicit CondVar(Mutex* mu) : mu_(mu) { assert(mu != nullptr); }
  53. ~CondVar() = default;
  54. CondVar(const CondVar&) = delete;
  55. CondVar& operator=(const CondVar&) = delete;
  56. void Wait() {
  57. std::unique_lock<std::mutex> lock(mu_->mu_, std::adopt_lock);
  58. cv_.wait(lock);
  59. lock.release();
  60. }
  61. void Signal() { cv_.notify_one(); }
  62. void SignalAll() { cv_.notify_all(); }
  63. private:
  64. std::condition_variable cv_;
  65. Mutex* const mu_;
  66. };
  67. inline bool Snappy_Compress(const char* input, size_t length,
  68. std::string* output) {
  69. #if HAVE_SNAPPY
  70. output->resize(snappy::MaxCompressedLength(length));
  71. size_t outlen;
  72. snappy::RawCompress(input, length, &(*output)[0], &outlen);
  73. output->resize(outlen);
  74. return true;
  75. #else
  76. // Silence compiler warnings about unused arguments.
  77. (void)input; (void)length; (void)output;
  78. #endif // HAVE_SNAPPY
  79. return false;
  80. }
  81. inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
  82. size_t* result) {
  83. #if HAVE_SNAPPY
  84. return snappy::GetUncompressedLength(input, length, result);
  85. #else
  86. // Silence compiler warnings about unused arguments.
  87. (void)input; (void)length; (void)result;
  88. return false;
  89. #endif // HAVE_SNAPPY
  90. }
  91. inline bool Snappy_Uncompress(const char* input, size_t length, char* output) {
  92. #if HAVE_SNAPPY
  93. return snappy::RawUncompress(input, length, output);
  94. #else
  95. // Silence compiler warnings about unused arguments.
  96. (void)input; (void)length; (void)output;
  97. return false;
  98. #endif // HAVE_SNAPPY
  99. }
  100. inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
  101. // Silence compiler warnings about unused arguments.
  102. (void)func; (void)arg;
  103. return false;
  104. }
  105. inline uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size) {
  106. #if HAVE_CRC32C
  107. return ::crc32c::Extend(crc, reinterpret_cast<const uint8_t*>(buf), size);
  108. #else
  109. // Silence compiler warnings about unused arguments.
  110. (void)crc; (void)buf; (void)size;
  111. return 0;
  112. #endif // HAVE_CRC32C
  113. }
  114. } // namespace port
  115. } // namespace leveldb
  116. #endif // STORAGE_LEVELDB_PORT_PORT_STDCXX_H_