mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-13 01:40:36 +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
Notes:
sideshowbarker
2024-07-18 05:42:38 +09:00
Author: https://github.com/sin-ack Commit: https://github.com/SerenityOS/serenity/commit/134dbe2607f Pull-request: https://github.com/SerenityOS/serenity/pull/9410 Reviewed-by: https://github.com/awesomekling
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…
Reference in a new issue