diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/host_memory.cpp | 22 | ||||
| -rw-r--r-- | src/core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/core/file_sys/patch_manager.cpp | 5 | ||||
| -rw-r--r-- | src/core/file_sys/romfs.cpp | 3 | ||||
| -rw-r--r-- | src/core/file_sys/vfs_cached.cpp | 63 | ||||
| -rw-r--r-- | src/core/file_sys/vfs_cached.h | 31 | ||||
| -rw-r--r-- | src/core/file_sys/vfs_vector.cpp | 19 | ||||
| -rw-r--r-- | src/core/file_sys/vfs_vector.h | 4 |
8 files changed, 123 insertions, 26 deletions
diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index 8e4f1f97a..01457d8c6 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp | |||
| @@ -14,6 +14,7 @@ | |||
| 14 | #ifndef _GNU_SOURCE | 14 | #ifndef _GNU_SOURCE |
| 15 | #define _GNU_SOURCE | 15 | #define _GNU_SOURCE |
| 16 | #endif | 16 | #endif |
| 17 | #include <boost/icl/interval_set.hpp> | ||
| 17 | #include <fcntl.h> | 18 | #include <fcntl.h> |
| 18 | #include <sys/mman.h> | 19 | #include <sys/mman.h> |
| 19 | #include <unistd.h> | 20 | #include <unistd.h> |
| @@ -415,6 +416,7 @@ public: | |||
| 415 | madvise(virtual_base, virtual_size, MADV_HUGEPAGE); | 416 | madvise(virtual_base, virtual_size, MADV_HUGEPAGE); |
| 416 | #endif | 417 | #endif |
| 417 | 418 | ||
| 419 | placeholders.add({0, virtual_size}); | ||
| 418 | good = true; | 420 | good = true; |
| 419 | } | 421 | } |
| 420 | 422 | ||
| @@ -423,6 +425,10 @@ public: | |||
| 423 | } | 425 | } |
| 424 | 426 | ||
| 425 | void Map(size_t virtual_offset, size_t host_offset, size_t length) { | 427 | void Map(size_t virtual_offset, size_t host_offset, size_t length) { |
| 428 | { | ||
| 429 | std::scoped_lock lock{placeholder_mutex}; | ||
| 430 | placeholders.subtract({virtual_offset, virtual_offset + length}); | ||
| 431 | } | ||
| 426 | 432 | ||
| 427 | void* ret = mmap(virtual_base + virtual_offset, length, PROT_READ | PROT_WRITE, | 433 | void* ret = mmap(virtual_base + virtual_offset, length, PROT_READ | PROT_WRITE, |
| 428 | MAP_SHARED | MAP_FIXED, fd, host_offset); | 434 | MAP_SHARED | MAP_FIXED, fd, host_offset); |
| @@ -433,6 +439,19 @@ public: | |||
| 433 | // The method name is wrong. We're still talking about the virtual range. | 439 | // The method name is wrong. We're still talking about the virtual range. |
| 434 | // We don't want to unmap, we want to reserve this memory. | 440 | // We don't want to unmap, we want to reserve this memory. |
| 435 | 441 | ||
| 442 | { | ||
| 443 | std::scoped_lock lock{placeholder_mutex}; | ||
| 444 | auto it = placeholders.find({virtual_offset - 1, virtual_offset + length + 1}); | ||
| 445 | |||
| 446 | if (it != placeholders.end()) { | ||
| 447 | size_t prev_upper = virtual_offset + length; | ||
| 448 | virtual_offset = std::min(virtual_offset, it->lower()); | ||
| 449 | length = std::max(it->upper(), prev_upper) - virtual_offset; | ||
| 450 | } | ||
| 451 | |||
| 452 | placeholders.add({virtual_offset, virtual_offset + length}); | ||
| 453 | } | ||
| 454 | |||
| 436 | void* ret = mmap(virtual_base + virtual_offset, length, PROT_NONE, | 455 | void* ret = mmap(virtual_base + virtual_offset, length, PROT_NONE, |
| 437 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); | 456 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); |
| 438 | ASSERT_MSG(ret != MAP_FAILED, "mmap failed: {}", strerror(errno)); | 457 | ASSERT_MSG(ret != MAP_FAILED, "mmap failed: {}", strerror(errno)); |
| @@ -476,6 +495,9 @@ private: | |||
| 476 | } | 495 | } |
| 477 | 496 | ||
| 478 | int fd{-1}; // memfd file descriptor, -1 is the error value of memfd_create | 497 | int fd{-1}; // memfd file descriptor, -1 is the error value of memfd_create |
| 498 | |||
| 499 | boost::icl::interval_set<size_t> placeholders; ///< Mapped placeholders | ||
| 500 | std::mutex placeholder_mutex; ///< Mutex for placeholders | ||
| 479 | }; | 501 | }; |
| 480 | 502 | ||
| 481 | #else // ^^^ Linux ^^^ vvv Generic vvv | 503 | #else // ^^^ Linux ^^^ vvv Generic vvv |
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 45328158f..e8bf68866 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -106,6 +106,8 @@ add_library(core STATIC | |||
| 106 | file_sys/system_archive/time_zone_binary.h | 106 | file_sys/system_archive/time_zone_binary.h |
| 107 | file_sys/vfs.cpp | 107 | file_sys/vfs.cpp |
| 108 | file_sys/vfs.h | 108 | file_sys/vfs.h |
| 109 | file_sys/vfs_cached.cpp | ||
| 110 | file_sys/vfs_cached.h | ||
| 109 | file_sys/vfs_concat.cpp | 111 | file_sys/vfs_concat.cpp |
| 110 | file_sys/vfs_concat.h | 112 | file_sys/vfs_concat.h |
| 111 | file_sys/vfs_layered.cpp | 113 | file_sys/vfs_layered.cpp |
diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index 4c80e13a9..f786f2add 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp | |||
| @@ -21,6 +21,7 @@ | |||
| 21 | #include "core/file_sys/patch_manager.h" | 21 | #include "core/file_sys/patch_manager.h" |
| 22 | #include "core/file_sys/registered_cache.h" | 22 | #include "core/file_sys/registered_cache.h" |
| 23 | #include "core/file_sys/romfs.h" | 23 | #include "core/file_sys/romfs.h" |
| 24 | #include "core/file_sys/vfs_cached.h" | ||
| 24 | #include "core/file_sys/vfs_layered.h" | 25 | #include "core/file_sys/vfs_layered.h" |
| 25 | #include "core/file_sys/vfs_vector.h" | 26 | #include "core/file_sys/vfs_vector.h" |
| 26 | #include "core/hle/service/filesystem/filesystem.h" | 27 | #include "core/hle/service/filesystem/filesystem.h" |
| @@ -380,11 +381,11 @@ static void ApplyLayeredFS(VirtualFile& romfs, u64 title_id, ContentRecordType t | |||
| 380 | 381 | ||
| 381 | auto romfs_dir = FindSubdirectoryCaseless(subdir, "romfs"); | 382 | auto romfs_dir = FindSubdirectoryCaseless(subdir, "romfs"); |
| 382 | if (romfs_dir != nullptr) | 383 | if (romfs_dir != nullptr) |
| 383 | layers.push_back(std::move(romfs_dir)); | 384 | layers.push_back(std::make_shared<CachedVfsDirectory>(romfs_dir)); |
| 384 | 385 | ||
| 385 | auto ext_dir = FindSubdirectoryCaseless(subdir, "romfs_ext"); | 386 | auto ext_dir = FindSubdirectoryCaseless(subdir, "romfs_ext"); |
| 386 | if (ext_dir != nullptr) | 387 | if (ext_dir != nullptr) |
| 387 | layers_ext.push_back(std::move(ext_dir)); | 388 | layers_ext.push_back(std::make_shared<CachedVfsDirectory>(ext_dir)); |
| 388 | } | 389 | } |
| 389 | 390 | ||
| 390 | // When there are no layers to apply, return early as there is no need to rebuild the RomFS | 391 | // When there are no layers to apply, return early as there is no need to rebuild the RomFS |
diff --git a/src/core/file_sys/romfs.cpp b/src/core/file_sys/romfs.cpp index fb5683a6b..614da2130 100644 --- a/src/core/file_sys/romfs.cpp +++ b/src/core/file_sys/romfs.cpp | |||
| @@ -9,6 +9,7 @@ | |||
| 9 | #include "core/file_sys/fsmitm_romfsbuild.h" | 9 | #include "core/file_sys/fsmitm_romfsbuild.h" |
| 10 | #include "core/file_sys/romfs.h" | 10 | #include "core/file_sys/romfs.h" |
| 11 | #include "core/file_sys/vfs.h" | 11 | #include "core/file_sys/vfs.h" |
| 12 | #include "core/file_sys/vfs_cached.h" | ||
| 12 | #include "core/file_sys/vfs_concat.h" | 13 | #include "core/file_sys/vfs_concat.h" |
| 13 | #include "core/file_sys/vfs_offset.h" | 14 | #include "core/file_sys/vfs_offset.h" |
| 14 | #include "core/file_sys/vfs_vector.h" | 15 | #include "core/file_sys/vfs_vector.h" |
| @@ -132,7 +133,7 @@ VirtualDir ExtractRomFS(VirtualFile file, RomFSExtractionType type) { | |||
| 132 | out = out->GetSubdirectories().front(); | 133 | out = out->GetSubdirectories().front(); |
| 133 | } | 134 | } |
| 134 | 135 | ||
| 135 | return out; | 136 | return std::make_shared<CachedVfsDirectory>(out); |
| 136 | } | 137 | } |
| 137 | 138 | ||
| 138 | VirtualFile CreateRomFS(VirtualDir dir, VirtualDir ext) { | 139 | VirtualFile CreateRomFS(VirtualDir dir, VirtualDir ext) { |
diff --git a/src/core/file_sys/vfs_cached.cpp b/src/core/file_sys/vfs_cached.cpp new file mode 100644 index 000000000..c3154ee81 --- /dev/null +++ b/src/core/file_sys/vfs_cached.cpp | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include "core/file_sys/vfs_cached.h" | ||
| 5 | #include "core/file_sys/vfs_types.h" | ||
| 6 | |||
| 7 | namespace FileSys { | ||
| 8 | |||
| 9 | CachedVfsDirectory::CachedVfsDirectory(VirtualDir& source_dir) | ||
| 10 | : name(source_dir->GetName()), parent(source_dir->GetParentDirectory()) { | ||
| 11 | for (auto& dir : source_dir->GetSubdirectories()) { | ||
| 12 | dirs.emplace(dir->GetName(), std::make_shared<CachedVfsDirectory>(dir)); | ||
| 13 | } | ||
| 14 | for (auto& file : source_dir->GetFiles()) { | ||
| 15 | files.emplace(file->GetName(), file); | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | CachedVfsDirectory::~CachedVfsDirectory() = default; | ||
| 20 | |||
| 21 | VirtualFile CachedVfsDirectory::GetFile(std::string_view file_name) const { | ||
| 22 | auto it = files.find(file_name); | ||
| 23 | if (it != files.end()) { | ||
| 24 | return it->second; | ||
| 25 | } | ||
| 26 | |||
| 27 | return nullptr; | ||
| 28 | } | ||
| 29 | |||
| 30 | VirtualDir CachedVfsDirectory::GetSubdirectory(std::string_view dir_name) const { | ||
| 31 | auto it = dirs.find(dir_name); | ||
| 32 | if (it != dirs.end()) { | ||
| 33 | return it->second; | ||
| 34 | } | ||
| 35 | |||
| 36 | return nullptr; | ||
| 37 | } | ||
| 38 | |||
| 39 | std::vector<VirtualFile> CachedVfsDirectory::GetFiles() const { | ||
| 40 | std::vector<VirtualFile> out; | ||
| 41 | for (auto& [file_name, file] : files) { | ||
| 42 | out.push_back(file); | ||
| 43 | } | ||
| 44 | return out; | ||
| 45 | } | ||
| 46 | |||
| 47 | std::vector<VirtualDir> CachedVfsDirectory::GetSubdirectories() const { | ||
| 48 | std::vector<VirtualDir> out; | ||
| 49 | for (auto& [dir_name, dir] : dirs) { | ||
| 50 | out.push_back(dir); | ||
| 51 | } | ||
| 52 | return out; | ||
| 53 | } | ||
| 54 | |||
| 55 | std::string CachedVfsDirectory::GetName() const { | ||
| 56 | return name; | ||
| 57 | } | ||
| 58 | |||
| 59 | VirtualDir CachedVfsDirectory::GetParentDirectory() const { | ||
| 60 | return parent; | ||
| 61 | } | ||
| 62 | |||
| 63 | } // namespace FileSys | ||
diff --git a/src/core/file_sys/vfs_cached.h b/src/core/file_sys/vfs_cached.h new file mode 100644 index 000000000..113acac12 --- /dev/null +++ b/src/core/file_sys/vfs_cached.h | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <string_view> | ||
| 7 | #include <vector> | ||
| 8 | #include "core/file_sys/vfs.h" | ||
| 9 | |||
| 10 | namespace FileSys { | ||
| 11 | |||
| 12 | class CachedVfsDirectory : public ReadOnlyVfsDirectory { | ||
| 13 | public: | ||
| 14 | CachedVfsDirectory(VirtualDir& source_directory); | ||
| 15 | |||
| 16 | ~CachedVfsDirectory() override; | ||
| 17 | VirtualFile GetFile(std::string_view file_name) const override; | ||
| 18 | VirtualDir GetSubdirectory(std::string_view dir_name) const override; | ||
| 19 | std::vector<VirtualFile> GetFiles() const override; | ||
| 20 | std::vector<VirtualDir> GetSubdirectories() const override; | ||
| 21 | std::string GetName() const override; | ||
| 22 | VirtualDir GetParentDirectory() const override; | ||
| 23 | |||
| 24 | private: | ||
| 25 | std::string name; | ||
| 26 | VirtualDir parent; | ||
| 27 | std::map<std::string, VirtualDir, std::less<>> dirs; | ||
| 28 | std::map<std::string, VirtualFile, std::less<>> files; | ||
| 29 | }; | ||
| 30 | |||
| 31 | } // namespace FileSys | ||
diff --git a/src/core/file_sys/vfs_vector.cpp b/src/core/file_sys/vfs_vector.cpp index af1df4c51..251d9d7c9 100644 --- a/src/core/file_sys/vfs_vector.cpp +++ b/src/core/file_sys/vfs_vector.cpp | |||
| @@ -67,23 +67,6 @@ VectorVfsDirectory::VectorVfsDirectory(std::vector<VirtualFile> files_, | |||
| 67 | 67 | ||
| 68 | VectorVfsDirectory::~VectorVfsDirectory() = default; | 68 | VectorVfsDirectory::~VectorVfsDirectory() = default; |
| 69 | 69 | ||
| 70 | VirtualFile VectorVfsDirectory::GetFile(std::string_view file_name) const { | ||
| 71 | if (!optimized_file_index_built) { | ||
| 72 | optimized_file_index.clear(); | ||
| 73 | for (size_t i = 0; i < files.size(); i++) { | ||
| 74 | optimized_file_index.emplace(files[i]->GetName(), i); | ||
| 75 | } | ||
| 76 | optimized_file_index_built = true; | ||
| 77 | } | ||
| 78 | |||
| 79 | const auto it = optimized_file_index.find(file_name); | ||
| 80 | if (it != optimized_file_index.end()) { | ||
| 81 | return files[it->second]; | ||
| 82 | } | ||
| 83 | |||
| 84 | return nullptr; | ||
| 85 | } | ||
| 86 | |||
| 87 | std::vector<VirtualFile> VectorVfsDirectory::GetFiles() const { | 70 | std::vector<VirtualFile> VectorVfsDirectory::GetFiles() const { |
| 88 | return files; | 71 | return files; |
| 89 | } | 72 | } |
| @@ -124,7 +107,6 @@ bool VectorVfsDirectory::DeleteSubdirectory(std::string_view subdir_name) { | |||
| 124 | } | 107 | } |
| 125 | 108 | ||
| 126 | bool VectorVfsDirectory::DeleteFile(std::string_view file_name) { | 109 | bool VectorVfsDirectory::DeleteFile(std::string_view file_name) { |
| 127 | optimized_file_index_built = false; | ||
| 128 | return FindAndRemoveVectorElement(files, file_name); | 110 | return FindAndRemoveVectorElement(files, file_name); |
| 129 | } | 111 | } |
| 130 | 112 | ||
| @@ -142,7 +124,6 @@ VirtualFile VectorVfsDirectory::CreateFile(std::string_view file_name) { | |||
| 142 | } | 124 | } |
| 143 | 125 | ||
| 144 | void VectorVfsDirectory::AddFile(VirtualFile file) { | 126 | void VectorVfsDirectory::AddFile(VirtualFile file) { |
| 145 | optimized_file_index_built = false; | ||
| 146 | files.push_back(std::move(file)); | 127 | files.push_back(std::move(file)); |
| 147 | } | 128 | } |
| 148 | 129 | ||
diff --git a/src/core/file_sys/vfs_vector.h b/src/core/file_sys/vfs_vector.h index c9955755b..bfedb6e42 100644 --- a/src/core/file_sys/vfs_vector.h +++ b/src/core/file_sys/vfs_vector.h | |||
| @@ -105,7 +105,6 @@ public: | |||
| 105 | VirtualDir parent = nullptr); | 105 | VirtualDir parent = nullptr); |
| 106 | ~VectorVfsDirectory() override; | 106 | ~VectorVfsDirectory() override; |
| 107 | 107 | ||
| 108 | VirtualFile GetFile(std::string_view file_name) const override; | ||
| 109 | std::vector<VirtualFile> GetFiles() const override; | 108 | std::vector<VirtualFile> GetFiles() const override; |
| 110 | std::vector<VirtualDir> GetSubdirectories() const override; | 109 | std::vector<VirtualDir> GetSubdirectories() const override; |
| 111 | bool IsWritable() const override; | 110 | bool IsWritable() const override; |
| @@ -127,9 +126,6 @@ private: | |||
| 127 | 126 | ||
| 128 | VirtualDir parent; | 127 | VirtualDir parent; |
| 129 | std::string name; | 128 | std::string name; |
| 130 | |||
| 131 | mutable std::map<std::string, size_t, std::less<>> optimized_file_index; | ||
| 132 | mutable bool optimized_file_index_built{}; | ||
| 133 | }; | 129 | }; |
| 134 | 130 | ||
| 135 | } // namespace FileSys | 131 | } // namespace FileSys |