1
// Copyright (c) 2012- PPSSPP Project.
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
22
#include "base/mutex.h"
23
#include "Common/Common.h"
24
#include "Core/Loaders.h"
26
class DiskCachingFileLoaderCache;
28
class DiskCachingFileLoader : public FileLoader {
30
DiskCachingFileLoader(FileLoader *backend);
31
~DiskCachingFileLoader() override;
33
bool Exists() override;
34
bool ExistsFast() override;
35
bool IsDirectory() override;
36
s64 FileSize() override;
37
std::string Path() const override;
39
void Seek(s64 absolutePos) override;
40
size_t Read(size_t bytes, size_t count, void *data, Flags flags = Flags::NONE) override {
41
return ReadAt(filepos_, bytes, count, data, flags);
43
size_t Read(size_t bytes, void *data, Flags flags = Flags::NONE) override {
44
return ReadAt(filepos_, bytes, data, flags);
46
size_t ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data, Flags flags = Flags::NONE) override {
47
return ReadAt(absolutePos, bytes * count, data, flags) / bytes;
49
size_t ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags = Flags::NONE) override;
51
static std::vector<std::string> GetCachedPathsInUse();
62
DiskCachingFileLoaderCache *cache_;
64
// We don't support concurrent disk cache access (we use memory cached indexes.)
65
// So we have to ensure there's only one of these per.
66
static std::map<std::string, DiskCachingFileLoaderCache *> caches_;
67
static recursive_mutex cachesMutex_;
70
class DiskCachingFileLoaderCache {
72
DiskCachingFileLoaderCache(const std::string &path, u64 filesize);
73
~DiskCachingFileLoaderCache();
84
return --refCount_ == 0;
87
static void SetCacheDir(const std::string &path) {
91
size_t ReadFromCache(s64 pos, size_t bytes, void *data);
92
// Guaranteed to read at least one block into the cache.
93
size_t SaveIntoCache(FileLoader *backend, s64 pos, size_t bytes, void *data, FileLoader::Flags flags);
98
void InitCache(const std::string &path);
100
bool MakeCacheSpaceFor(size_t blocks);
101
void RebalanceGenerations();
102
u32 AllocateBlock(u32 indexPos);
105
bool ReadBlockData(u8 *dest, BlockInfo &info, size_t offset, size_t size);
106
void WriteBlockData(BlockInfo &info, u8 *src);
107
void WriteIndexData(u32 indexPos, BlockInfo &info);
108
s64 GetBlockOffset(u32 block);
110
std::string MakeCacheFilePath(const std::string &path);
111
std::string MakeCacheFilename(const std::string &path);
112
bool LoadCacheFile(const std::string &path);
113
void LoadCacheIndex();
114
void CreateCacheFile(const std::string &path);
115
bool LockCacheFile(bool lockStatus);
116
bool RemoveCacheFile(const std::string &path);
117
void CloseFileHandle();
120
u32 DetermineMaxBlocks();
121
u32 CountCachedFiles();
122
void GarbageCollectCacheFiles(u64 goalBytes);
131
// index[filesize / blockSize] <-- ~500 KB for 4GB
132
// 32 (fileoffset - headersize) / blockSize -> -1=not present
135
// blocks[up to maxBlocks]
140
DEFAULT_BLOCK_SIZE = 65536,
141
MAX_BLOCKS_PER_READ = 16,
142
MAX_BLOCKS_LOWER_BOUND = 256, // 16 MB
143
MAX_BLOCKS_UPPER_BOUND = 8192, // 512 MB
144
INVALID_BLOCK = 0xFFFFFFFF,
145
INVALID_INDEX = 0xFFFFFFFF,
152
u16 oldestGeneration_;
157
recursive_mutex lock_;
158
std::string origPath_;
170
FLAG_LOCKED = 1 << 0,
178
BlockInfo() : block(-1), generation(0), hits(0) {
182
std::vector<BlockInfo> index_;
183
std::vector<u32> blockIndexLookup_;
188
static std::string cacheDir_;