From 7132f71cf010e4f13eb27c1a24d8878aa984b43f Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 29 Mar 2017 18:31:00 +0300 Subject: Remove extra memory copy in VirtFs::loadFile. Also add const into VirtFs::loadFile. --- src/fs/virtfs/virtfs.cpp | 8 +++++--- src/fs/virtfs/virtfs.h | 4 ++-- src/fs/virtfs/virtfs1_unittest.cc | 14 ++++++++------ src/fs/virtfs/virtfsdir.cpp | 6 +++--- src/fs/virtfs/virtfsdir.h | 6 +++--- src/fs/virtfs/virtfsfuncs.h | 6 +++--- src/fs/virtfs/virtfstools.cpp | 4 ++-- src/fs/virtfs/virtfszip.cpp | 13 ++++--------- src/fs/virtfs/virtfszip.h | 6 +++--- src/gui/windows/updaterwindow.cpp | 2 +- src/integrity_unittest.cc | 2 +- src/utils/translation/poparser.cpp | 2 +- src/utils/translation/translationmanager.cpp | 3 ++- src/utils/xml/libxml.cpp | 4 ++-- src/utils/xml/pugixml.cpp | 4 ++-- src/utils/xml/tinyxml2.cpp | 4 ++-- 16 files changed, 44 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/fs/virtfs/virtfs.cpp b/src/fs/virtfs/virtfs.cpp index 9d1432270..d9423f66e 100644 --- a/src/fs/virtfs/virtfs.cpp +++ b/src/fs/virtfs/virtfs.cpp @@ -562,8 +562,8 @@ namespace VirtFs return file->funcs->eof(file); } - char *loadFile(std::string filename, - int &restrict fileSize) + const char *loadFile(std::string filename, + int &restrict fileSize) { prepareFsPath(filename); if (checkPath(filename) == false) @@ -575,7 +575,9 @@ namespace VirtFs FOR_EACH (std::vector::const_iterator, it, mEntries) { VirtFsEntry *const entry = *it; - char *const buf = entry->funcs->loadFile(entry, filename, fileSize); + const char *const buf = entry->funcs->loadFile(entry, + filename, + fileSize); if (buf != nullptr) return buf; } diff --git a/src/fs/virtfs/virtfs.h b/src/fs/virtfs/virtfs.h index e09d6c6ac..ca74206e8 100644 --- a/src/fs/virtfs/virtfs.h +++ b/src/fs/virtfs/virtfs.h @@ -93,8 +93,8 @@ namespace VirtFs bool mountDirSilent2(std::string newDir, const Append append); #endif // UNITTESTS - char *loadFile(std::string filename, - int &restrict fileSize); + const char *loadFile(std::string filename, + int &restrict fileSize); } // namespace VirtFs extern const char *dirSeparator; diff --git a/src/fs/virtfs/virtfs1_unittest.cc b/src/fs/virtfs/virtfs1_unittest.cc index ee8e97e3e..a48f9829c 100644 --- a/src/fs/virtfs/virtfs1_unittest.cc +++ b/src/fs/virtfs/virtfs1_unittest.cc @@ -1433,8 +1433,8 @@ TEST_CASE("VirtFs1 loadFile1") VirtFs::mountDir(prefix + "data", Append_false); - char *const buffer = VirtFs::loadFile("test/test.txt", fileSize); - REQUIRE(static_cast(buffer) != nullptr); + const char *const buffer = VirtFs::loadFile("test/test.txt", fileSize); + REQUIRE(static_cast(buffer) != nullptr); REQUIRE(fileSize == 23); REQUIRE(strncmp(buffer, "test line 1\ntest line 2", 23) == 0); delete [] buffer; @@ -1459,8 +1459,9 @@ TEST_CASE("VirtFs1 loadFile2") SECTION("test 1") { - char *restrict buffer = VirtFs::loadFile("dir2//test.txt", fileSize); - REQUIRE(static_cast(buffer) != nullptr); + const char *restrict buffer = VirtFs::loadFile("dir2//test.txt", + fileSize); + REQUIRE(static_cast(buffer) != nullptr); REQUIRE(fileSize == 23); REQUIRE(strncmp(buffer, "test line 1\ntest line 2", 23) == 0); delete [] buffer; @@ -1468,8 +1469,9 @@ TEST_CASE("VirtFs1 loadFile2") SECTION("test 2") { - char *restrict buffer = VirtFs::loadFile("dir2\\/test.txt", fileSize); - REQUIRE(static_cast(buffer) != nullptr); + const char *restrict buffer = VirtFs::loadFile("dir2\\/test.txt", + fileSize); + REQUIRE(static_cast(buffer) != nullptr); REQUIRE(fileSize == 23); REQUIRE(strncmp(buffer, "test line 1\ntest line 2", 23) == 0); delete [] buffer; diff --git a/src/fs/virtfs/virtfsdir.cpp b/src/fs/virtfs/virtfsdir.cpp index 923d1409d..b6f197ae3 100644 --- a/src/fs/virtfs/virtfsdir.cpp +++ b/src/fs/virtfs/virtfsdir.cpp @@ -443,9 +443,9 @@ namespace VirtFsDir return pos < 0 || len < 0 || pos >= len; } - char *loadFile(VirtFsEntry *restrict const entry, - const std::string &restrict filename, - int &restrict fileSize) + const char *loadFile(VirtFsEntry *restrict const entry, + const std::string &restrict filename, + int &restrict fileSize) { VirtDirEntry *const dirEntry = static_cast(entry); const std::string path = entry->root + filename; diff --git a/src/fs/virtfs/virtfsdir.h b/src/fs/virtfs/virtfsdir.h index 985cc5dc2..37e472a12 100644 --- a/src/fs/virtfs/virtfsdir.h +++ b/src/fs/virtfs/virtfsdir.h @@ -82,9 +82,9 @@ namespace VirtFsDir int seek(VirtFile *restrict const file, const uint64_t pos); int eof(VirtFile *restrict const file); - char *loadFile(VirtFsEntry *restrict const entry, - const std::string &restrict fileName, - int &restrict fileSize); + const char *loadFile(VirtFsEntry *restrict const entry, + const std::string &restrict fileName, + int &restrict fileSize); } // namespace VirtFsDir #endif // UTILS_VIRTFSDIR_H diff --git a/src/fs/virtfs/virtfsfuncs.h b/src/fs/virtfs/virtfsfuncs.h index 05b74cc7d..21b7a3220 100644 --- a/src/fs/virtfs/virtfsfuncs.h +++ b/src/fs/virtfs/virtfsfuncs.h @@ -84,9 +84,9 @@ struct VirtFsFuncs final VirtFile *(*openAppend) (VirtFsEntry *restrict const entry, const std::string &filename); int (*eof) (VirtFile *restrict const file); - char *(*loadFile) (VirtFsEntry *restrict const entry, - const std::string &restrict fileName, - int &restrict fileSize); + const char *(*loadFile) (VirtFsEntry *restrict const entry, + const std::string &restrict fileName, + int &restrict fileSize); }; #endif // UTILS_VIRTFSFUNCS_H diff --git a/src/fs/virtfs/virtfstools.cpp b/src/fs/virtfs/virtfstools.cpp index cc11f5022..8bcc0d065 100644 --- a/src/fs/virtfs/virtfstools.cpp +++ b/src/fs/virtfs/virtfstools.cpp @@ -159,7 +159,7 @@ namespace VirtFs std::string loadTextFileString(const std::string &fileName) { int contentsLength; - char *fileContents = VirtFs::loadFile(fileName, contentsLength); + const char *fileContents = VirtFs::loadFile(fileName, contentsLength); if (!fileContents) { @@ -175,7 +175,7 @@ namespace VirtFs StringVect &lines) { int contentsLength; - char *fileContents = VirtFs::loadFile(fileName, contentsLength); + const char *fileContents = VirtFs::loadFile(fileName, contentsLength); if (!fileContents) { diff --git a/src/fs/virtfs/virtfszip.cpp b/src/fs/virtfs/virtfszip.cpp index 62ad591ae..d344a4c64 100644 --- a/src/fs/virtfs/virtfszip.cpp +++ b/src/fs/virtfs/virtfszip.cpp @@ -336,9 +336,9 @@ namespace VirtFsZip return file->mPos >= file->mSize; } - char *loadFile(VirtFsEntry *restrict const entry, - const std::string &restrict filename, - int &restrict fileSize) + const char *loadFile(VirtFsEntry *restrict const entry, + const std::string &restrict filename, + int &restrict fileSize) { VirtZipEntry *const zipEntry = static_cast(entry); FOR_EACH (std::vector::const_iterator, @@ -357,12 +357,7 @@ namespace VirtFsZip filename.c_str()); fileSize = header->uncompressSize; - // Allocate memory and load the file - char *restrict const buffer = new char[fileSize]; - if (fileSize > 0) - buffer[fileSize - 1] = 0; - memcpy(buffer, buf, fileSize); - return buffer; + return reinterpret_cast(buf); } } return nullptr; diff --git a/src/fs/virtfs/virtfszip.h b/src/fs/virtfs/virtfszip.h index 77196a72b..bf2d75f3a 100644 --- a/src/fs/virtfs/virtfszip.h +++ b/src/fs/virtfs/virtfszip.h @@ -71,9 +71,9 @@ namespace VirtFsZip int seek(VirtFile *restrict const file, const uint64_t pos); int eof(VirtFile *restrict const file); - char *loadFile(VirtFsEntry *restrict const entry, - const std::string &restrict fileName, - int &restrict fileSize); + const char *loadFile(VirtFsEntry *restrict const entry, + const std::string &restrict fileName, + int &restrict fileSize); } // namespace VirtFsZip #endif // UTILS_VIRTFSZIP_H diff --git a/src/gui/windows/updaterwindow.cpp b/src/gui/windows/updaterwindow.cpp index 082db75cd..6ef065eed 100644 --- a/src/gui/windows/updaterwindow.cpp +++ b/src/gui/windows/updaterwindow.cpp @@ -1069,7 +1069,7 @@ bool UpdaterWindow::validateFile(const std::string &filePath, unsigned long UpdaterWindow::getFileHash(const std::string &filePath) { int size = 0; - char *const buf = VirtFs::loadFile(filePath, size); + const char *const buf = VirtFs::loadFile(filePath, size); if (buf == nullptr) return 0; unsigned long res = Net::Download::adlerBuffer(buf, size); diff --git a/src/integrity_unittest.cc b/src/integrity_unittest.cc index aea7fe073..31157b0b6 100644 --- a/src/integrity_unittest.cc +++ b/src/integrity_unittest.cc @@ -76,7 +76,7 @@ static bool compareBuffers(const unsigned char *const buf2) { bool isCorrect(true); int sz = 0; - unsigned char *buf1 = reinterpret_cast( + const unsigned char *buf1 = reinterpret_cast( VirtFs::loadFile("hide.png", sz)); REQUIRE(buf1 != nullptr); REQUIRE(sz == 368); diff --git a/src/utils/translation/poparser.cpp b/src/utils/translation/poparser.cpp index 6ac1221a9..af436b7de 100644 --- a/src/utils/translation/poparser.cpp +++ b/src/utils/translation/poparser.cpp @@ -46,7 +46,7 @@ PoParser::PoParser() : void PoParser::openFile(const std::string &name) { int size; - char *buf = VirtFs::loadFile(getFileName(name), size); + const char *buf = VirtFs::loadFile(getFileName(name), size); if (buf) { diff --git a/src/utils/translation/translationmanager.cpp b/src/utils/translation/translationmanager.cpp index 12c5519c4..6acde15d6 100644 --- a/src/utils/translation/translationmanager.cpp +++ b/src/utils/translation/translationmanager.cpp @@ -93,7 +93,8 @@ bool TranslationManager::translateFile(const std::string &fileName, return false; int contentsLength; - char *fileContents = VirtFs::loadFile(fileName, contentsLength); + const char *fileContents = VirtFs::loadFile(fileName, + contentsLength); if (!fileContents) { diff --git a/src/utils/xml/libxml.cpp b/src/utils/xml/libxml.cpp index ccb3aca39..f7319f7ee 100644 --- a/src/utils/xml/libxml.cpp +++ b/src/utils/xml/libxml.cpp @@ -98,9 +98,9 @@ namespace XML valid = true; if (useResman == UseVirtFs_true) { - data = VirtFs::loadFile( + data = const_cast(VirtFs::loadFile( filename.c_str(), - size); + size)); } else { diff --git a/src/utils/xml/pugixml.cpp b/src/utils/xml/pugixml.cpp index 1b5a3278e..7ad2779cd 100644 --- a/src/utils/xml/pugixml.cpp +++ b/src/utils/xml/pugixml.cpp @@ -76,9 +76,9 @@ namespace XML valid = true; if (useResman == UseVirtFs_true) { - data = VirtFs::loadFile( + data = const_cast(VirtFs::loadFile( filename.c_str(), - size); + size)); } else { diff --git a/src/utils/xml/tinyxml2.cpp b/src/utils/xml/tinyxml2.cpp index c88930828..214d08f42 100644 --- a/src/utils/xml/tinyxml2.cpp +++ b/src/utils/xml/tinyxml2.cpp @@ -68,9 +68,9 @@ namespace XML valid = true; if (useResman == UseVirtFs_true) { - data = VirtFs::loadFile( + data = const_cast(VirtFs::loadFile( filename.c_str(), - size); + size)); } else { -- cgit v1.2.3-70-g09d2