Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Simplify xbt:Backtrace.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 7 Jan 2020 21:14:54 +0000 (22:14 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 7 Jan 2020 21:37:05 +0000 (22:37 +0100)
include/xbt/backtrace.hpp
src/xbt/backtrace.cpp

index 05bf810..d20fab8 100644 (file)
@@ -38,15 +38,10 @@ class BacktraceImpl;
  */
 class Backtrace {
 public:
-  BacktraceImpl* impl_ = nullptr;
+  std::shared_ptr<BacktraceImpl> 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;
 };
index 3e58b77..4e26ce0 100644 (file)
@@ -57,94 +57,32 @@ std::unique_ptr<char, std::function<void(char*)>> 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<BacktraceImpl>()) {}
 
-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