Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
gballoc.c@38:ed9c888e5e12, 2018-01-17 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Wed Jan 17 08:58:29 2018 -0800
- Revision:
- 38:ed9c888e5e12
- Parent:
- 34:0a87c89bdcd0
- Child:
- 45:1119d0f2c4d8
1.1.30
Who changed what in which revision?
User | Revision | Line number | New 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; |
AzureIoTClient | 34:0a87c89bdcd0 | 31 | static size_t g_allocations = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 32 | static GBALLOC_STATE gballocState = GBALLOC_STATE_NOT_INIT; |
Azure.IoT Build | 0:fa2de1b79154 | 33 | |
Azure.IoT Build | 0:fa2de1b79154 | 34 | static LOCK_HANDLE gballocThreadSafeLock = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 35 | |
Azure.IoT Build | 0:fa2de1b79154 | 36 | int gballoc_init(void) |
Azure.IoT Build | 0:fa2de1b79154 | 37 | { |
Azure.IoT Build | 0:fa2de1b79154 | 38 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 39 | |
Azure.IoT Build | 0:fa2de1b79154 | 40 | if (gballocState != GBALLOC_STATE_NOT_INIT) |
Azure.IoT Build | 0:fa2de1b79154 | 41 | { |
Azure.IoT Build | 0:fa2de1b79154 | 42 | /* Codes_SRS_GBALLOC_01_025: [Init after Init shall fail and return a non-zero value.] */ |
AzureIoTClient | 21:b92006c5b9ff | 43 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 44 | } |
Azure.IoT Build | 0:fa2de1b79154 | 45 | /* 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 | 46 | else if ((gballocThreadSafeLock = Lock_Init()) == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 47 | { |
Azure.IoT Build | 0:fa2de1b79154 | 48 | /* Codes_SRS_GBALLOC_01_027: [If the Lock creation fails, gballoc_init shall return a non-zero value.]*/ |
AzureIoTClient | 21:b92006c5b9ff | 49 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 50 | } |
Azure.IoT Build | 0:fa2de1b79154 | 51 | else |
Azure.IoT Build | 0:fa2de1b79154 | 52 | { |
Azure.IoT Build | 0:fa2de1b79154 | 53 | gballocState = GBALLOC_STATE_INIT; |
Azure.IoT Build | 0:fa2de1b79154 | 54 | |
Azure.IoT Build | 0:fa2de1b79154 | 55 | /* 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 | 56 | totalSize = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 57 | maxSize = 0; |
AzureIoTClient | 34:0a87c89bdcd0 | 58 | g_allocations = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 59 | |
Azure.IoT Build | 0:fa2de1b79154 | 60 | /* Codes_SRS_GBALLOC_01_024: [gballoc_init shall initialize the gballoc module and return 0 upon success.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 61 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 62 | } |
Azure.IoT Build | 0:fa2de1b79154 | 63 | |
Azure.IoT Build | 0:fa2de1b79154 | 64 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 65 | } |
Azure.IoT Build | 0:fa2de1b79154 | 66 | |
Azure.IoT Build | 0:fa2de1b79154 | 67 | void gballoc_deinit(void) |
Azure.IoT Build | 0:fa2de1b79154 | 68 | { |
Azure.IoT Build | 0:fa2de1b79154 | 69 | if (gballocState == GBALLOC_STATE_INIT) |
Azure.IoT Build | 0:fa2de1b79154 | 70 | { |
Azure.IoT Build | 0:fa2de1b79154 | 71 | /* Codes_SRS_GBALLOC_01_028: [gballoc_deinit shall free all resources allocated by gballoc_init.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 72 | (void)Lock_Deinit(gballocThreadSafeLock); |
Azure.IoT Build | 0:fa2de1b79154 | 73 | } |
Azure.IoT Build | 0:fa2de1b79154 | 74 | |
Azure.IoT Build | 0:fa2de1b79154 | 75 | gballocState = GBALLOC_STATE_NOT_INIT; |
Azure.IoT Build | 0:fa2de1b79154 | 76 | } |
Azure.IoT Build | 0:fa2de1b79154 | 77 | |
Azure.IoT Build | 0:fa2de1b79154 | 78 | void* gballoc_malloc(size_t size) |
Azure.IoT Build | 0:fa2de1b79154 | 79 | { |
Azure.IoT Build | 0:fa2de1b79154 | 80 | void* result; |
Azure.IoT Build | 0:fa2de1b79154 | 81 | |
Azure.IoT Build | 0:fa2de1b79154 | 82 | if (gballocState != GBALLOC_STATE_INIT) |
Azure.IoT Build | 0:fa2de1b79154 | 83 | { |
Azure.IoT Build | 0:fa2de1b79154 | 84 | /* 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 | 85 | result = malloc(size); |
Azure.IoT Build | 0:fa2de1b79154 | 86 | } |
Azure.IoT Build | 0:fa2de1b79154 | 87 | /* Codes_SRS_GBALLOC_01_030: [gballoc_malloc shall ensure thread safety by using the lock created by gballoc_Init.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 88 | else if (LOCK_OK != Lock(gballocThreadSafeLock)) |
Azure.IoT Build | 0:fa2de1b79154 | 89 | { |
Azure.IoT Build | 0:fa2de1b79154 | 90 | /* Codes_SRS_GBALLOC_01_048: [If acquiring the lock fails, gballoc_malloc shall return NULL.] */ |
AzureIoTClient | 1:9190c0f4d23a | 91 | LogError("Failed to get the Lock."); |
Azure.IoT Build | 0:fa2de1b79154 | 92 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 93 | } |
Azure.IoT Build | 0:fa2de1b79154 | 94 | else |
Azure.IoT Build | 0:fa2de1b79154 | 95 | { |
AzureIoTClient | 38:ed9c888e5e12 | 96 | ALLOCATION* allocation = (ALLOCATION*)malloc(sizeof(ALLOCATION)); |
AzureIoTClient | 38:ed9c888e5e12 | 97 | if (allocation == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 98 | { |
AzureIoTClient | 38:ed9c888e5e12 | 99 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 100 | } |
Azure.IoT Build | 0:fa2de1b79154 | 101 | else |
Azure.IoT Build | 0:fa2de1b79154 | 102 | { |
AzureIoTClient | 38:ed9c888e5e12 | 103 | /* Codes_SRS_GBALLOC_01_003: [gb_malloc shall call the C99 malloc function and return its result.] */ |
AzureIoTClient | 38:ed9c888e5e12 | 104 | result = malloc(size); |
AzureIoTClient | 38:ed9c888e5e12 | 105 | if (result == NULL) |
AzureIoTClient | 38:ed9c888e5e12 | 106 | { |
AzureIoTClient | 38:ed9c888e5e12 | 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.] */ |
AzureIoTClient | 38:ed9c888e5e12 | 108 | free(allocation); |
AzureIoTClient | 38:ed9c888e5e12 | 109 | } |
AzureIoTClient | 38:ed9c888e5e12 | 110 | else |
AzureIoTClient | 38:ed9c888e5e12 | 111 | { |
AzureIoTClient | 38:ed9c888e5e12 | 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.] */ |
AzureIoTClient | 38:ed9c888e5e12 | 113 | allocation->ptr = result; |
AzureIoTClient | 38:ed9c888e5e12 | 114 | allocation->size = size; |
AzureIoTClient | 38:ed9c888e5e12 | 115 | allocation->next = head; |
AzureIoTClient | 38:ed9c888e5e12 | 116 | head = allocation; |
Azure.IoT Build | 0:fa2de1b79154 | 117 | |
AzureIoTClient | 38:ed9c888e5e12 | 118 | g_allocations++; |
AzureIoTClient | 38:ed9c888e5e12 | 119 | totalSize += size; |
AzureIoTClient | 38:ed9c888e5e12 | 120 | /* 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 | 121 | if (maxSize < totalSize) |
AzureIoTClient | 38:ed9c888e5e12 | 122 | { |
AzureIoTClient | 38:ed9c888e5e12 | 123 | maxSize = totalSize; |
AzureIoTClient | 38:ed9c888e5e12 | 124 | } |
Azure.IoT Build | 0:fa2de1b79154 | 125 | } |
Azure.IoT Build | 0:fa2de1b79154 | 126 | } |
Azure.IoT Build | 0:fa2de1b79154 | 127 | |
Azure.IoT Build | 0:fa2de1b79154 | 128 | (void)Unlock(gballocThreadSafeLock); |
Azure.IoT Build | 0:fa2de1b79154 | 129 | } |
Azure.IoT Build | 0:fa2de1b79154 | 130 | |
Azure.IoT Build | 0:fa2de1b79154 | 131 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 132 | } |
Azure.IoT Build | 0:fa2de1b79154 | 133 | |
Azure.IoT Build | 0:fa2de1b79154 | 134 | void* gballoc_calloc(size_t nmemb, size_t size) |
Azure.IoT Build | 0:fa2de1b79154 | 135 | { |
Azure.IoT Build | 0:fa2de1b79154 | 136 | void* result; |
Azure.IoT Build | 0:fa2de1b79154 | 137 | |
Azure.IoT Build | 0:fa2de1b79154 | 138 | if (gballocState != GBALLOC_STATE_INIT) |
Azure.IoT Build | 0:fa2de1b79154 | 139 | { |
Azure.IoT Build | 0:fa2de1b79154 | 140 | /* 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 | 141 | result = calloc(nmemb, size); |
Azure.IoT Build | 0:fa2de1b79154 | 142 | } |
Azure.IoT Build | 0:fa2de1b79154 | 143 | /* Codes_SRS_GBALLOC_01_031: [gballoc_calloc shall ensure thread safety by using the lock created by gballoc_Init] */ |
Azure.IoT Build | 0:fa2de1b79154 | 144 | else if (LOCK_OK != Lock(gballocThreadSafeLock)) |
Azure.IoT Build | 0:fa2de1b79154 | 145 | { |
Azure.IoT Build | 0:fa2de1b79154 | 146 | /* Codes_SRS_GBALLOC_01_046: [If acquiring the lock fails, gballoc_calloc shall return NULL.] */ |
AzureIoTClient | 1:9190c0f4d23a | 147 | LogError("Failed to get the Lock."); |
Azure.IoT Build | 0:fa2de1b79154 | 148 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 149 | } |
Azure.IoT Build | 0:fa2de1b79154 | 150 | else |
Azure.IoT Build | 0:fa2de1b79154 | 151 | { |
AzureIoTClient | 38:ed9c888e5e12 | 152 | ALLOCATION* allocation = (ALLOCATION*)malloc(sizeof(ALLOCATION)); |
AzureIoTClient | 38:ed9c888e5e12 | 153 | if (allocation == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 154 | { |
AzureIoTClient | 38:ed9c888e5e12 | 155 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 156 | } |
Azure.IoT Build | 0:fa2de1b79154 | 157 | else |
Azure.IoT Build | 0:fa2de1b79154 | 158 | { |
AzureIoTClient | 38:ed9c888e5e12 | 159 | /* Codes_SRS_GBALLOC_01_020: [gballoc_calloc shall call the C99 calloc function and return its result.] */ |
AzureIoTClient | 38:ed9c888e5e12 | 160 | result = calloc(nmemb, size); |
AzureIoTClient | 38:ed9c888e5e12 | 161 | if (result == NULL) |
AzureIoTClient | 38:ed9c888e5e12 | 162 | { |
AzureIoTClient | 38:ed9c888e5e12 | 163 | /* 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 | 164 | free(allocation); |
AzureIoTClient | 38:ed9c888e5e12 | 165 | } |
AzureIoTClient | 38:ed9c888e5e12 | 166 | else |
AzureIoTClient | 38:ed9c888e5e12 | 167 | { |
AzureIoTClient | 38:ed9c888e5e12 | 168 | /* 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 | 169 | allocation->ptr = result; |
AzureIoTClient | 38:ed9c888e5e12 | 170 | allocation->size = nmemb * size; |
AzureIoTClient | 38:ed9c888e5e12 | 171 | allocation->next = head; |
AzureIoTClient | 38:ed9c888e5e12 | 172 | head = allocation; |
AzureIoTClient | 38:ed9c888e5e12 | 173 | g_allocations++; |
Azure.IoT Build | 0:fa2de1b79154 | 174 | |
AzureIoTClient | 38:ed9c888e5e12 | 175 | totalSize += allocation->size; |
AzureIoTClient | 38:ed9c888e5e12 | 176 | /* 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 | 177 | if (maxSize < totalSize) |
AzureIoTClient | 38:ed9c888e5e12 | 178 | { |
AzureIoTClient | 38:ed9c888e5e12 | 179 | maxSize = totalSize; |
AzureIoTClient | 38:ed9c888e5e12 | 180 | } |
Azure.IoT Build | 0:fa2de1b79154 | 181 | } |
Azure.IoT Build | 0:fa2de1b79154 | 182 | } |
Azure.IoT Build | 0:fa2de1b79154 | 183 | |
Azure.IoT Build | 0:fa2de1b79154 | 184 | (void)Unlock(gballocThreadSafeLock); |
Azure.IoT Build | 0:fa2de1b79154 | 185 | } |
Azure.IoT Build | 0:fa2de1b79154 | 186 | |
Azure.IoT Build | 0:fa2de1b79154 | 187 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 188 | } |
Azure.IoT Build | 0:fa2de1b79154 | 189 | |
Azure.IoT Build | 0:fa2de1b79154 | 190 | void* gballoc_realloc(void* ptr, size_t size) |
Azure.IoT Build | 0:fa2de1b79154 | 191 | { |
Azure.IoT Build | 0:fa2de1b79154 | 192 | ALLOCATION* curr; |
Azure.IoT Build | 0:fa2de1b79154 | 193 | void* result; |
Azure.IoT Build | 0:fa2de1b79154 | 194 | ALLOCATION* allocation = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 195 | |
Azure.IoT Build | 0:fa2de1b79154 | 196 | if (gballocState != GBALLOC_STATE_INIT) |
Azure.IoT Build | 0:fa2de1b79154 | 197 | { |
Azure.IoT Build | 0:fa2de1b79154 | 198 | /* 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 | 199 | result = realloc(ptr, size); |
Azure.IoT Build | 0:fa2de1b79154 | 200 | } |
Azure.IoT Build | 0:fa2de1b79154 | 201 | /* Codes_SRS_GBALLOC_01_032: [gballoc_realloc shall ensure thread safety by using the lock created by gballoc_Init.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 202 | else if (LOCK_OK != Lock(gballocThreadSafeLock)) |
Azure.IoT Build | 0:fa2de1b79154 | 203 | { |
Azure.IoT Build | 0:fa2de1b79154 | 204 | /* Codes_SRS_GBALLOC_01_047: [If acquiring the lock fails, gballoc_realloc shall return NULL.] */ |
AzureIoTClient | 1:9190c0f4d23a | 205 | LogError("Failed to get the Lock."); |
Azure.IoT Build | 0:fa2de1b79154 | 206 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 207 | } |
Azure.IoT Build | 0:fa2de1b79154 | 208 | else |
Azure.IoT Build | 0:fa2de1b79154 | 209 | { |
AzureIoTClient | 38:ed9c888e5e12 | 210 | if (ptr == NULL) |
AzureIoTClient | 38:ed9c888e5e12 | 211 | { |
AzureIoTClient | 38:ed9c888e5e12 | 212 | /* 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 | 213 | allocation = (ALLOCATION*)malloc(sizeof(ALLOCATION)); |
AzureIoTClient | 38:ed9c888e5e12 | 214 | } |
AzureIoTClient | 38:ed9c888e5e12 | 215 | else |
Azure.IoT Build | 0:fa2de1b79154 | 216 | { |
AzureIoTClient | 38:ed9c888e5e12 | 217 | curr = head; |
AzureIoTClient | 38:ed9c888e5e12 | 218 | while (curr != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 219 | { |
AzureIoTClient | 38:ed9c888e5e12 | 220 | if (curr->ptr == ptr) |
AzureIoTClient | 38:ed9c888e5e12 | 221 | { |
AzureIoTClient | 38:ed9c888e5e12 | 222 | allocation = curr; |
AzureIoTClient | 38:ed9c888e5e12 | 223 | break; |
AzureIoTClient | 38:ed9c888e5e12 | 224 | } |
AzureIoTClient | 38:ed9c888e5e12 | 225 | else |
AzureIoTClient | 38:ed9c888e5e12 | 226 | { |
AzureIoTClient | 38:ed9c888e5e12 | 227 | curr = (ALLOCATION*)curr->next; |
AzureIoTClient | 38:ed9c888e5e12 | 228 | } |
AzureIoTClient | 38:ed9c888e5e12 | 229 | } |
AzureIoTClient | 38:ed9c888e5e12 | 230 | } |
AzureIoTClient | 38:ed9c888e5e12 | 231 | |
AzureIoTClient | 38:ed9c888e5e12 | 232 | if (allocation == NULL) |
AzureIoTClient | 38:ed9c888e5e12 | 233 | { |
AzureIoTClient | 38:ed9c888e5e12 | 234 | /* 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 | 235 | /* 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 | 236 | result = NULL; |
AzureIoTClient | 38:ed9c888e5e12 | 237 | } |
AzureIoTClient | 38:ed9c888e5e12 | 238 | else |
AzureIoTClient | 38:ed9c888e5e12 | 239 | { |
AzureIoTClient | 38:ed9c888e5e12 | 240 | result = realloc(ptr, size); |
AzureIoTClient | 38:ed9c888e5e12 | 241 | if (result == NULL) |
AzureIoTClient | 38:ed9c888e5e12 | 242 | { |
AzureIoTClient | 38:ed9c888e5e12 | 243 | /* 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 | 244 | if (ptr == NULL) |
AzureIoTClient | 38:ed9c888e5e12 | 245 | { |
AzureIoTClient | 38:ed9c888e5e12 | 246 | free(allocation); |
AzureIoTClient | 38:ed9c888e5e12 | 247 | } |
Azure.IoT Build | 0:fa2de1b79154 | 248 | } |
Azure.IoT Build | 0:fa2de1b79154 | 249 | else |
Azure.IoT Build | 0:fa2de1b79154 | 250 | { |
AzureIoTClient | 38:ed9c888e5e12 | 251 | if (ptr != NULL) |
AzureIoTClient | 38:ed9c888e5e12 | 252 | { |
AzureIoTClient | 38:ed9c888e5e12 | 253 | /* 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 | 254 | allocation->ptr = result; |
AzureIoTClient | 38:ed9c888e5e12 | 255 | totalSize -= allocation->size; |
AzureIoTClient | 38:ed9c888e5e12 | 256 | allocation->size = size; |
AzureIoTClient | 38:ed9c888e5e12 | 257 | } |
AzureIoTClient | 38:ed9c888e5e12 | 258 | else |
AzureIoTClient | 38:ed9c888e5e12 | 259 | { |
AzureIoTClient | 38:ed9c888e5e12 | 260 | /* add block */ |
AzureIoTClient | 38:ed9c888e5e12 | 261 | allocation->ptr = result; |
AzureIoTClient | 38:ed9c888e5e12 | 262 | allocation->size = size; |
AzureIoTClient | 38:ed9c888e5e12 | 263 | allocation->next = head; |
AzureIoTClient | 38:ed9c888e5e12 | 264 | head = allocation; |
AzureIoTClient | 38:ed9c888e5e12 | 265 | } |
Azure.IoT Build | 0:fa2de1b79154 | 266 | |
AzureIoTClient | 38:ed9c888e5e12 | 267 | /* 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 | 268 | totalSize += size; |
AzureIoTClient | 38:ed9c888e5e12 | 269 | g_allocations++; |
AzureIoTClient | 38:ed9c888e5e12 | 270 | |
AzureIoTClient | 38:ed9c888e5e12 | 271 | /* 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 | 272 | if (maxSize < totalSize) |
AzureIoTClient | 38:ed9c888e5e12 | 273 | { |
AzureIoTClient | 38:ed9c888e5e12 | 274 | maxSize = totalSize; |
AzureIoTClient | 38:ed9c888e5e12 | 275 | } |
Azure.IoT Build | 0:fa2de1b79154 | 276 | } |
Azure.IoT Build | 0:fa2de1b79154 | 277 | } |
Azure.IoT Build | 0:fa2de1b79154 | 278 | |
Azure.IoT Build | 0:fa2de1b79154 | 279 | (void)Unlock(gballocThreadSafeLock); |
Azure.IoT Build | 0:fa2de1b79154 | 280 | } |
Azure.IoT Build | 0:fa2de1b79154 | 281 | |
Azure.IoT Build | 0:fa2de1b79154 | 282 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 283 | } |
Azure.IoT Build | 0:fa2de1b79154 | 284 | |
Azure.IoT Build | 0:fa2de1b79154 | 285 | void gballoc_free(void* ptr) |
Azure.IoT Build | 0:fa2de1b79154 | 286 | { |
Azure.IoT Build | 0:fa2de1b79154 | 287 | ALLOCATION* curr = head; |
Azure.IoT Build | 0:fa2de1b79154 | 288 | ALLOCATION* prev = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 289 | |
Azure.IoT Build | 0:fa2de1b79154 | 290 | if (gballocState != GBALLOC_STATE_INIT) |
Azure.IoT Build | 0:fa2de1b79154 | 291 | { |
Azure.IoT Build | 0:fa2de1b79154 | 292 | /* Codes_SRS_GBALLOC_01_042: [If gballoc was not initialized gballoc_free shall shall simply call free.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 293 | free(ptr); |
Azure.IoT Build | 0:fa2de1b79154 | 294 | } |
Azure.IoT Build | 0:fa2de1b79154 | 295 | /* Codes_SRS_GBALLOC_01_033: [gballoc_free shall ensure thread safety by using the lock created by gballoc_Init.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 296 | else if (LOCK_OK != Lock(gballocThreadSafeLock)) |
Azure.IoT Build | 0:fa2de1b79154 | 297 | { |
Azure.IoT Build | 0:fa2de1b79154 | 298 | /* Codes_SRS_GBALLOC_01_049: [If acquiring the lock fails, gballoc_free shall do nothing.] */ |
AzureIoTClient | 1:9190c0f4d23a | 299 | LogError("Failed to get the Lock."); |
Azure.IoT Build | 0:fa2de1b79154 | 300 | } |
Azure.IoT Build | 0:fa2de1b79154 | 301 | else |
Azure.IoT Build | 0:fa2de1b79154 | 302 | { |
AzureIoTClient | 38:ed9c888e5e12 | 303 | /* 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 | 304 | while (curr != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 305 | { |
AzureIoTClient | 38:ed9c888e5e12 | 306 | if (curr->ptr == ptr) |
Azure.IoT Build | 0:fa2de1b79154 | 307 | { |
AzureIoTClient | 38:ed9c888e5e12 | 308 | /* Codes_SRS_GBALLOC_01_008: [gballoc_free shall call the C99 free function.] */ |
AzureIoTClient | 38:ed9c888e5e12 | 309 | free(ptr); |
AzureIoTClient | 38:ed9c888e5e12 | 310 | totalSize -= curr->size; |
AzureIoTClient | 38:ed9c888e5e12 | 311 | if (prev != NULL) |
AzureIoTClient | 38:ed9c888e5e12 | 312 | { |
AzureIoTClient | 38:ed9c888e5e12 | 313 | prev->next = curr->next; |
AzureIoTClient | 38:ed9c888e5e12 | 314 | } |
AzureIoTClient | 38:ed9c888e5e12 | 315 | else |
AzureIoTClient | 38:ed9c888e5e12 | 316 | { |
AzureIoTClient | 38:ed9c888e5e12 | 317 | head = (ALLOCATION*)curr->next; |
AzureIoTClient | 38:ed9c888e5e12 | 318 | } |
AzureIoTClient | 38:ed9c888e5e12 | 319 | |
AzureIoTClient | 38:ed9c888e5e12 | 320 | free(curr); |
AzureIoTClient | 38:ed9c888e5e12 | 321 | break; |
Azure.IoT Build | 0:fa2de1b79154 | 322 | } |
Azure.IoT Build | 0:fa2de1b79154 | 323 | |
AzureIoTClient | 38:ed9c888e5e12 | 324 | prev = curr; |
AzureIoTClient | 38:ed9c888e5e12 | 325 | curr = (ALLOCATION*)curr->next; |
Azure.IoT Build | 0:fa2de1b79154 | 326 | } |
Azure.IoT Build | 0:fa2de1b79154 | 327 | |
AzureIoTClient | 38:ed9c888e5e12 | 328 | if ((curr == NULL) && (ptr != NULL)) |
AzureIoTClient | 38:ed9c888e5e12 | 329 | { |
AzureIoTClient | 38:ed9c888e5e12 | 330 | /* 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 | 331 | |
AzureIoTClient | 38:ed9c888e5e12 | 332 | /* could not find the allocation */ |
AzureIoTClient | 38:ed9c888e5e12 | 333 | LogError("Could not free allocation for address %p (not found)", ptr); |
AzureIoTClient | 38:ed9c888e5e12 | 334 | } |
Azure.IoT Build | 0:fa2de1b79154 | 335 | (void)Unlock(gballocThreadSafeLock); |
Azure.IoT Build | 0:fa2de1b79154 | 336 | } |
Azure.IoT Build | 0:fa2de1b79154 | 337 | } |
Azure.IoT Build | 0:fa2de1b79154 | 338 | |
Azure.IoT Build | 0:fa2de1b79154 | 339 | size_t gballoc_getMaximumMemoryUsed(void) |
Azure.IoT Build | 0:fa2de1b79154 | 340 | { |
Azure.IoT Build | 0:fa2de1b79154 | 341 | size_t result; |
Azure.IoT Build | 0:fa2de1b79154 | 342 | |
Azure.IoT Build | 0:fa2de1b79154 | 343 | /* Codes_SRS_GBALLOC_01_038: [If gballoc was not initialized gballoc_getMaximumMemoryUsed shall return MAX_INT_SIZE.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 344 | if (gballocState != GBALLOC_STATE_INIT) |
Azure.IoT Build | 0:fa2de1b79154 | 345 | { |
AzureIoTClient | 1:9190c0f4d23a | 346 | LogError("gballoc is not initialized."); |
Azure.IoT Build | 0:fa2de1b79154 | 347 | result = SIZE_MAX; |
Azure.IoT Build | 0:fa2de1b79154 | 348 | } |
Azure.IoT Build | 0:fa2de1b79154 | 349 | /* Codes_SRS_GBALLOC_01_034: [gballoc_getMaximumMemoryUsed shall ensure thread safety by using the lock created by gballoc_Init.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 350 | else if (LOCK_OK != Lock(gballocThreadSafeLock)) |
Azure.IoT Build | 0:fa2de1b79154 | 351 | { |
Azure.IoT Build | 0:fa2de1b79154 | 352 | /* Codes_SRS_GBALLOC_01_050: [If the lock cannot be acquired, gballoc_getMaximumMemoryUsed shall return SIZE_MAX.] */ |
AzureIoTClient | 1:9190c0f4d23a | 353 | LogError("Failed to get the Lock."); |
Azure.IoT Build | 0:fa2de1b79154 | 354 | result = SIZE_MAX; |
Azure.IoT Build | 0:fa2de1b79154 | 355 | } |
Azure.IoT Build | 0:fa2de1b79154 | 356 | else |
Azure.IoT Build | 0:fa2de1b79154 | 357 | { |
AzureIoTClient | 25:8507bf644fdf | 358 | /* 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 | 359 | result = maxSize; |
AzureIoTClient | 25:8507bf644fdf | 360 | (void)Unlock(gballocThreadSafeLock); |
AzureIoTClient | 25:8507bf644fdf | 361 | } |
Azure.IoT Build | 0:fa2de1b79154 | 362 | |
Azure.IoT Build | 0:fa2de1b79154 | 363 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 364 | } |
Azure.IoT Build | 0:fa2de1b79154 | 365 | |
Azure.IoT Build | 0:fa2de1b79154 | 366 | size_t gballoc_getCurrentMemoryUsed(void) |
Azure.IoT Build | 0:fa2de1b79154 | 367 | { |
Azure.IoT Build | 0:fa2de1b79154 | 368 | size_t result; |
Azure.IoT Build | 0:fa2de1b79154 | 369 | |
Azure.IoT Build | 0:fa2de1b79154 | 370 | /* Codes_SRS_GBALLOC_01_044: [If gballoc was not initialized gballoc_getCurrentMemoryUsed shall return SIZE_MAX.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 371 | if (gballocState != GBALLOC_STATE_INIT) |
Azure.IoT Build | 0:fa2de1b79154 | 372 | { |
AzureIoTClient | 1:9190c0f4d23a | 373 | LogError("gballoc is not initialized."); |
Azure.IoT Build | 0:fa2de1b79154 | 374 | result = SIZE_MAX; |
Azure.IoT Build | 0:fa2de1b79154 | 375 | } |
Azure.IoT Build | 0:fa2de1b79154 | 376 | /* Codes_SRS_GBALLOC_01_036: [gballoc_getCurrentMemoryUsed shall ensure thread safety by using the lock created by gballoc_Init.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 377 | else if (LOCK_OK != Lock(gballocThreadSafeLock)) |
Azure.IoT Build | 0:fa2de1b79154 | 378 | { |
Azure.IoT Build | 0:fa2de1b79154 | 379 | /* Codes_SRS_GBALLOC_01_051: [If the lock cannot be acquired, gballoc_getCurrentMemoryUsed shall return SIZE_MAX.] */ |
AzureIoTClient | 1:9190c0f4d23a | 380 | LogError("Failed to get the Lock."); |
Azure.IoT Build | 0:fa2de1b79154 | 381 | result = SIZE_MAX; |
Azure.IoT Build | 0:fa2de1b79154 | 382 | } |
Azure.IoT Build | 0:fa2de1b79154 | 383 | else |
Azure.IoT Build | 0:fa2de1b79154 | 384 | { |
AzureIoTClient | 25:8507bf644fdf | 385 | /*Codes_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 386 | result = totalSize; |
AzureIoTClient | 25:8507bf644fdf | 387 | (void)Unlock(gballocThreadSafeLock); |
Azure.IoT Build | 0:fa2de1b79154 | 388 | } |
Azure.IoT Build | 0:fa2de1b79154 | 389 | |
Azure.IoT Build | 0:fa2de1b79154 | 390 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 391 | } |
AzureIoTClient | 34:0a87c89bdcd0 | 392 | |
AzureIoTClient | 34:0a87c89bdcd0 | 393 | size_t gballoc_getAllocationCount(void) |
AzureIoTClient | 34:0a87c89bdcd0 | 394 | { |
AzureIoTClient | 34:0a87c89bdcd0 | 395 | size_t result; |
AzureIoTClient | 34:0a87c89bdcd0 | 396 | |
AzureIoTClient | 34:0a87c89bdcd0 | 397 | /* Codes_SRS_GBALLOC_07_001: [ If gballoc was not initialized gballoc_getAllocationCount shall return 0. ] */ |
AzureIoTClient | 34:0a87c89bdcd0 | 398 | if (gballocState != GBALLOC_STATE_INIT) |
AzureIoTClient | 34:0a87c89bdcd0 | 399 | { |
AzureIoTClient | 34:0a87c89bdcd0 | 400 | LogError("gballoc is not initialized."); |
AzureIoTClient | 34:0a87c89bdcd0 | 401 | result = 0; |
AzureIoTClient | 34:0a87c89bdcd0 | 402 | } |
AzureIoTClient | 34:0a87c89bdcd0 | 403 | /* Codes_SRS_GBALLOC_07_002: [ gballoc_getAllocationCount shall ensure thread safety by using the lock created by gballoc_Init ] */ |
AzureIoTClient | 34:0a87c89bdcd0 | 404 | else if (LOCK_OK != Lock(gballocThreadSafeLock)) |
AzureIoTClient | 34:0a87c89bdcd0 | 405 | { |
AzureIoTClient | 34:0a87c89bdcd0 | 406 | /* Codes_SRS_GBALLOC_07_003: [ If the lock cannot be acquired, gballoc_getAllocationCount shall return 0. ] */ |
AzureIoTClient | 34:0a87c89bdcd0 | 407 | LogError("Failed to get the Lock."); |
AzureIoTClient | 34:0a87c89bdcd0 | 408 | result = 0; |
AzureIoTClient | 34:0a87c89bdcd0 | 409 | } |
AzureIoTClient | 34:0a87c89bdcd0 | 410 | else |
AzureIoTClient | 34:0a87c89bdcd0 | 411 | { |
AzureIoTClient | 34:0a87c89bdcd0 | 412 | /* Codes_SRS_GBALLOC_07_004: [ gballoc_getAllocationCount shall return the currently number of allocations. ] */ |
AzureIoTClient | 34:0a87c89bdcd0 | 413 | result = g_allocations; |
AzureIoTClient | 34:0a87c89bdcd0 | 414 | (void)Unlock(gballocThreadSafeLock); |
AzureIoTClient | 34:0a87c89bdcd0 | 415 | } |
AzureIoTClient | 34:0a87c89bdcd0 | 416 | |
AzureIoTClient | 34:0a87c89bdcd0 | 417 | return result; |
AzureIoTClient | 34:0a87c89bdcd0 | 418 | } |
AzureIoTClient | 34:0a87c89bdcd0 | 419 | |
AzureIoTClient | 34:0a87c89bdcd0 | 420 | void gballoc_resetMetrics() |
AzureIoTClient | 34:0a87c89bdcd0 | 421 | { |
AzureIoTClient | 34:0a87c89bdcd0 | 422 | /* Codes_SRS_GBALLOC_07_005: [ If gballoc was not initialized gballoc_reset Metrics shall do nothing.] */ |
AzureIoTClient | 34:0a87c89bdcd0 | 423 | if (gballocState != GBALLOC_STATE_INIT) |
AzureIoTClient | 34:0a87c89bdcd0 | 424 | { |
AzureIoTClient | 34:0a87c89bdcd0 | 425 | LogError("gballoc is not initialized."); |
AzureIoTClient | 34:0a87c89bdcd0 | 426 | } |
AzureIoTClient | 34:0a87c89bdcd0 | 427 | /* Codes_SRS_GBALLOC_07_006: [ gballoc_resetMetrics shall ensure thread safety by using the lock created by gballoc_Init ]*/ |
AzureIoTClient | 34:0a87c89bdcd0 | 428 | else if (LOCK_OK != Lock(gballocThreadSafeLock)) |
AzureIoTClient | 34:0a87c89bdcd0 | 429 | { |
AzureIoTClient | 34:0a87c89bdcd0 | 430 | /* Codes_SRS_GBALLOC_07_007: [ If the lock cannot be acquired, gballoc_reset Metrics shall do nothing.] */ |
AzureIoTClient | 34:0a87c89bdcd0 | 431 | LogError("Failed to get the Lock."); |
AzureIoTClient | 34:0a87c89bdcd0 | 432 | } |
AzureIoTClient | 34:0a87c89bdcd0 | 433 | else |
AzureIoTClient | 34:0a87c89bdcd0 | 434 | { |
AzureIoTClient | 34:0a87c89bdcd0 | 435 | /* 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 | 436 | totalSize = 0; |
AzureIoTClient | 34:0a87c89bdcd0 | 437 | maxSize = 0; |
AzureIoTClient | 34:0a87c89bdcd0 | 438 | g_allocations = 0; |
AzureIoTClient | 34:0a87c89bdcd0 | 439 | (void)Unlock(gballocThreadSafeLock); |
AzureIoTClient | 34:0a87c89bdcd0 | 440 | } |
AzureIoTClient | 34:0a87c89bdcd0 | 441 | } |