mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-28 05:34:11 -04:00
Modernize C++ usage (#144)
* Stratosphere: Use modern C++ idioms in some places * algorithms like std::for_each are used instead of raw loops * Stratosphere: Replace more raw loops with algorithms * Stratosphere: Add a utility predicate function to test for equality with a reference element This can be used to rewrite some common raw loops using algorithms instead * fs.mitm: Use variant * fs.mitm: Use enum class * fs.mitm: Turn RomFSSourceInfo::Cleanup into a destructor This obsoletes the need for a custom deleter in other places * fs.mitm: Use enum class some more * fs.mitm: Use unique_ptr * fs.mitm: Simplify initialization * Stratosphere: Simplify initialization * fs.mitm: Use unique_ptr (fix memory leak along the way) The previous code was using "delete" rather than "delete[]" * fs.mitm: Use vector::emplace_back rather than push_back emplace_back constructs elements in-place, hence avoiding a redundant element copy. * Stratosphere: Replace more raw loops with algorithms * Stratosphere: Use unique_ptr * fs.mitm: Replace more raw loops with algorithms * Stratosphere: Prefer move-construction over copy-construction when moving sink parameters around
This commit is contained in:
parent
f1c326aec3
commit
ad636f7216
28 changed files with 353 additions and 374 deletions
|
@ -1,9 +1,11 @@
|
|||
#include <switch.h>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include "ldr_launch_queue.hpp"
|
||||
#include "meta_tools.hpp"
|
||||
|
||||
static LaunchQueue::LaunchItem g_launch_queue[LAUNCH_QUEUE_SIZE] = {0};
|
||||
static std::array<LaunchQueue::LaunchItem, LAUNCH_QUEUE_SIZE> g_launch_queue = {0};
|
||||
|
||||
Result LaunchQueue::add(u64 tid, const char *args, u64 arg_size) {
|
||||
if(arg_size > LAUNCH_QUEUE_ARG_SIZE_MAX) {
|
||||
|
@ -45,12 +47,11 @@ Result LaunchQueue::add_item(const LaunchItem *item) {
|
|||
}
|
||||
|
||||
int LaunchQueue::get_index(u64 tid) {
|
||||
for(unsigned int i = 0; i < LAUNCH_QUEUE_SIZE; i++) {
|
||||
if(g_launch_queue[i].tid == tid) {
|
||||
return i;
|
||||
}
|
||||
auto it = std::find_if(g_launch_queue.begin(), g_launch_queue.end(), member_equals_fn(&LaunchQueue::LaunchItem::tid, tid));
|
||||
if (it == g_launch_queue.end()) {
|
||||
return LAUNCH_QUEUE_FULL;
|
||||
}
|
||||
return LAUNCH_QUEUE_FULL;
|
||||
return std::distance(g_launch_queue.begin(), it);
|
||||
}
|
||||
|
||||
int LaunchQueue::get_free_index(u64 tid) {
|
||||
|
@ -74,8 +75,8 @@ void LaunchQueue::clear() {
|
|||
|
||||
|
||||
LaunchQueue::LaunchItem *LaunchQueue::get_item(u64 tid) {
|
||||
int idx;
|
||||
if ((idx = get_index(tid)) == LAUNCH_QUEUE_FULL) {
|
||||
int idx = get_index(tid);
|
||||
if (idx == LAUNCH_QUEUE_FULL) {
|
||||
return NULL;
|
||||
}
|
||||
return &g_launch_queue[idx];
|
||||
|
|
|
@ -93,7 +93,7 @@ int main(int argc, char **argv)
|
|||
consoleDebugInit(debugDevice_SVC);
|
||||
|
||||
/* TODO: What's a good timeout value to use here? */
|
||||
WaitableManager *server_manager = new WaitableManager(U64_MAX);
|
||||
auto server_manager = std::make_unique<WaitableManager>(U64_MAX);
|
||||
|
||||
/* Add services to manager. */
|
||||
server_manager->add_waitable(new ServiceServer<ProcessManagerService>("ldr:pm", 1));
|
||||
|
@ -107,8 +107,6 @@ int main(int argc, char **argv)
|
|||
/* Loop forever, servicing our services. */
|
||||
server_manager->process();
|
||||
|
||||
/* Cleanup. */
|
||||
delete server_manager;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <switch.h>
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <functional>
|
||||
#include <cstring>
|
||||
#include "sha256.h"
|
||||
#include "ldr_nro.hpp"
|
||||
|
@ -37,12 +38,7 @@ Result NroUtils::LoadNro(Registration::Process *target_proc, Handle process_h, u
|
|||
u8 nro_hash[0x20];
|
||||
SHA256_CTX sha_ctx;
|
||||
/* Ensure there is an available NRO slot. */
|
||||
for (i = 0; i < NRO_INFO_MAX; i++) {
|
||||
if (!target_proc->nro_infos[i].in_use) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i >= NRO_INFO_MAX) {
|
||||
if (std::all_of(target_proc->nro_infos.begin(), target_proc->nro_infos.end(), std::mem_fn(&Registration::NroInfo::in_use))) {
|
||||
return 0x6E09;
|
||||
}
|
||||
for (i = 0; i < 0x200; i++) {
|
||||
|
@ -118,4 +114,4 @@ LOAD_NRO_END:
|
|||
mcm_bss.Close();
|
||||
}
|
||||
return 0x0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include "ldr_registration.hpp"
|
||||
#include "ldr_nro.hpp"
|
||||
|
||||
|
@ -9,12 +10,11 @@ static Registration::List g_registration_list = {0};
|
|||
static u64 g_num_registered = 1;
|
||||
|
||||
Registration::Process *Registration::GetFreeProcess() {
|
||||
for (unsigned int i = 0; i < REGISTRATION_LIST_MAX; i++) {
|
||||
if (!g_registration_list.processes[i].in_use) {
|
||||
return &g_registration_list.processes[i];
|
||||
}
|
||||
auto process_it = std::find_if_not(g_registration_list.processes.begin(), g_registration_list.processes.end(), std::mem_fn(&Registration::Process::in_use));
|
||||
if (process_it == g_registration_list.processes.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return NULL;
|
||||
return &*process_it;
|
||||
}
|
||||
|
||||
Registration::Process *Registration::GetProcess(u64 index) {
|
||||
|
@ -98,15 +98,13 @@ void Registration::AddNsoInfo(u64 index, u64 base_address, u64 size, const unsig
|
|||
if (target_process == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < NSO_INFO_MAX; i++) {
|
||||
if (!target_process->nso_infos[i].in_use) {
|
||||
target_process->nso_infos[i].info.base_address = base_address;
|
||||
target_process->nso_infos[i].info.size = size;
|
||||
std::copy(build_id, build_id + sizeof(target_process->nso_infos[i].info.build_id), target_process->nso_infos[i].info.build_id);
|
||||
target_process->nso_infos[i].in_use = true;
|
||||
return;
|
||||
}
|
||||
|
||||
auto nso_info_it = std::find_if_not(target_process->nso_infos.begin(), target_process->nso_infos.end(), std::mem_fn(&Registration::NsoInfoHolder::in_use));
|
||||
if (nso_info_it != target_process->nso_infos.end()) {
|
||||
nso_info_it->info.base_address = base_address;
|
||||
nso_info_it->info.size = size;
|
||||
std::copy(build_id, build_id + sizeof(nso_info_it->info.build_id), nso_info_it->info.build_id);
|
||||
nso_info_it->in_use = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,13 +128,12 @@ Result Registration::AddNrrInfo(u64 index, MappedCodeMemory *nrr_info) {
|
|||
return 0x7009;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < NRR_INFO_MAX; i++) {
|
||||
if (!target_process->nrr_infos[i].IsActive()) {
|
||||
target_process->nrr_infos[i] = *nrr_info;
|
||||
return 0;
|
||||
}
|
||||
auto nrr_info_it = std::find_if_not(target_process->nrr_infos.begin(), target_process->nrr_infos.end(), std::mem_fn(&MappedCodeMemory::IsActive));
|
||||
if (nrr_info_it == target_process->nrr_infos.end()) {
|
||||
return 0x7009;
|
||||
}
|
||||
return 0x7009;
|
||||
*nrr_info_it = *nrr_info;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result Registration::RemoveNrrInfo(u64 index, u64 base_address) {
|
||||
|
@ -193,7 +190,7 @@ bool Registration::IsNroAlreadyLoaded(u64 index, u8 *build_id) {
|
|||
}
|
||||
|
||||
for (unsigned int i = 0; i < NRO_INFO_MAX; i++) {
|
||||
if (target_process->nro_infos[i].in_use && std::memcmp(target_process->nro_infos[i].build_id, build_id, 0x20) == 0) {
|
||||
if (target_process->nro_infos[i].in_use && std::equal(build_id, build_id + 0x20, target_process->nro_infos[i].build_id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -207,20 +204,18 @@ void Registration::AddNroToProcess(u64 index, MappedCodeMemory *nro, MappedCodeM
|
|||
return;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < NRO_INFO_MAX; i++) {
|
||||
if (!target_process->nro_infos[i].in_use) {
|
||||
target_process->nro_infos[i].base_address = nro->code_memory_address;
|
||||
target_process->nro_infos[i].nro_heap_address = nro->base_address;
|
||||
target_process->nro_infos[i].nro_heap_size = nro->size;
|
||||
target_process->nro_infos[i].bss_heap_address = bss->base_address;
|
||||
target_process->nro_infos[i].bss_heap_size = bss->size;
|
||||
target_process->nro_infos[i].text_size = text_size;
|
||||
target_process->nro_infos[i].ro_size = ro_size;
|
||||
target_process->nro_infos[i].rw_size = rw_size;
|
||||
std::copy(build_id, build_id + sizeof(target_process->nro_infos[i].build_id), target_process->nro_infos[i].build_id);
|
||||
target_process->nro_infos[i].in_use = true;
|
||||
break;
|
||||
}
|
||||
auto nro_info_it = std::find_if_not(target_process->nro_infos.begin(), target_process->nro_infos.end(), std::mem_fn(&Registration::NroInfo::in_use));
|
||||
if (nro_info_it != target_process->nro_infos.end()) {
|
||||
nro_info_it->base_address = nro->code_memory_address;
|
||||
nro_info_it->nro_heap_address = nro->base_address;
|
||||
nro_info_it->nro_heap_size = nro->size;
|
||||
nro_info_it->bss_heap_address = bss->base_address;
|
||||
nro_info_it->bss_heap_size = bss->size;
|
||||
nro_info_it->text_size = text_size;
|
||||
nro_info_it->ro_size = ro_size;
|
||||
nro_info_it->rw_size = rw_size;
|
||||
std::copy(build_id, build_id + sizeof(nro_info_it->build_id), nro_info_it->build_id);
|
||||
nro_info_it->in_use = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -254,11 +249,9 @@ Result Registration::GetNsoInfosForProcessId(Registration::NsoInfo *out, u32 max
|
|||
}
|
||||
u32 cur = 0;
|
||||
|
||||
if (max_out > 0) {
|
||||
for (unsigned int i = 0; i < NSO_INFO_MAX && cur < max_out; i++) {
|
||||
if (target_process->nso_infos[i].in_use) {
|
||||
out[cur++] = target_process->nso_infos[i].info;
|
||||
}
|
||||
for (unsigned int i = 0; i < NSO_INFO_MAX && cur < max_out; i++) {
|
||||
if (target_process->nso_infos[i].in_use) {
|
||||
out[cur++] = target_process->nso_infos[i].info;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <array>
|
||||
|
||||
#include "ldr_map.hpp"
|
||||
|
||||
|
@ -48,14 +49,14 @@ class Registration {
|
|||
u64 process_id;
|
||||
u64 title_id;
|
||||
Registration::TidSid tid_sid;
|
||||
Registration::NsoInfoHolder nso_infos[NSO_INFO_MAX];
|
||||
Registration::NroInfo nro_infos[NRO_INFO_MAX];
|
||||
MappedCodeMemory nrr_infos[NRR_INFO_MAX];
|
||||
std::array<Registration::NsoInfoHolder, NSO_INFO_MAX> nso_infos;
|
||||
std::array<Registration::NroInfo, NRO_INFO_MAX> nro_infos;
|
||||
std::array<MappedCodeMemory, NRR_INFO_MAX> nrr_infos;
|
||||
void *owner_ro_service;
|
||||
};
|
||||
|
||||
struct List {
|
||||
Registration::Process processes[REGISTRATION_LIST_MAX];
|
||||
std::array<Registration::Process, REGISTRATION_LIST_MAX> processes;
|
||||
u64 num_processes;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue