kernel: Replace usage of boost::intrusive_ptr with std::shared_ptr for kernel objects. (#3154)

* kernel: Replace usage of boost::intrusive_ptr with std::shared_ptr for kernel objects.

- See https://github.com/citra-emu/citra/pull/4710 for details.
This commit is contained in:
bunnei 2019-11-24 20:15:51 -05:00 committed by GitHub
parent b03242067d
commit 9046d4a548
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
74 changed files with 377 additions and 378 deletions

View file

@ -5,10 +5,9 @@
#pragma once
#include <atomic>
#include <memory>
#include <string>
#include <boost/smart_ptr/intrusive_ptr.hpp>
#include "common/common_types.h"
namespace Kernel {
@ -32,7 +31,7 @@ enum class HandleType : u32 {
ServerSession,
};
class Object : NonCopyable {
class Object : NonCopyable, public std::enable_shared_from_this<Object> {
public:
explicit Object(KernelCore& kernel);
virtual ~Object();
@ -61,35 +60,24 @@ protected:
KernelCore& kernel;
private:
friend void intrusive_ptr_add_ref(Object*);
friend void intrusive_ptr_release(Object*);
std::atomic<u32> ref_count{0};
std::atomic<u32> object_id{0};
};
// Special functions used by boost::instrusive_ptr to do automatic ref-counting
inline void intrusive_ptr_add_ref(Object* object) {
object->ref_count.fetch_add(1, std::memory_order_relaxed);
}
inline void intrusive_ptr_release(Object* object) {
if (object->ref_count.fetch_sub(1, std::memory_order_acq_rel) == 1) {
delete object;
}
}
template <typename T>
using SharedPtr = boost::intrusive_ptr<T>;
std::shared_ptr<T> SharedFrom(T* raw) {
if (raw == nullptr)
return nullptr;
return std::static_pointer_cast<T>(raw->shared_from_this());
}
/**
* Attempts to downcast the given Object pointer to a pointer to T.
* @return Derived pointer to the object, or `nullptr` if `object` isn't of type T.
*/
template <typename T>
inline SharedPtr<T> DynamicObjectCast(SharedPtr<Object> object) {
inline std::shared_ptr<T> DynamicObjectCast(std::shared_ptr<Object> object) {
if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) {
return boost::static_pointer_cast<T>(object);
return std::static_pointer_cast<T>(object);
}
return nullptr;
}