From 6a4aaadcd7dfc3aa65dbff183c0130cdd0bc1f8a Mon Sep 17 00:00:00 2001 From: sdong Date: Wed, 5 Aug 2015 20:51:27 -0700 Subject: [PATCH] Avoid type unique_ptr in LogWriterNumber::writer for Windows build break Summary: Visual Studio complains about deque because LogWriterNumber is non-copyable for its unique_ptr member writer. Move away from it, and do explit free. It is less safe but I can't think of a better way to unblock it. Test Plan: valgrind check test Reviewers: anthony, IslamAbdelRahman, kolmike, rven, yhchiang Reviewed By: yhchiang Subscribers: leveldb, dhruba Differential Revision: https://reviews.facebook.net/D43647 --- db/db_impl.cc | 17 +++++++++-------- db/db_impl.h | 20 +++++++++++++++++--- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 10f1d2675..7652a66d6 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -369,6 +369,9 @@ DBImpl::~DBImpl() { for (auto l : logs_to_free_) { delete l; } + for (auto& log : logs_) { + log.ClearWriter(); + } logs_.clear(); // versions need to be destroyed before table_cache since it can hold @@ -557,7 +560,7 @@ void DBImpl::FindObsoleteFiles(JobContext* job_context, bool force, // logs_ could have changed while we were waiting. continue; } - logs_to_free_.push_back(log.writer.release()); + logs_to_free_.push_back(log.ReleaseWriter()); logs_.pop_front(); } // Current log cannot be obsolete. @@ -1978,7 +1981,7 @@ Status DBImpl::SyncWAL() { auto& log = *it; assert(!log.getting_synced); log.getting_synced = true; - logs_to_sync.push_back(log.writer.get()); + logs_to_sync.push_back(log.writer); } need_log_dir_sync = !log_dir_synced_; @@ -2015,7 +2018,7 @@ void DBImpl::MarkLogsSynced( auto& log = *it; assert(log.getting_synced); if (status.ok() && logs_.size() > 1) { - logs_to_free_.push_back(log.writer.release()); + logs_to_free_.push_back(log.ReleaseWriter()); logs_.erase(it++); } else { log.getting_synced = false; @@ -3805,7 +3808,7 @@ Status DBImpl::SwitchMemtable(ColumnFamilyData* cfd, WriteContext* context) { assert(new_log != nullptr); log_empty_ = true; log_dir_synced_ = false; - logs_.emplace_back(logfile_number_, std::unique_ptr(new_log)); + logs_.emplace_back(logfile_number_, new_log); alive_log_files_.push_back(LogFileNumberSize(logfile_number_)); for (auto loop_cfd : *versions_->GetColumnFamilySet()) { // all this is just optimization to delete logs that @@ -4338,10 +4341,8 @@ Status DB::Open(const DBOptions& db_options, const std::string& dbname, impl->logfile_number_ = new_log_number; unique_ptr file_writer( new WritableFileWriter(std::move(lfile), opt_env_options)); - impl->logs_.emplace_back( - new_log_number, - std::unique_ptr( - new log::Writer(std::move(file_writer)))); + impl->logs_.emplace_back(new_log_number, + new log::Writer(std::move(file_writer))); // set column family handles for (auto cf : column_families) { diff --git a/db/db_impl.h b/db/db_impl.h index bdec031f7..3dd95f8b2 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -532,10 +532,24 @@ class DBImpl : public DB { bool getting_flushed = false; }; struct LogWriterNumber { - LogWriterNumber(uint64_t _number, std::unique_ptr _writer) - : number(_number), writer(std::move(_writer)) {} + // pass ownership of _writer + LogWriterNumber(uint64_t _number, log::Writer* _writer) + : number(_number), writer(_writer) {} + + log::Writer* ReleaseWriter() { + auto* w = writer; + writer = nullptr; + return w; + } + void ClearWriter() { + delete writer; + writer = nullptr; + } + uint64_t number; - std::unique_ptr writer; + // Visual Studio doesn't support deque's member to be noncopyable because + // of a unique_ptr as a member. + log::Writer* writer; // own // true for some prefix of logs_ bool getting_synced = false; };