1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 15:57:34 +00:00

AK: Add RETURNS_NONNULL attribute and use it

This attribute tells compilers that the pointer returned by a function
is never null, which lets it optimize away null checks in some places.
This seems like a nice addition to `NonnullOwnPtr` and `NonnullRefPtr`.

Using this attribute causes extra UBSan checks to be emitted. To offset
its performance loss, some additional methods were marked ALWAYS_INLINE,
which lets the compiler optimize duplicate checks
This commit is contained in:
Daniel Bertalan 2021-06-29 15:45:24 +02:00 committed by Ali Mohammad Pur
parent 65b2d3add3
commit 23d66fe719
3 changed files with 29 additions and 15 deletions

View file

@ -156,20 +156,20 @@ public:
return *ptr;
}
ALWAYS_INLINE T* ptr()
ALWAYS_INLINE RETURNS_NONNULL T* ptr()
{
return as_nonnull_ptr();
}
ALWAYS_INLINE const T* ptr() const
ALWAYS_INLINE RETURNS_NONNULL const T* ptr() const
{
return as_nonnull_ptr();
}
ALWAYS_INLINE T* operator->()
ALWAYS_INLINE RETURNS_NONNULL T* operator->()
{
return as_nonnull_ptr();
}
ALWAYS_INLINE const T* operator->() const
ALWAYS_INLINE RETURNS_NONNULL const T* operator->() const
{
return as_nonnull_ptr();
}
@ -183,11 +183,11 @@ public:
return *as_nonnull_ptr();
}
ALWAYS_INLINE operator T*()
ALWAYS_INLINE RETURNS_NONNULL operator T*()
{
return as_nonnull_ptr();
}
ALWAYS_INLINE operator const T*() const
ALWAYS_INLINE RETURNS_NONNULL operator const T*() const
{
return as_nonnull_ptr();
}
@ -232,7 +232,7 @@ private:
return (T*)(m_bits.load(AK::MemoryOrder::memory_order_relaxed) & ~(FlatPtr)1);
}
ALWAYS_INLINE T* as_nonnull_ptr() const
ALWAYS_INLINE RETURNS_NONNULL T* as_nonnull_ptr() const
{
T* ptr = (T*)(m_bits.load(AK::MemoryOrder::memory_order_relaxed) & ~(FlatPtr)1);
VERIFY(ptr);