A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Revision:
40:f0ceafa8d570
Parent:
28:add19eb7defa
Child:
46:01f7ca900e07
--- a/message.c	Thu Feb 15 11:36:00 2018 -0800
+++ b/message.c	Mon Mar 05 17:41:28 2018 -0800
@@ -7,6 +7,7 @@
 #include "azure_c_shared_utility/optimize_size.h"
 #include "azure_c_shared_utility/gballoc.h"
 #include "azure_c_shared_utility/xlogging.h"
+#include "azure_uamqp_c/amqp_definitions.h"
 #include "azure_uamqp_c/message.h"
 #include "azure_uamqp_c/amqpvalue.h"
 
@@ -1027,22 +1028,34 @@
             {
                 message->body_amqp_data_items = new_body_amqp_data_items;
 
-                message->body_amqp_data_items[message->body_amqp_data_count].body_data_section_bytes = (unsigned char*)malloc(amqp_data.length);
-                if (message->body_amqp_data_items[message->body_amqp_data_count].body_data_section_bytes == NULL)
+                if (amqp_data.length == 0)
                 {
-                    /* Codes_SRS_MESSAGE_01_153: [ If allocating memory to store the added AMQP data fails, `message_add_body_amqp_data` shall fail and return a non-zero value. ]*/
-                    LogError("Cannot allocate memory for body AMQP data to be added");
-                    result = __FAILURE__;
-                }
-                else
-                {
-                    message->body_amqp_data_items[message->body_amqp_data_count].body_data_section_length = amqp_data.length;
-                    (void)memcpy(message->body_amqp_data_items[message->body_amqp_data_count].body_data_section_bytes, amqp_data.bytes, amqp_data.length);
+                    message->body_amqp_data_items[message->body_amqp_data_count].body_data_section_bytes = NULL;
+                    message->body_amqp_data_items[message->body_amqp_data_count].body_data_section_length = 0;
                     message->body_amqp_data_count++;
 
                     /* Codes_SRS_MESSAGE_01_087: [ On success it shall return 0. ]*/
                     result = 0;
                 }
+                else
+                {
+                    message->body_amqp_data_items[message->body_amqp_data_count].body_data_section_bytes = (unsigned char*)malloc(amqp_data.length);
+                    if (message->body_amqp_data_items[message->body_amqp_data_count].body_data_section_bytes == NULL)
+                    {
+                        /* Codes_SRS_MESSAGE_01_153: [ If allocating memory to store the added AMQP data fails, `message_add_body_amqp_data` shall fail and return a non-zero value. ]*/
+                        LogError("Cannot allocate memory for body AMQP data to be added");
+                        result = __FAILURE__;
+                    }
+                    else
+                    {
+                        message->body_amqp_data_items[message->body_amqp_data_count].body_data_section_length = amqp_data.length;
+                        (void)memcpy(message->body_amqp_data_items[message->body_amqp_data_count].body_data_section_bytes, amqp_data.bytes, amqp_data.length);
+                        message->body_amqp_data_count++;
+
+                        /* Codes_SRS_MESSAGE_01_087: [ On success it shall return 0. ]*/
+                        result = 0;
+                    }
+                }
             }
         }
     }