2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-10-07 19:10:56 +00:00
|
|
|
#define REFPTR_SCRUB_BYTE 0xe0
|
|
|
|
|
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-29 22:26:13 +00:00
|
|
|
#ifdef KERNEL
|
2021-10-07 17:12:37 +00:00
|
|
|
# include <Kernel/Library/ThreadSafeRefPtr.h>
|
|
|
|
#else
|
|
|
|
|
|
|
|
# include <AK/Assertions.h>
|
|
|
|
# include <AK/Atomic.h>
|
2021-11-06 09:14:14 +00:00
|
|
|
# include <AK/Error.h>
|
2021-10-07 17:12:37 +00:00
|
|
|
# include <AK/Format.h>
|
|
|
|
# include <AK/NonnullRefPtr.h>
|
|
|
|
# include <AK/StdLibExtras.h>
|
|
|
|
# include <AK/Traits.h>
|
|
|
|
# include <AK/Types.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-07-11 14:43:20 +00:00
|
|
|
template<typename T>
|
|
|
|
class OwnPtr;
|
|
|
|
|
2022-05-07 10:50:54 +00:00
|
|
|
template<typename T>
|
2021-12-01 21:05:13 +00:00
|
|
|
class [[nodiscard]] RefPtr {
|
2022-05-07 10:50:54 +00:00
|
|
|
template<typename U>
|
2020-09-23 16:17:43 +00: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-29 22:26:13 +00:00
|
|
|
template<typename U>
|
|
|
|
friend class WeakPtr;
|
2022-06-19 03:34:44 +00: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-29 22:26:13 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
public:
|
2019-06-07 15:13:23 +00:00
|
|
|
enum AdoptTag {
|
2019-05-28 09:53:16 +00:00
|
|
|
Adopt
|
|
|
|
};
|
|
|
|
|
2021-01-10 23:29:28 +00:00
|
|
|
RefPtr() = default;
|
2021-10-07 17:12:37 +00:00
|
|
|
RefPtr(T const* ptr)
|
|
|
|
: m_ptr(const_cast<T*>(ptr))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
ref_if_not_null(m_ptr);
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2021-10-07 17:12:37 +00:00
|
|
|
|
|
|
|
RefPtr(T const& object)
|
|
|
|
: m_ptr(const_cast<T*>(&object))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
m_ptr->ref();
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2021-10-07 17:12:37 +00:00
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr(AdoptTag, T& object)
|
2021-10-07 17:12:37 +00:00
|
|
|
: m_ptr(&object)
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
}
|
2021-10-07 17:12:37 +00:00
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr(RefPtr&& other)
|
2021-10-07 17:12:37 +00:00
|
|
|
: m_ptr(other.leak_ref())
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
}
|
2021-10-07 17:12:37 +00:00
|
|
|
|
|
|
|
ALWAYS_INLINE RefPtr(NonnullRefPtr<T> const& other)
|
|
|
|
: m_ptr(const_cast<T*>(other.ptr()))
|
2019-07-11 13:36:01 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
m_ptr->ref();
|
2019-07-11 13:36:01 +00:00
|
|
|
}
|
2021-10-07 17:12:37 +00:00
|
|
|
|
2019-07-11 13:36:01 +00:00
|
|
|
template<typename U>
|
2021-10-07 17:12:37 +00:00
|
|
|
ALWAYS_INLINE RefPtr(NonnullRefPtr<U> const& other) requires(IsConvertible<U*, T*>)
|
|
|
|
: m_ptr(const_cast<T*>(static_cast<T const*>(other.ptr())))
|
2019-07-11 13:36:01 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
m_ptr->ref();
|
2019-07-11 13:36:01 +00:00
|
|
|
}
|
2021-10-07 17:12:37 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename U>
|
2021-09-03 17:11:51 +00:00
|
|
|
ALWAYS_INLINE RefPtr(NonnullRefPtr<U>&& other) requires(IsConvertible<U*, T*>)
|
2021-10-07 17:12:37 +00:00
|
|
|
: m_ptr(static_cast<T*>(&other.leak_ref()))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
}
|
2021-10-07 17:12:37 +00:00
|
|
|
|
2022-05-07 10:50:54 +00:00
|
|
|
template<typename U>
|
|
|
|
RefPtr(RefPtr<U>&& other) requires(IsConvertible<U*, T*>)
|
2021-10-07 17:12:37 +00:00
|
|
|
: m_ptr(static_cast<T*>(other.leak_ref()))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
}
|
2021-10-07 17:12:37 +00:00
|
|
|
|
|
|
|
RefPtr(RefPtr const& other)
|
|
|
|
: m_ptr(other.m_ptr)
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
ref_if_not_null(m_ptr);
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2021-10-07 17:12:37 +00:00
|
|
|
|
2022-05-07 10:50:54 +00:00
|
|
|
template<typename U>
|
|
|
|
RefPtr(RefPtr<U> const& other) requires(IsConvertible<U*, T*>)
|
2021-10-07 17:12:37 +00:00
|
|
|
: m_ptr(const_cast<T*>(static_cast<T const*>(other.ptr())))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
ref_if_not_null(m_ptr);
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2021-10-07 17:12:37 +00:00
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE ~RefPtr()
|
2018-10-16 10:20:51 +00:00
|
|
|
{
|
|
|
|
clear();
|
2021-10-07 17:12:37 +00:00
|
|
|
# ifdef SANITIZE_PTRS
|
2021-10-07 19:10:56 +00:00
|
|
|
m_ptr = reinterpret_cast<T*>(explode_byte(REFPTR_SCRUB_BYTE));
|
2021-10-07 17:12:37 +00:00
|
|
|
# endif
|
2018-10-16 10:20:51 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-07-11 14:43:20 +00:00
|
|
|
template<typename U>
|
2021-10-07 17:12:37 +00:00
|
|
|
RefPtr(OwnPtr<U> const&) = delete;
|
2019-07-11 14:43:20 +00:00
|
|
|
template<typename U>
|
2021-10-07 17:12:37 +00:00
|
|
|
RefPtr& operator=(OwnPtr<U> const&) = delete;
|
2019-07-11 14:43:20 +00: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-29 22:26:13 +00:00
|
|
|
void swap(RefPtr& other)
|
|
|
|
{
|
2021-10-07 17:12:37 +00: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-29 22:26:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 10:50:54 +00:00
|
|
|
template<typename U>
|
|
|
|
void swap(RefPtr<U>& other) requires(IsConvertible<U*, T*>)
|
2019-08-02 09:56:55 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
AK::swap(m_ptr, other.m_ptr);
|
2019-08-02 09:56:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(RefPtr&& other)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
RefPtr tmp { move(other) };
|
|
|
|
swap(tmp);
|
2018-10-10 09:53:07 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-05-07 10:50:54 +00:00
|
|
|
template<typename U>
|
|
|
|
ALWAYS_INLINE RefPtr& operator=(RefPtr<U>&& other) requires(IsConvertible<U*, T*>)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
RefPtr tmp { move(other) };
|
|
|
|
swap(tmp);
|
2018-10-10 09:53:07 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-04-14 00:36:06 +00:00
|
|
|
template<typename U>
|
2021-09-03 17:11:51 +00:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(NonnullRefPtr<U>&& other) requires(IsConvertible<U*, T*>)
|
2019-04-14 00:36:06 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
RefPtr tmp { move(other) };
|
|
|
|
swap(tmp);
|
2019-04-14 00:36:06 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-07 17:12:37 +00:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(NonnullRefPtr<T> const& other)
|
2019-07-11 13:36:01 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
RefPtr tmp { other };
|
|
|
|
swap(tmp);
|
2019-07-11 13:36:01 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-06-15 16:45:44 +00:00
|
|
|
template<typename U>
|
2021-10-07 17:12:37 +00:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(NonnullRefPtr<U> const& other) requires(IsConvertible<U*, T*>)
|
2019-06-15 16:45:44 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
RefPtr tmp { other };
|
|
|
|
swap(tmp);
|
2019-06-15 16:45:44 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-07 17:12:37 +00:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(RefPtr const& other)
|
2019-07-11 13:36:01 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
RefPtr tmp { other };
|
|
|
|
swap(tmp);
|
2019-07-11 13:36:01 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-06-15 16:45:44 +00:00
|
|
|
template<typename U>
|
2021-10-07 17:12:37 +00:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(RefPtr<U> const& other) requires(IsConvertible<U*, T*>)
|
2019-06-15 16:45:44 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
RefPtr tmp { other };
|
|
|
|
swap(tmp);
|
2019-06-15 16:45:44 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-07 17:12:37 +00:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(T const* ptr)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
RefPtr tmp { ptr };
|
|
|
|
swap(tmp);
|
2018-10-10 09:53:07 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-07 17:12:37 +00:00
|
|
|
ALWAYS_INLINE RefPtr& operator=(T const& object)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
RefPtr tmp { object };
|
|
|
|
swap(tmp);
|
2018-10-10 09:53:07 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr& operator=(std::nullptr_t)
|
2018-10-10 09:53:07 +00: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-29 22:26:13 +00:00
|
|
|
ALWAYS_INLINE bool assign_if_null(RefPtr&& other)
|
|
|
|
{
|
|
|
|
if (this == &other)
|
|
|
|
return is_null();
|
2021-10-07 17:12:37 +00: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-29 22:26:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 10:50:54 +00: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-29 22:26:13 +00:00
|
|
|
{
|
|
|
|
if (this == &other)
|
|
|
|
return is_null();
|
2021-10-07 17:12:37 +00: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-29 22:26:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE void clear()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
unref_if_not_null(m_ptr);
|
|
|
|
m_ptr = nullptr;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 17:12:37 +00:00
|
|
|
bool operator!() const { return !m_ptr; }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-08-02 10:05:09 +00:00
|
|
|
[[nodiscard]] T* leak_ref()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
return exchange(m_ptr, nullptr);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 09:47:38 +00:00
|
|
|
NonnullRefPtr<T> release_nonnull()
|
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
auto* ptr = leak_ref();
|
|
|
|
VERIFY(ptr);
|
|
|
|
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *ptr);
|
2019-08-14 09:47:38 +00: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-29 22:26:13 +00:00
|
|
|
ALWAYS_INLINE T* ptr() { return as_ptr(); }
|
|
|
|
ALWAYS_INLINE const T* ptr() const { return as_ptr(); }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE T* operator->()
|
2019-08-02 10:00:43 +00: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-29 22:26:13 +00:00
|
|
|
return as_nonnull_ptr();
|
2019-08-02 10:00:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE const T* operator->() const
|
2019-08-02 10:00:43 +00: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-29 22:26:13 +00:00
|
|
|
return as_nonnull_ptr();
|
2019-08-02 10:00:43 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE T& operator*()
|
2019-08-02 10:00:43 +00: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-29 22:26:13 +00:00
|
|
|
return *as_nonnull_ptr();
|
2019-08-02 10:00:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE const T& operator*() const
|
2019-08-02 10:00:43 +00: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-29 22:26:13 +00:00
|
|
|
return *as_nonnull_ptr();
|
2019-08-02 10:00:43 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00: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-29 22:26:13 +00:00
|
|
|
ALWAYS_INLINE operator const T*() const { return as_ptr(); }
|
|
|
|
ALWAYS_INLINE operator T*() { return as_ptr(); }
|
2019-04-14 00:36:06 +00:00
|
|
|
|
2020-11-25 19:08:37 +00:00
|
|
|
ALWAYS_INLINE operator bool() { return !is_null(); }
|
2018-10-10 09:53:07 +00: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-29 22:26:13 +00:00
|
|
|
bool operator==(std::nullptr_t) const { return is_null(); }
|
|
|
|
bool operator!=(std::nullptr_t) const { return !is_null(); }
|
2019-04-14 00:36:06 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator==(RefPtr const& other) const { return as_ptr() == other.as_ptr(); }
|
|
|
|
bool operator!=(RefPtr const& other) const { return as_ptr() != other.as_ptr(); }
|
2019-04-14 00:36:06 +00: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-29 22:26:13 +00:00
|
|
|
bool operator==(RefPtr& other) { return as_ptr() == other.as_ptr(); }
|
|
|
|
bool operator!=(RefPtr& other) { return as_ptr() != other.as_ptr(); }
|
2019-04-19 19:36:11 +00:00
|
|
|
|
2022-06-19 03:34:44 +00:00
|
|
|
template<typename U>
|
|
|
|
bool operator==(NonnullRefPtr<U> const& other) const { return as_ptr() == other.m_ptr; }
|
|
|
|
template<typename U>
|
|
|
|
bool operator!=(NonnullRefPtr<U> const& other) const { return as_ptr() != other.m_ptr; }
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
bool operator==(NonnullRefPtr<U>& other) { return as_ptr() == other.m_ptr; }
|
|
|
|
template<typename U>
|
|
|
|
bool operator!=(NonnullRefPtr<U>& other) { return as_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-29 22:26:13 +00:00
|
|
|
bool operator==(const T* other) const { return as_ptr() == other; }
|
|
|
|
bool operator!=(const T* other) const { return as_ptr() != other; }
|
2019-04-14 00:36:06 +00: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-29 22:26:13 +00:00
|
|
|
bool operator==(T* other) { return as_ptr() == other; }
|
|
|
|
bool operator!=(T* other) { return as_ptr() != other; }
|
|
|
|
|
2021-10-07 17:12:37 +00:00
|
|
|
ALWAYS_INLINE bool is_null() const { return !m_ptr; }
|
2018-11-05 09:23:00 +00:00
|
|
|
|
2018-10-10 09:53:07 +00: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-29 22:26:13 +00:00
|
|
|
ALWAYS_INLINE T* as_ptr() const
|
|
|
|
{
|
2021-10-07 17:12:37 +00: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-29 22:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE T* as_nonnull_ptr() const
|
|
|
|
{
|
2021-10-07 17:12:37 +00: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-29 22:26:13 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 17:12:37 +00:00
|
|
|
T* m_ptr { nullptr };
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2021-03-09 20:30:07 +00:00
|
|
|
template<typename T>
|
|
|
|
struct Formatter<RefPtr<T>> : Formatter<const T*> {
|
2021-11-16 00:15:21 +00:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, RefPtr<T> const& value)
|
2021-03-09 20:30:07 +00:00
|
|
|
{
|
2021-11-16 00:15:21 +00:00
|
|
|
return Formatter<const T*>::format(builder, value.ptr());
|
2021-03-09 20:30:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-16 18:36:15 +00:00
|
|
|
template<typename T>
|
|
|
|
struct Traits<RefPtr<T>> : public GenericTraits<RefPtr<T>> {
|
2021-05-08 09:11:37 +00:00
|
|
|
using PeekType = T*;
|
|
|
|
using ConstPeekType = const T*;
|
2022-04-01 17:58:27 +00: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 18:36:15 +00:00
|
|
|
};
|
|
|
|
|
2020-04-05 09:11:07 +00:00
|
|
|
template<typename T, typename U>
|
2022-04-01 17:58:27 +00:00
|
|
|
inline NonnullRefPtr<T> static_ptr_cast(NonnullRefPtr<U> const& ptr)
|
2020-04-05 09:11:07 +00:00
|
|
|
{
|
|
|
|
return NonnullRefPtr<T>(static_cast<const T&>(*ptr));
|
|
|
|
}
|
|
|
|
|
2022-05-07 10:50:54 +00:00
|
|
|
template<typename T, typename U>
|
2022-04-01 17:58:27 +00:00
|
|
|
inline RefPtr<T> static_ptr_cast(RefPtr<U> const& ptr)
|
2020-04-05 09:11:07 +00:00
|
|
|
{
|
2022-05-07 10:50:54 +00:00
|
|
|
return RefPtr<T>(static_cast<const T*>(ptr.ptr()));
|
2020-04-05 09:11:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 10:50:54 +00:00
|
|
|
template<typename T, typename U>
|
|
|
|
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-29 22:26:13 +00:00
|
|
|
{
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2021-05-13 04:02:43 +00: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 08:04:41 +00:00
|
|
|
template<typename T, class... Args>
|
2022-02-03 14:43:34 +00:00
|
|
|
requires(IsConstructible<T, Args...>) inline ErrorOr<NonnullRefPtr<T>> try_make_ref_counted(Args&&... args)
|
2021-06-20 08:04:41 +00:00
|
|
|
{
|
2022-02-03 14:43:34 +00:00
|
|
|
return adopt_nonnull_ref_or_enomem(new (nothrow) T(forward<Args>(args)...));
|
2021-06-20 08:04:41 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 08:21:14 +00:00
|
|
|
// FIXME: Remove once P0960R3 is available in Clang.
|
|
|
|
template<typename T, class... Args>
|
2022-02-03 14:43:34 +00:00
|
|
|
inline ErrorOr<NonnullRefPtr<T>> try_make_ref_counted(Args&&... args)
|
2021-07-01 08:21:14 +00:00
|
|
|
{
|
2022-02-03 14:43:34 +00:00
|
|
|
return adopt_nonnull_ref_or_enomem(new (nothrow) T { forward<Args>(args)... });
|
2021-07-01 08:21:14 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 09:14:14 +00: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-06 21:16:50 +00:00
|
|
|
return Error::from_errno(ENOMEM);
|
2021-11-06 09:14:14 +00:00
|
|
|
return result.release_nonnull();
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 04:02:43 +00:00
|
|
|
using AK::adopt_ref_if_nonnull;
|
2019-06-21 16:37:47 +00:00
|
|
|
using AK::RefPtr;
|
2020-04-05 09:11:07 +00:00
|
|
|
using AK::static_ptr_cast;
|
2021-09-02 22:07:29 +00:00
|
|
|
using AK::try_make_ref_counted;
|
2021-08-14 12:33:50 +00:00
|
|
|
|
|
|
|
#endif
|