From 8eec1dc810604813501286320813ce7d7a461494 Mon Sep 17 00:00:00 2001 From: Kp Date: Sat, 13 May 2023 15:02:55 +0000 Subject: [PATCH] Eliminate unnecessary string copy in PHYSFSX_addRelToSearchPath Pass a mutable buffer from the caller, and allow PHYSFSX_addRelToSearchPath to adjust the capitalization in that buffer, rather than creating a copy in a stack local. This can slightly affect status messages that use the name, but otherwise should have no effect on the game. --- common/include/physfsx.h | 8 ++++---- similar/editor/med.cpp | 20 ++++++++++++++++---- similar/main/inferno.cpp | 7 +++++-- similar/main/mission.cpp | 3 ++- similar/misc/physfsx.cpp | 18 +++++++++--------- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/common/include/physfsx.h b/common/include/physfsx.h index 898a914f5..d49d8cf95 100644 --- a/common/include/physfsx.h +++ b/common/include/physfsx.h @@ -437,7 +437,7 @@ enum class physfs_search_path : int append, }; -PHYSFS_ErrorCode PHYSFSX_addRelToSearchPath(const char *relname, std::array &realPath, physfs_search_path); +PHYSFS_ErrorCode PHYSFSX_addRelToSearchPath(char *relname, std::array &realPath, physfs_search_path); void PHYSFSX_removeRelFromSearchPath(const char *relname); extern int PHYSFSX_fsize(const char *hogname); extern void PHYSFSX_listSearchPathContent(); @@ -483,8 +483,8 @@ using RAIIPHYSFS_LiteralMount = std::unique_ptr; -RAIIPHYSFS_LiteralMount make_PHYSFSX_LiteralMount(const char *const name, physfs_search_path); -RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(const char *const name, physfs_search_path position); +[[nodiscard]] +RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(char *const name, physfs_search_path position); extern int PHYSFSX_rename(const char *oldpath, const char *newpath); @@ -502,7 +502,7 @@ namespace dsx { bool PHYSFSX_init(int argc, char *argv[]); int PHYSFSX_checkSupportedArchiveTypes(); #if defined(DXX_BUILD_DESCENT_II) -RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(const char *const name1, const char *const name2, physfs_search_path); +RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(char *name1, char *name2, physfs_search_path); #endif } diff --git a/similar/editor/med.cpp b/similar/editor/med.cpp index 91ce99d8a..7b6633104 100644 --- a/similar/editor/med.cpp +++ b/similar/editor/med.cpp @@ -350,10 +350,22 @@ void init_editor() // first, make sure we can find the files we need std::array pathname; - PHYSFSX_addRelToSearchPath("editor/data", pathname, physfs_search_path::append); // look in source directory first (for work in progress) - PHYSFSX_addRelToSearchPath("editor", pathname, physfs_search_path::append); // then in editor directory - PHYSFSX_addRelToSearchPath("editor.zip", pathname, physfs_search_path::append); // then in a zip file - PHYSFSX_addRelToSearchPath("editor.dxa", pathname, physfs_search_path::append); // or addon pack + { + static char relname[]{"editor/data"}; + PHYSFSX_addRelToSearchPath(relname, pathname, physfs_search_path::append); // look in source directory first (for work in progress) + } + { + static char relname[]{"editor"}; + PHYSFSX_addRelToSearchPath(relname, pathname, physfs_search_path::append); // then in editor directory + } + { + static char relname[]{"editor.zip"}; + PHYSFSX_addRelToSearchPath(relname, pathname, physfs_search_path::append); // then in a zip file + } + { + static char relname[]{"editor.dxa"}; + PHYSFSX_addRelToSearchPath(relname, pathname, physfs_search_path::append); // or addon pack + } if (!ui_init()) { diff --git a/similar/main/inferno.cpp b/similar/main/inferno.cpp index f25ad8c7a..a7f63bdfb 100644 --- a/similar/main/inferno.cpp +++ b/similar/main/inferno.cpp @@ -465,11 +465,14 @@ static int main(int argc, char *argv[]) return(0); #if defined(DXX_BUILD_DESCENT_I) - const auto descent_hog = make_PHYSFSX_ComputedPathMount("descent.hog", physfs_search_path::append); + static char relname_descent_hog[]{"descent.hog"}; + const auto descent_hog = make_PHYSFSX_ComputedPathMount(relname_descent_hog, physfs_search_path::append); #define DXX_NAME_NUMBER "1" #define DXX_HOGFILE_NAMES "descent.hog" #elif defined(DXX_BUILD_DESCENT_II) - const auto descent_hog = make_PHYSFSX_ComputedPathMount("descent2.hog", "d2demo.hog", physfs_search_path::append); + static char relname_descent2_hog[]{"descent2.hog"}; + static char relname_d2demo_hog[]{"d2demo.hog"}; + const auto descent_hog = make_PHYSFSX_ComputedPathMount(relname_descent2_hog, relname_d2demo_hog, physfs_search_path::append); #define DXX_NAME_NUMBER "2" #define DXX_HOGFILE_NAMES "descent2.hog or d2demo.hog" #endif diff --git a/similar/main/mission.cpp b/similar/main/mission.cpp index 97dd17838..66a75b58e 100644 --- a/similar/main/mission.cpp +++ b/similar/main/mission.cpp @@ -938,7 +938,8 @@ static const char *load_mission(const mle *const mission) #endif { std::array pathname; - if (const auto r = PHYSFSX_addRelToSearchPath("descent.hog", pathname, physfs_search_path::prepend); r != PHYSFS_ERR_OK) + static char relname[]{"descent.hog"}; + if (const auto r = PHYSFSX_addRelToSearchPath(relname, pathname, physfs_search_path::prepend); r != PHYSFS_ERR_OK) #if defined(DXX_BUILD_DESCENT_I) Error("descent.hog not available!\n%s", PHYSFS_getErrorByCode(r)); #elif defined(DXX_BUILD_DESCENT_II) diff --git a/similar/misc/physfsx.cpp b/similar/misc/physfsx.cpp index 55a3cfa27..b29408389 100644 --- a/similar/misc/physfsx.cpp +++ b/similar/misc/physfsx.cpp @@ -235,7 +235,10 @@ bool PHYSFSX_init(int argc, char *argv[]) #endif std::array pathname; - PHYSFSX_addRelToSearchPath("data", pathname, physfs_search_path::append); // 'Data' subdirectory + { + static char relname[]{"data"}; + PHYSFSX_addRelToSearchPath(relname, pathname, physfs_search_path::append); // 'Data' subdirectory + } // For Macintosh, add the 'Resources' directory in the .app bundle to the searchpaths #if defined(__APPLE__) && defined(__MACH__) @@ -271,7 +274,7 @@ bool PHYSFSX_init(int argc, char *argv[]) } #if defined(DXX_BUILD_DESCENT_II) -RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(const char *const name1, const char *const name2, physfs_search_path position) +RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(char *const name1, char *const name2, physfs_search_path position) { auto pathname = std::make_unique>(); if (PHYSFSX_addRelToSearchPath(name1, *pathname.get(), position) == PHYSFS_ERR_OK || @@ -287,11 +290,8 @@ namespace dcx { // Add a searchpath, but that searchpath is relative to an existing searchpath // It will add the first one it finds and return 1, if it doesn't find any it returns 0 -PHYSFS_ErrorCode PHYSFSX_addRelToSearchPath(const char *relname, std::array &pathname, physfs_search_path add_to_end) +PHYSFS_ErrorCode PHYSFSX_addRelToSearchPath(char *const relname2, std::array &pathname, physfs_search_path add_to_end) { - char relname2[PATH_MAX]; - - snprintf(relname2, sizeof(relname2), "%s", relname); PHYSFSEXT_locateCorrectCase(relname2); if (!PHYSFSX_getRealPath(relname2, pathname)) @@ -307,13 +307,13 @@ PHYSFS_ErrorCode PHYSFSX_addRelToSearchPath(const char *relname, std::array((PHYSFS_tell)(file))); } -RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(const char *const name, physfs_search_path position) +RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(char *const name, physfs_search_path position) { auto pathname = std::make_unique>(); if (PHYSFSX_addRelToSearchPath(name, *pathname.get(), position) == PHYSFS_ERR_OK)