diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_auto_object.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_auto_object.hpp index 32b7259de..16f1399df 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_auto_object.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_auto_object.hpp @@ -22,13 +22,19 @@ namespace ams::kern { class KProcess; + #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 + #define MESOSPHERE_AUTOOBJECT_TRAITS(CLASS, BASE_CLASS) \ NON_COPYABLE(CLASS); \ NON_MOVEABLE(CLASS); \ private: \ friend class ::ams::kern::KClassTokenGenerator; \ static constexpr inline auto ObjectType = ::ams::kern::KClassTokenGenerator::ObjectType::CLASS; \ - static constexpr inline const char * const TypeName = #CLASS; \ + static constexpr inline const char * const TypeName = MESOSPHERE_AUTO_OBJECT_TYPENAME_IMPL(CLASS); \ static constexpr inline ClassTokenType ClassToken() { return ::ams::kern::ClassToken; } \ public: \ using BaseClass = BASE_CLASS; \ @@ -118,8 +124,6 @@ namespace ams::kern { #if defined(MESOSPHERE_ENABLE_DEVIRTUALIZED_DYNAMIC_CAST) ClassTokenType m_class_token; #endif - public: - static KAutoObject *Create(KAutoObject *ptr); public: constexpr ALWAYS_INLINE explicit KAutoObject(util::ConstantInitializeTag) : m_next_closed_object(nullptr), m_ref_count(0) #if defined(MESOSPHERE_ENABLE_DEVIRTUALIZED_DYNAMIC_CAST) @@ -204,6 +208,23 @@ namespace ams::kern { public: /* Getter, for KThread. */ ALWAYS_INLINE KAutoObject *GetNextClosedObject() { return m_next_closed_object; } + public: + template requires (std::derived_from) + static ALWAYS_INLINE void Create(typename std::type_identity::type *obj) { + /* Get auto object pointer. */ + KAutoObject &auto_object = *static_cast(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; + } }; class KAutoObjectWithListContainer; diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_session_request.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_session_request.hpp index 33fa7effa..f688586fa 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_session_request.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_session_request.hpp @@ -137,7 +137,7 @@ namespace ams::kern { static KSessionRequest *Create() { KSessionRequest *req = KSessionRequest::Allocate(); if (AMS_LIKELY(req != nullptr)) { - KAutoObject::Create(req); + KAutoObject::Create(req); } return req; } @@ -145,7 +145,7 @@ namespace ams::kern { static KSessionRequest *CreateFromUnusedSlabMemory() { KSessionRequest *req = KSessionRequest::AllocateFromUnusedSlabMemory(); if (AMS_LIKELY(req != nullptr)) { - KAutoObject::Create(req); + KAutoObject::Create(req); } return req; } diff --git a/libraries/libmesosphere/include/mesosphere/kern_slab_helpers.hpp b/libraries/libmesosphere/include/mesosphere/kern_slab_helpers.hpp index 5eb753081..079055261 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_slab_helpers.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_slab_helpers.hpp @@ -118,7 +118,7 @@ namespace ams::kern { static Derived *Create() { Derived *obj = Allocate(); if (AMS_LIKELY(obj != nullptr)) { - KAutoObject::Create(obj); + KAutoObject::Create(obj); } return obj; } @@ -130,7 +130,7 @@ namespace ams::kern { Derived * const obj = GetPointer(AllocateUnusedSlabMemory(sizeof(Derived), alignof(Derived))); if (AMS_LIKELY(obj != nullptr)) { std::construct_at(obj); - KAutoObject::Create(obj); + KAutoObject::Create(obj); } return obj; } diff --git a/libraries/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp b/libraries/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp index aa22f91f2..3f91ecfb7 100644 --- a/libraries/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp +++ b/libraries/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp @@ -487,7 +487,7 @@ namespace ams::kern::board::nintendo::nx { { /* Construct the resource limit object. */ KResourceLimit &sys_res_limit = Kernel::GetSystemResourceLimit(); - KAutoObject::Create(std::addressof(sys_res_limit)); + KAutoObject::Create(std::addressof(sys_res_limit)); sys_res_limit.Initialize(); /* Set the initial limits. */ diff --git a/libraries/libmesosphere/source/kern_k_auto_object.cpp b/libraries/libmesosphere/source/kern_k_auto_object.cpp deleted file mode 100644 index 57722a3c8..000000000 --- a/libraries/libmesosphere/source/kern_k_auto_object.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 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 . - */ -#include - -namespace ams::kern { - - KAutoObject *KAutoObject::Create(KAutoObject *obj) { - obj->m_ref_count = 1; - - #if defined(MESOSPHERE_ENABLE_DEVIRTUALIZED_DYNAMIC_CAST) - obj->m_class_token = obj->GetTypeObj().GetClassToken(); - #endif - - return obj; - } - -} diff --git a/libraries/libmesosphere/source/kern_k_event.cpp b/libraries/libmesosphere/source/kern_k_event.cpp index 371225c43..f2e37d113 100644 --- a/libraries/libmesosphere/source/kern_k_event.cpp +++ b/libraries/libmesosphere/source/kern_k_event.cpp @@ -21,7 +21,7 @@ namespace ams::kern { MESOSPHERE_ASSERT_THIS(); /* Create our readable event. */ - KAutoObject::Create(std::addressof(m_readable_event)); + KAutoObject::Create(std::addressof(m_readable_event)); /* Initialize our readable event. */ m_readable_event.Initialize(this); diff --git a/libraries/libmesosphere/source/kern_k_light_session.cpp b/libraries/libmesosphere/source/kern_k_light_session.cpp index 64f57b786..e0128b1a5 100644 --- a/libraries/libmesosphere/source/kern_k_light_session.cpp +++ b/libraries/libmesosphere/source/kern_k_light_session.cpp @@ -27,8 +27,8 @@ namespace ams::kern { this->Open(); /* Create our sub sessions. */ - KAutoObject::Create(std::addressof(m_server)); - KAutoObject::Create(std::addressof(m_client)); + KAutoObject::Create(std::addressof(m_server)); + KAutoObject::Create(std::addressof(m_client)); /* Initialize our sub sessions. */ m_server.Initialize(this); diff --git a/libraries/libmesosphere/source/kern_k_port.cpp b/libraries/libmesosphere/source/kern_k_port.cpp index ba17da7f5..4a5a57d5c 100644 --- a/libraries/libmesosphere/source/kern_k_port.cpp +++ b/libraries/libmesosphere/source/kern_k_port.cpp @@ -22,8 +22,8 @@ namespace ams::kern { this->Open(); /* Create and initialize our server/client pair. */ - KAutoObject::Create(std::addressof(m_server)); - KAutoObject::Create(std::addressof(m_client)); + KAutoObject::Create(std::addressof(m_server)); + KAutoObject::Create(std::addressof(m_client)); m_server.Initialize(this); m_client.Initialize(this, max_sessions); diff --git a/libraries/libmesosphere/source/kern_k_session.cpp b/libraries/libmesosphere/source/kern_k_session.cpp index 18815d025..fe8c572f5 100644 --- a/libraries/libmesosphere/source/kern_k_session.cpp +++ b/libraries/libmesosphere/source/kern_k_session.cpp @@ -27,8 +27,8 @@ namespace ams::kern { this->Open(); /* Create our sub sessions. */ - KAutoObject::Create(std::addressof(m_server)); - KAutoObject::Create(std::addressof(m_client)); + KAutoObject::Create(std::addressof(m_server)); + KAutoObject::Create(std::addressof(m_client)); /* Initialize our sub sessions. */ m_server.Initialize(this); diff --git a/libraries/libmesosphere/source/kern_kernel.cpp b/libraries/libmesosphere/source/kern_kernel.cpp index 6c63acdb4..090d5028b 100644 --- a/libraries/libmesosphere/source/kern_kernel.cpp +++ b/libraries/libmesosphere/source/kern_kernel.cpp @@ -50,8 +50,8 @@ namespace ams::kern { void *main_thread_stack = GetVoidPointer(KMemoryLayout::GetMainStackTopAddress(core_id)); KThread *idle_thread = std::addressof(Kernel::GetIdleThread(core_id)); void *idle_thread_stack = GetVoidPointer(KMemoryLayout::GetIdleStackTopAddress(core_id)); - KAutoObject::Create(main_thread); - KAutoObject::Create(idle_thread); + KAutoObject::Create(main_thread); + KAutoObject::Create(idle_thread); main_thread->Initialize(nullptr, 0, main_thread_stack, 0, KThread::MainThreadPriority, core_id, nullptr, KThread::ThreadType_Main); idle_thread->Initialize(nullptr, 0, idle_thread_stack, 0, KThread::IdleThreadPriority, core_id, nullptr, KThread::ThreadType_Main);