2020-01-30 07:06:25 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* 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-31 00:29:51 +01:00
|
|
|
#include <mesosphere/kern_common.hpp>
|
2020-01-30 07:06:25 +01:00
|
|
|
#include <mesosphere/kern_k_typed_address.hpp>
|
|
|
|
#include <mesosphere/kern_k_class_token.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
class KProcess;
|
|
|
|
|
2020-01-30 08:46:55 +01:00
|
|
|
#define MESOSPHERE_AUTOOBJECT_TRAITS(CLASS, BASE_CLASS) \
|
|
|
|
NON_COPYABLE(CLASS); \
|
|
|
|
NON_MOVEABLE(CLASS); \
|
2020-01-30 07:06:25 +01:00
|
|
|
private: \
|
2020-02-07 20:51:58 +01:00
|
|
|
friend class ::ams::kern::KClassTokenGenerator; \
|
2020-01-30 08:46:55 +01:00
|
|
|
static constexpr inline auto ObjectType = ::ams::kern::KClassTokenGenerator::ObjectType::CLASS; \
|
2020-01-30 07:06:25 +01:00
|
|
|
static constexpr inline const char * const TypeName = #CLASS; \
|
2020-01-30 08:46:55 +01:00
|
|
|
static constexpr inline ClassTokenType ClassToken() { return ::ams::kern::ClassToken<CLASS>; } \
|
2020-01-30 07:06:25 +01:00
|
|
|
public: \
|
2020-01-30 08:46:55 +01:00
|
|
|
using BaseClass = BASE_CLASS; \
|
|
|
|
static constexpr ALWAYS_INLINE TypeObj GetStaticTypeObj() { \
|
|
|
|
constexpr ClassTokenType Token = ClassToken(); \
|
|
|
|
return TypeObj(TypeName, Token); \
|
|
|
|
} \
|
2020-01-30 07:06:25 +01:00
|
|
|
static constexpr ALWAYS_INLINE const char *GetStaticTypeName() { return TypeName; } \
|
2020-01-30 08:46:55 +01:00
|
|
|
virtual TypeObj GetTypeObj() const { return GetStaticTypeObj(); } \
|
|
|
|
virtual const char *GetTypeName() { return GetStaticTypeName(); } \
|
2020-01-30 07:06:25 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
|
|
class KAutoObject {
|
|
|
|
protected:
|
|
|
|
class TypeObj {
|
|
|
|
private:
|
2020-12-18 02:18:47 +01:00
|
|
|
const char *m_name;
|
|
|
|
ClassTokenType m_class_token;
|
2020-01-30 07:06:25 +01:00
|
|
|
public:
|
2020-12-18 02:18:47 +01:00
|
|
|
constexpr explicit TypeObj(const char *n, ClassTokenType tok) : m_name(n), m_class_token(tok) { /* ... */ }
|
2020-01-30 07:06:25 +01:00
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
constexpr ALWAYS_INLINE const char *GetName() const { return m_name; }
|
|
|
|
constexpr ALWAYS_INLINE ClassTokenType GetClassToken() const { return m_class_token; }
|
2020-01-30 07:06:25 +01: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) {
|
|
|
|
return (this->GetClassToken() | rhs.GetClassToken()) == this->GetClassToken();
|
|
|
|
}
|
|
|
|
};
|
2020-01-30 08:46:55 +01:00
|
|
|
private:
|
|
|
|
MESOSPHERE_AUTOOBJECT_TRAITS(KAutoObject, KAutoObject);
|
2020-01-30 07:06:25 +01:00
|
|
|
private:
|
2021-04-07 22:38:51 +02:00
|
|
|
KAutoObject *m_next_closed_object;
|
2020-12-18 02:18:47 +01:00
|
|
|
std::atomic<u32> m_ref_count;
|
2020-01-30 07:06:25 +01:00
|
|
|
public:
|
|
|
|
static KAutoObject *Create(KAutoObject *ptr);
|
|
|
|
public:
|
2021-04-07 22:38:51 +02:00
|
|
|
constexpr ALWAYS_INLINE explicit KAutoObject() : m_next_closed_object(nullptr), m_ref_count(0) { MESOSPHERE_ASSERT_THIS(); }
|
2020-01-31 00:29:51 +01:00
|
|
|
virtual ~KAutoObject() { MESOSPHERE_ASSERT_THIS(); }
|
2020-01-30 07:06:25 +01:00
|
|
|
|
|
|
|
/* Destroy is responsible for destroying the auto object's resources when ref_count hits zero. */
|
2020-01-31 00:29:51 +01:00
|
|
|
virtual void Destroy() { MESOSPHERE_ASSERT_THIS(); }
|
2020-01-30 07:06:25 +01:00
|
|
|
|
|
|
|
/* Finalize is responsible for cleaning up resource, but does not destroy the object. */
|
2020-01-31 00:29:51 +01:00
|
|
|
virtual void Finalize() { MESOSPHERE_ASSERT_THIS(); }
|
2020-01-30 07:06:25 +01:00
|
|
|
|
|
|
|
virtual KProcess *GetOwner() const { return nullptr; }
|
|
|
|
|
|
|
|
u32 GetReferenceCount() const {
|
2021-01-08 11:13:36 +01:00
|
|
|
return m_ref_count.load();
|
2020-01-30 07:06:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool IsDerivedFrom(const TypeObj &rhs) const {
|
|
|
|
return this->GetTypeObj().IsDerivedFrom(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool IsDerivedFrom(const KAutoObject &rhs) const {
|
|
|
|
return this->IsDerivedFrom(rhs.GetTypeObj());
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 03:03:27 +02:00
|
|
|
bool Open();
|
|
|
|
void Close();
|
2021-04-07 22:38:51 +02: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; }
|
2020-01-30 07:06:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class KAutoObjectWithListContainer;
|
|
|
|
|
|
|
|
class KAutoObjectWithList : public KAutoObject {
|
|
|
|
private:
|
|
|
|
friend class KAutoObjectWithListContainer;
|
|
|
|
private:
|
|
|
|
util::IntrusiveRedBlackTreeNode list_node;
|
|
|
|
public:
|
|
|
|
static ALWAYS_INLINE int Compare(const KAutoObjectWithList &lhs, const KAutoObjectWithList &rhs) {
|
|
|
|
const u64 lid = lhs.GetId();
|
|
|
|
const u64 rid = rhs.GetId();
|
|
|
|
|
|
|
|
if (lid < rid) {
|
|
|
|
return -1;
|
|
|
|
} else if (lid > rid) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
virtual u64 GetId() const {
|
|
|
|
return reinterpret_cast<u64>(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-09 23:49:51 +02:00
|
|
|
template<typename T> requires std::derived_from<T, KAutoObject>
|
2020-01-30 07:06:25 +01:00
|
|
|
class KScopedAutoObject {
|
|
|
|
NON_COPYABLE(KScopedAutoObject);
|
2020-07-09 23:49:51 +02:00
|
|
|
private:
|
|
|
|
template<typename U>
|
|
|
|
friend class KScopedAutoObject;
|
2020-01-30 07:06:25 +01:00
|
|
|
private:
|
2020-12-18 02:18:47 +01:00
|
|
|
T *m_obj;
|
2020-01-30 07:06:25 +01:00
|
|
|
private:
|
|
|
|
constexpr ALWAYS_INLINE void Swap(KScopedAutoObject &rhs) {
|
2020-12-18 02:18:47 +01:00
|
|
|
std::swap(m_obj, rhs.m_obj);
|
2020-01-30 07:06:25 +01:00
|
|
|
}
|
|
|
|
public:
|
2020-12-18 02:18:47 +01:00
|
|
|
constexpr ALWAYS_INLINE KScopedAutoObject() : m_obj(nullptr) { /* ... */ }
|
|
|
|
constexpr ALWAYS_INLINE KScopedAutoObject(T *o) : m_obj(o) {
|
|
|
|
if (m_obj != nullptr) {
|
|
|
|
m_obj->Open();
|
2020-01-31 00:29:51 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-20 04:38:20 +01:00
|
|
|
|
2021-04-07 22:38:51 +02:00
|
|
|
ALWAYS_INLINE ~KScopedAutoObject() {
|
2020-12-18 02:18:47 +01:00
|
|
|
if (m_obj != nullptr) {
|
|
|
|
m_obj->Close();
|
2020-01-30 07:06:25 +01:00
|
|
|
}
|
2020-12-18 02:18:47 +01:00
|
|
|
m_obj = nullptr;
|
2020-01-30 07:06:25 +01:00
|
|
|
}
|
|
|
|
|
2020-07-17 05:40:59 +02:00
|
|
|
template<typename U> requires (std::derived_from<T, U> || std::derived_from<U, T>)
|
2020-12-02 02:34:09 +01:00
|
|
|
constexpr KScopedAutoObject(KScopedAutoObject<U> &&rhs) {
|
2020-07-17 05:40:59 +02:00
|
|
|
if constexpr (std::derived_from<U, T>) {
|
|
|
|
/* Upcast. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_obj = rhs.m_obj;
|
|
|
|
rhs.m_obj = nullptr;
|
2020-07-09 23:49:51 +02:00
|
|
|
} else {
|
2020-07-17 05:40:59 +02:00
|
|
|
/* Downcast. */
|
|
|
|
T *derived = nullptr;
|
2020-12-18 02:18:47 +01:00
|
|
|
if (rhs.m_obj != nullptr) {
|
|
|
|
derived = rhs.m_obj->template DynamicCast<T *>();
|
2020-07-17 05:40:59 +02:00
|
|
|
if (derived == nullptr) {
|
2020-12-18 02:18:47 +01:00
|
|
|
rhs.m_obj->Close();
|
2020-07-17 05:40:59 +02:00
|
|
|
}
|
2020-07-09 23:49:51 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
m_obj = derived;
|
|
|
|
rhs.m_obj = nullptr;
|
2020-07-09 23:49:51 +02:00
|
|
|
}
|
2020-01-30 07:06:25 +01:00
|
|
|
}
|
|
|
|
|
2020-07-17 05:40:59 +02:00
|
|
|
constexpr ALWAYS_INLINE KScopedAutoObject<T> &operator=(KScopedAutoObject<T> &&rhs) {
|
2020-01-30 07:06:25 +01:00
|
|
|
rhs.Swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
constexpr ALWAYS_INLINE T *operator->() { return m_obj; }
|
|
|
|
constexpr ALWAYS_INLINE T &operator*() { return *m_obj; }
|
2020-01-30 07:06:25 +01:00
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE void Reset(T *o) {
|
|
|
|
KScopedAutoObject(o).Swap(*this);
|
|
|
|
}
|
2020-02-20 18:05:01 +01:00
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
constexpr ALWAYS_INLINE T *GetPointerUnsafe() { return m_obj; }
|
2020-03-14 07:12:47 +01:00
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
constexpr ALWAYS_INLINE T *ReleasePointerUnsafe() { T *ret = m_obj; m_obj = nullptr; return ret; }
|
2020-07-22 12:29:27 +02:00
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
constexpr ALWAYS_INLINE bool IsNull() const { return m_obj == nullptr; }
|
|
|
|
constexpr ALWAYS_INLINE bool IsNotNull() const { return m_obj != nullptr; }
|
2020-01-30 07:06:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|