1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
/*
Copyright (C) 2015 The Qt Company Ltd
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef STORAGE_LEVELDB_PORT_PORT_QT_H_
#define STORAGE_LEVELDB_PORT_PORT_QT_H_
#include <QtCore/qatomic.h>
#include <QMutex>
#include <QWaitCondition>
#include <string>
#ifdef SNAPPY
#include <snappy.h>
#endif
#ifdef Q_CC_MSVC
#include "win/stdint.h"
#define snprintf _snprintf
#else
#include <stdint.h>
#endif
namespace leveldb {
namespace port {
static const bool kLittleEndian = (Q_BYTE_ORDER == Q_LITTLE_ENDIAN);
class Mutex : public QMutex {
public:
Mutex() { }
void Lock() { lock(); }
void Unlock() { unlock(); }
void AssertHeld() { Q_ASSERT(!tryLock()); }
};
class CondVar : public QWaitCondition {
public:
explicit CondVar(Mutex* mu) : m_mutex(mu) { }
void Wait() { wait(m_mutex); }
void Signal() { wakeOne(); }
void SignalAll() { wakeAll(); }
private:
Mutex* m_mutex;
};
#if QT_VERSION >= QT_VERSION_CHECK(5, 3, 0)
typedef QAtomicInteger<int> OnceType;
#else
typedef QAtomicInt OnceType;
#endif
#define LEVELDB_ONCE_INIT 0
inline void InitOnce(port::OnceType* once, void (*initializer)())
{
if (once->testAndSetAcquire(0, 1))
initializer();
}
class AtomicPointer : public QAtomicPointer<void> {
public:
AtomicPointer() { }
AtomicPointer(void *p) : QAtomicPointer<void>(p) { }
void* Acquire_Load() const { return loadAcquire(); }
void Release_Store(void* p) { storeRelease(p); }
void* NoBarrier_Load() const { return load(); }
void NoBarrier_Store(void* p) { store(p); }
};
// SNAPPY, non Qt specific:
inline bool Snappy_Compress(const char* input, size_t length,
::std::string* output) {
#ifdef SNAPPY
output->resize(snappy::MaxCompressedLength(length));
size_t outlen;
snappy::RawCompress(input, length, &(*output)[0], &outlen);
output->resize(outlen);
return true;
#endif
return false;
}
inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
size_t* result) {
#ifdef SNAPPY
return snappy::GetUncompressedLength(input, length, result);
#else
return false;
#endif
}
inline bool Snappy_Uncompress(const char* input, size_t length,
char* output) {
#ifdef SNAPPY
return snappy::RawUncompress(input, length, output);
#else
return false;
#endif
}
inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
return false;
}
} // namespace port
} // namespace leveldb
#endif // STORAGE_LEVELDB_PORT_PORT_QT_H_
|