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
|
|
|
*/
|
|
|
|
|
2019-02-25 11:43:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-10-07 19:10:56 +00:00
|
|
|
#define NONNULLREFPTR_SCRUB_BYTE 0xe1
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Atomic.h>
|
|
|
|
#include <AK/Format.h>
|
|
|
|
#include <AK/Traits.h>
|
|
|
|
#include <AK/Types.h>
|
2019-02-25 11:43:52 +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>
|
2019-08-02 09:51:28 +00:00
|
|
|
class RefPtr;
|
2019-07-11 14:43:20 +00:00
|
|
|
|
2019-02-25 11:43:52 +00:00
|
|
|
template<typename T>
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE void ref_if_not_null(T* ptr)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
|
|
|
if (ptr)
|
2019-06-21 13:29:31 +00:00
|
|
|
ptr->ref();
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE void unref_if_not_null(T* ptr)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
|
|
|
if (ptr)
|
2020-01-23 14:14:21 +00:00
|
|
|
ptr->unref();
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2021-12-01 21:05:13 +00:00
|
|
|
class [[nodiscard]] NonnullRefPtr {
|
2022-05-07 10:50:54 +00:00
|
|
|
template<typename U>
|
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
|
|
|
friend class RefPtr;
|
|
|
|
template<typename U>
|
|
|
|
friend class NonnullRefPtr;
|
|
|
|
template<typename U>
|
|
|
|
friend class WeakPtr;
|
|
|
|
|
2019-02-25 11:43:52 +00:00
|
|
|
public:
|
2020-11-11 22:21:01 +00:00
|
|
|
using ElementType = T;
|
2019-07-25 09:10:28 +00:00
|
|
|
|
|
|
|
enum AdoptTag { Adopt };
|
2019-05-28 09:53:16 +00:00
|
|
|
|
2021-10-07 17:12:37 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr(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-05-28 09:53:16 +00:00
|
|
|
template<typename U>
|
2021-10-07 17:12:37 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr(U const& object) requires(IsConvertible<U*, T*>)
|
|
|
|
: m_ptr(const_cast<T*>(static_cast<T const*>(&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
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr(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
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr&& 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
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename U>
|
2021-09-03 17:11:51 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr(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
|
|
|
|
|
|
|
ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr const& other)
|
|
|
|
: m_ptr(const_cast<T*>(other.ptr()))
|
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-05-28 09:53:16 +00:00
|
|
|
template<typename U>
|
2021-10-07 17:12:37 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr<U> const& other) requires(IsConvertible<U*, T*>)
|
|
|
|
: 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
|
|
|
m_ptr->ref();
|
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 ~NonnullRefPtr()
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
unref_if_not_null(m_ptr);
|
|
|
|
m_ptr = nullptr;
|
2022-08-19 18:53:40 +00:00
|
|
|
#ifdef SANITIZE_PTRS
|
2021-10-07 19:10:56 +00:00
|
|
|
m_ptr = reinterpret_cast<T*>(explode_byte(NONNULLREFPTR_SCRUB_BYTE));
|
2022-08-19 18:53:40 +00:00
|
|
|
#endif
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 14:43:20 +00:00
|
|
|
template<typename U>
|
2022-04-01 17:58:27 +00:00
|
|
|
NonnullRefPtr(OwnPtr<U> const&) = delete;
|
2019-07-11 14:43:20 +00:00
|
|
|
template<typename U>
|
2022-04-01 17:58:27 +00:00
|
|
|
NonnullRefPtr& operator=(OwnPtr<U> const&) = delete;
|
2019-07-11 14:43:20 +00:00
|
|
|
|
2019-08-02 09:51:28 +00:00
|
|
|
template<typename U>
|
2022-04-01 17:58:27 +00:00
|
|
|
NonnullRefPtr(RefPtr<U> const&) = delete;
|
2019-08-02 09:51:28 +00:00
|
|
|
template<typename U>
|
2022-04-01 17:58:27 +00:00
|
|
|
NonnullRefPtr& operator=(RefPtr<U> const&) = delete;
|
|
|
|
NonnullRefPtr(RefPtr<T> const&) = delete;
|
|
|
|
NonnullRefPtr& operator=(RefPtr<T> const&) = delete;
|
2019-08-02 09:51:28 +00:00
|
|
|
|
2021-10-07 17:12:37 +00:00
|
|
|
NonnullRefPtr& operator=(NonnullRefPtr const& other)
|
2019-06-24 07:58:21 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
NonnullRefPtr tmp { other };
|
|
|
|
swap(tmp);
|
2019-06-24 07:58:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2021-10-07 17:12:37 +00:00
|
|
|
NonnullRefPtr& operator=(NonnullRefPtr<U> const& other) requires(IsConvertible<U*, T*>)
|
2019-06-24 07:58:21 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
NonnullRefPtr tmp { other };
|
|
|
|
swap(tmp);
|
2019-06-24 07:58:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr& operator=(NonnullRefPtr&& other)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
NonnullRefPtr tmp { move(other) };
|
|
|
|
swap(tmp);
|
2019-02-25 11:43:52 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2021-09-03 17:11:51 +00:00
|
|
|
NonnullRefPtr& operator=(NonnullRefPtr<U>&& other) requires(IsConvertible<U*, T*>)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
NonnullRefPtr tmp { move(other) };
|
|
|
|
swap(tmp);
|
2019-02-25 11:43:52 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-07 17:12:37 +00:00
|
|
|
NonnullRefPtr& operator=(T const& object)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
NonnullRefPtr tmp { object };
|
|
|
|
swap(tmp);
|
2019-02-25 11:43:52 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T& leak_ref()
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
T* ptr = exchange(m_ptr, nullptr);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(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
|
|
|
return *ptr;
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 13:45:24 +00:00
|
|
|
ALWAYS_INLINE RETURNS_NONNULL T* ptr()
|
2019-05-28 09:53:16 +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-05-28 09:53:16 +00:00
|
|
|
}
|
2022-08-19 18:53:40 +00:00
|
|
|
ALWAYS_INLINE RETURNS_NONNULL T const* ptr() const
|
2019-05-28 09:53:16 +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-05-28 09:53:16 +00:00
|
|
|
}
|
2019-02-25 11:43:52 +00:00
|
|
|
|
2021-06-29 13:45:24 +00:00
|
|
|
ALWAYS_INLINE RETURNS_NONNULL T* operator->()
|
2019-05-28 09:53:16 +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-05-28 09:53:16 +00:00
|
|
|
}
|
2022-08-19 18:53:40 +00:00
|
|
|
ALWAYS_INLINE RETURNS_NONNULL T const* operator->() const
|
2019-05-28 09:53:16 +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-05-28 09:53:16 +00:00
|
|
|
}
|
2019-02-25 11:43:52 +00:00
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE T& operator*()
|
2019-05-28 09:53:16 +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-05-28 09:53:16 +00:00
|
|
|
}
|
2022-08-19 18:53:40 +00:00
|
|
|
ALWAYS_INLINE T const& operator*() const
|
2019-05-28 09:53:16 +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-05-28 09:53:16 +00:00
|
|
|
}
|
2019-02-25 11:43:52 +00:00
|
|
|
|
2021-06-29 13:45:24 +00:00
|
|
|
ALWAYS_INLINE RETURNS_NONNULL operator T*()
|
2019-05-28 09:53:16 +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-05-28 09:53:16 +00:00
|
|
|
}
|
2022-08-19 18:53:40 +00:00
|
|
|
ALWAYS_INLINE RETURNS_NONNULL operator T const*() const
|
2019-05-28 09:53:16 +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-05-28 09:53:16 +00:00
|
|
|
}
|
2019-04-14 00:36:06 +00:00
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE operator T&()
|
2019-06-15 16:45:44 +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-06-15 16:45:44 +00:00
|
|
|
}
|
2022-08-19 18:53:40 +00:00
|
|
|
ALWAYS_INLINE operator T const&() const
|
2019-06-15 16:45:44 +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-06-15 16:45:44 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 17:00:05 +00:00
|
|
|
operator bool() const = delete;
|
|
|
|
bool operator!() const = delete;
|
|
|
|
|
2020-01-18 12:33:44 +00:00
|
|
|
void swap(NonnullRefPtr& other)
|
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
AK::swap(m_ptr, other.m_ptr);
|
2020-01-18 12:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2021-09-03 17:11:51 +00:00
|
|
|
void swap(NonnullRefPtr<U>& other) requires(IsConvertible<U*, T*>)
|
2020-01-18 12:33:44 +00:00
|
|
|
{
|
2021-10-07 17:12:37 +00:00
|
|
|
AK::swap(m_ptr, other.m_ptr);
|
2020-01-18 12:33:44 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:02:09 +00:00
|
|
|
bool operator==(NonnullRefPtr const& other) const { return m_ptr == other.m_ptr; }
|
|
|
|
bool operator!=(NonnullRefPtr const& other) const { return m_ptr != other.m_ptr; }
|
|
|
|
|
|
|
|
bool operator==(NonnullRefPtr& other) { return m_ptr == other.m_ptr; }
|
|
|
|
bool operator!=(NonnullRefPtr& other) { return m_ptr != other.m_ptr; }
|
|
|
|
|
2021-12-01 21:05:13 +00:00
|
|
|
// clang-format off
|
2019-02-25 11:43:52 +00:00
|
|
|
private:
|
2019-06-24 07:58:21 +00:00
|
|
|
NonnullRefPtr() = delete;
|
2021-12-01 21:05:13 +00:00
|
|
|
// clang-format on
|
|
|
|
|
2021-06-29 13:45:24 +00:00
|
|
|
ALWAYS_INLINE RETURNS_NONNULL T* as_nonnull_ptr() const
|
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
|
|
|
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 };
|
2019-02-25 11:43:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2021-04-23 14:46:57 +00:00
|
|
|
inline NonnullRefPtr<T> adopt_ref(T& object)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2019-06-21 16:37:47 +00:00
|
|
|
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, object);
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 19:14:25 +00:00
|
|
|
template<typename T>
|
2022-08-19 18:53:40 +00:00
|
|
|
struct Formatter<NonnullRefPtr<T>> : Formatter<T const*> {
|
2021-11-16 00:15:21 +00:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, NonnullRefPtr<T> const& value)
|
2020-10-04 19:14:25 +00:00
|
|
|
{
|
2022-08-19 18:53:40 +00:00
|
|
|
return Formatter<T const*>::format(builder, value.ptr());
|
2020-10-04 19:14:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-19 09:29:21 +00:00
|
|
|
template<typename T, typename U>
|
2021-09-03 17:11:51 +00:00
|
|
|
inline void swap(NonnullRefPtr<T>& a, NonnullRefPtr<U>& b) requires(IsConvertible<U*, T*>)
|
2020-01-19 09:29:21 +00:00
|
|
|
{
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2021-06-20 08:04:41 +00:00
|
|
|
template<typename T, class... Args>
|
2021-09-02 22:07:29 +00:00
|
|
|
requires(IsConstructible<T, Args...>) inline NonnullRefPtr<T> make_ref_counted(Args&&... args)
|
2021-06-20 08:04:41 +00:00
|
|
|
{
|
|
|
|
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *new T(forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
2021-07-01 08:21:14 +00:00
|
|
|
// FIXME: Remove once P0960R3 is available in Clang.
|
|
|
|
template<typename T, class... Args>
|
2021-09-02 22:07:29 +00:00
|
|
|
inline NonnullRefPtr<T> make_ref_counted(Args&&... args)
|
2021-07-01 08:21:14 +00:00
|
|
|
{
|
|
|
|
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *new T { forward<Args>(args)... });
|
|
|
|
}
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 09:27:12 +00:00
|
|
|
template<typename T>
|
|
|
|
struct Traits<NonnullRefPtr<T>> : public GenericTraits<NonnullRefPtr<T>> {
|
|
|
|
using PeekType = T*;
|
2022-08-19 18:53:40 +00:00
|
|
|
using ConstPeekType = T const*;
|
2022-04-01 17:58:27 +00:00
|
|
|
static unsigned hash(NonnullRefPtr<T> const& p) { return ptr_hash(p.ptr()); }
|
|
|
|
static bool equals(NonnullRefPtr<T> const& a, NonnullRefPtr<T> const& b) { return a.ptr() == b.ptr(); }
|
2021-05-08 09:27:12 +00:00
|
|
|
};
|
|
|
|
|
2021-04-23 14:46:57 +00:00
|
|
|
using AK::adopt_ref;
|
2021-09-02 22:07:29 +00:00
|
|
|
using AK::make_ref_counted;
|
2019-06-21 16:37:47 +00:00
|
|
|
using AK::NonnullRefPtr;
|