mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-22 09:19:03 +00:00
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:
committed by
Andreas Kling
parent
6d39b792f0
commit
b0fef03e3f
@@ -191,6 +191,14 @@ inline void swap(OwnPtr<T>& a, OwnPtr<U>& b)
|
|||||||
a.swap(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>
|
template<typename T>
|
||||||
struct Traits<OwnPtr<T>> : public GenericTraits<OwnPtr<T>> {
|
struct Traits<OwnPtr<T>> : public GenericTraits<OwnPtr<T>> {
|
||||||
using PeekType = 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;
|
using AK::OwnPtr;
|
||||||
|
|||||||
Reference in New Issue
Block a user