pw_kvs: Add sector methods to remove free bytes and valid bytes
Add methods for SectorDescriptor to RemoveFreeBytes and
RemoveValidBytes. This will allow adding an underflow check.
Change-Id: Ie5c2527df16dde37143f52f13f6646448497530d
diff --git a/pw_kvs/key_value_store.cc b/pw_kvs/key_value_store.cc
index c9c4820..617eb6b 100644
--- a/pw_kvs/key_value_store.cc
+++ b/pw_kvs/key_value_store.cc
@@ -478,7 +478,7 @@
DBG("Writing existing entry; found sector: %zu", SectorIndex(sector));
TRY(AppendEntry(sector, key_descriptor, key, value, new_state));
- old_sector.valid_bytes -= original_entry.size();
+ old_sector.RemoveValidBytes(original_entry.size());
return Status::OK;
}
@@ -547,9 +547,7 @@
// Do the valid bytes accounting for the sector the entry was relocated out
// of.
- // TODO: Move this accounting to a method in SectorDescriptor, with a
- // safety/correctness check that valid_bytes >= size.
- old_sector.valid_bytes -= header.size();
+ old_sector.RemoveValidBytes(header.size());
return Status::OK;
}
@@ -762,7 +760,7 @@
key_descriptor->state = new_state;
sector->valid_bytes += written;
- sector->tail_free_bytes -= written;
+ sector->RemoveFreeBytes(written);
return Status::OK;
}
diff --git a/pw_kvs/public/pw_kvs/key_value_store.h b/pw_kvs/public/pw_kvs/key_value_store.h
index b67de56..792682a 100644
--- a/pw_kvs/public/pw_kvs/key_value_store.h
+++ b/pw_kvs/public/pw_kvs/key_value_store.h
@@ -246,6 +246,16 @@
bool HasSpace(size_t required_space) const {
return (tail_free_bytes >= required_space);
}
+
+ void RemoveFreeBytes(size_t size) {
+ // TODO: add safety check for tail_free_bytes > size.
+ tail_free_bytes -= size;
+ }
+
+ void RemoveValidBytes(size_t size) {
+ // TODO: add safety check for valid_bytes > size.
+ valid_bytes -= size;
+ }
};
static uint32_t HashKey(std::string_view string);