From 34cc424dffb95939f16753a42462f10df5b96dbf Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Tue, 7 Jan 2020 22:14:54 +0100 Subject: [PATCH] Simplify xbt:Backtrace. --- include/xbt/backtrace.hpp | 9 +--- src/xbt/backtrace.cpp | 90 ++++++--------------------------------- 2 files changed, 16 insertions(+), 83 deletions(-) diff --git a/include/xbt/backtrace.hpp b/include/xbt/backtrace.hpp index 05bf810ba4..d20fab8ecd 100644 --- a/include/xbt/backtrace.hpp +++ b/include/xbt/backtrace.hpp @@ -38,15 +38,10 @@ class BacktraceImpl; */ class Backtrace { public: - BacktraceImpl* impl_ = nullptr; + std::shared_ptr impl_; Backtrace(); - Backtrace(const Backtrace& bt); - Backtrace(Backtrace&& bt); - Backtrace& operator=(const Backtrace& rhs); - Backtrace& operator=(Backtrace&& rhs); - ~Backtrace(); /** @brief Translate the backtrace in a human friendly form, unmangled with source code locations. */ - std::string const resolve() const; + std::string resolve() const; /** @brief Display the resolved backtrace on stderr */ void display() const; }; diff --git a/src/xbt/backtrace.cpp b/src/xbt/backtrace.cpp index 3e58b77351..4e26ce0c60 100644 --- a/src/xbt/backtrace.cpp +++ b/src/xbt/backtrace.cpp @@ -57,94 +57,32 @@ std::unique_ptr> demangle(const char* name) } class BacktraceImpl { - short refcount_ = 1; - +#if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE + const boost::stacktrace::stacktrace st = boost::stacktrace::stacktrace(); +#else + const char st[1] = ""; // fallback value +#endif public: - void ref() { refcount_++; } - bool unref() + std::string resolve() const { - refcount_--; - if (refcount_ == 0) { - delete this; - return true; - } else { - return false; - } + std::stringstream ss; + ss << st; + return ss.str(); } -#if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE - boost::stacktrace::stacktrace st; -#endif }; -Backtrace::Backtrace() -{ -#if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE - impl_ = new BacktraceImpl(); - impl_->st = boost::stacktrace::stacktrace(); -#endif -} - -Backtrace::Backtrace(const Backtrace& bt) : impl_(bt.impl_) -{ - if (impl_) - impl_->ref(); -} - -Backtrace::Backtrace(Backtrace&& bt) -{ - std::swap(impl_, bt.impl_); -} - -Backtrace& Backtrace::operator=(const Backtrace& rhs) -{ - if (this != &rhs) { - if (impl_) - impl_->unref(); - impl_ = rhs.impl_; - if (impl_) - impl_->ref(); - } - return *this; -} - -Backtrace& Backtrace::operator=(Backtrace&& rhs) -{ - if (this != &rhs) { - if (impl_) - impl_->unref(); - impl_ = rhs.impl_; - rhs.impl_ = nullptr; - } - return *this; -} +Backtrace::Backtrace() : impl_(std::make_shared()) {} -Backtrace::~Backtrace() +std::string Backtrace::resolve() const { - if (impl_) - impl_->unref(); -} - -std::string const Backtrace::resolve() const -{ - std::string result(""); - -#if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE - std::stringstream ss; - ss << impl_->st; - result.append(ss.str()); -#endif - return result; + return impl_->resolve(); } void Backtrace::display() const { std::string backtrace = resolve(); - if (backtrace.empty()) { - fprintf(stderr, "(backtrace not set -- did you install Boost.Stacktrace?)\n"); - return; - } - fprintf(stderr, "Backtrace (displayed in actor %s):\n", xbt_procname()); - std::fprintf(stderr, "%s\n", backtrace.c_str()); + fprintf(stderr, "Backtrace (displayed in actor %s):\n%s\n", xbt_procname(), + backtrace.empty() ? "(backtrace not set -- did you install Boost.Stacktrace?)" : backtrace.c_str()); } } // namespace xbt -- 2.20.1