AK: Add a variant of adopt_nonnull_own_or_enomem() for userspace

This variant returns ErrorOr<NonnullOwnPtr<T>> instead of KResultOr.
Eventually the KResultOr variant should go away once the kernel adopts
Error and ErrorOr<T>.
This commit is contained in:
Andreas Kling 2021-11-07 12:10:16 +01:00
parent 2c70c479ab
commit e841f3c283
Notes: sideshowbarker 2024-07-18 01:23:19 +09:00

View file

@ -10,6 +10,8 @@
#include <AK/RefCounted.h>
#ifdef KERNEL
# include <Kernel/API/KResult.h>
#else
# include <AK/Error.h>
#endif
#define OWNPTR_SCRUB_BYTE 0xf0
@ -216,6 +218,15 @@ inline Kernel::KResultOr<NonnullOwnPtr<T>> adopt_nonnull_own_or_enomem(T* object
return ENOMEM;
return result.release_nonnull();
}
#else
template<typename T>
inline ErrorOr<NonnullOwnPtr<T>> adopt_nonnull_own_or_enomem(T* object)
{
auto result = adopt_own_if_nonnull(object);
if (!result)
return ENOMEM;
return result.release_nonnull();
}
#endif
template<typename T, class... Args>
@ -242,10 +253,7 @@ struct Traits<OwnPtr<T>> : public GenericTraits<OwnPtr<T>> {
}
using AK::adopt_nonnull_own_or_enomem;
using AK::adopt_own_if_nonnull;
using AK::OwnPtr;
using AK::try_make;
#ifdef KERNEL
using AK::adopt_nonnull_own_or_enomem;
#endif