cs: fix allocator aborts

This commit is contained in:
Michael Scire 2021-07-27 23:55:53 -07:00
parent 999318838f
commit fe8d031708
4 changed files with 51 additions and 7 deletions

View file

@ -84,11 +84,17 @@ namespace ams::pgl {
class EventObserver { class EventObserver {
NON_COPYABLE(EventObserver); NON_COPYABLE(EventObserver);
private: private:
std::unique_ptr<impl::EventObserverInterface> m_impl; struct Deleter {
void operator()(impl::EventObserverInterface *);
};
public:
using UniquePtr = std::unique_ptr<impl::EventObserverInterface, Deleter>;
private:
UniquePtr m_impl;
public: public:
EventObserver() { /* ... */ } EventObserver() { /* ... */ }
explicit EventObserver(std::unique_ptr<impl::EventObserverInterface> impl) : m_impl(std::move(impl)) { /* ... */ } explicit EventObserver(UniquePtr impl) : m_impl(std::move(impl)) { /* ... */ }
EventObserver(EventObserver &&rhs) { EventObserver(EventObserver &&rhs) {
m_impl = std::move(rhs.m_impl); m_impl = std::move(rhs.m_impl);

View file

@ -33,6 +33,7 @@ namespace ams::cs {
SendFirmwareVersion(socket, header); SendFirmwareVersion(socket, header);
break; break;
/* TODO: Command support. */ /* TODO: Command support. */
/* TODO: Command support. */
default: default:
scs::CommandProcessor::ProcessCommand(header, body, socket); scs::CommandProcessor::ProcessCommand(header, body, socket);
break; break;

View file

@ -18,6 +18,44 @@
namespace ams::pgl { namespace ams::pgl {
namespace {
struct PglEventObserverAllocator;
using RemoteAllocator = ams::sf::ExpHeapStaticAllocator<1_KB, PglEventObserverAllocator>;
using RemoteObjectFactory = ams::sf::ObjectFactory<typename RemoteAllocator::Policy>;
class StaticAllocatorInitializer {
public:
StaticAllocatorInitializer() {
RemoteAllocator::Initialize(lmem::CreateOption_None);
}
} g_static_allocator_initializer;
template<typename T, typename... Args>
T *AllocateFromStaticExpHeap(Args &&... args) {
T * const object = static_cast<T *>(RemoteAllocator::Allocate(sizeof(T)));
if (AMS_LIKELY(object != nullptr)) {
std::construct_at(object, std::forward<Args>(args)...);
}
return object;
}
template<typename T>
void FreeToStaticExpHeap(T *object) {
return RemoteAllocator::Deallocate(object, sizeof(T));
}
template<typename T, typename... Args> requires std::derived_from<T, impl::EventObserverInterface>
EventObserver::UniquePtr MakeUniqueFromStaticExpHeap(Args &&... args) {
return EventObserver::UniquePtr{AllocateFromStaticExpHeap<T>(std::forward<Args>(args)...)};
}
}
void EventObserver::Deleter::operator()(impl::EventObserverInterface *obj) {
FreeToStaticExpHeap(obj);
}
Result Initialize() { Result Initialize() {
return ::pglInitialize(); return ::pglInitialize();
} }
@ -79,17 +117,16 @@ namespace ams::pgl {
::PglEventObserver obs; ::PglEventObserver obs;
R_TRY(::pglGetEventObserver(std::addressof(obs))); R_TRY(::pglGetEventObserver(std::addressof(obs)));
/* TODO: Real allocator */
if (hos::GetVersion() >= hos::Version_12_0_0) { if (hos::GetVersion() >= hos::Version_12_0_0) {
auto observer_holder = std::make_unique<impl::EventObserverByTipc<RemoteEventObserver>>(obs); auto observer_holder = MakeUniqueFromStaticExpHeap<impl::EventObserverByTipc<RemoteEventObserver>>(obs);
R_UNLESS(observer_holder != nullptr, pgl::ResultOutOfMemory()); R_UNLESS(observer_holder != nullptr, pgl::ResultOutOfMemory());
*out = pgl::EventObserver(std::move(observer_holder)); *out = pgl::EventObserver(std::move(observer_holder));
} else { } else {
auto remote_observer = ams::sf::CreateSharedObjectEmplaced<pgl::sf::IEventObserver, RemoteEventObserver>(obs); auto remote_observer = RemoteObjectFactory::CreateSharedEmplaced<pgl::sf::IEventObserver, RemoteEventObserver>(obs);
R_UNLESS(remote_observer != nullptr, pgl::ResultOutOfMemory()); R_UNLESS(remote_observer != nullptr, pgl::ResultOutOfMemory());
auto observer_holder = std::make_unique<impl::EventObserverByCmif>(std::move(remote_observer)); auto observer_holder = MakeUniqueFromStaticExpHeap<impl::EventObserverByCmif>(std::move(remote_observer));
R_UNLESS(observer_holder != nullptr, pgl::ResultOutOfMemory()); R_UNLESS(observer_holder != nullptr, pgl::ResultOutOfMemory());
*out = pgl::EventObserver(std::move(observer_holder)); *out = pgl::EventObserver(std::move(observer_holder));

View file

@ -70,7 +70,7 @@ namespace ams::cs {
alignas(os::MemoryPageSize) constinit u8 g_heap_memory[32_KB]; alignas(os::MemoryPageSize) constinit u8 g_heap_memory[32_KB];
alignas(0x40) constinit u8 g_htcs_buffer[1_KB]; alignas(0x40) constinit u8 g_htcs_buffer[2_KB];
constinit os::SdkMutex g_heap_mutex; constinit os::SdkMutex g_heap_mutex;
constinit lmem::HeapHandle g_heap_handle; constinit lmem::HeapHandle g_heap_handle;