diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-06 10:14:14 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-08 00:35:27 +0100 |
commit | 56992f90b7908d6665a9581e28452462ba563d2f (patch) | |
tree | 9dd56e64762d7aa96b8b84857d2ecf63df772eed /AK | |
parent | e2eabb41321bbac5d9b2aca1930f734379e490ab (diff) | |
download | serenity-56992f90b7908d6665a9581e28452462ba563d2f.zip |
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>>. :^)
Diffstat (limited to 'AK')
-rw-r--r-- | AK/RefPtr.h | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/AK/RefPtr.h b/AK/RefPtr.h index 2df9109987..7c56b287e8 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -14,6 +14,7 @@ # include <AK/Assertions.h> # include <AK/Atomic.h> +# include <AK/Error.h> # include <AK/Format.h> # include <AK/NonnullRefPtr.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)... }); } +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; |