From 08d9de69074ee289fc38a99fdf442727221a93fe Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sun, 8 Mar 2020 16:54:32 -0700 Subject: [PATCH] ldr: optimize memory usage for tracking system programs --- .../loader/source/ldr_launch_record.cpp | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/stratosphere/loader/source/ldr_launch_record.cpp b/stratosphere/loader/source/ldr_launch_record.cpp index aca54a1ea..4135d4f6e 100644 --- a/stratosphere/loader/source/ldr_launch_record.cpp +++ b/stratosphere/loader/source/ldr_launch_record.cpp @@ -22,15 +22,43 @@ namespace ams::ldr { /* Global cache. */ std::set g_launched_programs; + constexpr size_t NumSystemProgramIds = ncm::SystemProgramId::End.value - ncm::SystemProgramId::Start.value + 1; + static_assert(util::IsPowerOfTwo(NumSystemProgramIds)); + static_assert(util::IsAligned(NumSystemProgramIds, BITSIZEOF(u64))); + + bool IsTrackableSystemProgramId(ncm::ProgramId program_id) { + return ncm::SystemProgramId::Start <= program_id && program_id <= ncm::SystemProgramId::End; + } + + u64 g_system_launch_records[NumSystemProgramIds / BITSIZEOF(u64)]; + + void SetLaunchedSystemProgram(ncm::SystemProgramId program_id) { + const u64 val = program_id.value - ncm::SystemProgramId::Start.value; + g_system_launch_records[val / BITSIZEOF(u64)] |= (1ul << (val % BITSIZEOF(u64))); + } + + bool HasLaunchedSystemProgram(ncm::SystemProgramId program_id) { + const u64 val = program_id.value - ncm::SystemProgramId::Start.value; + return (g_system_launch_records[val / BITSIZEOF(u64)] & (1ul << (val % BITSIZEOF(u64)))) != 0; + } + } /* Launch Record API. */ bool HasLaunchedProgram(ncm::ProgramId program_id) { - return g_launched_programs.find(static_cast(program_id)) != g_launched_programs.end(); + if (IsTrackableSystemProgramId(program_id)) { + return HasLaunchedSystemProgram(ncm::SystemProgramId{program_id.value}); + } else { + return g_launched_programs.find(static_cast(program_id)) != g_launched_programs.end(); + } } void SetLaunchedProgram(ncm::ProgramId program_id) { - g_launched_programs.insert(static_cast(program_id)); + if (IsTrackableSystemProgramId(program_id)) { + SetLaunchedSystemProgram(ncm::SystemProgramId{program_id.value}); + } else { + g_launched_programs.insert(static_cast(program_id)); + } } }