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:
KAutoObject *m_next_closed_object;
std::atomic<u32> m_ref_count;
u32 m_reserved;
public:
static KAutoObject *Create(KAutoObject *ptr);
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. */
virtual void Destroy() { MESOSPHERE_ASSERT_THIS(); }

View file

@ -35,13 +35,13 @@ namespace ams::kern {
ServerClosed = 3,
};
private:
std::atomic<std::underlying_type<State>::type> m_atomic_state;
bool m_initialized;
KServerSession m_server;
KClientSession m_client;
std::atomic<std::underlying_type<State>::type> m_atomic_state;
KClientPort *m_port;
uintptr_t m_name;
KProcess *m_process;
bool m_initialized;
private:
ALWAYS_INLINE void SetState(State state) {
m_atomic_state = static_cast<u8>(state);
@ -52,7 +52,7 @@ namespace ams::kern {
}
public:
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(std::same_as<ams::util::RedBlackKeyType<ConditionVariableComparator, void>, ConditionVariableComparator::RedBlackKeyType>);
private:
static inline std::atomic<u64> s_next_thread_id = 0;
static constinit inline std::atomic<u64> s_next_thread_id = 0;
private:
alignas(16) KThreadContext m_thread_context{};
util::IntrusiveListNode m_process_list_node{};
util::IntrusiveRedBlackTreeNode m_condvar_arbiter_tree_node{};
s32 m_priority{};
@ -189,6 +188,7 @@ namespace ams::kern {
ConditionVariableThreadTree *m_condvar_tree{};
uintptr_t m_condvar_key{};
alignas(16) KThreadContext m_thread_context{};
u64 m_virtual_affinity_mask{};
KAffinityMask m_physical_affinity_mask{};
u64 m_thread_id{};
@ -204,7 +204,6 @@ namespace ams::kern {
s64 m_schedule_count{};
s64 m_last_scheduled_tick{};
QueueEntry m_per_core_priority_queue_entry[cpu::NumCores]{};
KLightLock *m_waiting_lock{};
KThreadQueue *m_wait_queue{};
WaiterList m_waiter_list{};
WaiterList m_pinned_waiter_list{};

View file

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

View file

@ -53,6 +53,7 @@ namespace ams::freebsd {
RB_RED = 1,
};
#pragma pack(push, 4)
template<typename T>
class RBEntry {
private:
@ -82,6 +83,7 @@ namespace ams::freebsd {
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<RBEntry<T>> { static constexpr bool value = true; };

View file

@ -31,6 +31,7 @@ namespace ams::util {
}
#pragma pack(push, 4)
struct IntrusiveRedBlackTreeNode {
NON_COPYABLE(IntrusiveRedBlackTreeNode);
public:
@ -45,6 +46,8 @@ namespace ams::util {
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>
class IntrusiveRedBlackTree;
@ -501,6 +504,7 @@ namespace ams::util {
private:
static constexpr TypedStorage<Derived> 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>>
@ -515,7 +519,7 @@ namespace ams::util {
static constexpr bool IsValid() {
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:
template<class, class, class>
@ -541,7 +545,7 @@ namespace ams::util {
};
template<class Derived>
class IntrusiveRedBlackTreeBaseNode : public IntrusiveRedBlackTreeNode {
class alignas(void *) IntrusiveRedBlackTreeBaseNode : public IntrusiveRedBlackTreeNode {
public:
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)); }