mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 08:17:34 +00:00
Kernel: Make self-contained locking smart pointers their own classes
Until now, our kernel has reimplemented a number of AK classes to provide automatic internal locking: - RefPtr - NonnullRefPtr - WeakPtr - Weakable This patch renames the Kernel classes so that they can coexist with the original AK classes: - RefPtr => LockRefPtr - NonnullRefPtr => NonnullLockRefPtr - WeakPtr => LockWeakPtr - Weakable => LockWeakable The goal here is to eventually get rid of the Lock* classes in favor of using external locking.
This commit is contained in:
parent
e475263113
commit
11eee67b85
360 changed files with 1703 additions and 1672 deletions
29
AK/Forward.h
29
AK/Forward.h
|
@ -55,7 +55,7 @@ struct Array;
|
|||
template<typename Container, typename ValueType>
|
||||
class SimpleIterator;
|
||||
|
||||
using ReadonlyBytes = Span<const u8>;
|
||||
using ReadonlyBytes = Span<u8 const>;
|
||||
using Bytes = Span<u8>;
|
||||
|
||||
template<typename T, AK::MemoryOrder DefaultMemoryOrder>
|
||||
|
@ -107,24 +107,30 @@ template<typename T>
|
|||
class NonnullOwnPtr;
|
||||
|
||||
template<typename T, size_t inline_capacity = 0>
|
||||
class NonnullRefPtrVector;
|
||||
class NonnullOwnPtrVector;
|
||||
|
||||
template<typename T, size_t inline_capacity = 0>
|
||||
class NonnullOwnPtrVector;
|
||||
class NonnullRefPtrVector;
|
||||
|
||||
template<typename T>
|
||||
class Optional;
|
||||
|
||||
#ifdef KERNEL
|
||||
template<typename T>
|
||||
struct RefPtrTraits;
|
||||
class NonnullLockRefPtr;
|
||||
|
||||
template<typename T, size_t inline_capacity = 0>
|
||||
class NonnullLockRefPtrVector;
|
||||
|
||||
template<typename T>
|
||||
struct LockRefPtrTraits;
|
||||
|
||||
template<typename T, typename PtrTraits = LockRefPtrTraits<T>>
|
||||
class LockRefPtr;
|
||||
#endif
|
||||
|
||||
template<typename T, typename PtrTraits = RefPtrTraits<T>>
|
||||
class RefPtr;
|
||||
#else
|
||||
template<typename T>
|
||||
class RefPtr;
|
||||
#endif
|
||||
|
||||
template<typename T>
|
||||
class OwnPtr;
|
||||
|
@ -192,3 +198,10 @@ using AK::Utf32View;
|
|||
using AK::Utf8CodePointIterator;
|
||||
using AK::Utf8View;
|
||||
using AK::Vector;
|
||||
|
||||
#ifdef KERNEL
|
||||
using AK::LockRefPtr;
|
||||
using AK::LockRefPtrTraits;
|
||||
using AK::NonnullLockRefPtr;
|
||||
using AK::NonnullLockRefPtrVector;
|
||||
#endif
|
||||
|
|
|
@ -8,6 +8,9 @@
|
|||
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
|
||||
#ifdef KERNEL
|
||||
# include <Kernel/Library/LockRefPtr.h>
|
||||
#endif
|
||||
namespace AK::Detail {
|
||||
|
||||
template<typename T, typename Container>
|
||||
|
@ -20,6 +23,13 @@ struct SubstituteIntrusiveContainerType<T, NonnullRefPtr<T>> {
|
|||
using Type = RefPtr<T>;
|
||||
};
|
||||
|
||||
#ifdef KERNEL
|
||||
template<typename T>
|
||||
struct SubstituteIntrusiveContainerType<T, NonnullLockRefPtr<T>> {
|
||||
using Type = LockRefPtr<T>;
|
||||
};
|
||||
#endif
|
||||
|
||||
template<typename Container, bool _IsRaw>
|
||||
struct SelfReferenceIfNeeded {
|
||||
Container reference = nullptr;
|
||||
|
|
|
@ -13,6 +13,10 @@
|
|||
#include <AK/Noncopyable.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
|
||||
#ifdef KERNEL
|
||||
# include <Kernel/Library/LockRefPtr.h>
|
||||
#endif
|
||||
|
||||
namespace AK::Detail {
|
||||
|
||||
template<typename T, typename Container = RawPtr<T>>
|
||||
|
@ -56,7 +60,7 @@ public:
|
|||
void prepend(T& n);
|
||||
void insert_before(T&, T&);
|
||||
void remove(T& n);
|
||||
[[nodiscard]] bool contains(const T&) const;
|
||||
[[nodiscard]] bool contains(T const&) const;
|
||||
[[nodiscard]] Container first() const;
|
||||
[[nodiscard]] Container last() const;
|
||||
|
||||
|
@ -71,7 +75,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
const T& operator*() const { return *m_value; }
|
||||
T const& operator*() const { return *m_value; }
|
||||
auto operator->() const { return m_value; }
|
||||
T& operator*() { return *m_value; }
|
||||
auto operator->() { return m_value; }
|
||||
|
@ -99,7 +103,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
const T& operator*() const { return *m_value; }
|
||||
T const& operator*() const { return *m_value; }
|
||||
auto operator->() const { return m_value; }
|
||||
T& operator*() { return *m_value; }
|
||||
auto operator->() { return m_value; }
|
||||
|
@ -122,12 +126,12 @@ public:
|
|||
class ConstIterator {
|
||||
public:
|
||||
ConstIterator() = default;
|
||||
ConstIterator(const T* value)
|
||||
ConstIterator(T const* value)
|
||||
: m_value(value)
|
||||
{
|
||||
}
|
||||
|
||||
const T& operator*() const { return *m_value; }
|
||||
T const& operator*() const { return *m_value; }
|
||||
auto operator->() const { return m_value; }
|
||||
bool operator==(ConstIterator const& other) const { return other.m_value == m_value; }
|
||||
bool operator!=(ConstIterator const& other) const { return !(*this == other); }
|
||||
|
@ -138,7 +142,7 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
const T* m_value { nullptr };
|
||||
T const* m_value { nullptr };
|
||||
};
|
||||
|
||||
ConstIterator begin() const;
|
||||
|
@ -147,8 +151,8 @@ public:
|
|||
private:
|
||||
static T* next(T* current);
|
||||
static T* prev(T* current);
|
||||
static const T* next(const T* current);
|
||||
static const T* prev(const T* current);
|
||||
static T const* next(T const* current);
|
||||
static T const* prev(T const* current);
|
||||
static T* node_to_value(SubstitutedIntrusiveListNode<T, Container>& node);
|
||||
IntrusiveListStorage<T, Container> m_storage;
|
||||
};
|
||||
|
@ -284,7 +288,7 @@ inline void IntrusiveList<T, Container, member>::remove(T& n)
|
|||
}
|
||||
|
||||
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
||||
inline bool IntrusiveList<T, Container, member>::contains(const T& n) const
|
||||
inline bool IntrusiveList<T, Container, member>::contains(T const& n) const
|
||||
{
|
||||
auto& nnode = n.*member;
|
||||
return nnode.m_storage == &m_storage;
|
||||
|
@ -323,18 +327,18 @@ inline Container IntrusiveList<T, Container, member>::last() const
|
|||
}
|
||||
|
||||
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
||||
inline const T* IntrusiveList<T, Container, member>::next(const T* current)
|
||||
inline T const* IntrusiveList<T, Container, member>::next(T const* current)
|
||||
{
|
||||
auto& nextnode = (current->*member).m_next;
|
||||
const T* nextstruct = nextnode ? node_to_value(*nextnode) : nullptr;
|
||||
T const* nextstruct = nextnode ? node_to_value(*nextnode) : nullptr;
|
||||
return nextstruct;
|
||||
}
|
||||
|
||||
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
||||
inline const T* IntrusiveList<T, Container, member>::prev(const T* current)
|
||||
inline T const* IntrusiveList<T, Container, member>::prev(T const* current)
|
||||
{
|
||||
auto& prevnode = (current->*member).m_prev;
|
||||
const T* prevstruct = prevnode ? node_to_value(*prevnode) : nullptr;
|
||||
T const* prevstruct = prevnode ? node_to_value(*prevnode) : nullptr;
|
||||
return prevstruct;
|
||||
}
|
||||
|
||||
|
@ -429,6 +433,22 @@ public:
|
|||
[[nodiscard]] NonnullRefPtr<T> take_last() { return *IntrusiveList<T, RefPtr<T>, member>::take_last(); }
|
||||
};
|
||||
|
||||
#ifdef KERNEL
|
||||
// Specialise IntrusiveList for NonnullLockRefPtr
|
||||
// By default, intrusive lists cannot contain null entries anyway, so switch to LockRefPtr
|
||||
// and just make the user-facing functions deref the pointers.
|
||||
|
||||
template<class T, SubstitutedIntrusiveListNode<T, NonnullLockRefPtr<T>> T::*member>
|
||||
class IntrusiveList<T, NonnullLockRefPtr<T>, member> : public IntrusiveList<T, LockRefPtr<T>, member> {
|
||||
public:
|
||||
[[nodiscard]] NonnullLockRefPtr<T> first() const { return *IntrusiveList<T, LockRefPtr<T>, member>::first(); }
|
||||
[[nodiscard]] NonnullLockRefPtr<T> last() const { return *IntrusiveList<T, LockRefPtr<T>, member>::last(); }
|
||||
|
||||
[[nodiscard]] NonnullLockRefPtr<T> take_first() { return *IntrusiveList<T, LockRefPtr<T>, member>::take_first(); }
|
||||
[[nodiscard]] NonnullLockRefPtr<T> take_last() { return *IntrusiveList<T, LockRefPtr<T>, member>::take_last(); }
|
||||
};
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
namespace AK {
|
||||
|
|
|
@ -8,14 +8,11 @@
|
|||
|
||||
#define NONNULLREFPTR_SCRUB_BYTE 0xe1
|
||||
|
||||
#ifdef KERNEL
|
||||
# include <Kernel/Library/ThreadSafeNonnullRefPtr.h>
|
||||
#else
|
||||
# include <AK/Assertions.h>
|
||||
# include <AK/Atomic.h>
|
||||
# include <AK/Format.h>
|
||||
# include <AK/Traits.h>
|
||||
# include <AK/Types.h>
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/Atomic.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/Traits.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
@ -98,9 +95,9 @@ public:
|
|||
{
|
||||
unref_if_not_null(m_ptr);
|
||||
m_ptr = nullptr;
|
||||
# ifdef SANITIZE_PTRS
|
||||
#ifdef SANITIZE_PTRS
|
||||
m_ptr = reinterpret_cast<T*>(explode_byte(NONNULLREFPTR_SCRUB_BYTE));
|
||||
# endif
|
||||
#endif
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
|
@ -163,7 +160,7 @@ public:
|
|||
{
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
ALWAYS_INLINE RETURNS_NONNULL const T* ptr() const
|
||||
ALWAYS_INLINE RETURNS_NONNULL T const* ptr() const
|
||||
{
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
|
@ -172,7 +169,7 @@ public:
|
|||
{
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
ALWAYS_INLINE RETURNS_NONNULL const T* operator->() const
|
||||
ALWAYS_INLINE RETURNS_NONNULL T const* operator->() const
|
||||
{
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
|
@ -181,7 +178,7 @@ public:
|
|||
{
|
||||
return *as_nonnull_ptr();
|
||||
}
|
||||
ALWAYS_INLINE const T& operator*() const
|
||||
ALWAYS_INLINE T const& operator*() const
|
||||
{
|
||||
return *as_nonnull_ptr();
|
||||
}
|
||||
|
@ -190,7 +187,7 @@ public:
|
|||
{
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
ALWAYS_INLINE RETURNS_NONNULL operator const T*() const
|
||||
ALWAYS_INLINE RETURNS_NONNULL operator T const*() const
|
||||
{
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
|
@ -199,7 +196,7 @@ public:
|
|||
{
|
||||
return *as_nonnull_ptr();
|
||||
}
|
||||
ALWAYS_INLINE operator const T&() const
|
||||
ALWAYS_INLINE operator T const&() const
|
||||
{
|
||||
return *as_nonnull_ptr();
|
||||
}
|
||||
|
@ -245,10 +242,10 @@ inline NonnullRefPtr<T> adopt_ref(T& object)
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
struct Formatter<NonnullRefPtr<T>> : Formatter<const T*> {
|
||||
struct Formatter<NonnullRefPtr<T>> : Formatter<T const*> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, NonnullRefPtr<T> const& value)
|
||||
{
|
||||
return Formatter<const T*>::format(builder, value.ptr());
|
||||
return Formatter<T const*>::format(builder, value.ptr());
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -275,7 +272,7 @@ inline NonnullRefPtr<T> make_ref_counted(Args&&... args)
|
|||
template<typename T>
|
||||
struct Traits<NonnullRefPtr<T>> : public GenericTraits<NonnullRefPtr<T>> {
|
||||
using PeekType = T*;
|
||||
using ConstPeekType = const T*;
|
||||
using ConstPeekType = T const*;
|
||||
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(); }
|
||||
};
|
||||
|
@ -283,5 +280,3 @@ struct Traits<NonnullRefPtr<T>> : public GenericTraits<NonnullRefPtr<T>> {
|
|||
using AK::adopt_ref;
|
||||
using AK::make_ref_counted;
|
||||
using AK::NonnullRefPtr;
|
||||
|
||||
#endif
|
||||
|
|
48
AK/RefPtr.h
48
AK/RefPtr.h
|
@ -8,18 +8,14 @@
|
|||
|
||||
#define REFPTR_SCRUB_BYTE 0xe0
|
||||
|
||||
#ifdef KERNEL
|
||||
# include <Kernel/Library/ThreadSafeRefPtr.h>
|
||||
#else
|
||||
|
||||
# include <AK/Assertions.h>
|
||||
# include <AK/Atomic.h>
|
||||
# include <AK/Error.h>
|
||||
# include <AK/Format.h>
|
||||
# include <AK/NonnullRefPtr.h>
|
||||
# include <AK/StdLibExtras.h>
|
||||
# include <AK/Traits.h>
|
||||
# include <AK/Types.h>
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/Atomic.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <AK/Traits.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
@ -104,9 +100,9 @@ public:
|
|||
ALWAYS_INLINE ~RefPtr()
|
||||
{
|
||||
clear();
|
||||
# ifdef SANITIZE_PTRS
|
||||
#ifdef SANITIZE_PTRS
|
||||
m_ptr = reinterpret_cast<T*>(explode_byte(REFPTR_SCRUB_BYTE));
|
||||
# endif
|
||||
#endif
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
|
@ -236,14 +232,14 @@ public:
|
|||
}
|
||||
|
||||
ALWAYS_INLINE T* ptr() { return as_ptr(); }
|
||||
ALWAYS_INLINE const T* ptr() const { return as_ptr(); }
|
||||
ALWAYS_INLINE T const* ptr() const { return as_ptr(); }
|
||||
|
||||
ALWAYS_INLINE T* operator->()
|
||||
{
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE const T* operator->() const
|
||||
ALWAYS_INLINE T const* operator->() const
|
||||
{
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
|
@ -253,12 +249,12 @@ public:
|
|||
return *as_nonnull_ptr();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE const T& operator*() const
|
||||
ALWAYS_INLINE T const& operator*() const
|
||||
{
|
||||
return *as_nonnull_ptr();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE operator const T*() const { return as_ptr(); }
|
||||
ALWAYS_INLINE operator T const*() const { return as_ptr(); }
|
||||
ALWAYS_INLINE operator T*() { return as_ptr(); }
|
||||
|
||||
ALWAYS_INLINE operator bool() { return !is_null(); }
|
||||
|
@ -282,8 +278,8 @@ public:
|
|||
template<typename U>
|
||||
bool operator!=(NonnullRefPtr<U>& other) { return as_ptr() != other.m_ptr; }
|
||||
|
||||
bool operator==(const T* other) const { return as_ptr() == other; }
|
||||
bool operator!=(const T* other) const { return as_ptr() != other; }
|
||||
bool operator==(T const* other) const { return as_ptr() == other; }
|
||||
bool operator!=(T const* other) const { return as_ptr() != other; }
|
||||
|
||||
bool operator==(T* other) { return as_ptr() == other; }
|
||||
bool operator!=(T* other) { return as_ptr() != other; }
|
||||
|
@ -306,17 +302,17 @@ private:
|
|||
};
|
||||
|
||||
template<typename T>
|
||||
struct Formatter<RefPtr<T>> : Formatter<const T*> {
|
||||
struct Formatter<RefPtr<T>> : Formatter<T const*> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, RefPtr<T> const& value)
|
||||
{
|
||||
return Formatter<const T*>::format(builder, value.ptr());
|
||||
return Formatter<T const*>::format(builder, value.ptr());
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Traits<RefPtr<T>> : public GenericTraits<RefPtr<T>> {
|
||||
using PeekType = T*;
|
||||
using ConstPeekType = const T*;
|
||||
using ConstPeekType = T const*;
|
||||
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(); }
|
||||
};
|
||||
|
@ -324,13 +320,13 @@ struct Traits<RefPtr<T>> : public GenericTraits<RefPtr<T>> {
|
|||
template<typename T, typename U>
|
||||
inline NonnullRefPtr<T> static_ptr_cast(NonnullRefPtr<U> const& ptr)
|
||||
{
|
||||
return NonnullRefPtr<T>(static_cast<const T&>(*ptr));
|
||||
return NonnullRefPtr<T>(static_cast<T const&>(*ptr));
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
inline RefPtr<T> static_ptr_cast(RefPtr<U> const& ptr)
|
||||
{
|
||||
return RefPtr<T>(static_cast<const T*>(ptr.ptr()));
|
||||
return RefPtr<T>(static_cast<T const*>(ptr.ptr()));
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
|
@ -375,5 +371,3 @@ using AK::adopt_ref_if_nonnull;
|
|||
using AK::RefPtr;
|
||||
using AK::static_ptr_cast;
|
||||
using AK::try_make_ref_counted;
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,11 +6,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#ifdef KERNEL
|
||||
# include <Kernel/Library/ThreadSafeWeakPtr.h>
|
||||
#else
|
||||
|
||||
# include <AK/Weakable.h>
|
||||
#include <AK/Weakable.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
@ -185,4 +181,3 @@ WeakPtr<T> make_weak_ptr_if_nonnull(T const* ptr)
|
|||
}
|
||||
|
||||
using AK::WeakPtr;
|
||||
#endif
|
||||
|
|
|
@ -6,15 +6,12 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#ifdef KERNEL
|
||||
# include <Kernel/Library/ThreadSafeWeakable.h>
|
||||
#else
|
||||
# include <AK/Assertions.h>
|
||||
# include <AK/Atomic.h>
|
||||
# include <AK/RefCounted.h>
|
||||
# include <AK/RefPtr.h>
|
||||
# include <AK/StdLibExtras.h>
|
||||
# include <sched.h>
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/Atomic.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <sched.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
@ -125,5 +122,3 @@ private:
|
|||
}
|
||||
|
||||
using AK::Weakable;
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue