1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
23
24
33
34
38
39
44
45
46
47
48
49
56
57
58
59
60
61
62
63
64
65
66
67
68
105
106
121
122
123
#include "nvs_item_hash_list.hpp"
namespace nvs
{
HashList::HashList()
{
}{ ... }
void HashList::clear()
{
for (auto it = mBlockList.begin(); it != mBlockList.end();) {
auto tmp = it;
++it;
mBlockList.erase(tmp);
delete static_cast<HashListBlock*>(tmp);
}{...}
}{ ... }
HashList::~HashList()
{
clear();
}{ ... }
HashList::HashListBlock::HashListBlock()
{
static_assert(sizeof(HashListBlock) == HashListBlock::BYTE_SIZE,
"cache block size calculation incorrect");
}{ ... }
esp_err_t HashList::insert(const Item& item, size_t index)
{
const uint32_t hash_24 = item.calculateCrc32WithoutValue() & 0xffffff;
if (mBlockList.size()) {
auto& block = mBlockList.back();
if (block.mCount < HashListBlock::ENTRY_COUNT) {
block.mNodes[block.mCount++] = HashListNode(hash_24, index);
return ESP_OK;
}{...}
}{...}
HashListBlock* newBlock = new (std::nothrow) HashListBlock;
if (!newBlock) return ESP_ERR_NO_MEM;
mBlockList.push_back(newBlock);
newBlock->mNodes[0] = HashListNode(hash_24, index);
newBlock->mCount++;
return ESP_OK;
}{ ... }
bool HashList::erase(size_t index)
{
for (auto it = mBlockList.begin(); it != mBlockList.end();) {
bool haveEntries = false;
bool foundIndex = false;
for (size_t i = 0; i < it->mCount; ++i) {
if (it->mNodes[i].mIndex == index) {
it->mNodes[i].mIndex = 0xff;
foundIndex = true;
}{...}
if (it->mNodes[i].mIndex != 0xff) {
haveEntries = true;
}{...}
if (haveEntries && foundIndex) {
return true;
}{...}
}{...}
if (!haveEntries) {
auto tmp = it;
++it;
mBlockList.erase(tmp);
delete static_cast<HashListBlock*>(tmp);
}{...} else {
++it;
}{...}
if (foundIndex) {
return true;
}{...}
}{...}
return false;
}{ ... }
size_t HashList::find(size_t start, const Item& item)
{
const uint32_t hash_24 = item.calculateCrc32WithoutValue() & 0xffffff;
for (auto it = mBlockList.begin(); it != mBlockList.end(); ++it) {
for (size_t index = 0; index < it->mCount; ++index) {
HashListNode& e = it->mNodes[index];
if (e.mIndex >= start &&
e.mHash == hash_24 &&
e.mIndex != 0xff) {
return e.mIndex;
}{...}
}{...}
}{...}
return SIZE_MAX;
}{ ... }
}{...}