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
  Mutex() = default;
52
  ~Mutex() = default;
53

            
54
  Mutex(const Mutex&) = delete;
55
  Mutex& operator=(const Mutex&) = delete;
56

            
57
  void Lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.lock(); }
58
  void Unlock() UNLOCK_FUNCTION() { mu_.unlock(); }
59
  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
  explicit CondVar(Mutex* mu) : mu_(mu) { assert(mu != nullptr); }
70
  ~CondVar() = default;
71

            
72
  CondVar(const CondVar&) = delete;
73
  CondVar& operator=(const CondVar&) = delete;
74

            
75
  void Wait() {
76
    std::unique_lock<std::mutex> lock(mu_->mu_, std::adopt_lock);
77
    cv_.wait(lock);
78
    lock.release();
79
  }
80
  void Signal() { cv_.notify_one(); }
81
  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
                            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
  (void)input;
99
  (void)length;
100
  (void)output;
101
#endif  // HAVE_SNAPPY
102

            
103
  return false;
104
}
105

            
106
inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
107
                                         size_t* result) {
108
#if HAVE_SNAPPY
109
  return snappy::GetUncompressedLength(input, length, result);
110
#else
111
  // Silence compiler warnings about unused arguments.
112
  (void)input;
113
  (void)length;
114
  (void)result;
115
  return false;
116
#endif  // HAVE_SNAPPY
117
}
118

            
119
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
  (void)input;
125
  (void)length;
126
  (void)output;
127
  return false;
128
#endif  // HAVE_SNAPPY
129
}
130

            
131
inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
132
  // Silence compiler warnings about unused arguments.
133
  (void)func;
134
  (void)arg;
135
  return false;
136
}
137

            
138
inline uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size) {
139
#if HAVE_CRC32C
140
  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
}
149

            
150
}  // namespace port
151
}  // namespace leveldb
152

            
153
#endif  // STORAGE_LEVELDB_PORT_PORT_STDCXX_H_