1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:47:35 +00:00

AK: Use unsigned int for refcount

And while fixing all the tests that look at ref_count(),
sneak in a fix for the test suite name.
This commit is contained in:
Sergey Bugaev 2020-06-12 16:33:38 +03:00 committed by Andreas Kling
parent 0466810638
commit 583108004c
3 changed files with 29 additions and 27 deletions

View file

@ -58,13 +58,15 @@ constexpr auto call_one_ref_left_if_present(...) -> FalseType
class RefCountedBase { class RefCountedBase {
public: public:
typedef unsigned int RefCountType;
ALWAYS_INLINE void ref() const ALWAYS_INLINE void ref() const
{ {
ASSERT(m_ref_count); ASSERT(m_ref_count);
++m_ref_count; ++m_ref_count;
} }
ALWAYS_INLINE int ref_count() const ALWAYS_INLINE RefCountType ref_count() const
{ {
return m_ref_count; return m_ref_count;
} }
@ -73,7 +75,7 @@ protected:
RefCountedBase() { } RefCountedBase() { }
ALWAYS_INLINE ~RefCountedBase() ALWAYS_INLINE ~RefCountedBase()
{ {
ASSERT(!m_ref_count); ASSERT(m_ref_count == 0);
} }
ALWAYS_INLINE void deref_base() const ALWAYS_INLINE void deref_base() const
@ -82,7 +84,7 @@ protected:
--m_ref_count; --m_ref_count;
} }
mutable int m_ref_count { 1 }; mutable RefCountType m_ref_count { 1 };
}; };
template<typename T> template<typename T>

View file

@ -37,26 +37,26 @@ TEST_CASE(basics)
{ {
auto object = adopt(*new Object); auto object = adopt(*new Object);
EXPECT(object.ptr() != nullptr); EXPECT(object.ptr() != nullptr);
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
object->ref(); object->ref();
EXPECT_EQ(object->ref_count(), 2); EXPECT_EQ(object->ref_count(), 2u);
object->unref(); object->unref();
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
{ {
NonnullRefPtr another = object; NonnullRefPtr another = object;
EXPECT_EQ(object->ref_count(), 2); EXPECT_EQ(object->ref_count(), 2u);
} }
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
} }
TEST_CASE(assign_reference) TEST_CASE(assign_reference)
{ {
auto object = adopt(*new Object); auto object = adopt(*new Object);
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
object = *object; object = *object;
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
} }
TEST_CASE(assign_owner_of_self) TEST_CASE(assign_owner_of_self)
@ -70,14 +70,14 @@ TEST_CASE(assign_owner_of_self)
child->parent = move(parent); child->parent = move(parent);
child = *child->parent; child = *child->parent;
EXPECT_EQ(child->ref_count(), 1); EXPECT_EQ(child->ref_count(), 1u);
} }
TEST_CASE(swap_with_self) TEST_CASE(swap_with_self)
{ {
auto object = adopt(*new Object); auto object = adopt(*new Object);
swap(object, object); swap(object, object);
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
} }
TEST_MAIN(String) TEST_MAIN(NonnullRefPtr)

View file

@ -37,48 +37,48 @@ TEST_CASE(basics)
{ {
RefPtr<Object> object = adopt(*new Object); RefPtr<Object> object = adopt(*new Object);
EXPECT(object.ptr() != nullptr); EXPECT(object.ptr() != nullptr);
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
object->ref(); object->ref();
EXPECT_EQ(object->ref_count(), 2); EXPECT_EQ(object->ref_count(), 2u);
object->unref(); object->unref();
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
{ {
NonnullRefPtr another = *object; NonnullRefPtr another = *object;
EXPECT_EQ(object->ref_count(), 2); EXPECT_EQ(object->ref_count(), 2u);
} }
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
} }
TEST_CASE(assign_reference) TEST_CASE(assign_reference)
{ {
RefPtr<Object> object = adopt(*new Object); RefPtr<Object> object = adopt(*new Object);
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
object = *object; object = *object;
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
} }
TEST_CASE(assign_ptr) TEST_CASE(assign_ptr)
{ {
RefPtr<Object> object = adopt(*new Object); RefPtr<Object> object = adopt(*new Object);
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
object = object.ptr(); object = object.ptr();
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
} }
TEST_CASE(assign_moved_self) TEST_CASE(assign_moved_self)
{ {
RefPtr<Object> object = adopt(*new Object); RefPtr<Object> object = adopt(*new Object);
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
object = move(object); object = move(object);
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
} }
TEST_CASE(assign_copy_self) TEST_CASE(assign_copy_self)
{ {
RefPtr<Object> object = adopt(*new Object); RefPtr<Object> object = adopt(*new Object);
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
#ifdef __clang__ #ifdef __clang__
#pragma clang diagnostic push #pragma clang diagnostic push
@ -89,7 +89,7 @@ TEST_CASE(assign_copy_self)
#pragma clang diagnostic pop #pragma clang diagnostic pop
#endif #endif
EXPECT_EQ(object->ref_count(), 1); EXPECT_EQ(object->ref_count(), 1u);
} }
TEST_MAIN(RefPtr) TEST_MAIN(RefPtr)