CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/FileLoaders/CachingFileLoader.h
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
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.
6
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.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include <map>
21
#include <mutex>
22
#include <thread>
23
24
#include "Common/CommonTypes.h"
25
#include "Core/Loaders.h"
26
27
class CachingFileLoader : public ProxiedFileLoader {
28
public:
29
CachingFileLoader(FileLoader *backend);
30
~CachingFileLoader();
31
32
bool Exists() override;
33
bool ExistsFast() override;
34
bool IsDirectory() override;
35
s64 FileSize() override;
36
37
size_t ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data, Flags flags = Flags::NONE) override {
38
return ReadAt(absolutePos, bytes * count, data, flags) / bytes;
39
}
40
size_t ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags = Flags::NONE) override;
41
42
private:
43
void Prepare();
44
void InitCache();
45
void ShutdownCache();
46
size_t ReadFromCache(s64 pos, size_t bytes, void *data);
47
// Guaranteed to read at least one block into the cache.
48
void SaveIntoCache(s64 pos, size_t bytes, Flags flags, bool readingAhead = false);
49
bool MakeCacheSpaceFor(size_t blocks, bool readingAhead);
50
void StartReadAhead(s64 pos);
51
52
enum {
53
BLOCK_SIZE = 65536,
54
BLOCK_SHIFT = 16,
55
MAX_BLOCKS_PER_READ = 16,
56
MAX_BLOCKS_CACHED = 4096, // 256 MB
57
BLOCK_READAHEAD = 4,
58
};
59
60
s64 filesize_ = 0;
61
int exists_ = -1;
62
int isDirectory_ = -1;
63
u64 generation_;
64
u64 oldestGeneration_;
65
size_t cacheSize_;
66
67
struct BlockInfo {
68
u8 *ptr;
69
u64 generation;
70
71
BlockInfo() : ptr(nullptr), generation(0) {
72
}
73
BlockInfo(u8 *p) : ptr(p), generation(0) {
74
}
75
};
76
77
std::map<s64, BlockInfo> blocks_;
78
std::recursive_mutex blocksMutex_;
79
bool aheadThreadRunning_ = false;
80
std::thread aheadThread_;
81
std::once_flag preparedFlag_;
82
};
83
84