From 04394f84bc0aa4af34682014ffd549b687144f5d Mon Sep 17 00:00:00 2001 From: lpbeliveau-silabs Date: Mon, 5 Jun 2023 15:26:38 -0400 Subject: [PATCH] Replaced Platform memory alloc with ScopedMemoryBuffer and removed reallocation --- .../clusters/scenes-server/SceneTableImpl.cpp | 31 ++++++------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/src/app/clusters/scenes-server/SceneTableImpl.cpp b/src/app/clusters/scenes-server/SceneTableImpl.cpp index 548711371711d2..9151cda2f22207 100644 --- a/src/app/clusters/scenes-server/SceneTableImpl.cpp +++ b/src/app/clusters/scenes-server/SceneTableImpl.cpp @@ -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(chip::Platform::MemoryCalloc(sizeof(clusterId), clusterCount)); - VerifyOrReturnError(nullptr != cBuffer, CHIP_ERROR_NO_MEMORY); + chip::Platform::ScopedMemoryBuffer 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 cSpan(cBuffer, clusterCount); + Span cSpan(cBuffer.Get(), clusterCount); for (clusterId cluster : cSpan) { ExtensionFieldSet EFS; @@ -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(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;