1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 12:27:36 +00:00

AK: Add Retained<T>, like RetainPtr, but never null.

Also use some Clang attribute wizardry to get a warning for use-after-move.
This commit is contained in:
Andreas Kling 2019-02-25 12:43:52 +01:00
parent 0b957ed2b1
commit 2cfcbdc735
31 changed files with 214 additions and 104 deletions

View file

@ -11,10 +11,10 @@ namespace AK {
template<typename T>
class Buffer : public Retainable<Buffer<T>> {
public:
static RetainPtr<Buffer> create_uninitialized(size_t count);
static RetainPtr<Buffer> copy(const T*, size_t count);
static RetainPtr<Buffer> wrap(T*, size_t count);
static RetainPtr<Buffer> adopt(T*, size_t count);
static Retained<Buffer> create_uninitialized(size_t count);
static Retained<Buffer> copy(const T*, size_t count);
static Retained<Buffer> wrap(T*, size_t count);
static Retained<Buffer> adopt(T*, size_t count);
~Buffer() { clear(); }
@ -105,25 +105,25 @@ inline void Buffer<T>::grow(size_t size)
}
template<typename T>
inline RetainPtr<Buffer<T>> Buffer<T>::create_uninitialized(size_t size)
inline Retained<Buffer<T>> Buffer<T>::create_uninitialized(size_t size)
{
return ::adopt(*new Buffer<T>(size));
}
template<typename T>
inline RetainPtr<Buffer<T>> Buffer<T>::copy(const T* elements, size_t size)
inline Retained<Buffer<T>> Buffer<T>::copy(const T* elements, size_t size)
{
return ::adopt(*new Buffer<T>(elements, size, Copy));
}
template<typename T>
inline RetainPtr<Buffer<T>> Buffer<T>::wrap(T* elements, size_t size)
inline Retained<Buffer<T>> Buffer<T>::wrap(T* elements, size_t size)
{
return ::adopt(*new Buffer<T>(elements, size, Wrap));
}
template<typename T>
inline RetainPtr<Buffer<T>> Buffer<T>::adopt(T* elements, size_t size)
inline Retained<Buffer<T>> Buffer<T>::adopt(T* elements, size_t size)
{
return ::adopt(*new Buffer<T>(elements, size, Adopt));
}

View file

@ -1,23 +1,10 @@
#pragma once
#include "Types.h"
#include <AK/Types.h>
#include <AK/Retained.h>
namespace AK {
template<typename T>
inline void retain_if_not_null(T* ptr)
{
if (ptr)
ptr->retain();
}
template<typename T>
inline void release_if_not_null(T* ptr)
{
if (ptr)
ptr->release();
}
template<typename T>
class RetainPtr {
public:
@ -30,6 +17,7 @@ public:
RetainPtr(AdoptTag, T& object) : m_ptr(&object) { }
RetainPtr(RetainPtr& other) : m_ptr(other.copy_ref().leak_ref()) { }
RetainPtr(RetainPtr&& other) : m_ptr(other.leak_ref()) { }
template<typename U> RetainPtr(Retained<U>&& other) : m_ptr(static_cast<T*>(&other.leak_ref())) { }
template<typename U> RetainPtr(RetainPtr<U>&& other) : m_ptr(static_cast<T*>(other.leak_ref())) { }
RetainPtr(const RetainPtr& other) : m_ptr(const_cast<RetainPtr&>(other).copy_ref().leak_ref()) { }
template<typename U> RetainPtr(const RetainPtr<U>& other) : m_ptr(const_cast<RetainPtr<U>&>(other).copy_ref().leak_ref()) { }
@ -128,14 +116,7 @@ private:
T* m_ptr = nullptr;
};
template<typename T>
inline RetainPtr<T> adopt(T& object)
{
return RetainPtr<T>(RetainPtr<T>::Adopt, object);
}
}
using AK::RetainPtr;
using AK::adopt;

123
AK/Retained.h Normal file
View file

@ -0,0 +1,123 @@
#pragma once
#include <AK/Types.h>
#ifdef __clang__
#define CONSUMABLE(initial_state) __attribute__((consumable(initial_state)))
#define CALLABLE_WHEN(state) __attribute__((callable_when(state)))
#define SET_TYPESTATE(state) __attribute__((set_typestate(state)))
#define RETURN_TYPESTATE(state) __attribute__((return_typestate(state)))
#else
#define CONSUMABLE(initial_state)
#define CALLABLE_WHEN(state)
#define SET_TYPESTATE(state)
#define RETURN_TYPESTATE(state)
#endif
namespace AK {
template<typename T>
inline void retain_if_not_null(T* ptr)
{
if (ptr)
ptr->retain();
}
template<typename T>
inline void release_if_not_null(T* ptr)
{
if (ptr)
ptr->release();
}
template<typename T>
class CONSUMABLE(unconsumed) Retained {
public:
enum AdoptTag { Adopt };
RETURN_TYPESTATE(unconsumed) Retained(T& object) : m_ptr(&object) { m_ptr->retain(); }
RETURN_TYPESTATE(unconsumed) Retained(AdoptTag, T& object) : m_ptr(&object) { }
RETURN_TYPESTATE(unconsumed) Retained(Retained& other) : m_ptr(&other.copy_ref().leak_ref()) { }
RETURN_TYPESTATE(unconsumed) Retained(Retained&& other) : m_ptr(&other.leak_ref()) { }
template<typename U> RETURN_TYPESTATE(unconsumed) Retained(Retained<U>&& other) : m_ptr(static_cast<T*>(&other.leak_ref())) { }
RETURN_TYPESTATE(unconsumed) Retained(const Retained& other) : m_ptr(&const_cast<Retained&>(other).copy_ref().leak_ref()) { }
template<typename U> RETURN_TYPESTATE(unconsumed) Retained(const Retained<U>& other) : m_ptr(&const_cast<Retained<U>&>(other).copy_ref().leak_ref()) { }
~Retained()
{
release_if_not_null(m_ptr);
m_ptr = nullptr;
#ifdef SANITIZE_PTRS
if constexpr(sizeof(T*) == 8)
m_ptr = (T*)(0xb0b0b0b0b0b0b0b0);
else
m_ptr = (T*)(0xb0b0b0b0);
#endif
}
CALLABLE_WHEN(unconsumed) Retained& operator=(Retained&& other)
{
if (this != &other) {
release_if_not_null(m_ptr);
m_ptr = &other.leak_ref();
}
return *this;
}
template<typename U>
CALLABLE_WHEN(unconsumed) Retained& operator=(Retained<U>&& other)
{
if (this != static_cast<void*>(&other)) {
release_if_not_null(m_ptr);
m_ptr = &other.leak_ref();
}
return *this;
}
CALLABLE_WHEN(unconsumed) Retained& operator=(T& object)
{
if (m_ptr != &object)
release_if_not_null(m_ptr);
m_ptr = &object;
m_ptr->retain();
return *this;
}
CALLABLE_WHEN(unconsumed) Retained copy_ref() const
{
return Retained(*m_ptr);
}
CALLABLE_WHEN(unconsumed) SET_TYPESTATE(consumed)
T& leak_ref()
{
ASSERT(m_ptr);
T* leakedPtr = m_ptr;
m_ptr = nullptr;
return *leakedPtr;
}
CALLABLE_WHEN(unconsumed) T* ptr() { ASSERT(m_ptr); return m_ptr; }
CALLABLE_WHEN(unconsumed) const T* ptr() const { ASSERT(m_ptr); return m_ptr; }
CALLABLE_WHEN(unconsumed) T* operator->() { ASSERT(m_ptr); return m_ptr; }
CALLABLE_WHEN(unconsumed) const T* operator->() const { ASSERT(m_ptr); return m_ptr; }
CALLABLE_WHEN(unconsumed) T& operator*() { ASSERT(m_ptr); return *m_ptr; }
CALLABLE_WHEN(unconsumed) const T& operator*() const { ASSERT(m_ptr); return *m_ptr; }
private:
Retained() { }
T* m_ptr { nullptr };
};
template<typename T>
inline Retained<T> adopt(T& object)
{
return Retained<T>(Retained<T>::Adopt, object);
}
}
using AK::Retained;
using AK::adopt;

View file

@ -60,11 +60,18 @@ static inline T ceil_div(T a, U b)
return result;
}
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconsumed"
#endif
template <typename T>
T&& move(T& arg)
{
return static_cast<T&&>(arg);
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
template<typename T>
struct Identity {