mirror of
https://github.com/RGBCube/serenity
synced 2025-06-27 20:22:07 +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:
parent
6d39b792f0
commit
b0fef03e3f
1 changed files with 9 additions and 0 deletions
|
@ -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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue