2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-02-19 17:05:26 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2010-02-19 17:05:26 +00:00
|
|
|
|
2017-09-09 19:52:35 +00:00
|
|
|
#include <algorithm>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <cstring>
|
2020-12-19 17:06:15 +00:00
|
|
|
#include <memory>
|
2014-03-12 19:33:41 +00:00
|
|
|
#include <string>
|
2016-01-02 06:20:01 +00:00
|
|
|
#include <type_traits>
|
2010-02-19 17:05:26 +00:00
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2020-09-15 10:29:41 +00:00
|
|
|
#include "Common/IOFile.h"
|
2017-09-09 19:52:35 +00:00
|
|
|
#include "Common/Version.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2010-02-19 17:05:26 +00:00
|
|
|
// On disk format:
|
2010-11-15 05:22:03 +00:00
|
|
|
// 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;
|
|
|
|
//}
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
template <typename K, typename V>
|
|
|
|
class LinearDiskCacheReader
|
|
|
|
{
|
|
|
|
public:
|
2016-01-21 20:27:56 +00:00
|
|
|
virtual void Read(const K& key, const V* value, u32 value_size) = 0;
|
2011-06-11 19:37:21 +00:00
|
|
|
};
|
2010-02-19 17:05:26 +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.
|
2010-11-15 05:22:03 +00:00
|
|
|
|
|
|
|
// K and V are some POD type
|
|
|
|
// K : the key type
|
|
|
|
// V : value array type
|
|
|
|
template <typename K, typename V>
|
|
|
|
class LinearDiskCache
|
|
|
|
{
|
2010-02-19 17:05:26 +00:00
|
|
|
public:
|
2010-11-15 05:22:03 +00:00
|
|
|
// return number of read entries
|
2014-03-12 19:33:41 +00:00
|
|
|
u32 OpenAndRead(const std::string& filename, LinearDiskCacheReader<K, V>& reader)
|
2010-11-15 05:22:03 +00:00
|
|
|
{
|
2017-12-25 21:53:44 +00:00
|
|
|
// Since we're reading/writing directly to the storage of K instances,
|
|
|
|
// K must be trivially copyable.
|
2016-01-02 06:20:01 +00:00
|
|
|
static_assert(std::is_trivially_copyable<K>::value, "K must be a trivially copyable type");
|
|
|
|
|
2010-11-15 05:22:03 +00:00
|
|
|
// close any currently opened file
|
|
|
|
Close();
|
2011-12-31 04:37:46 +00:00
|
|
|
m_num_entries = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-11-15 05:22:03 +00:00
|
|
|
// try opening for reading/writing
|
2020-11-04 19:59:34 +00:00
|
|
|
m_file.Open(filename, "r+b");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-04 19:59:34 +00:00
|
|
|
const u64 file_size = m_file.GetSize();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-05-04 21:47:23 +00:00
|
|
|
m_header.Init();
|
2020-11-04 19:59:34 +00:00
|
|
|
if (m_file.IsOpen() && ValidateHeader())
|
2010-11-15 05:22:03 +00:00
|
|
|
{
|
|
|
|
// good header, read some key/value pairs
|
2011-06-11 19:37:21 +00:00
|
|
|
K key;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-12-19 17:06:15 +00:00
|
|
|
std::unique_ptr<V[]> value = nullptr;
|
2015-09-29 02:27:27 +00:00
|
|
|
u32 value_size = 0;
|
|
|
|
u32 entry_number = 0;
|
2020-12-19 17:19:40 +00:00
|
|
|
u64 last_valid_value_start = m_file.Tell();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-04 19:59:34 +00:00
|
|
|
while (m_file.ReadArray(&value_size, 1))
|
2011-06-11 19:37:21 +00:00
|
|
|
{
|
2020-11-04 19:59:34 +00:00
|
|
|
const u64 next_extent = m_file.Tell() + sizeof(value_size) + value_size;
|
2011-12-31 04:37:46 +00:00
|
|
|
if (next_extent > file_size)
|
|
|
|
break;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-12-19 17:06:15 +00:00
|
|
|
// TODO: use make_unique_for_overwrite in C++20
|
|
|
|
value = std::unique_ptr<V[]>(new V[value_size]);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
// read key/value and pass to reader
|
2020-12-19 17:06:15 +00:00
|
|
|
if (m_file.ReadArray(&key, 1) && m_file.ReadArray(value.get(), value_size) &&
|
2020-11-04 19:59:34 +00:00
|
|
|
m_file.ReadArray(&entry_number, 1) && entry_number == m_num_entries + 1)
|
2011-12-31 04:37:46 +00:00
|
|
|
{
|
2020-12-19 17:19:40 +00:00
|
|
|
last_valid_value_start = m_file.Tell();
|
2020-12-19 17:06:15 +00:00
|
|
|
reader.Read(key, value.get(), value_size);
|
2011-12-31 04:37:46 +00:00
|
|
|
}
|
2011-06-11 19:37:21 +00:00
|
|
|
else
|
2011-12-31 04:37:46 +00:00
|
|
|
{
|
2011-06-11 19:37:21 +00:00
|
|
|
break;
|
2011-12-31 04:37:46 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2011-12-31 04:37:46 +00:00
|
|
|
m_num_entries++;
|
2010-11-15 05:22:03 +00:00
|
|
|
}
|
2020-11-04 19:59:34 +00:00
|
|
|
m_file.Clear();
|
2020-12-19 17:19:40 +00:00
|
|
|
m_file.Seek(last_valid_value_start, SEEK_SET);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2011-12-31 04:37:46 +00:00
|
|
|
return m_num_entries;
|
2010-11-15 05:22:03 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-11-15 05:22:03 +00:00
|
|
|
// failed to open file for reading or bad header
|
|
|
|
// close and recreate file
|
|
|
|
Close();
|
2020-11-04 19:59:34 +00:00
|
|
|
m_file.Open(filename, "wb");
|
2010-11-15 05:22:03 +00:00
|
|
|
WriteHeader();
|
|
|
|
return 0;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-04 19:59:34 +00:00
|
|
|
void Sync() { m_file.Flush(); }
|
2010-11-15 05:22:03 +00:00
|
|
|
void Close()
|
|
|
|
{
|
2020-11-04 19:59:34 +00:00
|
|
|
if (m_file.IsOpen())
|
|
|
|
m_file.Close();
|
2010-11-15 05:22:03 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-02-19 17:05:26 +00:00
|
|
|
// Appends a key-value pair to the store.
|
2016-01-21 20:27:56 +00:00
|
|
|
void Append(const K& key, const V* value, u32 value_size)
|
2010-11-15 05:22:03 +00:00
|
|
|
{
|
2011-12-31 04:37:46 +00:00
|
|
|
// TODO: Should do a check that we don't already have "key"? (I think each caller does that
|
|
|
|
// already.)
|
2020-11-04 19:59:34 +00:00
|
|
|
m_file.WriteArray(&value_size, 1);
|
|
|
|
m_file.WriteArray(&key, 1);
|
|
|
|
m_file.WriteArray(value, value_size);
|
2011-12-31 04:37:46 +00:00
|
|
|
m_num_entries++;
|
2020-11-04 19:59:34 +00:00
|
|
|
m_file.WriteArray(&m_num_entries, 1);
|
2010-11-15 05:22:03 +00:00
|
|
|
}
|
2010-02-19 17:05:26 +00:00
|
|
|
|
|
|
|
private:
|
2020-11-04 19:59:34 +00:00
|
|
|
void WriteHeader() { m_file.WriteArray(&m_header, 1); }
|
2010-11-15 05:22:03 +00:00
|
|
|
bool ValidateHeader()
|
|
|
|
{
|
|
|
|
char file_header[sizeof(Header)];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-04 19:59:34 +00:00
|
|
|
return (m_file.ReadArray(file_header, sizeof(Header)) &&
|
2014-03-10 11:30:55 +00:00
|
|
|
!memcmp((const char*)&m_header, file_header, sizeof(Header)));
|
2010-11-15 05:22:03 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-11-15 05:22:03 +00:00
|
|
|
struct Header
|
|
|
|
{
|
2016-05-04 21:47:23 +00:00
|
|
|
void Init()
|
2013-02-26 09:48:19 +00:00
|
|
|
{
|
2015-09-15 16:13:13 +00:00
|
|
|
// Null-terminator is intentionally not copied.
|
|
|
|
std::memcpy(&id, "DCAC", sizeof(u32));
|
2017-09-09 19:52:35 +00:00
|
|
|
std::memcpy(ver, Common::scm_rev_git_str.c_str(),
|
|
|
|
std::min(Common::scm_rev_git_str.size(), sizeof(ver)));
|
2013-02-26 09:48:19 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-09-15 16:13:13 +00:00
|
|
|
u32 id;
|
2016-05-04 21:47:23 +00:00
|
|
|
const u16 key_t_size = sizeof(K);
|
|
|
|
const u16 value_t_size = sizeof(V);
|
|
|
|
char ver[40] = {};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-11-15 05:22:03 +00:00
|
|
|
} m_header;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-04 19:59:34 +00:00
|
|
|
File::IOFile m_file;
|
2011-12-31 04:37:46 +00:00
|
|
|
u32 m_num_entries;
|
2010-02-19 17:05:26 +00:00
|
|
|
};
|