2014-08-12 01:30:13 +02:00
|
|
|
#ifndef safe_ptr_hh_INCLUDED
|
|
|
|
#define safe_ptr_hh_INCLUDED
|
|
|
|
|
|
|
|
// #define SAFE_PTR_TRACK_CALLSTACKS
|
|
|
|
|
|
|
|
#include "assert.hh"
|
2015-02-23 21:39:56 +01:00
|
|
|
#include "ref_ptr.hh"
|
2015-05-27 14:48:45 +02:00
|
|
|
#include "backtrace.hh"
|
2015-02-23 21:39:56 +01:00
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
2014-08-12 01:30:13 +02:00
|
|
|
|
|
|
|
#ifdef SAFE_PTR_TRACK_CALLSTACKS
|
2015-01-12 14:58:41 +01:00
|
|
|
#include "vector.hh"
|
2014-08-12 01:30:13 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-02-19 14:58:25 +01:00
|
|
|
// *** SafePtr: objects that assert nobody references them when they die ***
|
2014-08-12 01:30:13 +02:00
|
|
|
|
|
|
|
class SafeCountable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
#ifdef KAK_DEBUG
|
|
|
|
SafeCountable() : m_count(0) {}
|
|
|
|
~SafeCountable()
|
|
|
|
{
|
|
|
|
kak_assert(m_count == 0);
|
|
|
|
#ifdef SAFE_PTR_TRACK_CALLSTACKS
|
|
|
|
kak_assert(m_callstacks.empty());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-02-23 21:39:56 +01:00
|
|
|
friend void inc_ref_count(const SafeCountable* sc, void* ptr)
|
2014-08-12 01:30:13 +02:00
|
|
|
{
|
2015-02-23 21:39:56 +01:00
|
|
|
++sc->m_count;
|
2014-08-12 01:30:13 +02:00
|
|
|
#ifdef SAFE_PTR_TRACK_CALLSTACKS
|
2015-02-23 21:39:56 +01:00
|
|
|
sc->m_callstacks.emplace_back(ptr);
|
2014-08-12 01:30:13 +02:00
|
|
|
#endif
|
|
|
|
}
|
2015-02-23 21:39:56 +01:00
|
|
|
|
|
|
|
friend void dec_ref_count(const SafeCountable* sc, void* ptr)
|
2014-08-12 01:30:13 +02:00
|
|
|
{
|
2015-02-23 21:39:56 +01:00
|
|
|
--sc->m_count;
|
|
|
|
kak_assert(sc->m_count >= 0);
|
2014-08-12 01:30:13 +02:00
|
|
|
#ifdef SAFE_PTR_TRACK_CALLSTACKS
|
2015-02-23 21:39:56 +01:00
|
|
|
auto it = std::find_if(sc->m_callstacks.begin(), sc->m_callstacks.end(),
|
2014-08-12 01:30:13 +02:00
|
|
|
[=](const Callstack& cs) { return cs.ptr == ptr; });
|
2015-02-23 21:39:56 +01:00
|
|
|
kak_assert(it != sc->m_callstacks.end());
|
|
|
|
sc->m_callstacks.erase(it);
|
2014-08-12 01:30:13 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-02-23 21:39:56 +01:00
|
|
|
friend void ref_ptr_moved(const SafeCountable* sc, void* from, void* to)
|
2014-08-12 01:30:13 +02:00
|
|
|
{
|
|
|
|
#ifdef SAFE_PTR_TRACK_CALLSTACKS
|
2015-02-23 21:39:56 +01:00
|
|
|
auto it = std::find_if(sc->m_callstacks.begin(), sc->m_callstacks.end(),
|
2014-08-12 01:30:13 +02:00
|
|
|
[=](const Callstack& cs) { return cs.ptr == from; });
|
2015-02-23 21:39:56 +01:00
|
|
|
kak_assert(it != sc->m_callstacks.end());
|
2014-08-12 01:30:13 +02:00
|
|
|
it->ptr = to;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
#ifdef SAFE_PTR_TRACK_CALLSTACKS
|
|
|
|
struct Callstack
|
|
|
|
{
|
|
|
|
Callstack(void* p) : ptr(p) {}
|
|
|
|
void* ptr;
|
|
|
|
Backtrace bt;
|
|
|
|
};
|
|
|
|
|
2015-01-12 14:58:41 +01:00
|
|
|
mutable Vector<Callstack> m_callstacks;
|
2014-08-12 01:30:13 +02:00
|
|
|
#endif
|
|
|
|
mutable int m_count;
|
2015-02-23 21:39:56 +01:00
|
|
|
#else
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
friend void inc_ref_count(const SafeCountable* sc, void* ptr) {}
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
friend void dec_ref_count(const SafeCountable* sc, void* ptr) {}
|
2014-08-12 01:30:13 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2015-02-23 21:39:56 +01:00
|
|
|
template<typename T> using SafePtr =
|
2015-05-26 19:42:09 +02:00
|
|
|
RefPtr<T, typename std::conditional<std::is_const<T>::value,
|
|
|
|
const SafeCountable, SafeCountable>::type>;
|
2015-02-23 21:39:56 +01:00
|
|
|
|
2014-08-12 01:30:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // safe_ptr_hh_INCLUDED
|