58 #ifndef WOLFSSL_MEM_TRACK_H 59 #define WOLFSSL_MEM_TRACK_H 61 #if defined(USE_WOLFSSL_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY) 65 #if defined(WOLFSSL_TRACK_MEMORY) 67 #if defined(__linux__) || defined(__MACH__) 87 #ifdef WOLFSSL_DEBUG_MEMORY 98 byte alignit[
sizeof(
memHint) + ((16-1) & ~(16-1))];
111 #if defined(WOLFSSL_TRACK_MEMORY) 117 static pthread_mutex_t memLock = PTHREAD_MUTEX_INITIALIZER;
125 #ifdef WOLFSSL_DEBUG_MEMORY 126 WOLFSSL_LOCAL
void* TrackMalloc(
size_t sz,
const char* func,
unsigned int line);
127 WOLFSSL_LOCAL
void TrackFree(
void* ptr,
const char* func,
unsigned int line);
128 WOLFSSL_LOCAL
void* TrackRealloc(
void* ptr,
size_t sz,
const char* func,
unsigned int line);
130 WOLFSSL_LOCAL
void* TrackMalloc(
size_t sz);
131 WOLFSSL_LOCAL
void TrackFree(
void* ptr);
132 WOLFSSL_LOCAL
void* TrackRealloc(
void* ptr,
size_t sz);
134 WOLFSSL_LOCAL
int InitMemoryTracker(
void);
135 WOLFSSL_LOCAL
void ShowMemoryTracker(
void);
137 #define WC_STATIC static 140 #ifdef WOLFSSL_DEBUG_MEMORY 141 WC_STATIC WC_INLINE
void* TrackMalloc(
size_t sz,
const char* func,
unsigned int line)
143 WC_STATIC WC_INLINE
void* TrackMalloc(
size_t sz)
156 header = &mt->u.hint;
157 header->thisSize = sz;
158 header->thisMemory = (byte*)mt +
sizeof(
memoryTrack);
160 #ifdef WOLFSSL_DEBUG_MEMORY 161 #ifdef WOLFSSL_DEBUG_MEMORY_PRINT 162 printf(
"Alloc: %p -> %u at %s:%d\n", header->thisMemory, (word32)sz, func, line);
170 ourMemStats.totalAllocs++;
171 ourMemStats.totalBytes += sz;
172 ourMemStats.currentBytes += sz;
173 if (ourMemStats.currentBytes > ourMemStats.peakBytes)
174 ourMemStats.peakBytes = ourMemStats.currentBytes;
177 if (pthread_mutex_lock(&memLock) == 0) {
178 #ifdef WOLFSSL_DEBUG_MEMORY 185 if (ourMemList.tail == NULL) {
186 ourMemList.head = header;
190 ourMemList.tail->next = header;
191 header->prev = ourMemList.tail;
193 ourMemList.tail = header;
196 pthread_mutex_unlock(&memLock);
200 return header->thisMemory;
204 #ifdef WOLFSSL_DEBUG_MEMORY 205 WC_STATIC WC_INLINE
void TrackFree(
void* ptr,
const char* func,
unsigned int line)
207 WC_STATIC WC_INLINE
void TrackFree(
void* ptr)
219 header = &mt->u.hint;
220 sz = header->thisSize;
223 if (pthread_mutex_lock(&memLock) == 0)
228 ourMemStats.currentBytes -= header->thisSize;
229 ourMemStats.totalDeallocs++;
233 if (header == ourMemList.head && header == ourMemList.tail) {
234 ourMemList.head = NULL;
235 ourMemList.tail = NULL;
237 else if (header == ourMemList.head) {
238 ourMemList.head = header->next;
239 ourMemList.head->prev = NULL;
241 else if (header == ourMemList.tail) {
242 ourMemList.tail = header->prev;
243 ourMemList.tail->next = NULL;
255 pthread_mutex_unlock(&memLock);
259 #ifdef WOLFSSL_DEBUG_MEMORY 260 #ifdef WOLFSSL_DEBUG_MEMORY_PRINT 261 printf(
"Free: %p -> %u at %s:%d\n", ptr, (word32)sz, func, line);
273 #ifdef WOLFSSL_DEBUG_MEMORY 274 WC_STATIC WC_INLINE
void* TrackRealloc(
void* ptr,
size_t sz,
const char* func,
unsigned int line)
276 WC_STATIC WC_INLINE
void* TrackRealloc(
void* ptr,
size_t sz)
279 #ifdef WOLFSSL_DEBUG_MEMORY 280 void* ret = TrackMalloc(sz, func, line);
282 void* ret = TrackMalloc(sz);
291 header = &mt->u.hint;
293 if (header->thisSize < sz)
294 sz = header->thisSize;
298 XMEMCPY(ret, ptr, sz);
301 #ifdef WOLFSSL_DEBUG_MEMORY 302 TrackFree(ptr, func, line);
311 #ifdef WOLFSSL_TRACK_MEMORY 312 static wolfSSL_Malloc_cb mfDefault = NULL;
313 static wolfSSL_Free_cb ffDefault = NULL;
314 static wolfSSL_Realloc_cb rfDefault = NULL;
316 WC_STATIC WC_INLINE
int InitMemoryTracker(
void)
320 ret = wolfSSL_GetAllocators(&mfDefault, &ffDefault, &rfDefault);
322 printf(
"wolfSSL GetAllocators failed to get the defaults\n");
326 printf(
"wolfSSL SetAllocators failed for track memory\n");
331 if (pthread_mutex_lock(&memLock) == 0)
336 ourMemStats.totalAllocs = 0;
337 ourMemStats.totalDeallocs = 0;
338 ourMemStats.totalBytes = 0;
339 ourMemStats.peakBytes = 0;
340 ourMemStats.currentBytes = 0;
344 XMEMSET(&ourMemList, 0,
sizeof(ourMemList));
346 pthread_mutex_unlock(&memLock);
353 WC_STATIC WC_INLINE
void ShowMemoryTracker(
void)
356 if (pthread_mutex_lock(&memLock) == 0)
361 printf(
"total Allocs = %9ld\n", ourMemStats.totalAllocs);
362 printf(
"total Deallocs = %9ld\n", ourMemStats.totalDeallocs);
363 printf(
"total Bytes = %9ld\n", ourMemStats.totalBytes);
364 printf(
"peak Bytes = %9ld\n", ourMemStats.peakBytes);
365 printf(
"current Bytes = %9ld\n", ourMemStats.currentBytes);
369 if (ourMemList.count > 0) {
372 for (header = ourMemList.head; header != NULL; header = header->next) {
373 printf(
"Leak: Ptr %p, Size %u" 374 #ifdef WOLFSSL_DEBUG_MEMORY
378 (byte*)header +
sizeof(
memHint), (
unsigned int)header->thisSize
379 #ifdef WOLFSSL_DEBUG_MEMORY
380 , header->func, header->line
386 pthread_mutex_unlock(&memLock);
391 WC_STATIC WC_INLINE
int CleanupMemoryTracker(
void)
Definition: mem_track.h:95
Definition: mem_track.h:81
Definition: mem_track.h:73
Definition: mem_track.h:104
WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb, wolfSSL_Free_cb, wolfSSL_Realloc_cb)
This function registers the allocation functions used by wolfSSL. By default, if the system supports ...
Definition: memory.c:102