AK: Add adopt_nonnull_ref_or_enomem() for userspace

We already had this mechanism in the kernel. Let's have it in userspace
as well. This return an ErrorOr<NonnullRefPt<T>>. :^)
This commit is contained in:
Andreas Kling 2021-11-06 10:14:14 +01:00
parent e2eabb4132
commit 56992f90b7
Notes: sideshowbarker 2024-07-18 01:25:11 +09:00

View file

@ -14,6 +14,7 @@
# include <AK/Assertions.h> # include <AK/Assertions.h>
# include <AK/Atomic.h> # include <AK/Atomic.h>
# include <AK/Error.h>
# include <AK/Format.h> # include <AK/Format.h>
# include <AK/NonnullRefPtr.h> # include <AK/NonnullRefPtr.h>
# include <AK/StdLibExtras.h> # include <AK/StdLibExtras.h>
@ -347,6 +348,15 @@ inline RefPtr<T> try_make_ref_counted(Args&&... args)
return adopt_ref_if_nonnull(new (nothrow) T { forward<Args>(args)... }); return adopt_ref_if_nonnull(new (nothrow) T { forward<Args>(args)... });
} }
template<typename T>
inline ErrorOr<NonnullRefPtr<T>> adopt_nonnull_ref_or_enomem(T* object)
{
auto result = adopt_ref_if_nonnull(object);
if (!result)
return ENOMEM;
return result.release_nonnull();
}
} }
using AK::adopt_ref_if_nonnull; using AK::adopt_ref_if_nonnull;