Skip to content

Commit

Permalink
Replaced Platform memory alloc with ScopedMemoryBuffer and removed re…
Browse files Browse the repository at this point in the history
…allocation
  • Loading branch information
lpbeliveau-silabs committed Jun 5, 2023
1 parent f7dbf3f commit 04394f8
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions src/app/clusters/scenes-server/SceneTableImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,17 +737,15 @@ CHIP_ERROR DefaultSceneTableImpl::SceneSaveEFS(SceneTableEntry & scene)
{
if (!HandlerListEmpty())
{
// TODO : Once zap supports the scenable quality, implement a GetSceneableClusterCountFromEndpointType function to avoid
// over-allocation
uint8_t clusterCount = GetClusterCountFromEndpoint();
clusterId * cBuffer = static_cast<clusterId *>(chip::Platform::MemoryCalloc(sizeof(clusterId), clusterCount));
VerifyOrReturnError(nullptr != cBuffer, CHIP_ERROR_NO_MEMORY);
chip::Platform::ScopedMemoryBuffer<clusterId> cBuffer;
cBuffer.Alloc(clusterCount);
VerifyOrReturnError(nullptr != cBuffer.Get(), CHIP_ERROR_NO_MEMORY);
clusterCount = GetClustersFromEndpoint(cBuffer.Get(), clusterCount);

clusterCount = GetClustersFromEndpoint(cBuffer, clusterCount);
chip::Platform::MemoryRealloc(cBuffer, clusterCount);

// If there were no supported clusters on the endpoint, returns no error
VerifyOrReturnError(nullptr != cBuffer, CHIP_NO_ERROR);

Span<clusterId> cSpan(cBuffer, clusterCount);
Span<clusterId> cSpan(cBuffer.Get(), clusterCount);
for (clusterId cluster : cSpan)
{
ExtensionFieldSet EFS;
Expand All @@ -758,24 +756,13 @@ CHIP_ERROR DefaultSceneTableImpl::SceneSaveEFS(SceneTableEntry & scene)
{
if (handler.SupportsCluster(mEndpointId, cluster))
{
CHIP_ERROR err = handler.SerializeSave(mEndpointId, EFS.mID, EFSSpan);
if (CHIP_NO_ERROR != err)
{
chip::Platform::MemoryFree(cBuffer);
return err;
}
ReturnErrorOnFailure(handler.SerializeSave(mEndpointId, EFS.mID, EFSSpan));
EFS.mUsedBytes = static_cast<uint8_t>(EFSSpan.size());
err = scene.mStorageData.mExtensionFieldSets.InsertFieldSet(EFS);
if (CHIP_NO_ERROR != err)
{
chip::Platform::MemoryFree(cBuffer);
return err;
}
ReturnErrorOnFailure(scene.mStorageData.mExtensionFieldSets.InsertFieldSet(EFS));
break;
}
}
}
chip::Platform::MemoryFree(cBuffer);
}

return CHIP_NO_ERROR;
Expand Down

0 comments on commit 04394f8

Please sign in to comment.