AK+Kernel: Add an OOM-fallible try variant make_weak_ptr()

This will allow us to propagate allocation errors that may be raised by
the construction of the WeakLink.
This commit is contained in:
Idan Horowitz 2022-02-13 21:16:21 +02:00 committed by Andreas Kling
parent d6ea6c39a7
commit 98c20b65cc
Notes: sideshowbarker 2024-07-17 18:53:09 +09:00
3 changed files with 28 additions and 14 deletions

View File

@ -151,10 +151,10 @@ private:
template<typename T> template<typename T>
template<typename U> template<typename U>
inline WeakPtr<U> Weakable<T>::make_weak_ptr() const inline ErrorOr<WeakPtr<U>> Weakable<T>::try_make_weak_ptr() const
{ {
if (!m_link) if (!m_link)
m_link = adopt_ref(*new WeakLink(const_cast<T&>(static_cast<T const&>(*this)))); m_link = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) WeakLink(const_cast<T&>(static_cast<T const&>(*this)))));
return WeakPtr<U>(m_link); return WeakPtr<U>(m_link);
} }
@ -168,12 +168,18 @@ struct Formatter<WeakPtr<T>> : Formatter<const T*> {
}; };
template<typename T> template<typename T>
WeakPtr<T> make_weak_ptr_if_nonnull(const T* ptr) ErrorOr<WeakPtr<T>> try_make_weak_ptr_if_nonnull(T const* ptr)
{ {
if (ptr) { if (ptr) {
return ptr->template make_weak_ptr<T>(); return ptr->template try_make_weak_ptr<T>();
} }
return {}; return WeakPtr<T> {};
}
template<typename T>
WeakPtr<T> make_weak_ptr_if_nonnull(T const* ptr)
{
return MUST(try_make_weak_ptr_if_nonnull(ptr));
} }
} }

View File

@ -106,7 +106,9 @@ private:
public: public:
template<typename U = T> template<typename U = T>
WeakPtr<U> make_weak_ptr() const; WeakPtr<U> make_weak_ptr() const { return MUST(try_make_weak_ptr<U>()); }
template<typename U = T>
ErrorOr<WeakPtr<U>> try_make_weak_ptr() const;
protected: protected:
Weakable() = default; Weakable() = default;

View File

@ -177,7 +177,7 @@ private:
template<typename T> template<typename T>
template<typename U> template<typename U>
inline WeakPtr<U> Weakable<T>::make_weak_ptr() const inline ErrorOr<WeakPtr<U>> Weakable<T>::try_make_weak_ptr() const
{ {
if constexpr (IsBaseOf<RefCountedBase, T>) { if constexpr (IsBaseOf<RefCountedBase, T>) {
// Checking m_being_destroyed isn't sufficient when dealing with // Checking m_being_destroyed isn't sufficient when dealing with
@ -187,18 +187,18 @@ inline WeakPtr<U> Weakable<T>::make_weak_ptr() const
// that we prevent the destructor and revoke_weak_ptrs from being // that we prevent the destructor and revoke_weak_ptrs from being
// triggered until we're done. // triggered until we're done.
if (!static_cast<const T*>(this)->try_ref()) if (!static_cast<const T*>(this)->try_ref())
return {}; return WeakPtr<U> {};
} else { } else {
// For non-RefCounted types this means a weak reference can be // For non-RefCounted types this means a weak reference can be
// obtained until the ~Weakable destructor is invoked! // obtained until the ~Weakable destructor is invoked!
if (m_being_destroyed.load(AK::MemoryOrder::memory_order_acquire)) if (m_being_destroyed.load(AK::MemoryOrder::memory_order_acquire))
return {}; return WeakPtr<U> {};
} }
if (!m_link) { if (!m_link) {
// There is a small chance that we create a new WeakLink and throw // There is a small chance that we create a new WeakLink and throw
// it away because another thread beat us to it. But the window is // it away because another thread beat us to it. But the window is
// pretty small and the overhead isn't terrible. // pretty small and the overhead isn't terrible.
m_link.assign_if_null(adopt_ref(*new WeakLink(const_cast<T&>(static_cast<const T&>(*this))))); m_link.assign_if_null(TRY(adopt_nonnull_ref_or_enomem(new (nothrow) WeakLink(const_cast<T&>(static_cast<const T&>(*this))))));
} }
WeakPtr<U> weak_ptr(m_link); WeakPtr<U> weak_ptr(m_link);
@ -209,7 +209,7 @@ inline WeakPtr<U> Weakable<T>::make_weak_ptr() const
// We just dropped the last reference, which should have called // We just dropped the last reference, which should have called
// revoke_weak_ptrs, which should have invalidated our weak_ptr // revoke_weak_ptrs, which should have invalidated our weak_ptr
VERIFY(!weak_ptr.strong_ref()); VERIFY(!weak_ptr.strong_ref());
return {}; return WeakPtr<U> {};
} }
} }
return weak_ptr; return weak_ptr;
@ -229,12 +229,18 @@ struct Formatter<WeakPtr<T>> : Formatter<const T*> {
}; };
template<typename T> template<typename T>
WeakPtr<T> make_weak_ptr_if_nonnull(const T* ptr) ErrorOr<WeakPtr<T>> try_make_weak_ptr_if_nonnull(T const* ptr)
{ {
if (ptr) { if (ptr) {
return ptr->template make_weak_ptr<T>(); return ptr->template try_make_weak_ptr<T>();
} }
return {}; return WeakPtr<T> {};
}
template<typename T>
WeakPtr<T> make_weak_ptr_if_nonnull(T const* ptr)
{
return MUST(try_make_weak_ptr_if_nonnull(ptr));
} }
} }