Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Thu Oct 04 09:17:16 2018 -0700
Revision:
49:6bb8b9a66642
Parent:
48:81866008bba4
1.2.10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:fa2de1b79154 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:fa2de1b79154 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:fa2de1b79154 3
AzureIoTClient 45:1119d0f2c4d8 4
Azure.IoT Build 0:fa2de1b79154 5 #include <stdlib.h>
AzureIoTClient 19:2e0811512ceb 6 #include <stdint.h>
AzureIoTClient 19:2e0811512ceb 7 #include <stddef.h>
Azure.IoT Build 0:fa2de1b79154 8 #include "azure_c_shared_utility/lock.h"
AzureIoTClient 21:b92006c5b9ff 9 #include "azure_c_shared_utility/optimize_size.h"
Azure.IoT Build 6:c55b013dfc2a 10 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 11
AzureIoTClient 45:1119d0f2c4d8 12 #ifndef GB_USE_CUSTOM_HEAP
AzureIoTClient 45:1119d0f2c4d8 13
Azure.IoT Build 0:fa2de1b79154 14 #ifndef SIZE_MAX
Azure.IoT Build 0:fa2de1b79154 15 #define SIZE_MAX ((size_t)~(size_t)0)
Azure.IoT Build 0:fa2de1b79154 16 #endif
Azure.IoT Build 0:fa2de1b79154 17
Azure.IoT Build 0:fa2de1b79154 18 typedef struct ALLOCATION_TAG
Azure.IoT Build 0:fa2de1b79154 19 {
Azure.IoT Build 0:fa2de1b79154 20 size_t size;
Azure.IoT Build 0:fa2de1b79154 21 void* ptr;
Azure.IoT Build 0:fa2de1b79154 22 void* next;
Azure.IoT Build 0:fa2de1b79154 23 } ALLOCATION;
Azure.IoT Build 0:fa2de1b79154 24
Azure.IoT Build 0:fa2de1b79154 25 typedef enum GBALLOC_STATE_TAG
Azure.IoT Build 0:fa2de1b79154 26 {
Azure.IoT Build 0:fa2de1b79154 27 GBALLOC_STATE_INIT,
Azure.IoT Build 0:fa2de1b79154 28 GBALLOC_STATE_NOT_INIT
Azure.IoT Build 0:fa2de1b79154 29 } GBALLOC_STATE;
Azure.IoT Build 0:fa2de1b79154 30
Azure.IoT Build 0:fa2de1b79154 31 static ALLOCATION* head = NULL;
Azure.IoT Build 0:fa2de1b79154 32 static size_t totalSize = 0;
Azure.IoT Build 0:fa2de1b79154 33 static size_t maxSize = 0;
AzureIoTClient 34:0a87c89bdcd0 34 static size_t g_allocations = 0;
Azure.IoT Build 0:fa2de1b79154 35 static GBALLOC_STATE gballocState = GBALLOC_STATE_NOT_INIT;
Azure.IoT Build 0:fa2de1b79154 36
Azure.IoT Build 0:fa2de1b79154 37 static LOCK_HANDLE gballocThreadSafeLock = NULL;
Azure.IoT Build 0:fa2de1b79154 38
Azure.IoT Build 0:fa2de1b79154 39 int gballoc_init(void)
Azure.IoT Build 0:fa2de1b79154 40 {
Azure.IoT Build 0:fa2de1b79154 41 int result;
Azure.IoT Build 0:fa2de1b79154 42
Azure.IoT Build 0:fa2de1b79154 43 if (gballocState != GBALLOC_STATE_NOT_INIT)
Azure.IoT Build 0:fa2de1b79154 44 {
Azure.IoT Build 0:fa2de1b79154 45 /* Codes_SRS_GBALLOC_01_025: [Init after Init shall fail and return a non-zero value.] */
AzureIoTClient 21:b92006c5b9ff 46 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 47 }
Azure.IoT Build 0:fa2de1b79154 48 /* Codes_SRS_GBALLOC_01_026: [gballoc_Init shall create a lock handle that will be used to make the other gballoc APIs thread-safe.] */
Azure.IoT Build 0:fa2de1b79154 49 else if ((gballocThreadSafeLock = Lock_Init()) == NULL)
Azure.IoT Build 0:fa2de1b79154 50 {
Azure.IoT Build 0:fa2de1b79154 51 /* Codes_SRS_GBALLOC_01_027: [If the Lock creation fails, gballoc_init shall return a non-zero value.]*/
AzureIoTClient 21:b92006c5b9ff 52 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 53 }
Azure.IoT Build 0:fa2de1b79154 54 else
Azure.IoT Build 0:fa2de1b79154 55 {
Azure.IoT Build 0:fa2de1b79154 56 gballocState = GBALLOC_STATE_INIT;
Azure.IoT Build 0:fa2de1b79154 57
Azure.IoT Build 0:fa2de1b79154 58 /* Codes_ SRS_GBALLOC_01_002: [Upon initialization the total memory used and maximum total memory used tracked by the module shall be set to 0.] */
Azure.IoT Build 0:fa2de1b79154 59 totalSize = 0;
Azure.IoT Build 0:fa2de1b79154 60 maxSize = 0;
AzureIoTClient 34:0a87c89bdcd0 61 g_allocations = 0;
Azure.IoT Build 0:fa2de1b79154 62
Azure.IoT Build 0:fa2de1b79154 63 /* Codes_SRS_GBALLOC_01_024: [gballoc_init shall initialize the gballoc module and return 0 upon success.] */
Azure.IoT Build 0:fa2de1b79154 64 result = 0;
Azure.IoT Build 0:fa2de1b79154 65 }
Azure.IoT Build 0:fa2de1b79154 66
Azure.IoT Build 0:fa2de1b79154 67 return result;
Azure.IoT Build 0:fa2de1b79154 68 }
Azure.IoT Build 0:fa2de1b79154 69
Azure.IoT Build 0:fa2de1b79154 70 void gballoc_deinit(void)
Azure.IoT Build 0:fa2de1b79154 71 {
Azure.IoT Build 0:fa2de1b79154 72 if (gballocState == GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 73 {
Azure.IoT Build 0:fa2de1b79154 74 /* Codes_SRS_GBALLOC_01_028: [gballoc_deinit shall free all resources allocated by gballoc_init.] */
Azure.IoT Build 0:fa2de1b79154 75 (void)Lock_Deinit(gballocThreadSafeLock);
Azure.IoT Build 0:fa2de1b79154 76 }
Azure.IoT Build 0:fa2de1b79154 77
Azure.IoT Build 0:fa2de1b79154 78 gballocState = GBALLOC_STATE_NOT_INIT;
Azure.IoT Build 0:fa2de1b79154 79 }
Azure.IoT Build 0:fa2de1b79154 80
Azure.IoT Build 0:fa2de1b79154 81 void* gballoc_malloc(size_t size)
Azure.IoT Build 0:fa2de1b79154 82 {
Azure.IoT Build 0:fa2de1b79154 83 void* result;
Azure.IoT Build 0:fa2de1b79154 84
Azure.IoT Build 0:fa2de1b79154 85 if (gballocState != GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 86 {
Azure.IoT Build 0:fa2de1b79154 87 /* Codes_SRS_GBALLOC_01_039: [If gballoc was not initialized gballoc_malloc shall simply call malloc without any memory tracking being performed.] */
Azure.IoT Build 0:fa2de1b79154 88 result = malloc(size);
Azure.IoT Build 0:fa2de1b79154 89 }
Azure.IoT Build 0:fa2de1b79154 90 /* Codes_SRS_GBALLOC_01_030: [gballoc_malloc shall ensure thread safety by using the lock created by gballoc_Init.] */
Azure.IoT Build 0:fa2de1b79154 91 else if (LOCK_OK != Lock(gballocThreadSafeLock))
Azure.IoT Build 0:fa2de1b79154 92 {
Azure.IoT Build 0:fa2de1b79154 93 /* Codes_SRS_GBALLOC_01_048: [If acquiring the lock fails, gballoc_malloc shall return NULL.] */
AzureIoTClient 1:9190c0f4d23a 94 LogError("Failed to get the Lock.");
Azure.IoT Build 0:fa2de1b79154 95 result = NULL;
Azure.IoT Build 0:fa2de1b79154 96 }
Azure.IoT Build 0:fa2de1b79154 97 else
Azure.IoT Build 0:fa2de1b79154 98 {
AzureIoTClient 38:ed9c888e5e12 99 ALLOCATION* allocation = (ALLOCATION*)malloc(sizeof(ALLOCATION));
AzureIoTClient 38:ed9c888e5e12 100 if (allocation == NULL)
Azure.IoT Build 0:fa2de1b79154 101 {
AzureIoTClient 38:ed9c888e5e12 102 result = NULL;
Azure.IoT Build 0:fa2de1b79154 103 }
Azure.IoT Build 0:fa2de1b79154 104 else
Azure.IoT Build 0:fa2de1b79154 105 {
AzureIoTClient 38:ed9c888e5e12 106 /* Codes_SRS_GBALLOC_01_003: [gb_malloc shall call the C99 malloc function and return its result.] */
AzureIoTClient 38:ed9c888e5e12 107 result = malloc(size);
AzureIoTClient 38:ed9c888e5e12 108 if (result == NULL)
AzureIoTClient 38:ed9c888e5e12 109 {
AzureIoTClient 38:ed9c888e5e12 110 /* Codes_SRS_GBALLOC_01_012: [When the underlying malloc call fails, gballoc_malloc shall return NULL and size should not be counted towards total memory used.] */
AzureIoTClient 38:ed9c888e5e12 111 free(allocation);
AzureIoTClient 38:ed9c888e5e12 112 }
AzureIoTClient 38:ed9c888e5e12 113 else
AzureIoTClient 38:ed9c888e5e12 114 {
AzureIoTClient 38:ed9c888e5e12 115 /* Codes_SRS_GBALLOC_01_004: [If the underlying malloc call is successful, gb_malloc shall increment the total memory used with the amount indicated by size.] */
AzureIoTClient 38:ed9c888e5e12 116 allocation->ptr = result;
AzureIoTClient 38:ed9c888e5e12 117 allocation->size = size;
AzureIoTClient 38:ed9c888e5e12 118 allocation->next = head;
AzureIoTClient 38:ed9c888e5e12 119 head = allocation;
Azure.IoT Build 0:fa2de1b79154 120
AzureIoTClient 38:ed9c888e5e12 121 g_allocations++;
AzureIoTClient 38:ed9c888e5e12 122 totalSize += size;
AzureIoTClient 38:ed9c888e5e12 123 /* Codes_SRS_GBALLOC_01_011: [The maximum total memory used shall be the maximum of the total memory used at any point.] */
AzureIoTClient 38:ed9c888e5e12 124 if (maxSize < totalSize)
AzureIoTClient 38:ed9c888e5e12 125 {
AzureIoTClient 38:ed9c888e5e12 126 maxSize = totalSize;
AzureIoTClient 38:ed9c888e5e12 127 }
Azure.IoT Build 0:fa2de1b79154 128 }
Azure.IoT Build 0:fa2de1b79154 129 }
Azure.IoT Build 0:fa2de1b79154 130
Azure.IoT Build 0:fa2de1b79154 131 (void)Unlock(gballocThreadSafeLock);
Azure.IoT Build 0:fa2de1b79154 132 }
AzureIoTClient 48:81866008bba4 133
Azure.IoT Build 0:fa2de1b79154 134 return result;
Azure.IoT Build 0:fa2de1b79154 135 }
Azure.IoT Build 0:fa2de1b79154 136
Azure.IoT Build 0:fa2de1b79154 137 void* gballoc_calloc(size_t nmemb, size_t size)
Azure.IoT Build 0:fa2de1b79154 138 {
Azure.IoT Build 0:fa2de1b79154 139 void* result;
Azure.IoT Build 0:fa2de1b79154 140
Azure.IoT Build 0:fa2de1b79154 141 if (gballocState != GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 142 {
Azure.IoT Build 0:fa2de1b79154 143 /* Codes_SRS_GBALLOC_01_040: [If gballoc was not initialized gballoc_calloc shall simply call calloc without any memory tracking being performed.] */
Azure.IoT Build 0:fa2de1b79154 144 result = calloc(nmemb, size);
Azure.IoT Build 0:fa2de1b79154 145 }
Azure.IoT Build 0:fa2de1b79154 146 /* Codes_SRS_GBALLOC_01_031: [gballoc_calloc shall ensure thread safety by using the lock created by gballoc_Init] */
Azure.IoT Build 0:fa2de1b79154 147 else if (LOCK_OK != Lock(gballocThreadSafeLock))
Azure.IoT Build 0:fa2de1b79154 148 {
Azure.IoT Build 0:fa2de1b79154 149 /* Codes_SRS_GBALLOC_01_046: [If acquiring the lock fails, gballoc_calloc shall return NULL.] */
AzureIoTClient 1:9190c0f4d23a 150 LogError("Failed to get the Lock.");
Azure.IoT Build 0:fa2de1b79154 151 result = NULL;
Azure.IoT Build 0:fa2de1b79154 152 }
Azure.IoT Build 0:fa2de1b79154 153 else
Azure.IoT Build 0:fa2de1b79154 154 {
AzureIoTClient 38:ed9c888e5e12 155 ALLOCATION* allocation = (ALLOCATION*)malloc(sizeof(ALLOCATION));
AzureIoTClient 38:ed9c888e5e12 156 if (allocation == NULL)
Azure.IoT Build 0:fa2de1b79154 157 {
AzureIoTClient 38:ed9c888e5e12 158 result = NULL;
Azure.IoT Build 0:fa2de1b79154 159 }
Azure.IoT Build 0:fa2de1b79154 160 else
Azure.IoT Build 0:fa2de1b79154 161 {
AzureIoTClient 38:ed9c888e5e12 162 /* Codes_SRS_GBALLOC_01_020: [gballoc_calloc shall call the C99 calloc function and return its result.] */
AzureIoTClient 38:ed9c888e5e12 163 result = calloc(nmemb, size);
AzureIoTClient 38:ed9c888e5e12 164 if (result == NULL)
AzureIoTClient 38:ed9c888e5e12 165 {
AzureIoTClient 38:ed9c888e5e12 166 /* Codes_SRS_GBALLOC_01_022: [When the underlying calloc call fails, gballoc_calloc shall return NULL and size should not be counted towards total memory used.] */
AzureIoTClient 38:ed9c888e5e12 167 free(allocation);
AzureIoTClient 38:ed9c888e5e12 168 }
AzureIoTClient 38:ed9c888e5e12 169 else
AzureIoTClient 38:ed9c888e5e12 170 {
AzureIoTClient 38:ed9c888e5e12 171 /* Codes_SRS_GBALLOC_01_021: [If the underlying calloc call is successful, gballoc_calloc shall increment the total memory used with nmemb*size.] */
AzureIoTClient 38:ed9c888e5e12 172 allocation->ptr = result;
AzureIoTClient 38:ed9c888e5e12 173 allocation->size = nmemb * size;
AzureIoTClient 38:ed9c888e5e12 174 allocation->next = head;
AzureIoTClient 38:ed9c888e5e12 175 head = allocation;
AzureIoTClient 38:ed9c888e5e12 176 g_allocations++;
Azure.IoT Build 0:fa2de1b79154 177
AzureIoTClient 38:ed9c888e5e12 178 totalSize += allocation->size;
AzureIoTClient 38:ed9c888e5e12 179 /* Codes_SRS_GBALLOC_01_011: [The maximum total memory used shall be the maximum of the total memory used at any point.] */
AzureIoTClient 38:ed9c888e5e12 180 if (maxSize < totalSize)
AzureIoTClient 38:ed9c888e5e12 181 {
AzureIoTClient 38:ed9c888e5e12 182 maxSize = totalSize;
AzureIoTClient 38:ed9c888e5e12 183 }
Azure.IoT Build 0:fa2de1b79154 184 }
Azure.IoT Build 0:fa2de1b79154 185 }
Azure.IoT Build 0:fa2de1b79154 186
Azure.IoT Build 0:fa2de1b79154 187 (void)Unlock(gballocThreadSafeLock);
Azure.IoT Build 0:fa2de1b79154 188 }
Azure.IoT Build 0:fa2de1b79154 189
Azure.IoT Build 0:fa2de1b79154 190 return result;
Azure.IoT Build 0:fa2de1b79154 191 }
Azure.IoT Build 0:fa2de1b79154 192
Azure.IoT Build 0:fa2de1b79154 193 void* gballoc_realloc(void* ptr, size_t size)
Azure.IoT Build 0:fa2de1b79154 194 {
Azure.IoT Build 0:fa2de1b79154 195 ALLOCATION* curr;
Azure.IoT Build 0:fa2de1b79154 196 void* result;
Azure.IoT Build 0:fa2de1b79154 197 ALLOCATION* allocation = NULL;
Azure.IoT Build 0:fa2de1b79154 198
Azure.IoT Build 0:fa2de1b79154 199 if (gballocState != GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 200 {
Azure.IoT Build 0:fa2de1b79154 201 /* Codes_SRS_GBALLOC_01_041: [If gballoc was not initialized gballoc_realloc shall shall simply call realloc without any memory tracking being performed.] */
Azure.IoT Build 0:fa2de1b79154 202 result = realloc(ptr, size);
Azure.IoT Build 0:fa2de1b79154 203 }
Azure.IoT Build 0:fa2de1b79154 204 /* Codes_SRS_GBALLOC_01_032: [gballoc_realloc shall ensure thread safety by using the lock created by gballoc_Init.] */
Azure.IoT Build 0:fa2de1b79154 205 else if (LOCK_OK != Lock(gballocThreadSafeLock))
Azure.IoT Build 0:fa2de1b79154 206 {
Azure.IoT Build 0:fa2de1b79154 207 /* Codes_SRS_GBALLOC_01_047: [If acquiring the lock fails, gballoc_realloc shall return NULL.] */
AzureIoTClient 1:9190c0f4d23a 208 LogError("Failed to get the Lock.");
Azure.IoT Build 0:fa2de1b79154 209 result = NULL;
Azure.IoT Build 0:fa2de1b79154 210 }
Azure.IoT Build 0:fa2de1b79154 211 else
Azure.IoT Build 0:fa2de1b79154 212 {
AzureIoTClient 38:ed9c888e5e12 213 if (ptr == NULL)
AzureIoTClient 38:ed9c888e5e12 214 {
AzureIoTClient 38:ed9c888e5e12 215 /* Codes_SRS_GBALLOC_01_017: [When ptr is NULL, gballoc_realloc shall call the underlying realloc with ptr being NULL and the realloc result shall be tracked by gballoc.] */
AzureIoTClient 38:ed9c888e5e12 216 allocation = (ALLOCATION*)malloc(sizeof(ALLOCATION));
AzureIoTClient 38:ed9c888e5e12 217 }
AzureIoTClient 38:ed9c888e5e12 218 else
Azure.IoT Build 0:fa2de1b79154 219 {
AzureIoTClient 38:ed9c888e5e12 220 curr = head;
AzureIoTClient 38:ed9c888e5e12 221 while (curr != NULL)
Azure.IoT Build 0:fa2de1b79154 222 {
AzureIoTClient 38:ed9c888e5e12 223 if (curr->ptr == ptr)
AzureIoTClient 38:ed9c888e5e12 224 {
AzureIoTClient 38:ed9c888e5e12 225 allocation = curr;
AzureIoTClient 38:ed9c888e5e12 226 break;
AzureIoTClient 38:ed9c888e5e12 227 }
AzureIoTClient 38:ed9c888e5e12 228 else
AzureIoTClient 38:ed9c888e5e12 229 {
AzureIoTClient 38:ed9c888e5e12 230 curr = (ALLOCATION*)curr->next;
AzureIoTClient 38:ed9c888e5e12 231 }
AzureIoTClient 38:ed9c888e5e12 232 }
AzureIoTClient 38:ed9c888e5e12 233 }
AzureIoTClient 38:ed9c888e5e12 234
AzureIoTClient 38:ed9c888e5e12 235 if (allocation == NULL)
AzureIoTClient 38:ed9c888e5e12 236 {
AzureIoTClient 38:ed9c888e5e12 237 /* Codes_SRS_GBALLOC_01_015: [When allocating memory used for tracking by gballoc_realloc fails, gballoc_realloc shall return NULL and no change should be made to the counted total memory usage.] */
AzureIoTClient 38:ed9c888e5e12 238 /* Codes_SRS_GBALLOC_01_016: [When the ptr pointer cannot be found in the pointers tracked by gballoc, gballoc_realloc shall return NULL and the underlying realloc shall not be called.] */
AzureIoTClient 38:ed9c888e5e12 239 result = NULL;
AzureIoTClient 38:ed9c888e5e12 240 }
AzureIoTClient 38:ed9c888e5e12 241 else
AzureIoTClient 38:ed9c888e5e12 242 {
AzureIoTClient 38:ed9c888e5e12 243 result = realloc(ptr, size);
AzureIoTClient 38:ed9c888e5e12 244 if (result == NULL)
AzureIoTClient 38:ed9c888e5e12 245 {
AzureIoTClient 38:ed9c888e5e12 246 /* Codes_SRS_GBALLOC_01_014: [When the underlying realloc call fails, gballoc_realloc shall return NULL and no change should be made to the counted total memory usage.] */
AzureIoTClient 38:ed9c888e5e12 247 if (ptr == NULL)
AzureIoTClient 38:ed9c888e5e12 248 {
AzureIoTClient 38:ed9c888e5e12 249 free(allocation);
AzureIoTClient 38:ed9c888e5e12 250 }
Azure.IoT Build 0:fa2de1b79154 251 }
Azure.IoT Build 0:fa2de1b79154 252 else
Azure.IoT Build 0:fa2de1b79154 253 {
AzureIoTClient 38:ed9c888e5e12 254 if (ptr != NULL)
AzureIoTClient 38:ed9c888e5e12 255 {
AzureIoTClient 38:ed9c888e5e12 256 /* Codes_SRS_GBALLOC_01_006: [If the underlying realloc call is successful, gballoc_realloc shall look up the size associated with the pointer ptr and decrease the total memory used with that size.] */
AzureIoTClient 38:ed9c888e5e12 257 allocation->ptr = result;
AzureIoTClient 38:ed9c888e5e12 258 totalSize -= allocation->size;
AzureIoTClient 38:ed9c888e5e12 259 allocation->size = size;
AzureIoTClient 38:ed9c888e5e12 260 }
AzureIoTClient 38:ed9c888e5e12 261 else
AzureIoTClient 38:ed9c888e5e12 262 {
AzureIoTClient 38:ed9c888e5e12 263 /* add block */
AzureIoTClient 38:ed9c888e5e12 264 allocation->ptr = result;
AzureIoTClient 38:ed9c888e5e12 265 allocation->size = size;
AzureIoTClient 38:ed9c888e5e12 266 allocation->next = head;
AzureIoTClient 38:ed9c888e5e12 267 head = allocation;
AzureIoTClient 38:ed9c888e5e12 268 }
Azure.IoT Build 0:fa2de1b79154 269
AzureIoTClient 38:ed9c888e5e12 270 /* Codes_SRS_GBALLOC_01_007: [If realloc is successful, gballoc_realloc shall also increment the total memory used value tracked by this module.] */
AzureIoTClient 38:ed9c888e5e12 271 totalSize += size;
AzureIoTClient 38:ed9c888e5e12 272 g_allocations++;
AzureIoTClient 38:ed9c888e5e12 273
AzureIoTClient 38:ed9c888e5e12 274 /* Codes_SRS_GBALLOC_01_011: [The maximum total memory used shall be the maximum of the total memory used at any point.] */
AzureIoTClient 38:ed9c888e5e12 275 if (maxSize < totalSize)
AzureIoTClient 38:ed9c888e5e12 276 {
AzureIoTClient 38:ed9c888e5e12 277 maxSize = totalSize;
AzureIoTClient 38:ed9c888e5e12 278 }
Azure.IoT Build 0:fa2de1b79154 279 }
Azure.IoT Build 0:fa2de1b79154 280 }
Azure.IoT Build 0:fa2de1b79154 281
Azure.IoT Build 0:fa2de1b79154 282 (void)Unlock(gballocThreadSafeLock);
Azure.IoT Build 0:fa2de1b79154 283 }
Azure.IoT Build 0:fa2de1b79154 284
Azure.IoT Build 0:fa2de1b79154 285 return result;
Azure.IoT Build 0:fa2de1b79154 286 }
Azure.IoT Build 0:fa2de1b79154 287
Azure.IoT Build 0:fa2de1b79154 288 void gballoc_free(void* ptr)
Azure.IoT Build 0:fa2de1b79154 289 {
Azure.IoT Build 0:fa2de1b79154 290 ALLOCATION* curr = head;
Azure.IoT Build 0:fa2de1b79154 291 ALLOCATION* prev = NULL;
Azure.IoT Build 0:fa2de1b79154 292
Azure.IoT Build 0:fa2de1b79154 293 if (gballocState != GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 294 {
Azure.IoT Build 0:fa2de1b79154 295 /* Codes_SRS_GBALLOC_01_042: [If gballoc was not initialized gballoc_free shall shall simply call free.] */
Azure.IoT Build 0:fa2de1b79154 296 free(ptr);
Azure.IoT Build 0:fa2de1b79154 297 }
Azure.IoT Build 0:fa2de1b79154 298 /* Codes_SRS_GBALLOC_01_033: [gballoc_free shall ensure thread safety by using the lock created by gballoc_Init.] */
Azure.IoT Build 0:fa2de1b79154 299 else if (LOCK_OK != Lock(gballocThreadSafeLock))
Azure.IoT Build 0:fa2de1b79154 300 {
Azure.IoT Build 0:fa2de1b79154 301 /* Codes_SRS_GBALLOC_01_049: [If acquiring the lock fails, gballoc_free shall do nothing.] */
AzureIoTClient 1:9190c0f4d23a 302 LogError("Failed to get the Lock.");
Azure.IoT Build 0:fa2de1b79154 303 }
Azure.IoT Build 0:fa2de1b79154 304 else
Azure.IoT Build 0:fa2de1b79154 305 {
AzureIoTClient 38:ed9c888e5e12 306 /* Codes_SRS_GBALLOC_01_009: [gballoc_free shall also look up the size associated with the ptr pointer and decrease the total memory used with the associated size amount.] */
AzureIoTClient 38:ed9c888e5e12 307 while (curr != NULL)
Azure.IoT Build 0:fa2de1b79154 308 {
AzureIoTClient 38:ed9c888e5e12 309 if (curr->ptr == ptr)
Azure.IoT Build 0:fa2de1b79154 310 {
AzureIoTClient 38:ed9c888e5e12 311 /* Codes_SRS_GBALLOC_01_008: [gballoc_free shall call the C99 free function.] */
AzureIoTClient 38:ed9c888e5e12 312 free(ptr);
AzureIoTClient 38:ed9c888e5e12 313 totalSize -= curr->size;
AzureIoTClient 38:ed9c888e5e12 314 if (prev != NULL)
AzureIoTClient 38:ed9c888e5e12 315 {
AzureIoTClient 38:ed9c888e5e12 316 prev->next = curr->next;
AzureIoTClient 38:ed9c888e5e12 317 }
AzureIoTClient 38:ed9c888e5e12 318 else
AzureIoTClient 38:ed9c888e5e12 319 {
AzureIoTClient 38:ed9c888e5e12 320 head = (ALLOCATION*)curr->next;
AzureIoTClient 38:ed9c888e5e12 321 }
AzureIoTClient 38:ed9c888e5e12 322
AzureIoTClient 38:ed9c888e5e12 323 free(curr);
AzureIoTClient 38:ed9c888e5e12 324 break;
Azure.IoT Build 0:fa2de1b79154 325 }
Azure.IoT Build 0:fa2de1b79154 326
AzureIoTClient 38:ed9c888e5e12 327 prev = curr;
AzureIoTClient 38:ed9c888e5e12 328 curr = (ALLOCATION*)curr->next;
Azure.IoT Build 0:fa2de1b79154 329 }
Azure.IoT Build 0:fa2de1b79154 330
AzureIoTClient 38:ed9c888e5e12 331 if ((curr == NULL) && (ptr != NULL))
AzureIoTClient 38:ed9c888e5e12 332 {
AzureIoTClient 38:ed9c888e5e12 333 /* Codes_SRS_GBALLOC_01_019: [When the ptr pointer cannot be found in the pointers tracked by gballoc, gballoc_free shall not free any memory.] */
Azure.IoT Build 0:fa2de1b79154 334
AzureIoTClient 38:ed9c888e5e12 335 /* could not find the allocation */
AzureIoTClient 38:ed9c888e5e12 336 LogError("Could not free allocation for address %p (not found)", ptr);
AzureIoTClient 38:ed9c888e5e12 337 }
Azure.IoT Build 0:fa2de1b79154 338 (void)Unlock(gballocThreadSafeLock);
Azure.IoT Build 0:fa2de1b79154 339 }
Azure.IoT Build 0:fa2de1b79154 340 }
Azure.IoT Build 0:fa2de1b79154 341
Azure.IoT Build 0:fa2de1b79154 342 size_t gballoc_getMaximumMemoryUsed(void)
Azure.IoT Build 0:fa2de1b79154 343 {
Azure.IoT Build 0:fa2de1b79154 344 size_t result;
Azure.IoT Build 0:fa2de1b79154 345
Azure.IoT Build 0:fa2de1b79154 346 /* Codes_SRS_GBALLOC_01_038: [If gballoc was not initialized gballoc_getMaximumMemoryUsed shall return MAX_INT_SIZE.] */
Azure.IoT Build 0:fa2de1b79154 347 if (gballocState != GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 348 {
AzureIoTClient 1:9190c0f4d23a 349 LogError("gballoc is not initialized.");
Azure.IoT Build 0:fa2de1b79154 350 result = SIZE_MAX;
Azure.IoT Build 0:fa2de1b79154 351 }
Azure.IoT Build 0:fa2de1b79154 352 /* Codes_SRS_GBALLOC_01_034: [gballoc_getMaximumMemoryUsed shall ensure thread safety by using the lock created by gballoc_Init.] */
Azure.IoT Build 0:fa2de1b79154 353 else if (LOCK_OK != Lock(gballocThreadSafeLock))
Azure.IoT Build 0:fa2de1b79154 354 {
Azure.IoT Build 0:fa2de1b79154 355 /* Codes_SRS_GBALLOC_01_050: [If the lock cannot be acquired, gballoc_getMaximumMemoryUsed shall return SIZE_MAX.] */
AzureIoTClient 1:9190c0f4d23a 356 LogError("Failed to get the Lock.");
Azure.IoT Build 0:fa2de1b79154 357 result = SIZE_MAX;
Azure.IoT Build 0:fa2de1b79154 358 }
Azure.IoT Build 0:fa2de1b79154 359 else
Azure.IoT Build 0:fa2de1b79154 360 {
AzureIoTClient 25:8507bf644fdf 361 /* Codes_SRS_GBALLOC_01_010: [gballoc_getMaximumMemoryUsed shall return the maximum amount of total memory used recorded since the module initialization.] */
Azure.IoT Build 0:fa2de1b79154 362 result = maxSize;
AzureIoTClient 25:8507bf644fdf 363 (void)Unlock(gballocThreadSafeLock);
AzureIoTClient 25:8507bf644fdf 364 }
Azure.IoT Build 0:fa2de1b79154 365
Azure.IoT Build 0:fa2de1b79154 366 return result;
Azure.IoT Build 0:fa2de1b79154 367 }
Azure.IoT Build 0:fa2de1b79154 368
Azure.IoT Build 0:fa2de1b79154 369 size_t gballoc_getCurrentMemoryUsed(void)
Azure.IoT Build 0:fa2de1b79154 370 {
Azure.IoT Build 0:fa2de1b79154 371 size_t result;
Azure.IoT Build 0:fa2de1b79154 372
Azure.IoT Build 0:fa2de1b79154 373 /* Codes_SRS_GBALLOC_01_044: [If gballoc was not initialized gballoc_getCurrentMemoryUsed shall return SIZE_MAX.] */
Azure.IoT Build 0:fa2de1b79154 374 if (gballocState != GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 375 {
AzureIoTClient 1:9190c0f4d23a 376 LogError("gballoc is not initialized.");
Azure.IoT Build 0:fa2de1b79154 377 result = SIZE_MAX;
Azure.IoT Build 0:fa2de1b79154 378 }
Azure.IoT Build 0:fa2de1b79154 379 /* Codes_SRS_GBALLOC_01_036: [gballoc_getCurrentMemoryUsed shall ensure thread safety by using the lock created by gballoc_Init.]*/
Azure.IoT Build 0:fa2de1b79154 380 else if (LOCK_OK != Lock(gballocThreadSafeLock))
Azure.IoT Build 0:fa2de1b79154 381 {
Azure.IoT Build 0:fa2de1b79154 382 /* Codes_SRS_GBALLOC_01_051: [If the lock cannot be acquired, gballoc_getCurrentMemoryUsed shall return SIZE_MAX.] */
AzureIoTClient 1:9190c0f4d23a 383 LogError("Failed to get the Lock.");
Azure.IoT Build 0:fa2de1b79154 384 result = SIZE_MAX;
Azure.IoT Build 0:fa2de1b79154 385 }
Azure.IoT Build 0:fa2de1b79154 386 else
Azure.IoT Build 0:fa2de1b79154 387 {
AzureIoTClient 25:8507bf644fdf 388 /*Codes_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */
Azure.IoT Build 0:fa2de1b79154 389 result = totalSize;
AzureIoTClient 25:8507bf644fdf 390 (void)Unlock(gballocThreadSafeLock);
Azure.IoT Build 0:fa2de1b79154 391 }
Azure.IoT Build 0:fa2de1b79154 392
Azure.IoT Build 0:fa2de1b79154 393 return result;
Azure.IoT Build 0:fa2de1b79154 394 }
AzureIoTClient 34:0a87c89bdcd0 395
AzureIoTClient 34:0a87c89bdcd0 396 size_t gballoc_getAllocationCount(void)
AzureIoTClient 34:0a87c89bdcd0 397 {
AzureIoTClient 34:0a87c89bdcd0 398 size_t result;
AzureIoTClient 34:0a87c89bdcd0 399
AzureIoTClient 34:0a87c89bdcd0 400 /* Codes_SRS_GBALLOC_07_001: [ If gballoc was not initialized gballoc_getAllocationCount shall return 0. ] */
AzureIoTClient 34:0a87c89bdcd0 401 if (gballocState != GBALLOC_STATE_INIT)
AzureIoTClient 34:0a87c89bdcd0 402 {
AzureIoTClient 34:0a87c89bdcd0 403 LogError("gballoc is not initialized.");
AzureIoTClient 34:0a87c89bdcd0 404 result = 0;
AzureIoTClient 34:0a87c89bdcd0 405 }
AzureIoTClient 34:0a87c89bdcd0 406 /* Codes_SRS_GBALLOC_07_002: [ gballoc_getAllocationCount shall ensure thread safety by using the lock created by gballoc_Init ] */
AzureIoTClient 34:0a87c89bdcd0 407 else if (LOCK_OK != Lock(gballocThreadSafeLock))
AzureIoTClient 34:0a87c89bdcd0 408 {
AzureIoTClient 34:0a87c89bdcd0 409 /* Codes_SRS_GBALLOC_07_003: [ If the lock cannot be acquired, gballoc_getAllocationCount shall return 0. ] */
AzureIoTClient 34:0a87c89bdcd0 410 LogError("Failed to get the Lock.");
AzureIoTClient 34:0a87c89bdcd0 411 result = 0;
AzureIoTClient 34:0a87c89bdcd0 412 }
AzureIoTClient 34:0a87c89bdcd0 413 else
AzureIoTClient 34:0a87c89bdcd0 414 {
AzureIoTClient 34:0a87c89bdcd0 415 /* Codes_SRS_GBALLOC_07_004: [ gballoc_getAllocationCount shall return the currently number of allocations. ] */
AzureIoTClient 34:0a87c89bdcd0 416 result = g_allocations;
AzureIoTClient 34:0a87c89bdcd0 417 (void)Unlock(gballocThreadSafeLock);
AzureIoTClient 34:0a87c89bdcd0 418 }
AzureIoTClient 34:0a87c89bdcd0 419
AzureIoTClient 34:0a87c89bdcd0 420 return result;
AzureIoTClient 34:0a87c89bdcd0 421 }
AzureIoTClient 34:0a87c89bdcd0 422
AzureIoTClient 34:0a87c89bdcd0 423 void gballoc_resetMetrics()
AzureIoTClient 34:0a87c89bdcd0 424 {
AzureIoTClient 34:0a87c89bdcd0 425 /* Codes_SRS_GBALLOC_07_005: [ If gballoc was not initialized gballoc_reset Metrics shall do nothing.] */
AzureIoTClient 34:0a87c89bdcd0 426 if (gballocState != GBALLOC_STATE_INIT)
AzureIoTClient 34:0a87c89bdcd0 427 {
AzureIoTClient 34:0a87c89bdcd0 428 LogError("gballoc is not initialized.");
AzureIoTClient 34:0a87c89bdcd0 429 }
AzureIoTClient 34:0a87c89bdcd0 430 /* Codes_SRS_GBALLOC_07_006: [ gballoc_resetMetrics shall ensure thread safety by using the lock created by gballoc_Init ]*/
AzureIoTClient 34:0a87c89bdcd0 431 else if (LOCK_OK != Lock(gballocThreadSafeLock))
AzureIoTClient 34:0a87c89bdcd0 432 {
AzureIoTClient 34:0a87c89bdcd0 433 /* Codes_SRS_GBALLOC_07_007: [ If the lock cannot be acquired, gballoc_reset Metrics shall do nothing.] */
AzureIoTClient 34:0a87c89bdcd0 434 LogError("Failed to get the Lock.");
AzureIoTClient 34:0a87c89bdcd0 435 }
AzureIoTClient 34:0a87c89bdcd0 436 else
AzureIoTClient 34:0a87c89bdcd0 437 {
AzureIoTClient 34:0a87c89bdcd0 438 /* Codes_SRS_GBALLOC_07_008: [ gballoc_resetMetrics shall reset the total allocation size, max allocation size and number of allocation to zero. ] */
AzureIoTClient 34:0a87c89bdcd0 439 totalSize = 0;
AzureIoTClient 34:0a87c89bdcd0 440 maxSize = 0;
AzureIoTClient 34:0a87c89bdcd0 441 g_allocations = 0;
AzureIoTClient 34:0a87c89bdcd0 442 (void)Unlock(gballocThreadSafeLock);
AzureIoTClient 34:0a87c89bdcd0 443 }
AzureIoTClient 34:0a87c89bdcd0 444 }
AzureIoTClient 45:1119d0f2c4d8 445
AzureIoTClient 45:1119d0f2c4d8 446 #endif // GB_USE_CUSTOM_HEAP