From 2beda7c2b3d2fe70ff36edf178d9bb2f5b308bf9 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 3 Aug 2018 14:33:59 -0400 Subject: kernel/process: Use accessors instead of class members for referencing segment array Using member variables for referencing the segments array increases the size of the class in memory for little benefit. The same behavior can be achieved through the use of accessors that just return the relevant segment. --- src/core/hle/kernel/process.cpp | 6 +++--- src/core/hle/kernel/process.h | 44 ++++++++++++++++++++++++++++++----------- src/core/loader/elf.cpp | 6 +++--- src/core/loader/nro.cpp | 2 +- src/core/loader/nso.cpp | 2 +- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 5403ceef5..edf34c5a3 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -142,9 +142,9 @@ void Process::LoadModule(SharedPtr module_, VAddr base_addr) { }; // Map CodeSet segments - MapSegment(module_->code, VMAPermission::ReadExecute, MemoryState::CodeStatic); - MapSegment(module_->rodata, VMAPermission::Read, MemoryState::CodeMutable); - MapSegment(module_->data, VMAPermission::ReadWrite, MemoryState::CodeMutable); + MapSegment(module_->CodeSegment(), VMAPermission::ReadExecute, MemoryState::CodeStatic); + MapSegment(module_->RODataSegment(), VMAPermission::Read, MemoryState::CodeMutable); + MapSegment(module_->DataSegment(), VMAPermission::ReadWrite, MemoryState::CodeMutable); } ResultVal Process::HeapAllocate(VAddr target, u64 size, VMAPermission perms) { diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index 98d8da35e..f29ee67c6 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -55,6 +55,12 @@ enum class ProcessStatus { Created, Running, Exited }; class ResourceLimit; struct CodeSet final : public Object { + struct Segment { + size_t offset = 0; + VAddr addr = 0; + u32 size = 0; + }; + static SharedPtr Create(std::string name); std::string GetTypeName() const override { @@ -69,24 +75,38 @@ struct CodeSet final : public Object { return HANDLE_TYPE; } - /// Name of the process - std::string name; + Segment& CodeSegment() { + return segments[0]; + } - std::shared_ptr> memory; + const Segment& CodeSegment() const { + return segments[0]; + } - struct Segment { - size_t offset = 0; - VAddr addr = 0; - u32 size = 0; - }; + Segment& RODataSegment() { + return segments[1]; + } - Segment segments[3]; - Segment& code = segments[0]; - Segment& rodata = segments[1]; - Segment& data = segments[2]; + const Segment& RODataSegment() const { + return segments[1]; + } + + Segment& DataSegment() { + return segments[2]; + } + + const Segment& DataSegment() const { + return segments[2]; + } + + std::shared_ptr> memory; + Segment segments[3]; VAddr entrypoint; + /// Name of the process + std::string name; + private: CodeSet(); ~CodeSet() override; diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index 352938dcb..a7133f5a6 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -311,11 +311,11 @@ SharedPtr ElfReader::LoadInto(u32 vaddr) { CodeSet::Segment* codeset_segment; u32 permission_flags = p->p_flags & (PF_R | PF_W | PF_X); if (permission_flags == (PF_R | PF_X)) { - codeset_segment = &codeset->code; + codeset_segment = &codeset->CodeSegment(); } else if (permission_flags == (PF_R)) { - codeset_segment = &codeset->rodata; + codeset_segment = &codeset->RODataSegment(); } else if (permission_flags == (PF_R | PF_W)) { - codeset_segment = &codeset->data; + codeset_segment = &codeset->DataSegment(); } else { LOG_ERROR(Loader, "Unexpected ELF PT_LOAD segment id {} with flags {:X}", i, p->p_flags); diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index 7d3ec2a76..dc053cdad 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp @@ -159,7 +159,7 @@ bool AppLoader_NRO::LoadNro(FileSys::VirtualFile file, VAddr load_base) { // Resize program image to include .bss section and page align each section bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset); } - codeset->data.size += bss_size; + codeset->DataSegment().size += bss_size; program_image.resize(static_cast(program_image.size()) + bss_size); // Load codeset for current process diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 06b1b33f4..fee7d58c6 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -127,7 +127,7 @@ VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base) { // Resize program image to include .bss section and page align each section bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset); } - codeset->data.size += bss_size; + codeset->DataSegment().size += bss_size; const u32 image_size{PageAlignSize(static_cast(program_image.size()) + bss_size)}; program_image.resize(image_size); -- cgit v1.2.3 From e649db8c6bbb3e76f610fe9511e2dfc33bd6b322 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 3 Aug 2018 14:42:28 -0400 Subject: kernel/process: Use std::array where applicable --- src/core/hle/kernel/process.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index f29ee67c6..992689186 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include @@ -101,7 +102,7 @@ struct CodeSet final : public Object { std::shared_ptr> memory; - Segment segments[3]; + std::array segments; VAddr entrypoint; /// Name of the process -- cgit v1.2.3