2019-03-01 13:12:01 -08:00
|
|
|
// Copyright (c) 2018 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.
|
|
|
|
|
|
|
|
// Prevent Windows headers from defining min/max macros and instead
|
|
|
|
// use STL.
|
2019-03-21 16:45:04 +01:00
|
|
|
#ifndef NOMINMAX
|
2019-03-01 13:12:01 -08:00
|
|
|
#define NOMINMAX
|
2019-03-21 16:45:04 +01:00
|
|
|
#endif // ifndef NOMINMAX
|
2019-03-01 13:12:01 -08:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
2019-03-11 13:04:53 -07:00
|
|
|
#include <atomic>
|
2019-03-01 13:12:01 -08:00
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
2019-06-12 14:05:14 -07:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2019-03-01 13:12:01 -08:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2019-06-12 14:05:14 -07:00
|
|
|
#include <queue>
|
2019-03-01 13:12:01 -08:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/slice.h"
|
|
|
|
#include "port/port.h"
|
|
|
|
#include "port/thread_annotations.h"
|
|
|
|
#include "util/env_windows_test_helper.h"
|
|
|
|
#include "util/logging.h"
|
|
|
|
#include "util/mutexlock.h"
|
|
|
|
#include "util/windows_logger.h"
|
|
|
|
|
|
|
|
#if defined(DeleteFile)
|
|
|
|
#undef DeleteFile
|
|
|
|
#endif // defined(DeleteFile)
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr const size_t kWritableFileBufferSize = 65536;
|
|
|
|
|
|
|
|
// Up to 1000 mmaps for 64-bit binaries; none for 32-bit.
|
2019-06-12 14:05:14 -07:00
|
|
|
constexpr int kDefaultMmapLimit = (sizeof(void*) >= 8) ? 1000 : 0;
|
2019-03-01 13:12:01 -08:00
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
// Can be set by by EnvWindowsTestHelper::SetReadOnlyMMapLimit().
|
2019-03-01 13:12:01 -08:00
|
|
|
int g_mmap_limit = kDefaultMmapLimit;
|
|
|
|
|
|
|
|
std::string GetWindowsErrorMessage(DWORD error_code) {
|
|
|
|
std::string message;
|
|
|
|
char* error_text = nullptr;
|
|
|
|
// Use MBCS version of FormatMessage to match return value.
|
|
|
|
size_t error_text_size = ::FormatMessageA(
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
reinterpret_cast<char*>(&error_text), 0, nullptr);
|
|
|
|
if (!error_text) {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
message.assign(error_text, error_text_size);
|
|
|
|
::LocalFree(error_text);
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status WindowsError(const std::string& context, DWORD error_code) {
|
|
|
|
if (error_code == ERROR_FILE_NOT_FOUND || error_code == ERROR_PATH_NOT_FOUND)
|
|
|
|
return Status::NotFound(context, GetWindowsErrorMessage(error_code));
|
|
|
|
return Status::IOError(context, GetWindowsErrorMessage(error_code));
|
|
|
|
}
|
|
|
|
|
|
|
|
class ScopedHandle {
|
|
|
|
public:
|
|
|
|
ScopedHandle(HANDLE handle) : handle_(handle) {}
|
2019-06-12 14:05:14 -07:00
|
|
|
ScopedHandle(const ScopedHandle&) = delete;
|
2019-03-01 13:12:01 -08:00
|
|
|
ScopedHandle(ScopedHandle&& other) noexcept : handle_(other.Release()) {}
|
|
|
|
~ScopedHandle() { Close(); }
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
ScopedHandle& operator=(const ScopedHandle&) = delete;
|
|
|
|
|
2019-03-01 13:12:01 -08:00
|
|
|
ScopedHandle& operator=(ScopedHandle&& rhs) noexcept {
|
|
|
|
if (this != &rhs) handle_ = rhs.Release();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Close() {
|
|
|
|
if (!is_valid()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
HANDLE h = handle_;
|
|
|
|
handle_ = INVALID_HANDLE_VALUE;
|
|
|
|
return ::CloseHandle(h);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_valid() const {
|
|
|
|
return handle_ != INVALID_HANDLE_VALUE && handle_ != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE get() const { return handle_; }
|
|
|
|
|
|
|
|
HANDLE Release() {
|
|
|
|
HANDLE h = handle_;
|
|
|
|
handle_ = INVALID_HANDLE_VALUE;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
HANDLE handle_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helper class to limit resource usage to avoid exhaustion.
|
2019-03-11 13:04:53 -07:00
|
|
|
// Currently used to limit read-only file descriptors and mmap file usage
|
|
|
|
// so that we do not run out of file descriptors or virtual memory, or run into
|
|
|
|
// kernel performance problems for very large databases.
|
2019-03-01 13:12:01 -08:00
|
|
|
class Limiter {
|
|
|
|
public:
|
2019-03-11 13:04:53 -07:00
|
|
|
// Limit maximum number of resources to |max_acquires|.
|
|
|
|
Limiter(int max_acquires) : acquires_allowed_(max_acquires) {}
|
|
|
|
|
|
|
|
Limiter(const Limiter&) = delete;
|
|
|
|
Limiter operator=(const Limiter&) = delete;
|
2019-03-01 13:12:01 -08:00
|
|
|
|
|
|
|
// If another resource is available, acquire it and return true.
|
|
|
|
// Else return false.
|
2019-03-11 13:04:53 -07:00
|
|
|
bool Acquire() {
|
|
|
|
int old_acquires_allowed =
|
|
|
|
acquires_allowed_.fetch_sub(1, std::memory_order_relaxed);
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
if (old_acquires_allowed > 0) return true;
|
2019-03-11 13:04:53 -07:00
|
|
|
|
|
|
|
acquires_allowed_.fetch_add(1, std::memory_order_relaxed);
|
|
|
|
return false;
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release a resource acquired by a previous call to Acquire() that returned
|
|
|
|
// true.
|
2019-05-02 11:01:00 -07:00
|
|
|
void Release() { acquires_allowed_.fetch_add(1, std::memory_order_relaxed); }
|
2019-03-01 13:12:01 -08:00
|
|
|
|
|
|
|
private:
|
2019-03-11 13:04:53 -07:00
|
|
|
// The number of available resources.
|
|
|
|
//
|
|
|
|
// This is a counter and is not tied to the invariants of any other class, so
|
|
|
|
// it can be operated on safely using std::memory_order_relaxed.
|
|
|
|
std::atomic<int> acquires_allowed_;
|
2019-03-01 13:12:01 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
class WindowsSequentialFile : public SequentialFile {
|
|
|
|
public:
|
2019-06-12 14:05:14 -07:00
|
|
|
WindowsSequentialFile(std::string filename, ScopedHandle handle)
|
|
|
|
: handle_(std::move(handle)), filename_(std::move(filename)) {}
|
2019-03-01 13:12:01 -08:00
|
|
|
~WindowsSequentialFile() override {}
|
|
|
|
|
|
|
|
Status Read(size_t n, Slice* result, char* scratch) override {
|
|
|
|
DWORD bytes_read;
|
|
|
|
// DWORD is 32-bit, but size_t could technically be larger. However leveldb
|
|
|
|
// files are limited to leveldb::Options::max_file_size which is clamped to
|
|
|
|
// 1<<30 or 1 GiB.
|
|
|
|
assert(n <= std::numeric_limits<DWORD>::max());
|
2019-06-12 14:05:14 -07:00
|
|
|
if (!::ReadFile(handle_.get(), scratch, static_cast<DWORD>(n), &bytes_read,
|
2019-03-01 13:12:01 -08:00
|
|
|
nullptr)) {
|
2019-06-12 14:05:14 -07:00
|
|
|
return WindowsError(filename_, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
2019-06-12 14:05:14 -07:00
|
|
|
|
|
|
|
*result = Slice(scratch, bytes_read);
|
|
|
|
return Status::OK();
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Status Skip(uint64_t n) override {
|
|
|
|
LARGE_INTEGER distance;
|
|
|
|
distance.QuadPart = n;
|
2019-06-12 14:05:14 -07:00
|
|
|
if (!::SetFilePointerEx(handle_.get(), distance, nullptr, FILE_CURRENT)) {
|
2019-03-01 13:12:01 -08:00
|
|
|
return WindowsError(filename_, ::GetLastError());
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-06-12 14:05:14 -07:00
|
|
|
const ScopedHandle handle_;
|
|
|
|
const std::string filename_;
|
2019-03-01 13:12:01 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
class WindowsRandomAccessFile : public RandomAccessFile {
|
|
|
|
public:
|
2019-06-12 14:05:14 -07:00
|
|
|
WindowsRandomAccessFile(std::string filename, ScopedHandle handle)
|
|
|
|
: handle_(std::move(handle)), filename_(std::move(filename)) {}
|
2019-03-01 13:12:01 -08:00
|
|
|
|
|
|
|
~WindowsRandomAccessFile() override = default;
|
|
|
|
|
|
|
|
Status Read(uint64_t offset, size_t n, Slice* result,
|
|
|
|
char* scratch) const override {
|
|
|
|
DWORD bytes_read = 0;
|
|
|
|
OVERLAPPED overlapped = {0};
|
|
|
|
|
|
|
|
overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32);
|
|
|
|
overlapped.Offset = static_cast<DWORD>(offset);
|
|
|
|
if (!::ReadFile(handle_.get(), scratch, static_cast<DWORD>(n), &bytes_read,
|
|
|
|
&overlapped)) {
|
|
|
|
DWORD error_code = ::GetLastError();
|
|
|
|
if (error_code != ERROR_HANDLE_EOF) {
|
|
|
|
*result = Slice(scratch, 0);
|
|
|
|
return Status::IOError(filename_, GetWindowsErrorMessage(error_code));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = Slice(scratch, bytes_read);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-06-12 14:05:14 -07:00
|
|
|
const ScopedHandle handle_;
|
|
|
|
const std::string filename_;
|
2019-03-01 13:12:01 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
class WindowsMmapReadableFile : public RandomAccessFile {
|
|
|
|
public:
|
|
|
|
// base[0,length-1] contains the mmapped contents of the file.
|
2019-06-12 14:05:14 -07:00
|
|
|
WindowsMmapReadableFile(std::string filename, char* mmap_base, size_t length,
|
|
|
|
Limiter* mmap_limiter)
|
|
|
|
: mmap_base_(mmap_base),
|
2019-03-01 13:12:01 -08:00
|
|
|
length_(length),
|
2019-06-12 14:05:14 -07:00
|
|
|
mmap_limiter_(mmap_limiter),
|
|
|
|
filename_(std::move(filename)) {}
|
2019-03-01 13:12:01 -08:00
|
|
|
|
|
|
|
~WindowsMmapReadableFile() override {
|
2019-06-12 14:05:14 -07:00
|
|
|
::UnmapViewOfFile(mmap_base_);
|
|
|
|
mmap_limiter_->Release();
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Status Read(uint64_t offset, size_t n, Slice* result,
|
|
|
|
char* scratch) const override {
|
|
|
|
if (offset + n > length_) {
|
|
|
|
*result = Slice();
|
2019-06-12 14:05:14 -07:00
|
|
|
return WindowsError(filename_, ERROR_INVALID_PARAMETER);
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
2019-06-12 14:05:14 -07:00
|
|
|
|
|
|
|
*result = Slice(mmap_base_ + offset, n);
|
|
|
|
return Status::OK();
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-06-12 14:05:14 -07:00
|
|
|
char* const mmap_base_;
|
|
|
|
const size_t length_;
|
|
|
|
Limiter* const mmap_limiter_;
|
|
|
|
const std::string filename_;
|
2019-03-01 13:12:01 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
class WindowsWritableFile : public WritableFile {
|
|
|
|
public:
|
2019-06-12 14:05:14 -07:00
|
|
|
WindowsWritableFile(std::string filename, ScopedHandle handle)
|
|
|
|
: pos_(0), handle_(std::move(handle)), filename_(std::move(filename)) {}
|
2019-03-01 13:12:01 -08:00
|
|
|
|
|
|
|
~WindowsWritableFile() override = default;
|
|
|
|
|
|
|
|
Status Append(const Slice& data) override {
|
2019-06-12 14:05:14 -07:00
|
|
|
size_t write_size = data.size();
|
|
|
|
const char* write_data = data.data();
|
2019-03-01 13:12:01 -08:00
|
|
|
|
|
|
|
// Fit as much as possible into buffer.
|
2019-06-12 14:05:14 -07:00
|
|
|
size_t copy_size = std::min(write_size, kWritableFileBufferSize - pos_);
|
|
|
|
std::memcpy(buf_ + pos_, write_data, copy_size);
|
|
|
|
write_data += copy_size;
|
|
|
|
write_size -= copy_size;
|
|
|
|
pos_ += copy_size;
|
|
|
|
if (write_size == 0) {
|
2019-03-01 13:12:01 -08:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't fit in buffer, so need to do at least one write.
|
2019-06-12 14:05:14 -07:00
|
|
|
Status status = FlushBuffer();
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Small writes go to buffer, large writes are written directly.
|
2019-06-12 14:05:14 -07:00
|
|
|
if (write_size < kWritableFileBufferSize) {
|
|
|
|
std::memcpy(buf_, write_data, write_size);
|
|
|
|
pos_ = write_size;
|
2019-03-01 13:12:01 -08:00
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-06-12 14:05:14 -07:00
|
|
|
return WriteUnbuffered(write_data, write_size);
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Status Close() override {
|
2019-06-12 14:05:14 -07:00
|
|
|
Status status = FlushBuffer();
|
|
|
|
if (!handle_.Close() && status.ok()) {
|
|
|
|
status = WindowsError(filename_, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
2019-06-12 14:05:14 -07:00
|
|
|
return status;
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Status Flush() override { return FlushBuffer(); }
|
2019-03-01 13:12:01 -08:00
|
|
|
|
|
|
|
Status Sync() override {
|
2019-06-12 14:05:14 -07:00
|
|
|
// On Windows no need to sync parent directory. Its metadata will be updated
|
|
|
|
// via the creation of the new file, without an explicit sync.
|
|
|
|
|
|
|
|
Status status = FlushBuffer();
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!::FlushFileBuffers(handle_.get())) {
|
|
|
|
return Status::IOError(filename_,
|
|
|
|
GetWindowsErrorMessage(::GetLastError()));
|
|
|
|
}
|
|
|
|
return Status::OK();
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-06-12 14:05:14 -07:00
|
|
|
Status FlushBuffer() {
|
|
|
|
Status status = WriteUnbuffered(buf_, pos_);
|
2019-03-01 13:12:01 -08:00
|
|
|
pos_ = 0;
|
2019-06-12 14:05:14 -07:00
|
|
|
return status;
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Status WriteUnbuffered(const char* data, size_t size) {
|
2019-03-01 13:12:01 -08:00
|
|
|
DWORD bytes_written;
|
2019-06-12 14:05:14 -07:00
|
|
|
if (!::WriteFile(handle_.get(), data, static_cast<DWORD>(size),
|
|
|
|
&bytes_written, nullptr)) {
|
2019-03-01 13:12:01 -08:00
|
|
|
return Status::IOError(filename_,
|
|
|
|
GetWindowsErrorMessage(::GetLastError()));
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
// buf_[0, pos_-1] contains data to be written to handle_.
|
|
|
|
char buf_[kWritableFileBufferSize];
|
|
|
|
size_t pos_;
|
2019-06-12 14:05:14 -07:00
|
|
|
|
|
|
|
ScopedHandle handle_;
|
|
|
|
const std::string filename_;
|
2019-03-01 13:12:01 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Lock or unlock the entire file as specified by |lock|. Returns true
|
|
|
|
// when successful, false upon failure. Caller should call ::GetLastError()
|
|
|
|
// to determine cause of failure
|
|
|
|
bool LockOrUnlock(HANDLE handle, bool lock) {
|
|
|
|
if (lock) {
|
|
|
|
return ::LockFile(handle,
|
|
|
|
/*dwFileOffsetLow=*/0, /*dwFileOffsetHigh=*/0,
|
|
|
|
/*nNumberOfBytesToLockLow=*/MAXDWORD,
|
|
|
|
/*nNumberOfBytesToLockHigh=*/MAXDWORD);
|
|
|
|
} else {
|
|
|
|
return ::UnlockFile(handle,
|
|
|
|
/*dwFileOffsetLow=*/0, /*dwFileOffsetHigh=*/0,
|
|
|
|
/*nNumberOfBytesToLockLow=*/MAXDWORD,
|
|
|
|
/*nNumberOfBytesToLockHigh=*/MAXDWORD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class WindowsFileLock : public FileLock {
|
|
|
|
public:
|
2019-06-12 14:05:14 -07:00
|
|
|
WindowsFileLock(ScopedHandle handle, std::string filename)
|
|
|
|
: handle_(std::move(handle)), filename_(std::move(filename)) {}
|
2019-03-01 13:12:01 -08:00
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
const ScopedHandle& handle() const { return handle_; }
|
|
|
|
const std::string& filename() const { return filename_; }
|
2019-03-01 13:12:01 -08:00
|
|
|
|
|
|
|
private:
|
2019-06-12 14:05:14 -07:00
|
|
|
const ScopedHandle handle_;
|
|
|
|
const std::string filename_;
|
2019-03-01 13:12:01 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
class WindowsEnv : public Env {
|
|
|
|
public:
|
|
|
|
WindowsEnv();
|
|
|
|
~WindowsEnv() override {
|
2019-06-12 14:05:14 -07:00
|
|
|
static const char msg[] =
|
|
|
|
"WindowsEnv singleton destroyed. Unsupported behavior!\n";
|
|
|
|
std::fwrite(msg, 1, sizeof(msg), stderr);
|
|
|
|
std::abort();
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Status NewSequentialFile(const std::string& filename,
|
2019-03-01 13:12:01 -08:00
|
|
|
SequentialFile** result) override {
|
|
|
|
*result = nullptr;
|
|
|
|
DWORD desired_access = GENERIC_READ;
|
|
|
|
DWORD share_mode = FILE_SHARE_READ;
|
2019-06-12 14:05:14 -07:00
|
|
|
ScopedHandle handle = ::CreateFileA(
|
|
|
|
filename.c_str(), desired_access, share_mode,
|
|
|
|
/*lpSecurityAttributes=*/nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
|
|
|
|
/*hTemplateFile=*/nullptr);
|
2019-03-01 13:12:01 -08:00
|
|
|
if (!handle.is_valid()) {
|
2019-06-12 14:05:14 -07:00
|
|
|
return WindowsError(filename, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
2019-06-12 14:05:14 -07:00
|
|
|
|
|
|
|
*result = new WindowsSequentialFile(filename, std::move(handle));
|
2019-03-01 13:12:01 -08:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Status NewRandomAccessFile(const std::string& filename,
|
2019-03-01 13:12:01 -08:00
|
|
|
RandomAccessFile** result) override {
|
|
|
|
*result = nullptr;
|
|
|
|
DWORD desired_access = GENERIC_READ;
|
|
|
|
DWORD share_mode = FILE_SHARE_READ;
|
|
|
|
ScopedHandle handle =
|
2019-06-12 14:05:14 -07:00
|
|
|
::CreateFileA(filename.c_str(), desired_access, share_mode,
|
|
|
|
/*lpSecurityAttributes=*/nullptr, OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_READONLY,
|
|
|
|
/*hTemplateFile=*/nullptr);
|
2019-03-01 13:12:01 -08:00
|
|
|
if (!handle.is_valid()) {
|
2019-06-12 14:05:14 -07:00
|
|
|
return WindowsError(filename, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
if (!mmap_limiter_.Acquire()) {
|
2019-06-12 14:05:14 -07:00
|
|
|
*result = new WindowsRandomAccessFile(filename, std::move(handle));
|
2019-03-01 13:12:01 -08:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
LARGE_INTEGER file_size;
|
2019-06-12 14:05:14 -07:00
|
|
|
Status status;
|
2019-03-01 13:12:01 -08:00
|
|
|
if (!::GetFileSizeEx(handle.get(), &file_size)) {
|
2019-06-12 14:05:14 -07:00
|
|
|
mmap_limiter_.Release();
|
|
|
|
return WindowsError(filename, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedHandle mapping =
|
|
|
|
::CreateFileMappingA(handle.get(),
|
|
|
|
/*security attributes=*/nullptr, PAGE_READONLY,
|
|
|
|
/*dwMaximumSizeHigh=*/0,
|
2019-06-12 14:05:14 -07:00
|
|
|
/*dwMaximumSizeLow=*/0,
|
|
|
|
/*lpName=*/nullptr);
|
2019-03-01 13:12:01 -08:00
|
|
|
if (mapping.is_valid()) {
|
2019-06-12 14:05:14 -07:00
|
|
|
void* mmap_base = ::MapViewOfFile(mapping.get(), FILE_MAP_READ,
|
|
|
|
/*dwFileOffsetHigh=*/0,
|
|
|
|
/*dwFileOffsetLow=*/0,
|
|
|
|
/*dwNumberOfBytesToMap=*/0);
|
|
|
|
if (mmap_base) {
|
2019-03-01 13:12:01 -08:00
|
|
|
*result = new WindowsMmapReadableFile(
|
2019-06-12 14:05:14 -07:00
|
|
|
filename, reinterpret_cast<char*>(mmap_base),
|
|
|
|
static_cast<size_t>(file_size.QuadPart), &mmap_limiter_);
|
2019-03-01 13:12:01 -08:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
}
|
2019-06-12 14:05:14 -07:00
|
|
|
mmap_limiter_.Release();
|
|
|
|
return WindowsError(filename, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Status NewWritableFile(const std::string& filename,
|
2019-03-01 13:12:01 -08:00
|
|
|
WritableFile** result) override {
|
|
|
|
DWORD desired_access = GENERIC_WRITE;
|
2019-06-12 14:05:14 -07:00
|
|
|
DWORD share_mode = 0; // Exclusive access.
|
|
|
|
ScopedHandle handle = ::CreateFileA(
|
|
|
|
filename.c_str(), desired_access, share_mode,
|
|
|
|
/*lpSecurityAttributes=*/nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
|
|
|
|
/*hTemplateFile=*/nullptr);
|
2019-03-01 13:12:01 -08:00
|
|
|
if (!handle.is_valid()) {
|
|
|
|
*result = nullptr;
|
2019-06-12 14:05:14 -07:00
|
|
|
return WindowsError(filename, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
*result = new WindowsWritableFile(filename, std::move(handle));
|
2019-03-01 13:12:01 -08:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Status NewAppendableFile(const std::string& filename,
|
2019-03-01 13:12:01 -08:00
|
|
|
WritableFile** result) override {
|
2019-06-12 14:05:14 -07:00
|
|
|
DWORD desired_access = FILE_APPEND_DATA;
|
|
|
|
DWORD share_mode = 0; // Exclusive access.
|
|
|
|
ScopedHandle handle = ::CreateFileA(
|
|
|
|
filename.c_str(), desired_access, share_mode,
|
|
|
|
/*lpSecurityAttributes=*/nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
|
|
|
|
/*hTemplateFile=*/nullptr);
|
2019-03-01 13:12:01 -08:00
|
|
|
if (!handle.is_valid()) {
|
|
|
|
*result = nullptr;
|
2019-06-12 14:05:14 -07:00
|
|
|
return WindowsError(filename, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
*result = new WindowsWritableFile(filename, std::move(handle));
|
2019-03-01 13:12:01 -08:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
bool FileExists(const std::string& filename) override {
|
|
|
|
return GetFileAttributesA(filename.c_str()) != INVALID_FILE_ATTRIBUTES;
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Status GetChildren(const std::string& directory_path,
|
2019-03-01 13:12:01 -08:00
|
|
|
std::vector<std::string>* result) override {
|
2019-06-12 14:05:14 -07:00
|
|
|
const std::string find_pattern = directory_path + "\\*";
|
2019-03-01 13:12:01 -08:00
|
|
|
WIN32_FIND_DATAA find_data;
|
|
|
|
HANDLE dir_handle = ::FindFirstFileA(find_pattern.c_str(), &find_data);
|
|
|
|
if (dir_handle == INVALID_HANDLE_VALUE) {
|
|
|
|
DWORD last_error = ::GetLastError();
|
|
|
|
if (last_error == ERROR_FILE_NOT_FOUND) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-06-12 14:05:14 -07:00
|
|
|
return WindowsError(directory_path, last_error);
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
char base_name[_MAX_FNAME];
|
|
|
|
char ext[_MAX_EXT];
|
|
|
|
|
|
|
|
if (!_splitpath_s(find_data.cFileName, nullptr, 0, nullptr, 0, base_name,
|
|
|
|
ARRAYSIZE(base_name), ext, ARRAYSIZE(ext))) {
|
|
|
|
result->emplace_back(std::string(base_name) + ext);
|
|
|
|
}
|
|
|
|
} while (::FindNextFileA(dir_handle, &find_data));
|
|
|
|
DWORD last_error = ::GetLastError();
|
|
|
|
::FindClose(dir_handle);
|
|
|
|
if (last_error != ERROR_NO_MORE_FILES) {
|
2019-06-12 14:05:14 -07:00
|
|
|
return WindowsError(directory_path, last_error);
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Status DeleteFile(const std::string& filename) override {
|
|
|
|
if (!::DeleteFileA(filename.c_str())) {
|
|
|
|
return WindowsError(filename, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Status CreateDir(const std::string& dirname) override {
|
|
|
|
if (!::CreateDirectoryA(dirname.c_str(), nullptr)) {
|
|
|
|
return WindowsError(dirname, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Status DeleteDir(const std::string& dirname) override {
|
|
|
|
if (!::RemoveDirectoryA(dirname.c_str())) {
|
|
|
|
return WindowsError(dirname, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Status GetFileSize(const std::string& filename, uint64_t* size) override {
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA file_attributes;
|
|
|
|
if (!::GetFileAttributesExA(filename.c_str(), GetFileExInfoStandard,
|
|
|
|
&file_attributes)) {
|
|
|
|
return WindowsError(filename, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
ULARGE_INTEGER file_size;
|
2019-06-12 14:05:14 -07:00
|
|
|
file_size.HighPart = file_attributes.nFileSizeHigh;
|
|
|
|
file_size.LowPart = file_attributes.nFileSizeLow;
|
2019-03-01 13:12:01 -08:00
|
|
|
*size = file_size.QuadPart;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Status RenameFile(const std::string& from, const std::string& to) override {
|
|
|
|
// Try a simple move first. It will only succeed when |to| doesn't already
|
|
|
|
// exist.
|
|
|
|
if (::MoveFileA(from.c_str(), to.c_str())) {
|
2019-03-01 13:12:01 -08:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
DWORD move_error = ::GetLastError();
|
|
|
|
|
|
|
|
// Try the full-blown replace if the move fails, as ReplaceFile will only
|
2019-06-12 14:05:14 -07:00
|
|
|
// succeed when |to| does exist. When writing to a network share, we may not
|
|
|
|
// be able to change the ACLs. Ignore ACL errors then
|
2019-03-01 13:12:01 -08:00
|
|
|
// (REPLACEFILE_IGNORE_MERGE_ERRORS).
|
2019-06-12 14:05:14 -07:00
|
|
|
if (::ReplaceFileA(to.c_str(), from.c_str(), /*lpBackupFileName=*/nullptr,
|
|
|
|
REPLACEFILE_IGNORE_MERGE_ERRORS,
|
|
|
|
/*lpExclude=*/nullptr, /*lpReserved=*/nullptr)) {
|
2019-03-01 13:12:01 -08:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
DWORD replace_error = ::GetLastError();
|
2019-06-12 14:05:14 -07:00
|
|
|
// In the case of FILE_ERROR_NOT_FOUND from ReplaceFile, it is likely that
|
|
|
|
// |to| does not exist. In this case, the more relevant error comes from the
|
|
|
|
// call to MoveFile.
|
2019-03-01 13:12:01 -08:00
|
|
|
if (replace_error == ERROR_FILE_NOT_FOUND ||
|
|
|
|
replace_error == ERROR_PATH_NOT_FOUND) {
|
2019-06-12 14:05:14 -07:00
|
|
|
return WindowsError(from, move_error);
|
2019-03-01 13:12:01 -08:00
|
|
|
} else {
|
2019-06-12 14:05:14 -07:00
|
|
|
return WindowsError(from, replace_error);
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Status LockFile(const std::string& filename, FileLock** lock) override {
|
2019-03-01 13:12:01 -08:00
|
|
|
*lock = nullptr;
|
|
|
|
Status result;
|
|
|
|
ScopedHandle handle = ::CreateFileA(
|
2019-06-12 14:05:14 -07:00
|
|
|
filename.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
|
2019-03-01 13:12:01 -08:00
|
|
|
/*lpSecurityAttributes=*/nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
|
|
|
|
nullptr);
|
|
|
|
if (!handle.is_valid()) {
|
2019-06-12 14:05:14 -07:00
|
|
|
result = WindowsError(filename, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
} else if (!LockOrUnlock(handle.get(), true)) {
|
2019-06-12 14:05:14 -07:00
|
|
|
result = WindowsError("lock " + filename, ::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
} else {
|
2019-06-12 14:05:14 -07:00
|
|
|
*lock = new WindowsFileLock(std::move(handle), filename);
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status UnlockFile(FileLock* lock) override {
|
2019-06-12 14:05:14 -07:00
|
|
|
WindowsFileLock* windows_file_lock =
|
|
|
|
reinterpret_cast<WindowsFileLock*>(lock);
|
|
|
|
if (!LockOrUnlock(windows_file_lock->handle().get(), false)) {
|
|
|
|
return WindowsError("unlock " + windows_file_lock->filename(),
|
|
|
|
::GetLastError());
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
2019-06-12 14:05:14 -07:00
|
|
|
delete windows_file_lock;
|
|
|
|
return Status::OK();
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
void Schedule(void (*background_work_function)(void* background_work_arg),
|
|
|
|
void* background_work_arg) override;
|
2019-03-01 13:12:01 -08:00
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
void StartThread(void (*thread_main)(void* thread_main_arg),
|
|
|
|
void* thread_main_arg) override {
|
|
|
|
std::thread new_thread(thread_main, thread_main_arg);
|
|
|
|
new_thread.detach();
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Status GetTestDirectory(std::string* result) override {
|
|
|
|
const char* env = getenv("TEST_TMPDIR");
|
|
|
|
if (env && env[0] != '\0') {
|
|
|
|
*result = env;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
char tmp_path[MAX_PATH];
|
|
|
|
if (!GetTempPathA(ARRAYSIZE(tmp_path), tmp_path)) {
|
|
|
|
return WindowsError("GetTempPath", ::GetLastError());
|
|
|
|
}
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << tmp_path << "leveldbtest-" << std::this_thread::get_id();
|
|
|
|
*result = ss.str();
|
|
|
|
|
|
|
|
// Directory may already exist
|
|
|
|
CreateDir(*result);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-03-07 08:52:24 -08:00
|
|
|
Status NewLogger(const std::string& filename, Logger** result) override {
|
|
|
|
std::FILE* fp = std::fopen(filename.c_str(), "w");
|
|
|
|
if (fp == nullptr) {
|
|
|
|
*result = nullptr;
|
2019-06-12 14:05:14 -07:00
|
|
|
return WindowsError(filename, ::GetLastError());
|
2019-03-07 08:52:24 -08:00
|
|
|
} else {
|
|
|
|
*result = new WindowsLogger(fp);
|
|
|
|
return Status::OK();
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t NowMicros() override {
|
|
|
|
// GetSystemTimeAsFileTime typically has a resolution of 10-20 msec.
|
|
|
|
// TODO(cmumford): Switch to GetSystemTimePreciseAsFileTime which is
|
|
|
|
// available in Windows 8 and later.
|
|
|
|
FILETIME ft;
|
|
|
|
::GetSystemTimeAsFileTime(&ft);
|
|
|
|
// Each tick represents a 100-nanosecond intervals since January 1, 1601
|
|
|
|
// (UTC).
|
|
|
|
uint64_t num_ticks =
|
|
|
|
(static_cast<uint64_t>(ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
|
|
|
|
return num_ticks / 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SleepForMicroseconds(int micros) override {
|
|
|
|
std::this_thread::sleep_for(std::chrono::microseconds(micros));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-06-12 14:05:14 -07:00
|
|
|
void BackgroundThreadMain();
|
|
|
|
|
|
|
|
static void BackgroundThreadEntryPoint(WindowsEnv* env) {
|
|
|
|
env->BackgroundThreadMain();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stores the work item data in a Schedule() call.
|
|
|
|
//
|
|
|
|
// Instances are constructed on the thread calling Schedule() and used on the
|
|
|
|
// background thread.
|
|
|
|
//
|
|
|
|
// This structure is thread-safe beacuse it is immutable.
|
|
|
|
struct BackgroundWorkItem {
|
|
|
|
explicit BackgroundWorkItem(void (*function)(void* arg), void* arg)
|
|
|
|
: function(function), arg(arg) {}
|
|
|
|
|
|
|
|
void (*const function)(void*);
|
|
|
|
void* const arg;
|
2019-03-01 13:12:01 -08:00
|
|
|
};
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
port::Mutex background_work_mutex_;
|
|
|
|
port::CondVar background_work_cv_ GUARDED_BY(background_work_mutex_);
|
|
|
|
bool started_background_thread_ GUARDED_BY(background_work_mutex_);
|
|
|
|
|
|
|
|
std::queue<BackgroundWorkItem> background_work_queue_
|
|
|
|
GUARDED_BY(background_work_mutex_);
|
2019-05-03 09:31:18 -07:00
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
Limiter mmap_limiter_; // Thread-safe.
|
2019-03-01 13:12:01 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Return the maximum number of concurrent mmaps.
|
2019-06-12 14:05:14 -07:00
|
|
|
int MaxMmaps() { return g_mmap_limit; }
|
2019-03-01 13:12:01 -08:00
|
|
|
|
|
|
|
WindowsEnv::WindowsEnv()
|
2019-06-12 14:05:14 -07:00
|
|
|
: background_work_cv_(&background_work_mutex_),
|
|
|
|
started_background_thread_(false),
|
|
|
|
mmap_limiter_(MaxMmaps()) {}
|
2019-03-01 13:12:01 -08:00
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
void WindowsEnv::Schedule(
|
|
|
|
void (*background_work_function)(void* background_work_arg),
|
|
|
|
void* background_work_arg) {
|
|
|
|
background_work_mutex_.Lock();
|
2019-03-01 13:12:01 -08:00
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
// Start the background thread, if we haven't done so already.
|
|
|
|
if (!started_background_thread_) {
|
|
|
|
started_background_thread_ = true;
|
|
|
|
std::thread background_thread(WindowsEnv::BackgroundThreadEntryPoint, this);
|
|
|
|
background_thread.detach();
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
// If the queue is empty, the background thread may be waiting for work.
|
|
|
|
if (background_work_queue_.empty()) {
|
|
|
|
background_work_cv_.Signal();
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
background_work_queue_.emplace(background_work_function, background_work_arg);
|
|
|
|
background_work_mutex_.Unlock();
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
void WindowsEnv::BackgroundThreadMain() {
|
2019-03-01 13:12:01 -08:00
|
|
|
while (true) {
|
2019-06-12 14:05:14 -07:00
|
|
|
background_work_mutex_.Lock();
|
2019-03-01 13:12:01 -08:00
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
// Wait until there is work to be done.
|
|
|
|
while (background_work_queue_.empty()) {
|
|
|
|
background_work_cv_.Wait();
|
|
|
|
}
|
2019-03-01 13:12:01 -08:00
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
assert(!background_work_queue_.empty());
|
|
|
|
auto background_work_function = background_work_queue_.front().function;
|
|
|
|
void* background_work_arg = background_work_queue_.front().arg;
|
|
|
|
background_work_queue_.pop();
|
|
|
|
|
|
|
|
background_work_mutex_.Unlock();
|
|
|
|
background_work_function(background_work_arg);
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
// Wraps an Env instance whose destructor is never created.
|
|
|
|
//
|
|
|
|
// Intended usage:
|
|
|
|
// using PlatformSingletonEnv = SingletonEnv<PlatformEnv>;
|
|
|
|
// void ConfigurePosixEnv(int param) {
|
|
|
|
// PlatformSingletonEnv::AssertEnvNotInitialized();
|
|
|
|
// // set global configuration flags.
|
|
|
|
// }
|
|
|
|
// Env* Env::Default() {
|
|
|
|
// static PlatformSingletonEnv default_env;
|
|
|
|
// return default_env.env();
|
|
|
|
// }
|
|
|
|
template <typename EnvType>
|
|
|
|
class SingletonEnv {
|
|
|
|
public:
|
|
|
|
SingletonEnv() {
|
|
|
|
#if !defined(NDEBUG)
|
|
|
|
env_initialized_.store(true, std::memory_order::memory_order_relaxed);
|
|
|
|
#endif // !defined(NDEBUG)
|
|
|
|
static_assert(sizeof(env_storage_) >= sizeof(EnvType),
|
|
|
|
"env_storage_ will not fit the Env");
|
|
|
|
static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType),
|
|
|
|
"env_storage_ does not meet the Env's alignment needs");
|
|
|
|
new (&env_storage_) EnvType();
|
|
|
|
}
|
|
|
|
~SingletonEnv() = default;
|
|
|
|
|
|
|
|
SingletonEnv(const SingletonEnv&) = delete;
|
|
|
|
SingletonEnv& operator=(const SingletonEnv&) = delete;
|
|
|
|
|
|
|
|
Env* env() { return reinterpret_cast<Env*>(&env_storage_); }
|
2019-03-01 13:12:01 -08:00
|
|
|
|
2019-06-12 14:05:14 -07:00
|
|
|
static void AssertEnvNotInitialized() {
|
|
|
|
#if !defined(NDEBUG)
|
|
|
|
assert(!env_initialized_.load(std::memory_order::memory_order_relaxed));
|
|
|
|
#endif // !defined(NDEBUG)
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typename std::aligned_storage<sizeof(EnvType), alignof(EnvType)>::type
|
|
|
|
env_storage_;
|
|
|
|
#if !defined(NDEBUG)
|
|
|
|
static std::atomic<bool> env_initialized_;
|
|
|
|
#endif // !defined(NDEBUG)
|
|
|
|
};
|
|
|
|
|
|
|
|
#if !defined(NDEBUG)
|
|
|
|
template <typename EnvType>
|
|
|
|
std::atomic<bool> SingletonEnv<EnvType>::env_initialized_;
|
|
|
|
#endif // !defined(NDEBUG)
|
|
|
|
|
|
|
|
using WindowsDefaultEnv = SingletonEnv<WindowsEnv>;
|
|
|
|
|
|
|
|
} // namespace
|
2019-03-01 13:12:01 -08:00
|
|
|
|
|
|
|
void EnvWindowsTestHelper::SetReadOnlyMMapLimit(int limit) {
|
2019-06-12 14:05:14 -07:00
|
|
|
WindowsDefaultEnv::AssertEnvNotInitialized();
|
2019-03-01 13:12:01 -08:00
|
|
|
g_mmap_limit = limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
Env* Env::Default() {
|
2019-06-12 14:05:14 -07:00
|
|
|
static WindowsDefaultEnv env_container;
|
|
|
|
return env_container.env();
|
2019-03-01 13:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace leveldb
|