A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Thu Oct 04 09:16:13 2018 -0700
Revision:
47:365a93fdb5bb
Parent:
34:6be9c2058664
1.2.10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 34:6be9c2058664 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 34:6be9c2058664 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 34:6be9c2058664 3
AzureIoTClient 34:6be9c2058664 4 #include <stdlib.h>
AzureIoTClient 34:6be9c2058664 5 #include <stdint.h>
AzureIoTClient 34:6be9c2058664 6 #include <stddef.h>
AzureIoTClient 34:6be9c2058664 7 #include <string.h>
AzureIoTClient 34:6be9c2058664 8 #include "azure_c_shared_utility/optimize_size.h"
AzureIoTClient 34:6be9c2058664 9 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 34:6be9c2058664 10 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 34:6be9c2058664 11 #include "azure_uamqp_c/async_operation.h"
AzureIoTClient 34:6be9c2058664 12
AzureIoTClient 34:6be9c2058664 13 typedef struct ASYNC_OPERATION_INSTANCE_TAG
AzureIoTClient 34:6be9c2058664 14 {
AzureIoTClient 34:6be9c2058664 15 ASYNC_OPERATION_CANCEL_HANDLER_FUNC async_operation_cancel_handler;
AzureIoTClient 34:6be9c2058664 16 } ASYNC_OPERATION_INSTANCE;
AzureIoTClient 34:6be9c2058664 17
AzureIoTClient 34:6be9c2058664 18 ASYNC_OPERATION_HANDLE async_operation_create(ASYNC_OPERATION_CANCEL_HANDLER_FUNC async_operation_cancel_handler, size_t context_size)
AzureIoTClient 34:6be9c2058664 19 {
AzureIoTClient 34:6be9c2058664 20 ASYNC_OPERATION_INSTANCE* async_operation;
AzureIoTClient 34:6be9c2058664 21
AzureIoTClient 34:6be9c2058664 22 if (async_operation_cancel_handler == NULL)
AzureIoTClient 34:6be9c2058664 23 {
AzureIoTClient 34:6be9c2058664 24 /* Codes_SRS_ASYNC_OPERATION_01_002: [ If `async_operation_cancel_handler` is NULL, `async_operation_create` shall fail and return NULL.]*/
AzureIoTClient 34:6be9c2058664 25 LogError("Cannot allocate memory for async operation");
AzureIoTClient 34:6be9c2058664 26 async_operation = NULL;
AzureIoTClient 34:6be9c2058664 27 }
AzureIoTClient 34:6be9c2058664 28 else if (context_size < sizeof(ASYNC_OPERATION_INSTANCE))
AzureIoTClient 34:6be9c2058664 29 {
AzureIoTClient 34:6be9c2058664 30 /* Codes_SRS_ASYNC_OPERATION_01_003: [ If `context_size` is less than the size of the `async_operation_cancel_handler` argument, `async_operation_create` shall fail and return NULL.]*/
AzureIoTClient 34:6be9c2058664 31 LogError("Context size too small");
AzureIoTClient 34:6be9c2058664 32 async_operation = NULL;
AzureIoTClient 34:6be9c2058664 33 }
AzureIoTClient 34:6be9c2058664 34 else
AzureIoTClient 34:6be9c2058664 35 {
AzureIoTClient 34:6be9c2058664 36 async_operation = (ASYNC_OPERATION_INSTANCE*)malloc(context_size);
AzureIoTClient 34:6be9c2058664 37 if (async_operation == NULL)
AzureIoTClient 34:6be9c2058664 38 {
AzureIoTClient 34:6be9c2058664 39 /* Codes_SRS_ASYNC_OPERATION_01_004: [ If allocating memory for the new asynchronous operation instance fails, `async_operation_create` shall fail and return NULL.]*/
AzureIoTClient 34:6be9c2058664 40 LogError("Cannot allocate memory for async operation");
AzureIoTClient 34:6be9c2058664 41 }
AzureIoTClient 34:6be9c2058664 42 else
AzureIoTClient 34:6be9c2058664 43 {
AzureIoTClient 34:6be9c2058664 44 /* Codes_SRS_ASYNC_OPERATION_01_001: [ `async_operation_create` shall return a non-NULL handle to a newly created asynchronous operation instance.]*/
AzureIoTClient 34:6be9c2058664 45 async_operation->async_operation_cancel_handler = async_operation_cancel_handler;
AzureIoTClient 34:6be9c2058664 46 }
AzureIoTClient 34:6be9c2058664 47 }
AzureIoTClient 34:6be9c2058664 48
AzureIoTClient 34:6be9c2058664 49 return async_operation;
AzureIoTClient 34:6be9c2058664 50 }
AzureIoTClient 34:6be9c2058664 51
AzureIoTClient 34:6be9c2058664 52 void async_operation_destroy(ASYNC_OPERATION_HANDLE async_operation)
AzureIoTClient 34:6be9c2058664 53 {
AzureIoTClient 34:6be9c2058664 54 if (async_operation == NULL)
AzureIoTClient 34:6be9c2058664 55 {
AzureIoTClient 34:6be9c2058664 56 /* Codes_SRS_ASYNC_OPERATION_01_006: [ If `async_operation` is NULL, `async_operation_destroy` shall do nothing.]*/
AzureIoTClient 34:6be9c2058664 57 LogError("NULL async_operation");
AzureIoTClient 34:6be9c2058664 58 }
AzureIoTClient 34:6be9c2058664 59 else
AzureIoTClient 34:6be9c2058664 60 {
AzureIoTClient 34:6be9c2058664 61 /* Codes_SRS_ASYNC_OPERATION_01_005: [ `async_operation_destroy` shall free all recources associated with the asyncronous operation instance.]*/
AzureIoTClient 34:6be9c2058664 62 free(async_operation);
AzureIoTClient 34:6be9c2058664 63 }
AzureIoTClient 34:6be9c2058664 64 }
AzureIoTClient 34:6be9c2058664 65
AzureIoTClient 34:6be9c2058664 66 int async_operation_cancel(ASYNC_OPERATION_HANDLE async_operation)
AzureIoTClient 34:6be9c2058664 67 {
AzureIoTClient 34:6be9c2058664 68 int result;
AzureIoTClient 34:6be9c2058664 69
AzureIoTClient 34:6be9c2058664 70 if (async_operation == NULL)
AzureIoTClient 34:6be9c2058664 71 {
AzureIoTClient 34:6be9c2058664 72 LogError("NULL async_operation");
AzureIoTClient 34:6be9c2058664 73 result = __FAILURE__;
AzureIoTClient 34:6be9c2058664 74 }
AzureIoTClient 34:6be9c2058664 75 else
AzureIoTClient 34:6be9c2058664 76 {
AzureIoTClient 34:6be9c2058664 77 /* Codes_SRS_ASYNC_OPERATION_01_007: [ `async_operation_cancel` shall cancel the operation by calling the cancel handler function passed to `async_operation_create`.]*/
AzureIoTClient 34:6be9c2058664 78 async_operation->async_operation_cancel_handler(async_operation);
AzureIoTClient 34:6be9c2058664 79
AzureIoTClient 34:6be9c2058664 80 /* Codes_SRS_ASYNC_OPERATION_01_008: [ On success `async_operation_cancel` shall return 0.]*/
AzureIoTClient 34:6be9c2058664 81 result = 0;
AzureIoTClient 34:6be9c2058664 82 }
AzureIoTClient 34:6be9c2058664 83
AzureIoTClient 34:6be9c2058664 84 return result;
AzureIoTClient 34:6be9c2058664 85 }