pw_status: Minor update for PW_TRY macros
- Remove TRY* short name macros and switch existing use to PW_TRY*
- Make PW_TRY macros use constexpr rather than inline internally.
Change-Id: Ia86071183c76568241425ae8b5eafec8313fc133
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/18165
Reviewed-by: Keir Mierle <keir@google.com>
Commit-Queue: David Rogers <davidrogers@google.com>
diff --git a/pw_kvs/alignment.cc b/pw_kvs/alignment.cc
index 6e1d75c..1192905 100644
--- a/pw_kvs/alignment.cc
+++ b/pw_kvs/alignment.cc
@@ -23,7 +23,7 @@
size_t to_copy = std::min(write_size_ - bytes_in_buffer_, data.size());
std::memcpy(&buffer_[bytes_in_buffer_], data.data(), to_copy);
- TRY_WITH_SIZE(AddBytesToBuffer(to_copy));
+ PW_TRY_WITH_SIZE(AddBytesToBuffer(to_copy));
data = data.subspan(to_copy);
}
@@ -58,7 +58,7 @@
if (!result.ok()) {
return StatusWithSize(result.status(), bytes_written_);
}
- TRY_WITH_SIZE(AddBytesToBuffer(to_read));
+ PW_TRY_WITH_SIZE(AddBytesToBuffer(to_read));
size -= result.size();
}
diff --git a/pw_kvs/entry.cc b/pw_kvs/entry.cc
index 4ec7481..cfa9e76 100644
--- a/pw_kvs/entry.cc
+++ b/pw_kvs/entry.cc
@@ -40,7 +40,7 @@
const internal::EntryFormats& formats,
Entry* entry) {
EntryHeader header;
- TRY(partition.Read(address, sizeof(header), &header));
+ PW_TRY(partition.Read(address, sizeof(header), &header));
if (partition.AppearsErased(std::as_bytes(std::span(&header.magic, 1)))) {
return Status::NOT_FOUND;
@@ -134,11 +134,11 @@
// Use this object's header rather than the header in flash of flash, since
// this Entry may have been updated.
- TRY_WITH_SIZE(writer.Write(&header_, sizeof(header_)));
+ PW_TRY_WITH_SIZE(writer.Write(&header_, sizeof(header_)));
// Write only the key and value from the original entry.
FlashPartition::Input input(partition(), address() + sizeof(EntryHeader));
- TRY_WITH_SIZE(writer.Write(input, key_length() + value_size()));
+ PW_TRY_WITH_SIZE(writer.Write(input, key_length() + value_size()));
return writer.Flush();
}
@@ -154,7 +154,7 @@
StatusWithSize result = partition().Read(
address_ + sizeof(EntryHeader) + key_length() + offset_bytes,
buffer.subspan(0, read_size));
- TRY_WITH_SIZE(result);
+ PW_TRY_WITH_SIZE(result);
if (read_size != remaining_bytes) {
return StatusWithSize(Status::RESOURCE_EXHAUSTED, read_size);
@@ -174,7 +174,7 @@
std::array<std::byte, 2 * kMinAlignmentBytes> buffer;
while (address < end) {
const size_t read_size = std::min(size_t(end - address), buffer.size());
- TRY(partition_->Read(address, std::span(buffer).first(read_size)));
+ PW_TRY(partition_->Read(address, std::span(buffer).first(read_size)));
if (std::memcmp(buffer.data(), value_ptr, read_size) != 0) {
return Status::NOT_FOUND;
@@ -210,7 +210,7 @@
Address read_address = address_;
// Read the first chunk, which includes the header, and compare the checksum.
- TRY(partition().Read(read_address, read_size, buffer));
+ PW_TRY(partition().Read(read_address, read_size, buffer));
if (header_to_verify.checksum != header_.checksum) {
PW_LOG_ERROR("Expected checksum 0x%08" PRIx32 ", found 0x%08" PRIx32,
@@ -240,7 +240,7 @@
// Read the next chunk into the buffer.
read_address += read_size;
read_size = std::min(sizeof(buffer), bytes_to_read);
- TRY(partition().Read(read_address, read_size, buffer));
+ PW_TRY(partition().Read(read_address, read_size, buffer));
}
checksum_algo_->Finish();
@@ -295,7 +295,7 @@
std::array<std::byte, 2 * kMinAlignmentBytes> buffer;
while (address < end) {
const size_t read_size = std::min(size_t(end - address), buffer.size());
- TRY(partition_->Read(address, std::span(buffer).first(read_size)));
+ PW_TRY(partition_->Read(address, std::span(buffer).first(read_size)));
checksum_algo_->Update(buffer.data(), read_size);
address += read_size;
diff --git a/pw_kvs/flash_memory.cc b/pw_kvs/flash_memory.cc
index 49145bf..5da2f85 100644
--- a/pw_kvs/flash_memory.cc
+++ b/pw_kvs/flash_memory.cc
@@ -30,7 +30,7 @@
using std::byte;
StatusWithSize FlashPartition::Output::DoWrite(std::span<const byte> data) {
- TRY_WITH_SIZE(flash_.Write(address_, data));
+ PW_TRY_WITH_SIZE(flash_.Write(address_, data));
address_ += data.size();
return StatusWithSize(data.size());
}
@@ -46,7 +46,7 @@
return Status::PERMISSION_DENIED;
}
- TRY(CheckBounds(address, num_sectors * sector_size_bytes()));
+ PW_TRY(CheckBounds(address, num_sectors * sector_size_bytes()));
const size_t address_sector_offset = address % sector_size_bytes();
PW_CHECK_UINT_EQ(address_sector_offset, 0u);
@@ -54,7 +54,7 @@
}
StatusWithSize FlashPartition::Read(Address address, std::span<byte> output) {
- TRY_WITH_SIZE(CheckBounds(address, output.size()));
+ PW_TRY_WITH_SIZE(CheckBounds(address, output.size()));
return flash_.Read(PartitionToFlashAddress(address), output);
}
@@ -63,7 +63,7 @@
if (permission_ == PartitionPermission::kReadOnly) {
return StatusWithSize::PERMISSION_DENIED;
}
- TRY_WITH_SIZE(CheckBounds(address, data.size()));
+ PW_TRY_WITH_SIZE(CheckBounds(address, data.size()));
const size_t address_alignment_offset = address % alignment_bytes();
PW_CHECK_UINT_EQ(address_alignment_offset, 0u);
const size_t size_alignment_offset = data.size() % alignment_bytes();
@@ -96,7 +96,7 @@
while (length > 0u) {
// Check earlier that length is aligned, no need to round up
size_t read_size = std::min(sizeof(buffer), length);
- TRY(Read(source_flash_address + offset, read_size, buffer).status());
+ PW_TRY(Read(source_flash_address + offset, read_size, buffer).status());
for (byte b : std::span(buffer, read_size)) {
if (b != erased_byte) {
diff --git a/pw_kvs/key_value_store.cc b/pw_kvs/key_value_store.cc
index 83bdac6..05441d9 100644
--- a/pw_kvs/key_value_store.cc
+++ b/pw_kvs/key_value_store.cc
@@ -352,14 +352,14 @@
Status KeyValueStore::LoadEntry(Address entry_address,
Address* next_entry_address) {
Entry entry;
- TRY(Entry::Read(partition_, entry_address, formats_, &entry));
+ PW_TRY(Entry::Read(partition_, entry_address, formats_, &entry));
// Read the key from flash & validate the entry (which reads the value).
Entry::KeyBuffer key_buffer;
- TRY_ASSIGN(size_t key_length, entry.ReadKey(key_buffer));
+ PW_TRY_ASSIGN(size_t key_length, entry.ReadKey(key_buffer));
const string_view key(key_buffer.data(), key_length);
- TRY(entry.VerifyChecksumInFlash());
+ PW_TRY(entry.VerifyChecksumInFlash());
// A valid entry was found, so update the next entry address before doing any
// of the checks that happen in AddNewOrUpdateExisting.
@@ -402,16 +402,16 @@
StatusWithSize KeyValueStore::Get(string_view key,
std::span<byte> value_buffer,
size_t offset_bytes) const {
- TRY_WITH_SIZE(CheckReadOperation(key));
+ PW_TRY_WITH_SIZE(CheckReadOperation(key));
EntryMetadata metadata;
- TRY_WITH_SIZE(FindExisting(key, &metadata));
+ PW_TRY_WITH_SIZE(FindExisting(key, &metadata));
return Get(key, metadata, value_buffer, offset_bytes);
}
Status KeyValueStore::PutBytes(string_view key, std::span<const byte> value) {
- TRY(CheckWriteOperation(key));
+ PW_TRY(CheckWriteOperation(key));
DBG("Writing key/value; key length=%u, value length=%u",
unsigned(key.size()),
unsigned(value.size()));
@@ -443,10 +443,10 @@
}
Status KeyValueStore::Delete(string_view key) {
- TRY(CheckWriteOperation(key));
+ PW_TRY(CheckWriteOperation(key));
EntryMetadata metadata;
- TRY(FindExisting(key, &metadata));
+ PW_TRY(FindExisting(key, &metadata));
// TODO: figure out logging how to support multiple addresses.
DBG("Writing tombstone for key 0x%08x in %u sectors including %u",
@@ -484,10 +484,10 @@
}
StatusWithSize KeyValueStore::ValueSize(string_view key) const {
- TRY_WITH_SIZE(CheckReadOperation(key));
+ PW_TRY_WITH_SIZE(CheckReadOperation(key));
EntryMetadata metadata;
- TRY_WITH_SIZE(FindExisting(key, &metadata));
+ PW_TRY_WITH_SIZE(FindExisting(key, &metadata));
return ValueSize(metadata);
}
@@ -541,7 +541,7 @@
size_t offset_bytes) const {
Entry entry;
- TRY_WITH_SIZE(ReadEntry(metadata, entry));
+ PW_TRY_WITH_SIZE(ReadEntry(metadata, entry));
StatusWithSize result = entry.ReadValue(value_buffer, offset_bytes);
if (result.ok() && options_.verify_on_read && offset_bytes == 0u) {
@@ -560,10 +560,10 @@
Status KeyValueStore::FixedSizeGet(std::string_view key,
void* value,
size_t size_bytes) const {
- TRY(CheckWriteOperation(key));
+ PW_TRY(CheckWriteOperation(key));
EntryMetadata metadata;
- TRY(FindExisting(key, &metadata));
+ PW_TRY(FindExisting(key, &metadata));
return FixedSizeGet(key, metadata, value, size_bytes);
}
@@ -574,7 +574,7 @@
size_t size_bytes) const {
// Ensure that the size of the stored value matches the size of the type.
// Otherwise, report error. This check avoids potential memory corruption.
- TRY_ASSIGN(const size_t actual_size, ValueSize(metadata));
+ PW_TRY_ASSIGN(const size_t actual_size, ValueSize(metadata));
if (actual_size != size_bytes) {
DBG("Requested %u B read, but value is %u B",
@@ -591,7 +591,7 @@
StatusWithSize KeyValueStore::ValueSize(const EntryMetadata& metadata) const {
Entry entry;
- TRY_WITH_SIZE(ReadEntry(metadata, entry));
+ PW_TRY_WITH_SIZE(ReadEntry(metadata, entry));
return StatusWithSize(entry.value_size());
}
@@ -627,7 +627,7 @@
std::span<const byte> value) {
// Read the original entry to get the size for sector accounting purposes.
Entry entry;
- TRY(ReadEntry(metadata, entry));
+ PW_TRY(ReadEntry(metadata, entry));
return WriteEntry(key, value, new_state, &metadata, &entry);
}
@@ -668,11 +668,11 @@
// Find addresses to write the entry to. This may involve garbage collecting
// one or more sectors.
const size_t entry_size = Entry::size(partition_, key, value);
- TRY(GetAddressesForWrite(reserved_addresses, entry_size));
+ PW_TRY(GetAddressesForWrite(reserved_addresses, entry_size));
// Write the entry at the first address that was found.
Entry entry = CreateEntry(reserved_addresses[0], key, value, new_state);
- TRY(AppendEntry(entry, key, value));
+ PW_TRY(AppendEntry(entry, key, value));
// After writing the first entry successfully, update the key descriptors.
// Once a single new the entry is written, the old entries are invalidated.
@@ -683,7 +683,7 @@
// Write the additional copies of the entry, if redundancy is greater than 1.
for (size_t i = 1; i < redundancy(); ++i) {
entry.set_address(reserved_addresses[i]);
- TRY(AppendEntry(entry, key, value));
+ PW_TRY(AppendEntry(entry, key, value));
new_metadata.AddNewAddress(reserved_addresses[i]);
}
return Status::OK;
@@ -721,7 +721,8 @@
size_t write_size) {
for (size_t i = 0; i < redundancy(); i++) {
SectorDescriptor* sector;
- TRY(GetSectorForWrite(§or, write_size, std::span(write_addresses, i)));
+ PW_TRY(
+ GetSectorForWrite(§or, write_size, std::span(write_addresses, i)));
write_addresses[i] = sectors_.NextWritableAddress(*sector);
DBG("Found space for entry in sector %u at address %u",
@@ -803,11 +804,11 @@
unsigned(entry.size()),
unsigned(entry.address()),
unsigned(result.size()));
- TRY(MarkSectorCorruptIfNotOk(result.status(), §or));
+ PW_TRY(MarkSectorCorruptIfNotOk(result.status(), §or));
}
if (options_.verify_on_write) {
- TRY(MarkSectorCorruptIfNotOk(entry.VerifyChecksumInFlash(), §or));
+ PW_TRY(MarkSectorCorruptIfNotOk(entry.VerifyChecksumInFlash(), §or));
}
sector.RemoveWritableBytes(result.size());
@@ -820,16 +821,16 @@
Address new_address) {
const StatusWithSize result = entry.Copy(new_address);
- TRY_WITH_SIZE(MarkSectorCorruptIfNotOk(result.status(), new_sector));
+ PW_TRY_WITH_SIZE(MarkSectorCorruptIfNotOk(result.status(), new_sector));
if (options_.verify_on_write) {
Entry new_entry;
- TRY_WITH_SIZE(MarkSectorCorruptIfNotOk(
+ PW_TRY_WITH_SIZE(MarkSectorCorruptIfNotOk(
Entry::Read(partition_, new_address, formats_, &new_entry),
new_sector));
// TODO: add test that catches doing the verify on the old entry.
- TRY_WITH_SIZE(MarkSectorCorruptIfNotOk(new_entry.VerifyChecksumInFlash(),
- new_sector));
+ PW_TRY_WITH_SIZE(MarkSectorCorruptIfNotOk(new_entry.VerifyChecksumInFlash(),
+ new_sector));
}
// Entry was written successfully; update descriptor's address and the sector
// descriptors to reflect the new entry.
@@ -844,7 +845,7 @@
KeyValueStore::Address& address,
std::span<const Address> reserved_addresses) {
Entry entry;
- TRY(ReadEntry(metadata, entry));
+ PW_TRY(ReadEntry(metadata, entry));
// Find a new sector for the entry and write it to the new location. For
// relocation the find should not not be a sector already containing the key
@@ -854,12 +855,12 @@
// an immediate extra relocation).
SectorDescriptor* new_sector;
- TRY(sectors_.FindSpaceDuringGarbageCollection(
+ PW_TRY(sectors_.FindSpaceDuringGarbageCollection(
&new_sector, entry.size(), metadata.addresses(), reserved_addresses));
Address new_address = sectors_.NextWritableAddress(*new_sector);
- TRY_ASSIGN(const size_t result_size,
- CopyEntryToSector(entry, new_sector, new_address));
+ PW_TRY_ASSIGN(const size_t result_size,
+ CopyEntryToSector(entry, new_sector, new_address));
sectors_.FromAddress(address).RemoveValidBytes(result_size);
address = new_address;
@@ -877,7 +878,7 @@
CheckForErrors();
if (error_detected_) {
- TRY(Repair());
+ PW_TRY(Repair());
}
StatusWithSize update_status = UpdateEntriesToPrimaryFormat();
Status overall_status = update_status.status();
@@ -936,7 +937,7 @@
CheckForErrors();
// Do automatic repair, if KVS options allow for it.
if (error_detected_ && options_.recovery != ErrorRecovery::kManual) {
- TRY(Repair());
+ PW_TRY(Repair());
}
return GarbageCollect(std::span<const Address>());
}
@@ -970,7 +971,7 @@
DBG(" Relocate entry for Key 0x%08" PRIx32 ", sector %u",
metadata.hash(),
sectors_.Index(sectors_.FromAddress(address)));
- TRY(RelocateEntry(metadata, address, reserved_addresses));
+ PW_TRY(RelocateEntry(metadata, address, reserved_addresses));
}
}
@@ -984,7 +985,7 @@
// Step 1: Move any valid entries in the GC sector to other sectors
if (sector_to_gc.valid_bytes() != 0) {
for (EntryMetadata& metadata : entry_cache_) {
- TRY(RelocateKeyAddressesInSector(
+ PW_TRY(RelocateKeyAddressesInSector(
sector_to_gc, metadata, reserved_addresses));
}
}
@@ -998,7 +999,7 @@
// Step 2: Reinitialize the sector
sector_to_gc.mark_corrupt();
- TRY(partition_.Erase(sectors_.BaseAddress(sector_to_gc), 1));
+ PW_TRY(partition_.Erase(sectors_.BaseAddress(sector_to_gc), 1));
sector_to_gc.set_writable_bytes(partition_.sector_size_bytes());
DBG(" Garbage Collect sector %u complete", sectors_.Index(sector_to_gc));
@@ -1009,7 +1010,7 @@
size_t entries_updated = 0;
for (EntryMetadata& prior_metadata : entry_cache_) {
Entry entry;
- TRY_WITH_SIZE(ReadEntry(prior_metadata, entry));
+ PW_TRY_WITH_SIZE(ReadEntry(prior_metadata, entry));
if (formats_.primary().magic == entry.magic()) {
// Ignore entries that are already on the primary format.
continue;
@@ -1024,16 +1025,16 @@
entries_updated++;
last_transaction_id_ += 1;
- TRY_WITH_SIZE(entry.Update(formats_.primary(), last_transaction_id_));
+ PW_TRY_WITH_SIZE(entry.Update(formats_.primary(), last_transaction_id_));
// List of addresses for sectors with space for this entry.
Address* reserved_addresses = entry_cache_.TempReservedAddressesForWrite();
// Find addresses to write the entry to. This may involve garbage collecting
// one or more sectors.
- TRY_WITH_SIZE(GetAddressesForWrite(reserved_addresses, entry.size()));
+ PW_TRY_WITH_SIZE(GetAddressesForWrite(reserved_addresses, entry.size()));
- TRY_WITH_SIZE(
+ PW_TRY_WITH_SIZE(
CopyEntryToSector(entry,
§ors_.FromAddress(reserved_addresses[0]),
reserved_addresses[0]));
@@ -1046,7 +1047,7 @@
// Write the additional copies of the entry, if redundancy is greater
// than 1.
for (size_t i = 1; i < redundancy(); ++i) {
- TRY_WITH_SIZE(
+ PW_TRY_WITH_SIZE(
CopyEntryToSector(entry,
§ors_.FromAddress(reserved_addresses[i]),
reserved_addresses[i]));
@@ -1060,15 +1061,15 @@
// Add any missing redundant entries/copies for a key.
Status KeyValueStore::AddRedundantEntries(EntryMetadata& metadata) {
Entry entry;
- TRY(ReadEntry(metadata, entry));
- TRY(entry.VerifyChecksumInFlash());
+ PW_TRY(ReadEntry(metadata, entry));
+ PW_TRY(entry.VerifyChecksumInFlash());
while (metadata.addresses().size() < redundancy()) {
SectorDescriptor* new_sector;
- TRY(GetSectorForWrite(&new_sector, entry.size(), metadata.addresses()));
+ PW_TRY(GetSectorForWrite(&new_sector, entry.size(), metadata.addresses()));
Address new_address = sectors_.NextWritableAddress(*new_sector);
- TRY(CopyEntryToSector(entry, new_sector, new_address));
+ PW_TRY(CopyEntryToSector(entry, new_sector, new_address));
metadata.AddNewAddress(new_address);
}
diff --git a/pw_status/public/pw_status/try.h b/pw_status/public/pw_status/try.h
index cf4e673..76e7a19 100644
--- a/pw_status/public/pw_status/try.h
+++ b/pw_status/public/pw_status/try.h
@@ -51,23 +51,20 @@
#define _PW_TRY_UNIQUE(line) _PW_TRY_UNIQUE_EXPANDED(line)
#define _PW_TRY_UNIQUE_EXPANDED(line) _pw_try_unique_name_##line
-#define TRY PW_TRY
-#define TRY_WITH_SIZE PW_TRY_WITH_SIZE
-#define TRY_ASSIGN PW_TRY_ASSIGN
-
namespace pw::internal {
-inline Status ConvertToStatus(Status status) { return status; }
+constexpr Status ConvertToStatus(Status status) { return status; }
-inline Status ConvertToStatus(StatusWithSize status_with_size) {
+constexpr Status ConvertToStatus(StatusWithSize status_with_size) {
return status_with_size.status();
}
-inline StatusWithSize ConvertToStatusWithSize(Status status) {
+constexpr StatusWithSize ConvertToStatusWithSize(Status status) {
return StatusWithSize(status, 0);
}
-inline StatusWithSize ConvertToStatusWithSize(StatusWithSize status_with_size) {
+constexpr StatusWithSize ConvertToStatusWithSize(
+ StatusWithSize status_with_size) {
return status_with_size;
}
diff --git a/pw_status/try_test.cc b/pw_status/try_test.cc
index 5da96ed..4d52e5a 100644
--- a/pw_status/try_test.cc
+++ b/pw_status/try_test.cc
@@ -23,7 +23,7 @@
StatusWithSize ReturnStatusWithSize(StatusWithSize status) { return status; }
Status TryStatus(Status status) {
- TRY(ReturnStatus(status));
+ PW_TRY(ReturnStatus(status));
// Any status other than OK should have already returned.
EXPECT_EQ(status, Status::OK);
@@ -31,7 +31,7 @@
}
Status TryStatus(StatusWithSize status) {
- TRY(ReturnStatusWithSize(status));
+ PW_TRY(ReturnStatusWithSize(status));
// Any status other than OK should have already returned.
EXPECT_EQ(status.status(), Status::OK);
@@ -98,7 +98,7 @@
}
StatusWithSize TryStatusWithSize(StatusWithSize status) {
- TRY_WITH_SIZE(ReturnStatusWithSize(status));
+ PW_TRY_WITH_SIZE(ReturnStatusWithSize(status));
// Any status other than OK should have already returned.
EXPECT_TRUE(status.ok());
@@ -106,7 +106,7 @@
}
StatusWithSize TryStatusWithSize(Status status) {
- TRY_WITH_SIZE(ReturnStatus(status));
+ PW_TRY_WITH_SIZE(ReturnStatus(status));
// Any status other than OK should have already returned.
EXPECT_EQ(status, Status::OK);