Skip to content

Commit fdae1ae

Browse files
andy31415andreilitvinrestyled-commitsbzbarsky-apple
authored
Update MetadataList logic for better error handling (#37334)
* Update with review comments from 37127 * Addressing more comments * One more comment update * Restyled by clang-format * Update src/app/data-model-provider/ProviderMetadataTree.h Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> --------- Co-authored-by: Andrei Litvin <andreilitvin@google.com> Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent a8f1f99 commit fdae1ae

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

src/app/data-model-provider/MetadataList.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ CHIP_ERROR GenericAppendOnlyBuffer::EnsureAppendCapacity(size_t numElements)
8282

8383
if (mBuffer == nullptr)
8484
{
85-
mBuffer = static_cast<uint8_t *>(Platform::MemoryCalloc(numElements, mElementSize));
85+
mBuffer = static_cast<uint8_t *>(Platform::MemoryCalloc(numElements, mElementSize));
86+
VerifyOrReturnError(mBuffer != nullptr, CHIP_ERROR_NO_MEMORY);
8687
mCapacity = numElements;
8788
mBufferIsAllocated = true;
88-
return mBuffer != nullptr ? CHIP_NO_ERROR : CHIP_ERROR_NO_MEMORY;
89+
return CHIP_NO_ERROR;
8990
}
9091

9192
// we already have the data in buffer. we have two choices:
@@ -100,9 +101,9 @@ CHIP_ERROR GenericAppendOnlyBuffer::EnsureAppendCapacity(size_t numElements)
100101
else
101102
{
102103
// this is NOT an allocated buffer, but it should become one
103-
auto new_buffer = static_cast<uint8_t *>(Platform::MemoryCalloc(mElementCount + numElements, mElementSize));
104-
mBufferIsAllocated = true;
104+
auto new_buffer = static_cast<uint8_t *>(Platform::MemoryCalloc(mElementCount + numElements, mElementSize));
105105
VerifyOrReturnError(new_buffer != nullptr, CHIP_ERROR_NO_MEMORY);
106+
mBufferIsAllocated = true;
106107
memcpy(new_buffer, mBuffer, mElementCount * mElementSize);
107108
mBuffer = new_buffer;
108109
}
@@ -119,7 +120,7 @@ CHIP_ERROR GenericAppendOnlyBuffer::AppendSingleElementRaw(const void * buffer)
119120
return CHIP_NO_ERROR;
120121
}
121122

122-
CHIP_ERROR GenericAppendOnlyBuffer::AppendElementArrayRaw(const void * buffer, size_t numElements)
123+
CHIP_ERROR GenericAppendOnlyBuffer::AppendElementArrayRaw(const void * __restrict__ buffer, size_t numElements)
123124
{
124125
ReturnErrorOnFailure(EnsureAppendCapacity(numElements));
125126

src/app/data-model-provider/MetadataList.h

+14-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class GenericAppendOnlyBuffer
4747
/// Ensure that at least the specified number of elements
4848
/// can be appended to the internal buffer;
4949
///
50-
/// This will cause the internal buffer to become and allocated buffer
50+
/// This will cause the internal buffer to become an allocated buffer
5151
CHIP_ERROR EnsureAppendCapacity(size_t numElements);
5252

5353
bool IsEmpty() const { return mElementCount == 0; }
@@ -66,7 +66,10 @@ class GenericAppendOnlyBuffer
6666
///
6767
/// This ALWAYS COPIES the elements internally.
6868
/// Additional capacity is AUTOMATICALLY ADDED.
69-
CHIP_ERROR AppendElementArrayRaw(const void * buffer, size_t numElements);
69+
///
70+
/// buffer MUST NOT point inside "own" buffer as mBuffer may be reallocated
71+
/// as part of the appending.
72+
CHIP_ERROR AppendElementArrayRaw(const void * __restrict__ buffer, size_t numElements);
7073

7174
/// Appends a list of elements from a raw array.
7275
///
@@ -93,7 +96,11 @@ class GenericAppendOnlyBuffer
9396

9497
/// Represents a RAII instance owning a buffer.
9598
///
96-
/// It auto-frees the owned buffer on destruction
99+
/// It auto-frees the owned buffer on destruction via `Platform::MemoryFree`.
100+
///
101+
/// This class is designed to be a storage class for `GenericAppendOnlyBuffer` and
102+
/// its subclasses (i.e. GenericAppendOnlyBuffer uses PlatformMemory and this class
103+
/// does the same. They MUST be kept in sync.)
97104
class ScopedBuffer
98105
{
99106
public:
@@ -165,6 +172,10 @@ class ListBuilder : public detail::GenericAppendOnlyBuffer
165172
///
166173
/// Automatically attempts to allocate sufficient space to fulfill the element
167174
/// requirements.
175+
///
176+
/// `span` MUST NOT point inside "own" buffer (and generally will not
177+
/// as this class does not expose buffer access except by releasing ownership
178+
/// via `Take`)
168179
CHIP_ERROR AppendElements(SpanType span) { return AppendElementArrayRaw(span.data(), span.size()); }
169180

170181
/// Append a single element.

src/app/data-model-provider/ProviderMetadataTree.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ class ProviderMetadataTree
8383
virtual void Temporary_ReportAttributeChanged(const AttributePathParams & path) = 0;
8484

8585
// "convenience" functions that just return the data and ignore the error
86-
// This returns the builder as-is even after the error (e.g. not found would return empty data)
86+
// This returns the `ListBuilder<..>::TakeBuffer` from their equivalent fuctions as-is,
87+
// even after an error (e.g. not found would return empty data).
88+
//
89+
// Usage of these indicates no error handling (not even logging) and code should
90+
// consider handling errors instead.
8791
ReadOnlyBuffer<EndpointEntry> EndpointsIgnoreError();
8892
ReadOnlyBuffer<ServerClusterEntry> ServerClustersIgnoreError(EndpointId endpointId);
8993
ReadOnlyBuffer<AttributeEntry> AttributesIgnoreError(const ConcreteClusterPath & path);

0 commit comments

Comments
 (0)