From 97f5b20c12ca6ccf89d7720a5d41eaf4cda1b695 Mon Sep 17 00:00:00 2001 From: stickies-v Date: Wed, 31 Aug 2022 16:30:35 +0100 Subject: [PATCH 1/3] refactor: use std::string for thread names --- src/qt/guiutil.cpp | 2 +- src/util/system.cpp | 4 ++-- src/util/system.h | 2 +- src/util/thread.cpp | 6 ++++-- src/util/thread.h | 3 ++- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp index 5cc21dd40b1..b9f0be41e30 100644 --- a/src/qt/guiutil.cpp +++ b/src/qt/guiutil.cpp @@ -982,7 +982,7 @@ void PrintSlotException( std::string description = sender->metaObject()->className(); description += "->"; description += receiver->metaObject()->className(); - PrintExceptionContinue(exception, description.c_str()); + PrintExceptionContinue(exception, description); } void ShowModalDialogAsynchronously(QDialog* dialog) diff --git a/src/util/system.cpp b/src/util/system.cpp index 1953a9f8369..8864ae73c49 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -831,7 +831,7 @@ std::string HelpMessageOpt(const std::string &option, const std::string &message std::string("\n\n"); } -static std::string FormatException(const std::exception* pex, const char* pszThread) +static std::string FormatException(const std::exception* pex, std::string_view pszThread) { #ifdef WIN32 char pszModule[MAX_PATH] = ""; @@ -847,7 +847,7 @@ static std::string FormatException(const std::exception* pex, const char* pszThr "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread); } -void PrintExceptionContinue(const std::exception* pex, const char* pszThread) +void PrintExceptionContinue(const std::exception* pex, std::string_view pszThread) { std::string message = FormatException(pex, pszThread); LogPrintf("\n\n************************\n%s\n", message); diff --git a/src/util/system.h b/src/util/system.h index 756e6642f24..5f80ca51fcf 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -51,7 +51,7 @@ bool error(const char* fmt, const Args&... args) return false; } -void PrintExceptionContinue(const std::exception *pex, const char* pszThread); +void PrintExceptionContinue(const std::exception* pex, std::string_view pszThread); /** * Ensure file contents are fully committed to disk, using a platform-specific diff --git a/src/util/thread.cpp b/src/util/thread.cpp index f9f427ba202..ae98abdb3d7 100644 --- a/src/util/thread.cpp +++ b/src/util/thread.cpp @@ -10,10 +10,12 @@ #include #include +#include +#include -void util::TraceThread(const char* thread_name, std::function thread_func) +void util::TraceThread(std::string_view thread_name, std::function thread_func) { - util::ThreadRename(thread_name); + util::ThreadRename(std::string{thread_name}); try { LogPrintf("%s thread start\n", thread_name); thread_func(); diff --git a/src/util/thread.h b/src/util/thread.h index ca2eccc0c31..b80bf046a05 100644 --- a/src/util/thread.h +++ b/src/util/thread.h @@ -6,12 +6,13 @@ #define BITCOIN_UTIL_THREAD_H #include +#include namespace util { /** * A wrapper for do-something-once thread functions. */ -void TraceThread(const char* thread_name, std::function thread_func); +void TraceThread(std::string_view thread_name, std::function thread_func); } // namespace util From 200d84d5681918523d982b9ddf60d1127edcb448 Mon Sep 17 00:00:00 2001 From: stickies-v Date: Wed, 31 Aug 2022 16:43:58 +0100 Subject: [PATCH 2/3] refactor: use std::string for index names --- src/index/base.cpp | 7 +++++-- src/index/base.h | 7 +++++-- src/index/blockfilterindex.cpp | 4 ++-- src/index/blockfilterindex.h | 3 --- src/index/coinstatsindex.cpp | 2 +- src/index/coinstatsindex.h | 3 --- src/index/txindex.cpp | 2 +- src/index/txindex.h | 2 -- 8 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/index/base.cpp b/src/index/base.cpp index 1ebe89ef7cb..88c2ce98faa 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -18,6 +18,9 @@ #include // For g_chainman #include +#include +#include + using node::ReadBlockFromDisk; constexpr uint8_t DB_BEST_BLOCK{'B'}; @@ -62,8 +65,8 @@ void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator batch.Write(DB_BEST_BLOCK, locator); } -BaseIndex::BaseIndex(std::unique_ptr chain) - : m_chain{std::move(chain)} {} +BaseIndex::BaseIndex(std::unique_ptr chain, std::string name) + : m_chain{std::move(chain)}, m_name{std::move(name)} {} BaseIndex::~BaseIndex() { diff --git a/src/index/base.h b/src/index/base.h index 14693bc6feb..349178a535f 100644 --- a/src/index/base.h +++ b/src/index/base.h @@ -10,6 +10,8 @@ #include #include +#include + class CBlock; class CBlockIndex; class Chainstate; @@ -95,6 +97,7 @@ private: protected: std::unique_ptr m_chain; Chainstate* m_chainstate{nullptr}; + const std::string m_name; void BlockConnected(const std::shared_ptr& block, const CBlockIndex* pindex) override; @@ -117,13 +120,13 @@ protected: virtual DB& GetDB() const = 0; /// Get the name of the index for display in logs. - virtual const char* GetName() const = 0; + const std::string& GetName() const LIFETIMEBOUND { return m_name; } /// Update the internal best block index as well as the prune lock. void SetBestBlockIndex(const CBlockIndex* block); public: - BaseIndex(std::unique_ptr chain); + BaseIndex(std::unique_ptr chain, std::string name); /// Destructor interrupts sync thread if running and blocks until it exits. virtual ~BaseIndex(); diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp index f4837f34563..292e11c8747 100644 --- a/src/index/blockfilterindex.cpp +++ b/src/index/blockfilterindex.cpp @@ -97,7 +97,8 @@ static std::map g_filter_indexes; BlockFilterIndex::BlockFilterIndex(std::unique_ptr chain, BlockFilterType filter_type, size_t n_cache_size, bool f_memory, bool f_wipe) - : BaseIndex(std::move(chain)), m_filter_type(filter_type) + : BaseIndex(std::move(chain), BlockFilterTypeName(filter_type) + " block filter index") + , m_filter_type(filter_type) { const std::string& filter_name = BlockFilterTypeName(filter_type); if (filter_name.empty()) throw std::invalid_argument("unknown filter_type"); @@ -105,7 +106,6 @@ BlockFilterIndex::BlockFilterIndex(std::unique_ptr chain, Blo fs::path path = gArgs.GetDataDirNet() / "indexes" / "blockfilter" / fs::u8path(filter_name); fs::create_directories(path); - m_name = filter_name + " block filter index"; m_db = std::make_unique(path / "db", n_cache_size, f_memory, f_wipe); m_filter_fileseq = std::make_unique(std::move(path), "fltr", FLTR_FILE_CHUNK_SIZE); } diff --git a/src/index/blockfilterindex.h b/src/index/blockfilterindex.h index a31f7e460ec..5af46710910 100644 --- a/src/index/blockfilterindex.h +++ b/src/index/blockfilterindex.h @@ -26,7 +26,6 @@ class BlockFilterIndex final : public BaseIndex { private: BlockFilterType m_filter_type; - std::string m_name; std::unique_ptr m_db; FlatFilePos m_next_filter_pos; @@ -52,8 +51,6 @@ protected: BaseIndex::DB& GetDB() const LIFETIMEBOUND override { return *m_db; } - const char* GetName() const LIFETIMEBOUND override { return m_name.c_str(); } - public: /** Constructs the index, which becomes available to be queried. */ explicit BlockFilterIndex(std::unique_ptr chain, BlockFilterType filter_type, diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp index 99a1310c9ec..d3559b1b75e 100644 --- a/src/index/coinstatsindex.cpp +++ b/src/index/coinstatsindex.cpp @@ -105,7 +105,7 @@ struct DBHashKey { std::unique_ptr g_coin_stats_index; CoinStatsIndex::CoinStatsIndex(std::unique_ptr chain, size_t n_cache_size, bool f_memory, bool f_wipe) - : BaseIndex(std::move(chain)) + : BaseIndex(std::move(chain), "coinstatsindex") { fs::path path{gArgs.GetDataDirNet() / "indexes" / "coinstats"}; fs::create_directories(path); diff --git a/src/index/coinstatsindex.h b/src/index/coinstatsindex.h index 7375a85750d..fa59cb1ab18 100644 --- a/src/index/coinstatsindex.h +++ b/src/index/coinstatsindex.h @@ -20,7 +20,6 @@ struct CCoinsStats; class CoinStatsIndex final : public BaseIndex { private: - std::string m_name; std::unique_ptr m_db; MuHash3072 m_muhash; @@ -52,8 +51,6 @@ protected: BaseIndex::DB& GetDB() const override { return *m_db; } - const char* GetName() const override { return "coinstatsindex"; } - public: // Constructs the index, which becomes available to be queried. explicit CoinStatsIndex(std::unique_ptr chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false); diff --git a/src/index/txindex.cpp b/src/index/txindex.cpp index b719aface8e..a4fe1b611e0 100644 --- a/src/index/txindex.cpp +++ b/src/index/txindex.cpp @@ -49,7 +49,7 @@ bool TxIndex::DB::WriteTxs(const std::vector>& v_ } TxIndex::TxIndex(std::unique_ptr chain, size_t n_cache_size, bool f_memory, bool f_wipe) - : BaseIndex(std::move(chain)), m_db(std::make_unique(n_cache_size, f_memory, f_wipe)) + : BaseIndex(std::move(chain), "txindex"), m_db(std::make_unique(n_cache_size, f_memory, f_wipe)) {} TxIndex::~TxIndex() = default; diff --git a/src/index/txindex.h b/src/index/txindex.h index be240c4582d..8c1aa000339 100644 --- a/src/index/txindex.h +++ b/src/index/txindex.h @@ -27,8 +27,6 @@ protected: BaseIndex::DB& GetDB() const override; - const char* GetName() const override { return "txindex"; } - public: /// Constructs the index, which becomes available to be queried. explicit TxIndex(std::unique_ptr chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false); From 26cf9ea8e44d7fd6450336f567afaedd1275baf7 Mon Sep 17 00:00:00 2001 From: stickies-v Date: Wed, 14 Sep 2022 11:00:14 +0100 Subject: [PATCH 3/3] scripted-diff: rename pszThread to thread_name Since it is now a string_view instead of a const char*, update the name to reflect that the variable is no longer a "Pointer to String, Zero-terminated" (psz). -BEGIN VERIFY SCRIPT- sed -i s/pszThread/thread_name/ $(git grep -l pszThread src) -END VERIFY SCRIPT- --- src/util/system.cpp | 10 +++++----- src/util/system.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/util/system.cpp b/src/util/system.cpp index 8864ae73c49..c3c6cbfef62 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -831,7 +831,7 @@ std::string HelpMessageOpt(const std::string &option, const std::string &message std::string("\n\n"); } -static std::string FormatException(const std::exception* pex, std::string_view pszThread) +static std::string FormatException(const std::exception* pex, std::string_view thread_name) { #ifdef WIN32 char pszModule[MAX_PATH] = ""; @@ -841,15 +841,15 @@ static std::string FormatException(const std::exception* pex, std::string_view p #endif if (pex) return strprintf( - "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread); + "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, thread_name); else return strprintf( - "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread); + "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, thread_name); } -void PrintExceptionContinue(const std::exception* pex, std::string_view pszThread) +void PrintExceptionContinue(const std::exception* pex, std::string_view thread_name) { - std::string message = FormatException(pex, pszThread); + std::string message = FormatException(pex, thread_name); LogPrintf("\n\n************************\n%s\n", message); tfm::format(std::cerr, "\n\n************************\n%s\n", message); } diff --git a/src/util/system.h b/src/util/system.h index 5f80ca51fcf..c8e1de6700e 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -51,7 +51,7 @@ bool error(const char* fmt, const Args&... args) return false; } -void PrintExceptionContinue(const std::exception* pex, std::string_view pszThread); +void PrintExceptionContinue(const std::exception* pex, std::string_view thread_name); /** * Ensure file contents are fully committed to disk, using a platform-specific