From 78ab11cd7154c683f9a44d3524ee9c7acea4ade6 Mon Sep 17 00:00:00 2001 From: Dmitri Smirnov Date: Thu, 19 Jul 2018 15:07:22 -0700 Subject: [PATCH] Return new operator for Status allocations for Windows (#4128) Summary: Windows requires new/delete for memory allocations to be overriden. Refactor to be less intrusive. Differential Revision: D8878047 Pulled By: siying fbshipit-source-id: 35f2b5fec2f88ea48c9be926539c6469060aab36 --- CMakeLists.txt | 14 ++++---------- db/db_universal_compaction_test.cc | 2 +- port/win/win_jemalloc.cc | 4 ++++ util/status.cc | 14 ++++++++++++++ util/thread_local.cc | 2 +- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cd554aad4..a3540088d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ option(WITH_JEMALLOC "build with JeMalloc" OFF) option(WITH_SNAPPY "build with SNAPPY" OFF) option(WITH_LZ4 "build with lz4" OFF) option(WITH_ZLIB "build with zlib" OFF) +option(WITH_ZSTD "build with zstd" OFF) if(MSVC) # Defaults currently different for GFLAGS. # We will address find_package work a little later @@ -108,7 +109,6 @@ else() list(APPEND THIRDPARTY_LIBS ${LZ4_LIBRARIES}) endif() - option(WITH_ZSTD "build with zstd" OFF) if(WITH_ZSTD) find_package(zstd REQUIRED) add_definitions(-DZSTD) @@ -307,14 +307,6 @@ if(DISABLE_STALL_NOTIF) add_definitions(-DROCKSDB_DISABLE_STALL_NOTIFICATION) endif() -# Used to run CI build and tests so we can run faster -set(OPTIMIZE_DEBUG_DEFAULT 0) # Debug build is unoptimized by default use -DOPTDBG=1 to optimize - -if(DEFINED OPTDBG) - set(OPTIMIZE_DEBUG ${OPTDBG}) -else() - set(OPTIMIZE_DEBUG ${OPTIMIZE_DEBUG_DEFAULT}) -endif() if(DEFINED USE_RTTI) if(USE_RTTI) @@ -342,8 +334,10 @@ else() endif() endif() +# Used to run CI build and tests so we can run faster +option(OPTDBG "Build optimized debug build with MSVC" OFF) if(MSVC) - if((${OPTIMIZE_DEBUG} EQUAL 1)) + if(OPTDBG) message(STATUS "Debug optimization is enabled") set(CMAKE_CXX_FLAGS_DEBUG "/Oxt /${RUNTIME_LIBRARY}d") else() diff --git a/db/db_universal_compaction_test.cc b/db/db_universal_compaction_test.cc index 85a7da8fe..00303b056 100644 --- a/db/db_universal_compaction_test.cc +++ b/db/db_universal_compaction_test.cc @@ -1822,7 +1822,7 @@ TEST_P(DBTestUniversalCompaction, FinalSortedRunCompactFilesConflict) { "CompactFilesImpl:1"}}); rocksdb::SyncPoint::GetInstance()->EnableProcessing(); - std::thread compact_files_thread([&]() { + port::Thread compact_files_thread([&]() { ASSERT_OK(dbfull()->CompactFiles(CompactionOptions(), default_cfh, {first_sst_filename}, num_levels_ - 1)); }); diff --git a/port/win/win_jemalloc.cc b/port/win/win_jemalloc.cc index 6e5bfd427..fe57c631f 100644 --- a/port/win/win_jemalloc.cc +++ b/port/win/win_jemalloc.cc @@ -37,10 +37,14 @@ ZSTD_customMem GetJeZstdAllocationOverrides() { // Global operators to be replaced by a linker when this file is // a part of the build +namespace rocksdb { +namespace port { void* jemalloc_aligned_alloc(size_t size, size_t alignment) ROCKSDB_NOEXCEPT { return je_aligned_alloc(alignment, size); } void jemalloc_aligned_free(void* p) ROCKSDB_NOEXCEPT { je_free(p); } +} // port +} // rocksdb void* operator new(size_t size) { void* p = je_malloc(size); diff --git a/util/status.cc b/util/status.cc index f3f03d24e..8dc20f836 100644 --- a/util/status.cc +++ b/util/status.cc @@ -9,15 +9,29 @@ #include "rocksdb/status.h" #include +#ifdef OS_WIN +#include +#endif #include #include "port/port.h" namespace rocksdb { const char* Status::CopyState(const char* state) { +#ifdef OS_WIN + const size_t cch = + std::strlen(state) + 1; // +1 for the null terminator + char* result = new char[cch]; + errno_t ret; + ret = strncpy_s(result, cch, state, cch - 1); + result[cch - 1] = '\0'; + assert(ret == 0); + return result; +#else const size_t cch = std::strlen(state) + 1; // +1 for the null terminator return std::strncpy(new char[cch], state, cch); +#endif } Status::Status(Code _code, SubCode _subcode, const Slice& msg, const Slice& msg2) diff --git a/util/thread_local.cc b/util/thread_local.cc index 5972defce..9b901f6b1 100644 --- a/util/thread_local.cc +++ b/util/thread_local.cc @@ -185,7 +185,7 @@ void NTAPI WinOnThreadExit(PVOID module, DWORD reason, PVOID reserved) { // We decided to punt on PROCESS_EXIT if (DLL_THREAD_DETACH == reason) { if (thread_local_key != pthread_key_t(-1) && thread_local_inclass_routine != nullptr) { - void* tls = pthread_getspecific(thread_local_key); + void* tls = TlsGetValue(thread_local_key); if (tls != nullptr) { thread_local_inclass_routine(tls); }