nvs_item_hash_list.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "nvs_item_hash_list.hpp"
  14. namespace nvs
  15. {
  16. HashList::HashList()
  17. {
  18. }
  19. void HashList::clear()
  20. {
  21. for (auto it = mBlockList.begin(); it != mBlockList.end();) {
  22. auto tmp = it;
  23. ++it;
  24. mBlockList.erase(tmp);
  25. delete static_cast<HashListBlock*>(tmp);
  26. }
  27. }
  28. HashList::~HashList()
  29. {
  30. clear();
  31. }
  32. HashList::HashListBlock::HashListBlock()
  33. {
  34. static_assert(sizeof(HashListBlock) == HashListBlock::BYTE_SIZE,
  35. "cache block size calculation incorrect");
  36. }
  37. esp_err_t HashList::insert(const Item& item, size_t index)
  38. {
  39. const uint32_t hash_24 = item.calculateCrc32WithoutValue() & 0xffffff;
  40. // add entry to the end of last block if possible
  41. if (mBlockList.size()) {
  42. auto& block = mBlockList.back();
  43. if (block.mCount < HashListBlock::ENTRY_COUNT) {
  44. block.mNodes[block.mCount++] = HashListNode(hash_24, index);
  45. return ESP_OK;
  46. }
  47. }
  48. // if the above failed, create a new block and add entry to it
  49. HashListBlock* newBlock = new (std::nothrow) HashListBlock;
  50. if (!newBlock) return ESP_ERR_NO_MEM;
  51. mBlockList.push_back(newBlock);
  52. newBlock->mNodes[0] = HashListNode(hash_24, index);
  53. newBlock->mCount++;
  54. return ESP_OK;
  55. }
  56. bool HashList::erase(size_t index)
  57. {
  58. for (auto it = mBlockList.begin(); it != mBlockList.end();) {
  59. bool haveEntries = false;
  60. bool foundIndex = false;
  61. for (size_t i = 0; i < it->mCount; ++i) {
  62. if (it->mNodes[i].mIndex == index) {
  63. it->mNodes[i].mIndex = 0xff;
  64. foundIndex = true;
  65. /* found the item and removed it */
  66. }
  67. if (it->mNodes[i].mIndex != 0xff) {
  68. haveEntries = true;
  69. }
  70. if (haveEntries && foundIndex) {
  71. /* item was found, and HashListBlock still has some items */
  72. return true;
  73. }
  74. }
  75. /* no items left in HashListBlock, can remove */
  76. if (!haveEntries) {
  77. auto tmp = it;
  78. ++it;
  79. mBlockList.erase(tmp);
  80. delete static_cast<HashListBlock*>(tmp);
  81. } else {
  82. ++it;
  83. }
  84. if (foundIndex) {
  85. /* item was found and empty HashListBlock was removed */
  86. return true;
  87. }
  88. }
  89. // item hasn't been present in cache");
  90. return false;
  91. }
  92. size_t HashList::find(size_t start, const Item& item)
  93. {
  94. const uint32_t hash_24 = item.calculateCrc32WithoutValue() & 0xffffff;
  95. for (auto it = mBlockList.begin(); it != mBlockList.end(); ++it) {
  96. for (size_t index = 0; index < it->mCount; ++index) {
  97. HashListNode& e = it->mNodes[index];
  98. if (e.mIndex >= start &&
  99. e.mHash == hash_24 &&
  100. e.mIndex != 0xff) {
  101. return e.mIndex;
  102. }
  103. }
  104. }
  105. return SIZE_MAX;
  106. }
  107. } // namespace nvs