mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 18:02:05 -05:00
AK: Add adopt_nonnull_ref_or_enomem
This gets rid of the ENOMEM boilerplate for handling memory allocation failures in the kernel.
This commit is contained in:
parent
f25be94487
commit
134dbe2607
1 changed files with 16 additions and 0 deletions
16
AK/RefPtr.h
16
AK/RefPtr.h
|
@ -16,6 +16,7 @@
|
|||
#ifdef KERNEL
|
||||
# include <Kernel/Arch/x86/Processor.h>
|
||||
# include <Kernel/Arch/x86/ScopedCritical.h>
|
||||
# include <Kernel/KResult.h>
|
||||
#endif
|
||||
|
||||
namespace AK {
|
||||
|
@ -498,9 +499,24 @@ inline RefPtr<T> try_create(Args&&... args)
|
|||
return adopt_ref_if_nonnull(new (nothrow) T { forward<Args>(args)... });
|
||||
}
|
||||
|
||||
#ifdef KERNEL
|
||||
template<typename T>
|
||||
inline Kernel::KResultOr<NonnullRefPtr<T>> 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
|
||||
|
|
Loading…
Add table
Reference in a new issue