kern/util: update structure layouts to match Nintendo (saves 0x10 per KThread/KSession)

This commit is contained in:
Michael Scire 2021-10-16 16:13:10 -07:00
parent 7805a3624e
commit 26c02e2019
6 changed files with 15 additions and 12 deletions

View file

@ -71,10 +71,11 @@ namespace ams::kern {
private: private:
KAutoObject *m_next_closed_object; KAutoObject *m_next_closed_object;
std::atomic<u32> m_ref_count; std::atomic<u32> m_ref_count;
u32 m_reserved;
public: public:
static KAutoObject *Create(KAutoObject *ptr); static KAutoObject *Create(KAutoObject *ptr);
public: public:
constexpr ALWAYS_INLINE explicit KAutoObject() : m_next_closed_object(nullptr), m_ref_count(0) { MESOSPHERE_ASSERT_THIS(); } constexpr ALWAYS_INLINE explicit KAutoObject() : m_next_closed_object(nullptr), m_ref_count(0), m_reserved(0) { MESOSPHERE_ASSERT_THIS(); }
/* Destroy is responsible for destroying the auto object's resources when ref_count hits zero. */ /* Destroy is responsible for destroying the auto object's resources when ref_count hits zero. */
virtual void Destroy() { MESOSPHERE_ASSERT_THIS(); } virtual void Destroy() { MESOSPHERE_ASSERT_THIS(); }

View file

@ -35,13 +35,13 @@ namespace ams::kern {
ServerClosed = 3, ServerClosed = 3,
}; };
private: private:
std::atomic<std::underlying_type<State>::type> m_atomic_state;
bool m_initialized;
KServerSession m_server; KServerSession m_server;
KClientSession m_client; KClientSession m_client;
std::atomic<std::underlying_type<State>::type> m_atomic_state;
KClientPort *m_port; KClientPort *m_port;
uintptr_t m_name; uintptr_t m_name;
KProcess *m_process; KProcess *m_process;
bool m_initialized;
private: private:
ALWAYS_INLINE void SetState(State state) { ALWAYS_INLINE void SetState(State state) {
m_atomic_state = static_cast<u8>(state); m_atomic_state = static_cast<u8>(state);
@ -52,7 +52,7 @@ namespace ams::kern {
} }
public: public:
constexpr KSession() constexpr KSession()
: m_server(), m_client(), m_atomic_state(static_cast<std::underlying_type<State>::type>(State::Invalid)), m_port(), m_name(), m_process(), m_initialized() : m_atomic_state(static_cast<std::underlying_type<State>::type>(State::Invalid)), m_initialized(), m_server(), m_client(), m_port(), m_name(), m_process()
{ {
/* ... */ /* ... */
} }

View file

@ -177,9 +177,8 @@ namespace ams::kern {
static_assert(ams::util::HasRedBlackKeyType<ConditionVariableComparator>); static_assert(ams::util::HasRedBlackKeyType<ConditionVariableComparator>);
static_assert(std::same_as<ams::util::RedBlackKeyType<ConditionVariableComparator, void>, ConditionVariableComparator::RedBlackKeyType>); static_assert(std::same_as<ams::util::RedBlackKeyType<ConditionVariableComparator, void>, ConditionVariableComparator::RedBlackKeyType>);
private: private:
static inline std::atomic<u64> s_next_thread_id = 0; static constinit inline std::atomic<u64> s_next_thread_id = 0;
private: private:
alignas(16) KThreadContext m_thread_context{};
util::IntrusiveListNode m_process_list_node{}; util::IntrusiveListNode m_process_list_node{};
util::IntrusiveRedBlackTreeNode m_condvar_arbiter_tree_node{}; util::IntrusiveRedBlackTreeNode m_condvar_arbiter_tree_node{};
s32 m_priority{}; s32 m_priority{};
@ -189,6 +188,7 @@ namespace ams::kern {
ConditionVariableThreadTree *m_condvar_tree{}; ConditionVariableThreadTree *m_condvar_tree{};
uintptr_t m_condvar_key{}; uintptr_t m_condvar_key{};
alignas(16) KThreadContext m_thread_context{};
u64 m_virtual_affinity_mask{}; u64 m_virtual_affinity_mask{};
KAffinityMask m_physical_affinity_mask{}; KAffinityMask m_physical_affinity_mask{};
u64 m_thread_id{}; u64 m_thread_id{};
@ -204,7 +204,6 @@ namespace ams::kern {
s64 m_schedule_count{}; s64 m_schedule_count{};
s64 m_last_scheduled_tick{}; s64 m_last_scheduled_tick{};
QueueEntry m_per_core_priority_queue_entry[cpu::NumCores]{}; QueueEntry m_per_core_priority_queue_entry[cpu::NumCores]{};
KLightLock *m_waiting_lock{};
KThreadQueue *m_wait_queue{}; KThreadQueue *m_wait_queue{};
WaiterList m_waiter_list{}; WaiterList m_waiter_list{};
WaiterList m_pinned_waiter_list{}; WaiterList m_pinned_waiter_list{};

View file

@ -148,9 +148,6 @@ namespace ams::kern {
m_priority = prio; m_priority = prio;
m_base_priority = prio; m_base_priority = prio;
/* Set waiting lock to null. */
m_waiting_lock = nullptr;
/* Initialize wait queue/sync index. */ /* Initialize wait queue/sync index. */
m_synced_index = -1; m_synced_index = -1;
m_wait_queue = nullptr; m_wait_queue = nullptr;

View file

@ -53,6 +53,7 @@ namespace ams::freebsd {
RB_RED = 1, RB_RED = 1,
}; };
#pragma pack(push, 4)
template<typename T> template<typename T>
class RBEntry { class RBEntry {
private: private:
@ -82,6 +83,7 @@ namespace ams::freebsd {
constexpr ALWAYS_INLINE void SetColor(RBColor c) { m_rbe_color = c; } constexpr ALWAYS_INLINE void SetColor(RBColor c) { m_rbe_color = c; }
}; };
#pragma pack(pop)
template<typename T> struct CheckRBEntry { static constexpr bool value = false; }; template<typename T> struct CheckRBEntry { static constexpr bool value = false; };
template<typename T> struct CheckRBEntry<RBEntry<T>> { static constexpr bool value = true; }; template<typename T> struct CheckRBEntry<RBEntry<T>> { static constexpr bool value = true; };

View file

@ -31,6 +31,7 @@ namespace ams::util {
} }
#pragma pack(push, 4)
struct IntrusiveRedBlackTreeNode { struct IntrusiveRedBlackTreeNode {
NON_COPYABLE(IntrusiveRedBlackTreeNode); NON_COPYABLE(IntrusiveRedBlackTreeNode);
public: public:
@ -45,6 +46,8 @@ namespace ams::util {
constexpr ALWAYS_INLINE void SetRBEntry(const RBEntry &entry) { m_entry = entry; } constexpr ALWAYS_INLINE void SetRBEntry(const RBEntry &entry) { m_entry = entry; }
}; };
static_assert(sizeof(IntrusiveRedBlackTreeNode) == 3 * sizeof(void *) + std::max<size_t>(sizeof(freebsd::RBColor), 4));
#pragma pack(pop)
template<class T, class Traits, class Comparator> template<class T, class Traits, class Comparator>
class IntrusiveRedBlackTree; class IntrusiveRedBlackTree;
@ -501,6 +504,7 @@ namespace ams::util {
private: private:
static constexpr TypedStorage<Derived> DerivedStorage = {}; static constexpr TypedStorage<Derived> DerivedStorage = {};
static_assert(GetParent(GetNode(GetPointer(DerivedStorage))) == GetPointer(DerivedStorage)); static_assert(GetParent(GetNode(GetPointer(DerivedStorage))) == GetPointer(DerivedStorage));
static_assert(util::IsAligned(util::impl::OffsetOf<Member, Derived>, alignof(void *)));
}; };
template<auto T, class Derived = util::impl::GetParentType<T>> template<auto T, class Derived = util::impl::GetParentType<T>>
@ -515,7 +519,7 @@ namespace ams::util {
static constexpr bool IsValid() { static constexpr bool IsValid() {
TypedStorage<Derived> DerivedStorage = {}; TypedStorage<Derived> DerivedStorage = {};
return GetParent(GetNode(GetPointer(DerivedStorage))) == GetPointer(DerivedStorage); return GetParent(GetNode(GetPointer(DerivedStorage))) == GetPointer(DerivedStorage) && util::IsAligned(util::impl::OffsetOf<Member, Derived>, alignof(void *));
} }
private: private:
template<class, class, class> template<class, class, class>
@ -541,7 +545,7 @@ namespace ams::util {
}; };
template<class Derived> template<class Derived>
class IntrusiveRedBlackTreeBaseNode : public IntrusiveRedBlackTreeNode { class alignas(void *) IntrusiveRedBlackTreeBaseNode : public IntrusiveRedBlackTreeNode {
public: public:
constexpr ALWAYS_INLINE Derived *GetPrev() { return static_cast< Derived *>(impl::IntrusiveRedBlackTreeImpl::GetPrev(this)); } constexpr ALWAYS_INLINE Derived *GetPrev() { return static_cast< Derived *>(impl::IntrusiveRedBlackTreeImpl::GetPrev(this)); }
constexpr ALWAYS_INLINE const Derived *GetPrev() const { return static_cast<const Derived *>(impl::IntrusiveRedBlackTreeImpl::GetPrev(this)); } constexpr ALWAYS_INLINE const Derived *GetPrev() const { return static_cast<const Derived *>(impl::IntrusiveRedBlackTreeImpl::GetPrev(this)); }