From 452c61db7a2ab4df5894b46fee3d7534e8b3db1e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 3 Jun 2019 20:54:24 -0400 Subject: [PATCH 1/5] setsys_settings_items: Simplify buffer management in LoadConfiguration() We can use a std::string here instead of setting up a scope guard and manual allocations. We also don't need to care about null-termination, as c_str() will automatically ensure this is done when passing it into ini_parse_string(). --- .../source/set_mitm/setsys_settings_items.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/stratosphere/ams_mitm/source/set_mitm/setsys_settings_items.cpp b/stratosphere/ams_mitm/source/set_mitm/setsys_settings_items.cpp index ef483d696..e19965119 100644 --- a/stratosphere/ams_mitm/source/set_mitm/setsys_settings_items.cpp +++ b/stratosphere/ams_mitm/source/set_mitm/setsys_settings_items.cpp @@ -252,24 +252,20 @@ void SettingsItemManager::LoadConfiguration() { ON_SCOPE_EXIT { fsFileClose(&config_file); }; - + /* Allocate buffer. */ - char *config_buf = new char[0x10000]; - std::memset(config_buf, 0, 0x10000); - ON_SCOPE_EXIT { - delete[] config_buf; - }; - + std::string config_buf(0xFFFF, '\0'); + /* Read from file. */ if (R_SUCCEEDED(rc)) { size_t actual_size; - rc = fsFileRead(&config_file, 0, config_buf, 0xFFFF, FS_READOPTION_NONE, &actual_size); + rc = fsFileRead(&config_file, 0, config_buf.data(), config_buf.size(), FS_READOPTION_NONE, &actual_size); } - + if (R_SUCCEEDED(rc)) { - ini_parse_string(config_buf, SettingsItemIniHandler, &rc); + ini_parse_string(config_buf.c_str(), SettingsItemIniHandler, &rc); } - + /* Report error if we encountered one. */ if (R_FAILED(rc) && !g_threw_fatal) { g_threw_fatal = true; From dd10547ac23bdd9f834ab9864162978ef8105cd7 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 3 Jun 2019 20:57:52 -0400 Subject: [PATCH 2/5] setsys_settings_items: Less resource churn with combined key/value construction The previous string construction discards two temporary std::string instances (operator+ returns by value, not by reference), and creates a std::string that it doesn't need to (the one around key). Instead we can just append to the end of the initial std::string itself, saving on two unnecessary created strings. append() has a const char* overload as well (as does operator+), so we can just append the key string as is without creating an entire new string. --- .../source/set_mitm/setsys_settings_items.cpp | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/stratosphere/ams_mitm/source/set_mitm/setsys_settings_items.cpp b/stratosphere/ams_mitm/source/set_mitm/setsys_settings_items.cpp index e19965119..0f795e84c 100644 --- a/stratosphere/ams_mitm/source/set_mitm/setsys_settings_items.cpp +++ b/stratosphere/ams_mitm/source/set_mitm/setsys_settings_items.cpp @@ -153,8 +153,8 @@ static Result ParseValue(const char *name, const char *key, const char *val_tup) if (delimiter == NULL || value_len == 0 || type_len == 0) { return ResultSettingsItemValueInvalidFormat; } - - std::string kv = std::string(name) + "!" + std::string(key); + + std::string kv = std::string(name).append("!").append(key); SettingsItemValue value; if (strncasecmp(type, "str", type_len) == 0 || strncasecmp(type, "string", type_len) == 0) { @@ -242,7 +242,7 @@ static int SettingsItemIniHandler(void *user, const char *name, const char *key, return R_SUCCEEDED(rc) ? 1 : 0; } -void SettingsItemManager::LoadConfiguration() { +void SettingsItemManager::LoadConfiguration() { /* Open file. */ FsFile config_file; Result rc = Utils::OpenSdFile("/atmosphere/system_settings.ini", FS_OPEN_READ, &config_file); @@ -275,31 +275,31 @@ void SettingsItemManager::LoadConfiguration() { } Result SettingsItemManager::GetValueSize(const char *name, const char *key, u64 *out_size) { - std::string kv = std::string(name) + "!" + std::string(key); - + const std::string kv = std::string(name).append("!").append(key); + auto it = g_settings_items.find(kv); if (it == g_settings_items.end()) { return ResultSettingsItemNotFound; } - + *out_size = it->second.size; return ResultSuccess; } Result SettingsItemManager::GetValue(const char *name, const char *key, void *out, size_t max_size, u64 *out_size) { - std::string kv = std::string(name) + "!" + std::string(key); - + const std::string kv = std::string(name).append("!").append(key); + auto it = g_settings_items.find(kv); if (it == g_settings_items.end()) { return ResultSettingsItemNotFound; } - + size_t copy_size = it->second.size; if (max_size < copy_size) { copy_size = max_size; } *out_size = copy_size; - + memcpy(out, it->second.data, copy_size); return ResultSuccess; } From 7bdd4bb2d1a6d33f657e53c538a99ead758f7603 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 4 Jun 2019 06:28:31 -0400 Subject: [PATCH 3/5] bpcmitm_main: Add missing header guard Prevents potential inclusion issues. --- stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.hpp b/stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.hpp index 64345fbaa..db3e1395f 100644 --- a/stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.hpp +++ b/stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.hpp @@ -13,7 +13,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - + +#pragma once + #include #include #include From 89a83b0e5a822c71985f3c18dce731675be05fd9 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 4 Jun 2019 06:29:12 -0400 Subject: [PATCH 4/5] bpcmitm_main: Remove unnecessary includes Quite a few headers are unused in this header, so we can remove them to make compilation a tiny bit faster. --- stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.hpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.hpp b/stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.hpp index db3e1395f..f18c83b4c 100644 --- a/stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.hpp +++ b/stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.hpp @@ -16,14 +16,7 @@ #pragma once -#include -#include -#include -#include - #include -#include -#include constexpr u32 BpcMitmPriority = 32; constexpr u32 BpcMitmStackSize = 0x8000; From e7e5ef4e5d7a2b298a829cfe34bda95cbeeb7137 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sat, 8 Jun 2019 15:58:09 -0700 Subject: [PATCH 5/5] dmnt: update targetio file ops for new libnx api --- stratosphere/dmnt/source/dmnt_service_target_io.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stratosphere/dmnt/source/dmnt_service_target_io.cpp b/stratosphere/dmnt/source/dmnt_service_target_io.cpp index 614beb21c..7f130f303 100644 --- a/stratosphere/dmnt/source/dmnt_service_target_io.cpp +++ b/stratosphere/dmnt/source/dmnt_service_target_io.cpp @@ -172,7 +172,7 @@ Result DebugMonitorService::TargetIO_FileRead(InBuffer hnd, OutBuffer(read)); return rc; } @@ -189,7 +189,7 @@ Result DebugMonitorService::TargetIO_FileWrite(InBuffer hnd, InBuffer