Skip to content

Commit

Permalink
AK: Rename Retainable => RefCounted.
Browse files Browse the repository at this point in the history
(And various related renames that go along with it.)
  • Loading branch information
awesomekling committed Jun 21, 2019
1 parent ef1bfcb commit 77b9fa8
Show file tree
Hide file tree
Showing 45 changed files with 118 additions and 118 deletions.
2 changes: 1 addition & 1 deletion AK/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace AK {

class ByteBufferImpl : public Retainable<ByteBufferImpl> {
class ByteBufferImpl : public RefCounted<ByteBufferImpl> {
public:
static Retained<ByteBufferImpl> create_uninitialized(int size);
static Retained<ByteBufferImpl> create_zeroed(int);
Expand Down
6 changes: 3 additions & 3 deletions AK/JsonValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void JsonValue::copy_from(const JsonValue& other)
switch (m_type) {
case Type::String:
m_value.as_string = other.m_value.as_string;
AK::retain_if_not_null(m_value.as_string);
AK::ref_if_not_null(m_value.as_string);
break;
case Type::Object:
m_value.as_object = new JsonObject(*other.m_value.as_object);
Expand Down Expand Up @@ -101,7 +101,7 @@ JsonValue::JsonValue(const String& value)
} else {
m_type = Type::String;
m_value.as_string = const_cast<StringImpl*>(value.impl());
AK::retain_if_not_null(m_value.as_string);
AK::ref_if_not_null(m_value.as_string);
}
}

Expand All @@ -121,7 +121,7 @@ void JsonValue::clear()
{
switch (m_type) {
case Type::String:
AK::release_if_not_null(m_value.as_string);
AK::deref_if_not_null(m_value.as_string);
break;
case Type::Object:
delete m_value.as_object;
Expand Down
32 changes: 16 additions & 16 deletions AK/RetainPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ class RetainPtr {
RetainPtr(const T* ptr)
: m_ptr(const_cast<T*>(ptr))
{
retain_if_not_null(m_ptr);
ref_if_not_null(m_ptr);
}
RetainPtr(T* ptr)
: m_ptr(ptr)
{
retain_if_not_null(m_ptr);
ref_if_not_null(m_ptr);
}
RetainPtr(T& object)
: m_ptr(&object)
{
m_ptr->retain();
m_ptr->ref();
}
RetainPtr(const T& object)
: m_ptr(const_cast<T*>(&object))
{
m_ptr->retain();
m_ptr->ref();
}
RetainPtr(AdoptTag, T& object)
: m_ptr(&object)
Expand Down Expand Up @@ -79,7 +79,7 @@ class RetainPtr {
RetainPtr& operator=(RetainPtr&& other)
{
if (this != &other) {
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = other.leak_ref();
}
return *this;
Expand All @@ -89,7 +89,7 @@ class RetainPtr {
RetainPtr& operator=(RetainPtr<U>&& other)
{
if (this != static_cast<void*>(&other)) {
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = other.leak_ref();
}
return *this;
Expand All @@ -98,7 +98,7 @@ class RetainPtr {
template<typename U>
RetainPtr& operator=(Retained<U>&& other)
{
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = &other.leak_ref();
return *this;
}
Expand All @@ -107,38 +107,38 @@ class RetainPtr {
RetainPtr& operator=(const Retained<U>& other)
{
if (m_ptr != other.ptr())
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = const_cast<T*>(other.ptr());
ASSERT(m_ptr);
retain_if_not_null(m_ptr);
ref_if_not_null(m_ptr);
return *this;
}

template<typename U>
RetainPtr& operator=(const RetainPtr<U>& other)
{
if (m_ptr != other.ptr())
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = const_cast<T*>(other.ptr());
retain_if_not_null(m_ptr);
ref_if_not_null(m_ptr);
return *this;
}

RetainPtr& operator=(const T* ptr)
{
if (m_ptr != ptr)
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = const_cast<T*>(ptr);
retain_if_not_null(m_ptr);
ref_if_not_null(m_ptr);
return *this;
}

RetainPtr& operator=(const T& object)
{
if (m_ptr != &object)
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = const_cast<T*>(&object);
retain_if_not_null(m_ptr);
ref_if_not_null(m_ptr);
return *this;
}

Expand All @@ -155,7 +155,7 @@ class RetainPtr {

void clear()
{
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = nullptr;
}

Expand Down
46 changes: 23 additions & 23 deletions AK/Retainable.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,61 @@ constexpr auto call_will_be_destroyed_if_present(...) -> FalseType
}

template<class T>
constexpr auto call_one_retain_left_if_present(T* object) -> decltype(object->one_retain_left(), TrueType {})
constexpr auto call_one_ref_left_if_present(T* object) -> decltype(object->one_ref_left(), TrueType {})
{
object->one_retain_left();
object->one_ref_left();
return {};
}

constexpr auto call_one_retain_left_if_present(...) -> FalseType
constexpr auto call_one_ref_left_if_present(...) -> FalseType
{
return {};
}

class RetainableBase {
class RefCountedBase {
public:
void retain()
void ref()
{
ASSERT(m_retain_count);
++m_retain_count;
ASSERT(m_ref_count);
++m_ref_count;
}

int retain_count() const
int ref_count() const
{
return m_retain_count;
return m_ref_count;
}

protected:
RetainableBase() {}
~RetainableBase()
RefCountedBase() {}
~RefCountedBase()
{
ASSERT(!m_retain_count);
ASSERT(!m_ref_count);
}

void release_base()
void deref_base()
{
ASSERT(m_retain_count);
--m_retain_count;
ASSERT(m_ref_count);
--m_ref_count;
}

int m_retain_count { 1 };
int m_ref_count { 1 };
};

template<typename T>
class Retainable : public RetainableBase {
class RefCounted : public RefCountedBase {
public:
void release()
void deref()
{
release_base();
if (m_retain_count == 0) {
deref_base();
if (m_ref_count == 0) {
call_will_be_destroyed_if_present(static_cast<T*>(this));
delete static_cast<T*>(this);
} else if (m_retain_count == 1) {
call_one_retain_left_if_present(static_cast<T*>(this));
} else if (m_ref_count == 1) {
call_one_ref_left_if_present(static_cast<T*>(this));
}
}
};

}

using AK::Retainable;
using AK::RefCounted;
22 changes: 11 additions & 11 deletions AK/Retained.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
namespace AK {

template<typename T>
inline void retain_if_not_null(T* ptr)
inline void ref_if_not_null(T* ptr)
{
if (ptr)
ptr->retain();
ptr->ref();
}

template<typename T>
inline void release_if_not_null(T* ptr)
inline void deref_if_not_null(T* ptr)
{
if (ptr)
ptr->release();
ptr->deref();
}

template<typename T>
Expand All @@ -42,14 +42,14 @@ class CONSUMABLE(unconsumed) Retained {
Retained(const T& object)
: m_ptr(const_cast<T*>(&object))
{
m_ptr->retain();
m_ptr->ref();
}
template<typename U>
RETURN_TYPESTATE(unconsumed)
Retained(const U& object)
: m_ptr(&const_cast<T&>(static_cast<const T&>(object)))
{
m_ptr->retain();
m_ptr->ref();
}
RETURN_TYPESTATE(unconsumed)
Retained(AdoptTag, T& object)
Expand Down Expand Up @@ -85,7 +85,7 @@ class CONSUMABLE(unconsumed) Retained {
}
~Retained()
{
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = nullptr;
#ifdef SANITIZE_PTRS
if constexpr (sizeof(T*) == 8)
Expand All @@ -99,7 +99,7 @@ class CONSUMABLE(unconsumed) Retained {
Retained& operator=(Retained&& other)
{
if (this != &other) {
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = &other.leak_ref();
}
return *this;
Expand All @@ -110,7 +110,7 @@ class CONSUMABLE(unconsumed) Retained {
Retained& operator=(Retained<U>&& other)
{
if (this != static_cast<void*>(&other)) {
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = &other.leak_ref();
}
return *this;
Expand All @@ -120,9 +120,9 @@ class CONSUMABLE(unconsumed) Retained {
Retained& operator=(T& object)
{
if (m_ptr != &object)
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = &object;
m_ptr->retain();
m_ptr->ref();
return *this;
}

Expand Down
2 changes: 1 addition & 1 deletion AK/StringImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum ShouldChomp {
Chomp
};

class StringImpl : public Retainable<StringImpl> {
class StringImpl : public RefCounted<StringImpl> {
public:
static Retained<StringImpl> create_uninitialized(int length, char*& buffer);
static RetainPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);
Expand Down
2 changes: 1 addition & 1 deletion AK/Weakable.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ template<typename T>
class WeakPtr;

template<typename T>
class WeakLink : public Retainable<WeakLink<T>> {
class WeakLink : public RefCounted<WeakLink<T>> {
friend class Weakable<T>;

public:
Expand Down
2 changes: 1 addition & 1 deletion Applications/IRCClient/IRCChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class IRCClient;
class IRCChannelMemberListModel;
class IRCWindow;

class IRCChannel : public Retainable<IRCChannel> {
class IRCChannel : public RefCounted<IRCChannel> {
public:
static Retained<IRCChannel> create(IRCClient&, const String&);
~IRCChannel();
Expand Down
2 changes: 1 addition & 1 deletion Applications/IRCClient/IRCLogBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class IRCLogBufferModel;

class IRCLogBuffer : public Retainable<IRCLogBuffer> {
class IRCLogBuffer : public RefCounted<IRCLogBuffer> {
public:
static Retained<IRCLogBuffer> create();
~IRCLogBuffer();
Expand Down
2 changes: 1 addition & 1 deletion Applications/IRCClient/IRCQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class IRCClient;
class IRCWindow;

class IRCQuery : public Retainable<IRCQuery> {
class IRCQuery : public RefCounted<IRCQuery> {
public:
static Retained<IRCQuery> create(IRCClient&, const String& name);
~IRCQuery();
Expand Down
2 changes: 1 addition & 1 deletion DevTools/VisualBuilder/VBWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ inline void for_each_direction(Callback callback)
callback(Direction::DownLeft);
}

class VBWidget : public Retainable<VBWidget>
class VBWidget : public RefCounted<VBWidget>
, public Weakable<VBWidget> {
friend class VBWidgetPropertyModel;

Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/DiskDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// FIXME: Support 64-bit DiskOffset
typedef dword DiskOffset;

class DiskDevice : public Retainable<DiskDevice> {
class DiskDevice : public RefCounted<DiskDevice> {
public:
virtual ~DiskDevice();

Expand Down
2 changes: 1 addition & 1 deletion Kernel/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Region;
// - Called by mmap() when userspace wants to memory-map this File somewhere.
// - Should create a Region in the Process and return it if successful.

class File : public Retainable<File> {
class File : public RefCounted<File> {
public:
virtual ~File();

Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/Custody.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class VFS;

// FIXME: Custody needs some locking.

class Custody : public Retainable<Custody> {
class Custody : public RefCounted<Custody> {
public:
static Custody* get_if_cached(Custody* parent, const String& name);
static Retained<Custody> get_or_create(Custody* parent, const String& name, Inode&);
Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/Ext2FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ InodeIdentifier Ext2FSInode::lookup(StringView name)
return {};
}

void Ext2FSInode::one_retain_left()
void Ext2FSInode::one_ref_left()
{
// FIXME: I would like to not live forever, but uncached Ext2FS is fucking painful right now.
}
Expand Down
Loading

0 comments on commit 77b9fa8

Please sign in to comment.