nvs_item_hash_list.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. void 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;
  46. }
  47. }
  48. // if the above failed, create a new block and add entry to it
  49. HashListBlock* newBlock = new HashListBlock;
  50. mBlockList.push_back(newBlock);
  51. newBlock->mNodes[0] = HashListNode(hash_24, index);
  52. newBlock->mCount++;
  53. }
  54. void HashList::erase(size_t index, bool itemShouldExist)
  55. {
  56. for (auto it = mBlockList.begin(); it != mBlockList.end();) {
  57. bool haveEntries = false;
  58. for (size_t i = 0; i < it->mCount; ++i) {
  59. if (it->mNodes[i].mIndex == index) {
  60. it->mNodes[i].mIndex = 0xff;
  61. return;
  62. }
  63. if (it->mNodes[i].mIndex != 0xff) {
  64. haveEntries = true;
  65. }
  66. }
  67. if (!haveEntries) {
  68. auto tmp = it;
  69. ++it;
  70. mBlockList.erase(tmp);
  71. delete static_cast<HashListBlock*>(tmp);
  72. } else {
  73. ++it;
  74. }
  75. }
  76. if (itemShouldExist) {
  77. assert(false && "item should have been present in cache");
  78. }
  79. }
  80. size_t HashList::find(size_t start, const Item& item)
  81. {
  82. const uint32_t hash_24 = item.calculateCrc32WithoutValue() & 0xffffff;
  83. for (auto it = mBlockList.begin(); it != mBlockList.end(); ++it) {
  84. for (size_t index = 0; index < it->mCount; ++index) {
  85. HashListNode& e = it->mNodes[index];
  86. if (e.mIndex >= start &&
  87. e.mHash == hash_24 &&
  88. e.mIndex != 0xff) {
  89. return e.mIndex;
  90. }
  91. }
  92. }
  93. return SIZE_MAX;
  94. }
  95. } // namespace nvs