1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
// Copyright (C) 2003 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#ifndef _LINEAR_DISKCACHE
#define _LINEAR_DISKCACHE
#include "Common.h"
#include <fstream>
// Update this to the current SVN revision every time you change shader generation code.
// We don't automatically get this from SVN_REV because that would mean regenerating the
// shader cache for every revision, graphics-related or not, which is simply annoying.
enum
{
LINEAR_DISKCACHE_VER = 6964
};
// On disk format:
//header{
// u32 'DCAC';
// u32 version; // svn_rev
// u16 sizeof(key_type);
// u16 sizeof(value_type);
//}
//key_value_pair{
// u32 value_size;
// key_type key;
// value_type[value_size] value;
//}
template <typename K, typename V>
class LinearDiskCacheReader
{
public:
virtual void Read(const K &key, const V *value, u32 value_size) = 0;
};
// Dead simple unsorted key-value store with append functionality.
// No random read functionality, all reading is done in OpenAndRead.
// Keys and values can contain any characters, including \0.
//
// Suitable for caching generated shader bytecode between executions.
// Not tuned for extreme performance but should be reasonably fast.
// Does not support keys or values larger than 2GB, which should be reasonable.
// Keys must have non-zero length; values can have zero length.
// K and V are some POD type
// K : the key type
// V : value array type
template <typename K, typename V>
class LinearDiskCache
{
public:
// return number of read entries
u32 OpenAndRead(const char *filename, LinearDiskCacheReader<K, V> &reader)
{
using std::ios_base;
// close any currently opened file
Close();
// try opening for reading/writing
m_file.open(filename, ios_base::in | ios_base::out | ios_base::binary | ios_base::app);
if (m_file.is_open() && ValidateHeader())
{
// good header, read some key/value pairs
u32 num_entries = 0;
K key;
V *value = NULL;
u32 value_size;
while (Read(&value_size))
{
delete[] value;
value = new V[value_size];
// read key/value and pass to reader
if (Read(&key) && Read(value, value_size))
reader.Read(key, value, value_size);
else
break;
++num_entries;
}
m_file.clear();
delete[] value;
return num_entries;
}
// failed to open file for reading or bad header
// close and recreate file
Close();
m_file.open(filename, ios_base::out | ios_base::trunc | ios_base::binary);
WriteHeader();
return 0;
}
void Sync()
{
m_file.flush();
}
void Close()
{
if (m_file.is_open())
m_file.close();
// clear any error flags
m_file.clear();
}
// Appends a key-value pair to the store.
void Append(const K &key, const V *value, u32 value_size)
{
// TODO: Should do a check that we don't already have "key"?
Write(&value_size);
Write(&key);
Write(value, value_size);
}
private:
void WriteHeader()
{
Write(&m_header);
}
bool ValidateHeader()
{
char file_header[sizeof(Header)];
return (Read(file_header, sizeof(Header))
&& !memcmp((const char*)&m_header, file_header, sizeof(Header)));
}
template <typename D>
bool Write(const D *data, u32 count = 1)
{
return m_file.write((const char*)data, count * sizeof(D)).good();
}
template <typename D>
bool Read(const D *data, u32 count = 1)
{
return m_file.read((char*)data, count * sizeof(D)).good();
}
struct Header
{
Header()
: id(*(u32*)"DCAC")
, ver(LINEAR_DISKCACHE_VER)
, key_t_size(sizeof(K))
, value_t_size(sizeof(V))
{}
const u32 id, ver;
const u16 key_t_size, value_t_size;
} m_header;
std::fstream m_file;
};
#endif // _LINEAR_DISKCACHE
|