strat: use m_ for member variables
This commit is contained in:
@@ -21,10 +21,10 @@ namespace ams::kvdb {
|
||||
/* Functionality for parsing/generating a key value archive. */
|
||||
class ArchiveReader {
|
||||
private:
|
||||
AutoBuffer &buffer;
|
||||
size_t offset;
|
||||
AutoBuffer &m_buffer;
|
||||
size_t m_offset;
|
||||
public:
|
||||
ArchiveReader(AutoBuffer &b) : buffer(b), offset(0) { /* ... */ }
|
||||
ArchiveReader(AutoBuffer &b) : m_buffer(b), m_offset(0) { /* ... */ }
|
||||
private:
|
||||
Result Peek(void *dst, size_t size);
|
||||
Result Read(void *dst, size_t size);
|
||||
@@ -36,10 +36,10 @@ namespace ams::kvdb {
|
||||
|
||||
class ArchiveWriter {
|
||||
private:
|
||||
AutoBuffer &buffer;
|
||||
size_t offset;
|
||||
AutoBuffer &m_buffer;
|
||||
size_t m_offset;
|
||||
public:
|
||||
ArchiveWriter(AutoBuffer &b) : buffer(b), offset(0) { /* ... */ }
|
||||
ArchiveWriter(AutoBuffer &b) : m_buffer(b), m_offset(0) { /* ... */ }
|
||||
private:
|
||||
Result Write(const void *src, size_t size);
|
||||
public:
|
||||
@@ -49,14 +49,14 @@ namespace ams::kvdb {
|
||||
|
||||
class ArchiveSizeHelper {
|
||||
private:
|
||||
size_t size;
|
||||
size_t m_size;
|
||||
public:
|
||||
ArchiveSizeHelper();
|
||||
|
||||
void AddEntry(size_t key_size, size_t value_size);
|
||||
|
||||
size_t GetSize() const {
|
||||
return this->size;
|
||||
return m_size;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -21,20 +21,20 @@ namespace ams::kvdb {
|
||||
class AutoBuffer {
|
||||
NON_COPYABLE(AutoBuffer);
|
||||
private:
|
||||
u8 *buffer;
|
||||
size_t size;
|
||||
u8 *m_buffer;
|
||||
size_t m_size;
|
||||
public:
|
||||
AutoBuffer() : buffer(nullptr), size(0) { /* ... */ }
|
||||
AutoBuffer() : m_buffer(nullptr), m_size(0) { /* ... */ }
|
||||
|
||||
~AutoBuffer() {
|
||||
this->Reset();
|
||||
}
|
||||
|
||||
AutoBuffer(AutoBuffer &&rhs) {
|
||||
this->buffer = rhs.buffer;
|
||||
this->size = rhs.size;
|
||||
rhs.buffer = nullptr;
|
||||
rhs.size = 0;
|
||||
m_buffer = rhs.m_buffer;
|
||||
m_size = rhs.m_size;
|
||||
rhs.m_buffer = nullptr;
|
||||
rhs.m_size = 0;
|
||||
}
|
||||
|
||||
AutoBuffer &operator=(AutoBuffer &&rhs) {
|
||||
@@ -43,35 +43,35 @@ namespace ams::kvdb {
|
||||
}
|
||||
|
||||
void Swap(AutoBuffer &rhs) {
|
||||
std::swap(this->buffer, rhs.buffer);
|
||||
std::swap(this->size, rhs.size);
|
||||
std::swap(m_buffer, rhs.m_buffer);
|
||||
std::swap(m_size, rhs.m_size);
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
if (this->buffer != nullptr) {
|
||||
delete[] this->buffer;
|
||||
this->buffer = nullptr;
|
||||
this->size = 0;
|
||||
if (m_buffer != nullptr) {
|
||||
delete[] m_buffer;
|
||||
m_buffer = nullptr;
|
||||
m_size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
u8 *Get() const {
|
||||
return this->buffer;
|
||||
return m_buffer;
|
||||
}
|
||||
|
||||
size_t GetSize() const {
|
||||
return this->size;
|
||||
return m_size;
|
||||
}
|
||||
|
||||
Result Initialize(size_t size) {
|
||||
/* Check that we're not already initialized. */
|
||||
AMS_ABORT_UNLESS(this->buffer == nullptr);
|
||||
AMS_ABORT_UNLESS(m_buffer == nullptr);
|
||||
|
||||
/* Allocate a buffer. */
|
||||
this->buffer = new (std::nothrow) u8[size];
|
||||
R_UNLESS(this->buffer != nullptr, kvdb::ResultAllocationFailed());
|
||||
m_buffer = new (std::nothrow) u8[size];
|
||||
R_UNLESS(m_buffer != nullptr, kvdb::ResultAllocationFailed());
|
||||
|
||||
this->size = size;
|
||||
m_size = size;
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ namespace ams::kvdb {
|
||||
R_TRY(this->Initialize(size));
|
||||
|
||||
/* Copy the input data in. */
|
||||
std::memcpy(this->buffer, buf, size);
|
||||
std::memcpy(m_buffer, buf, size);
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace ams::kvdb {
|
||||
class BoundedString {
|
||||
static_assert(N > 0, "BoundedString requires non-zero backing buffer!");
|
||||
private:
|
||||
char buffer[N];
|
||||
char m_buffer[N];
|
||||
private:
|
||||
/* Utility. */
|
||||
static inline void CheckLength(size_t len) {
|
||||
@@ -32,7 +32,7 @@ namespace ams::kvdb {
|
||||
public:
|
||||
/* Constructors. */
|
||||
constexpr BoundedString() {
|
||||
buffer[0] = 0;
|
||||
m_buffer[0] = 0;
|
||||
}
|
||||
|
||||
explicit constexpr BoundedString(const char *s) {
|
||||
@@ -49,8 +49,8 @@ namespace ams::kvdb {
|
||||
|
||||
std::va_list args;
|
||||
va_start(args, format);
|
||||
CheckLength(util::VSNPrintf(string.buffer, N, format, args));
|
||||
string.buffer[N - 1] = 0;
|
||||
CheckLength(util::VSNPrintf(string.m_buffer, N, format, args));
|
||||
string.m_buffer[N - 1] = 0;
|
||||
va_end(args);
|
||||
|
||||
return string;
|
||||
@@ -58,30 +58,30 @@ namespace ams::kvdb {
|
||||
|
||||
/* Getters. */
|
||||
size_t GetLength() const {
|
||||
return util::Strnlen(this->buffer, N);
|
||||
return util::Strnlen(m_buffer, N);
|
||||
}
|
||||
|
||||
const char *Get() const {
|
||||
return this->buffer;
|
||||
return m_buffer;
|
||||
}
|
||||
|
||||
operator const char *() const {
|
||||
return this->buffer;
|
||||
return m_buffer;
|
||||
}
|
||||
|
||||
/* Setters. */
|
||||
void Set(const char *s) {
|
||||
/* Ensure string can fit in our buffer. */
|
||||
CheckLength(util::Strnlen(s, N));
|
||||
std::strncpy(this->buffer, s, N);
|
||||
this->buffer[N - 1] = 0;
|
||||
std::strncpy(m_buffer, s, N);
|
||||
m_buffer[N - 1] = 0;
|
||||
}
|
||||
|
||||
void SetFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) {
|
||||
/* Format into the buffer, abort if too large. */
|
||||
std::va_list args;
|
||||
va_start(args, format);
|
||||
CheckLength(util::VSNPrintf(this->buffer, N, format, args));
|
||||
CheckLength(util::VSNPrintf(m_buffer, N, format, args));
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
@@ -89,21 +89,21 @@ namespace ams::kvdb {
|
||||
void Append(const char *s) {
|
||||
const size_t length = GetLength();
|
||||
CheckLength(length + util::Strnlen(s, N));
|
||||
std::strncat(this->buffer, s, N - length - 1);
|
||||
std::strncat(m_buffer, s, N - length - 1);
|
||||
}
|
||||
|
||||
void Append(char c) {
|
||||
const size_t length = GetLength();
|
||||
CheckLength(length + 1);
|
||||
this->buffer[length] = c;
|
||||
this->buffer[length + 1] = 0;
|
||||
m_buffer[length] = c;
|
||||
m_buffer[length + 1] = 0;
|
||||
}
|
||||
|
||||
void AppendFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) {
|
||||
const size_t length = GetLength();
|
||||
std::va_list args;
|
||||
va_start(args, format);
|
||||
CheckLength(util::VSNPrintf(this->buffer + length, N - length, format, args) + length);
|
||||
CheckLength(util::VSNPrintf(m_buffer + length, N - length, format, args) + length);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
@@ -113,19 +113,19 @@ namespace ams::kvdb {
|
||||
AMS_ABORT_UNLESS(offset + length <= GetLength());
|
||||
AMS_ABORT_UNLESS(dst_size > length);
|
||||
/* Copy substring to dst. */
|
||||
std::strncpy(dst, this->buffer + offset, length);
|
||||
std::strncpy(dst, m_buffer + offset, length);
|
||||
dst[length] = 0;
|
||||
}
|
||||
|
||||
BoundedString<N> GetSubstring(size_t offset, size_t length) const {
|
||||
BoundedString<N> string;
|
||||
GetSubstring(string.buffer, N, offset, length);
|
||||
GetSubstring(string.m_buffer, N, offset, length);
|
||||
return string;
|
||||
}
|
||||
|
||||
/* Comparison. */
|
||||
constexpr bool operator==(const BoundedString<N> &rhs) const {
|
||||
return std::strncmp(this->buffer, rhs.buffer, N) == 0;
|
||||
return std::strncmp(m_buffer, rhs.m_buffer, N) == 0;
|
||||
}
|
||||
|
||||
constexpr bool operator!=(const BoundedString<N> &rhs) const {
|
||||
@@ -133,7 +133,7 @@ namespace ams::kvdb {
|
||||
}
|
||||
|
||||
bool EndsWith(const char *s, size_t offset) const {
|
||||
return std::strncmp(this->buffer + offset, s, N - offset) == 0;
|
||||
return std::strncmp(m_buffer + offset, s, N - offset) == 0;
|
||||
}
|
||||
|
||||
bool EndsWith(const char *s) const {
|
||||
|
||||
@@ -34,9 +34,9 @@ namespace ams::kvdb {
|
||||
static constexpr size_t FileSize = sizeof(LruHeader) + BufferSize;
|
||||
using Path = FileKeyValueStore::Path;
|
||||
private:
|
||||
Path file_path;
|
||||
Key *keys;
|
||||
LruHeader header;
|
||||
Path m_file_path;
|
||||
Key *m_keys;
|
||||
LruHeader m_header;
|
||||
public:
|
||||
static Result CreateNewList(const char *path) {
|
||||
/* Create new lru_list.dat. */
|
||||
@@ -56,40 +56,40 @@ namespace ams::kvdb {
|
||||
private:
|
||||
void RemoveIndex(size_t i) {
|
||||
AMS_ABORT_UNLESS(i < this->GetCount());
|
||||
std::memmove(this->keys + i, this->keys + i + 1, sizeof(*this->keys) * (this->GetCount() - (i + 1)));
|
||||
std::memmove(m_keys + i, m_keys + i + 1, sizeof(*m_keys) * (this->GetCount() - (i + 1)));
|
||||
this->DecrementCount();
|
||||
}
|
||||
|
||||
void IncrementCount() {
|
||||
this->header.entry_count++;
|
||||
m_header.entry_count++;
|
||||
}
|
||||
|
||||
void DecrementCount() {
|
||||
this->header.entry_count--;
|
||||
m_header.entry_count--;
|
||||
}
|
||||
public:
|
||||
LruList() : keys(nullptr), header({}) { /* ... */ }
|
||||
LruList() : m_keys(nullptr), m_header() { /* ... */ }
|
||||
|
||||
Result Initialize(const char *path, void *buf, size_t size) {
|
||||
/* Only initialize once, and ensure we have sufficient memory. */
|
||||
AMS_ABORT_UNLESS(this->keys == nullptr);
|
||||
AMS_ABORT_UNLESS(m_keys == nullptr);
|
||||
AMS_ABORT_UNLESS(size >= BufferSize);
|
||||
|
||||
/* Setup member variables. */
|
||||
this->keys = static_cast<Key *>(buf);
|
||||
this->file_path.Set(path);
|
||||
std::memset(this->keys, 0, BufferSize);
|
||||
m_keys = static_cast<Key *>(buf);
|
||||
m_file_path.Set(path);
|
||||
std::memset(m_keys, 0, BufferSize);
|
||||
|
||||
/* Open file. */
|
||||
fs::FileHandle file;
|
||||
R_TRY(fs::OpenFile(std::addressof(file), this->file_path, fs::OpenMode_Read));
|
||||
R_TRY(fs::OpenFile(std::addressof(file), m_file_path, fs::OpenMode_Read));
|
||||
ON_SCOPE_EXIT { fs::CloseFile(file); };
|
||||
|
||||
/* Read header. */
|
||||
R_TRY(fs::ReadFile(file, 0, std::addressof(this->header), sizeof(this->header)));
|
||||
R_TRY(fs::ReadFile(file, 0, std::addressof(m_header), sizeof(m_header)));
|
||||
|
||||
/* Read entries. */
|
||||
R_TRY(fs::ReadFile(file, sizeof(this->header), this->keys, BufferSize));
|
||||
R_TRY(fs::ReadFile(file, sizeof(m_header), m_keys, BufferSize));
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
@@ -97,14 +97,14 @@ namespace ams::kvdb {
|
||||
Result Save() {
|
||||
/* Open file. */
|
||||
fs::FileHandle file;
|
||||
R_TRY(fs::OpenFile(std::addressof(file), this->file_path, fs::OpenMode_Read));
|
||||
R_TRY(fs::OpenFile(std::addressof(file), m_file_path, fs::OpenMode_Read));
|
||||
ON_SCOPE_EXIT { fs::CloseFile(file); };
|
||||
|
||||
/* Write header. */
|
||||
R_TRY(fs::WriteFile(file, 0, std::addressof(this->header), sizeof(this->header), fs::WriteOption::None));
|
||||
R_TRY(fs::WriteFile(file, 0, std::addressof(m_header), sizeof(m_header), fs::WriteOption::None));
|
||||
|
||||
/* Write entries. */
|
||||
R_TRY(fs::WriteFile(file, sizeof(this->header), this->keys, BufferSize, fs::WriteOption::None));
|
||||
R_TRY(fs::WriteFile(file, sizeof(m_header), m_keys, BufferSize, fs::WriteOption::None));
|
||||
|
||||
/* Flush. */
|
||||
R_TRY(fs::FlushFile(file));
|
||||
@@ -113,7 +113,7 @@ namespace ams::kvdb {
|
||||
}
|
||||
|
||||
size_t GetCount() const {
|
||||
return this->header.entry_count;
|
||||
return m_header.entry_count;
|
||||
}
|
||||
|
||||
bool IsEmpty() const {
|
||||
@@ -126,7 +126,7 @@ namespace ams::kvdb {
|
||||
|
||||
Key Get(size_t i) const {
|
||||
AMS_ABORT_UNLESS(i < this->GetCount());
|
||||
return this->keys[i];
|
||||
return m_keys[i];
|
||||
}
|
||||
|
||||
Key Peek() const {
|
||||
@@ -136,7 +136,7 @@ namespace ams::kvdb {
|
||||
|
||||
void Push(const Key &key) {
|
||||
AMS_ABORT_UNLESS(!this->IsFull());
|
||||
this->keys[this->GetCount()] = key;
|
||||
m_keys[this->GetCount()] = key;
|
||||
this->IncrementCount();
|
||||
}
|
||||
|
||||
@@ -150,7 +150,7 @@ namespace ams::kvdb {
|
||||
|
||||
/* Iterate over the list, removing the last entry that matches the key. */
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
if (this->keys[count - 1 - i] == key) {
|
||||
if (m_keys[count - 1 - i] == key) {
|
||||
this->RemoveIndex(count - 1 - i);
|
||||
return true;
|
||||
}
|
||||
@@ -164,7 +164,7 @@ namespace ams::kvdb {
|
||||
|
||||
/* Iterate over the list, checking to see if we have the key. */
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
if (this->keys[count - 1 - i] == key) {
|
||||
if (m_keys[count - 1 - i] == key) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -196,8 +196,8 @@ namespace ams::kvdb {
|
||||
/* as FileKeyValueStore paths are limited to 0x100 anyway. */
|
||||
using Path = typename LeastRecentlyUsedList::Path;
|
||||
private:
|
||||
FileKeyValueStore kvs;
|
||||
LeastRecentlyUsedList lru_list;
|
||||
FileKeyValueStore m_kvs;
|
||||
LeastRecentlyUsedList m_lru_list;
|
||||
private:
|
||||
static constexpr Path GetLeastRecentlyUsedListPath(const char *dir) {
|
||||
return Path::MakeFormat("%s/%s", dir, "lru_list.dat");
|
||||
@@ -258,14 +258,14 @@ namespace ams::kvdb {
|
||||
}
|
||||
private:
|
||||
void RemoveOldestKey() {
|
||||
const Key &oldest_key = this->lru_list.Peek();
|
||||
this->lru_list.Pop();
|
||||
this->kvs.Remove(oldest_key);
|
||||
const Key &oldest_key = m_lru_list.Peek();
|
||||
m_lru_list.Pop();
|
||||
m_kvs.Remove(oldest_key);
|
||||
}
|
||||
public:
|
||||
Result Initialize(const char *dir, void *buf, size_t size) {
|
||||
/* Initialize list. */
|
||||
R_TRY(this->lru_list.Initialize(GetLeastRecentlyUsedListPath(dir), buf, size));
|
||||
R_TRY(m_lru_list.Initialize(GetLeastRecentlyUsedListPath(dir), buf, size));
|
||||
|
||||
/* Initialize kvs. */
|
||||
/* NOTE: Despite creating the kvs folder and returning an error if it does not exist, */
|
||||
@@ -274,13 +274,13 @@ namespace ams::kvdb {
|
||||
/* instead of in the same directory as a folder containing the store's .val files. */
|
||||
/* This is probably a Nintendo bug, but because system saves contain data in the wrong */
|
||||
/* layout it can't really be fixed without breaking existing devices... */
|
||||
R_TRY(this->kvs.Initialize(dir));
|
||||
R_TRY(m_kvs.Initialize(dir));
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
size_t GetCount() const {
|
||||
return this->lru_list.GetCount();
|
||||
return m_lru_list.GetCount();
|
||||
}
|
||||
|
||||
size_t GetCapacity() const {
|
||||
@@ -288,53 +288,53 @@ namespace ams::kvdb {
|
||||
}
|
||||
|
||||
Key GetKey(size_t i) const {
|
||||
return this->lru_list.Get(i);
|
||||
return m_lru_list.Get(i);
|
||||
}
|
||||
|
||||
bool Contains(const Key &key) const {
|
||||
return this->lru_list.Contains(key);
|
||||
return m_lru_list.Contains(key);
|
||||
}
|
||||
|
||||
Result Get(size_t *out_size, void *out_value, size_t max_out_size, const Key &key) {
|
||||
/* Note that we accessed the key. */
|
||||
this->lru_list.Update(key);
|
||||
return this->kvs.Get(out_size, out_value, max_out_size, key);
|
||||
m_lru_list.Update(key);
|
||||
return m_kvs.Get(out_size, out_value, max_out_size, key);
|
||||
}
|
||||
|
||||
template<typename Value>
|
||||
Result Get(Value *out_value, const Key &key) {
|
||||
/* Note that we accessed the key. */
|
||||
this->lru_list.Update(key);
|
||||
return this->kvs.Get(out_value, key);
|
||||
m_lru_list.Update(key);
|
||||
return m_kvs.Get(out_value, key);
|
||||
}
|
||||
|
||||
Result GetSize(size_t *out_size, const Key &key) {
|
||||
return this->kvs.GetSize(out_size, key);
|
||||
return m_kvs.GetSize(out_size, key);
|
||||
}
|
||||
|
||||
Result Set(const Key &key, const void *value, size_t value_size) {
|
||||
if (this->lru_list.Update(key)) {
|
||||
if (m_lru_list.Update(key)) {
|
||||
/* If an entry for the key exists, delete the existing value file. */
|
||||
this->kvs.Remove(key);
|
||||
m_kvs.Remove(key);
|
||||
} else {
|
||||
/* If the list is full, we need to remove the oldest key. */
|
||||
if (this->lru_list.IsFull()) {
|
||||
if (m_lru_list.IsFull()) {
|
||||
this->RemoveOldestKey();
|
||||
}
|
||||
|
||||
/* Add the key to the list. */
|
||||
this->lru_list.Push(key);
|
||||
m_lru_list.Push(key);
|
||||
}
|
||||
|
||||
/* Loop, trying to save the new value to disk. */
|
||||
while (true) {
|
||||
/* Try to set the key. */
|
||||
R_TRY_CATCH(this->kvs.Set(key, value, value_size)) {
|
||||
R_TRY_CATCH(m_kvs.Set(key, value, value_size)) {
|
||||
R_CATCH(fs::ResultNotEnoughFreeSpace) {
|
||||
/* If our entry is the only thing in the Lru list, remove it. */
|
||||
if (this->lru_list.GetCount() == 1) {
|
||||
this->lru_list.Pop();
|
||||
R_TRY(this->lru_list.Save());
|
||||
if (m_lru_list.GetCount() == 1) {
|
||||
m_lru_list.Pop();
|
||||
R_TRY(m_lru_list.Save());
|
||||
return fs::ResultNotEnoughFreeSpace();
|
||||
}
|
||||
|
||||
@@ -349,7 +349,7 @@ namespace ams::kvdb {
|
||||
}
|
||||
|
||||
/* Save the list. */
|
||||
R_TRY(this->lru_list.Save());
|
||||
R_TRY(m_lru_list.Save());
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
@@ -361,19 +361,19 @@ namespace ams::kvdb {
|
||||
|
||||
Result Remove(const Key &key) {
|
||||
/* Remove the key. */
|
||||
this->lru_list.Remove(key);
|
||||
R_TRY(this->kvs.Remove(key));
|
||||
R_TRY(this->lru_list.Save());
|
||||
m_lru_list.Remove(key);
|
||||
R_TRY(m_kvs.Remove(key));
|
||||
R_TRY(m_lru_list.Save());
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result RemoveAll() {
|
||||
/* TODO: Nintendo doesn't check errors here. Should we? */
|
||||
while (!this->lru_list.IsEmpty()) {
|
||||
while (!m_lru_list.IsEmpty()) {
|
||||
this->RemoveOldestKey();
|
||||
}
|
||||
R_TRY(this->lru_list.Save());
|
||||
R_TRY(m_lru_list.Save());
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
@@ -42,17 +42,17 @@ namespace ams::kvdb {
|
||||
|
||||
class Cache {
|
||||
private:
|
||||
u8 *backing_buffer = nullptr;
|
||||
size_t backing_buffer_size = 0;
|
||||
size_t backing_buffer_free_offset = 0;
|
||||
Entry *entries = nullptr;
|
||||
size_t count = 0;
|
||||
size_t capacity = 0;
|
||||
u8 *m_backing_buffer = nullptr;
|
||||
size_t m_backing_buffer_size = 0;
|
||||
size_t m_backing_buffer_free_offset = 0;
|
||||
Entry *m_entries = nullptr;
|
||||
size_t m_count = 0;
|
||||
size_t m_capacity = 0;
|
||||
private:
|
||||
void *Allocate(size_t size);
|
||||
|
||||
bool HasEntries() const {
|
||||
return this->entries != nullptr && this->capacity != 0;
|
||||
return m_entries != nullptr && m_capacity != 0;
|
||||
}
|
||||
public:
|
||||
Result Initialize(void *buffer, size_t buffer_size, size_t capacity);
|
||||
@@ -63,14 +63,14 @@ namespace ams::kvdb {
|
||||
bool Contains(const void *key, size_t key_size);
|
||||
};
|
||||
private:
|
||||
os::SdkMutex lock;
|
||||
Path dir_path;
|
||||
Cache cache;
|
||||
os::SdkMutex m_lock;
|
||||
Path m_dir_path;
|
||||
Cache m_cache;
|
||||
private:
|
||||
Path GetPath(const void *key, size_t key_size);
|
||||
Result GetKey(size_t *out_size, void *out_key, size_t max_out_size, const FileName &file_name);
|
||||
public:
|
||||
FileKeyValueStore() : lock() { /* ... */ }
|
||||
FileKeyValueStore() : m_lock() { /* ... */ }
|
||||
|
||||
/* Basic accessors. */
|
||||
Result Initialize(const char *dir);
|
||||
|
||||
@@ -33,36 +33,36 @@ namespace ams::kvdb {
|
||||
/* Subtypes. */
|
||||
class Entry {
|
||||
private:
|
||||
Key key;
|
||||
void *value;
|
||||
size_t value_size;
|
||||
Key m_key;
|
||||
void *m_value;
|
||||
size_t m_value_size;
|
||||
public:
|
||||
constexpr Entry(const Key &k, void *v, size_t s) : key(k), value(v), value_size(s) { /* ... */ }
|
||||
constexpr Entry(const Key &k, void *v, size_t s) : m_key(k), m_value(v), m_value_size(s) { /* ... */ }
|
||||
|
||||
const Key &GetKey() const {
|
||||
return this->key;
|
||||
return m_key;
|
||||
}
|
||||
|
||||
template<typename Value = void>
|
||||
Value *GetValuePointer() {
|
||||
/* Size check. Note: Nintendo does not size check. */
|
||||
if constexpr (!std::is_same<Value, void>::value) {
|
||||
AMS_ABORT_UNLESS(sizeof(Value) <= this->value_size);
|
||||
AMS_ABORT_UNLESS(sizeof(Value) <= m_value_size);
|
||||
/* Ensure we only get pod. */
|
||||
static_assert(util::is_pod<Value>::value, "KeyValueStore Values must be pod");
|
||||
}
|
||||
return reinterpret_cast<Value *>(this->value);
|
||||
return reinterpret_cast<Value *>(m_value);
|
||||
}
|
||||
|
||||
template<typename Value = void>
|
||||
const Value *GetValuePointer() const {
|
||||
/* Size check. Note: Nintendo does not size check. */
|
||||
if constexpr (!std::is_same<Value, void>::value) {
|
||||
AMS_ABORT_UNLESS(sizeof(Value) <= this->value_size);
|
||||
AMS_ABORT_UNLESS(sizeof(Value) <= m_value_size);
|
||||
/* Ensure we only get pod. */
|
||||
static_assert(util::is_pod<Value>::value, "KeyValueStore Values must be pod");
|
||||
}
|
||||
return reinterpret_cast<Value *>(this->value);
|
||||
return reinterpret_cast<Value *>(m_value);
|
||||
}
|
||||
|
||||
template<typename Value>
|
||||
@@ -76,55 +76,55 @@ namespace ams::kvdb {
|
||||
}
|
||||
|
||||
size_t GetValueSize() const {
|
||||
return this->value_size;
|
||||
return m_value_size;
|
||||
}
|
||||
|
||||
constexpr inline bool operator<(const Key &rhs) const {
|
||||
return key < rhs;
|
||||
return m_key < rhs;
|
||||
}
|
||||
|
||||
constexpr inline bool operator==(const Key &rhs) const {
|
||||
return key == rhs;
|
||||
return m_key == rhs;
|
||||
}
|
||||
};
|
||||
|
||||
class Index {
|
||||
private:
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
Entry *entries;
|
||||
MemoryResource *memory_resource;
|
||||
size_t m_count;
|
||||
size_t m_capacity;
|
||||
Entry *m_entries;
|
||||
MemoryResource *m_memory_resource;
|
||||
public:
|
||||
Index() : count(0), capacity(0), entries(nullptr), memory_resource(nullptr) { /* ... */ }
|
||||
Index() : m_count(0), m_capacity(0), m_entries(nullptr), m_memory_resource(nullptr) { /* ... */ }
|
||||
|
||||
~Index() {
|
||||
if (this->entries != nullptr) {
|
||||
if (m_entries != nullptr) {
|
||||
this->ResetEntries();
|
||||
this->memory_resource->Deallocate(this->entries, sizeof(Entry) * this->capacity);
|
||||
this->entries = nullptr;
|
||||
m_memory_resource->Deallocate(m_entries, sizeof(Entry) * m_capacity);
|
||||
m_entries = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
size_t GetCount() const {
|
||||
return this->count;
|
||||
return m_count;
|
||||
}
|
||||
|
||||
size_t GetCapacity() const {
|
||||
return this->capacity;
|
||||
return m_capacity;
|
||||
}
|
||||
|
||||
void ResetEntries() {
|
||||
for (size_t i = 0; i < this->count; i++) {
|
||||
this->memory_resource->Deallocate(this->entries[i].GetValuePointer(), this->entries[i].GetValueSize());
|
||||
for (size_t i = 0; i < m_count; i++) {
|
||||
m_memory_resource->Deallocate(m_entries[i].GetValuePointer(), m_entries[i].GetValueSize());
|
||||
}
|
||||
this->count = 0;
|
||||
m_count = 0;
|
||||
}
|
||||
|
||||
Result Initialize(size_t capacity, MemoryResource *mr) {
|
||||
this->entries = reinterpret_cast<Entry *>(mr->Allocate(sizeof(Entry) * capacity));
|
||||
R_UNLESS(this->entries != nullptr, kvdb::ResultAllocationFailed());
|
||||
this->capacity = capacity;
|
||||
this->memory_resource = mr;
|
||||
m_entries = reinterpret_cast<Entry *>(mr->Allocate(sizeof(Entry) * capacity));
|
||||
R_UNLESS(m_entries != nullptr, kvdb::ResultAllocationFailed());
|
||||
m_capacity = capacity;
|
||||
m_memory_resource = mr;
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
@@ -133,16 +133,16 @@ namespace ams::kvdb {
|
||||
Entry *it = this->lower_bound(key);
|
||||
if (it != this->end() && it->GetKey() == key) {
|
||||
/* Entry already exists. Free old value. */
|
||||
this->memory_resource->Deallocate(it->GetValuePointer(), it->GetValueSize());
|
||||
m_memory_resource->Deallocate(it->GetValuePointer(), it->GetValueSize());
|
||||
} else {
|
||||
/* We need to add a new entry. Check we have room, move future keys forward. */
|
||||
R_UNLESS(this->count < this->capacity, kvdb::ResultOutOfKeyResource());
|
||||
R_UNLESS(m_count < m_capacity, kvdb::ResultOutOfKeyResource());
|
||||
std::memmove(it + 1, it, sizeof(*it) * (this->end() - it));
|
||||
this->count++;
|
||||
m_count++;
|
||||
}
|
||||
|
||||
/* Allocate new value. */
|
||||
void *new_value = this->memory_resource->Allocate(value_size);
|
||||
void *new_value = m_memory_resource->Allocate(value_size);
|
||||
R_UNLESS(new_value != nullptr, kvdb::ResultAllocationFailed());
|
||||
std::memcpy(new_value, value, value_size);
|
||||
|
||||
@@ -152,9 +152,9 @@ namespace ams::kvdb {
|
||||
}
|
||||
|
||||
Result AddUnsafe(const Key &key, void *value, size_t value_size) {
|
||||
R_UNLESS(this->count < this->capacity, kvdb::ResultOutOfKeyResource());
|
||||
R_UNLESS(m_count < m_capacity, kvdb::ResultOutOfKeyResource());
|
||||
|
||||
this->entries[this->count++] = Entry(key, value, value_size);
|
||||
m_entries[m_count++] = Entry(key, value, value_size);
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
@@ -164,9 +164,9 @@ namespace ams::kvdb {
|
||||
R_UNLESS(it != this->end(), kvdb::ResultKeyNotFound());
|
||||
|
||||
/* Free the value, move entries back. */
|
||||
this->memory_resource->Deallocate(it->GetValuePointer(), it->GetValueSize());
|
||||
m_memory_resource->Deallocate(it->GetValuePointer(), it->GetValueSize());
|
||||
std::memmove(it, it + 1, sizeof(*it) * (this->end() - (it + 1)));
|
||||
this->count--;
|
||||
m_count--;
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
@@ -211,19 +211,19 @@ namespace ams::kvdb {
|
||||
}
|
||||
private:
|
||||
Entry *GetBegin() {
|
||||
return this->entries;
|
||||
return m_entries;
|
||||
}
|
||||
|
||||
const Entry *GetBegin() const {
|
||||
return this->entries;
|
||||
return m_entries;
|
||||
}
|
||||
|
||||
Entry *GetEnd() {
|
||||
return this->GetBegin() + this->count;
|
||||
return this->GetBegin() + m_count;
|
||||
}
|
||||
|
||||
const Entry *GetEnd() const {
|
||||
return this->GetBegin() + this->count;
|
||||
return this->GetBegin() + m_count;
|
||||
}
|
||||
|
||||
Entry *GetLowerBound(const Key &key) {
|
||||
@@ -255,10 +255,10 @@ namespace ams::kvdb {
|
||||
private:
|
||||
using Path = kvdb::BoundedString<fs::EntryNameLengthMax>;
|
||||
private:
|
||||
Index index;
|
||||
Path path;
|
||||
Path temp_path;
|
||||
MemoryResource *memory_resource;
|
||||
Index m_index;
|
||||
Path m_path;
|
||||
Path m_temp_path;
|
||||
MemoryResource *m_memory_resource;
|
||||
public:
|
||||
MemoryKeyValueStore() { /* ... */ }
|
||||
|
||||
@@ -269,12 +269,12 @@ namespace ams::kvdb {
|
||||
R_UNLESS(entry_type == fs::DirectoryEntryType_Directory, fs::ResultPathNotFound());
|
||||
|
||||
/* Set paths. */
|
||||
this->path.SetFormat("%s%s", dir, "/imkvdb.arc");
|
||||
this->temp_path.SetFormat("%s%s", dir, "/imkvdb.tmp");
|
||||
m_path.SetFormat("%s%s", dir, "/imkvdb.arc");
|
||||
m_temp_path.SetFormat("%s%s", dir, "/imkvdb.tmp");
|
||||
|
||||
/* Initialize our index. */
|
||||
R_TRY(this->index.Initialize(capacity, mr));
|
||||
this->memory_resource = mr;
|
||||
R_TRY(m_index.Initialize(capacity, mr));
|
||||
m_memory_resource = mr;
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
@@ -282,26 +282,26 @@ namespace ams::kvdb {
|
||||
Result Initialize(size_t capacity, MemoryResource *mr) {
|
||||
/* This initializes without an archive file. */
|
||||
/* A store initialized this way cannot have its contents loaded from or flushed to disk. */
|
||||
this->path.Set("");
|
||||
this->temp_path.Set("");
|
||||
m_path.Set("");
|
||||
m_temp_path.Set("");
|
||||
|
||||
/* Initialize our index. */
|
||||
R_TRY(this->index.Initialize(capacity, mr));
|
||||
this->memory_resource = mr;
|
||||
R_TRY(m_index.Initialize(capacity, mr));
|
||||
m_memory_resource = mr;
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
size_t GetCount() const {
|
||||
return this->index.GetCount();
|
||||
return m_index.GetCount();
|
||||
}
|
||||
|
||||
size_t GetCapacity() const {
|
||||
return this->index.GetCapacity();
|
||||
return m_index.GetCapacity();
|
||||
}
|
||||
|
||||
Result Load() {
|
||||
/* Reset any existing entries. */
|
||||
this->index.ResetEntries();
|
||||
m_index.ResetEntries();
|
||||
|
||||
/* Try to read the archive -- note, path not found is a success condition. */
|
||||
/* This is because no archive file = no entries, so we're in the right state. */
|
||||
@@ -323,14 +323,14 @@ namespace ams::kvdb {
|
||||
R_TRY(reader.GetEntrySize(std::addressof(key_size), std::addressof(value_size)));
|
||||
|
||||
/* Allocate memory for value. */
|
||||
void *new_value = this->memory_resource->Allocate(value_size);
|
||||
void *new_value = m_memory_resource->Allocate(value_size);
|
||||
R_UNLESS(new_value != nullptr, kvdb::ResultAllocationFailed());
|
||||
auto value_guard = SCOPE_GUARD { this->memory_resource->Deallocate(new_value, value_size); };
|
||||
auto value_guard = SCOPE_GUARD { m_memory_resource->Deallocate(new_value, value_size); };
|
||||
|
||||
/* Read key and value. */
|
||||
Key key;
|
||||
R_TRY(reader.ReadEntry(std::addressof(key), sizeof(key), new_value, value_size));
|
||||
R_TRY(this->index.AddUnsafe(key, new_value, value_size));
|
||||
R_TRY(m_index.AddUnsafe(key, new_value, value_size));
|
||||
|
||||
/* We succeeded, so cancel the value guard to prevent deallocation. */
|
||||
value_guard.Cancel();
|
||||
@@ -349,7 +349,7 @@ namespace ams::kvdb {
|
||||
{
|
||||
ArchiveWriter writer(buffer);
|
||||
writer.WriteHeader(this->GetCount());
|
||||
for (const auto &it : this->index) {
|
||||
for (const auto &it : m_index) {
|
||||
const auto &key = it.GetKey();
|
||||
writer.WriteEntry(std::addressof(key), sizeof(Key), it.GetValuePointer(), it.GetValueSize());
|
||||
}
|
||||
@@ -360,7 +360,7 @@ namespace ams::kvdb {
|
||||
}
|
||||
|
||||
Result Set(const Key &key, const void *value, size_t value_size) {
|
||||
return this->index.Set(key, value, value_size);
|
||||
return m_index.Set(key, value, value_size);
|
||||
}
|
||||
|
||||
template<typename Value>
|
||||
@@ -428,47 +428,47 @@ namespace ams::kvdb {
|
||||
}
|
||||
|
||||
Result Remove(const Key &key) {
|
||||
return this->index.Remove(key);
|
||||
return m_index.Remove(key);
|
||||
}
|
||||
|
||||
Entry *begin() {
|
||||
return this->index.begin();
|
||||
return m_index.begin();
|
||||
}
|
||||
|
||||
const Entry *begin() const {
|
||||
return this->index.begin();
|
||||
return m_index.begin();
|
||||
}
|
||||
|
||||
Entry *end() {
|
||||
return this->index.end();
|
||||
return m_index.end();
|
||||
}
|
||||
|
||||
const Entry *end() const {
|
||||
return this->index.end();
|
||||
return m_index.end();
|
||||
}
|
||||
|
||||
const Entry *cbegin() const {
|
||||
return this->index.cbegin();
|
||||
return m_index.cbegin();
|
||||
}
|
||||
|
||||
const Entry *cend() const {
|
||||
return this->index.cend();
|
||||
return m_index.cend();
|
||||
}
|
||||
|
||||
Entry *lower_bound(const Key &key) {
|
||||
return this->index.lower_bound(key);
|
||||
return m_index.lower_bound(key);
|
||||
}
|
||||
|
||||
const Entry *lower_bound(const Key &key) const {
|
||||
return this->index.lower_bound(key);
|
||||
return m_index.lower_bound(key);
|
||||
}
|
||||
|
||||
Entry *find(const Key &key) {
|
||||
return this->index.find(key);
|
||||
return m_index.find(key);
|
||||
}
|
||||
|
||||
const Entry *find(const Key &key) const {
|
||||
return this->index.find(key);
|
||||
return m_index.find(key);
|
||||
}
|
||||
private:
|
||||
Result SaveArchiveToFile(const char *path, const void *buf, size_t size) {
|
||||
@@ -492,16 +492,16 @@ namespace ams::kvdb {
|
||||
Result Commit(const AutoBuffer &buffer, bool destructive) {
|
||||
if (destructive) {
|
||||
/* Delete and save to the real archive. */
|
||||
R_TRY(SaveArchiveToFile(this->path.Get(), buffer.Get(), buffer.GetSize()));
|
||||
R_TRY(SaveArchiveToFile(m_path.Get(), buffer.Get(), buffer.GetSize()));
|
||||
} else {
|
||||
/* Delete and save to a temporary archive. */
|
||||
R_TRY(SaveArchiveToFile(this->temp_path.Get(), buffer.Get(), buffer.GetSize()));
|
||||
R_TRY(SaveArchiveToFile(m_temp_path.Get(), buffer.Get(), buffer.GetSize()));
|
||||
|
||||
/* Try to delete the saved archive, but allow deletion failure. */
|
||||
fs::DeleteFile(this->path.Get());
|
||||
fs::DeleteFile(m_path.Get());
|
||||
|
||||
/* Rename the path. */
|
||||
R_TRY(fs::RenameFile(this->temp_path.Get(), this->path.Get()));
|
||||
R_TRY(fs::RenameFile(m_temp_path.Get(), m_path.Get()));
|
||||
}
|
||||
|
||||
return ResultSuccess();
|
||||
@@ -510,7 +510,7 @@ namespace ams::kvdb {
|
||||
size_t GetArchiveSize() const {
|
||||
ArchiveSizeHelper size_helper;
|
||||
|
||||
for (const auto &it : this->index) {
|
||||
for (const auto &it : m_index) {
|
||||
size_helper.AddEntry(sizeof(Key), it.GetValueSize());
|
||||
}
|
||||
|
||||
@@ -520,7 +520,7 @@ namespace ams::kvdb {
|
||||
Result ReadArchiveFile(AutoBuffer *dst) const {
|
||||
/* Open the file. */
|
||||
fs::FileHandle file;
|
||||
R_TRY(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Read));
|
||||
R_TRY(fs::OpenFile(std::addressof(file), m_path, fs::OpenMode_Read));
|
||||
ON_SCOPE_EXIT { fs::CloseFile(file); };
|
||||
|
||||
/* Get the archive file size. */
|
||||
|
||||
Reference in New Issue
Block a user