AK: Introduce adopt_own_if_nonnull(..) to aid in Kernel OOM hardening

Unfortunately adopt_own requires a reference, which obviously does not
work well with when attempting to harden against allocation failure.
The adopt_own_if_nonnull() variant will allow you to avoid using bare
pointers, while still allowing you to handle allocation failure.
This commit is contained in:
Brian Gianforcaro 2021-05-12 20:35:08 -07:00 committed by Andreas Kling
parent 6d39b792f0
commit b0fef03e3f
Notes: sideshowbarker 2024-07-18 18:16:03 +09:00

View file

@ -191,6 +191,14 @@ inline void swap(OwnPtr<T>& a, OwnPtr<U>& b)
a.swap(b);
}
template<typename T>
inline OwnPtr<T> adopt_own_if_nonnull(T* object)
{
if (object)
return OwnPtr<T>(object);
return {};
}
template<typename T>
struct Traits<OwnPtr<T>> : public GenericTraits<OwnPtr<T>> {
using PeekType = T*;
@ -201,4 +209,5 @@ struct Traits<OwnPtr<T>> : public GenericTraits<OwnPtr<T>> {
}
using AK::adopt_own_if_nonnull;
using AK::OwnPtr;