optimization to use the existing buffer in blockwise-transfer.
authorhyuna0213.jo <hyuna0213.jo@samsung.com>
Mon, 14 Sep 2015 09:32:49 +0000 (18:32 +0900)
committerPatrick Lankswert <patrick.lankswert@intel.com>
Fri, 18 Sep 2015 15:58:23 +0000 (15:58 +0000)
if we have to update payload data, we always allocate new memory.
so we modified the code to use the existing memory if one exists.
This patch addresses the first part of IOT-690.

Change-Id: I04ca551273caf02f783c62a76f11f7e07409d8dd
Signed-off-by: hyuna0213.jo <hyuna0213.jo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2503
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Patrick Lankswert <patrick.lankswert@intel.com>
resource/csdk/connectivity/src/cablockwisetransfer.c

index f0b8af3b8a4601f40986b4e42f11aa3d9010ebe1..6b3633b6e57591cce3a16fb3480c62894985eb88 100644 (file)
@@ -2054,44 +2054,39 @@ CAResult_t CAUpdatePayloadToCAData(CAData_t *data, const CAPayload_t payload,
     VERIFY_NON_NULL(data, TAG, "data is NULL");
     VERIFY_NON_NULL(payload, TAG, "payload is NULL");
 
-    if (data->requestInfo)
+    CAPayload_t newPayload = NULL;
+    switch (data->dataType)
     {
-        // allocate payload field
-        if (data->requestInfo->info.payload)
-        {
-            char *temp = (char *) OICCalloc(payloadLen, sizeof(char));
-            if (!temp)
+        case CA_REQUEST_DATA:
+            // allocate payload field
+            newPayload = OICRealloc(data->requestInfo->info.payload, payloadLen);
+            if (!newPayload)
             {
                 OIC_LOG(ERROR, TAG, "out of memory");
                 return CA_STATUS_FAILED;
             }
-            memcpy(temp, payload, payloadLen);
-
-            // save the full payload
-            OICFree(data->requestInfo->info.payload);
-            data->requestInfo->info.payload = (CAPayload_t) temp;
-        }
-        data->requestInfo->info.payloadSize = payloadLen;
-    }
+            data->requestInfo->info.payload = newPayload;
+            memcpy(data->requestInfo->info.payload, payload, payloadLen);
+            data->requestInfo->info.payloadSize = payloadLen;
+            break;
 
-    if (data->responseInfo)
-    {
-        // allocate payload field
-        if (data->responseInfo->info.payload)
-        {
-            char *temp = (char *) OICCalloc(payloadLen, sizeof(char));
-            if (!temp)
+        case CA_RESPONSE_DATA:
+            // allocate payload field
+            newPayload = OICRealloc(data->responseInfo->info.payload, payloadLen);
+            if (!newPayload)
             {
                 OIC_LOG(ERROR, TAG, "out of memory");
                 return CA_STATUS_FAILED;
             }
-            memcpy(temp, payload, payloadLen);
+            data->responseInfo->info.payload = newPayload;
+            memcpy(data->responseInfo->info.payload, payload, payloadLen);
+            data->responseInfo->info.payloadSize = payloadLen;
+            break;
 
-            // save the full payload
-            OICFree(data->responseInfo->info.payload);
-            data->responseInfo->info.payload = (CAPayload_t) temp;
-        }
-        data->responseInfo->info.payloadSize = payloadLen;
+        default:
+            // does not occur case
+            OIC_LOG(ERROR, TAG, "not supported data type");
+            return CA_NOT_SUPPORTED;
     }
 
     OIC_LOG(DEBUG, TAG, "OUT-UpdatePayload");