diff options
Diffstat (limited to '')
-rw-r--r-- | src/core/file_sys/vfs_real.h | 46 |
1 files changed, 38 insertions, 8 deletions
diff --git a/src/core/file_sys/vfs_real.h b/src/core/file_sys/vfs_real.h index acde1ac89..26ea7df62 100644 --- a/src/core/file_sys/vfs_real.h +++ b/src/core/file_sys/vfs_real.h @@ -3,8 +3,11 @@ #pragma once +#include <map> +#include <mutex> +#include <optional> #include <string_view> -#include <boost/container/flat_map.hpp> +#include "common/intrusive_list.h" #include "core/file_sys/mode.h" #include "core/file_sys/vfs.h" @@ -14,6 +17,13 @@ class IOFile; namespace FileSys { +struct FileReference : public Common::IntrusiveListBaseNode<FileReference> { + std::shared_ptr<Common::FS::IOFile> file{}; +}; + +class RealVfsFile; +class RealVfsDirectory; + class RealVfsFilesystem : public VfsFilesystem { public: RealVfsFilesystem(); @@ -35,10 +45,31 @@ public: bool DeleteDirectory(std::string_view path) override; private: - boost::container::flat_map<std::string, std::weak_ptr<Common::FS::IOFile>> cache; + using ReferenceListType = Common::IntrusiveListBaseTraits<FileReference>::ListType; + std::map<std::string, std::weak_ptr<VfsFile>, std::less<>> cache; + ReferenceListType open_references; + ReferenceListType closed_references; + std::mutex list_lock; + size_t num_open_files{}; + +private: + friend class RealVfsFile; + std::unique_lock<std::mutex> RefreshReference(const std::string& path, Mode perms, + FileReference& reference); + void DropReference(std::unique_ptr<FileReference>&& reference); + +private: + friend class RealVfsDirectory; + VirtualFile OpenFileFromEntry(std::string_view path, std::optional<u64> size, + Mode perms = Mode::Read); + +private: + void EvictSingleReferenceLocked(); + void InsertReferenceIntoListLocked(FileReference& reference); + void RemoveReferenceFromListLocked(FileReference& reference); }; -// An implmentation of VfsFile that represents a file on the user's computer. +// An implementation of VfsFile that represents a file on the user's computer. class RealVfsFile : public VfsFile { friend class RealVfsDirectory; friend class RealVfsFilesystem; @@ -57,16 +88,15 @@ public: bool Rename(std::string_view name) override; private: - RealVfsFile(RealVfsFilesystem& base, std::shared_ptr<Common::FS::IOFile> backing, - const std::string& path, Mode perms = Mode::Read); - - void Close(); + RealVfsFile(RealVfsFilesystem& base, std::unique_ptr<FileReference> reference, + const std::string& path, Mode perms = Mode::Read, std::optional<u64> size = {}); RealVfsFilesystem& base; - std::shared_ptr<Common::FS::IOFile> backing; + std::unique_ptr<FileReference> reference; std::string path; std::string parent_path; std::vector<std::string> path_components; + std::optional<u64> size; Mode perms; }; |