A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Revision:
21:f9c433d8e6ca
Parent:
19:000ab4e6a2c1
Child:
22:524bded3f7a8
--- a/amqp_management.c	Fri Mar 10 11:47:49 2017 -0800
+++ b/amqp_management.c	Fri Mar 24 16:35:45 2017 -0700
@@ -6,9 +6,9 @@
 #include <stdbool.h>
 #include <string.h>
 #include "azure_c_shared_utility/optimize_size.h"
+#include "azure_c_shared_utility/gballoc.h"
 #include "azure_uamqp_c/amqp_management.h"
 #include "azure_uamqp_c/link.h"
-#include "azure_uamqp_c/amqpalloc.h"
 #include "azure_uamqp_c/message_sender.h"
 #include "azure_uamqp_c/message_receiver.h"
 #include "azure_uamqp_c/messaging.h"
@@ -61,7 +61,7 @@
 static void remove_operation_message_by_index(AMQP_MANAGEMENT_INSTANCE* amqp_management_instance, size_t index)
 {
     message_destroy(amqp_management_instance->operation_messages[index]->message);
-    amqpalloc_free(amqp_management_instance->operation_messages[index]);
+    free(amqp_management_instance->operation_messages[index]);
 
     if (amqp_management_instance->operation_message_count - index > 1)
     {
@@ -70,12 +70,12 @@
 
     if (amqp_management_instance->operation_message_count == 1)
     {
-        amqpalloc_free(amqp_management_instance->operation_messages);
+        free(amqp_management_instance->operation_messages);
         amqp_management_instance->operation_messages = NULL;
     }
     else
     {
-        OPERATION_MESSAGE_INSTANCE** new_operation_messages = (OPERATION_MESSAGE_INSTANCE**)amqpalloc_realloc(amqp_management_instance->operation_messages, sizeof(OPERATION_MESSAGE_INSTANCE*) * (amqp_management_instance->operation_message_count - 1));
+        OPERATION_MESSAGE_INSTANCE** new_operation_messages = (OPERATION_MESSAGE_INSTANCE**)realloc(amqp_management_instance->operation_messages, sizeof(OPERATION_MESSAGE_INSTANCE*) * (amqp_management_instance->operation_message_count - 1));
         if (new_operation_messages != NULL)
         {
             amqp_management_instance->operation_messages = new_operation_messages;
@@ -326,27 +326,39 @@
     }
     else
     {
-        AMQP_VALUE message_id = amqpvalue_create_message_id_ulong(next_message_id);
-        if (message_id == NULL)
+        if (properties == NULL)
+        {
+            properties = properties_create();
+        }
+
+        if (properties == NULL)
         {
             result = __FAILURE__;
         }
         else
         {
-            if (properties_set_message_id(properties, message_id) != 0)
+            AMQP_VALUE message_id = amqpvalue_create_message_id_ulong(next_message_id);
+            if (message_id == NULL)
+            {
+                result = __FAILURE__;
+            }
+            else
+            {
+                if (properties_set_message_id(properties, message_id) != 0)
+                {
+                    result = __FAILURE__;
+                }
+
+                amqpvalue_destroy(message_id);
+            }
+
+            if (message_set_properties(message, properties) != 0)
             {
                 result = __FAILURE__;
             }
 
-            amqpvalue_destroy(message_id);
+            properties_destroy(properties);
         }
-
-        if (message_set_properties(message, properties) != 0)
-        {
-            result = __FAILURE__;
-        }
-
-        properties_destroy(properties);
     }
 
     return result;
@@ -398,7 +410,7 @@
     }
     else
     {
-        result = (AMQP_MANAGEMENT_INSTANCE*)amqpalloc_malloc(sizeof(AMQP_MANAGEMENT_INSTANCE));
+        result = (AMQP_MANAGEMENT_INSTANCE*)malloc(sizeof(AMQP_MANAGEMENT_INSTANCE));
         if (result != NULL)
         {
             result->session = session;
@@ -408,11 +420,12 @@
             result->operation_messages = NULL;
             result->on_amqp_management_state_changed = on_amqp_management_state_changed;
             result->callback_context = callback_context;
+            result->amqp_management_state = AMQP_MANAGEMENT_STATE_IDLE;
 
             AMQP_VALUE source = messaging_create_source(management_node);
             if (source == NULL)
             {
-                amqpalloc_free(result);
+                free(result);
                 result = NULL;
             }
             else
@@ -420,14 +433,14 @@
                 AMQP_VALUE target = messaging_create_target(management_node);
                 if (target == NULL)
                 {
-                    amqpalloc_free(result);
+                    free(result);
                     result = NULL;
                 }
                 else
                 {
                     static const char* sender_suffix = "-sender";
 
-                    char* sender_link_name = (char*)amqpalloc_malloc(strlen(management_node) + strlen(sender_suffix) + 1);
+                    char* sender_link_name = (char*)malloc(strlen(management_node) + strlen(sender_suffix) + 1);
                     if (sender_link_name == NULL)
                     {
                         result = NULL;
@@ -439,7 +452,7 @@
                         (void)strcpy(sender_link_name, management_node);
                         (void)strcat(sender_link_name, sender_suffix);
 
-                        char* receiver_link_name = (char*)amqpalloc_malloc(strlen(management_node) + strlen(receiver_suffix) + 1);
+                        char* receiver_link_name = (char*)malloc(strlen(management_node) + strlen(receiver_suffix) + 1);
                         if (receiver_link_name == NULL)
                         {
                             result = NULL;
@@ -452,7 +465,7 @@
                             result->sender_link = link_create(session, "cbs-sender", role_sender, source, target);
                             if (result->sender_link == NULL)
                             {
-                                amqpalloc_free(result);
+                                free(result);
                                 result = NULL;
                             }
                             else
@@ -461,7 +474,7 @@
                                 if (result->receiver_link == NULL)
                                 {
                                     link_destroy(result->sender_link);
-                                    amqpalloc_free(result);
+                                    free(result);
                                     result = NULL;
                                 }
                                 else
@@ -471,7 +484,7 @@
                                     {
                                         link_destroy(result->sender_link);
                                         link_destroy(result->receiver_link);
-                                        amqpalloc_free(result);
+                                        free(result);
                                         result = NULL;
                                     }
                                     else
@@ -481,7 +494,7 @@
                                         {
                                             link_destroy(result->sender_link);
                                             link_destroy(result->receiver_link);
-                                            amqpalloc_free(result);
+                                            free(result);
                                             result = NULL;
                                         }
                                         else
@@ -492,7 +505,7 @@
                                                 messagesender_destroy(result->message_sender);
                                                 link_destroy(result->sender_link);
                                                 link_destroy(result->receiver_link);
-                                                amqpalloc_free(result);
+                                                free(result);
                                                 result = NULL;
                                             }
                                             else
@@ -504,10 +517,10 @@
                                 }
                             }
 
-                            amqpalloc_free(receiver_link_name);
+                            free(receiver_link_name);
                         }
 
-                        amqpalloc_free(sender_link_name);
+                        free(sender_link_name);
                     }
 
                     amqpvalue_destroy(target);
@@ -533,17 +546,17 @@
             for (i = 0; i < amqp_management->operation_message_count; i++)
             {
                 message_destroy(amqp_management->operation_messages[i]->message);
-                amqpalloc_free(amqp_management->operation_messages[i]);
+                free(amqp_management->operation_messages[i]);
             }
 
-            amqpalloc_free(amqp_management->operation_messages);
+            free(amqp_management->operation_messages);
         }
 
         link_destroy(amqp_management->sender_link);
         link_destroy(amqp_management->receiver_link);
         messagesender_destroy(amqp_management->message_sender);
         messagereceiver_destroy(amqp_management->message_receiver);
-        amqpalloc_free(amqp_management);
+        free(amqp_management);
     }
 }
 
@@ -635,7 +648,7 @@
                 }
                 else
                 {
-                    OPERATION_MESSAGE_INSTANCE* pending_operation_message = amqpalloc_malloc(sizeof(OPERATION_MESSAGE_INSTANCE));
+                    OPERATION_MESSAGE_INSTANCE* pending_operation_message = malloc(sizeof(OPERATION_MESSAGE_INSTANCE));
                     if (pending_operation_message == NULL)
                     {
                         result = __FAILURE__;
@@ -650,11 +663,11 @@
 
                         amqp_management->next_message_id++;
 
-                        OPERATION_MESSAGE_INSTANCE** new_operation_messages = amqpalloc_realloc(amqp_management->operation_messages, (amqp_management->operation_message_count + 1) * sizeof(OPERATION_MESSAGE_INSTANCE*));
+                        OPERATION_MESSAGE_INSTANCE** new_operation_messages = realloc(amqp_management->operation_messages, (amqp_management->operation_message_count + 1) * sizeof(OPERATION_MESSAGE_INSTANCE*));
                         if (new_operation_messages == NULL)
                         {
                             message_destroy(message);
-                            amqpalloc_free(pending_operation_message);
+                            free(pending_operation_message);
                             result = __FAILURE__;
                         }
                         else
@@ -667,7 +680,7 @@
                             {
                                 if (on_operation_complete != NULL)
                                 {
-                                    on_operation_complete(context, OPERATION_RESULT_CBS_ERROR, 0, NULL);
+                                    on_operation_complete(context, OPERATION_RESULT_ERROR, 0, NULL);
                                 }
 
                                 result = __FAILURE__;