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