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
|
|
|
|
|
2021-10-07 22:10:56 +03:00
|
|
|
#define REFPTR_SCRUB_BYTE 0xe0
|
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Atomic.h>
|
|
|
|
#include <AK/Error.h>
|
|
|
|
#include <AK/Format.h>
|
2022-12-16 06:51:55 +03:00
|
|
|
#include <AK/Forward.h>
|
2022-08-19 21:53:40 +03:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
#include <AK/Traits.h>
|
|
|
|
#include <AK/Types.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2022-05-07 13:50:54 +03:00
|
|
|
template<typename T>
|
2021-12-02 00:05:13 +03:00
|
|
|
class [[nodiscard]] RefPtr {
|
2022-05-07 13:50:54 +03:00
|
|
|
template<typename U>
|
2020-09-23 19:17:43 +03:00
|
|
|
friend class RefPtr;
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
template<typename U>
|
|
|
|
friend class WeakPtr;
|
2022-06-19 06:34:44 +03:00
|
|
|
template<typename U>
|
|
|
|
friend class NonnullRefPtr;
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
public:
|
2019-06-07 18:13:23 +03:00
|
|
|
enum AdoptTag {
|
2019-05-28 12:53:16 +03:00
|
|
|
Adopt
|
|
|
|
};
|
|
|
|
|
2021-01-11 02:29:28 +03:00
|
|
|
RefPtr() = default;
|
2021-10-07 20:12:37 +03:00
|
|
|
RefPtr(T const* ptr)
|
|
|
|
: m_ptr(const_cast<T*>(ptr))
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
ref_if_not_null(m_ptr);
|
2019-05-28 12:53:16 +03:00
|
|
|
}
|
2021-10-07 20:12:37 +03:00
|
|
|
|
|
|
|
RefPtr(T const& object)
|
|
|
|
: m_ptr(const_cast<T*>(&object))
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
m_ptr->ref();
|
2019-05-28 12:53:16 +03:00
|
|
|
}
|
2021-10-07 20:12:37 +03:00
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr(AdoptTag, T& object)
|
2021-10-07 20:12:37 +03:00
|
|
|
: m_ptr(&object)
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
|
|
|
}
|
2021-10-07 20:12:37 +03:00
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr(RefPtr&& other)
|
2021-10-07 20:12:37 +03:00
|
|
|
: m_ptr(other.leak_ref())
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
|
|
|
}
|
2021-10-07 20:12:37 +03:00
|
|
|
|
|
|
|
ALWAYS_INLINE RefPtr(NonnullRefPtr<T> const& other)
|
|
|
|
: m_ptr(const_cast<T*>(other.ptr()))
|
2019-07-11 16:36:01 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
m_ptr->ref();
|
2019-07-11 16:36:01 +03:00
|
|
|
}
|
2021-10-07 20:12:37 +03:00
|
|
|
|
2019-07-11 16:36:01 +03:00
|
|
|
template<typename U>
|
2022-10-17 01:06:11 +03:00
|
|
|
ALWAYS_INLINE RefPtr(NonnullRefPtr<U> const& other)
|
|
|
|
requires(IsConvertible<U*, T*>)
|
2021-10-07 20:12:37 +03:00
|
|
|
: m_ptr(const_cast<T*>(static_cast<T const*>(other.ptr())))
|
2019-07-11 16:36:01 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
m_ptr->ref();
|
2019-07-11 16:36:01 +03:00
|
|
|
}
|
2021-10-07 20:12:37 +03:00
|
|
|
|
2019-05-28 12:53:16 +03:00
|
|
|
template<typename U>
|
2022-10-17 01:06:11 +03:00
|
|
|
ALWAYS_INLINE RefPtr(NonnullRefPtr<U>&& other)
|
|
|
|
requires(IsConvertible<U*, T*>)
|
2021-10-07 20:12:37 +03:00
|
|
|
: m_ptr(static_cast<T*>(&other.leak_ref()))
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
|
|
|
}
|
2021-10-07 20:12:37 +03:00
|
|
|
|
2022-05-07 13:50:54 +03:00
|
|
|
template<typename U>
|
2022-10-17 01:06:11 +03:00
|
|
|
RefPtr(RefPtr<U>&& other)
|
|
|
|
requires(IsConvertible<U*, T*>)
|
2021-10-07 20:12:37 +03:00
|
|
|
: m_ptr(static_cast<T*>(other.leak_ref()))
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
|
|
|
}
|
2021-10-07 20:12:37 +03:00
|
|
|
|
|
|
|
RefPtr(RefPtr const& other)
|
|
|
|
: m_ptr(other.m_ptr)
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
ref_if_not_null(m_ptr);
|
2019-05-28 12:53:16 +03:00
|
|
|
}
|
2021-10-07 20:12:37 +03:00
|
|
|
|
2022-05-07 13:50:54 +03:00
|
|
|
template<typename U>
|
2022-10-17 01:06:11 +03:00
|
|
|
RefPtr(RefPtr<U> const& other)
|
|
|
|
requires(IsConvertible<U*, T*>)
|
2021-10-07 20:12:37 +03:00
|
|
|
: m_ptr(const_cast<T*>(static_cast<T const*>(other.ptr())))
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
ref_if_not_null(m_ptr);
|
2019-05-28 12:53:16 +03:00
|
|
|
}
|
2021-10-07 20:12:37 +03:00
|
|
|
|
2020-05-20 14:59:31 +03:00
|
|
|
ALWAYS_INLINE ~RefPtr()
|
2018-10-16 13:20:51 +03:00
|
|
|
{
|
|
|
|
clear();
|
2022-08-19 21:53:40 +03:00
|
|
|
#ifdef SANITIZE_PTRS
|
2021-10-07 22:10:56 +03:00
|
|
|
m_ptr = reinterpret_cast<T*>(explode_byte(REFPTR_SCRUB_BYTE));
|
2022-08-19 21:53:40 +03:00
|
|
|
#endif
|
2018-10-16 13:20:51 +03:00
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-07-11 17:43:20 +03:00
|
|
|
template<typename U>
|
2021-10-07 20:12:37 +03:00
|
|
|
RefPtr(OwnPtr<U> const&) = delete;
|
2019-07-11 17:43:20 +03:00
|
|
|
template<typename U>
|
2021-10-07 20:12:37 +03:00
|
|
|
RefPtr& operator=(OwnPtr<U> const&) = delete;
|
2019-07-11 17:43:20 +03:00
|
|
|
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
void swap(RefPtr& other)
|
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
AK::swap(m_ptr, other.m_ptr);
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 13:50:54 +03:00
|
|
|
template<typename U>
|
2022-10-17 01:06:11 +03:00
|
|
|
void swap(RefPtr<U>& other)
|
|
|
|
requires(IsConvertible<U*, T*>)
|
2019-08-02 12:56:55 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
AK::swap(m_ptr, other.m_ptr);
|
2019-08-02 12:56:55 +03:00
|
|
|
}
|
|
|
|
|
2020-05-20 14:59:31 +03:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(RefPtr&& other)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
RefPtr tmp { move(other) };
|
|
|
|
swap(tmp);
|
2018-10-10 12:53:07 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-05-07 13:50:54 +03:00
|
|
|
template<typename U>
|
2022-10-17 01:06:11 +03:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(RefPtr<U>&& other)
|
|
|
|
requires(IsConvertible<U*, T*>)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
RefPtr tmp { move(other) };
|
|
|
|
swap(tmp);
|
2018-10-10 12:53:07 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-04-14 03:36:06 +03:00
|
|
|
template<typename U>
|
2022-10-17 01:06:11 +03:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(NonnullRefPtr<U>&& other)
|
|
|
|
requires(IsConvertible<U*, T*>)
|
2019-04-14 03:36:06 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
RefPtr tmp { move(other) };
|
|
|
|
swap(tmp);
|
2019-04-14 03:36:06 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-07 20:12:37 +03:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(NonnullRefPtr<T> const& other)
|
2019-07-11 16:36:01 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
RefPtr tmp { other };
|
|
|
|
swap(tmp);
|
2019-07-11 16:36:01 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-06-15 19:45:44 +03:00
|
|
|
template<typename U>
|
2022-10-17 01:06:11 +03:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(NonnullRefPtr<U> const& other)
|
|
|
|
requires(IsConvertible<U*, T*>)
|
2019-06-15 19:45:44 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
RefPtr tmp { other };
|
|
|
|
swap(tmp);
|
2019-06-15 19:45:44 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-07 20:12:37 +03:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(RefPtr const& other)
|
2019-07-11 16:36:01 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
RefPtr tmp { other };
|
|
|
|
swap(tmp);
|
2019-07-11 16:36:01 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-06-15 19:45:44 +03:00
|
|
|
template<typename U>
|
2022-10-17 01:06:11 +03:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(RefPtr<U> const& other)
|
|
|
|
requires(IsConvertible<U*, T*>)
|
2019-06-15 19:45:44 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
RefPtr tmp { other };
|
|
|
|
swap(tmp);
|
2019-06-15 19:45:44 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-07 20:12:37 +03:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(T const* ptr)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
RefPtr tmp { ptr };
|
|
|
|
swap(tmp);
|
2018-10-10 12:53:07 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-07 20:12:37 +03:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(T const& object)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
RefPtr tmp { object };
|
|
|
|
swap(tmp);
|
2018-10-10 12:53:07 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-13 09:59:30 +03:00
|
|
|
RefPtr& operator=(nullptr_t)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
ALWAYS_INLINE bool assign_if_null(RefPtr&& other)
|
|
|
|
{
|
|
|
|
if (this == &other)
|
|
|
|
return is_null();
|
2021-10-07 20:12:37 +03:00
|
|
|
*this = move(other);
|
|
|
|
return true;
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 13:50:54 +03:00
|
|
|
template<typename U>
|
|
|
|
ALWAYS_INLINE bool assign_if_null(RefPtr<U>&& other)
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
{
|
|
|
|
if (this == &other)
|
|
|
|
return is_null();
|
2021-10-07 20:12:37 +03:00
|
|
|
*this = move(other);
|
|
|
|
return true;
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
}
|
|
|
|
|
2020-05-20 14:59:31 +03:00
|
|
|
ALWAYS_INLINE void clear()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
unref_if_not_null(m_ptr);
|
|
|
|
m_ptr = nullptr;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-07 20:12:37 +03:00
|
|
|
bool operator!() const { return !m_ptr; }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-08-02 13:05:09 +03:00
|
|
|
[[nodiscard]] T* leak_ref()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
return exchange(m_ptr, nullptr);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-08-14 12:47:38 +03:00
|
|
|
NonnullRefPtr<T> release_nonnull()
|
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
auto* ptr = leak_ref();
|
|
|
|
VERIFY(ptr);
|
|
|
|
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *ptr);
|
2019-08-14 12:47:38 +03:00
|
|
|
}
|
|
|
|
|
2022-11-19 04:03:48 +03:00
|
|
|
ALWAYS_INLINE T* ptr() const { return as_ptr(); }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2022-11-19 04:03:48 +03:00
|
|
|
ALWAYS_INLINE T* operator->() const
|
2019-08-02 13:00:43 +03:00
|
|
|
{
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
return as_nonnull_ptr();
|
2019-08-02 13:00:43 +03:00
|
|
|
}
|
|
|
|
|
2022-11-19 04:03:48 +03:00
|
|
|
ALWAYS_INLINE T& operator*() const
|
2019-08-02 13:00:43 +03:00
|
|
|
{
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
return *as_nonnull_ptr();
|
2019-08-02 13:00:43 +03:00
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2022-11-19 04:03:48 +03:00
|
|
|
ALWAYS_INLINE operator T*() const { return as_ptr(); }
|
2019-04-14 03:36:06 +03:00
|
|
|
|
2020-11-25 22:08:37 +03:00
|
|
|
ALWAYS_INLINE operator bool() { return !is_null(); }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2022-12-13 09:59:30 +03:00
|
|
|
bool operator==(nullptr_t) const { return is_null(); }
|
2019-04-14 03:36:06 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool operator==(RefPtr const& other) const { return as_ptr() == other.as_ptr(); }
|
2019-04-14 03:36:06 +03:00
|
|
|
|
2022-06-19 06:34:44 +03:00
|
|
|
template<typename U>
|
|
|
|
bool operator==(NonnullRefPtr<U> const& other) const { return as_ptr() == other.m_ptr; }
|
|
|
|
|
2022-11-19 04:03:48 +03:00
|
|
|
template<typename RawPtr>
|
2022-10-17 01:06:11 +03:00
|
|
|
bool operator==(RawPtr other) const
|
|
|
|
requires(IsPointer<RawPtr>)
|
|
|
|
{
|
|
|
|
return as_ptr() == other;
|
|
|
|
}
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
|
2021-10-07 20:12:37 +03:00
|
|
|
ALWAYS_INLINE bool is_null() const { return !m_ptr; }
|
2018-11-05 12:23:00 +03:00
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
private:
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
ALWAYS_INLINE T* as_ptr() const
|
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
return m_ptr;
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE T* as_nonnull_ptr() const
|
|
|
|
{
|
2021-10-07 20:12:37 +03:00
|
|
|
VERIFY(m_ptr);
|
|
|
|
return m_ptr;
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
}
|
|
|
|
|
2021-10-07 20:12:37 +03:00
|
|
|
T* m_ptr { nullptr };
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
2021-03-09 23:30:07 +03:00
|
|
|
template<typename T>
|
2022-08-19 21:53:40 +03:00
|
|
|
struct Formatter<RefPtr<T>> : Formatter<T const*> {
|
2021-11-16 03:15:21 +03:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, RefPtr<T> const& value)
|
2021-03-09 23:30:07 +03:00
|
|
|
{
|
2022-08-19 21:53:40 +03:00
|
|
|
return Formatter<T const*>::format(builder, value.ptr());
|
2021-03-09 23:30:07 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-16 21:36:15 +03:00
|
|
|
template<typename T>
|
|
|
|
struct Traits<RefPtr<T>> : public GenericTraits<RefPtr<T>> {
|
2021-05-08 12:11:37 +03:00
|
|
|
using PeekType = T*;
|
2022-08-19 21:53:40 +03:00
|
|
|
using ConstPeekType = T const*;
|
2022-04-01 20:58:27 +03:00
|
|
|
static unsigned hash(RefPtr<T> const& p) { return ptr_hash(p.ptr()); }
|
|
|
|
static bool equals(RefPtr<T> const& a, RefPtr<T> const& b) { return a.ptr() == b.ptr(); }
|
2020-02-16 21:36:15 +03:00
|
|
|
};
|
|
|
|
|
2020-04-05 12:11:07 +03:00
|
|
|
template<typename T, typename U>
|
2022-04-01 20:58:27 +03:00
|
|
|
inline NonnullRefPtr<T> static_ptr_cast(NonnullRefPtr<U> const& ptr)
|
2020-04-05 12:11:07 +03:00
|
|
|
{
|
2022-08-19 21:53:40 +03:00
|
|
|
return NonnullRefPtr<T>(static_cast<T const&>(*ptr));
|
2020-04-05 12:11:07 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 13:50:54 +03:00
|
|
|
template<typename T, typename U>
|
2022-04-01 20:58:27 +03:00
|
|
|
inline RefPtr<T> static_ptr_cast(RefPtr<U> const& ptr)
|
2020-04-05 12:11:07 +03:00
|
|
|
{
|
2022-08-19 21:53:40 +03:00
|
|
|
return RefPtr<T>(static_cast<T const*>(ptr.ptr()));
|
2020-04-05 12:11:07 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 13:50:54 +03:00
|
|
|
template<typename T, typename U>
|
2022-10-17 01:06:11 +03:00
|
|
|
inline void swap(RefPtr<T>& a, RefPtr<U>& b)
|
|
|
|
requires(IsConvertible<U*, T*>)
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
{
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2021-05-13 07:02:43 +03:00
|
|
|
template<typename T>
|
|
|
|
inline RefPtr<T> adopt_ref_if_nonnull(T* object)
|
|
|
|
{
|
|
|
|
if (object)
|
|
|
|
return RefPtr<T>(RefPtr<T>::Adopt, *object);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-06-20 11:04:41 +03:00
|
|
|
template<typename T, class... Args>
|
2022-02-03 17:43:34 +03:00
|
|
|
requires(IsConstructible<T, Args...>) inline ErrorOr<NonnullRefPtr<T>> try_make_ref_counted(Args&&... args)
|
2021-06-20 11:04:41 +03:00
|
|
|
{
|
2022-02-03 17:43:34 +03:00
|
|
|
return adopt_nonnull_ref_or_enomem(new (nothrow) T(forward<Args>(args)...));
|
2021-06-20 11:04:41 +03:00
|
|
|
}
|
|
|
|
|
2021-07-01 11:21:14 +03:00
|
|
|
// FIXME: Remove once P0960R3 is available in Clang.
|
|
|
|
template<typename T, class... Args>
|
2022-02-03 17:43:34 +03:00
|
|
|
inline ErrorOr<NonnullRefPtr<T>> try_make_ref_counted(Args&&... args)
|
2021-07-01 11:21:14 +03:00
|
|
|
{
|
2022-02-03 17:43:34 +03:00
|
|
|
return adopt_nonnull_ref_or_enomem(new (nothrow) T { forward<Args>(args)... });
|
2021-07-01 11:21:14 +03:00
|
|
|
}
|
|
|
|
|
2021-11-06 12:14:14 +03:00
|
|
|
template<typename T>
|
|
|
|
inline ErrorOr<NonnullRefPtr<T>> adopt_nonnull_ref_or_enomem(T* object)
|
|
|
|
{
|
|
|
|
auto result = adopt_ref_if_nonnull(object);
|
|
|
|
if (!result)
|
2021-11-07 00:16:50 +03:00
|
|
|
return Error::from_errno(ENOMEM);
|
2021-11-06 12:14:14 +03:00
|
|
|
return result.release_nonnull();
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-05-13 07:02:43 +03:00
|
|
|
using AK::adopt_ref_if_nonnull;
|
2019-06-21 19:37:47 +03:00
|
|
|
using AK::RefPtr;
|
2020-04-05 12:11:07 +03:00
|
|
|
using AK::static_ptr_cast;
|
2021-09-03 01:07:29 +03:00
|
|
|
using AK::try_make_ref_counted;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|