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_auto_object.hpp>
|
|
|
|
#include <mesosphere/kern_k_light_lock.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct GetAutoObjectWithListComparator;
|
|
|
|
|
|
|
|
class KAutoObjectWithListContainerBase {
|
|
|
|
NON_COPYABLE(KAutoObjectWithListContainerBase);
|
|
|
|
NON_MOVEABLE(KAutoObjectWithListContainerBase);
|
|
|
|
protected:
|
|
|
|
template<typename ListType>
|
|
|
|
class ListAccessorImpl {
|
|
|
|
NON_COPYABLE(ListAccessorImpl);
|
|
|
|
NON_MOVEABLE(ListAccessorImpl);
|
|
|
|
private:
|
|
|
|
KScopedLightLock m_lk;
|
|
|
|
ListType &m_list;
|
|
|
|
public:
|
|
|
|
explicit ALWAYS_INLINE ListAccessorImpl(KAutoObjectWithListContainerBase *container, ListType &list) : m_lk(container->m_lock), m_list(list) { /* ... */ }
|
|
|
|
explicit ALWAYS_INLINE ListAccessorImpl(KAutoObjectWithListContainerBase &container, ListType &list) : m_lk(container.m_lock), m_list(list) { /* ... */ }
|
|
|
|
|
|
|
|
ALWAYS_INLINE ~ListAccessorImpl() { /* ... */ }
|
|
|
|
|
|
|
|
ALWAYS_INLINE typename ListType::iterator begin() const {
|
|
|
|
return m_list.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE typename ListType::iterator end() const {
|
|
|
|
return m_list.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE typename ListType::iterator find(typename ListType::const_reference ref) const {
|
|
|
|
return m_list.find(ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE typename ListType::iterator find_key(typename ListType::const_key_reference ref) const {
|
|
|
|
return m_list.find_key(ref);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ListType>
|
|
|
|
friend class ListAccessorImpl;
|
|
|
|
private:
|
|
|
|
KLightLock m_lock;
|
|
|
|
protected:
|
|
|
|
constexpr KAutoObjectWithListContainerBase() : m_lock() { /* ... */ }
|
|
|
|
|
|
|
|
ALWAYS_INLINE void InitializeImpl() { MESOSPHERE_ASSERT_THIS(); }
|
|
|
|
ALWAYS_INLINE void FinalizeImpl() { MESOSPHERE_ASSERT_THIS(); }
|
|
|
|
|
|
|
|
template<typename ListType>
|
|
|
|
void RegisterImpl(KAutoObjectWithList *obj, ListType &list) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
KScopedLightLock lk(m_lock);
|
|
|
|
|
|
|
|
list.insert(*obj);
|
|
|
|
}
|
2020-01-30 06:06:25 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
template<typename ListType>
|
|
|
|
void UnregisterImpl(KAutoObjectWithList *obj, ListType &list) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2020-01-30 06:06:25 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
KScopedLightLock lk(m_lock);
|
2021-10-24 04:58:48 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
list.erase(list.iterator_to(*obj));
|
|
|
|
}
|
2020-01-30 06:06:25 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
template<typename U, typename ListType>
|
|
|
|
size_t GetOwnedCountImpl(const KProcess *owner, ListType &list) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2020-01-30 06:06:25 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
KScopedLightLock lk(m_lock);
|
2020-01-30 06:06:25 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
size_t count = 0;
|
2021-10-24 20:04:31 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
for (const auto &obj : list) {
|
2022-02-19 21:26:08 +00:00
|
|
|
MESOSPHERE_AUDIT(obj.template DynamicCast<const U *>() != nullptr);
|
2021-10-25 00:00:05 +00:00
|
|
|
if (static_cast<const U &>(obj).GetOwner() == owner) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
2021-10-24 20:04:31 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
};
|
2021-10-24 20:04:31 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
struct DummyKAutoObjectWithListComparator {
|
|
|
|
static NOINLINE int Compare(KAutoObjectWithList &lhs, KAutoObjectWithList &rhs) {
|
2021-10-25 00:06:43 +00:00
|
|
|
MESOSPHERE_UNUSED(lhs, rhs);
|
|
|
|
MESOSPHERE_PANIC("DummyKAutoObjectWithListComparator invoked");
|
2021-10-25 00:00:05 +00:00
|
|
|
}
|
|
|
|
};
|
2021-10-24 20:04:31 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
}
|
2021-10-24 20:04:31 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
template<typename T>
|
|
|
|
class KAutoObjectWithListContainer : public impl::KAutoObjectWithListContainerBase {
|
|
|
|
private:
|
|
|
|
using Base = impl::KAutoObjectWithListContainerBase;
|
|
|
|
public:
|
|
|
|
class ListAccessor;
|
|
|
|
friend class ListAccessor;
|
2021-10-24 20:04:31 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
template<typename Comparator>
|
|
|
|
using ListType = util::IntrusiveRedBlackTreeMemberTraits<&KAutoObjectWithList::m_list_node>::TreeType<Comparator>;
|
2021-10-24 20:04:31 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
using DummyListType = ListType<impl::DummyKAutoObjectWithListComparator>;
|
|
|
|
private:
|
|
|
|
DummyListType m_dummy_object_list;
|
|
|
|
public:
|
|
|
|
constexpr ALWAYS_INLINE KAutoObjectWithListContainer() : Base(), m_dummy_object_list() { static_assert(std::derived_from<T, KAutoObjectWithList>); }
|
2021-10-24 20:04:31 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
ALWAYS_INLINE void Initialize() { return this->InitializeImpl(); }
|
|
|
|
ALWAYS_INLINE void Finalize() { return this->FinalizeImpl(); }
|
2021-10-24 20:04:31 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
void Register(T *obj);
|
|
|
|
void Unregister(T *obj);
|
2021-10-24 20:04:31 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
private:
|
|
|
|
size_t GetOwnedCountChecked(const KProcess *owner);
|
|
|
|
public:
|
|
|
|
template<typename U> requires (std::same_as<U, T> && requires (const U &u) { { u.GetOwner() } -> std::convertible_to<const KProcess *>; })
|
|
|
|
ALWAYS_INLINE size_t GetOwnedCount(const KProcess *owner) {
|
|
|
|
return this->GetOwnedCountChecked(owner);
|
2021-10-24 20:04:31 +00:00
|
|
|
}
|
2020-01-30 06:06:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|