Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Fri Feb 24 14:01:41 2017 -0800
Revision:
21:b92006c5b9ff
Parent:
19:2e0811512ceb
Child:
25:8507bf644fdf
1.1.8

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
Azure.IoT Build 0:fa2de1b79154 4 #include <stdlib.h>
AzureIoTClient 19:2e0811512ceb 5 #include <stdint.h>
AzureIoTClient 19:2e0811512ceb 6 #include <stddef.h>
Azure.IoT Build 0:fa2de1b79154 7 #include "azure_c_shared_utility/lock.h"
AzureIoTClient 21:b92006c5b9ff 8 #include "azure_c_shared_utility/optimize_size.h"
Azure.IoT Build 6:c55b013dfc2a 9 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 10
Azure.IoT Build 0:fa2de1b79154 11 #ifndef SIZE_MAX
Azure.IoT Build 0:fa2de1b79154 12 #define SIZE_MAX ((size_t)~(size_t)0)
Azure.IoT Build 0:fa2de1b79154 13 #endif
Azure.IoT Build 0:fa2de1b79154 14
Azure.IoT Build 0:fa2de1b79154 15 typedef struct ALLOCATION_TAG
Azure.IoT Build 0:fa2de1b79154 16 {
Azure.IoT Build 0:fa2de1b79154 17 size_t size;
Azure.IoT Build 0:fa2de1b79154 18 void* ptr;
Azure.IoT Build 0:fa2de1b79154 19 void* next;
Azure.IoT Build 0:fa2de1b79154 20 } ALLOCATION;
Azure.IoT Build 0:fa2de1b79154 21
Azure.IoT Build 0:fa2de1b79154 22 typedef enum GBALLOC_STATE_TAG
Azure.IoT Build 0:fa2de1b79154 23 {
Azure.IoT Build 0:fa2de1b79154 24 GBALLOC_STATE_INIT,
Azure.IoT Build 0:fa2de1b79154 25 GBALLOC_STATE_NOT_INIT
Azure.IoT Build 0:fa2de1b79154 26 } GBALLOC_STATE;
Azure.IoT Build 0:fa2de1b79154 27
Azure.IoT Build 0:fa2de1b79154 28 static ALLOCATION* head = NULL;
Azure.IoT Build 0:fa2de1b79154 29 static size_t totalSize = 0;
Azure.IoT Build 0:fa2de1b79154 30 static size_t maxSize = 0;
Azure.IoT Build 0:fa2de1b79154 31 static GBALLOC_STATE gballocState = GBALLOC_STATE_NOT_INIT;
Azure.IoT Build 0:fa2de1b79154 32
Azure.IoT Build 0:fa2de1b79154 33 static LOCK_HANDLE gballocThreadSafeLock = NULL;
Azure.IoT Build 0:fa2de1b79154 34
Azure.IoT Build 0:fa2de1b79154 35 int gballoc_init(void)
Azure.IoT Build 0:fa2de1b79154 36 {
Azure.IoT Build 0:fa2de1b79154 37 int result;
Azure.IoT Build 0:fa2de1b79154 38
Azure.IoT Build 0:fa2de1b79154 39 if (gballocState != GBALLOC_STATE_NOT_INIT)
Azure.IoT Build 0:fa2de1b79154 40 {
Azure.IoT Build 0:fa2de1b79154 41 /* Codes_SRS_GBALLOC_01_025: [Init after Init shall fail and return a non-zero value.] */
AzureIoTClient 21:b92006c5b9ff 42 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 43 }
Azure.IoT Build 0:fa2de1b79154 44 /* 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 45 else if ((gballocThreadSafeLock = Lock_Init()) == NULL)
Azure.IoT Build 0:fa2de1b79154 46 {
Azure.IoT Build 0:fa2de1b79154 47 /* Codes_SRS_GBALLOC_01_027: [If the Lock creation fails, gballoc_init shall return a non-zero value.]*/
AzureIoTClient 21:b92006c5b9ff 48 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 49 }
Azure.IoT Build 0:fa2de1b79154 50 else
Azure.IoT Build 0:fa2de1b79154 51 {
Azure.IoT Build 0:fa2de1b79154 52 gballocState = GBALLOC_STATE_INIT;
Azure.IoT Build 0:fa2de1b79154 53
Azure.IoT Build 0:fa2de1b79154 54 /* 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 55 totalSize = 0;
Azure.IoT Build 0:fa2de1b79154 56 maxSize = 0;
Azure.IoT Build 0:fa2de1b79154 57
Azure.IoT Build 0:fa2de1b79154 58 /* Codes_SRS_GBALLOC_01_024: [gballoc_init shall initialize the gballoc module and return 0 upon success.] */
Azure.IoT Build 0:fa2de1b79154 59 result = 0;
Azure.IoT Build 0:fa2de1b79154 60 }
Azure.IoT Build 0:fa2de1b79154 61
Azure.IoT Build 0:fa2de1b79154 62 return result;
Azure.IoT Build 0:fa2de1b79154 63 }
Azure.IoT Build 0:fa2de1b79154 64
Azure.IoT Build 0:fa2de1b79154 65 void gballoc_deinit(void)
Azure.IoT Build 0:fa2de1b79154 66 {
Azure.IoT Build 0:fa2de1b79154 67 if (gballocState == GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 68 {
Azure.IoT Build 0:fa2de1b79154 69 /* Codes_SRS_GBALLOC_01_028: [gballoc_deinit shall free all resources allocated by gballoc_init.] */
Azure.IoT Build 0:fa2de1b79154 70 (void)Lock_Deinit(gballocThreadSafeLock);
Azure.IoT Build 0:fa2de1b79154 71 }
Azure.IoT Build 0:fa2de1b79154 72
Azure.IoT Build 0:fa2de1b79154 73 gballocState = GBALLOC_STATE_NOT_INIT;
Azure.IoT Build 0:fa2de1b79154 74 }
Azure.IoT Build 0:fa2de1b79154 75
Azure.IoT Build 0:fa2de1b79154 76 void* gballoc_malloc(size_t size)
Azure.IoT Build 0:fa2de1b79154 77 {
Azure.IoT Build 0:fa2de1b79154 78 void* result;
Azure.IoT Build 0:fa2de1b79154 79
Azure.IoT Build 0:fa2de1b79154 80 if (gballocState != GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 81 {
Azure.IoT Build 0:fa2de1b79154 82 /* 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 83 result = malloc(size);
Azure.IoT Build 0:fa2de1b79154 84 }
Azure.IoT Build 0:fa2de1b79154 85 /* Codes_SRS_GBALLOC_01_030: [gballoc_malloc shall ensure thread safety by using the lock created by gballoc_Init.] */
Azure.IoT Build 0:fa2de1b79154 86 else if (LOCK_OK != Lock(gballocThreadSafeLock))
Azure.IoT Build 0:fa2de1b79154 87 {
Azure.IoT Build 0:fa2de1b79154 88 /* Codes_SRS_GBALLOC_01_048: [If acquiring the lock fails, gballoc_malloc shall return NULL.] */
AzureIoTClient 1:9190c0f4d23a 89 LogError("Failed to get the Lock.");
Azure.IoT Build 0:fa2de1b79154 90 result = NULL;
Azure.IoT Build 0:fa2de1b79154 91 }
Azure.IoT Build 0:fa2de1b79154 92 else
Azure.IoT Build 0:fa2de1b79154 93 {
Azure.IoT Build 0:fa2de1b79154 94 ALLOCATION* allocation = (ALLOCATION*)malloc(sizeof(ALLOCATION));
Azure.IoT Build 0:fa2de1b79154 95 if (allocation == NULL)
Azure.IoT Build 0:fa2de1b79154 96 {
Azure.IoT Build 0:fa2de1b79154 97 result = NULL;
Azure.IoT Build 0:fa2de1b79154 98 }
Azure.IoT Build 0:fa2de1b79154 99 else
Azure.IoT Build 0:fa2de1b79154 100 {
Azure.IoT Build 0:fa2de1b79154 101 /* Codes_SRS_GBALLOC_01_003: [gb_malloc shall call the C99 malloc function and return its result.] */
Azure.IoT Build 0:fa2de1b79154 102 result = malloc(size);
Azure.IoT Build 0:fa2de1b79154 103 if (result == NULL)
Azure.IoT Build 0:fa2de1b79154 104 {
Azure.IoT Build 0:fa2de1b79154 105 /* 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.] */
Azure.IoT Build 0:fa2de1b79154 106 free(allocation);
Azure.IoT Build 0:fa2de1b79154 107 }
Azure.IoT Build 0:fa2de1b79154 108 else
Azure.IoT Build 0:fa2de1b79154 109 {
Azure.IoT Build 0:fa2de1b79154 110 /* 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.] */
Azure.IoT Build 0:fa2de1b79154 111 allocation->ptr = result;
Azure.IoT Build 0:fa2de1b79154 112 allocation->size = size;
Azure.IoT Build 0:fa2de1b79154 113 allocation->next = head;
Azure.IoT Build 0:fa2de1b79154 114 head = allocation;
Azure.IoT Build 0:fa2de1b79154 115
Azure.IoT Build 0:fa2de1b79154 116 totalSize += size;
Azure.IoT Build 0:fa2de1b79154 117 /* Codes_SRS_GBALLOC_01_011: [The maximum total memory used shall be the maximum of the total memory used at any point.] */
Azure.IoT Build 0:fa2de1b79154 118 if (maxSize < totalSize)
Azure.IoT Build 0:fa2de1b79154 119 {
Azure.IoT Build 0:fa2de1b79154 120 maxSize = totalSize;
Azure.IoT Build 0:fa2de1b79154 121 }
Azure.IoT Build 0:fa2de1b79154 122 }
Azure.IoT Build 0:fa2de1b79154 123 }
Azure.IoT Build 0:fa2de1b79154 124
Azure.IoT Build 0:fa2de1b79154 125 (void)Unlock(gballocThreadSafeLock);
Azure.IoT Build 0:fa2de1b79154 126 }
Azure.IoT Build 0:fa2de1b79154 127
Azure.IoT Build 0:fa2de1b79154 128 return result;
Azure.IoT Build 0:fa2de1b79154 129 }
Azure.IoT Build 0:fa2de1b79154 130
Azure.IoT Build 0:fa2de1b79154 131 void* gballoc_calloc(size_t nmemb, size_t size)
Azure.IoT Build 0:fa2de1b79154 132 {
Azure.IoT Build 0:fa2de1b79154 133 void* result;
Azure.IoT Build 0:fa2de1b79154 134
Azure.IoT Build 0:fa2de1b79154 135 if (gballocState != GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 136 {
Azure.IoT Build 0:fa2de1b79154 137 /* 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 138 result = calloc(nmemb, size);
Azure.IoT Build 0:fa2de1b79154 139 }
Azure.IoT Build 0:fa2de1b79154 140 /* Codes_SRS_GBALLOC_01_031: [gballoc_calloc shall ensure thread safety by using the lock created by gballoc_Init] */
Azure.IoT Build 0:fa2de1b79154 141 else if (LOCK_OK != Lock(gballocThreadSafeLock))
Azure.IoT Build 0:fa2de1b79154 142 {
Azure.IoT Build 0:fa2de1b79154 143 /* Codes_SRS_GBALLOC_01_046: [If acquiring the lock fails, gballoc_calloc shall return NULL.] */
AzureIoTClient 1:9190c0f4d23a 144 LogError("Failed to get the Lock.");
Azure.IoT Build 0:fa2de1b79154 145 result = NULL;
Azure.IoT Build 0:fa2de1b79154 146 }
Azure.IoT Build 0:fa2de1b79154 147 else
Azure.IoT Build 0:fa2de1b79154 148 {
Azure.IoT Build 0:fa2de1b79154 149 ALLOCATION* allocation = (ALLOCATION*)malloc(sizeof(ALLOCATION));
Azure.IoT Build 0:fa2de1b79154 150 if (allocation == NULL)
Azure.IoT Build 0:fa2de1b79154 151 {
Azure.IoT Build 0:fa2de1b79154 152 result = NULL;
Azure.IoT Build 0:fa2de1b79154 153 }
Azure.IoT Build 0:fa2de1b79154 154 else
Azure.IoT Build 0:fa2de1b79154 155 {
Azure.IoT Build 0:fa2de1b79154 156 /* Codes_SRS_GBALLOC_01_020: [gballoc_calloc shall call the C99 calloc function and return its result.] */
Azure.IoT Build 0:fa2de1b79154 157 result = calloc(nmemb, size);
Azure.IoT Build 0:fa2de1b79154 158 if (result == NULL)
Azure.IoT Build 0:fa2de1b79154 159 {
Azure.IoT Build 0:fa2de1b79154 160 /* 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.] */
Azure.IoT Build 0:fa2de1b79154 161 free(allocation);
Azure.IoT Build 0:fa2de1b79154 162 }
Azure.IoT Build 0:fa2de1b79154 163 else
Azure.IoT Build 0:fa2de1b79154 164 {
Azure.IoT Build 0:fa2de1b79154 165 /* Codes_SRS_GBALLOC_01_021: [If the underlying calloc call is successful, gballoc_calloc shall increment the total memory used with nmemb*size.] */
Azure.IoT Build 0:fa2de1b79154 166 allocation->ptr = result;
Azure.IoT Build 0:fa2de1b79154 167 allocation->size = nmemb * size;
Azure.IoT Build 0:fa2de1b79154 168 allocation->next = head;
Azure.IoT Build 0:fa2de1b79154 169 head = allocation;
Azure.IoT Build 0:fa2de1b79154 170
Azure.IoT Build 0:fa2de1b79154 171 totalSize += allocation->size;
Azure.IoT Build 0:fa2de1b79154 172 /* Codes_SRS_GBALLOC_01_011: [The maximum total memory used shall be the maximum of the total memory used at any point.] */
Azure.IoT Build 0:fa2de1b79154 173 if (maxSize < totalSize)
Azure.IoT Build 0:fa2de1b79154 174 {
Azure.IoT Build 0:fa2de1b79154 175 maxSize = totalSize;
Azure.IoT Build 0:fa2de1b79154 176 }
Azure.IoT Build 0:fa2de1b79154 177 }
Azure.IoT Build 0:fa2de1b79154 178 }
Azure.IoT Build 0:fa2de1b79154 179
Azure.IoT Build 0:fa2de1b79154 180 (void)Unlock(gballocThreadSafeLock);
Azure.IoT Build 0:fa2de1b79154 181 }
Azure.IoT Build 0:fa2de1b79154 182
Azure.IoT Build 0:fa2de1b79154 183 return result;
Azure.IoT Build 0:fa2de1b79154 184 }
Azure.IoT Build 0:fa2de1b79154 185
Azure.IoT Build 0:fa2de1b79154 186 void* gballoc_realloc(void* ptr, size_t size)
Azure.IoT Build 0:fa2de1b79154 187 {
Azure.IoT Build 0:fa2de1b79154 188 ALLOCATION* curr;
Azure.IoT Build 0:fa2de1b79154 189 void* result;
Azure.IoT Build 0:fa2de1b79154 190 ALLOCATION* allocation = NULL;
Azure.IoT Build 0:fa2de1b79154 191
Azure.IoT Build 0:fa2de1b79154 192 if (gballocState != GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 193 {
Azure.IoT Build 0:fa2de1b79154 194 /* 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 195 result = realloc(ptr, size);
Azure.IoT Build 0:fa2de1b79154 196 }
Azure.IoT Build 0:fa2de1b79154 197 /* Codes_SRS_GBALLOC_01_032: [gballoc_realloc shall ensure thread safety by using the lock created by gballoc_Init.] */
Azure.IoT Build 0:fa2de1b79154 198 else if (LOCK_OK != Lock(gballocThreadSafeLock))
Azure.IoT Build 0:fa2de1b79154 199 {
Azure.IoT Build 0:fa2de1b79154 200 /* Codes_SRS_GBALLOC_01_047: [If acquiring the lock fails, gballoc_realloc shall return NULL.] */
AzureIoTClient 1:9190c0f4d23a 201 LogError("Failed to get the Lock.");
Azure.IoT Build 0:fa2de1b79154 202 result = NULL;
Azure.IoT Build 0:fa2de1b79154 203 }
Azure.IoT Build 0:fa2de1b79154 204 else
Azure.IoT Build 0:fa2de1b79154 205 {
Azure.IoT Build 0:fa2de1b79154 206 if (ptr == NULL)
Azure.IoT Build 0:fa2de1b79154 207 {
Azure.IoT Build 0:fa2de1b79154 208 /* 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.] */
Azure.IoT Build 0:fa2de1b79154 209 allocation = (ALLOCATION*)malloc(sizeof(ALLOCATION));
Azure.IoT Build 0:fa2de1b79154 210 }
Azure.IoT Build 0:fa2de1b79154 211 else
Azure.IoT Build 0:fa2de1b79154 212 {
Azure.IoT Build 0:fa2de1b79154 213 curr = head;
Azure.IoT Build 0:fa2de1b79154 214 while (curr != NULL)
Azure.IoT Build 0:fa2de1b79154 215 {
Azure.IoT Build 0:fa2de1b79154 216 if (curr->ptr == ptr)
Azure.IoT Build 0:fa2de1b79154 217 {
Azure.IoT Build 0:fa2de1b79154 218 allocation = curr;
Azure.IoT Build 0:fa2de1b79154 219 break;
Azure.IoT Build 0:fa2de1b79154 220 }
Azure.IoT Build 0:fa2de1b79154 221 else
Azure.IoT Build 0:fa2de1b79154 222 {
Azure.IoT Build 0:fa2de1b79154 223 curr = (ALLOCATION*)curr->next;
Azure.IoT Build 0:fa2de1b79154 224 }
Azure.IoT Build 0:fa2de1b79154 225 }
Azure.IoT Build 0:fa2de1b79154 226 }
Azure.IoT Build 0:fa2de1b79154 227
Azure.IoT Build 0:fa2de1b79154 228 if (allocation == NULL)
Azure.IoT Build 0:fa2de1b79154 229 {
Azure.IoT Build 0:fa2de1b79154 230 /* 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.] */
Azure.IoT Build 0:fa2de1b79154 231 /* 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.] */
Azure.IoT Build 0:fa2de1b79154 232 result = NULL;
Azure.IoT Build 0:fa2de1b79154 233 }
Azure.IoT Build 0:fa2de1b79154 234 else
Azure.IoT Build 0:fa2de1b79154 235 {
Azure.IoT Build 0:fa2de1b79154 236 result = realloc(ptr, size);
Azure.IoT Build 0:fa2de1b79154 237 if (result == NULL)
Azure.IoT Build 0:fa2de1b79154 238 {
Azure.IoT Build 0:fa2de1b79154 239 /* 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.] */
Azure.IoT Build 0:fa2de1b79154 240 if (ptr == NULL)
Azure.IoT Build 0:fa2de1b79154 241 {
Azure.IoT Build 0:fa2de1b79154 242 free(allocation);
Azure.IoT Build 0:fa2de1b79154 243 }
Azure.IoT Build 0:fa2de1b79154 244 }
Azure.IoT Build 0:fa2de1b79154 245 else
Azure.IoT Build 0:fa2de1b79154 246 {
Azure.IoT Build 0:fa2de1b79154 247 if (ptr != NULL)
Azure.IoT Build 0:fa2de1b79154 248 {
Azure.IoT Build 0:fa2de1b79154 249 /* 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.] */
Azure.IoT Build 0:fa2de1b79154 250 allocation->ptr = result;
Azure.IoT Build 0:fa2de1b79154 251 totalSize -= allocation->size;
Azure.IoT Build 0:fa2de1b79154 252 allocation->size = size;
Azure.IoT Build 0:fa2de1b79154 253 }
Azure.IoT Build 0:fa2de1b79154 254 else
Azure.IoT Build 0:fa2de1b79154 255 {
Azure.IoT Build 0:fa2de1b79154 256 /* add block */
Azure.IoT Build 0:fa2de1b79154 257 allocation->ptr = result;
Azure.IoT Build 0:fa2de1b79154 258 allocation->size = size;
Azure.IoT Build 0:fa2de1b79154 259 allocation->next = head;
Azure.IoT Build 0:fa2de1b79154 260 head = allocation;
Azure.IoT Build 0:fa2de1b79154 261 }
Azure.IoT Build 0:fa2de1b79154 262
Azure.IoT Build 0:fa2de1b79154 263 /* Codes_SRS_GBALLOC_01_007: [If realloc is successful, gballoc_realloc shall also increment the total memory used value tracked by this module.] */
Azure.IoT Build 0:fa2de1b79154 264 totalSize += size;
Azure.IoT Build 0:fa2de1b79154 265
Azure.IoT Build 0:fa2de1b79154 266 /* Codes_SRS_GBALLOC_01_011: [The maximum total memory used shall be the maximum of the total memory used at any point.] */
Azure.IoT Build 0:fa2de1b79154 267 if (maxSize < totalSize)
Azure.IoT Build 0:fa2de1b79154 268 {
Azure.IoT Build 0:fa2de1b79154 269 maxSize = totalSize;
Azure.IoT Build 0:fa2de1b79154 270 }
Azure.IoT Build 0:fa2de1b79154 271 }
Azure.IoT Build 0:fa2de1b79154 272 }
Azure.IoT Build 0:fa2de1b79154 273
Azure.IoT Build 0:fa2de1b79154 274 (void)Unlock(gballocThreadSafeLock);
Azure.IoT Build 0:fa2de1b79154 275 }
Azure.IoT Build 0:fa2de1b79154 276
Azure.IoT Build 0:fa2de1b79154 277 return result;
Azure.IoT Build 0:fa2de1b79154 278 }
Azure.IoT Build 0:fa2de1b79154 279
Azure.IoT Build 0:fa2de1b79154 280 void gballoc_free(void* ptr)
Azure.IoT Build 0:fa2de1b79154 281 {
Azure.IoT Build 0:fa2de1b79154 282 ALLOCATION* curr = head;
Azure.IoT Build 0:fa2de1b79154 283 ALLOCATION* prev = NULL;
Azure.IoT Build 0:fa2de1b79154 284
Azure.IoT Build 0:fa2de1b79154 285 if (gballocState != GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 286 {
Azure.IoT Build 0:fa2de1b79154 287 /* Codes_SRS_GBALLOC_01_042: [If gballoc was not initialized gballoc_free shall shall simply call free.] */
Azure.IoT Build 0:fa2de1b79154 288 free(ptr);
Azure.IoT Build 0:fa2de1b79154 289 }
Azure.IoT Build 0:fa2de1b79154 290 /* Codes_SRS_GBALLOC_01_033: [gballoc_free shall ensure thread safety by using the lock created by gballoc_Init.] */
Azure.IoT Build 0:fa2de1b79154 291 else if (LOCK_OK != Lock(gballocThreadSafeLock))
Azure.IoT Build 0:fa2de1b79154 292 {
Azure.IoT Build 0:fa2de1b79154 293 /* Codes_SRS_GBALLOC_01_049: [If acquiring the lock fails, gballoc_free shall do nothing.] */
AzureIoTClient 1:9190c0f4d23a 294 LogError("Failed to get the Lock.");
Azure.IoT Build 0:fa2de1b79154 295 }
Azure.IoT Build 0:fa2de1b79154 296 else
Azure.IoT Build 0:fa2de1b79154 297 {
Azure.IoT Build 0:fa2de1b79154 298 /* 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.] */
Azure.IoT Build 0:fa2de1b79154 299 while (curr != NULL)
Azure.IoT Build 0:fa2de1b79154 300 {
Azure.IoT Build 0:fa2de1b79154 301 if (curr->ptr == ptr)
Azure.IoT Build 0:fa2de1b79154 302 {
Azure.IoT Build 0:fa2de1b79154 303 /* Codes_SRS_GBALLOC_01_008: [gballoc_free shall call the C99 free function.] */
Azure.IoT Build 0:fa2de1b79154 304 free(ptr);
Azure.IoT Build 0:fa2de1b79154 305 totalSize -= curr->size;
Azure.IoT Build 0:fa2de1b79154 306 if (prev != NULL)
Azure.IoT Build 0:fa2de1b79154 307 {
Azure.IoT Build 0:fa2de1b79154 308 prev->next = curr->next;
Azure.IoT Build 0:fa2de1b79154 309 }
Azure.IoT Build 0:fa2de1b79154 310 else
Azure.IoT Build 0:fa2de1b79154 311 {
Azure.IoT Build 0:fa2de1b79154 312 head = (ALLOCATION*)curr->next;
Azure.IoT Build 0:fa2de1b79154 313 }
Azure.IoT Build 0:fa2de1b79154 314
Azure.IoT Build 0:fa2de1b79154 315 free(curr);
Azure.IoT Build 0:fa2de1b79154 316 break;
Azure.IoT Build 0:fa2de1b79154 317 }
Azure.IoT Build 0:fa2de1b79154 318
Azure.IoT Build 0:fa2de1b79154 319 prev = curr;
Azure.IoT Build 0:fa2de1b79154 320 curr = (ALLOCATION*)curr->next;
Azure.IoT Build 0:fa2de1b79154 321 }
Azure.IoT Build 0:fa2de1b79154 322
Azure.IoT Build 0:fa2de1b79154 323 if ((curr == NULL) && (ptr != NULL))
Azure.IoT Build 0:fa2de1b79154 324 {
Azure.IoT Build 0:fa2de1b79154 325 /* 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 326
Azure.IoT Build 0:fa2de1b79154 327 /* could not find the allocation */
AzureIoTClient 1:9190c0f4d23a 328 LogError("Could not free allocation for address %p (not found)", ptr);
Azure.IoT Build 0:fa2de1b79154 329 }
Azure.IoT Build 0:fa2de1b79154 330 (void)Unlock(gballocThreadSafeLock);
Azure.IoT Build 0:fa2de1b79154 331 }
Azure.IoT Build 0:fa2de1b79154 332 }
Azure.IoT Build 0:fa2de1b79154 333
Azure.IoT Build 0:fa2de1b79154 334 size_t gballoc_getMaximumMemoryUsed(void)
Azure.IoT Build 0:fa2de1b79154 335 {
Azure.IoT Build 0:fa2de1b79154 336 size_t result;
Azure.IoT Build 0:fa2de1b79154 337
Azure.IoT Build 0:fa2de1b79154 338 /* Codes_SRS_GBALLOC_01_038: [If gballoc was not initialized gballoc_getMaximumMemoryUsed shall return MAX_INT_SIZE.] */
Azure.IoT Build 0:fa2de1b79154 339 if (gballocState != GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 340 {
AzureIoTClient 1:9190c0f4d23a 341 LogError("gballoc is not initialized.");
Azure.IoT Build 0:fa2de1b79154 342 result = SIZE_MAX;
Azure.IoT Build 0:fa2de1b79154 343 }
Azure.IoT Build 0:fa2de1b79154 344 /* Codes_SRS_GBALLOC_01_034: [gballoc_getMaximumMemoryUsed shall ensure thread safety by using the lock created by gballoc_Init.] */
Azure.IoT Build 0:fa2de1b79154 345 else if (LOCK_OK != Lock(gballocThreadSafeLock))
Azure.IoT Build 0:fa2de1b79154 346 {
Azure.IoT Build 0:fa2de1b79154 347 /* Codes_SRS_GBALLOC_01_050: [If the lock cannot be acquired, gballoc_getMaximumMemoryUsed shall return SIZE_MAX.] */
AzureIoTClient 1:9190c0f4d23a 348 LogError("Failed to get the Lock.");
Azure.IoT Build 0:fa2de1b79154 349 result = SIZE_MAX;
Azure.IoT Build 0:fa2de1b79154 350 }
Azure.IoT Build 0:fa2de1b79154 351 else
Azure.IoT Build 0:fa2de1b79154 352 {
Azure.IoT Build 0:fa2de1b79154 353 /* 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 354 result = maxSize;
Azure.IoT Build 0:fa2de1b79154 355 Unlock(gballocThreadSafeLock);
Azure.IoT Build 0:fa2de1b79154 356 }
Azure.IoT Build 0:fa2de1b79154 357
Azure.IoT Build 0:fa2de1b79154 358 return result;
Azure.IoT Build 0:fa2de1b79154 359 }
Azure.IoT Build 0:fa2de1b79154 360
Azure.IoT Build 0:fa2de1b79154 361 size_t gballoc_getCurrentMemoryUsed(void)
Azure.IoT Build 0:fa2de1b79154 362 {
Azure.IoT Build 0:fa2de1b79154 363 size_t result;
Azure.IoT Build 0:fa2de1b79154 364
Azure.IoT Build 0:fa2de1b79154 365 /* Codes_SRS_GBALLOC_01_044: [If gballoc was not initialized gballoc_getCurrentMemoryUsed shall return SIZE_MAX.] */
Azure.IoT Build 0:fa2de1b79154 366 if (gballocState != GBALLOC_STATE_INIT)
Azure.IoT Build 0:fa2de1b79154 367 {
AzureIoTClient 1:9190c0f4d23a 368 LogError("gballoc is not initialized.");
Azure.IoT Build 0:fa2de1b79154 369 result = SIZE_MAX;
Azure.IoT Build 0:fa2de1b79154 370 }
Azure.IoT Build 0:fa2de1b79154 371 /* Codes_SRS_GBALLOC_01_036: [gballoc_getCurrentMemoryUsed shall ensure thread safety by using the lock created by gballoc_Init.]*/
Azure.IoT Build 0:fa2de1b79154 372 else if (LOCK_OK != Lock(gballocThreadSafeLock))
Azure.IoT Build 0:fa2de1b79154 373 {
Azure.IoT Build 0:fa2de1b79154 374 /* Codes_SRS_GBALLOC_01_051: [If the lock cannot be acquired, gballoc_getCurrentMemoryUsed shall return SIZE_MAX.] */
AzureIoTClient 1:9190c0f4d23a 375 LogError("Failed to get the Lock.");
Azure.IoT Build 0:fa2de1b79154 376 result = SIZE_MAX;
Azure.IoT Build 0:fa2de1b79154 377 }
Azure.IoT Build 0:fa2de1b79154 378 else
Azure.IoT Build 0:fa2de1b79154 379 {
Azure.IoT Build 0:fa2de1b79154 380 /*Codes_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */
Azure.IoT Build 0:fa2de1b79154 381 result = totalSize;
Azure.IoT Build 0:fa2de1b79154 382 Unlock(gballocThreadSafeLock);
Azure.IoT Build 0:fa2de1b79154 383 }
Azure.IoT Build 0:fa2de1b79154 384
Azure.IoT Build 0:fa2de1b79154 385 return result;
Azure.IoT Build 0:fa2de1b79154 386 }