Simplify btif multi adv data handling

Use static fixed size arrays to transport advertisement data instead of
dynamically allocated ones. This simplifies memory allocation and
cleanup handling.

Bug: 28485365
Change-Id: I3a2642e3b11a272eeec50fd959e10820ea3de74f
diff --git a/btif/include/btif_gatt_multi_adv_util.h b/btif/include/btif_gatt_multi_adv_util.h
index b4ed6c7..7d6cba7 100644
--- a/btif/include/btif_gatt_multi_adv_util.h
+++ b/btif/include/btif_gatt_multi_adv_util.h
@@ -46,11 +46,11 @@
     int max_interval;
     int appearance;
     uint16_t manufacturer_len;
-    uint8_t* p_manufacturer_data;
+    uint8_t p_manufacturer_data[MAX_SIZE_MANUFACTURER_DATA];
     uint16_t service_data_len;
-    uint8_t* p_service_data;
+    uint8_t p_service_data[MAX_SIZE_SERVICE_DATA];
     uint16_t service_uuid_len;
-    uint8_t* p_service_uuid;
+    uint8_t p_service_uuid[MAX_SIZE_SERVICE_DATA];
 } btif_adv_data_t;
 
 
diff --git a/btif/src/btif_gatt_client.cc b/btif/src/btif_gatt_client.cc
index fb8a548..dc3bfb7 100644
--- a/btif/src/btif_gatt_client.cc
+++ b/btif/src/btif_gatt_client.cc
@@ -1083,23 +1083,6 @@
                           (char*) &btif_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
 }
 
-static void btgattc_free_event_data(UINT16 event, char *event_data)
-{
-    switch (event)
-    {
-        case BTIF_GATTC_ADV_INSTANCE_SET_DATA:
-        case BTIF_GATTC_SET_ADV_DATA:
-        {
-            btif_adv_data_t *adv_data = (btif_adv_data_t *)event_data;
-            btif_gattc_adv_data_cleanup(adv_data);
-            break;
-        }
-
-        default:
-            break;
-    }
-}
-
 static void btgattc_handle_event(uint16_t event, char* p_param)
 {
     tBTA_GATT_STATUS           status;
@@ -1598,8 +1581,6 @@
             LOG_ERROR(LOG_TAG, "%s: Unknown event (%d)!", __FUNCTION__, event);
             break;
     }
-
-    btgattc_free_event_data(event, p_param);
 }
 
 /*******************************************************************************
@@ -1666,44 +1647,6 @@
                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
 }
 
-static void btif_gattc_deep_copy(UINT16 event, char *p_dest, char *p_src)
-{
-    switch (event)
-    {
-        case BTIF_GATTC_ADV_INSTANCE_SET_DATA:
-        case BTIF_GATTC_SET_ADV_DATA:
-        {
-            const btif_adv_data_t *src = (btif_adv_data_t*) p_src;
-            btif_adv_data_t *dst = (btif_adv_data_t*) p_dest;
-            maybe_non_aligned_memcpy(dst, src, sizeof(*src));
-
-            if (src->p_manufacturer_data)
-            {
-                dst->p_manufacturer_data = (UINT8 *)osi_malloc(src->manufacturer_len);
-                memcpy(dst->p_manufacturer_data, src->p_manufacturer_data,
-                       src->manufacturer_len);
-            }
-
-            if (src->p_service_data)
-            {
-                dst->p_service_data = (UINT8 *)osi_malloc(src->service_data_len);
-                memcpy(dst->p_service_data, src->p_service_data, src->service_data_len);
-            }
-
-            if (src->p_service_uuid)
-            {
-                dst->p_service_uuid = (UINT8 *)osi_malloc(src->service_uuid_len);
-                memcpy(dst->p_service_uuid, src->p_service_uuid, src->service_uuid_len);
-            }
-            break;
-        }
-
-        default:
-            ASSERTC(false, "Unhandled deep copy", event);
-            break;
-    }
-}
-
 static bt_status_t btif_gattc_set_adv_data(int client_if, bool set_scan_rsp, bool include_name,
                 bool include_txpower, int min_interval, int max_interval, int appearance,
                 uint16_t manufacturer_len, char* manufacturer_data,
@@ -1719,8 +1662,7 @@
         &adv_data);
 
     bt_status_t status = btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SET_ADV_DATA,
-                       (char*) &adv_data, sizeof(adv_data), btif_gattc_deep_copy);
-    btif_gattc_adv_data_cleanup(&adv_data);
+                       (char*) &adv_data, sizeof(adv_data), NULL);
     return status;
 }
 
@@ -2056,9 +1998,7 @@
 
     bt_status_t status = btif_transfer_context(
         btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_SET_DATA,
-        (char *)&multi_adv_data_inst, sizeof(multi_adv_data_inst),
-        btif_gattc_deep_copy);
-    btif_gattc_adv_data_cleanup(&multi_adv_data_inst);
+        (char *)&multi_adv_data_inst, sizeof(multi_adv_data_inst), NULL);
     return status;
 }
 
diff --git a/btif/src/btif_gatt_multi_adv_util.cc b/btif/src/btif_gatt_multi_adv_util.cc
index d60292d..134be38 100644
--- a/btif/src/btif_gatt_multi_adv_util.cc
+++ b/btif/src/btif_gatt_multi_adv_util.cc
@@ -222,36 +222,35 @@
     p_multi_adv_inst->min_interval = min_interval;
     p_multi_adv_inst->max_interval = max_interval;
     p_multi_adv_inst->appearance = appearance;
-    p_multi_adv_inst->manufacturer_len = manufacturer_len;
 
     if (manufacturer_len > 0)
     {
-        p_multi_adv_inst->p_manufacturer_data = (uint8_t*)osi_malloc(manufacturer_len);
+        if (manufacturer_len > MAX_SIZE_MANUFACTURER_DATA)
+            manufacturer_len = MAX_SIZE_MANUFACTURER_DATA;
+
+        p_multi_adv_inst->manufacturer_len = manufacturer_len;
         memcpy(p_multi_adv_inst->p_manufacturer_data, manufacturer_data, manufacturer_len);
     }
 
-    p_multi_adv_inst->service_data_len = service_data_len;
     if (service_data_len > 0)
     {
-        p_multi_adv_inst->p_service_data = (uint8_t*)osi_malloc(service_data_len);
+        if (service_data_len > MAX_SIZE_PROPRIETARY_ELEMENT)
+            service_data_len = MAX_SIZE_PROPRIETARY_ELEMENT;
+
+        p_multi_adv_inst->service_data_len = service_data_len;
         memcpy(p_multi_adv_inst->p_service_data, service_data, service_data_len);
     }
 
-    p_multi_adv_inst->service_uuid_len = service_uuid_len;
     if (service_uuid_len > 0)
     {
-        p_multi_adv_inst->p_service_uuid = (uint8_t*)osi_malloc(service_uuid_len);
+        if (service_uuid_len > MAX_SIZE_SERVICE_DATA)
+            service_uuid_len = MAX_SIZE_SERVICE_DATA;
+
+        p_multi_adv_inst->service_uuid_len = service_uuid_len;
         memcpy(p_multi_adv_inst->p_service_uuid, service_uuid, service_uuid_len);
     }
 }
 
-void btif_gattc_adv_data_cleanup(btif_adv_data_t* adv)
-{
-    osi_free_and_reset((void **)&adv->p_service_data);
-    osi_free_and_reset((void **)&adv->p_service_uuid);
-    osi_free_and_reset((void **)&adv->p_manufacturer_data);
-}
-
 BOOLEAN btif_gattc_copy_datacb(int cbindex, const btif_adv_data_t *p_adv_data,
                                BOOLEAN bInstData) {
     btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
@@ -320,7 +319,6 @@
     }
 
     if (p_adv_data->manufacturer_len > 0 &&
-        p_adv_data->p_manufacturer_data != NULL &&
         p_adv_data->manufacturer_len < MAX_SIZE_MANUFACTURER_DATA)
     {
       p_multi_adv_data_cb->inst_cb[cbindex].mask |= BTM_BLE_AD_BIT_MANU;
@@ -331,7 +329,6 @@
     }
 
     if (p_adv_data->service_data_len > 0 &&
-        p_adv_data->p_service_data != NULL &&
         p_adv_data->service_data_len < MAX_SIZE_PROPRIETARY_ELEMENT)
     {
       BTIF_TRACE_DEBUG("%s - In service_data", __func__);
@@ -347,7 +344,7 @@
       p_multi_adv_data_cb->inst_cb[cbindex].mask |= BTM_BLE_AD_BIT_PROPRIETARY;
     }
 
-    if (p_adv_data->service_uuid_len && p_adv_data->p_service_uuid)
+    if (p_adv_data->service_uuid_len)
     {
         UINT16 *p_uuid_out16 = NULL;
         UINT32 *p_uuid_out32 = NULL;