~ppsspp/ppsspp/ppsspp_1.3.0

« back to all changes in this revision

Viewing changes to Core/FileLoaders/RamCachingFileLoader.h

  • Committer: Sérgio Benjamim
  • Date: 2017-01-02 00:12:05 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20170102001205-cxbta9za203nmjwm
1.3.0 source (from ppsspp_1.3.0-r160.p5.l1762.a165.t83~56~ubuntu16.04.1.tar.xz).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "base/mutex.h"
 
22
#include "Common/CommonTypes.h"
 
23
#include "Core/Loaders.h"
 
24
 
 
25
class RamCachingFileLoader : public FileLoader {
 
26
public:
 
27
        RamCachingFileLoader(FileLoader *backend);
 
28
        ~RamCachingFileLoader() override;
 
29
 
 
30
        bool Exists() override;
 
31
        bool ExistsFast() override;
 
32
        bool IsDirectory() override;
 
33
        s64 FileSize() override;
 
34
        std::string Path() const override;
 
35
 
 
36
        void Seek(s64 absolutePos) override;
 
37
        size_t Read(size_t bytes, size_t count, void *data, Flags flags = Flags::NONE) override {
 
38
                return ReadAt(filepos_, bytes, count, data, flags);
 
39
        }
 
40
        size_t Read(size_t bytes, void *data, Flags flags = Flags::NONE) override {
 
41
                return ReadAt(filepos_, bytes, data, flags);
 
42
        }
 
43
        size_t ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data, Flags flags = Flags::NONE) override {
 
44
                return ReadAt(absolutePos, bytes * count, data, flags) / bytes;
 
45
        }
 
46
        size_t ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags = Flags::NONE) override;
 
47
 
 
48
private:
 
49
        void InitCache();
 
50
        void ShutdownCache();
 
51
        size_t ReadFromCache(s64 pos, size_t bytes, void *data);
 
52
        // Guaranteed to read at least one block into the cache.
 
53
        void SaveIntoCache(s64 pos, size_t bytes, Flags flags);
 
54
        void StartReadAhead(s64 pos);
 
55
        u32 NextAheadBlock();
 
56
 
 
57
        enum {
 
58
                BLOCK_SIZE = 65536,
 
59
                BLOCK_SHIFT = 16,
 
60
                MAX_BLOCKS_PER_READ = 16,
 
61
                BLOCK_READAHEAD = 4,
 
62
        };
 
63
 
 
64
        s64 filesize_;
 
65
        s64 filepos_;
 
66
        FileLoader *backend_;
 
67
        u8 *cache_;
 
68
        int exists_;
 
69
        int isDirectory_;
 
70
 
 
71
        std::vector<u8> blocks_;
 
72
        recursive_mutex blocksMutex_;
 
73
        mutable recursive_mutex backendMutex_;
 
74
        u32 aheadRemaining_;
 
75
        s64 aheadPos_;
 
76
        bool aheadThread_;
 
77
};