Demo using MBED TLS

Dependencies:   EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed

Fork of iothub_client_sample_amqp by Azure IoT

Committer:
markrad
Date:
Thu Jan 05 00:20:03 2017 +0000
Revision:
58:f50b97b08851
Sample using MBED TLS

Who changed what in which revision?

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