2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-02-09 11:50:13 -03:00
|
|
|
#include <AK/Assertions.h>
|
2020-06-12 10:38:24 -03:00
|
|
|
#include <AK/Atomic.h>
|
2020-06-12 10:44:17 -03:00
|
|
|
#include <AK/Checked.h>
|
2020-06-12 10:46:47 -03:00
|
|
|
#include <AK/Noncopyable.h>
|
2020-05-20 09:13:39 -03:00
|
|
|
#include <AK/Platform.h>
|
2020-02-09 11:50:13 -03:00
|
|
|
#include <AK/StdLibExtras.h>
|
2018-10-10 06:53:07 -03:00
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
2018-12-19 19:28:09 -02:00
|
|
|
template<class T>
|
2020-12-05 23:47:20 -03:00
|
|
|
constexpr auto call_will_be_destroyed_if_present(const T* object) -> decltype(const_cast<T*>(object)->will_be_destroyed(), TrueType {})
|
2018-12-19 19:28:09 -02:00
|
|
|
{
|
2020-04-17 07:41:45 -03:00
|
|
|
const_cast<T*>(object)->will_be_destroyed();
|
2019-05-28 06:53:16 -03:00
|
|
|
return {};
|
2018-12-19 19:28:09 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr auto call_will_be_destroyed_if_present(...) -> FalseType
|
|
|
|
|
{
|
2019-05-28 06:53:16 -03:00
|
|
|
return {};
|
2018-12-19 19:28:09 -02:00
|
|
|
}
|
|
|
|
|
|
2018-12-31 23:38:09 -02:00
|
|
|
template<class T>
|
2020-12-05 23:47:20 -03:00
|
|
|
constexpr auto call_one_ref_left_if_present(const T* object) -> decltype(const_cast<T*>(object)->one_ref_left(), TrueType {})
|
2018-12-31 23:38:09 -02:00
|
|
|
{
|
2020-04-17 07:41:45 -03:00
|
|
|
const_cast<T*>(object)->one_ref_left();
|
2019-05-28 06:53:16 -03:00
|
|
|
return {};
|
2018-12-31 23:38:09 -02:00
|
|
|
}
|
|
|
|
|
|
2019-06-21 10:29:31 -03:00
|
|
|
constexpr auto call_one_ref_left_if_present(...) -> FalseType
|
2018-12-31 23:38:09 -02:00
|
|
|
{
|
2019-05-28 06:53:16 -03:00
|
|
|
return {};
|
2018-12-31 23:38:09 -02:00
|
|
|
}
|
|
|
|
|
|
2019-06-21 10:29:31 -03:00
|
|
|
class RefCountedBase {
|
2020-08-26 16:52:24 -03:00
|
|
|
AK_MAKE_NONCOPYABLE(RefCountedBase);
|
|
|
|
|
AK_MAKE_NONMOVABLE(RefCountedBase);
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
public:
|
2020-11-11 19:21:01 -03:00
|
|
|
using RefCountType = unsigned int;
|
2020-11-03 11:51:56 -03:00
|
|
|
using AllowOwnPtr = FalseType;
|
2020-06-12 10:33:38 -03:00
|
|
|
|
2021-06-02 20:12:09 -03:00
|
|
|
void ref() const
|
|
|
|
|
{
|
|
|
|
|
auto old_ref_count = m_ref_count.fetch_add(1, AK::MemoryOrder::memory_order_relaxed);
|
|
|
|
|
VERIFY(old_ref_count > 0);
|
|
|
|
|
VERIFY(!Checked<RefCountType>::addition_would_overflow(old_ref_count, 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] bool try_ref() const
|
|
|
|
|
{
|
|
|
|
|
RefCountType expected = m_ref_count.load(AK::MemoryOrder::memory_order_relaxed);
|
|
|
|
|
for (;;) {
|
|
|
|
|
if (expected == 0)
|
|
|
|
|
return false;
|
|
|
|
|
VERIFY(!Checked<RefCountType>::addition_would_overflow(expected, 1));
|
|
|
|
|
if (m_ref_count.compare_exchange_strong(expected, expected + 1, AK::MemoryOrder::memory_order_acquire))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] RefCountType ref_count() const
|
|
|
|
|
{
|
|
|
|
|
return m_ref_count.load(AK::MemoryOrder::memory_order_relaxed);
|
|
|
|
|
}
|
2018-10-10 06:53:07 -03:00
|
|
|
|
|
|
|
|
protected:
|
2021-01-10 20:29:28 -03:00
|
|
|
RefCountedBase() = default;
|
2021-06-02 20:12:09 -03:00
|
|
|
~RefCountedBase()
|
|
|
|
|
{
|
|
|
|
|
VERIFY(m_ref_count.load(AK::MemoryOrder::memory_order_relaxed) == 0);
|
|
|
|
|
}
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2021-06-02 20:12:09 -03:00
|
|
|
RefCountType deref_base() const
|
|
|
|
|
{
|
|
|
|
|
auto old_ref_count = m_ref_count.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel);
|
|
|
|
|
VERIFY(old_ref_count > 0);
|
|
|
|
|
return old_ref_count - 1;
|
|
|
|
|
}
|
2019-03-16 09:48:56 -03:00
|
|
|
|
2020-06-12 10:38:24 -03:00
|
|
|
mutable Atomic<RefCountType> m_ref_count { 1 };
|
2018-10-10 06:53:07 -03:00
|
|
|
};
|
|
|
|
|
|
2019-03-16 08:47:19 -03:00
|
|
|
template<typename T>
|
2019-06-21 10:29:31 -03:00
|
|
|
class RefCounted : public RefCountedBase {
|
2019-03-16 08:47:19 -03:00
|
|
|
public:
|
2020-12-29 17:14:21 -03:00
|
|
|
bool unref() const
|
2019-03-16 08:47:19 -03:00
|
|
|
{
|
2020-06-12 10:38:24 -03:00
|
|
|
auto new_ref_count = deref_base();
|
|
|
|
|
if (new_ref_count == 0) {
|
2020-04-17 07:41:45 -03:00
|
|
|
call_will_be_destroyed_if_present(static_cast<const T*>(this));
|
|
|
|
|
delete static_cast<const T*>(this);
|
2020-12-29 17:14:21 -03:00
|
|
|
return true;
|
2020-06-12 10:38:24 -03:00
|
|
|
} else if (new_ref_count == 1) {
|
2020-04-17 07:41:45 -03:00
|
|
|
call_one_ref_left_if_present(static_cast<const T*>(this));
|
2019-03-16 08:47:19 -03:00
|
|
|
}
|
2020-12-29 17:14:21 -03:00
|
|
|
return false;
|
2019-03-16 08:47:19 -03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
2019-06-21 10:29:31 -03:00
|
|
|
using AK::RefCounted;
|
2021-08-15 07:26:22 -03:00
|
|
|
using AK::RefCountedBase;
|