From 134dbe2607fecc597b1ad72642f0f26d92677030 Mon Sep 17 00:00:00 2001 From: sin-ack Date: Sat, 14 Aug 2021 12:33:50 +0000 Subject: [PATCH] AK: Add adopt_nonnull_ref_or_enomem This gets rid of the ENOMEM boilerplate for handling memory allocation failures in the kernel. --- AK/RefPtr.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/AK/RefPtr.h b/AK/RefPtr.h index 1d194a38b36..ea7fa22fea8 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -16,6 +16,7 @@ #ifdef KERNEL # include # include +# include #endif namespace AK { @@ -498,9 +499,24 @@ inline RefPtr try_create(Args&&... args) return adopt_ref_if_nonnull(new (nothrow) T { forward(args)... }); } +#ifdef KERNEL +template +inline Kernel::KResultOr> adopt_nonnull_ref_or_enomem(T* object) +{ + auto result = adopt_ref_if_nonnull(object); + if (!result) + return ENOMEM; + return result.release_nonnull(); +} +#endif + } using AK::adopt_ref_if_nonnull; using AK::RefPtr; using AK::static_ptr_cast; using AK::try_create; + +#ifdef KERNEL +using AK::adopt_nonnull_ref_or_enomem; +#endif