From 05f908da1f06dee8bd7f9dc117f50a24c0c2a68f Mon Sep 17 00:00:00 2001 From: Tony Wasserka Date: Sat, 16 Jun 2018 17:39:09 +0200 Subject: [PATCH] Stratosphere: Replace more raw loops with algorithms --- .../include/stratosphere/domainowner.hpp | 56 ++++++++----------- .../source/multithreadedwaitablemanager.cpp | 15 ++--- 2 files changed, 27 insertions(+), 44 deletions(-) diff --git a/stratosphere/libstratosphere/include/stratosphere/domainowner.hpp b/stratosphere/libstratosphere/include/stratosphere/domainowner.hpp index 09dde7918..d20fe5efc 100644 --- a/stratosphere/libstratosphere/include/stratosphere/domainowner.hpp +++ b/stratosphere/libstratosphere/include/stratosphere/domainowner.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include @@ -11,18 +12,13 @@ class IServiceObject; class DomainOwner { private: - std::shared_ptr domain_objects[DOMAIN_ID_MAX]; + std::array, DOMAIN_ID_MAX> domain_objects; public: - DomainOwner() { - for (unsigned int i = 0; i < DOMAIN_ID_MAX; i++) { - domain_objects[i].reset(); - } - } - - virtual ~DomainOwner() { - /* Shared ptrs should auto delete here. */ - } - + DomainOwner() = default; + + /* Shared ptrs should auto delete here. */ + virtual ~DomainOwner() = default; + std::shared_ptr get_domain_object(unsigned int i) { if (i < DOMAIN_ID_MAX) { return domain_objects[i]; @@ -31,15 +27,15 @@ class DomainOwner { } Result reserve_object(std::shared_ptr object, unsigned int *out_i) { - for (unsigned int i = 4; i < DOMAIN_ID_MAX; i++) { - if (domain_objects[i] == NULL) { - domain_objects[i] = object; - object->set_owner(this); - *out_i = i; - return 0; - } + auto object_it = std::find(domain_objects.begin() + 4, domain_objects.end(), nullptr); + if (object_it == domain_objects.end()) { + return 0x1900B; } - return 0x1900B; + + *out_i = std::distance(domain_objects.begin(), object_it); + *object_it = object; + (*object_it)->set_owner(this); + return 0; } Result set_object(std::shared_ptr object, unsigned int i) { @@ -52,26 +48,18 @@ class DomainOwner { } unsigned int get_object_id(std::shared_ptr object) { - for (unsigned int i = 0; i < DOMAIN_ID_MAX; i++) { - if (domain_objects[i] == object) { - return i; - } - } - return DOMAIN_ID_MAX; + auto object_it = std::find(domain_objects.begin(), domain_objects.end(), object); + return std::distance(domain_objects.begin(), object_it); } void delete_object(unsigned int i) { - if (domain_objects[i]) { - domain_objects[i].reset(); - } + domain_objects[i].reset(); } void delete_object(std::shared_ptr object) { - for (unsigned int i = 0; i < DOMAIN_ID_MAX; i++) { - if (domain_objects[i] == object) { - domain_objects[i].reset(); - break; - } + auto object_it = std::find(domain_objects.begin(), domain_objects.end(), object); + if (object_it != domain_objects.end()) { + object_it->reset(); } } -}; \ No newline at end of file +}; diff --git a/stratosphere/libstratosphere/source/multithreadedwaitablemanager.cpp b/stratosphere/libstratosphere/source/multithreadedwaitablemanager.cpp index 6acc53a65..dcf1a931c 100644 --- a/stratosphere/libstratosphere/source/multithreadedwaitablemanager.cpp +++ b/stratosphere/libstratosphere/source/multithreadedwaitablemanager.cpp @@ -1,6 +1,7 @@ #include #include +#include #include @@ -44,21 +45,15 @@ IWaitable *MultiThreadedWaitableManager::get_waitable() { rc = svcWaitSynchronization(&handle_index, handles.data(), this->waitables.size(), this->timeout); IWaitable *w = this->waitables[handle_index]; if (R_SUCCEEDED(rc)) { - for (int i = 0; i < handle_index; i++) { - this->waitables[i]->update_priority(); - } + std::for_each(waitables.begin(), waitables.begin() + handle_index, std::mem_fn(&IWaitable::update_priority)); this->waitables.erase(this->waitables.begin() + handle_index); } else if (rc == 0xEA01) { /* Timeout. */ - for (auto & waitable : this->waitables) { - waitable->update_priority(); - } + std::for_each(waitables.begin(), waitables.end(), std::mem_fn(&IWaitable::update_priority)); } else if (rc != 0xF601 && rc != 0xE401) { /* TODO: Panic. When can this happen? */ } else { - for (int i = 0; i < handle_index; i++) { - this->waitables[i]->update_priority(); - } + std::for_each(waitables.begin(), waitables.begin() + handle_index, std::mem_fn(&IWaitable::update_priority)); this->waitables.erase(this->waitables.begin() + handle_index); delete w; } @@ -107,4 +102,4 @@ void MultiThreadedWaitableManager::thread_func(void *t) { } } } -} \ No newline at end of file +}