pw_kvs: Modify FindSectorWithSpace() for extra default options
Modify FindSectorWithSpace() with option to skip a sector
in the search as well as to bypass the free sector rule.
Change-Id: I750b91562a2d3af5d8952ece0db94c2c01659bb7
diff --git a/pw_kvs/key_value_store.cc b/pw_kvs/key_value_store.cc
index a778851..fdf5e14 100644
--- a/pw_kvs/key_value_store.cc
+++ b/pw_kvs/key_value_store.cc
@@ -468,10 +468,13 @@
return Status::UNIMPLEMENTED;
}
-// Find either an existing sector with enough space, or an empty sector.
-// Maintains the invariant that there is always at least 1 empty sector.
+// Find either an existing sector with enough space that is not the sector to
+// skip, or an empty sector. Maintains the invariant that there is always at
+// least 1 empty sector unless set to bypass the rule.
KeyValueStore::SectorDescriptor* KeyValueStore::FindSectorWithSpace(
- size_t size) {
+ size_t size,
+ SectorDescriptor* sector_to_skip,
+ bool bypass_empty_sector_rule) {
const size_t sector_count = partition_.sector_count();
// TODO: Ignore last written sector for now and scan from the beginning.
@@ -479,13 +482,18 @@
size_t start = (last_written_sector_ + 1) % sector_count;
SectorDescriptor* first_empty_sector = nullptr;
- bool at_least_two_empty_sectors = false;
+ bool at_least_two_empty_sectors = bypass_empty_sector_rule;
for (size_t i = start; i != last_written_sector_;
i = (i + 1) % sector_count) {
DBG("Examining sector %zu", i);
SectorDescriptor& sector = sector_map_[i];
+ if (sector_to_skip == §or) {
+ DBG("Skipping the skip sector");
+ continue;
+ }
+
if (!SectorEmpty(sector) && sector.HasSpace(size)) {
DBG("Partially occupied sector with enough space; done!");
return §or;
diff --git a/pw_kvs/public/pw_kvs/key_value_store.h b/pw_kvs/public/pw_kvs/key_value_store.h
index 8e9c9c6..2b17a97 100644
--- a/pw_kvs/public/pw_kvs/key_value_store.h
+++ b/pw_kvs/public/pw_kvs/key_value_store.h
@@ -265,7 +265,10 @@
Status RelocateEntry(KeyDescriptor& key_descriptor);
- SectorDescriptor* FindSectorWithSpace(size_t size);
+ SectorDescriptor* FindSectorWithSpace(
+ size_t size,
+ SectorDescriptor* sector_to_skip = nullptr,
+ bool bypass_empty_sector_rule = false);
Status FindOrRecoverSectorWithSpace(SectorDescriptor** sector, size_t size);