Branch data Line data Source code
# 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 : :
# 5 : : #ifndef STORAGE_LEVELDB_PORT_PORT_STDCXX_H_
# 6 : : #define STORAGE_LEVELDB_PORT_PORT_STDCXX_H_
# 7 : :
# 8 : : // port/port_config.h availability is automatically detected via __has_include
# 9 : : // in newer compilers. If LEVELDB_HAS_PORT_CONFIG_H is defined, it overrides the
# 10 : : // configuration detection.
# 11 : : #if defined(LEVELDB_HAS_PORT_CONFIG_H)
# 12 : :
# 13 : : #if LEVELDB_HAS_PORT_CONFIG_H
# 14 : : #include "port/port_config.h"
# 15 : : #endif // LEVELDB_HAS_PORT_CONFIG_H
# 16 : :
# 17 : : #elif defined(__has_include)
# 18 : :
# 19 : : #if __has_include("port/port_config.h")
# 20 : : #include "port/port_config.h"
# 21 : : #endif // __has_include("port/port_config.h")
# 22 : :
# 23 : : #endif // defined(LEVELDB_HAS_PORT_CONFIG_H)
# 24 : :
# 25 : : #if HAVE_CRC32C
# 26 : : #include <crc32c/crc32c.h>
# 27 : : #endif // HAVE_CRC32C
# 28 : : #if HAVE_SNAPPY
# 29 : : #include <snappy.h>
# 30 : : #endif // HAVE_SNAPPY
# 31 : :
# 32 : : #include <cassert>
# 33 : : #include <condition_variable> // NOLINT
# 34 : : #include <cstddef>
# 35 : : #include <cstdint>
# 36 : : #include <mutex> // NOLINT
# 37 : : #include <string>
# 38 : :
# 39 : : #include "port/thread_annotations.h"
# 40 : :
# 41 : : namespace leveldb {
# 42 : : namespace port {
# 43 : :
# 44 : : static const bool kLittleEndian = !LEVELDB_IS_BIG_ENDIAN;
# 45 : :
# 46 : : class CondVar;
# 47 : :
# 48 : : // Thinly wraps std::mutex.
# 49 : : class LOCKABLE Mutex {
# 50 : : public:
# 51 : 85406 : Mutex() = default;
# 52 : 83890 : ~Mutex() = default;
# 53 : :
# 54 : : Mutex(const Mutex&) = delete;
# 55 : : Mutex& operator=(const Mutex&) = delete;
# 56 : :
# 57 : 27224334 : void Lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.lock(); }
# 58 : 27224267 : void Unlock() UNLOCK_FUNCTION() { mu_.unlock(); }
# 59 : 36449 : void AssertHeld() ASSERT_EXCLUSIVE_LOCK() {}
# 60 : :
# 61 : : private:
# 62 : : friend class CondVar;
# 63 : : std::mutex mu_;
# 64 : : };
# 65 : :
# 66 : : // Thinly wraps std::condition_variable.
# 67 : : class CondVar {
# 68 : : public:
# 69 : 16612 : explicit CondVar(Mutex* mu) : mu_(mu) { assert(mu != nullptr); }
# 70 : 15871 : ~CondVar() = default;
# 71 : :
# 72 : : CondVar(const CondVar&) = delete;
# 73 : : CondVar& operator=(const CondVar&) = delete;
# 74 : :
# 75 : 111 : void Wait() {
# 76 : 111 : std::unique_lock<std::mutex> lock(mu_->mu_, std::adopt_lock);
# 77 : 111 : cv_.wait(lock);
# 78 : 111 : lock.release();
# 79 : 111 : }
# 80 : 111 : void Signal() { cv_.notify_one(); }
# 81 : 111 : void SignalAll() { cv_.notify_all(); }
# 82 : :
# 83 : : private:
# 84 : : std::condition_variable cv_;
# 85 : : Mutex* const mu_;
# 86 : : };
# 87 : :
# 88 : : inline bool Snappy_Compress(const char* input, size_t length,
# 89 : 0 : std::string* output) {
# 90 : : #if HAVE_SNAPPY
# 91 : : output->resize(snappy::MaxCompressedLength(length));
# 92 : : size_t outlen;
# 93 : : snappy::RawCompress(input, length, &(*output)[0], &outlen);
# 94 : : output->resize(outlen);
# 95 : : return true;
# 96 : : #else
# 97 : : // Silence compiler warnings about unused arguments.
# 98 : 0 : (void)input;
# 99 : 0 : (void)length;
# 100 : 0 : (void)output;
# 101 : 0 : #endif // HAVE_SNAPPY
# 102 : :
# 103 : 0 : return false;
# 104 : 0 : }
# 105 : :
# 106 : : inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
# 107 : 0 : size_t* result) {
# 108 : : #if HAVE_SNAPPY
# 109 : : return snappy::GetUncompressedLength(input, length, result);
# 110 : : #else
# 111 : : // Silence compiler warnings about unused arguments.
# 112 : 0 : (void)input;
# 113 : 0 : (void)length;
# 114 : 0 : (void)result;
# 115 : 0 : return false;
# 116 : 0 : #endif // HAVE_SNAPPY
# 117 : 0 : }
# 118 : :
# 119 : 0 : inline bool Snappy_Uncompress(const char* input, size_t length, char* output) {
# 120 : : #if HAVE_SNAPPY
# 121 : : return snappy::RawUncompress(input, length, output);
# 122 : : #else
# 123 : : // Silence compiler warnings about unused arguments.
# 124 : 0 : (void)input;
# 125 : 0 : (void)length;
# 126 : 0 : (void)output;
# 127 : 0 : return false;
# 128 : 0 : #endif // HAVE_SNAPPY
# 129 : 0 : }
# 130 : :
# 131 : 0 : inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
# 132 : 0 : // Silence compiler warnings about unused arguments.
# 133 : 0 : (void)func;
# 134 : 0 : (void)arg;
# 135 : 0 : return false;
# 136 : 0 : }
# 137 : :
# 138 : 99407 : inline uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size) {
# 139 : 99407 : #if HAVE_CRC32C
# 140 : 99407 : return ::crc32c::Extend(crc, reinterpret_cast<const uint8_t*>(buf), size);
# 141 : : #else
# 142 : : // Silence compiler warnings about unused arguments.
# 143 : : (void)crc;
# 144 : : (void)buf;
# 145 : : (void)size;
# 146 : : return 0;
# 147 : : #endif // HAVE_CRC32C
# 148 : 99407 : }
# 149 : :
# 150 : : } // namespace port
# 151 : : } // namespace leveldb
# 152 : :
# 153 : : #endif // STORAGE_LEVELDB_PORT_PORT_STDCXX_H_
|