port_stdcxx.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <condition_variable> // NOLINT
  26. #include <cstddef>
  27. #include <cstdint>
  28. #include <mutex> // NOLINT
  29. #include <string>
  30. #include "port/thread_annotations.h"
  31. namespace leveldb {
  32. namespace port {
  33. class CondVar;
  34. // Thinly wraps std::mutex.
  35. class LOCKABLE Mutex {
  36. public:
  37. Mutex() = default;
  38. ~Mutex() = default;
  39. Mutex(const Mutex&) = delete;
  40. Mutex& operator=(const Mutex&) = delete;
  41. void Lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.lock(); }
  42. void Unlock() UNLOCK_FUNCTION() { mu_.unlock(); }
  43. void AssertHeld() ASSERT_EXCLUSIVE_LOCK() {}
  44. private:
  45. friend class CondVar;
  46. std::mutex mu_;
  47. };
  48. // Thinly wraps std::condition_variable.
  49. class CondVar {
  50. public:
  51. explicit CondVar(Mutex* mu) : mu_(mu) { assert(mu != nullptr); }
  52. ~CondVar() = default;
  53. CondVar(const CondVar&) = delete;
  54. CondVar& operator=(const CondVar&) = delete;
  55. void Wait() {
  56. std::unique_lock<std::mutex> lock(mu_->mu_, std::adopt_lock);
  57. cv_.wait(lock);
  58. lock.release();
  59. }
  60. void Signal() { cv_.notify_one(); }
  61. void SignalAll() { cv_.notify_all(); }
  62. private:
  63. std::condition_variable cv_;
  64. Mutex* const mu_;
  65. };
  66. inline bool Snappy_Compress(const char* input, size_t length,
  67. std::string* output) {
  68. #if HAVE_SNAPPY
  69. output->resize(snappy::MaxCompressedLength(length));
  70. size_t outlen;
  71. snappy::RawCompress(input, length, &(*output)[0], &outlen);
  72. output->resize(outlen);
  73. return true;
  74. #else
  75. // Silence compiler warnings about unused arguments.
  76. (void)input;
  77. (void)length;
  78. (void)output;
  79. #endif // HAVE_SNAPPY
  80. return false;
  81. }
  82. inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
  83. size_t* result) {
  84. #if HAVE_SNAPPY
  85. return snappy::GetUncompressedLength(input, length, result);
  86. #else
  87. // Silence compiler warnings about unused arguments.
  88. (void)input;
  89. (void)length;
  90. (void)result;
  91. return false;
  92. #endif // HAVE_SNAPPY
  93. }
  94. inline bool Snappy_Uncompress(const char* input, size_t length, char* output) {
  95. #if HAVE_SNAPPY
  96. return snappy::RawUncompress(input, length, output);
  97. #else
  98. // Silence compiler warnings about unused arguments.
  99. (void)input;
  100. (void)length;
  101. (void)output;
  102. return false;
  103. #endif // HAVE_SNAPPY
  104. }
  105. inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
  106. // Silence compiler warnings about unused arguments.
  107. (void)func;
  108. (void)arg;
  109. return false;
  110. }
  111. inline uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size) {
  112. #if HAVE_CRC32C
  113. return ::crc32c::Extend(crc, reinterpret_cast<const uint8_t*>(buf), size);
  114. #else
  115. // Silence compiler warnings about unused arguments.
  116. (void)crc;
  117. (void)buf;
  118. (void)size;
  119. return 0;
  120. #endif // HAVE_CRC32C
  121. }
  122. } // namespace port
  123. } // namespace leveldb
  124. #endif // STORAGE_LEVELDB_PORT_PORT_STDCXX_H_