Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed
Fork of iothub_client_sample_amqp by
amqpalloc.c
00001 // Copyright (c) Microsoft. All rights reserved. 00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 00003 00004 #include <stdlib.h> 00005 #ifdef _CRTDBG_MAP_ALLOC 00006 #include <crtdbg.h> 00007 #endif /* _CRTDBG_MAP_ALLOC */ 00008 #include <stdbool.h> 00009 #include <stdint.h> 00010 #include <stdio.h> 00011 #include "azure_uamqp_c/amqpalloc.h" 00012 00013 #ifndef SIZE_MAX 00014 #define SIZE_MAX ((size_t)~(size_t)0) 00015 #endif 00016 00017 static bool alloc_trace = false; 00018 00019 typedef struct ALLOCATION_TAG 00020 { 00021 size_t size; 00022 void* ptr; 00023 void* next; 00024 } ALLOCATION; 00025 00026 static ALLOCATION* head = NULL; 00027 static size_t total_size = 0; 00028 static size_t max_size = 0; 00029 00030 #define LOG_TRACE_MALLOC // printf 00031 00032 #ifndef DISABLE_MEMORY_TRACE 00033 00034 void* trace_malloc(size_t size) 00035 { 00036 void* result; 00037 00038 ALLOCATION* allocation = (ALLOCATION*)malloc(sizeof(ALLOCATION)); 00039 if (allocation == NULL) 00040 { 00041 result = NULL; 00042 } 00043 else 00044 { 00045 LOG_TRACE_MALLOC("Alloc: %lu\r\n", (unsigned long)size); 00046 result = malloc(size); 00047 if (result == NULL) 00048 { 00049 free(allocation); 00050 } 00051 else 00052 { 00053 allocation->ptr = result; 00054 allocation->size = size; 00055 allocation->next = head; 00056 head = allocation; 00057 00058 total_size += size; 00059 if (max_size < total_size) 00060 { 00061 max_size = total_size; 00062 } 00063 } 00064 } 00065 00066 return result; 00067 } 00068 00069 void* trace_calloc(size_t nmemb, size_t size) 00070 { 00071 void* result; 00072 00073 ALLOCATION* allocation = (ALLOCATION*)malloc(sizeof(ALLOCATION)); 00074 if (allocation == NULL) 00075 { 00076 result = NULL; 00077 } 00078 else 00079 { 00080 result = calloc(nmemb, size); 00081 if (result == NULL) 00082 { 00083 free(allocation); 00084 } 00085 else 00086 { 00087 allocation->ptr = result; 00088 allocation->size = nmemb * size; 00089 allocation->next = head; 00090 head = allocation; 00091 00092 total_size += allocation->size; 00093 if (max_size < total_size) 00094 { 00095 max_size = total_size; 00096 } 00097 } 00098 } 00099 00100 return result; 00101 } 00102 00103 void* trace_realloc(void* ptr, size_t size) 00104 { 00105 ALLOCATION* curr; 00106 void* result; 00107 ALLOCATION* allocation = NULL; 00108 00109 if (ptr == NULL) 00110 { 00111 LOG_TRACE_MALLOC("Realloc: %lu\r\n", (unsigned long)size); 00112 allocation = (ALLOCATION*)malloc(sizeof(ALLOCATION)); 00113 } 00114 else 00115 { 00116 curr = head; 00117 while (curr != NULL) 00118 { 00119 if (curr->ptr == ptr) 00120 { 00121 allocation = curr; 00122 break; 00123 } 00124 else 00125 { 00126 curr = (ALLOCATION*)curr->next; 00127 } 00128 } 00129 } 00130 00131 if (allocation == NULL) 00132 { 00133 result = NULL; 00134 } 00135 else 00136 { 00137 result = realloc(ptr, size); 00138 if (result == NULL) 00139 { 00140 if (ptr == NULL) 00141 { 00142 free(allocation); 00143 } 00144 } 00145 else 00146 { 00147 if (ptr != NULL) 00148 { 00149 LOG_TRACE_MALLOC("Realloc change: %ld\r\n", (long)size - allocation->size); 00150 00151 allocation->ptr = result; 00152 total_size -= allocation->size; 00153 allocation->size = size; 00154 } 00155 else 00156 { 00157 LOG_TRACE_MALLOC("Realloc: %lu\r\n", (unsigned long)size); 00158 00159 /* add block */ 00160 allocation->ptr = result; 00161 allocation->size = size; 00162 allocation->next = head; 00163 head = allocation; 00164 } 00165 00166 total_size += size; 00167 00168 if (max_size < total_size) 00169 { 00170 max_size = total_size; 00171 } 00172 } 00173 } 00174 00175 return result; 00176 } 00177 00178 void trace_free(void* ptr) 00179 { 00180 ALLOCATION* curr = head; 00181 ALLOCATION* prev = NULL; 00182 00183 while (curr != NULL) 00184 { 00185 if (curr->ptr == ptr) 00186 { 00187 free(ptr); 00188 LOG_TRACE_MALLOC("Free: %lu\r\n", (unsigned long)curr->size); 00189 total_size -= curr->size; 00190 if (prev != NULL) 00191 { 00192 prev->next = curr->next; 00193 } 00194 else 00195 { 00196 head = (ALLOCATION*)curr->next; 00197 } 00198 00199 free(curr); 00200 break; 00201 } 00202 00203 prev = curr; 00204 curr = (ALLOCATION*)curr->next; 00205 } 00206 } 00207 00208 void* amqpalloc_malloc(size_t size) 00209 { 00210 void* result; 00211 if (!alloc_trace) 00212 { 00213 result = malloc(size); 00214 } 00215 else 00216 { 00217 result = trace_malloc(size); 00218 } 00219 00220 return result; 00221 } 00222 00223 void amqpalloc_free(void* ptr) 00224 { 00225 if (!alloc_trace) 00226 { 00227 free(ptr); 00228 } 00229 else 00230 { 00231 trace_free(ptr); 00232 } 00233 } 00234 00235 void* amqpalloc_calloc(size_t nmemb, size_t size) 00236 { 00237 void* result; 00238 00239 if (!alloc_trace) 00240 { 00241 result = calloc(nmemb, size); 00242 } 00243 else 00244 { 00245 result = trace_calloc(nmemb, size); 00246 } 00247 00248 return result; 00249 } 00250 00251 void* amqpalloc_realloc(void* ptr, size_t size) 00252 { 00253 void* result; 00254 00255 if (!alloc_trace) 00256 { 00257 result = realloc(ptr, size); 00258 } 00259 else 00260 { 00261 result = trace_realloc(ptr, size); 00262 } 00263 00264 return result; 00265 } 00266 00267 #endif 00268 00269 size_t amqpalloc_get_maximum_memory_used(void) 00270 { 00271 return max_size; 00272 } 00273 00274 size_t amqpalloc_get_current_memory_used(void) 00275 { 00276 return total_size; 00277 } 00278 00279 void amqpalloc_set_memory_tracing_enabled(bool memory_tracing_enabled) 00280 { 00281 alloc_trace = memory_tracing_enabled; 00282 }
Generated on Tue Jul 12 2022 12:43:17 by
