From 8269e1a197bc62d0e17f8f7a483dfb62c00c2d46 Mon Sep 17 00:00:00 2001 From: sin-ack Date: Sun, 15 Aug 2021 08:37:33 +0000 Subject: [PATCH] AK: Add adopt_nonnull_own_or_enomem This is basically a complement to adopt_nonnull_ref_or_enomem, and simplifies boilerplate for try_create functions which just return ENOMEM or the object based on whether it was able to allocate. --- AK/OwnPtr.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index ee220a9aaa..51e42db882 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -8,6 +8,9 @@ #include #include +#ifdef KERNEL +# include +#endif namespace AK { @@ -205,6 +208,17 @@ inline OwnPtr adopt_own_if_nonnull(T* object) return {}; } +#ifdef KERNEL +template +inline Kernel::KResultOr> adopt_nonnull_own_or_enomem(T* object) +{ + auto result = adopt_own_if_nonnull(object); + if (!result) + return ENOMEM; + return result.release_nonnull(); +} +#endif + template requires(IsConstructible) inline OwnPtr try_make(Args&&... args) { @@ -232,3 +246,7 @@ struct Traits> : public GenericTraits> { using AK::adopt_own_if_nonnull; using AK::OwnPtr; using AK::try_make; + +#ifdef KERNEL +using AK::adopt_nonnull_own_or_enomem; +#endif