pw_kvs: Add part of the garbage collection implementation

Add part of the KVS garbage collection. This gets most of what is
needed, with only the RelocateEntry() method needing work.

Change-Id: Icff1926f43e620d9f48ffb0b94d8c8139836c30a
diff --git a/pw_kvs/public/pw_kvs/flash_memory.h b/pw_kvs/public/pw_kvs/flash_memory.h
index 5495141..06a5148 100644
--- a/pw_kvs/public/pw_kvs/flash_memory.h
+++ b/pw_kvs/public/pw_kvs/flash_memory.h
@@ -189,15 +189,13 @@
                                 size_t len,
                                 bool* is_erased);
 
-  constexpr uint32_t sector_size_bytes() const {
+  // Overridden by derived classes. The reported sector size is space available
+  // to users of FlashPartition. It accounts for space reserved in the sector
+  // for FlashPartition to store metadata.
+  virtual uint32_t sector_size_bytes() const {
     return flash_.sector_size_bytes();
   }
 
-  // Overridden by base classes which store metadata at the start of a sector.
-  virtual uint32_t sector_available_size_bytes() const {
-    return sector_size_bytes();
-  }
-
   size_t size_bytes() const { return sector_count() * sector_size_bytes(); }
 
   virtual size_t alignment_bytes() const { return flash_.alignment_bytes(); }
diff --git a/pw_kvs/public/pw_kvs/key_value_store.h b/pw_kvs/public/pw_kvs/key_value_store.h
index 115bea0..f399629 100644
--- a/pw_kvs/public/pw_kvs/key_value_store.h
+++ b/pw_kvs/public/pw_kvs/key_value_store.h
@@ -248,6 +248,8 @@
 
   Status WriteEntryForNewKey(std::string_view key, span<const std::byte> value);
 
+  Status RelocateEntry(KeyMapEntry& entry);
+
   SectorMapEntry* FindSectorWithSpace(size_t size);
 
   Status FindOrRecoverSectorWithSpace(SectorMapEntry** sector, size_t size);
@@ -268,10 +270,15 @@
       std::string_view key,
       span<const std::byte> value) const;
 
-  Status RelocateEntry(KeyMapEntry* entry);
+  bool AddressInSector(const SectorMapEntry& sector, Address address) const {
+    const Address sector_base = SectorBaseAddress(&sector);
+    const Address sector_end = sector_base + partition_.sector_size_bytes();
+
+    return ((address >= sector_base) && (address < sector_end));
+  }
 
   bool SectorEmpty(const SectorMapEntry& sector) const {
-    return (sector.tail_free_bytes == partition_.sector_available_size_bytes());
+    return (sector.tail_free_bytes == partition_.sector_size_bytes());
   }
 
   size_t RecoverableBytes(const SectorMapEntry& sector) {
@@ -279,7 +286,7 @@
            sector.tail_free_bytes;
   }
 
-  Address SectorBaseAddress(SectorMapEntry* sector) const {
+  Address SectorBaseAddress(const SectorMapEntry* sector) const {
     return (sector - sector_map_.data()) * partition_.sector_size_bytes();
   }