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/RamCachingFileLoader.h
Views: 1401
1
// Copyright (c) 2015- 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 <vector>
21
#include <mutex>
22
#include <thread>
23
24
#include "Common/CommonTypes.h"
25
#include "Core/Loaders.h"
26
27
class RamCachingFileLoader : public ProxiedFileLoader {
28
public:
29
RamCachingFileLoader(FileLoader *backend);
30
~RamCachingFileLoader();
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
void Cancel() override;
43
44
private:
45
void InitCache();
46
void ShutdownCache();
47
size_t ReadFromCache(s64 pos, size_t bytes, void *data);
48
// Guaranteed to read at least one block into the cache.
49
void SaveIntoCache(s64 pos, size_t bytes, Flags flags);
50
void StartReadAhead(s64 pos);
51
u32 NextAheadBlock();
52
53
enum {
54
BLOCK_SIZE = 65536,
55
BLOCK_SHIFT = 16,
56
MAX_BLOCKS_PER_READ = 16,
57
BLOCK_READAHEAD = 4,
58
};
59
60
s64 filesize_ = 0;
61
u8 *cache_ = nullptr;
62
int exists_ = -1;
63
int isDirectory_ = -1;
64
65
std::vector<u8> blocks_;
66
std::mutex blocksMutex_;
67
u32 aheadRemaining_;
68
s64 aheadPos_;
69
std::thread aheadThread_;
70
bool aheadThreadRunning_ = false;
71
bool aheadCancel_ = false;
72
};
73
74