2014-12-17 05:38:14 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
2013-09-05 00:17:46 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2014-08-17 17:45:50 +00:00
|
|
|
#pragma once
|
2013-09-05 00:17:46 +00:00
|
|
|
|
|
|
|
#include <fstream>
|
2016-09-18 00:38:01 +00:00
|
|
|
#include "common/common_types.h"
|
2013-09-05 00:17:46 +00:00
|
|
|
|
|
|
|
// defined in Version.cpp
|
2016-09-18 00:38:01 +00:00
|
|
|
extern const char* scm_rev_git_str;
|
2013-09-05 00:17:46 +00:00
|
|
|
|
|
|
|
// On disk format:
|
2016-09-18 00:38:01 +00:00
|
|
|
// header{
|
2013-09-05 00:17:46 +00:00
|
|
|
// u32 'DCAC';
|
|
|
|
// u32 version; // svn_rev
|
|
|
|
// u16 sizeof(key_type);
|
|
|
|
// u16 sizeof(value_type);
|
|
|
|
//}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
// key_value_pair{
|
2013-09-05 00:17:46 +00:00
|
|
|
// u32 value_size;
|
|
|
|
// key_type key;
|
|
|
|
// value_type[value_size] value;
|
|
|
|
//}
|
|
|
|
|
|
|
|
template <typename K, typename V>
|
2016-09-18 00:38:01 +00:00
|
|
|
class LinearDiskCacheReader {
|
2013-09-05 00:17:46 +00:00
|
|
|
public:
|
2016-09-18 00:38:01 +00:00
|
|
|
virtual void Read(const K& key, const V* value, u32 value_size) = 0;
|
2013-09-05 00:17:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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>
|
2016-09-18 00:38:01 +00:00
|
|
|
class LinearDiskCache {
|
2013-09-05 00:17:46 +00:00
|
|
|
public:
|
2014-04-01 22:20:08 +00:00
|
|
|
// return number of read entries
|
2016-09-18 00:38:01 +00:00
|
|
|
u32 OpenAndRead(const char* filename, LinearDiskCacheReader<K, V>& reader) {
|
2014-04-01 22:20:08 +00:00
|
|
|
using std::ios_base;
|
|
|
|
|
|
|
|
// close any currently opened file
|
|
|
|
Close();
|
|
|
|
m_num_entries = 0;
|
|
|
|
|
|
|
|
// try opening for reading/writing
|
|
|
|
OpenFStream(m_file, filename, ios_base::in | ios_base::out | ios_base::binary);
|
|
|
|
|
|
|
|
m_file.seekg(0, std::ios::end);
|
|
|
|
std::fstream::pos_type end_pos = m_file.tellg();
|
|
|
|
m_file.seekg(0, std::ios::beg);
|
|
|
|
std::fstream::pos_type start_pos = m_file.tellg();
|
|
|
|
std::streamoff file_size = end_pos - start_pos;
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
if (m_file.is_open() && ValidateHeader()) {
|
2014-04-01 22:20:08 +00:00
|
|
|
// good header, read some key/value pairs
|
|
|
|
K key;
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
V* value = nullptr;
|
2014-04-01 22:20:08 +00:00
|
|
|
u32 value_size;
|
|
|
|
u32 entry_number;
|
|
|
|
|
|
|
|
std::fstream::pos_type last_pos = m_file.tellg();
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
while (Read(&value_size)) {
|
|
|
|
std::streamoff next_extent =
|
|
|
|
(last_pos - start_pos) + sizeof(value_size) + value_size;
|
2014-04-01 22:20:08 +00:00
|
|
|
if (next_extent > file_size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
delete[] value;
|
|
|
|
value = new V[value_size];
|
|
|
|
|
|
|
|
// read key/value and pass to reader
|
2016-09-18 00:38:01 +00:00
|
|
|
if (Read(&key) && Read(value, value_size) && Read(&entry_number) &&
|
|
|
|
entry_number == m_num_entries + 1) {
|
2014-04-01 22:20:08 +00:00
|
|
|
reader.Read(key, value, value_size);
|
2016-09-18 00:38:01 +00:00
|
|
|
} else {
|
2014-04-01 22:20:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_num_entries++;
|
|
|
|
last_pos = m_file.tellg();
|
|
|
|
}
|
|
|
|
m_file.seekp(last_pos);
|
|
|
|
m_file.clear();
|
|
|
|
|
|
|
|
delete[] value;
|
|
|
|
return m_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;
|
|
|
|
}
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
void Sync() {
|
2014-04-01 22:20:08 +00:00
|
|
|
m_file.flush();
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
void Close() {
|
2014-04-01 22:20:08 +00:00
|
|
|
if (m_file.is_open())
|
|
|
|
m_file.close();
|
|
|
|
// clear any error flags
|
|
|
|
m_file.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Appends a key-value pair to the store.
|
2016-09-18 00:38:01 +00:00
|
|
|
void Append(const K& key, const V* value, u32 value_size) {
|
|
|
|
// TODO: Should do a check that we don't already have "key"? (I think each caller does that
|
|
|
|
// already.)
|
2014-04-01 22:20:08 +00:00
|
|
|
Write(&value_size);
|
|
|
|
Write(&key);
|
|
|
|
Write(value, value_size);
|
|
|
|
m_num_entries++;
|
|
|
|
Write(&m_num_entries);
|
|
|
|
}
|
2013-09-05 00:17:46 +00:00
|
|
|
|
|
|
|
private:
|
2016-09-18 00:38:01 +00:00
|
|
|
void WriteHeader() {
|
2014-04-01 22:20:08 +00:00
|
|
|
Write(&m_header);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
bool ValidateHeader() {
|
2014-04-01 22:20:08 +00:00
|
|
|
char file_header[sizeof(Header)];
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
return (Read(file_header, sizeof(Header)) &&
|
|
|
|
!memcmp((const char*)&m_header, file_header, sizeof(Header)));
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename D>
|
2016-09-18 00:38:01 +00:00
|
|
|
bool Write(const D* data, u32 count = 1) {
|
2014-04-01 22:20:08 +00:00
|
|
|
return m_file.write((const char*)data, count * sizeof(D)).good();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename D>
|
2016-09-18 00:38:01 +00:00
|
|
|
bool Read(const D* data, u32 count = 1) {
|
2014-04-01 22:20:08 +00:00
|
|
|
return m_file.read((char*)data, count * sizeof(D)).good();
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
struct Header {
|
|
|
|
Header() : id(*(u32*)"DCAC"), key_t_size(sizeof(K)), value_t_size(sizeof(V)) {
|
2014-04-01 22:20:08 +00:00
|
|
|
memcpy(ver, scm_rev_git_str, 40);
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 id;
|
|
|
|
const u16 key_t_size, value_t_size;
|
|
|
|
char ver[40];
|
|
|
|
|
|
|
|
} m_header;
|
|
|
|
|
|
|
|
std::fstream m_file;
|
|
|
|
u32 m_num_entries;
|
2013-09-05 00:17:46 +00:00
|
|
|
};
|