#pragma once #include "spinlock.h" #include namespace vlc { template class Refcounted { using RefcountType = uint32_t; volatile RefcountType m_refCount = 0; Spinlock m_lock; public: RefcountType incRef() { m_lock.lock(); auto result = m_refCount; m_refCount++; m_lock.unlock(); return result; } RefcountType decRef() { m_lock.lock(); assert(m_refCount > 0); auto result = m_refCount; m_refCount--; m_lock.unlock(); return result; } RefcountType refs() const { return m_refCount; } protected: Refcounted() = default; ~Refcounted() = default; explicit Refcounted(RefcountType initial_refs) : m_refCount(initial_refs) { } }; template class Refcounted { using RefcountType = uint32_t; RefcountType m_refCount = 0; public: RefcountType incRef() { auto result = m_refCount; m_refCount++; return result; } RefcountType decRef() { assert(m_refCount > 0); auto result = m_refCount; m_refCount--; return result; } RefcountType refs() const { return m_refCount; } protected: Refcounted() = default; ~Refcounted() = default; explicit Refcounted(RefcountType initial_refs) : m_refCount(initial_refs) { } }; } template void intrusiveIncRef(vlc::Refcounted* ptr) { if (ptr) { ptr->incRef(); } } template void intrusiveDecRef(vlc::Refcounted* ptr) { if (ptr && ptr->decRef() == 1) { delete static_cast(ptr); } }