clang format files under port/ (#10849)

Summary:
Run "clang-format" against files under port to make it happy.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/10849

Test Plan: Watch existing CI to pass.

Reviewed By: anand1976

Differential Revision: D40645839

fbshipit-source-id: 582b4215503223795cf6234af90cc4e8e4eba773
This commit is contained in:
sdong 2022-10-24 16:56:01 -07:00 committed by Facebook GitHub Bot
parent 4d9cb433fa
commit 7cf27eae0a
16 changed files with 189 additions and 222 deletions

View File

@ -11,7 +11,9 @@
#elif defined(__GNUC__) && __GNUC__ >= 7
#define FALLTHROUGH_INTENDED [[gnu::fallthrough]]
#else
#define FALLTHROUGH_INTENDED do {} while (0)
#define FALLTHROUGH_INTENDED \
do { \
} while (0)
#endif
#endif

View File

@ -10,9 +10,9 @@
#pragma once
#if defined(__GNUC__) && __GNUC__ >= 4
#define LIKELY(x) (__builtin_expect((x), 1))
#define LIKELY(x) (__builtin_expect((x), 1))
#define UNLIKELY(x) (__builtin_expect((x), 0))
#else
#define LIKELY(x) (x)
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#endif

View File

@ -33,11 +33,11 @@ int closedir(DIR* dirp);
} // namespace port
using port::dirent;
using port::closedir;
using port::DIR;
using port::dirent;
using port::opendir;
using port::readdir;
using port::closedir;
} // namespace ROCKSDB_NAMESPACE

View File

@ -57,23 +57,21 @@ static int PthreadCall(const char* label, int result) {
}
Mutex::Mutex(bool adaptive) {
(void) adaptive;
(void)adaptive;
#ifdef ROCKSDB_PTHREAD_ADAPTIVE_MUTEX
if (!adaptive) {
PthreadCall("init mutex", pthread_mutex_init(&mu_, nullptr));
} else {
pthread_mutexattr_t mutex_attr;
PthreadCall("init mutex attr", pthread_mutexattr_init(&mutex_attr));
PthreadCall("set mutex attr",
pthread_mutexattr_settype(&mutex_attr,
PTHREAD_MUTEX_ADAPTIVE_NP));
PthreadCall("set mutex attr", pthread_mutexattr_settype(
&mutex_attr, PTHREAD_MUTEX_ADAPTIVE_NP));
PthreadCall("init mutex", pthread_mutex_init(&mu_, &mutex_attr));
PthreadCall("destroy mutex attr",
pthread_mutexattr_destroy(&mutex_attr));
PthreadCall("destroy mutex attr", pthread_mutexattr_destroy(&mutex_attr));
}
#else
PthreadCall("init mutex", pthread_mutex_init(&mu_, nullptr));
#endif // ROCKSDB_PTHREAD_ADAPTIVE_MUTEX
#endif // ROCKSDB_PTHREAD_ADAPTIVE_MUTEX
}
Mutex::~Mutex() { PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_)); }
@ -108,9 +106,8 @@ void Mutex::AssertHeld() {
#endif
}
CondVar::CondVar(Mutex* mu)
: mu_(mu) {
PthreadCall("init cv", pthread_cond_init(&cv_, nullptr));
CondVar::CondVar(Mutex* mu) : mu_(mu) {
PthreadCall("init cv", pthread_cond_init(&cv_, nullptr));
}
CondVar::~CondVar() { PthreadCall("destroy cv", pthread_cond_destroy(&cv_)); }
@ -146,9 +143,7 @@ bool CondVar::TimedWait(uint64_t abs_time_us) {
return false;
}
void CondVar::Signal() {
PthreadCall("signal", pthread_cond_signal(&cv_));
}
void CondVar::Signal() { PthreadCall("signal", pthread_cond_signal(&cv_)); }
void CondVar::SignalAll() {
PthreadCall("broadcast", pthread_cond_broadcast(&cv_));
@ -158,28 +153,40 @@ RWMutex::RWMutex() {
PthreadCall("init mutex", pthread_rwlock_init(&mu_, nullptr));
}
RWMutex::~RWMutex() { PthreadCall("destroy mutex", pthread_rwlock_destroy(&mu_)); }
RWMutex::~RWMutex() {
PthreadCall("destroy mutex", pthread_rwlock_destroy(&mu_));
}
void RWMutex::ReadLock() { PthreadCall("read lock", pthread_rwlock_rdlock(&mu_)); }
void RWMutex::ReadLock() {
PthreadCall("read lock", pthread_rwlock_rdlock(&mu_));
}
void RWMutex::WriteLock() { PthreadCall("write lock", pthread_rwlock_wrlock(&mu_)); }
void RWMutex::WriteLock() {
PthreadCall("write lock", pthread_rwlock_wrlock(&mu_));
}
void RWMutex::ReadUnlock() { PthreadCall("read unlock", pthread_rwlock_unlock(&mu_)); }
void RWMutex::ReadUnlock() {
PthreadCall("read unlock", pthread_rwlock_unlock(&mu_));
}
void RWMutex::WriteUnlock() { PthreadCall("write unlock", pthread_rwlock_unlock(&mu_)); }
void RWMutex::WriteUnlock() {
PthreadCall("write unlock", pthread_rwlock_unlock(&mu_));
}
int PhysicalCoreID() {
#if defined(ROCKSDB_SCHED_GETCPU_PRESENT) && defined(__x86_64__) && \
(__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 22))
// sched_getcpu uses VDSO getcpu() syscall since 2.22. I believe Linux offers VDSO
// support only on x86_64. This is the fastest/preferred method if available.
// sched_getcpu uses VDSO getcpu() syscall since 2.22. I believe Linux offers
// VDSO support only on x86_64. This is the fastest/preferred method if
// available.
int cpuno = sched_getcpu();
if (cpuno < 0) {
return -1;
}
return cpuno;
#elif defined(__x86_64__) || defined(__i386__)
// clang/gcc both provide cpuid.h, which defines __get_cpuid(), for x86_64 and i386.
// clang/gcc both provide cpuid.h, which defines __get_cpuid(), for x86_64 and
// i386.
unsigned eax, ebx = 0, ecx, edx;
if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
return -1;
@ -217,11 +224,11 @@ int GetMaxOpenFiles() {
return -1;
}
void *cacheline_aligned_alloc(size_t size) {
void* cacheline_aligned_alloc(size_t size) {
#if __GNUC__ < 5 && defined(__SANITIZE_ADDRESS__)
return malloc(size);
#elif ( _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || defined(__APPLE__))
void *m;
#elif (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || defined(__APPLE__))
void* m;
errno = posix_memalign(&m, CACHE_LINE_SIZE, size);
return errno ? nullptr : m;
#else
@ -229,9 +236,7 @@ void *cacheline_aligned_alloc(size_t size) {
#endif
}
void cacheline_aligned_free(void *memblock) {
free(memblock);
}
void cacheline_aligned_free(void* memblock) { free(memblock); }
static size_t GetPageSize() {
#if defined(OS_LINUX) || defined(_SC_PAGESIZE)

View File

@ -25,36 +25,36 @@
#undef PLATFORM_IS_LITTLE_ENDIAN
#if defined(OS_MACOSX)
#include <machine/endian.h>
#if defined(__DARWIN_LITTLE_ENDIAN) && defined(__DARWIN_BYTE_ORDER)
#define PLATFORM_IS_LITTLE_ENDIAN \
(__DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN)
#endif
#include <machine/endian.h>
#if defined(__DARWIN_LITTLE_ENDIAN) && defined(__DARWIN_BYTE_ORDER)
#define PLATFORM_IS_LITTLE_ENDIAN \
(__DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN)
#endif
#elif defined(OS_SOLARIS)
#include <sys/isa_defs.h>
#ifdef _LITTLE_ENDIAN
#define PLATFORM_IS_LITTLE_ENDIAN true
#else
#define PLATFORM_IS_LITTLE_ENDIAN false
#endif
#include <alloca.h>
#include <sys/isa_defs.h>
#ifdef _LITTLE_ENDIAN
#define PLATFORM_IS_LITTLE_ENDIAN true
#else
#define PLATFORM_IS_LITTLE_ENDIAN false
#endif
#include <alloca.h>
#elif defined(OS_AIX)
#include <sys/types.h>
#include <arpa/nameser_compat.h>
#define PLATFORM_IS_LITTLE_ENDIAN (BYTE_ORDER == LITTLE_ENDIAN)
#include <alloca.h>
#include <arpa/nameser_compat.h>
#include <sys/types.h>
#define PLATFORM_IS_LITTLE_ENDIAN (BYTE_ORDER == LITTLE_ENDIAN)
#include <alloca.h>
#elif defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || \
defined(OS_DRAGONFLYBSD) || defined(OS_ANDROID)
#include <sys/endian.h>
#include <sys/types.h>
#define PLATFORM_IS_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN)
#include <sys/endian.h>
#include <sys/types.h>
#define PLATFORM_IS_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN)
#else
#include <endian.h>
#include <endian.h>
#endif
#include <pthread.h>
#include <stdint.h>
#include <string.h>
#include <limits>
#include <string>
@ -62,8 +62,8 @@
#define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
#endif
#if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) ||\
defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD) ||\
#if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) || \
defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD) || \
defined(OS_ANDROID) || defined(CYGWIN) || defined(OS_AIX)
// Use fread/fwrite/fflush on platforms without _unlocked variants
#define fread_unlocked fread
@ -71,8 +71,8 @@
#define fflush_unlocked fflush
#endif
#if defined(OS_MACOSX) || defined(OS_FREEBSD) ||\
defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
#if defined(OS_MACOSX) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || \
defined(OS_DRAGONFLYBSD)
// Use fsync() on platforms without fdatasync()
#define fdatasync fsync
#endif
@ -139,10 +139,10 @@ class RWMutex {
void WriteLock();
void ReadUnlock();
void WriteUnlock();
void AssertHeld() { }
void AssertHeld() {}
private:
pthread_rwlock_t mu_; // the underlying platform mutex
pthread_rwlock_t mu_; // the underlying platform mutex
};
class CondVar {
@ -154,6 +154,7 @@ class CondVar {
bool TimedWait(uint64_t abs_time_us);
void Signal();
void SignalAll();
private:
pthread_cond_t cv_;
Mutex* mu_;
@ -205,9 +206,9 @@ extern void InitOnce(OnceType* once, void (*initializer)());
static_assert((CACHE_LINE_SIZE & (CACHE_LINE_SIZE - 1)) == 0,
"Cache line size must be a power of 2 number of bytes");
extern void *cacheline_aligned_alloc(size_t size);
extern void* cacheline_aligned_alloc(size_t size);
extern void cacheline_aligned_free(void *memblock);
extern void cacheline_aligned_free(void* memblock);
#if defined(__aarch64__)
// __builtin_prefetch(..., 1) turns into a prefetch into prfm pldl3keep. On
@ -236,5 +237,5 @@ int64_t GetProcessID();
// true on success or false on failure.
bool GenerateRfcUuid(std::string* output);
} // namespace port
} // namespace port
} // namespace ROCKSDB_NAMESPACE

View File

@ -24,13 +24,13 @@ void* SaveStack(int* /*num_frames*/, int /*first_frames_to_skip*/) {
#else
#include <cxxabi.h>
#include <execinfo.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <cxxabi.h>
#if defined(OS_FREEBSD)
#include <sys/sysctl.h>

View File

@ -39,8 +39,8 @@ inline struct tm* LocalTimeR(const time_t* timep, struct tm* result) {
} // namespace ROCKSDB_NAMESPACE
#else
#include <time.h>
#include <sys/time.h>
#include <time.h>
namespace ROCKSDB_NAMESPACE {

View File

@ -27,9 +27,9 @@ std::string GetWindowsErrSz(DWORD err);
inline IOStatus IOErrorFromWindowsError(const std::string& context, DWORD err) {
return ((err == ERROR_HANDLE_DISK_FULL) || (err == ERROR_DISK_FULL))
? IOStatus::NoSpace(context, GetWindowsErrSz(err))
: ((err == ERROR_FILE_NOT_FOUND) || (err == ERROR_PATH_NOT_FOUND))
? IOStatus::PathNotFound(context, GetWindowsErrSz(err))
: IOStatus::IOError(context, GetWindowsErrSz(err));
: ((err == ERROR_FILE_NOT_FOUND) || (err == ERROR_PATH_NOT_FOUND))
? IOStatus::PathNotFound(context, GetWindowsErrSz(err))
: IOStatus::IOError(context, GetWindowsErrSz(err));
}
inline IOStatus IOErrorFromLastWindowsError(const std::string& context) {
@ -39,10 +39,9 @@ inline IOStatus IOErrorFromLastWindowsError(const std::string& context) {
inline IOStatus IOError(const std::string& context, int err_number) {
return (err_number == ENOSPC)
? IOStatus::NoSpace(context, errnoStr(err_number).c_str())
: (err_number == ENOENT)
? IOStatus::PathNotFound(context,
errnoStr(err_number).c_str())
: IOStatus::IOError(context, errnoStr(err_number).c_str());
: (err_number == ENOENT)
? IOStatus::PathNotFound(context, errnoStr(err_number).c_str())
: IOStatus::IOError(context, errnoStr(err_number).c_str());
}
class WinFileData;

View File

@ -38,7 +38,6 @@
#undef DeleteFile
#undef GetCurrentTime
#ifndef strcasecmp
#define strcasecmp _stricmp
#endif
@ -133,12 +132,9 @@ class Mutex {
void operator=(const Mutex&) = delete;
private:
friend class CondVar;
std::mutex& getLock() {
return mutex_;
}
std::mutex& getLock() { return mutex_; }
std::mutex mutex_;
#ifndef NDEBUG
@ -170,8 +166,7 @@ class RWMutex {
class CondVar {
public:
explicit CondVar(Mutex* mu) : mu_(mu) {
}
explicit CondVar(Mutex* mu) : mu_(mu) {}
~CondVar();
void Wait();
@ -191,7 +186,6 @@ class CondVar {
Mutex* mu_;
};
#ifdef _POSIX_THREADS
using Thread = std::thread;
#else
@ -204,15 +198,14 @@ using Thread = WindowsThread;
// Posix semantics with initialization
// adopted in the project
struct OnceType {
struct Init {};
struct Init {};
OnceType() {}
OnceType(const Init&) {}
OnceType(const OnceType&) = delete;
OnceType& operator=(const OnceType&) = delete;
OnceType() {}
OnceType(const Init&) {}
OnceType(const OnceType&) = delete;
OnceType& operator=(const OnceType&) = delete;
std::once_flag flag_;
std::once_flag flag_;
};
#define LEVELDB_ONCE_INIT port::OnceType::Init()
@ -228,7 +221,7 @@ void* jemalloc_aligned_alloc(size_t size, size_t alignment) noexcept;
void jemalloc_aligned_free(void* p) noexcept;
#endif
inline void *cacheline_aligned_alloc(size_t size) {
inline void* cacheline_aligned_alloc(size_t size) {
#ifdef ROCKSDB_JEMALLOC
return jemalloc_aligned_alloc(size, CACHE_LINE_SIZE);
#else
@ -236,7 +229,7 @@ inline void *cacheline_aligned_alloc(size_t size) {
#endif
}
inline void cacheline_aligned_free(void *memblock) {
inline void cacheline_aligned_free(void* memblock) {
#ifdef ROCKSDB_JEMALLOC
jemalloc_aligned_free(memblock);
#else
@ -322,7 +315,6 @@ bool GenerateRfcUuid(std::string* output);
} // namespace port
#ifdef ROCKSDB_WINDOWS_UTF8_FILENAMES
#define RX_FILESTRING std::wstring
@ -376,11 +368,11 @@ bool GenerateRfcUuid(std::string* output);
#endif
using port::pthread_key_t;
using port::pthread_getspecific;
using port::pthread_key_create;
using port::pthread_key_delete;
using port::pthread_key_t;
using port::pthread_setspecific;
using port::pthread_getspecific;
using port::truncate;
} // namespace ROCKSDB_NAMESPACE

View File

@ -10,10 +10,11 @@
#if defined(OS_WIN)
#ifndef ROCKSDB_JEMALLOC
# error This file can only be part of jemalloc aware build
#error This file can only be part of jemalloc aware build
#endif
#include <stdexcept>
#include "jemalloc/jemalloc.h"
#include "port/win/port_win.h"
@ -31,10 +32,10 @@ void JemallocDeallocateForZSTD(void* /* opaque */, void* address) {
ZSTD_customMem GetJeZstdAllocationOverrides() {
return {JemallocAllocateForZSTD, JemallocDeallocateForZSTD, nullptr};
}
} // namespace port
} // namespace port
} // namespace ROCKSDB_NAMESPACE
#endif // (ZSTD_VERSION_NUMBER >= 500)
#endif // defined(ZSTD) defined(ZSTD_STATIC_LINKING_ONLY)
#endif // (ZSTD_VERSION_NUMBER >= 500)
#endif // defined(ZSTD) defined(ZSTD_STATIC_LINKING_ONLY)
// Global operators to be replaced by a linker when this file is
// a part of the build

View File

@ -57,9 +57,7 @@ void WinLogger::DebugWriter(const char* str, int len) {
WinLogger::~WinLogger() { CloseInternal().PermitUncheckedError(); }
Status WinLogger::CloseImpl() {
return CloseInternal();
}
Status WinLogger::CloseImpl() { return CloseInternal(); }
Status WinLogger::CloseInternal() {
Status s;
@ -160,7 +158,7 @@ void WinLogger::Logv(const char* format, va_list ap) {
DWORD bytesWritten = 0;
BOOL ret = WriteFile(file_, base, static_cast<DWORD>(write_size),
&bytesWritten, NULL);
&bytesWritten, NULL);
if (ret == FALSE) {
std::string errSz = GetWindowsErrSz(GetLastError());
fprintf(stderr, "%s", errSz.c_str());
@ -187,7 +185,7 @@ void WinLogger::Logv(const char* format, va_list ap) {
size_t WinLogger::GetLogFileSize() const { return log_size_; }
}
} // namespace port
} // namespace ROCKSDB_NAMESPACE

View File

@ -44,9 +44,8 @@ class WinLogger : public ROCKSDB_NAMESPACE::Logger {
void DebugWriter(const char* str, int len);
protected:
Status CloseImpl() override;
protected:
Status CloseImpl() override;
private:
HANDLE file_;
@ -60,6 +59,6 @@ protected:
const static uint64_t flush_every_seconds_ = 5;
};
}
} // namespace port
} // namespace ROCKSDB_NAMESPACE

View File

@ -17,7 +17,7 @@
#include "port/win/win_thread.h"
#include <assert.h>
#include <process.h> // __beginthreadex
#include <process.h> // __beginthreadex
#include <windows.h>
#include <stdexcept>
@ -28,14 +28,10 @@ namespace ROCKSDB_NAMESPACE {
namespace port {
struct WindowsThread::Data {
std::function<void()> func_;
uintptr_t handle_;
uintptr_t handle_;
Data(std::function<void()>&& func) :
func_(std::move(func)),
handle_(0) {
}
Data(std::function<void()>&& func) : func_(std::move(func)), handle_(0) {}
Data(const Data&) = delete;
Data& operator=(const Data&) = delete;
@ -43,36 +39,30 @@ struct WindowsThread::Data {
static unsigned int __stdcall ThreadProc(void* arg);
};
void WindowsThread::Init(std::function<void()>&& func) {
data_ = std::make_shared<Data>(std::move(func));
// We create another instance of std::shared_ptr to get an additional ref
// since we may detach and destroy this instance before the threadproc
// may start to run. We choose to allocate this additional ref on the heap
// so we do not need to synchronize and allow this thread to proceed
std::unique_ptr<std::shared_ptr<Data>> th_data(new std::shared_ptr<Data>(data_));
std::unique_ptr<std::shared_ptr<Data>> th_data(
new std::shared_ptr<Data>(data_));
data_->handle_ = _beginthreadex(NULL,
0, // stack size
&Data::ThreadProc,
th_data.get(),
0, // init flag
&th_id_);
0, // stack size
&Data::ThreadProc, th_data.get(),
0, // init flag
&th_id_);
if (data_->handle_ == 0) {
throw std::system_error(std::make_error_code(
std::errc::resource_unavailable_try_again),
"Unable to create a thread");
throw std::system_error(
std::make_error_code(std::errc::resource_unavailable_try_again),
"Unable to create a thread");
}
th_data.release();
}
WindowsThread::WindowsThread() :
data_(nullptr),
th_id_(0)
{}
WindowsThread::WindowsThread() : data_(nullptr), th_id_(0) {}
WindowsThread::~WindowsThread() {
// Must be joined or detached
@ -87,13 +77,11 @@ WindowsThread::~WindowsThread() {
}
}
WindowsThread::WindowsThread(WindowsThread&& o) noexcept :
WindowsThread() {
WindowsThread::WindowsThread(WindowsThread&& o) noexcept : WindowsThread() {
*this = std::move(o);
}
WindowsThread& WindowsThread::operator=(WindowsThread&& o) noexcept {
if (joinable()) {
assert(false);
std::terminate();
@ -107,9 +95,7 @@ WindowsThread& WindowsThread::operator=(WindowsThread&& o) noexcept {
return *this;
}
bool WindowsThread::joinable() const {
return (data_ && data_->handle_ != 0);
}
bool WindowsThread::joinable() const { return (data_ && data_->handle_ != 0); }
WindowsThread::native_handle_type WindowsThread::native_handle() const {
return reinterpret_cast<native_handle_type>(data_->handle_);
@ -120,36 +106,33 @@ unsigned WindowsThread::hardware_concurrency() {
}
void WindowsThread::join() {
if (!joinable()) {
assert(false);
throw std::system_error(
std::make_error_code(std::errc::invalid_argument),
"Thread is no longer joinable");
throw std::system_error(std::make_error_code(std::errc::invalid_argument),
"Thread is no longer joinable");
}
if (GetThreadId(GetCurrentThread()) == th_id_) {
assert(false);
throw std::system_error(
std::make_error_code(std::errc::resource_deadlock_would_occur),
"Can not join itself");
std::make_error_code(std::errc::resource_deadlock_would_occur),
"Can not join itself");
}
auto ret = WaitForSingleObject(reinterpret_cast<HANDLE>(data_->handle_),
INFINITE);
auto ret =
WaitForSingleObject(reinterpret_cast<HANDLE>(data_->handle_), INFINITE);
if (ret != WAIT_OBJECT_0) {
auto lastError = GetLastError();
assert(false);
throw std::system_error(static_cast<int>(lastError),
std::system_category(),
"WaitForSingleObjectFailed: thread join");
throw std::system_error(static_cast<int>(lastError), std::system_category(),
"WaitForSingleObjectFailed: thread join");
}
BOOL rc
#if defined(_MSC_VER)
= FALSE;
= FALSE;
#else
__attribute__((__unused__));
__attribute__((__unused__));
#endif
rc = CloseHandle(reinterpret_cast<HANDLE>(data_->handle_));
assert(rc != 0);
@ -157,12 +140,10 @@ void WindowsThread::join() {
}
bool WindowsThread::detach() {
if (!joinable()) {
assert(false);
throw std::system_error(
std::make_error_code(std::errc::invalid_argument),
"Thread is no longer available");
throw std::system_error(std::make_error_code(std::errc::invalid_argument),
"Thread is no longer available");
}
BOOL ret = CloseHandle(reinterpret_cast<HANDLE>(data_->handle_));
@ -171,18 +152,18 @@ bool WindowsThread::detach() {
return (ret != 0);
}
void WindowsThread::swap(WindowsThread& o) {
void WindowsThread::swap(WindowsThread& o) {
data_.swap(o.data_);
std::swap(th_id_, o.th_id_);
}
unsigned int __stdcall WindowsThread::Data::ThreadProc(void* arg) {
unsigned int __stdcall WindowsThread::Data::ThreadProc(void* arg) {
auto ptr = reinterpret_cast<std::shared_ptr<Data>*>(arg);
std::unique_ptr<std::shared_ptr<Data>> data(ptr);
(*data)->func_();
return 0;
}
} // namespace port
} // namespace port
} // namespace ROCKSDB_NAMESPACE
#endif // !_POSIX_THREADS

View File

@ -11,8 +11,8 @@
#ifndef _POSIX_THREADS
#include <memory>
#include <functional>
#include <memory>
#include <type_traits>
#include "rocksdb/rocksdb_namespace.h"
@ -31,8 +31,8 @@ namespace port {
class WindowsThread {
struct Data;
std::shared_ptr<Data> data_;
unsigned int th_id_;
std::shared_ptr<Data> data_;
unsigned int th_id_;
void Init(std::function<void()>&&);
@ -104,7 +104,7 @@ class WindowsThread {
void swap(WindowsThread&);
};
} // namespace port
} // namespace port
} // namespace ROCKSDB_NAMESPACE
namespace std {
@ -112,6 +112,6 @@ inline void swap(ROCKSDB_NAMESPACE::port::WindowsThread& th1,
ROCKSDB_NAMESPACE::port::WindowsThread& th2) {
th1.swap(th2);
}
} // namespace std
} // namespace std
#endif // !_POSIX_THREADS

View File

@ -10,12 +10,13 @@
#if defined(OS_WIN)
#include "port/win/xpress_win.h"
#include <windows.h>
#include <cassert>
#include <memory>
#include <limits>
#include <iostream>
#include <limits>
#include <memory>
#ifdef XPRESS
@ -41,10 +42,9 @@ auto CloseDecompressorFun = [](void* h) {
::CloseDecompressor(reinterpret_cast<DECOMPRESSOR_HANDLE>(h));
}
};
}
} // namespace
bool Compress(const char* input, size_t length, std::string* output) {
assert(input != nullptr);
assert(output != nullptr);
@ -57,42 +57,40 @@ bool Compress(const char* input, size_t length, std::string* output) {
COMPRESSOR_HANDLE compressor = NULL;
BOOL success = CreateCompressor(
COMPRESS_ALGORITHM_XPRESS, // Compression Algorithm
allocRoutinesPtr, // Optional allocation routine
&compressor); // Handle
BOOL success =
CreateCompressor(COMPRESS_ALGORITHM_XPRESS, // Compression Algorithm
allocRoutinesPtr, // Optional allocation routine
&compressor); // Handle
if (!success) {
#ifdef _DEBUG
std::cerr << "XPRESS: Failed to create Compressor LastError: " <<
GetLastError() << std::endl;
std::cerr << "XPRESS: Failed to create Compressor LastError: "
<< GetLastError() << std::endl;
#endif
return false;
}
std::unique_ptr<void, decltype(CloseCompressorFun)>
compressorGuard(compressor, CloseCompressorFun);
std::unique_ptr<void, decltype(CloseCompressorFun)> compressorGuard(
compressor, CloseCompressorFun);
SIZE_T compressedBufferSize = 0;
// Query compressed buffer size.
success = ::Compress(
compressor, // Compressor Handle
const_cast<char*>(input), // Input buffer
length, // Uncompressed data size
NULL, // Compressed Buffer
0, // Compressed Buffer size
&compressedBufferSize); // Compressed Data size
success = ::Compress(compressor, // Compressor Handle
const_cast<char*>(input), // Input buffer
length, // Uncompressed data size
NULL, // Compressed Buffer
0, // Compressed Buffer size
&compressedBufferSize); // Compressed Data size
if (!success) {
auto lastError = GetLastError();
if (lastError != ERROR_INSUFFICIENT_BUFFER) {
#ifdef _DEBUG
std::cerr <<
"XPRESS: Failed to estimate compressed buffer size LastError " <<
lastError << std::endl;
std::cerr
<< "XPRESS: Failed to estimate compressed buffer size LastError "
<< lastError << std::endl;
#endif
return false;
}
@ -106,18 +104,17 @@ bool Compress(const char* input, size_t length, std::string* output) {
SIZE_T compressedDataSize = 0;
// Compress
success = ::Compress(
compressor, // Compressor Handle
const_cast<char*>(input), // Input buffer
length, // Uncompressed data size
&result[0], // Compressed Buffer
compressedBufferSize, // Compressed Buffer size
&compressedDataSize); // Compressed Data size
success = ::Compress(compressor, // Compressor Handle
const_cast<char*>(input), // Input buffer
length, // Uncompressed data size
&result[0], // Compressed Buffer
compressedBufferSize, // Compressed Buffer size
&compressedDataSize); // Compressed Data size
if (!success) {
#ifdef _DEBUG
std::cerr << "XPRESS: Failed to compress LastError " <<
GetLastError() << std::endl;
std::cerr << "XPRESS: Failed to compress LastError " << GetLastError()
<< std::endl;
#endif
return false;
}
@ -141,42 +138,39 @@ char* Decompress(const char* input_data, size_t input_length,
DECOMPRESSOR_HANDLE decompressor = NULL;
BOOL success = CreateDecompressor(
COMPRESS_ALGORITHM_XPRESS, // Compression Algorithm
allocRoutinesPtr, // Optional allocation routine
&decompressor); // Handle
BOOL success =
CreateDecompressor(COMPRESS_ALGORITHM_XPRESS, // Compression Algorithm
allocRoutinesPtr, // Optional allocation routine
&decompressor); // Handle
if (!success) {
#ifdef _DEBUG
std::cerr << "XPRESS: Failed to create Decompressor LastError "
<< GetLastError() << std::endl;
<< GetLastError() << std::endl;
#endif
return nullptr;
}
std::unique_ptr<void, decltype(CloseDecompressorFun)>
compressorGuard(decompressor, CloseDecompressorFun);
std::unique_ptr<void, decltype(CloseDecompressorFun)> compressorGuard(
decompressor, CloseDecompressorFun);
SIZE_T decompressedBufferSize = 0;
success = ::Decompress(
decompressor, // Compressor Handle
const_cast<char*>(input_data), // Compressed data
input_length, // Compressed data size
NULL, // Buffer set to NULL
0, // Buffer size set to 0
&decompressedBufferSize); // Decompressed Data size
success = ::Decompress(decompressor, // Compressor Handle
const_cast<char*>(input_data), // Compressed data
input_length, // Compressed data size
NULL, // Buffer set to NULL
0, // Buffer size set to 0
&decompressedBufferSize); // Decompressed Data size
if (!success) {
auto lastError = GetLastError();
if (lastError != ERROR_INSUFFICIENT_BUFFER) {
#ifdef _DEBUG
std::cerr
<< "XPRESS: Failed to estimate decompressed buffer size LastError "
<< lastError << std::endl;
<< "XPRESS: Failed to estimate decompressed buffer size LastError "
<< lastError << std::endl;
#endif
return nullptr;
}
@ -190,19 +184,14 @@ char* Decompress(const char* input_data, size_t input_length,
SIZE_T decompressedDataSize = 0;
success = ::Decompress(
decompressor,
const_cast<char*>(input_data),
input_length,
outputBuffer.get(),
decompressedBufferSize,
&decompressedDataSize);
success = ::Decompress(decompressor, const_cast<char*>(input_data),
input_length, outputBuffer.get(),
decompressedBufferSize, &decompressedDataSize);
if (!success) {
#ifdef _DEBUG
std::cerr <<
"XPRESS: Failed to decompress LastError " <<
GetLastError() << std::endl;
std::cerr << "XPRESS: Failed to decompress LastError " << GetLastError()
<< std::endl;
#endif
return nullptr;
}
@ -212,8 +201,8 @@ char* Decompress(const char* input_data, size_t input_length,
// Return the raw buffer to the caller supporting the tradition
return outputBuffer.release();
}
}
}
} // namespace xpress
} // namespace port
} // namespace ROCKSDB_NAMESPACE
#endif

View File

@ -21,6 +21,6 @@ bool Compress(const char* input, size_t length, std::string* output);
char* Decompress(const char* input_data, size_t input_length,
size_t* uncompressed_size);
}
}
} // namespace xpress
} // namespace port
} // namespace ROCKSDB_NAMESPACE