Refactor RefPtr handling to use a policy class

THe previous overload based system was pretty complex for no good
reason.
This commit is contained in:
Maxime Coste 2016-12-03 12:32:16 +00:00
parent 5ff8245cc8
commit 75986911f8
3 changed files with 66 additions and 67 deletions

View File

@ -6,12 +6,20 @@
namespace Kakoune namespace Kakoune
{ {
struct WorstMatch { [[gnu::always_inline]] WorstMatch(...) {} }; struct RefCountable
{
int refcount = 0;
virtual ~RefCountable() = default;
};
[[gnu::always_inline]] struct RefCountablePolicy
inline void ref_ptr_moved(WorstMatch, void*, void*) noexcept {} {
static void inc_ref(RefCountable* r, void*) { ++r->refcount; }
static void dec_ref(RefCountable* r, void*) { if (--r->refcount == 0) delete r; }
static void ptr_moved(RefCountable*, void*, void*) noexcept {}
};
template<typename T, typename TForOverload = T> template<typename T, typename Policy = RefCountablePolicy>
struct RefPtr struct RefPtr
{ {
RefPtr() = default; RefPtr() = default;
@ -83,40 +91,23 @@ private:
void acquire() void acquire()
{ {
if (m_ptr) if (m_ptr)
inc_ref_count(static_cast<TForOverload*>(m_ptr), this); Policy::inc_ref(m_ptr, this);
} }
[[gnu::always_inline]] [[gnu::always_inline]]
void release() void release()
{ {
if (m_ptr) if (m_ptr)
dec_ref_count(static_cast<TForOverload*>(m_ptr), this); Policy::dec_ref(m_ptr, this);
m_ptr = nullptr; m_ptr = nullptr;
} }
[[gnu::always_inline]] [[gnu::always_inline]]
void moved(void* from) void moved(void* from)
noexcept(noexcept(ref_ptr_moved(static_cast<TForOverload*>(nullptr), nullptr, nullptr))) noexcept(noexcept(Policy::ptr_moved(nullptr, nullptr, nullptr)))
{ {
if (m_ptr) if (m_ptr)
ref_ptr_moved(static_cast<TForOverload*>(m_ptr), from, this); Policy::ptr_moved(m_ptr, from, this);
}
};
struct RefCountable
{
int refcount = 0;
virtual ~RefCountable() = default;
friend void inc_ref_count(RefCountable* r, void*)
{
++r->refcount;
}
friend void dec_ref_count(RefCountable* r, void*)
{
if (--r->refcount == 0)
delete r;
} }
}; };

View File

@ -33,37 +33,8 @@ public:
#endif #endif
} }
friend void inc_ref_count(const SafeCountable* sc, void* ptr)
{
++sc->m_count;
#ifdef SAFE_PTR_TRACK_CALLSTACKS
sc->m_callstacks.emplace_back(ptr);
#endif
}
friend void dec_ref_count(const SafeCountable* sc, void* ptr)
{
--sc->m_count;
kak_assert(sc->m_count >= 0);
#ifdef SAFE_PTR_TRACK_CALLSTACKS
auto it = std::find_if(sc->m_callstacks.begin(), sc->m_callstacks.end(),
[=](const Callstack& cs) { return cs.ptr == ptr; });
kak_assert(it != sc->m_callstacks.end());
sc->m_callstacks.erase(it);
#endif
}
friend void ref_ptr_moved(const SafeCountable* sc, void* from, void* to)
{
#ifdef SAFE_PTR_TRACK_CALLSTACKS
auto it = std::find_if(sc->m_callstacks.begin(), sc->m_callstacks.end(),
[=](const Callstack& cs) { return cs.ptr == from; });
kak_assert(it != sc->m_callstacks.end());
it->ptr = to;
#endif
}
private: private:
friend struct SafeCountablePolicy;
#ifdef SAFE_PTR_TRACK_CALLSTACKS #ifdef SAFE_PTR_TRACK_CALLSTACKS
struct Callstack struct Callstack
{ {
@ -75,20 +46,50 @@ private:
mutable Vector<Callstack> m_callstacks; mutable Vector<Callstack> m_callstacks;
#endif #endif
mutable int m_count; mutable int m_count;
#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) {}
#endif #endif
}; };
template<typename T, typename U> struct SafeCountablePolicy
using PropagateConst = typename std::conditional<std::is_const<T>::value, const U, U>::type; {
#ifdef KAK_DEBUG
static void inc_ref(const SafeCountable* sc, void* ptr)
{
++sc->m_count;
#ifdef SAFE_PTR_TRACK_CALLSTACKS
sc->m_callstacks.emplace_back(ptr);
#endif
}
static void dec_ref(const SafeCountable* sc, void* ptr)
{
--sc->m_count;
kak_assert(sc->m_count >= 0);
#ifdef SAFE_PTR_TRACK_CALLSTACKS
auto it = std::find_if(sc->m_callstacks.begin(), sc->m_callstacks.end(),
[=](const Callstack& cs) { return cs.ptr == ptr; });
kak_assert(it != sc->m_callstacks.end());
sc->m_callstacks.erase(it);
#endif
}
static void ptr_moved(const SafeCountable* sc, void* from, void* to)
{
#ifdef SAFE_PTR_TRACK_CALLSTACKS
auto it = std::find_if(sc->m_callstacks.begin(), sc->m_callstacks.end(),
[=](const Callstack& cs) { return cs.ptr == from; });
kak_assert(it != sc->m_callstacks.end());
it->ptr = to;
#endif
}
#else
static void inc_ref(const SafeCountable*, void* ptr) {}
static void dec_ref(const SafeCountable*, void* ptr) {}
static void ptr_moved(const SafeCountable*, void*, void*) {}
#endif
};
template<typename T> template<typename T>
using SafePtr = RefPtr<T, PropagateConst<T, SafeCountable>>; using SafePtr = RefPtr<T, SafeCountablePolicy>;
} }

View File

@ -24,7 +24,14 @@ struct StringData : UseMemoryDomain<MemoryDomain::SharedString>
[[gnu::always_inline]] [[gnu::always_inline]]
StringView strview() const { return {data(), length}; } StringView strview() const { return {data(), length}; }
static RefPtr<StringData> create(StringView str, char back = 0) struct PtrPolicy
{
static void inc_ref(StringData* r, void*) { ++r->refcount; }
static void dec_ref(StringData* r, void*) { if (--r->refcount == 0) delete r; }
static void ptr_moved(StringData*, void*, void*) noexcept {}
};
static RefPtr<StringData, PtrPolicy> create(StringView str, char back = 0)
{ {
const int len = (int)str.length() + (back != 0 ? 1 : 0); const int len = (int)str.length() + (back != 0 ? 1 : 0);
void* ptr = StringData::operator new(sizeof(StringData) + len + 1); void* ptr = StringData::operator new(sizeof(StringData) + len + 1);
@ -34,7 +41,7 @@ struct StringData : UseMemoryDomain<MemoryDomain::SharedString>
res->data()[len-1] = back; res->data()[len-1] = back;
res->data()[len] = 0; res->data()[len] = 0;
res->hash = hash_data(res->data(), res->length); res->hash = hash_data(res->data(), res->length);
return RefPtr<StringData>(res); return RefPtr<StringData, PtrPolicy>{res};
} }
static void destroy(StringData* s) static void destroy(StringData* s)
@ -54,7 +61,7 @@ struct StringData : UseMemoryDomain<MemoryDomain::SharedString>
} }
}; };
using StringDataPtr = RefPtr<StringData>; using StringDataPtr = RefPtr<StringData, StringData::PtrPolicy>;
class StringRegistry : public Singleton<StringRegistry> class StringRegistry : public Singleton<StringRegistry>
{ {