Skip to content

Commit b13a68e

Browse files
committed
Squashed 'src/leveldb/' changes from 196962f..c521b3a
c521b3a Merge #11: fixup define checks. Cleans up some oopses from #5. 8b1cd37 fixup define checks. Cleans up some oopses from #5. 6b1508d Merge #6: Fixes typo fceb805 Merge #10: Clean up compile-time warnings (gcc 7.1) 0ec2a34 Clean up compile-time warnings (gcc 7.1) d4c268a Merge #5: Move helper functions out of sse4.2 object 8d4eb08 Add HasAcceleratedCRC32C to port_win.h 77cfbfd25 crc32: move helper functions out of port_posix_sse.cc 4c1e9e0 silence compiler warnings about uninitialized variables 4953164 Merge #2: Prefer std::atomic over MemoryBarrier 2953978 Fixes typo f134284 Merge #1: Merge upstream LevelDB 1.20 ba8a445 Prefer std::atomic over MemoryBarrier git-subtree-dir: src/leveldb git-subtree-split: c521b3a
1 parent cf44e4c commit b13a68e

12 files changed

+64
-45
lines changed

db/memtable.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void MemTable::Add(SequenceNumber s, ValueType type,
101101
p += 8;
102102
p = EncodeVarint32(p, val_size);
103103
memcpy(p, value.data(), val_size);
104-
assert((p + val_size) - buf == encoded_len);
104+
assert(p + val_size == buf + encoded_len);
105105
table_.Insert(buf);
106106
}
107107

db/version_set.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
namespace leveldb {
2222

23-
static int TargetFileSize(const Options* options) {
23+
static size_t TargetFileSize(const Options* options) {
2424
return options->max_file_size;
2525
}
2626

db/version_set.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ class Compaction {
376376
// Each compaction reads inputs from "level_" and "level_+1"
377377
std::vector<FileMetaData*> inputs_[2]; // The two sets of inputs
378378

379-
// State used to check for number of of overlapping grandparent files
379+
// State used to check for number of overlapping grandparent files
380380
// (parent == level_ + 1, grandparent == level_ + 2)
381381
std::vector<FileMetaData*> grandparents_;
382382
size_t grandparent_index_; // Index in grandparent_starts_

port/atomic_pointer.h

+25-22
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,30 @@
4646
namespace leveldb {
4747
namespace port {
4848

49+
// AtomicPointer based on <cstdatomic> if available
50+
#if defined(LEVELDB_ATOMIC_PRESENT)
51+
class AtomicPointer {
52+
private:
53+
std::atomic<void*> rep_;
54+
public:
55+
AtomicPointer() { }
56+
explicit AtomicPointer(void* v) : rep_(v) { }
57+
inline void* Acquire_Load() const {
58+
return rep_.load(std::memory_order_acquire);
59+
}
60+
inline void Release_Store(void* v) {
61+
rep_.store(v, std::memory_order_release);
62+
}
63+
inline void* NoBarrier_Load() const {
64+
return rep_.load(std::memory_order_relaxed);
65+
}
66+
inline void NoBarrier_Store(void* v) {
67+
rep_.store(v, std::memory_order_relaxed);
68+
}
69+
};
70+
71+
#else
72+
4973
// Define MemoryBarrier() if available
5074
// Windows on x86
5175
#if defined(OS_WIN) && defined(COMPILER_MSVC) && defined(ARCH_CPU_X86_FAMILY)
@@ -142,28 +166,6 @@ class AtomicPointer {
142166
}
143167
};
144168

145-
// AtomicPointer based on <cstdatomic>
146-
#elif defined(LEVELDB_ATOMIC_PRESENT)
147-
class AtomicPointer {
148-
private:
149-
std::atomic<void*> rep_;
150-
public:
151-
AtomicPointer() { }
152-
explicit AtomicPointer(void* v) : rep_(v) { }
153-
inline void* Acquire_Load() const {
154-
return rep_.load(std::memory_order_acquire);
155-
}
156-
inline void Release_Store(void* v) {
157-
rep_.store(v, std::memory_order_release);
158-
}
159-
inline void* NoBarrier_Load() const {
160-
return rep_.load(std::memory_order_relaxed);
161-
}
162-
inline void NoBarrier_Store(void* v) {
163-
rep_.store(v, std::memory_order_relaxed);
164-
}
165-
};
166-
167169
// Atomic pointer based on sparc memory barriers
168170
#elif defined(__sparcv9) && defined(__GNUC__)
169171
class AtomicPointer {
@@ -228,6 +230,7 @@ class AtomicPointer {
228230
#else
229231
#error Please implement AtomicPointer for this platform.
230232

233+
#endif
231234
#endif
232235

233236
#undef LEVELDB_HAVE_MEMORY_BARRIER

port/port_example.h

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ extern bool Snappy_Uncompress(const char* input_data, size_t input_length,
129129
// The concatenation of all "data[0,n-1]" fragments is the heap profile.
130130
extern bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg);
131131

132+
// Determine whether a working accelerated crc32 implementation exists
133+
// Returns true if AcceleratedCRC32C is safe to call
134+
bool HasAcceleratedCRC32C();
135+
132136
// Extend the CRC to include the first n bytes of buf.
133137
//
134138
// Returns zero if the CRC cannot be extended using acceleration, else returns

port/port_posix.cc

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#include <stdio.h>
99
#include <string.h>
1010

11+
#if (defined(__x86_64__) || defined(__i386__)) && defined(__GNUC__)
12+
#include <cpuid.h>
13+
#endif
14+
1115
namespace leveldb {
1216
namespace port {
1317

@@ -49,5 +53,15 @@ void InitOnce(OnceType* once, void (*initializer)()) {
4953
PthreadCall("once", pthread_once(once, initializer));
5054
}
5155

56+
bool HasAcceleratedCRC32C() {
57+
#if (defined(__x86_64__) || defined(__i386__)) && defined(__GNUC__)
58+
unsigned int eax, ebx, ecx, edx;
59+
__get_cpuid(1, &eax, &ebx, &ecx, &edx);
60+
return (ecx & (1 << 20)) != 0;
61+
#else
62+
return false;
63+
#endif
64+
}
65+
5266
} // namespace port
5367
} // namespace leveldb

port/port_posix.h

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
152152
return false;
153153
}
154154

155+
bool HasAcceleratedCRC32C();
155156
uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size);
156157

157158
} // namespace port

port/port_posix_sse.cc

-19
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <intrin.h>
2020
#elif defined(__GNUC__) && defined(__SSE4_2__)
2121
#include <nmmintrin.h>
22-
#include <cpuid.h>
2322
#endif
2423

2524
#endif // defined(LEVELDB_PLATFORM_POSIX_SSE)
@@ -48,20 +47,6 @@ static inline uint64_t LE_LOAD64(const uint8_t *p) {
4847

4948
#endif // defined(_M_X64) || defined(__x86_64__)
5049

51-
static inline bool HaveSSE42() {
52-
#if defined(_MSC_VER)
53-
int cpu_info[4];
54-
__cpuid(cpu_info, 1);
55-
return (cpu_info[2] & (1 << 20)) != 0;
56-
#elif defined(__GNUC__)
57-
unsigned int eax, ebx, ecx, edx;
58-
__get_cpuid(1, &eax, &ebx, &ecx, &edx);
59-
return (ecx & (1 << 20)) != 0;
60-
#else
61-
return false;
62-
#endif
63-
}
64-
6550
#endif // defined(LEVELDB_PLATFORM_POSIX_SSE)
6651

6752
// For further improvements see Intel publication at:
@@ -70,10 +55,6 @@ uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size) {
7055
#if !defined(LEVELDB_PLATFORM_POSIX_SSE)
7156
return 0;
7257
#else
73-
static bool have = HaveSSE42();
74-
if (!have) {
75-
return 0;
76-
}
7758

7859
const uint8_t *p = reinterpret_cast<const uint8_t *>(buf);
7960
const uint8_t *e = p + size;

port/port_win.cc

+11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include <windows.h>
3434
#include <cassert>
35+
#include <intrin.h>
3536

3637
namespace leveldb {
3738
namespace port {
@@ -143,5 +144,15 @@ void AtomicPointer::NoBarrier_Store(void* v) {
143144
rep_ = v;
144145
}
145146

147+
bool HasAcceleratedCRC32C() {
148+
#if defined(__x86_64__) || defined(__i386__)
149+
int cpu_info[4];
150+
__cpuid(cpu_info, 1);
151+
return (cpu_info[2] & (1 << 20)) != 0;
152+
#else
153+
return false;
154+
#endif
155+
}
156+
146157
}
147158
}

port/port_win.h

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
168168
return false;
169169
}
170170

171+
bool HasAcceleratedCRC32C();
171172
uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size);
172173

173174
}

util/crc32c.cc

+4
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ static inline uint32_t LE_LOAD32(const uint8_t *p) {
288288
// Determine if the CPU running this program can accelerate the CRC32C
289289
// calculation.
290290
static bool CanAccelerateCRC32C() {
291+
if (!port::HasAcceleratedCRC32C())
292+
return false;
293+
294+
// Double-check that the accelerated implementation functions correctly.
291295
// port::AcceleretedCRC32C returns zero when unable to accelerate.
292296
static const char kTestCRCBuffer[] = "TestCRCBuffer";
293297
static const char kBufSize = sizeof(kTestCRCBuffer) - 1;

util/logging.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool ConsumeDecimalNumber(Slice* in, uint64_t* val) {
4949
uint64_t v = 0;
5050
int digits = 0;
5151
while (!in->empty()) {
52-
char c = (*in)[0];
52+
unsigned char c = (*in)[0];
5353
if (c >= '0' && c <= '9') {
5454
++digits;
5555
const int delta = (c - '0');

0 commit comments

Comments
 (0)