diff options
Diffstat (limited to 'src/core/hle/kernel/object.h')
| -rw-r--r-- | src/core/hle/kernel/object.h | 30 |
1 files changed, 9 insertions, 21 deletions
diff --git a/src/core/hle/kernel/object.h b/src/core/hle/kernel/object.h index a6faeb83b..bbbb4e7cc 100644 --- a/src/core/hle/kernel/object.h +++ b/src/core/hle/kernel/object.h | |||
| @@ -5,10 +5,9 @@ | |||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <atomic> | 7 | #include <atomic> |
| 8 | #include <memory> | ||
| 8 | #include <string> | 9 | #include <string> |
| 9 | 10 | ||
| 10 | #include <boost/smart_ptr/intrusive_ptr.hpp> | ||
| 11 | |||
| 12 | #include "common/common_types.h" | 11 | #include "common/common_types.h" |
| 13 | 12 | ||
| 14 | namespace Kernel { | 13 | namespace Kernel { |
| @@ -32,7 +31,7 @@ enum class HandleType : u32 { | |||
| 32 | ServerSession, | 31 | ServerSession, |
| 33 | }; | 32 | }; |
| 34 | 33 | ||
| 35 | class Object : NonCopyable { | 34 | class Object : NonCopyable, public std::enable_shared_from_this<Object> { |
| 36 | public: | 35 | public: |
| 37 | explicit Object(KernelCore& kernel); | 36 | explicit Object(KernelCore& kernel); |
| 38 | virtual ~Object(); | 37 | virtual ~Object(); |
| @@ -61,35 +60,24 @@ protected: | |||
| 61 | KernelCore& kernel; | 60 | KernelCore& kernel; |
| 62 | 61 | ||
| 63 | private: | 62 | private: |
| 64 | friend void intrusive_ptr_add_ref(Object*); | ||
| 65 | friend void intrusive_ptr_release(Object*); | ||
| 66 | |||
| 67 | std::atomic<u32> ref_count{0}; | ||
| 68 | std::atomic<u32> object_id{0}; | 63 | std::atomic<u32> object_id{0}; |
| 69 | }; | 64 | }; |
| 70 | 65 | ||
| 71 | // Special functions used by boost::instrusive_ptr to do automatic ref-counting | ||
| 72 | inline void intrusive_ptr_add_ref(Object* object) { | ||
| 73 | object->ref_count.fetch_add(1, std::memory_order_relaxed); | ||
| 74 | } | ||
| 75 | |||
| 76 | inline void intrusive_ptr_release(Object* object) { | ||
| 77 | if (object->ref_count.fetch_sub(1, std::memory_order_acq_rel) == 1) { | ||
| 78 | delete object; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | template <typename T> | 66 | template <typename T> |
| 83 | using SharedPtr = boost::intrusive_ptr<T>; | 67 | std::shared_ptr<T> SharedFrom(T* raw) { |
| 68 | if (raw == nullptr) | ||
| 69 | return nullptr; | ||
| 70 | return std::static_pointer_cast<T>(raw->shared_from_this()); | ||
| 71 | } | ||
| 84 | 72 | ||
| 85 | /** | 73 | /** |
| 86 | * Attempts to downcast the given Object pointer to a pointer to T. | 74 | * Attempts to downcast the given Object pointer to a pointer to T. |
| 87 | * @return Derived pointer to the object, or `nullptr` if `object` isn't of type T. | 75 | * @return Derived pointer to the object, or `nullptr` if `object` isn't of type T. |
| 88 | */ | 76 | */ |
| 89 | template <typename T> | 77 | template <typename T> |
| 90 | inline SharedPtr<T> DynamicObjectCast(SharedPtr<Object> object) { | 78 | inline std::shared_ptr<T> DynamicObjectCast(std::shared_ptr<Object> object) { |
| 91 | if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) { | 79 | if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) { |
| 92 | return boost::static_pointer_cast<T>(object); | 80 | return std::static_pointer_cast<T>(object); |
| 93 | } | 81 | } |
| 94 | return nullptr; | 82 | return nullptr; |
| 95 | } | 83 | } |