Initial import with skill sheet working

This commit is contained in:
2024-12-04 00:11:23 +01:00
commit 9050c80ab4
4488 changed files with 671048 additions and 0 deletions

View File

@ -0,0 +1,48 @@
#include <stdint.h>
#include <windows.h>
// credit: https://groups.google.com/forum/#!msg/leveldb/VuECZMnsob4/F6pGPGaK-XwJ
namespace leveldb {
namespace port {
class AtomicPointer {
private:
void* rep_;
public:
AtomicPointer () {}
explicit AtomicPointer(void* v) {
InterlockedExchangePointer(&rep_, v);
}
// Read and return the stored pointer with the guarantee that no
// later memory access (read or write) by this thread can be
// reordered ahead of this read.
inline void* Acquire_Load() const {
void* r;
InterlockedExchangePointer(&r, rep_ );
return r;
}
// Set v as the stored pointer with the guarantee that no earlier
// memory access (read or write) by this thread can be reordered
// after this store.
inline void Release_Store(void* v) {
InterlockedExchangePointer(&rep_, v);
}
// Read the stored pointer with no ordering guarantees.
inline void* NoBarrier_Load() const {
void* r = reinterpret_cast<void*>(rep_);
return r;
}
// Set va as the stored pointer with no ordering guarantees.
inline void NoBarrier_Store(void* v) {
rep_ = reinterpret_cast<void*>(v);
}
};
} // namespace port
} // namespace leveldb

View File

@ -0,0 +1,692 @@
/*
* originally from http://code.google.com/r/kkowalczyk-leveldb/
* code by Krzysztof Kowalczyk kkowalczyk kowalczyk@gmail.com
* See also http://blog.kowalczyk.info/software/leveldb-for-windows/index.html
*/
#include <stdio.h>
#include <string.h>
#include <deque>
#include <process.h>
#include "leveldb/env.h"
#include "leveldb/slice.h"
#include "port/port.h"
#include "util/logging.h"
#include "win_logger.h"
#if defined(_MSC_VER)
# ifdef DeleteFile
# undef DeleteFile
# endif
#endif
// To properly support file names on Windows we should be using Unicode
// (WCHAR) strings. To accomodate existing interface which uses std::string,
// we use the following convention:
// * all filenames that we return (e.g. from GetTestDirectory()) are
// utf8-encoded
// * we'll try to interpret all input file names as if they're
// utf8-encoded. If they're not valid utf8 strings, we'll try
// to interpret them according to a current code page
// This just works for names that don't use characters outside ascii
// and for those that do, the caller needs to be aware of this convention
// whenever it bubbles up to the user-level API.
namespace leveldb {
static Status IOError(const std::string& context, DWORD err = (DWORD)-1) {
char *err_msg = NULL;
Status s;
if ((DWORD)-1 == err)
err = GetLastError();
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&err_msg, 0, NULL);
if (!err_msg)
return Status::IOError(context);
s = Status::IOError(context, err_msg);
LocalFree(err_msg);
return s;
}
class WinSequentialFile: public SequentialFile {
private:
std::string fname_;
HANDLE file_;
public:
WinSequentialFile(const std::string& fname, HANDLE f)
: fname_(fname), file_(f) { }
virtual ~WinSequentialFile() { CloseHandle(file_); }
virtual Status Read(size_t n, Slice* result, char* scratch) {
DWORD n2 = n;
DWORD r = 0;
BOOL ok = ReadFile(file_, (void*)scratch, n2, &r, NULL);
*result = Slice(scratch, r);
if (!ok) {
// We leave status as ok if we hit the end of the file
if (GetLastError() != ERROR_HANDLE_EOF) {
return IOError(fname_);
}
}
return Status::OK();
}
virtual Status Skip(uint64_t n) {
LARGE_INTEGER pos;
pos.QuadPart = n;
DWORD res = SetFilePointerEx(file_, pos, NULL, FILE_CURRENT);
if (res == 0)
return IOError(fname_);
return Status::OK();
}
};
class WinRandomAccessFile: public RandomAccessFile {
private:
std::string fname_;
HANDLE file_;
public:
WinRandomAccessFile(const std::string& fname, HANDLE file)
: fname_(fname), file_(file) { }
virtual ~WinRandomAccessFile() { CloseHandle(file_); }
virtual Status Read(uint64_t offset, size_t n, Slice* result,
char* scratch) const {
OVERLAPPED overlapped = { 0 };
overlapped.Offset = static_cast<DWORD>(offset);
overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32);
DWORD bytes_read = 0;
BOOL success = ReadFile(file_, scratch, n, &bytes_read, &overlapped);
*result = Slice(scratch, bytes_read);
return success != FALSE ? Status::OK() : Status::IOError(fname_);
}
};
class WinWritableFile : public WritableFile {
private:
std::string name_;
HANDLE file_;
public:
WinWritableFile(std::string name, HANDLE h) : name_(name), file_(h) {
}
virtual ~WinWritableFile() {
Close();
}
virtual Status Append(const Slice& data) {
DWORD n = data.size();
DWORD pos = 0;
while (pos < n) {
DWORD written = 0;
BOOL ok = WriteFile(file_, data.data() + pos, n - pos, &written, NULL);
if (!ok)
return IOError(name_+ "Append: cannot write");
pos += written;
}
return Status::OK();
}
virtual Status Close() {
if (INVALID_HANDLE_VALUE == file_)
return Status::OK();
Status s = Sync();
CloseHandle(file_);
file_ = INVALID_HANDLE_VALUE;
return s;
}
virtual Status Flush() {
return Status::OK();
}
virtual Status Sync() {
BOOL ok = FlushFileBuffers(file_);
if (!ok)
return IOError(name_);
return Status::OK();
}
};
namespace {
#define DIR_SEP_CHAR L'\\'
#define DIR_SEP_STR L"\\"
WCHAR *ToWcharFromCodePage(const char *src, UINT cp) {
int required_buf_size = MultiByteToWideChar(cp, 0, src, -1, NULL, 0);
if (0 == required_buf_size) // indicates an error
return NULL;
WCHAR *res = reinterpret_cast<WCHAR*>(malloc(sizeof(WCHAR) * required_buf_size));
if (!res)
return NULL;
MultiByteToWideChar(cp, 0, src, -1, res, required_buf_size);
return res;
}
// try to convert to WCHAR string trying most common code pages
// to be as permissive as we can be
WCHAR *ToWcharPermissive(const char *s) {
WCHAR *ws = ToWcharFromCodePage(s, CP_UTF8);
if (ws != NULL)
return ws;
ws = ToWcharFromCodePage(s, CP_ACP);
if (ws != NULL)
return ws;
ws = ToWcharFromCodePage(s, CP_OEMCP);
return ws;
}
char *ToUtf8(const WCHAR *s) {
int required_buf_size = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, NULL, NULL);
char *res = (char*)malloc(sizeof(char) * required_buf_size);
if (!res)
return NULL;
WideCharToMultiByte(CP_UTF8, 0, s, -1, res, required_buf_size, NULL, NULL);
return res;
}
static size_t WstrLen(const WCHAR *s) {
if (NULL == s)
return 0;
return wcslen(s);
}
static WCHAR *WstrJoin(const WCHAR *s1, const WCHAR *s2, const WCHAR *s3=NULL) {
size_t s1_len = WstrLen(s1);
size_t s2_len = WstrLen(s2);
size_t s3_len = WstrLen(s3);
size_t len =s1_len + s2_len + s3_len + 1;
WCHAR *res = (WCHAR*)malloc(sizeof(WCHAR) * len);
if (!res)
return NULL;
WCHAR *tmp = res;
if (s1 != NULL) {
memcpy(tmp, s1, s1_len * sizeof(WCHAR));
tmp += s1_len;
}
if (s2 != NULL) {
memcpy(tmp, s2, s2_len * sizeof(WCHAR));
tmp += s2_len;
}
if (s3 != NULL) {
memcpy(tmp, s3, s3_len * sizeof(WCHAR));
tmp += s3_len;
}
*tmp = 0;
return res;
}
static bool WstrEndsWith(const WCHAR *s1, WCHAR c) {
size_t len = WstrLen(s1);
return ((len > 0) && (s1[len-1] == c));
}
static WCHAR *WstrPathJoin(const WCHAR *s1, const WCHAR *s2) {
if (WstrEndsWith(s1, DIR_SEP_CHAR))
return WstrJoin(s1, s2);
return WstrJoin(s1, DIR_SEP_STR, s2);
}
// Return true if s is "." or "..", which are 2 directories
// we should skip when enumerating a directory
static bool SkipDir(const WCHAR *s) {
if (*s == L'.') {
if (s[1] == 0)
return true;
return ((s[1] == '.') && (s[2] == 0));
}
return false;
}
class WinFileLock : public FileLock {
public:
WinFileLock(const std::string &fname, HANDLE file)
: fname_(fname), file_(file) {
}
virtual ~WinFileLock() {
Close();
}
bool Close() {
bool ok = true;
if (file_ != INVALID_HANDLE_VALUE)
ok = (CloseHandle(file_) != FALSE);
file_ = INVALID_HANDLE_VALUE;
return ok;
}
std::string fname_;
HANDLE file_;
};
class WinEnv : public Env {
public:
WinEnv();
virtual ~WinEnv() {
fprintf(stderr, "Destroying Env::Default()\n");
//exit(1);
}
virtual Status NewSequentialFile(const std::string& fname,
SequentialFile** result) {
*result = NULL;
WCHAR *file_name = ToWcharPermissive(fname.c_str());
if (file_name == NULL) {
return Status::InvalidArgument("Invalid file name");
}
HANDLE h = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
free((void*)file_name);
if (h == INVALID_HANDLE_VALUE) {
return IOError(fname);
}
*result = new WinSequentialFile(fname, h);
return Status::OK();
}
virtual Status NewRandomAccessFile(const std::string& fname,
RandomAccessFile** result) {
*result = NULL;
WCHAR *file_name = ToWcharPermissive(fname.c_str());
if (file_name == NULL) {
return Status::InvalidArgument("Invalid file name");
}
HANDLE h = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
free((void*)file_name);
if (h == INVALID_HANDLE_VALUE) {
return IOError(fname);
}
*result = new WinRandomAccessFile(fname, h);
return Status::OK();
}
virtual Status NewWritableFile(const std::string& fname,
WritableFile** result) {
*result = NULL;
WCHAR *file_name = ToWcharPermissive(fname.c_str());
if (file_name == NULL)
return Status::InvalidArgument("Invalid file name");
HANDLE h = CreateFileW(file_name, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
free((void*)file_name);
if (h == INVALID_HANDLE_VALUE) {
return IOError(fname);
}
*result = new WinWritableFile(fname, h);
return Status::OK();
}
virtual bool FileExists(const std::string& fname) {
WCHAR *file_name = ToWcharPermissive(fname.c_str());
if (file_name == NULL)
return false;
WIN32_FILE_ATTRIBUTE_DATA file_info;
BOOL res = GetFileAttributesExW(file_name, GetFileExInfoStandard, &file_info);
free((void*)file_name);
if (0 == res)
return false;
if ((file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
return false;
return true;
}
virtual Status GetChildren(const std::string& dir,
std::vector<std::string>* result) {
result->clear();
WCHAR *dir_name = ToWcharPermissive(dir.c_str());
if (dir_name == NULL)
return Status::InvalidArgument("Invalid file name");
WCHAR *pattern = WstrPathJoin(dir_name, L"*");
free(dir_name);
if (NULL == pattern)
return Status::InvalidArgument("Invalid file name");
WIN32_FIND_DATAW file_data;
HANDLE h = FindFirstFileW(pattern, &file_data);
free(pattern);
if (INVALID_HANDLE_VALUE == h) {
if (ERROR_FILE_NOT_FOUND == GetLastError())
return Status::OK();
return IOError(dir);
}
for (;;) {
WCHAR *s = file_data.cFileName;
if (!SkipDir(s)) {
char *s2 = ToUtf8(s);
result->push_back(s2);
free(s2);
}
if (FALSE == FindNextFileW(h, &file_data))
break;
}
FindClose(h);
return Status::OK();
}
virtual Status DeleteFile(const std::string& fname) {
WCHAR *file_path = ToWcharPermissive(fname.c_str());
if (file_path == NULL)
return Status::InvalidArgument("Invalid file name");
BOOL ok = DeleteFileW(file_path);
free(file_path);
if (!ok) {
DWORD err = GetLastError();
if ((ERROR_PATH_NOT_FOUND == err) || (ERROR_FILE_NOT_FOUND == err))
return Status::OK();
return IOError("DeleteFile " + fname);
}
return Status::OK();
}
bool CreateDirIfNotExists(const WCHAR *dir) {
BOOL ok = CreateDirectoryW(dir, NULL);
if (ok)
return true;
return (ERROR_ALREADY_EXISTS == GetLastError());
}
bool DirExists(const WCHAR *dir) {
WIN32_FILE_ATTRIBUTE_DATA file_info;
BOOL res = GetFileAttributesExW(dir, GetFileExInfoStandard, &file_info);
if (0 == res)
return false;
return (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
WCHAR *WstrDupN(const WCHAR *s, size_t len) {
void *res = malloc((len + 1) * sizeof(WCHAR));
if (!res)
return NULL;
memcpy(res, s, len * sizeof(WCHAR));
WCHAR *res2 = reinterpret_cast<WCHAR*>(res);
res2[len] = 0;
return res2;
}
bool IsPathSep(WCHAR c) {
return (c == '\\') || (c == '/');
}
WCHAR *GetPathParent(const WCHAR *path) {
const WCHAR *last_sep = NULL;
const WCHAR *tmp = path;
// find the last path separator
// (ignoring one at the end of the string)
while (*tmp) {
if (IsPathSep(*tmp)) {
if (0 != tmp[1])
last_sep = tmp;
}
++tmp;
}
if (NULL == last_sep)
return NULL;
size_t len = last_sep - path;
return WstrDupN(path, len);
}
bool CreateDirRecursive(WCHAR *dir) {
WCHAR *parent = GetPathParent(dir);
bool ok = true;
if (parent && !DirExists(parent)) {
ok = CreateDirRecursive(parent);
}
free(parent);
if (!ok)
return false;
return CreateDirIfNotExists(dir);
}
virtual Status CreateDir(const std::string& name) {
WCHAR *dir = ToWcharPermissive(name.c_str());
if (dir == NULL)
return Status::InvalidArgument("Invalid file name");
bool ok = CreateDirRecursive(dir);
free(dir);
if (!ok)
return IOError(name);
return Status::OK();
}
#if 1
virtual Status DeleteDir(const std::string& name) {
WCHAR *dir = ToWcharPermissive(name.c_str());
if (dir == NULL)
return Status::InvalidArgument("Invalid file name");
BOOL ok = RemoveDirectoryW(dir);
free(dir);
if (!ok)
return IOError(name);
return Status::OK();
}
#else
virtual Status DeleteDir(const std::string& dirname) {
WCHAR *dir = ToWcharPermissive(dirname.c_str());
if (dir == NULL)
return Status::InvalidArgument("Invalid file name");
SHFILEOPSTRUCTW fileop = { 0 };
fileop.wFunc = FO_DELETE;
fileop.pFrom = (const WCHAR*)dir;
fileop.fFlags = FOF_NO_UI;
int res = SHFileOperationW(&fileop);
free((void*)dir);
if (res == 0 && fileop.fAnyOperationsAborted == FALSE)
return Status::OK();
return IOError("DeleteDir " + dirname);
}
#endif
virtual Status GetFileSize(const std::string& fname, uint64_t* size) {
WCHAR *file_name = ToWcharPermissive(fname.c_str());
if (file_name == NULL)
return Status::InvalidArgument("Invalid file name");
HANDLE h = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
free(file_name);
if (h == INVALID_HANDLE_VALUE)
return IOError("GetFileSize " + fname);
// Not using GetFileAttributesEx() as it doesn't interact well with symlinks, etc.
LARGE_INTEGER lsize;
BOOL ok = GetFileSizeEx(h, &lsize);
CloseHandle(h);
if (!ok)
return IOError("GetFileSize " + fname);
*size = static_cast<uint64_t>(lsize.QuadPart);
return Status::OK();
}
virtual Status RenameFile(const std::string& src, const std::string& target) {
WCHAR *src2 = ToWcharPermissive(src.c_str());
WCHAR *target2 = ToWcharPermissive(target.c_str());
if ((src2 == NULL) || (target2 == NULL)) {
free(src2);
free(target2);
return Status::InvalidArgument("Invalid file name");
}
BOOL ok = MoveFileExW(src2, target2, MOVEFILE_REPLACE_EXISTING);
free(src2);
free(target2);
if (!ok)
return IOError("RenameFile " + src + " " + target);
return Status::OK();
}
virtual Status LockFile(const std::string& fname, FileLock** lock) {
*lock = NULL;
WCHAR *file_name = ToWcharPermissive(fname.c_str());
if (file_name == NULL) {
return Status::InvalidArgument("Invalid file name");
}
HANDLE h = CreateFileW(file_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
free((void*)file_name);
if (h == INVALID_HANDLE_VALUE) {
return IOError("LockFile " + fname);
}
*lock = new WinFileLock(fname, h);
return Status::OK();
}
virtual Status UnlockFile(FileLock* lock) {
Status s;
WinFileLock* my_lock = reinterpret_cast<WinFileLock*>(lock);
if (!my_lock->Close()) {
s = Status::IOError(my_lock->fname_, "Could not close lock file.");
}
delete my_lock;
return Status::OK();
}
virtual void Schedule(void (*function)(void*), void* arg);
virtual void StartThread(void (*function)(void* arg), void* arg);
virtual Status GetTestDirectory(std::string* result) {
WCHAR buf[MAX_PATH];
DWORD res = GetTempPathW(MAX_PATH, buf);
if (0 == res) {
return IOError("Can't get test directory");
}
char *s = ToUtf8(buf);
if (!s) {
return IOError("Can't get test directory");
}
*result = std::string(s);
free(s);
return Status::OK();
}
virtual Status NewLogger(const std::string& fname, Logger** result) {
*result = NULL;
FILE* f = fopen(fname.c_str(), "wt");
if (f == NULL)
return Status::IOError(fname, strerror(errno));
*result = new WinLogger(f);
return Status::OK();
}
virtual uint64_t NowMicros() {
LARGE_INTEGER count;
QueryPerformanceCounter(&count);
return count.QuadPart * 1000000i64 / freq_.QuadPart;
}
virtual void SleepForMicroseconds(int micros) {
// round up to the next millisecond
Sleep((micros + 999) / 1000);
}
private:
LARGE_INTEGER freq_;
// BGThread() is the body of the background thread
void BGThread();
static unsigned __stdcall BGThreadWrapper(void* arg) {
(reinterpret_cast<WinEnv*>(arg))->BGThread();
_endthreadex(0);
return 0;
}
leveldb::port::Mutex mu_;
leveldb::port::CondVar bgsignal_;
HANDLE bgthread_;
// Entry per Schedule() call
struct BGItem { void* arg; void (*function)(void*); };
typedef std::deque<BGItem> BGQueue;
BGQueue queue_;
};
WinEnv::WinEnv() : bgthread_(NULL), bgsignal_(&mu_) {
QueryPerformanceFrequency(&freq_);
}
void WinEnv::Schedule(void (*function)(void*), void* arg) {
mu_.Lock();
// Start background thread if necessary
if (NULL == bgthread_) {
bgthread_ = (HANDLE)_beginthreadex(NULL, 0, &WinEnv::BGThreadWrapper, this, 0, NULL);
}
// Add to priority queue
queue_.push_back(BGItem());
queue_.back().function = function;
queue_.back().arg = arg;
mu_.Unlock();
bgsignal_.Signal();
}
void WinEnv::BGThread() {
while (true) {
// Wait until there is an item that is ready to run
mu_.Lock();
while (queue_.empty()) {
bgsignal_.Wait();
}
void (*function)(void*) = queue_.front().function;
void* arg = queue_.front().arg;
queue_.pop_front();
mu_.Unlock();
(*function)(arg);
}
// TODO: CloseHandle(bgthread_) ??
}
namespace {
struct StartThreadState {
void (*user_function)(void*);
void* arg;
HANDLE threadHandle;
};
}
static unsigned __stdcall StartThreadWrapper(void* arg) {
StartThreadState* state = reinterpret_cast<StartThreadState*>(arg);
state->user_function(state->arg);
_endthreadex(0);
CloseHandle(state->threadHandle);
delete state;
return 0;
}
void WinEnv::StartThread(void (*function)(void* arg), void* arg) {
StartThreadState* state = new StartThreadState;
state->user_function = function;
state->arg = arg;
state->threadHandle = (HANDLE)_beginthreadex(NULL, 0, &StartThreadWrapper, state, 0, NULL);
}
}
static Env* default_env;
static void InitDefaultEnv() { default_env = new WinEnv(); }
static leveldb::port::Mutex default_env_mutex;
Env* Env::Default() {
default_env_mutex.Lock();
if (NULL == default_env)
InitDefaultEnv();
default_env_mutex.Unlock();
return default_env;
}
}

View File

@ -0,0 +1,36 @@
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
#include "port_uv.h"
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include "util/logging.h"
namespace leveldb {
namespace port {
Mutex::Mutex() { uv_mutex_init(&mu_); }
Mutex::~Mutex() { uv_mutex_destroy(&mu_); }
void Mutex::Lock() { uv_mutex_lock(&mu_); }
void Mutex::Unlock() { uv_mutex_unlock(&mu_); }
CondVar::CondVar(Mutex* mu) : mu_(mu) { uv_cond_init(&cv_); }
CondVar::~CondVar() { uv_cond_destroy(&cv_); }
void CondVar::Wait() { uv_cond_wait(&cv_, &mu_->mu_); }
void CondVar::Signal() { uv_cond_signal(&cv_); }
void CondVar::SignalAll() { uv_cond_broadcast(&cv_); }
void InitOnce(OnceType* once, void (*initializer)()) { uv_once(once, initializer); }
} // namespace port
} // namespace leveldb

View File

@ -0,0 +1,157 @@
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
//
// See port_example.h for documentation for the following types/functions.
#ifndef STORAGE_LEVELDB_PORT_PORT_LIBUV_H_
#define STORAGE_LEVELDB_PORT_PORT_LIBUV_H_
#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
#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
#elif defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) ||\
defined(OS_DRAGONFLYBSD) || defined(OS_ANDROID)
#include <sys/types.h>
#include <sys/endian.h>
#elif defined(_MSC_VER)
#if _MSC_VER < 1600
#include "stdint-msvc2008.h"
#endif
#define PLATFORM_IS_LITTLE_ENDIAN true
#if _MSC_VER < 1900
#define snprintf _snprintf
#endif
#define close _close
#define fread_unlocked _fread_nolock
#else
#include <endian.h>
#endif
#include <uv.h>
#ifdef SNAPPY
#include <snappy.h>
#endif
#include <stdint.h>
#include <string>
#if defined(_MSC_VER)
#include "atomic_pointer_win.h"
#undef small
#else
#include "port/atomic_pointer.h"
#endif
#ifndef PLATFORM_IS_LITTLE_ENDIAN
#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) ||\
defined(OS_ANDROID)
// Use fread/fwrite/fflush on platforms without _unlocked variants
#define fread_unlocked fread
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif
#if defined(OS_MACOSX) || defined(OS_FREEBSD) ||\
defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
// Use fsync() on platforms without fdatasync()
#define fdatasync fsync
#endif
namespace leveldb {
namespace port {
static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
#undef PLATFORM_IS_LITTLE_ENDIAN
class CondVar;
class Mutex {
public:
Mutex();
~Mutex();
void Lock();
void Unlock();
void AssertHeld() { }
private:
friend class CondVar;
uv_mutex_t mu_;
// No copying
Mutex(const Mutex&);
void operator=(const Mutex&);
};
class CondVar {
public:
explicit CondVar(Mutex* mu);
~CondVar();
void Wait();
void Signal();
void SignalAll();
private:
uv_cond_t cv_;
Mutex* mu_;
};
typedef uv_once_t OnceType;
#define LEVELDB_ONCE_INIT UV_ONCE_INIT
extern void InitOnce(OnceType* once, void (*initializer)());
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;
}
uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size);
} // namespace port
} // namespace leveldb
#endif // STORAGE_LEVELDB_PORT_PORT_LIBUV_H_

View File

@ -0,0 +1,153 @@
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
//
// See port_example.h for documentation for the following types/functions.
#ifndef STORAGE_LEVELDB_PORT_PORT_LIBUV_H_
#define STORAGE_LEVELDB_PORT_PORT_LIBUV_H_
#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
#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
#elif defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) ||\
defined(OS_DRAGONFLYBSD) || defined(OS_ANDROID)
#include <sys/types.h>
#include <sys/endian.h>
#elif defined(_MSC_VER)
#define PLATFORM_IS_LITTLE_ENDIAN true
#define snprintf _snprintf
#define close _close
#define fread_unlocked _fread_nolock
#else
#include <endian.h>
#endif
typedef intptr_t ssize_t;
#define _SSIZE_T_
#define _SSIZE_T_DEFINED
#include <uv.h>
#ifdef SNAPPY
#include <snappy.h>
#endif
#if defined(_MSC_VER) && _MSC_VER < 1600
# include "stdint-msvc2008.h"
#else
# include <stdint.h>
#endif
#include <string>
#include "port/atomic_pointer.h"
#ifndef PLATFORM_IS_LITTLE_ENDIAN
#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) ||\
defined(OS_ANDROID)
// Use fread/fwrite/fflush on platforms without _unlocked variants
#define fread_unlocked fread
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif
#if defined(OS_MACOSX) || defined(OS_FREEBSD) ||\
defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
// Use fsync() on platforms without fdatasync()
#define fdatasync fsync
#endif
namespace leveldb {
namespace port {
static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
#undef PLATFORM_IS_LITTLE_ENDIAN
class CondVar;
class Mutex {
public:
Mutex();
~Mutex();
void Lock();
void Unlock();
void AssertHeld() { }
private:
friend class CondVar;
uv_mutex_t mu_;
// No copying
Mutex(const Mutex&);
void operator=(const Mutex&);
};
class CondVar {
public:
explicit CondVar(Mutex* mu);
~CondVar();
void Wait();
void Signal();
void SignalAll();
private:
uv_cond_t cv_;
Mutex* mu_;
};
typedef uv_once_t OnceType;
#define LEVELDB_ONCE_INIT 0
extern void InitOnce(OnceType* once, void (*initializer)());
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_LIBUV_H_

View File

@ -0,0 +1,247 @@
// ISO C9x compliant stdint.h for Microsoft Visual Studio
// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124
//
// Copyright (c) 2006-2008 Alexander Chemeris
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. The name of the author may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef _MSC_VER // [
#error "Use this header only with Microsoft Visual C++ compilers!"
#endif // _MSC_VER ]
#ifndef _MSC_STDINT_H_ // [
#define _MSC_STDINT_H_
#if _MSC_VER > 1000
#pragma once
#endif
#include <limits.h>
// For Visual Studio 6 in C++ mode and for many Visual Studio versions when
// compiling for ARM we should wrap <wchar.h> include with 'extern "C++" {}'
// or compiler give many errors like this:
// error C2733: second C linkage of overloaded function 'wmemchr' not allowed
#ifdef __cplusplus
extern "C" {
#endif
# include <wchar.h>
#ifdef __cplusplus
}
#endif
// Define _W64 macros to mark types changing their size, like intptr_t.
#ifndef _W64
# if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300
# define _W64 __w64
# else
# define _W64
# endif
#endif
// 7.18.1 Integer types
// 7.18.1.1 Exact-width integer types
// Visual Studio 6 and Embedded Visual C++ 4 doesn't
// realize that, e.g. char has the same size as __int8
// so we give up on __intX for them.
#if (_MSC_VER < 1300)
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
#else
typedef signed __int8 int8_t;
typedef signed __int16 int16_t;
typedef signed __int32 int32_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
#endif
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
// 7.18.1.2 Minimum-width integer types
typedef int8_t int_least8_t;
typedef int16_t int_least16_t;
typedef int32_t int_least32_t;
typedef int64_t int_least64_t;
typedef uint8_t uint_least8_t;
typedef uint16_t uint_least16_t;
typedef uint32_t uint_least32_t;
typedef uint64_t uint_least64_t;
// 7.18.1.3 Fastest minimum-width integer types
typedef int8_t int_fast8_t;
typedef int16_t int_fast16_t;
typedef int32_t int_fast32_t;
typedef int64_t int_fast64_t;
typedef uint8_t uint_fast8_t;
typedef uint16_t uint_fast16_t;
typedef uint32_t uint_fast32_t;
typedef uint64_t uint_fast64_t;
// 7.18.1.4 Integer types capable of holding object pointers
#ifdef _WIN64 // [
typedef signed __int64 intptr_t;
typedef unsigned __int64 uintptr_t;
#else // _WIN64 ][
typedef _W64 signed int intptr_t;
typedef _W64 unsigned int uintptr_t;
#endif // _WIN64 ]
// 7.18.1.5 Greatest-width integer types
typedef int64_t intmax_t;
typedef uint64_t uintmax_t;
// 7.18.2 Limits of specified-width integer types
#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [ See footnote 220 at page 257 and footnote 221 at page 259
// 7.18.2.1 Limits of exact-width integer types
#define INT8_MIN ((int8_t)_I8_MIN)
#define INT8_MAX _I8_MAX
#define INT16_MIN ((int16_t)_I16_MIN)
#define INT16_MAX _I16_MAX
#define INT32_MIN ((int32_t)_I32_MIN)
#define INT32_MAX _I32_MAX
#define INT64_MIN ((int64_t)_I64_MIN)
#define INT64_MAX _I64_MAX
#define UINT8_MAX _UI8_MAX
#define UINT16_MAX _UI16_MAX
#define UINT32_MAX _UI32_MAX
#define UINT64_MAX _UI64_MAX
// 7.18.2.2 Limits of minimum-width integer types
#define INT_LEAST8_MIN INT8_MIN
#define INT_LEAST8_MAX INT8_MAX
#define INT_LEAST16_MIN INT16_MIN
#define INT_LEAST16_MAX INT16_MAX
#define INT_LEAST32_MIN INT32_MIN
#define INT_LEAST32_MAX INT32_MAX
#define INT_LEAST64_MIN INT64_MIN
#define INT_LEAST64_MAX INT64_MAX
#define UINT_LEAST8_MAX UINT8_MAX
#define UINT_LEAST16_MAX UINT16_MAX
#define UINT_LEAST32_MAX UINT32_MAX
#define UINT_LEAST64_MAX UINT64_MAX
// 7.18.2.3 Limits of fastest minimum-width integer types
#define INT_FAST8_MIN INT8_MIN
#define INT_FAST8_MAX INT8_MAX
#define INT_FAST16_MIN INT16_MIN
#define INT_FAST16_MAX INT16_MAX
#define INT_FAST32_MIN INT32_MIN
#define INT_FAST32_MAX INT32_MAX
#define INT_FAST64_MIN INT64_MIN
#define INT_FAST64_MAX INT64_MAX
#define UINT_FAST8_MAX UINT8_MAX
#define UINT_FAST16_MAX UINT16_MAX
#define UINT_FAST32_MAX UINT32_MAX
#define UINT_FAST64_MAX UINT64_MAX
// 7.18.2.4 Limits of integer types capable of holding object pointers
#ifdef _WIN64 // [
# define INTPTR_MIN INT64_MIN
# define INTPTR_MAX INT64_MAX
# define UINTPTR_MAX UINT64_MAX
#else // _WIN64 ][
# define INTPTR_MIN INT32_MIN
# define INTPTR_MAX INT32_MAX
# define UINTPTR_MAX UINT32_MAX
#endif // _WIN64 ]
// 7.18.2.5 Limits of greatest-width integer types
#define INTMAX_MIN INT64_MIN
#define INTMAX_MAX INT64_MAX
#define UINTMAX_MAX UINT64_MAX
// 7.18.3 Limits of other integer types
#ifdef _WIN64 // [
# define PTRDIFF_MIN _I64_MIN
# define PTRDIFF_MAX _I64_MAX
#else // _WIN64 ][
# define PTRDIFF_MIN _I32_MIN
# define PTRDIFF_MAX _I32_MAX
#endif // _WIN64 ]
#define SIG_ATOMIC_MIN INT_MIN
#define SIG_ATOMIC_MAX INT_MAX
#ifndef SIZE_MAX // [
# ifdef _WIN64 // [
# define SIZE_MAX _UI64_MAX
# else // _WIN64 ][
# define SIZE_MAX _UI32_MAX
# endif // _WIN64 ]
#endif // SIZE_MAX ]
// WCHAR_MIN and WCHAR_MAX are also defined in <wchar.h>
#ifndef WCHAR_MIN // [
# define WCHAR_MIN 0
#endif // WCHAR_MIN ]
#ifndef WCHAR_MAX // [
# define WCHAR_MAX _UI16_MAX
#endif // WCHAR_MAX ]
#define WINT_MIN 0
#define WINT_MAX _UI16_MAX
#endif // __STDC_LIMIT_MACROS ]
// 7.18.4 Limits of other integer types
#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260
// 7.18.4.1 Macros for minimum-width integer constants
#define INT8_C(val) val##i8
#define INT16_C(val) val##i16
#define INT32_C(val) val##i32
#define INT64_C(val) val##i64
#define UINT8_C(val) val##ui8
#define UINT16_C(val) val##ui16
#define UINT32_C(val) val##ui32
#define UINT64_C(val) val##ui64
// 7.18.4.2 Macros for greatest-width integer constants
#define INTMAX_C INT64_C
#define UINTMAX_C UINT64_C
#endif // __STDC_CONSTANT_MACROS ]
#endif // _MSC_STDINT_H_ ]

View File

@ -0,0 +1,45 @@
// uv_cond_* backport
// Lifted from the Node 0.9 version of libuv for Node 0.8 compatibility
// https://github.com/joyent/libuv/
// libuv copyright notice:
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef LEVELDB_PORT_LIBUV_CONVAR_H_
#define LEVELDB_PORT_LIBUV_CONVAR_H_
#include <uv.h>
#ifdef LDB_UV_POSIX
typedef pthread_cond_t ldb_uv_cond_t;
#endif
UV_EXTERN int ldb_uv_cond_init(ldb_uv_cond_t* cond);
UV_EXTERN void ldb_uv_cond_destroy(ldb_uv_cond_t* cond);
UV_EXTERN void ldb_uv_cond_signal(ldb_uv_cond_t* cond);
UV_EXTERN void ldb_uv_cond_broadcast(ldb_uv_cond_t* cond);
UV_EXTERN void ldb_uv_cond_wait(ldb_uv_cond_t* cond, uv_mutex_t* mutex);
UV_EXTERN int ldb_uv_cond_timedwait(ldb_uv_cond_t* cond, uv_mutex_t* mutex, uint64_t timeout);
#endif // LEVELDB_PORT_LIBUV_CONVAR_H_

View File

@ -0,0 +1,139 @@
// uv_cond_* backport
// Lifted from the Node 0.9 version of libuv for Node 0.8 compatibility
// https://github.com/joyent/libuv/
// libuv copyright notice:
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <pthread.h>
#include "uv_condvar.h"
#include <stdlib.h> /* abort */
#include <errno.h> /* ETIMEDOUT */
#undef NANOSEC
#define NANOSEC ((uint64_t) 1e9)
#if defined(__APPLE__) && defined(__MACH__)
int ldb_uv_cond_init(ldb_uv_cond_t* cond) {
if (pthread_cond_init(cond, NULL))
return -1;
else
return 0;
}
#else /* !(defined(__APPLE__) && defined(__MACH__)) */
int ldb_uv_cond_init(ldb_uv_cond_t* cond) {
pthread_condattr_t attr;
if (pthread_condattr_init(&attr))
return -1;
if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC))
goto error2;
if (pthread_cond_init(cond, &attr))
goto error2;
if (pthread_condattr_destroy(&attr))
goto error;
return 0;
error:
pthread_cond_destroy(cond);
error2:
pthread_condattr_destroy(&attr);
return -1;
}
#endif /* defined(__APPLE__) && defined(__MACH__) */
void ldb_uv_cond_destroy(ldb_uv_cond_t* cond) {
if (pthread_cond_destroy(cond))
abort();
}
void ldb_uv_cond_signal(ldb_uv_cond_t* cond) {
if (pthread_cond_signal(cond))
abort();
}
void ldb_uv_cond_broadcast(ldb_uv_cond_t* cond) {
if (pthread_cond_broadcast(cond))
abort();
}
void ldb_uv_cond_wait(ldb_uv_cond_t* cond, uv_mutex_t* mutex) {
if (pthread_cond_wait(cond, mutex))
abort();
}
#if defined(__APPLE__) && defined(__MACH__)
int ldb_uv_cond_timedwait(ldb_uv_cond_t* cond, uv_mutex_t* mutex, uint64_t timeout) {
int r;
struct timeval tv;
struct timespec ts;
uint64_t abstime;
gettimeofday(&tv, NULL);
abstime = tv.tv_sec * 1e9 + tv.tv_usec * 1e3 + timeout;
ts.tv_sec = abstime / NANOSEC;
ts.tv_nsec = abstime % NANOSEC;
r = pthread_cond_timedwait(cond, mutex, &ts);
if (r == 0)
return 0;
if (r == ETIMEDOUT)
return -1;
abort();
return -1; /* Satisfy the compiler. */
}
#else /* !(defined(__APPLE__) && defined(__MACH__)) */
int ldb_uv_cond_timedwait(ldb_uv_cond_t* cond, uv_mutex_t* mutex, uint64_t timeout) {
int r;
struct timespec ts;
uint64_t abstime;
abstime = uv_hrtime() + timeout;
ts.tv_sec = abstime / NANOSEC;
ts.tv_nsec = abstime % NANOSEC;
r = pthread_cond_timedwait(cond, mutex, &ts);
if (r == 0)
return 0;
if (r == ETIMEDOUT)
return -1;
abort();
return -1; /* Satisfy the compiler. */
}
#endif /* defined(__APPLE__) && defined(__MACH__) */

View File

@ -0,0 +1,79 @@
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
#include "win_logger.h"
#include <windows.h>
namespace leveldb {
void WinLogger::Logv(const char* format, va_list ap) {
const uint64_t thread_id = static_cast<uint64_t>(::GetCurrentThreadId());
// We try twice: the first time with a fixed-size stack allocated buffer,
// and the second time with a much larger dynamically allocated buffer.
char buffer[500];
for (int iter = 0; iter < 2; iter++) {
char* base;
int bufsize;
if (iter == 0) {
bufsize = sizeof(buffer);
base = buffer;
} else {
bufsize = 30000;
base = new char[bufsize];
}
char* p = base;
char* limit = base + bufsize;
SYSTEMTIME st;
// GetSystemTime returns UTC time, we want local time!
::GetLocalTime(&st);
p += _snprintf_s(p, limit - p, _TRUNCATE,
"%04d/%02d/%02d-%02d:%02d:%02d.%03d %llx ",
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wSecond,
st.wMilliseconds,
static_cast<long long unsigned int>(thread_id));
// Print the message
if (p < limit) {
va_list backup_ap = ap;
p += vsnprintf(p, limit - p, format, backup_ap);
va_end(backup_ap);
}
// Truncate to available space if necessary
if (p >= limit) {
if (iter == 0) {
continue; // Try again with larger buffer
} else {
p = limit - 1;
}
}
// Add newline if necessary
if (p == base || p[-1] != '\n') {
*p++ = '\n';
}
assert(p <= limit);
fwrite(base, 1, p - base, file_);
fflush(file_);
if (base != buffer) {
delete[] base;
}
break;
}
}
}

View File

@ -0,0 +1,28 @@
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
// Logger implementation for Windows
#ifndef STORAGE_LEVELDB_UTIL_WIN_LOGGER_H_
#define STORAGE_LEVELDB_UTIL_WIN_LOGGER_H_
#include <stdio.h>
#include "leveldb/env.h"
namespace leveldb {
class WinLogger : public Logger {
private:
FILE* file_;
public:
explicit WinLogger(FILE* f) : file_(f) { assert(file_); }
virtual ~WinLogger() {
fclose(file_);
}
virtual void Logv(const char* format, va_list ap);
};
}
#endif // STORAGE_LEVELDB_UTIL_WIN_LOGGER_H_