mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-02 12:30:31 +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
Notes:
sideshowbarker
2024-07-18 18:16:03 +09:00
Author: https://github.com/bgianfo Commit: https://github.com/SerenityOS/serenity/commit/b0fef03e3f2 Pull-request: https://github.com/SerenityOS/serenity/pull/7026
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);
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
Loading…
Reference in a new issue