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
This commit is contained in:
Dmitri Smirnov 2018-07-19 15:07:22 -07:00 committed by Facebook Github Bot
parent f3801528c1
commit 78ab11cd71
5 changed files with 24 additions and 12 deletions

View File

@ -48,6 +48,7 @@ option(WITH_JEMALLOC "build with JeMalloc" OFF)
option(WITH_SNAPPY "build with SNAPPY" OFF) option(WITH_SNAPPY "build with SNAPPY" OFF)
option(WITH_LZ4 "build with lz4" OFF) option(WITH_LZ4 "build with lz4" OFF)
option(WITH_ZLIB "build with zlib" OFF) option(WITH_ZLIB "build with zlib" OFF)
option(WITH_ZSTD "build with zstd" OFF)
if(MSVC) if(MSVC)
# Defaults currently different for GFLAGS. # Defaults currently different for GFLAGS.
# We will address find_package work a little later # We will address find_package work a little later
@ -108,7 +109,6 @@ else()
list(APPEND THIRDPARTY_LIBS ${LZ4_LIBRARIES}) list(APPEND THIRDPARTY_LIBS ${LZ4_LIBRARIES})
endif() endif()
option(WITH_ZSTD "build with zstd" OFF)
if(WITH_ZSTD) if(WITH_ZSTD)
find_package(zstd REQUIRED) find_package(zstd REQUIRED)
add_definitions(-DZSTD) add_definitions(-DZSTD)
@ -307,14 +307,6 @@ if(DISABLE_STALL_NOTIF)
add_definitions(-DROCKSDB_DISABLE_STALL_NOTIFICATION) add_definitions(-DROCKSDB_DISABLE_STALL_NOTIFICATION)
endif() 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(DEFINED USE_RTTI)
if(USE_RTTI) if(USE_RTTI)
@ -342,8 +334,10 @@ else()
endif() endif()
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(MSVC)
if((${OPTIMIZE_DEBUG} EQUAL 1)) if(OPTDBG)
message(STATUS "Debug optimization is enabled") message(STATUS "Debug optimization is enabled")
set(CMAKE_CXX_FLAGS_DEBUG "/Oxt /${RUNTIME_LIBRARY}d") set(CMAKE_CXX_FLAGS_DEBUG "/Oxt /${RUNTIME_LIBRARY}d")
else() else()

View File

@ -1822,7 +1822,7 @@ TEST_P(DBTestUniversalCompaction, FinalSortedRunCompactFilesConflict) {
"CompactFilesImpl:1"}}); "CompactFilesImpl:1"}});
rocksdb::SyncPoint::GetInstance()->EnableProcessing(); rocksdb::SyncPoint::GetInstance()->EnableProcessing();
std::thread compact_files_thread([&]() { port::Thread compact_files_thread([&]() {
ASSERT_OK(dbfull()->CompactFiles(CompactionOptions(), default_cfh, ASSERT_OK(dbfull()->CompactFiles(CompactionOptions(), default_cfh,
{first_sst_filename}, num_levels_ - 1)); {first_sst_filename}, num_levels_ - 1));
}); });

View File

@ -37,10 +37,14 @@ ZSTD_customMem GetJeZstdAllocationOverrides() {
// Global operators to be replaced by a linker when this file is // Global operators to be replaced by a linker when this file is
// a part of the build // a part of the build
namespace rocksdb {
namespace port {
void* jemalloc_aligned_alloc(size_t size, size_t alignment) ROCKSDB_NOEXCEPT { void* jemalloc_aligned_alloc(size_t size, size_t alignment) ROCKSDB_NOEXCEPT {
return je_aligned_alloc(alignment, size); return je_aligned_alloc(alignment, size);
} }
void jemalloc_aligned_free(void* p) ROCKSDB_NOEXCEPT { je_free(p); } void jemalloc_aligned_free(void* p) ROCKSDB_NOEXCEPT { je_free(p); }
} // port
} // rocksdb
void* operator new(size_t size) { void* operator new(size_t size) {
void* p = je_malloc(size); void* p = je_malloc(size);

View File

@ -9,15 +9,29 @@
#include "rocksdb/status.h" #include "rocksdb/status.h"
#include <stdio.h> #include <stdio.h>
#ifdef OS_WIN
#include <string.h>
#endif
#include <cstring> #include <cstring>
#include "port/port.h" #include "port/port.h"
namespace rocksdb { namespace rocksdb {
const char* Status::CopyState(const char* state) { 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 = const size_t cch =
std::strlen(state) + 1; // +1 for the null terminator std::strlen(state) + 1; // +1 for the null terminator
return std::strncpy(new char[cch], state, cch); return std::strncpy(new char[cch], state, cch);
#endif
} }
Status::Status(Code _code, SubCode _subcode, const Slice& msg, const Slice& msg2) Status::Status(Code _code, SubCode _subcode, const Slice& msg, const Slice& msg2)

View File

@ -185,7 +185,7 @@ void NTAPI WinOnThreadExit(PVOID module, DWORD reason, PVOID reserved) {
// We decided to punt on PROCESS_EXIT // We decided to punt on PROCESS_EXIT
if (DLL_THREAD_DETACH == reason) { if (DLL_THREAD_DETACH == reason) {
if (thread_local_key != pthread_key_t(-1) && thread_local_inclass_routine != nullptr) { 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) { if (tls != nullptr) {
thread_local_inclass_routine(tls); thread_local_inclass_routine(tls);
} }