2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2022-08-19 18:26:07 +03:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Checked.h>
|
|
|
|
#include <AK/Noncopyable.h>
|
|
|
|
#include <AK/Platform.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-06-21 16:29:31 +03:00
|
|
|
class RefCountedBase {
|
2020-08-26 22:52:24 +03:00
|
|
|
AK_MAKE_NONCOPYABLE(RefCountedBase);
|
|
|
|
AK_MAKE_NONMOVABLE(RefCountedBase);
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
public:
|
2020-11-12 01:21:01 +03:00
|
|
|
using RefCountType = unsigned int;
|
2020-11-03 17:51:56 +03:00
|
|
|
using AllowOwnPtr = FalseType;
|
2020-06-12 16:33:38 +03:00
|
|
|
|
2021-10-08 23:11:39 +03:00
|
|
|
ALWAYS_INLINE void ref() const
|
2021-06-03 02:12:09 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
VERIFY(m_ref_count > 0);
|
|
|
|
VERIFY(!Checked<RefCountType>::addition_would_overflow(m_ref_count, 1));
|
|
|
|
++m_ref_count;
|
2021-06-03 02:12:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool try_ref() const
|
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
if (m_ref_count == 0)
|
|
|
|
return false;
|
|
|
|
ref();
|
|
|
|
return true;
|
2021-06-03 02:12:09 +03:00
|
|
|
}
|
|
|
|
|
2021-10-07 20:12:37 +03:00
|
|
|
[[nodiscard]] RefCountType ref_count() const { return m_ref_count; }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
protected:
|
2021-01-11 02:29:28 +03:00
|
|
|
RefCountedBase() = default;
|
2021-10-07 20:12:37 +03:00
|
|
|
~RefCountedBase() { VERIFY(!m_ref_count); }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2021-10-08 23:11:39 +03:00
|
|
|
ALWAYS_INLINE RefCountType deref_base() const
|
2021-06-03 02:12:09 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
VERIFY(m_ref_count);
|
|
|
|
return --m_ref_count;
|
2021-06-03 02:12:09 +03:00
|
|
|
}
|
2019-03-16 15:48:56 +03:00
|
|
|
|
2021-10-07 20:12:37 +03:00
|
|
|
RefCountType mutable m_ref_count { 1 };
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
2019-03-16 14:47:19 +03:00
|
|
|
template<typename T>
|
2019-06-21 16:29:31 +03:00
|
|
|
class RefCounted : public RefCountedBase {
|
2019-03-16 14:47:19 +03:00
|
|
|
public:
|
2020-12-29 23:14:21 +03:00
|
|
|
bool unref() const
|
2019-03-16 14:47:19 +03:00
|
|
|
{
|
2022-01-08 21:35:43 +03:00
|
|
|
auto* that = const_cast<T*>(static_cast<T const*>(this));
|
2022-01-08 17:15:55 +03:00
|
|
|
|
2020-06-12 16:38:24 +03:00
|
|
|
auto new_ref_count = deref_base();
|
|
|
|
if (new_ref_count == 0) {
|
2022-01-08 17:15:55 +03:00
|
|
|
if constexpr (requires { that->will_be_destroyed(); })
|
|
|
|
that->will_be_destroyed();
|
2022-08-19 18:26:07 +03:00
|
|
|
delete static_cast<T const*>(this);
|
2020-12-29 23:14:21 +03:00
|
|
|
return true;
|
2019-03-16 14:47:19 +03:00
|
|
|
}
|
2020-12-29 23:14:21 +03:00
|
|
|
return false;
|
2019-03-16 14:47:19 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-06-21 16:29:31 +03:00
|
|
|
using AK::RefCounted;
|
2021-08-15 13:26:22 +03:00
|
|
|
using AK::RefCountedBase;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|