CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/FileLoaders/CachingFileLoader.h
Views: 1401
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#pragma once1819#include <map>20#include <mutex>21#include <thread>2223#include "Common/CommonTypes.h"24#include "Core/Loaders.h"2526class CachingFileLoader : public ProxiedFileLoader {27public:28CachingFileLoader(FileLoader *backend);29~CachingFileLoader();3031bool Exists() override;32bool ExistsFast() override;33bool IsDirectory() override;34s64 FileSize() override;3536size_t ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data, Flags flags = Flags::NONE) override {37return ReadAt(absolutePos, bytes * count, data, flags) / bytes;38}39size_t ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags = Flags::NONE) override;4041private:42void Prepare();43void InitCache();44void ShutdownCache();45size_t ReadFromCache(s64 pos, size_t bytes, void *data);46// Guaranteed to read at least one block into the cache.47void SaveIntoCache(s64 pos, size_t bytes, Flags flags, bool readingAhead = false);48bool MakeCacheSpaceFor(size_t blocks, bool readingAhead);49void StartReadAhead(s64 pos);5051enum {52BLOCK_SIZE = 65536,53BLOCK_SHIFT = 16,54MAX_BLOCKS_PER_READ = 16,55MAX_BLOCKS_CACHED = 4096, // 256 MB56BLOCK_READAHEAD = 4,57};5859s64 filesize_ = 0;60int exists_ = -1;61int isDirectory_ = -1;62u64 generation_;63u64 oldestGeneration_;64size_t cacheSize_;6566struct BlockInfo {67u8 *ptr;68u64 generation;6970BlockInfo() : ptr(nullptr), generation(0) {71}72BlockInfo(u8 *p) : ptr(p), generation(0) {73}74};7576std::map<s64, BlockInfo> blocks_;77std::recursive_mutex blocksMutex_;78bool aheadThreadRunning_ = false;79std::thread aheadThread_;80std::once_flag preparedFlag_;81};828384