components/nvs: fix formatting
This commit is contained in:
parent
076141aab9
commit
e87d80d478
10 changed files with 89 additions and 102 deletions
|
@ -20,12 +20,12 @@
|
||||||
class HandleEntry : public intrusive_list_node<HandleEntry>
|
class HandleEntry : public intrusive_list_node<HandleEntry>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
HandleEntry(){}
|
HandleEntry() {}
|
||||||
|
|
||||||
HandleEntry(nvs_handle handle, bool readOnly, uint8_t nsIndex) :
|
HandleEntry(nvs_handle handle, bool readOnly, uint8_t nsIndex) :
|
||||||
mHandle(handle),
|
mHandle(handle),
|
||||||
mReadOnly(readOnly),
|
mReadOnly(readOnly),
|
||||||
mNsIndex(nsIndex)
|
mNsIndex(nsIndex)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -263,12 +263,10 @@ static esp_err_t nvs_get_str_or_blob(nvs_handle handle, nvs::ItemType type, cons
|
||||||
|
|
||||||
if (length == nullptr) {
|
if (length == nullptr) {
|
||||||
return ESP_ERR_NVS_INVALID_LENGTH;
|
return ESP_ERR_NVS_INVALID_LENGTH;
|
||||||
}
|
} else if (out_value == nullptr) {
|
||||||
else if (out_value == nullptr) {
|
|
||||||
*length = dataSize;
|
*length = dataSize;
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
} else if (*length < dataSize) {
|
||||||
else if (*length < dataSize) {
|
|
||||||
*length = dataSize;
|
*length = dataSize;
|
||||||
return ESP_ERR_NVS_INVALID_LENGTH;
|
return ESP_ERR_NVS_INVALID_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,8 +53,7 @@ void HashList::insert(const Item& item, size_t index)
|
||||||
|
|
||||||
void HashList::erase(size_t index)
|
void HashList::erase(size_t index)
|
||||||
{
|
{
|
||||||
for (auto it = std::begin(mBlockList); it != std::end(mBlockList);)
|
for (auto it = std::begin(mBlockList); it != std::end(mBlockList);) {
|
||||||
{
|
|
||||||
bool haveEntries = false;
|
bool haveEntries = false;
|
||||||
for (size_t i = 0; i < it->mCount; ++i) {
|
for (size_t i = 0; i < it->mCount; ++i) {
|
||||||
if (it->mNodes[i].mIndex == index) {
|
if (it->mNodes[i].mIndex == index) {
|
||||||
|
@ -70,8 +69,7 @@ void HashList::erase(size_t index)
|
||||||
++it;
|
++it;
|
||||||
mBlockList.erase(tmp);
|
mBlockList.erase(tmp);
|
||||||
delete static_cast<HashListBlock*>(tmp);
|
delete static_cast<HashListBlock*>(tmp);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,13 +79,12 @@ void HashList::erase(size_t index)
|
||||||
size_t HashList::find(size_t start, const Item& item)
|
size_t HashList::find(size_t start, const Item& item)
|
||||||
{
|
{
|
||||||
const uint32_t hash_24 = item.calculateCrc32WithoutValue() & 0xffffff;
|
const uint32_t hash_24 = item.calculateCrc32WithoutValue() & 0xffffff;
|
||||||
for (auto it = std::begin(mBlockList); it != std::end(mBlockList); ++it)
|
for (auto it = std::begin(mBlockList); it != std::end(mBlockList); ++it) {
|
||||||
{
|
|
||||||
for (size_t index = 0; index < it->mCount; ++index) {
|
for (size_t index = 0; index < it->mCount; ++index) {
|
||||||
HashListNode& e = it->mNodes[index];
|
HashListNode& e = it->mNodes[index];
|
||||||
if (e.mIndex >= start &&
|
if (e.mIndex >= start &&
|
||||||
e.mHash == hash_24 &&
|
e.mHash == hash_24 &&
|
||||||
e.mIndex != 0xff) {
|
e.mIndex != 0xff) {
|
||||||
return e.mIndex;
|
return e.mIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,12 +34,12 @@ protected:
|
||||||
|
|
||||||
struct HashListNode {
|
struct HashListNode {
|
||||||
HashListNode() :
|
HashListNode() :
|
||||||
mIndex(0xff), mHash(0)
|
mIndex(0xff), mHash(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
HashListNode(uint32_t hash, size_t index) :
|
HashListNode(uint32_t hash, size_t index) :
|
||||||
mIndex((uint32_t) index), mHash(hash)
|
mIndex((uint32_t) index), mHash(hash)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,8 +47,7 @@ protected:
|
||||||
uint32_t mHash : 24;
|
uint32_t mHash : 24;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct HashListBlock : public intrusive_list_node<HashList::HashListBlock>
|
struct HashListBlock : public intrusive_list_node<HashList::HashListBlock> {
|
||||||
{
|
|
||||||
HashListBlock();
|
HashListBlock();
|
||||||
|
|
||||||
static const size_t BYTE_SIZE = 128;
|
static const size_t BYTE_SIZE = 128;
|
||||||
|
|
|
@ -59,11 +59,9 @@ esp_err_t Page::load(uint32_t sectorNumber)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if (header.mCrc32 != header.calculateCrc32()) {
|
||||||
else if (header.mCrc32 != header.calculateCrc32()) {
|
|
||||||
header.mState = PageState::CORRUPT;
|
header.mState = PageState::CORRUPT;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mState = header.mState;
|
mState = header.mState;
|
||||||
mSeqNumber = header.mSeqNumber;
|
mSeqNumber = header.mSeqNumber;
|
||||||
}
|
}
|
||||||
|
@ -208,6 +206,7 @@ esp_err_t Page::writeItem(uint8_t nsIndex, ItemType datatype, const char* key, c
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
@ -327,8 +326,7 @@ esp_err_t Page::eraseEntryAndSpan(size_t index)
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
auto rc = alterEntryState(index, EntryState::ERASED);
|
auto rc = alterEntryState(index, EntryState::ERASED);
|
||||||
if (rc != ESP_OK) {
|
if (rc != ESP_OK) {
|
||||||
return rc;
|
return rc;
|
||||||
|
@ -538,8 +536,7 @@ esp_err_t Page::mLoadEntryTable()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if (mState == PageState::FULL || mState == PageState::FREEING) {
|
||||||
else if (mState == PageState::FULL || mState == PageState::FREEING) {
|
|
||||||
// We have already filled mHashList for page in active state.
|
// We have already filled mHashList for page in active state.
|
||||||
// Do the same for the case when page is in full or freeing state.
|
// Do the same for the case when page is in full or freeing state.
|
||||||
Item item;
|
Item item;
|
||||||
|
@ -675,8 +672,7 @@ esp_err_t Page::findItem(uint8_t nsIndex, ItemType datatype, const char* key, si
|
||||||
size_t cachedIndex = mHashList.find(start, Item(nsIndex, datatype, 0, key));
|
size_t cachedIndex = mHashList.find(start, Item(nsIndex, datatype, 0, key));
|
||||||
if (cachedIndex < ENTRY_COUNT) {
|
if (cachedIndex < ENTRY_COUNT) {
|
||||||
start = cachedIndex;
|
start = cachedIndex;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return ESP_ERR_NVS_NOT_FOUND;
|
return ESP_ERR_NVS_NOT_FOUND;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -793,18 +789,15 @@ void Page::debugDump() const
|
||||||
EntryState state = mEntryTable.get(i);
|
EntryState state = mEntryTable.get(i);
|
||||||
if (state == EntryState::EMPTY) {
|
if (state == EntryState::EMPTY) {
|
||||||
printf("E\n");
|
printf("E\n");
|
||||||
}
|
} else if (state == EntryState::ERASED) {
|
||||||
else if (state == EntryState::ERASED) {
|
|
||||||
printf("X\n");
|
printf("X\n");
|
||||||
}
|
} else if (state == EntryState::WRITTEN) {
|
||||||
else if (state == EntryState::WRITTEN) {
|
|
||||||
Item item;
|
Item item;
|
||||||
readEntry(i, item);
|
readEntry(i, item);
|
||||||
if (skip == 0) {
|
if (skip == 0) {
|
||||||
printf("W ns=%2u type=%2u span=%3u key=\"%s\"\n", item.nsIndex, static_cast<unsigned>(item.datatype), item.span, item.key);
|
printf("W ns=%2u type=%2u span=%3u key=\"%s\"\n", item.nsIndex, static_cast<unsigned>(item.datatype), item.span, item.key);
|
||||||
skip = item.span - 1;
|
skip = item.span - 1;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
printf("D\n");
|
printf("D\n");
|
||||||
skip--;
|
skip--;
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,9 +167,11 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
class Header {
|
class Header
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
Header() {
|
Header()
|
||||||
|
{
|
||||||
std::fill_n(mReserved, sizeof(mReserved)/sizeof(mReserved[0]), UINT32_MAX);
|
std::fill_n(mReserved, sizeof(mReserved)/sizeof(mReserved[0]), UINT32_MAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,8 +47,7 @@ esp_err_t PageManager::load(uint32_t baseSector, uint32_t sectorCount)
|
||||||
if (mPageList.empty()) {
|
if (mPageList.empty()) {
|
||||||
mSeqNumber = 0;
|
mSeqNumber = 0;
|
||||||
return activatePage();
|
return activatePage();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
uint32_t lastSeqNo;
|
uint32_t lastSeqNo;
|
||||||
assert(mPageList.back().getSeqNumber(lastSeqNo) == ESP_OK);
|
assert(mPageList.back().getSeqNumber(lastSeqNo) == ESP_OK);
|
||||||
mSeqNumber = lastSeqNo + 1;
|
mSeqNumber = lastSeqNo + 1;
|
||||||
|
@ -78,7 +77,7 @@ esp_err_t PageManager::load(uint32_t baseSector, uint32_t sectorCount)
|
||||||
for (auto it = begin(); it!= end(); ++it) {
|
for (auto it = begin(); it!= end(); ++it) {
|
||||||
if (it->state() == Page::PageState::FREEING) {
|
if (it->state() == Page::PageState::FREEING) {
|
||||||
Page* newPage = &mPageList.back();
|
Page* newPage = &mPageList.back();
|
||||||
if(newPage->state() != Page::PageState::ACTIVE) {
|
if (newPage->state() != Page::PageState::ACTIVE) {
|
||||||
auto err = activatePage();
|
auto err = activatePage();
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
return err;
|
return err;
|
||||||
|
|
|
@ -51,7 +51,7 @@ esp_err_t Storage::init(uint32_t baseSector, uint32_t sectorCount)
|
||||||
Page& p = *it;
|
Page& p = *it;
|
||||||
size_t itemIndex = 0;
|
size_t itemIndex = 0;
|
||||||
Item item;
|
Item item;
|
||||||
while(p.findItem(Page::NS_INDEX, ItemType::U8, nullptr, itemIndex, item) == ESP_OK) {
|
while (p.findItem(Page::NS_INDEX, ItemType::U8, nullptr, itemIndex, item) == ESP_OK) {
|
||||||
NamespaceEntry* entry = new NamespaceEntry;
|
NamespaceEntry* entry = new NamespaceEntry;
|
||||||
item.getKey(entry->mName, sizeof(entry->mName) - 1);
|
item.getKey(entry->mName, sizeof(entry->mName) - 1);
|
||||||
item.getValue(entry->mIndex);
|
item.getValue(entry->mIndex);
|
||||||
|
@ -103,14 +103,13 @@ esp_err_t Storage::writeItem(uint8_t nsIndex, ItemType datatype, const char* key
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
}
|
} else if (err != ESP_OK) {
|
||||||
else if (err != ESP_OK) {
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (findPage) {
|
if (findPage) {
|
||||||
if (findPage->state() == Page::PageState::UNINITIALIZED ||
|
if (findPage->state() == Page::PageState::UNINITIALIZED ||
|
||||||
findPage->state() == Page::PageState::INVALID) {
|
findPage->state() == Page::PageState::INVALID) {
|
||||||
auto err = findItem(nsIndex, datatype, key, findPage, item);
|
auto err = findItem(nsIndex, datatype, key, findPage, item);
|
||||||
assert(err == ESP_OK);
|
assert(err == ESP_OK);
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,7 +78,7 @@ public:
|
||||||
static const size_t MAX_KEY_LENGTH = sizeof(key) - 1;
|
static const size_t MAX_KEY_LENGTH = sizeof(key) - 1;
|
||||||
|
|
||||||
Item(uint8_t nsIndex, ItemType datatype, uint8_t span, const char* key_)
|
Item(uint8_t nsIndex, ItemType datatype, uint8_t span, const char* key_)
|
||||||
: nsIndex(nsIndex), datatype(datatype), span(span), reserved(0xff)
|
: nsIndex(nsIndex), datatype(datatype), span(span), reserved(0xff)
|
||||||
{
|
{
|
||||||
std::fill_n(reinterpret_cast<uint32_t*>(key), sizeof(key) / 4, 0xffffffff);
|
std::fill_n(reinterpret_cast<uint32_t*>(key), sizeof(key) / 4, 0xffffffff);
|
||||||
std::fill_n(reinterpret_cast<uint32_t*>(data), sizeof(data) / 4, 0xffffffff);
|
std::fill_n(reinterpret_cast<uint32_t*>(data), sizeof(data) / 4, 0xffffffff);
|
||||||
|
|
Loading…
Reference in a new issue