diff options
author | Andrei Karas <akaras@inbox.ru> | 2017-03-29 18:31:00 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2017-03-29 18:31:00 +0300 |
commit | 7132f71cf010e4f13eb27c1a24d8878aa984b43f (patch) | |
tree | 330ad08a5ae15d0f4dc2d13461de1d5a47a69fe6 /src/fs/virtfs | |
parent | d9c0d1c3b2800b0c995393426b65a031203ca2f0 (diff) | |
download | plus-7132f71cf010e4f13eb27c1a24d8878aa984b43f.tar.gz plus-7132f71cf010e4f13eb27c1a24d8878aa984b43f.tar.bz2 plus-7132f71cf010e4f13eb27c1a24d8878aa984b43f.tar.xz plus-7132f71cf010e4f13eb27c1a24d8878aa984b43f.zip |
Remove extra memory copy in VirtFs::loadFile.
Also add const into VirtFs::loadFile.
Diffstat (limited to 'src/fs/virtfs')
-rw-r--r-- | src/fs/virtfs/virtfs.cpp | 8 | ||||
-rw-r--r-- | src/fs/virtfs/virtfs.h | 4 | ||||
-rw-r--r-- | src/fs/virtfs/virtfs1_unittest.cc | 14 | ||||
-rw-r--r-- | src/fs/virtfs/virtfsdir.cpp | 6 | ||||
-rw-r--r-- | src/fs/virtfs/virtfsdir.h | 6 | ||||
-rw-r--r-- | src/fs/virtfs/virtfsfuncs.h | 6 | ||||
-rw-r--r-- | src/fs/virtfs/virtfstools.cpp | 4 | ||||
-rw-r--r-- | src/fs/virtfs/virtfszip.cpp | 13 | ||||
-rw-r--r-- | src/fs/virtfs/virtfszip.h | 6 |
9 files changed, 33 insertions, 34 deletions
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<VirtFsEntry*>::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<void*>(buffer) != nullptr); + const char *const buffer = VirtFs::loadFile("test/test.txt", fileSize); + REQUIRE(static_cast<const void*>(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<void*>(buffer) != nullptr); + const char *restrict buffer = VirtFs::loadFile("dir2//test.txt", + fileSize); + REQUIRE(static_cast<const void*>(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<void*>(buffer) != nullptr); + const char *restrict buffer = VirtFs::loadFile("dir2\\/test.txt", + fileSize); + REQUIRE(static_cast<const void*>(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<VirtDirEntry*>(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<VirtZipEntry*>(entry); FOR_EACH (std::vector<ZipLocalHeader*>::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<const char*>(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 |