1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-12-18 08:22:04 +00:00

tipc: first draft object allocation logic

This commit is contained in:
Michael Scire 2021-04-08 15:24:22 -07:00 committed by SciresM
parent 21b883a75c
commit ec988c5a99
6 changed files with 236 additions and 1 deletions

View file

@ -15,4 +15,7 @@
*/ */
#pragma once #pragma once
#include <stratosphere/tipc/tipc_service_object.hpp>
#include <stratosphere/tipc/tipc_allocators.hpp>
#include <stratosphere/tipc/impl/tipc_impl_command_serialization.hpp> #include <stratosphere/tipc/impl/tipc_impl_command_serialization.hpp>

View file

@ -0,0 +1,32 @@
/*
* 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
#include <vapours.hpp>
namespace ams::tipc::impl {
template<typename Interface, typename Base, typename ImplHolder, typename ImplGetter, typename Root>
class ImplTemplateBaseT;
template<typename Interface, typename Base, typename ImplHolder, typename ImplGetter>
class ImplTemplateBase : public ImplTemplateBaseT<Interface, Base, ImplHolder, ImplGetter, Interface> {
private:
using BaseImpl = ImplTemplateBaseT<Interface, Base, ImplHolder, ImplGetter, Interface>;
public:
using BaseImpl::BaseImpl;
};
}

View file

@ -0,0 +1,86 @@
/*
* 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
#include <vapours.hpp>
#include <stratosphere/tipc/tipc_common.hpp>
#include <stratosphere/tipc/tipc_service_object_base.hpp>
namespace ams::tipc {
template<typename T>
concept IsServiceObjectAllocator = requires (T &t) {
{ t.Allocate() } -> std::convertible_to<ServiceObjectBase *>;
};
template<typename T> requires IsServiceObject<T>
class SingletonAllocator final {
private:
T m_singleton;
public:
constexpr ALWAYS_INLINE SingletonAllocator() : m_singleton() { /* ... */ }
ALWAYS_INLINE ServiceObjectBase *Allocate() { return std::addressof(m_singleton); }
};
template<typename T, size_t N> requires IsServiceObject<T>
class SlabAllocator final : public ServiceObjectDeleter {
private:
struct Entry {
bool used;
util::TypedStorage<T> storage;
};
private:
os::SdkMutex m_mutex;
Entry m_entries[N];
public:
constexpr ALWAYS_INLINE SlabAllocator() : m_entries() { /* ... */ }
ServiceObjectBase *Allocate() {
std::scoped_lock lk(m_mutex);
for (size_t i = 0; i < N; ++i) {
if (!m_entries[i].used) {
m_entries[i].used = true;
return util::ConstructAt(m_entries[i].storage);
}
}
AMS_ABORT("Failed to allocate entry in SlabAllocator<T, N>");
}
void Deallocate(ServiceObjectBase *object) {
std::scoped_lock lk(m_mutex);
for (size_t i = 0; i < N; ++i) {
if (m_entries[i].used && GetPointer(m_entries[i].storage) == object) {
util::DestroyAt(m_entries[i].storage);
m_entries[i].used = false;
return;
}
}
AMS_ABORT("Failed to deallocate entry in SlabAllocator<T, N>");
}
public:
virtual void DeleteServiceObject(ServiceObjectBase *object) override {
return this->Deallocate(object);
}
};
static_assert(IsServiceObjectAllocator<SlabAllocator<ServiceObjectBase, 1>>);
static_assert(IsServiceObjectDeleter<SlabAllocator<ServiceObjectBase, 1>>);
}

View file

@ -0,0 +1,61 @@
/*
* 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
#include <vapours.hpp>
#include <stratosphere/tipc/tipc_service_object_base.hpp>
#include <stratosphere/tipc/impl/tipc_impl_template_base.hpp>
namespace ams::tipc {
namespace impl {
template<typename Impl>
class EmplacedImplHolderBaseGetter {
using Type = Impl;
};
template<typename Impl>
class EmplacedImplHolder {
template<typename, typename, typename, typename>
friend class impl::ImplTemplateBaseT;
private:
using Impl2 = typename EmplacedImplHolderBaseGetter<Impl>::Type;
static_assert(!std::is_abstract<Impl2>::value);
private:
Impl2 m_impl;
private:
template<typename... Args>
constexpr explicit EmplacedImplHolder(Args &&... args) : m_impl(std::forward<Args>(args)...) { /* ... */ }
public:
static constexpr Impl *GetImplPointer(EmplacedImplHolder *holder) {
return std::addressof(holder->m_impl);
}
};
}
template<typename Interface, typename Impl>
class ServiceObject final : public impl::ImplTemplateBase<Interface, Interface, impl::EmplacedImplHolder<Impl>, impl::EmplacedImplHolder<Impl>> {
private:
using ImplBase = impl::ImplTemplateBase<Interface, Interface, impl::EmplacedImplHolder<Impl>, impl::EmplacedImplHolder<Impl>>;
public:
using ImplBase::ImplBase;
constexpr Impl &GetImpl() { return *impl::EmplacedImplHolder<Impl>::GetImplPointer(this); }
};
}

View file

@ -0,0 +1,53 @@
/*
* 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
#include <vapours.hpp>
namespace ams::tipc {
class ServiceObjectBase;
class ServiceObjectDeleter {
public:
virtual void DeleteServiceObject(ServiceObjectBase *object) = 0;
};
template<typename T>
concept IsServiceObjectDeleter = std::derived_from<T, ServiceObjectDeleter>;
class ServiceObjectBase {
private:
ServiceObjectDeleter *m_deleter;
public:
constexpr ALWAYS_INLINE ServiceObjectBase() : m_deleter(nullptr) { /* ... */ }
ALWAYS_INLINE void SetDeleter(ServiceObjectDeleter *deleter) {
m_deleter = deleter;
}
ALWAYS_INLINE ServiceObjectDeleter *GetDeleter() const {
return m_deleter;
}
virtual ~ServiceObjectBase() { /* ... */ }
virtual Result ProcessRequest() = 0;
};
template<typename T>
concept IsServiceObject = std::derived_from<T, ServiceObjectBase>;
}