mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 10:57:34 +00: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
|
#ifdef KERNEL
|
||||||
# include <Kernel/Arch/x86/Processor.h>
|
# include <Kernel/Arch/x86/Processor.h>
|
||||||
# include <Kernel/Arch/x86/ScopedCritical.h>
|
# include <Kernel/Arch/x86/ScopedCritical.h>
|
||||||
|
# include <Kernel/KResult.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
@ -498,9 +499,24 @@ inline RefPtr<T> try_create(Args&&... args)
|
||||||
return adopt_ref_if_nonnull(new (nothrow) T { forward<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::adopt_ref_if_nonnull;
|
||||||
using AK::RefPtr;
|
using AK::RefPtr;
|
||||||
using AK::static_ptr_cast;
|
using AK::static_ptr_cast;
|
||||||
using AK::try_create;
|
using AK::try_create;
|
||||||
|
|
||||||
|
#ifdef KERNEL
|
||||||
|
using AK::adopt_nonnull_ref_or_enomem;
|
||||||
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue