mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-04-23 21:14:49 +00:00
Stratosphere: Use modern C++ idioms in some places
* algorithms like std::for_each are used instead of raw loops
This commit is contained in:
parent
f41b780c0a
commit
c10dca48fe
5 changed files with 45 additions and 56 deletions
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
#include "iwaitable.hpp"
|
||||
|
@ -22,9 +23,7 @@ class IEvent : public IWaitable {
|
|||
}
|
||||
|
||||
~IEvent() {
|
||||
for (auto &h : this->handles) {
|
||||
svcCloseHandle(h);
|
||||
}
|
||||
std::for_each(handles.begin(), handles.end(), svcCloseHandle);
|
||||
}
|
||||
|
||||
virtual Result signal_event() = 0;
|
||||
|
@ -50,4 +49,4 @@ class IEvent : public IWaitable {
|
|||
/* TODO: Panic. */
|
||||
return 0xCAFE;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
@ -74,8 +74,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];
|
||||
|
|
|
@ -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) {
|
||||
|
@ -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
Reference in a new issue