2020-01-30 06:06:25 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-01-30 06:06:25 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
2020-01-30 23:29:51 +00:00
|
|
|
#include <mesosphere/kern_common.hpp>
|
2020-01-30 06:06:25 +00:00
|
|
|
#include <mesosphere/kern_k_typed_address.hpp>
|
|
|
|
#include <mesosphere/kern_k_class_token.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
class KProcess;
|
|
|
|
|
2021-10-23 23:04:04 +00:00
|
|
|
#if defined(MESOSPHERE_BUILD_FOR_DEBUGGING) || defined(MESOSPHERE_BUILD_FOR_AUDITING)
|
|
|
|
#define MESOSPHERE_AUTO_OBJECT_TYPENAME_IMPL(CLASS) #CLASS
|
|
|
|
#else
|
|
|
|
#define MESOSPHERE_AUTO_OBJECT_TYPENAME_IMPL(CLASS) ""
|
|
|
|
#endif
|
|
|
|
|
2020-01-30 07:46:55 +00:00
|
|
|
#define MESOSPHERE_AUTOOBJECT_TRAITS(CLASS, BASE_CLASS) \
|
|
|
|
NON_COPYABLE(CLASS); \
|
|
|
|
NON_MOVEABLE(CLASS); \
|
2020-01-30 06:06:25 +00:00
|
|
|
private: \
|
2020-02-07 19:51:58 +00:00
|
|
|
friend class ::ams::kern::KClassTokenGenerator; \
|
2020-01-30 07:46:55 +00:00
|
|
|
static constexpr inline auto ObjectType = ::ams::kern::KClassTokenGenerator::ObjectType::CLASS; \
|
2021-10-23 23:04:04 +00:00
|
|
|
static constexpr inline const char * const TypeName = MESOSPHERE_AUTO_OBJECT_TYPENAME_IMPL(CLASS); \
|
2020-01-30 07:46:55 +00:00
|
|
|
static constexpr inline ClassTokenType ClassToken() { return ::ams::kern::ClassToken<CLASS>; } \
|
2020-01-30 06:06:25 +00:00
|
|
|
public: \
|
2020-01-30 07:46:55 +00:00
|
|
|
using BaseClass = BASE_CLASS; \
|
2021-10-24 20:04:31 +00:00
|
|
|
static consteval ALWAYS_INLINE TypeObj GetStaticTypeObj() { \
|
2020-01-30 07:46:55 +00:00
|
|
|
constexpr ClassTokenType Token = ClassToken(); \
|
|
|
|
return TypeObj(TypeName, Token); \
|
|
|
|
} \
|
2021-10-24 20:04:31 +00:00
|
|
|
static consteval ALWAYS_INLINE const char *GetStaticTypeName() { return TypeName; } \
|
2020-01-30 07:46:55 +00:00
|
|
|
virtual TypeObj GetTypeObj() const { return GetStaticTypeObj(); } \
|
|
|
|
virtual const char *GetTypeName() { return GetStaticTypeName(); } \
|
2020-01-30 06:06:25 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
class KAutoObject {
|
2021-10-19 22:24:15 +00:00
|
|
|
public:
|
|
|
|
class ReferenceCount {
|
|
|
|
NON_COPYABLE(ReferenceCount);
|
|
|
|
NON_MOVEABLE(ReferenceCount);
|
|
|
|
private:
|
|
|
|
using Storage = u32;
|
|
|
|
private:
|
|
|
|
util::Atomic<Storage> m_value;
|
|
|
|
public:
|
|
|
|
ALWAYS_INLINE explicit ReferenceCount() { /* ... */ }
|
|
|
|
constexpr ALWAYS_INLINE explicit ReferenceCount(Storage v) : m_value(v) { /* ... */ }
|
|
|
|
|
|
|
|
ALWAYS_INLINE void operator=(Storage v) { m_value = v; }
|
|
|
|
|
|
|
|
ALWAYS_INLINE Storage GetValue() const { return m_value.Load(); }
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool Open() {
|
|
|
|
/* Atomically increment the reference count, only if it's positive. */
|
|
|
|
u32 cur = m_value.Load<std::memory_order_relaxed>();
|
|
|
|
do {
|
|
|
|
if (AMS_UNLIKELY(cur == 0)) {
|
|
|
|
MESOSPHERE_AUDIT(cur != 0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MESOSPHERE_ABORT_UNLESS(cur < cur + 1);
|
|
|
|
} while (AMS_UNLIKELY(!m_value.CompareExchangeWeak<std::memory_order_relaxed>(cur, cur + 1)));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool Close() {
|
2023-03-27 17:54:58 +00:00
|
|
|
/* Atomically decrement the reference count, not allowing it to decrement past zero. */
|
2021-10-19 22:24:15 +00:00
|
|
|
u32 cur = m_value.Load<std::memory_order_relaxed>();
|
|
|
|
do {
|
|
|
|
MESOSPHERE_ABORT_UNLESS(cur > 0);
|
|
|
|
} while (AMS_UNLIKELY(!m_value.CompareExchangeWeak<std::memory_order_relaxed>(cur, cur - 1)));
|
|
|
|
|
|
|
|
/* Return whether the object was closed. */
|
|
|
|
return cur - 1 == 0;
|
|
|
|
}
|
|
|
|
};
|
2020-01-30 06:06:25 +00:00
|
|
|
protected:
|
|
|
|
class TypeObj {
|
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
const char *m_name;
|
|
|
|
ClassTokenType m_class_token;
|
2020-01-30 06:06:25 +00:00
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr explicit TypeObj(const char *n, ClassTokenType tok) : m_name(n), m_class_token(tok) { /* ... */ }
|
2020-01-30 06:06:25 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr ALWAYS_INLINE const char *GetName() const { return m_name; }
|
|
|
|
constexpr ALWAYS_INLINE ClassTokenType GetClassToken() const { return m_class_token; }
|
2020-01-30 06:06:25 +00:00
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE bool operator==(const TypeObj &rhs) {
|
|
|
|
return this->GetClassToken() == rhs.GetClassToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE bool operator!=(const TypeObj &rhs) {
|
|
|
|
return this->GetClassToken() != rhs.GetClassToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE bool IsDerivedFrom(const TypeObj &rhs) {
|
2021-10-16 23:24:06 +00:00
|
|
|
return IsClassTokenDerivedFrom(this->GetClassToken(), rhs.GetClassToken());
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr ALWAYS_INLINE bool IsClassTokenDerivedFrom(ClassTokenType derived, ClassTokenType base) {
|
|
|
|
return (derived | base) == derived;
|
2020-01-30 06:06:25 +00:00
|
|
|
}
|
|
|
|
};
|
2020-01-30 07:46:55 +00:00
|
|
|
private:
|
|
|
|
MESOSPHERE_AUTOOBJECT_TRAITS(KAutoObject, KAutoObject);
|
2020-01-30 06:06:25 +00:00
|
|
|
private:
|
2021-04-07 20:38:51 +00:00
|
|
|
KAutoObject *m_next_closed_object;
|
2021-10-19 22:24:15 +00:00
|
|
|
ReferenceCount m_ref_count;
|
2021-10-16 23:24:06 +00:00
|
|
|
#if defined(MESOSPHERE_ENABLE_DEVIRTUALIZED_DYNAMIC_CAST)
|
|
|
|
ClassTokenType m_class_token;
|
|
|
|
#endif
|
2020-01-30 06:06:25 +00:00
|
|
|
public:
|
2021-10-23 22:25:20 +00:00
|
|
|
constexpr ALWAYS_INLINE explicit KAutoObject(util::ConstantInitializeTag) : m_next_closed_object(nullptr), m_ref_count(0)
|
2021-10-16 23:24:06 +00:00
|
|
|
#if defined(MESOSPHERE_ENABLE_DEVIRTUALIZED_DYNAMIC_CAST)
|
2021-10-17 00:17:26 +00:00
|
|
|
, m_class_token(0)
|
2021-10-16 23:24:06 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
}
|
2020-01-30 06:06:25 +00:00
|
|
|
|
2021-10-23 22:25:20 +00:00
|
|
|
ALWAYS_INLINE explicit KAutoObject() : m_ref_count(0) { MESOSPHERE_ASSERT_THIS(); }
|
|
|
|
|
2020-01-30 06:06:25 +00:00
|
|
|
/* Destroy is responsible for destroying the auto object's resources when ref_count hits zero. */
|
2020-01-30 23:29:51 +00:00
|
|
|
virtual void Destroy() { MESOSPHERE_ASSERT_THIS(); }
|
2020-01-30 06:06:25 +00:00
|
|
|
|
|
|
|
/* Finalize is responsible for cleaning up resource, but does not destroy the object. */
|
2021-10-24 04:13:26 +00:00
|
|
|
/* NOTE: This is a virtual function in official kernel, but because everything which uses it */
|
|
|
|
/* is already using CRTP for slab heap, we have devirtualized it for performance gain. */
|
|
|
|
/* virtual void Finalize() { MESOSPHERE_ASSERT_THIS(); } */
|
2020-01-30 06:06:25 +00:00
|
|
|
|
2021-10-24 20:04:31 +00:00
|
|
|
/* NOTE: This is a virtual function which is unused-except-for-debug in Nintendo's kernel. */
|
|
|
|
/* virtual KProcess *GetOwner() const { return nullptr; } */
|
2020-01-30 06:06:25 +00:00
|
|
|
|
|
|
|
u32 GetReferenceCount() const {
|
2021-10-19 22:24:15 +00:00
|
|
|
return m_ref_count.GetValue();
|
2020-01-30 06:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool IsDerivedFrom(const TypeObj &rhs) const {
|
2021-10-16 23:24:06 +00:00
|
|
|
#if defined(MESOSPHERE_ENABLE_DEVIRTUALIZED_DYNAMIC_CAST)
|
|
|
|
return TypeObj::IsClassTokenDerivedFrom(m_class_token, rhs.GetClassToken());
|
|
|
|
#else
|
|
|
|
return this->GetTypeObj().IsDerivedFrom(rhs);
|
|
|
|
#endif
|
2020-01-30 06:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool IsDerivedFrom(const KAutoObject &rhs) const {
|
2021-10-16 23:24:06 +00:00
|
|
|
#if defined(MESOSPHERE_ENABLE_DEVIRTUALIZED_DYNAMIC_CAST)
|
|
|
|
return TypeObj::IsClassTokenDerivedFrom(m_class_token, rhs.m_class_token);
|
|
|
|
#else
|
|
|
|
return this->IsDerivedFrom(rhs.GetTypeObj());
|
|
|
|
#endif
|
2020-01-30 06:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Derived>
|
|
|
|
ALWAYS_INLINE Derived DynamicCast() {
|
|
|
|
static_assert(std::is_pointer<Derived>::value);
|
|
|
|
using DerivedType = typename std::remove_pointer<Derived>::type;
|
|
|
|
|
|
|
|
if (AMS_LIKELY(this->IsDerivedFrom(DerivedType::GetStaticTypeObj()))) {
|
|
|
|
return static_cast<Derived>(this);
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Derived>
|
|
|
|
ALWAYS_INLINE const Derived DynamicCast() const {
|
|
|
|
static_assert(std::is_pointer<Derived>::value);
|
|
|
|
using DerivedType = typename std::remove_pointer<Derived>::type;
|
|
|
|
|
|
|
|
if (AMS_LIKELY(this->IsDerivedFrom(DerivedType::GetStaticTypeObj()))) {
|
|
|
|
return static_cast<Derived>(this);
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 06:58:39 +00:00
|
|
|
MESOSPHERE_ALWAYS_INLINE_IF_RELEASE bool Open() {
|
2021-10-19 22:24:15 +00:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
return m_ref_count.Open();
|
|
|
|
}
|
|
|
|
|
2022-03-23 06:58:39 +00:00
|
|
|
MESOSPHERE_ALWAYS_INLINE_IF_RELEASE void Close() {
|
2021-10-19 22:24:15 +00:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
if (m_ref_count.Close()) {
|
|
|
|
this->ScheduleDestruction();
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 20:38:51 +00:00
|
|
|
private:
|
|
|
|
/* NOTE: This has to be defined *after* KThread is defined. */
|
|
|
|
/* Nintendo seems to handle this by defining Open/Close() in a cpp, but we'd like them to remain in headers. */
|
|
|
|
/* Implementation for this will be inside kern_k_thread.hpp, so it can be ALWAYS_INLINE. */
|
|
|
|
void ScheduleDestruction();
|
|
|
|
public:
|
|
|
|
/* Getter, for KThread. */
|
|
|
|
ALWAYS_INLINE KAutoObject *GetNextClosedObject() { return m_next_closed_object; }
|
2021-10-23 23:04:04 +00:00
|
|
|
public:
|
|
|
|
template<typename Derived> requires (std::derived_from<Derived, KAutoObject>)
|
|
|
|
static ALWAYS_INLINE void Create(typename std::type_identity<Derived>::type *obj) {
|
|
|
|
/* Get auto object pointer. */
|
|
|
|
KAutoObject &auto_object = *static_cast<KAutoObject *>(obj);
|
|
|
|
|
|
|
|
/* If we should, set our class token. */
|
|
|
|
#if defined(MESOSPHERE_ENABLE_DEVIRTUALIZED_DYNAMIC_CAST)
|
|
|
|
{
|
|
|
|
constexpr auto Token = Derived::GetStaticTypeObj().GetClassToken();
|
|
|
|
auto_object.m_class_token = Token;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Initialize reference count to 1. */
|
|
|
|
auto_object.m_ref_count = 1;
|
|
|
|
}
|
2020-01-30 06:06:25 +00:00
|
|
|
};
|
|
|
|
|
2021-10-17 00:17:26 +00:00
|
|
|
class KAutoObjectWithListBase : public KAutoObject {
|
|
|
|
private:
|
2021-10-23 22:25:20 +00:00
|
|
|
void *m_alignment_forcer_unused[0];
|
2021-10-17 00:17:26 +00:00
|
|
|
public:
|
2021-10-23 22:25:20 +00:00
|
|
|
constexpr ALWAYS_INLINE explicit KAutoObjectWithListBase(util::ConstantInitializeTag) : KAutoObject(util::ConstantInitialize), m_alignment_forcer_unused{} { /* ... */ }
|
|
|
|
|
|
|
|
ALWAYS_INLINE explicit KAutoObjectWithListBase() { /* ... */ }
|
2021-10-17 00:17:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class KAutoObjectWithList : public KAutoObjectWithListBase {
|
2020-01-30 06:06:25 +00:00
|
|
|
private:
|
2021-10-25 00:00:05 +00:00
|
|
|
template<typename>
|
2020-01-30 06:06:25 +00:00
|
|
|
friend class KAutoObjectWithListContainer;
|
|
|
|
private:
|
2021-09-17 22:08:13 +00:00
|
|
|
util::IntrusiveRedBlackTreeNode m_list_node;
|
2020-01-30 06:06:25 +00:00
|
|
|
public:
|
2021-10-23 22:25:20 +00:00
|
|
|
constexpr ALWAYS_INLINE KAutoObjectWithList(util::ConstantInitializeTag) : KAutoObjectWithListBase(util::ConstantInitialize), m_list_node(util::ConstantInitialize) { /* ... */ }
|
|
|
|
ALWAYS_INLINE explicit KAutoObjectWithList() { /* ... */ }
|
2021-10-24 04:58:48 +00:00
|
|
|
public:
|
2021-10-25 00:00:05 +00:00
|
|
|
/* NOTE: This is virtual in Nintendo's kernel. */
|
|
|
|
u64 GetId() const;
|
2020-01-30 06:06:25 +00:00
|
|
|
};
|
|
|
|
|
2020-07-09 21:49:51 +00:00
|
|
|
template<typename T> requires std::derived_from<T, KAutoObject>
|
2020-01-30 06:06:25 +00:00
|
|
|
class KScopedAutoObject {
|
|
|
|
NON_COPYABLE(KScopedAutoObject);
|
2020-07-09 21:49:51 +00:00
|
|
|
private:
|
2023-05-07 00:49:36 +00:00
|
|
|
template<typename U> requires std::derived_from<U, KAutoObject>
|
2020-07-09 21:49:51 +00:00
|
|
|
friend class KScopedAutoObject;
|
2020-01-30 06:06:25 +00:00
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
T *m_obj;
|
2020-01-30 06:06:25 +00:00
|
|
|
private:
|
|
|
|
constexpr ALWAYS_INLINE void Swap(KScopedAutoObject &rhs) {
|
2020-12-18 01:18:47 +00:00
|
|
|
std::swap(m_obj, rhs.m_obj);
|
2020-01-30 06:06:25 +00:00
|
|
|
}
|
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr ALWAYS_INLINE KScopedAutoObject(T *o) : m_obj(o) {
|
|
|
|
if (m_obj != nullptr) {
|
|
|
|
m_obj->Open();
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-20 03:38:20 +00:00
|
|
|
|
2021-04-07 20:38:51 +00:00
|
|
|
ALWAYS_INLINE ~KScopedAutoObject() {
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_obj != nullptr) {
|
|
|
|
m_obj->Close();
|
2020-01-30 06:06:25 +00:00
|
|
|
}
|
2020-12-18 01:18:47 +00:00
|
|
|
m_obj = nullptr;
|
2020-01-30 06:06:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 03:40:59 +00:00
|
|
|
template<typename U> requires (std::derived_from<T, U> || std::derived_from<U, T>)
|
2020-12-02 01:34:09 +00:00
|
|
|
constexpr KScopedAutoObject(KScopedAutoObject<U> &&rhs) {
|
2020-07-17 03:40:59 +00:00
|
|
|
if constexpr (std::derived_from<U, T>) {
|
|
|
|
/* Upcast. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_obj = rhs.m_obj;
|
|
|
|
rhs.m_obj = nullptr;
|
2020-07-09 21:49:51 +00:00
|
|
|
} else {
|
2020-07-17 03:40:59 +00:00
|
|
|
/* Downcast. */
|
|
|
|
T *derived = nullptr;
|
2020-12-18 01:18:47 +00:00
|
|
|
if (rhs.m_obj != nullptr) {
|
|
|
|
derived = rhs.m_obj->template DynamicCast<T *>();
|
2020-07-17 03:40:59 +00:00
|
|
|
if (derived == nullptr) {
|
2020-12-18 01:18:47 +00:00
|
|
|
rhs.m_obj->Close();
|
2020-07-17 03:40:59 +00:00
|
|
|
}
|
2020-07-09 21:49:51 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
m_obj = derived;
|
|
|
|
rhs.m_obj = nullptr;
|
2020-07-09 21:49:51 +00:00
|
|
|
}
|
2020-01-30 06:06:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 03:40:59 +00:00
|
|
|
constexpr ALWAYS_INLINE KScopedAutoObject<T> &operator=(KScopedAutoObject<T> &&rhs) {
|
2020-01-30 06:06:25 +00:00
|
|
|
rhs.Swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr ALWAYS_INLINE T *operator->() { return m_obj; }
|
|
|
|
constexpr ALWAYS_INLINE T &operator*() { return *m_obj; }
|
2020-01-30 06:06:25 +00:00
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE void Reset(T *o) {
|
|
|
|
KScopedAutoObject(o).Swap(*this);
|
|
|
|
}
|
2020-02-20 17:05:01 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr ALWAYS_INLINE T *GetPointerUnsafe() { return m_obj; }
|
2020-03-14 06:12:47 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr ALWAYS_INLINE T *ReleasePointerUnsafe() { T *ret = m_obj; m_obj = nullptr; return ret; }
|
2020-07-22 10:29:27 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr ALWAYS_INLINE bool IsNull() const { return m_obj == nullptr; }
|
|
|
|
constexpr ALWAYS_INLINE bool IsNotNull() const { return m_obj != nullptr; }
|
2020-01-30 06:06:25 +00:00
|
|
|
};
|
|
|
|
|
2021-10-19 22:24:15 +00:00
|
|
|
template<typename T> requires std::derived_from<T, KAutoObject>
|
|
|
|
class KSharedAutoObject {
|
|
|
|
private:
|
|
|
|
T *m_object;
|
|
|
|
KAutoObject::ReferenceCount m_ref_count;
|
|
|
|
public:
|
|
|
|
explicit KSharedAutoObject() : m_object(nullptr) { /* ... */ }
|
|
|
|
|
|
|
|
void Attach(T *obj) {
|
|
|
|
MESOSPHERE_ASSERT(m_object == nullptr);
|
|
|
|
|
|
|
|
/* Set our object. */
|
|
|
|
m_object = obj;
|
|
|
|
|
|
|
|
/* Open reference to our object. */
|
|
|
|
m_object->Open();
|
|
|
|
|
|
|
|
/* Set our reference count. */
|
|
|
|
m_ref_count = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Open() {
|
|
|
|
return m_ref_count.Open();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Close() {
|
|
|
|
if (m_ref_count.Close()) {
|
|
|
|
this->Detach();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE T *Get() const {
|
|
|
|
return m_object;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
void Detach() {
|
|
|
|
/* Close our object, if we have one. */
|
|
|
|
if (T * const object = m_object; AMS_LIKELY(object != nullptr)) {
|
2023-11-01 17:25:31 +00:00
|
|
|
/* Set our object to a debug sentinel value, which will cause a crash if accessed. */
|
2021-10-19 22:24:15 +00:00
|
|
|
m_object = reinterpret_cast<T *>(1);
|
|
|
|
|
|
|
|
/* Close reference to our object. */
|
|
|
|
object->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-30 06:06:25 +00:00
|
|
|
|
|
|
|
}
|