serenity/Kernel/RefCounted.h
Andreas Kling 9396108034 Import the "gerbert" kernel I worked on earlier this year.
It's a lot crappier than I remembered it. It's gonna need a lot of work.
2018-10-16 11:02:00 +02:00

49 lines
820 B
C++

#pragma once
#include "Assertions.h"
#include "VGA.h"
#define DEBUG_REFCOUNTED
class RefCountedBase {
protected:
bool derefBase() const
{
return !--m_refCount;
}
mutable size_t m_refCount { 1 };
#ifdef DEBUG_REFCOUNTED
//mutable bool m_adopted { false };
#endif
};
template<typename T>
class RefCounted : public RefCountedBase {
public:
size_t refCount() const { return m_refCount; }
void ref() const
{
#ifdef DEBUG_REFCOUNTED
ASSERT(m_refCount);
//ASSERT(m_adopted);
#endif
++m_refCount;
}
void deref() const
{
#ifdef DEBUG_REFCOUNTED
ASSERT(m_refCount);
//ASSERT(m_adopted);
#endif
if (derefBase())
delete static_cast<const T*>(this);
}
protected:
RefCounted() { }
~RefCounted() { }
};