From 7245d2abe907b11bfa67c4ea0059ab00b34b5bc9 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 27 May 2015 13:48:45 +0100 Subject: [PATCH] Extract Backtrace out of SafePtr as a general utility --- src/{safe_ptr.cc => backtrace.cc} | 18 +++++++----------- src/backtrace.hh | 20 ++++++++++++++++++++ src/safe_ptr.hh | 11 +---------- 3 files changed, 28 insertions(+), 21 deletions(-) rename src/{safe_ptr.cc => backtrace.cc} (80%) create mode 100644 src/backtrace.hh diff --git a/src/safe_ptr.cc b/src/backtrace.cc similarity index 80% rename from src/safe_ptr.cc rename to src/backtrace.cc index c4d5f485c..f7be9f769 100644 --- a/src/safe_ptr.cc +++ b/src/backtrace.cc @@ -1,32 +1,29 @@ -#include "safe_ptr.hh" - -#ifdef SAFE_PTR_TRACK_CALLSTACKS +#include "backtrace.hh" #include +#include -#if defined(__linux__) +#if defined(__linux__) || defined(__APPLE__) # include #elif defined(__CYGWIN__) # include #endif - namespace Kakoune { - -SafeCountable::Backtrace::Backtrace() +Backtrace::Backtrace() { - #if defined(__linux__) + #if defined(__linux__) || defined(__APPLE__) num_frames = backtrace(stackframes, max_frames); #elif defined(__CYGWIN__) num_frames = CaptureStackBackTrace(0, max_frames, stackframes, nullptr); #endif } -const char* SafeCountable::Backtrace::desc() const +const char* Backtrace::desc() const { - #if defined(__linux__) + #if defined(__linux__) || defined(__APPLE__) char** symbols = backtrace_symbols(stackframes, num_frames); int size = 0; for (int i = 0; i < num_frames; ++i) @@ -57,4 +54,3 @@ const char* SafeCountable::Backtrace::desc() const } } -#endif diff --git a/src/backtrace.hh b/src/backtrace.hh new file mode 100644 index 000000000..5eca8827a --- /dev/null +++ b/src/backtrace.hh @@ -0,0 +1,20 @@ +#ifndef backtrace_hh_INCLUDED +#define backtrace_hh_INCLUDED + +namespace Kakoune +{ + +struct Backtrace +{ + static constexpr int max_frames = 16; + void* stackframes[max_frames]; + int num_frames = 0; + + Backtrace(); + const char* desc() const; +}; + +} + +#endif // backtrace_hh_INCLUDED + diff --git a/src/safe_ptr.hh b/src/safe_ptr.hh index 2e3b3d2b6..a5e11ab65 100644 --- a/src/safe_ptr.hh +++ b/src/safe_ptr.hh @@ -5,6 +5,7 @@ #include "assert.hh" #include "ref_ptr.hh" +#include "backtrace.hh" #include #include @@ -64,16 +65,6 @@ public: private: #ifdef SAFE_PTR_TRACK_CALLSTACKS - struct Backtrace - { - static constexpr int max_frames = 16; - void* stackframes[max_frames]; - int num_frames = 0; - - Backtrace(); - const char* desc() const; - }; - struct Callstack { Callstack(void* p) : ptr(p) {}