ssh lib
wolfcrypt/mem_track.h@1:e4ea39eba2fb, 2019-11-25 (annotated)
- Committer:
- sPymbed
- Date:
- Mon Nov 25 14:23:49 2019 +0000
- Revision:
- 1:e4ea39eba2fb
- Parent:
- 0:1387ff3eed4a
improved
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sPymbed | 0:1387ff3eed4a | 1 | /* mem_track.h |
sPymbed | 0:1387ff3eed4a | 2 | * |
sPymbed | 0:1387ff3eed4a | 3 | * Copyright (C) 2006-2017 wolfSSL Inc. |
sPymbed | 0:1387ff3eed4a | 4 | * |
sPymbed | 0:1387ff3eed4a | 5 | * This file is part of wolfSSL. |
sPymbed | 0:1387ff3eed4a | 6 | * |
sPymbed | 0:1387ff3eed4a | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
sPymbed | 0:1387ff3eed4a | 8 | * it under the terms of the GNU General Public License as published by |
sPymbed | 0:1387ff3eed4a | 9 | * the Free Software Foundation; either version 2 of the License, or |
sPymbed | 0:1387ff3eed4a | 10 | * (at your option) any later version. |
sPymbed | 0:1387ff3eed4a | 11 | * |
sPymbed | 0:1387ff3eed4a | 12 | * wolfSSL is distributed in the hope that it will be useful, |
sPymbed | 0:1387ff3eed4a | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
sPymbed | 0:1387ff3eed4a | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
sPymbed | 0:1387ff3eed4a | 15 | * GNU General Public License for more details. |
sPymbed | 0:1387ff3eed4a | 16 | * |
sPymbed | 0:1387ff3eed4a | 17 | * You should have received a copy of the GNU General Public License |
sPymbed | 0:1387ff3eed4a | 18 | * along with this program; if not, write to the Free Software |
sPymbed | 0:1387ff3eed4a | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
sPymbed | 0:1387ff3eed4a | 20 | */ |
sPymbed | 0:1387ff3eed4a | 21 | |
sPymbed | 0:1387ff3eed4a | 22 | |
sPymbed | 0:1387ff3eed4a | 23 | /* The memory tracker overrides the wolfSSL memory callback system and uses a |
sPymbed | 0:1387ff3eed4a | 24 | * static to track the total, peak and currently allocated bytes. |
sPymbed | 0:1387ff3eed4a | 25 | * |
sPymbed | 0:1387ff3eed4a | 26 | * If you are already using the memory callbacks then enabling this will |
sPymbed | 0:1387ff3eed4a | 27 | * override the memory callbacks and prevent your memory callbacks from |
sPymbed | 0:1387ff3eed4a | 28 | * working. This assumes malloc() and free() are available. Feel free to |
sPymbed | 0:1387ff3eed4a | 29 | * customize this for your needs. |
sPymbed | 0:1387ff3eed4a | 30 | |
sPymbed | 0:1387ff3eed4a | 31 | * The enable this feature define the following: |
sPymbed | 0:1387ff3eed4a | 32 | * #define USE_WOLFSSL_MEMORY |
sPymbed | 0:1387ff3eed4a | 33 | * #define WOLFSSL_TRACK_MEMORY |
sPymbed | 0:1387ff3eed4a | 34 | * |
sPymbed | 0:1387ff3eed4a | 35 | * On startup call: |
sPymbed | 0:1387ff3eed4a | 36 | * InitMemoryTracker(); |
sPymbed | 0:1387ff3eed4a | 37 | * |
sPymbed | 0:1387ff3eed4a | 38 | * When ready to dump the memory report call: |
sPymbed | 0:1387ff3eed4a | 39 | * ShowMemoryTracker(); |
sPymbed | 0:1387ff3eed4a | 40 | * |
sPymbed | 0:1387ff3eed4a | 41 | * Report example: |
sPymbed | 0:1387ff3eed4a | 42 | * total Allocs = 228 |
sPymbed | 0:1387ff3eed4a | 43 | * total Bytes = 93442 |
sPymbed | 0:1387ff3eed4a | 44 | * peak Bytes = 8840 |
sPymbed | 0:1387ff3eed4a | 45 | * current Bytes = 0 |
sPymbed | 0:1387ff3eed4a | 46 | * |
sPymbed | 0:1387ff3eed4a | 47 | * |
sPymbed | 0:1387ff3eed4a | 48 | * You can also: |
sPymbed | 0:1387ff3eed4a | 49 | * #define WOLFSSL_DEBUG_MEMORY |
sPymbed | 0:1387ff3eed4a | 50 | * |
sPymbed | 0:1387ff3eed4a | 51 | * To print every alloc/free along with the function and line number. |
sPymbed | 0:1387ff3eed4a | 52 | * Example output: |
sPymbed | 0:1387ff3eed4a | 53 | * Alloc: 0x7fa14a500010 -> 120 at wc_InitRng:496 |
sPymbed | 0:1387ff3eed4a | 54 | * Free: 0x7fa14a500010 -> 120 at wc_FreeRng:606 |
sPymbed | 0:1387ff3eed4a | 55 | */ |
sPymbed | 0:1387ff3eed4a | 56 | |
sPymbed | 0:1387ff3eed4a | 57 | |
sPymbed | 0:1387ff3eed4a | 58 | #ifndef WOLFSSL_MEM_TRACK_H |
sPymbed | 0:1387ff3eed4a | 59 | #define WOLFSSL_MEM_TRACK_H |
sPymbed | 0:1387ff3eed4a | 60 | |
sPymbed | 0:1387ff3eed4a | 61 | #if defined(USE_WOLFSSL_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY) |
sPymbed | 0:1387ff3eed4a | 62 | |
sPymbed | 0:1387ff3eed4a | 63 | #include "wolfcrypt/logging.h" |
sPymbed | 0:1387ff3eed4a | 64 | |
sPymbed | 0:1387ff3eed4a | 65 | typedef struct memoryStats { |
sPymbed | 0:1387ff3eed4a | 66 | size_t totalAllocs; /* number of allocations */ |
sPymbed | 0:1387ff3eed4a | 67 | size_t totalDeallocs; /* number of deallocations */ |
sPymbed | 0:1387ff3eed4a | 68 | size_t totalBytes; /* total number of bytes allocated */ |
sPymbed | 0:1387ff3eed4a | 69 | size_t peakBytes; /* concurrent max bytes */ |
sPymbed | 0:1387ff3eed4a | 70 | size_t currentBytes; /* total current bytes in use */ |
sPymbed | 0:1387ff3eed4a | 71 | } memoryStats; |
sPymbed | 0:1387ff3eed4a | 72 | |
sPymbed | 0:1387ff3eed4a | 73 | typedef struct memHint { |
sPymbed | 0:1387ff3eed4a | 74 | size_t thisSize; /* size of this memory */ |
sPymbed | 0:1387ff3eed4a | 75 | void* thisMemory; /* actual memory for user */ |
sPymbed | 0:1387ff3eed4a | 76 | } memHint; |
sPymbed | 0:1387ff3eed4a | 77 | |
sPymbed | 0:1387ff3eed4a | 78 | typedef struct memoryTrack { |
sPymbed | 0:1387ff3eed4a | 79 | union { |
sPymbed | 0:1387ff3eed4a | 80 | memHint hint; |
sPymbed | 0:1387ff3eed4a | 81 | byte alignit[16]; /* make sure we have strong alignment */ |
sPymbed | 0:1387ff3eed4a | 82 | } u; |
sPymbed | 0:1387ff3eed4a | 83 | } memoryTrack; |
sPymbed | 0:1387ff3eed4a | 84 | |
sPymbed | 0:1387ff3eed4a | 85 | #if defined(WOLFSSL_TRACK_MEMORY) |
sPymbed | 0:1387ff3eed4a | 86 | #define DO_MEM_STATS |
sPymbed | 0:1387ff3eed4a | 87 | static memoryStats ourMemStats; |
sPymbed | 0:1387ff3eed4a | 88 | #endif |
sPymbed | 0:1387ff3eed4a | 89 | |
sPymbed | 0:1387ff3eed4a | 90 | /* if defined to not using inline then declare function prototypes */ |
sPymbed | 0:1387ff3eed4a | 91 | #ifdef NO_INLINE |
sPymbed | 0:1387ff3eed4a | 92 | #define STATIC |
sPymbed | 0:1387ff3eed4a | 93 | #ifdef WOLFSSL_DEBUG_MEMORY |
sPymbed | 0:1387ff3eed4a | 94 | WOLFSSL_LOCAL void* TrackMalloc(size_t sz, const char* func, unsigned int line); |
sPymbed | 0:1387ff3eed4a | 95 | WOLFSSL_LOCAL void TrackFree(void* ptr, const char* func, unsigned int line); |
sPymbed | 0:1387ff3eed4a | 96 | WOLFSSL_LOCAL void* TrackRealloc(void* ptr, size_t sz, const char* func, unsigned int line); |
sPymbed | 0:1387ff3eed4a | 97 | #else |
sPymbed | 0:1387ff3eed4a | 98 | WOLFSSL_LOCAL void* TrackMalloc(size_t sz); |
sPymbed | 0:1387ff3eed4a | 99 | WOLFSSL_LOCAL void TrackFree(void* ptr); |
sPymbed | 0:1387ff3eed4a | 100 | WOLFSSL_LOCAL void* TrackRealloc(void* ptr, size_t sz); |
sPymbed | 0:1387ff3eed4a | 101 | #endif |
sPymbed | 0:1387ff3eed4a | 102 | WOLFSSL_LOCAL int InitMemoryTracker(void); |
sPymbed | 0:1387ff3eed4a | 103 | WOLFSSL_LOCAL void ShowMemoryTracker(void); |
sPymbed | 0:1387ff3eed4a | 104 | #else |
sPymbed | 0:1387ff3eed4a | 105 | #define STATIC static |
sPymbed | 0:1387ff3eed4a | 106 | #endif |
sPymbed | 0:1387ff3eed4a | 107 | |
sPymbed | 0:1387ff3eed4a | 108 | #ifdef WOLFSSL_DEBUG_MEMORY |
sPymbed | 0:1387ff3eed4a | 109 | STATIC WC_INLINE void* TrackMalloc(size_t sz, const char* func, unsigned int line) |
sPymbed | 0:1387ff3eed4a | 110 | #else |
sPymbed | 0:1387ff3eed4a | 111 | STATIC WC_INLINE void* TrackMalloc(size_t sz) |
sPymbed | 0:1387ff3eed4a | 112 | #endif |
sPymbed | 0:1387ff3eed4a | 113 | { |
sPymbed | 0:1387ff3eed4a | 114 | memoryTrack* mt; |
sPymbed | 0:1387ff3eed4a | 115 | |
sPymbed | 0:1387ff3eed4a | 116 | if (sz == 0) |
sPymbed | 0:1387ff3eed4a | 117 | return NULL; |
sPymbed | 0:1387ff3eed4a | 118 | |
sPymbed | 0:1387ff3eed4a | 119 | mt = (memoryTrack*)malloc(sizeof(memoryTrack) + sz); |
sPymbed | 0:1387ff3eed4a | 120 | if (mt == NULL) |
sPymbed | 0:1387ff3eed4a | 121 | return NULL; |
sPymbed | 0:1387ff3eed4a | 122 | |
sPymbed | 0:1387ff3eed4a | 123 | mt->u.hint.thisSize = sz; |
sPymbed | 0:1387ff3eed4a | 124 | mt->u.hint.thisMemory = (byte*)mt + sizeof(memoryTrack); |
sPymbed | 0:1387ff3eed4a | 125 | |
sPymbed | 0:1387ff3eed4a | 126 | #ifdef WOLFSSL_DEBUG_MEMORY |
sPymbed | 0:1387ff3eed4a | 127 | printf("Alloc: %p -> %u at %s:%d\n", mt->u.hint.thisMemory, (word32)sz, func, line); |
sPymbed | 0:1387ff3eed4a | 128 | #endif |
sPymbed | 0:1387ff3eed4a | 129 | |
sPymbed | 0:1387ff3eed4a | 130 | #ifdef DO_MEM_STATS |
sPymbed | 0:1387ff3eed4a | 131 | ourMemStats.totalAllocs++; |
sPymbed | 0:1387ff3eed4a | 132 | ourMemStats.totalBytes += sz; |
sPymbed | 0:1387ff3eed4a | 133 | ourMemStats.currentBytes += sz; |
sPymbed | 0:1387ff3eed4a | 134 | if (ourMemStats.currentBytes > ourMemStats.peakBytes) |
sPymbed | 0:1387ff3eed4a | 135 | ourMemStats.peakBytes = ourMemStats.currentBytes; |
sPymbed | 0:1387ff3eed4a | 136 | #endif |
sPymbed | 0:1387ff3eed4a | 137 | |
sPymbed | 0:1387ff3eed4a | 138 | return mt->u.hint.thisMemory; |
sPymbed | 0:1387ff3eed4a | 139 | } |
sPymbed | 0:1387ff3eed4a | 140 | |
sPymbed | 0:1387ff3eed4a | 141 | |
sPymbed | 0:1387ff3eed4a | 142 | #ifdef WOLFSSL_DEBUG_MEMORY |
sPymbed | 0:1387ff3eed4a | 143 | STATIC WC_INLINE void TrackFree(void* ptr, const char* func, unsigned int line) |
sPymbed | 0:1387ff3eed4a | 144 | #else |
sPymbed | 0:1387ff3eed4a | 145 | STATIC WC_INLINE void TrackFree(void* ptr) |
sPymbed | 0:1387ff3eed4a | 146 | #endif |
sPymbed | 0:1387ff3eed4a | 147 | { |
sPymbed | 0:1387ff3eed4a | 148 | memoryTrack* mt; |
sPymbed | 0:1387ff3eed4a | 149 | |
sPymbed | 0:1387ff3eed4a | 150 | if (ptr == NULL) { |
sPymbed | 0:1387ff3eed4a | 151 | return; |
sPymbed | 0:1387ff3eed4a | 152 | } |
sPymbed | 0:1387ff3eed4a | 153 | |
sPymbed | 0:1387ff3eed4a | 154 | mt = (memoryTrack*)ptr; |
sPymbed | 0:1387ff3eed4a | 155 | --mt; /* same as minus sizeof(memoryTrack), removes header */ |
sPymbed | 0:1387ff3eed4a | 156 | |
sPymbed | 0:1387ff3eed4a | 157 | #ifdef DO_MEM_STATS |
sPymbed | 0:1387ff3eed4a | 158 | ourMemStats.currentBytes -= mt->u.hint.thisSize; |
sPymbed | 0:1387ff3eed4a | 159 | ourMemStats.totalDeallocs++; |
sPymbed | 0:1387ff3eed4a | 160 | #endif |
sPymbed | 0:1387ff3eed4a | 161 | |
sPymbed | 0:1387ff3eed4a | 162 | #ifdef WOLFSSL_DEBUG_MEMORY |
sPymbed | 0:1387ff3eed4a | 163 | printf("Free: %p -> %u at %s:%d\n", ptr, (word32)mt->u.hint.thisSize, func, line); |
sPymbed | 0:1387ff3eed4a | 164 | #endif |
sPymbed | 0:1387ff3eed4a | 165 | |
sPymbed | 0:1387ff3eed4a | 166 | free(mt); |
sPymbed | 0:1387ff3eed4a | 167 | } |
sPymbed | 0:1387ff3eed4a | 168 | |
sPymbed | 0:1387ff3eed4a | 169 | |
sPymbed | 0:1387ff3eed4a | 170 | #ifdef WOLFSSL_DEBUG_MEMORY |
sPymbed | 0:1387ff3eed4a | 171 | STATIC WC_INLINE void* TrackRealloc(void* ptr, size_t sz, const char* func, unsigned int line) |
sPymbed | 0:1387ff3eed4a | 172 | #else |
sPymbed | 0:1387ff3eed4a | 173 | STATIC WC_INLINE void* TrackRealloc(void* ptr, size_t sz) |
sPymbed | 0:1387ff3eed4a | 174 | #endif |
sPymbed | 0:1387ff3eed4a | 175 | { |
sPymbed | 0:1387ff3eed4a | 176 | #ifdef WOLFSSL_DEBUG_MEMORY |
sPymbed | 0:1387ff3eed4a | 177 | void* ret = TrackMalloc(sz, func, line); |
sPymbed | 0:1387ff3eed4a | 178 | #else |
sPymbed | 0:1387ff3eed4a | 179 | void* ret = TrackMalloc(sz); |
sPymbed | 0:1387ff3eed4a | 180 | #endif |
sPymbed | 0:1387ff3eed4a | 181 | |
sPymbed | 0:1387ff3eed4a | 182 | if (ptr) { |
sPymbed | 0:1387ff3eed4a | 183 | /* if realloc is bigger, don't overread old ptr */ |
sPymbed | 0:1387ff3eed4a | 184 | memoryTrack* mt = (memoryTrack*)ptr; |
sPymbed | 0:1387ff3eed4a | 185 | --mt; /* same as minus sizeof(memoryTrack), removes header */ |
sPymbed | 0:1387ff3eed4a | 186 | |
sPymbed | 0:1387ff3eed4a | 187 | if (mt->u.hint.thisSize < sz) |
sPymbed | 0:1387ff3eed4a | 188 | sz = mt->u.hint.thisSize; |
sPymbed | 0:1387ff3eed4a | 189 | } |
sPymbed | 0:1387ff3eed4a | 190 | |
sPymbed | 0:1387ff3eed4a | 191 | if (ret && ptr) |
sPymbed | 0:1387ff3eed4a | 192 | XMEMCPY(ret, ptr, sz); |
sPymbed | 0:1387ff3eed4a | 193 | |
sPymbed | 0:1387ff3eed4a | 194 | if (ret) { |
sPymbed | 0:1387ff3eed4a | 195 | #ifdef WOLFSSL_DEBUG_MEMORY |
sPymbed | 0:1387ff3eed4a | 196 | TrackFree(ptr, func, line); |
sPymbed | 0:1387ff3eed4a | 197 | #else |
sPymbed | 0:1387ff3eed4a | 198 | TrackFree(ptr); |
sPymbed | 0:1387ff3eed4a | 199 | #endif |
sPymbed | 0:1387ff3eed4a | 200 | } |
sPymbed | 0:1387ff3eed4a | 201 | |
sPymbed | 0:1387ff3eed4a | 202 | return ret; |
sPymbed | 0:1387ff3eed4a | 203 | } |
sPymbed | 0:1387ff3eed4a | 204 | |
sPymbed | 0:1387ff3eed4a | 205 | #ifdef WOLFSSL_TRACK_MEMORY |
sPymbed | 0:1387ff3eed4a | 206 | STATIC WC_INLINE int InitMemoryTracker(void) |
sPymbed | 0:1387ff3eed4a | 207 | { |
sPymbed | 0:1387ff3eed4a | 208 | int ret = wolfSSL_SetAllocators(TrackMalloc, TrackFree, TrackRealloc); |
sPymbed | 0:1387ff3eed4a | 209 | if (ret < 0) { |
sPymbed | 0:1387ff3eed4a | 210 | printf("wolfSSL SetAllocators failed for track memory\n"); |
sPymbed | 0:1387ff3eed4a | 211 | return ret; |
sPymbed | 0:1387ff3eed4a | 212 | } |
sPymbed | 0:1387ff3eed4a | 213 | |
sPymbed | 0:1387ff3eed4a | 214 | #ifdef DO_MEM_STATS |
sPymbed | 0:1387ff3eed4a | 215 | ourMemStats.totalAllocs = 0; |
sPymbed | 0:1387ff3eed4a | 216 | ourMemStats.totalDeallocs = 0; |
sPymbed | 0:1387ff3eed4a | 217 | ourMemStats.totalBytes = 0; |
sPymbed | 0:1387ff3eed4a | 218 | ourMemStats.peakBytes = 0; |
sPymbed | 0:1387ff3eed4a | 219 | ourMemStats.currentBytes = 0; |
sPymbed | 0:1387ff3eed4a | 220 | #endif |
sPymbed | 0:1387ff3eed4a | 221 | |
sPymbed | 0:1387ff3eed4a | 222 | return ret; |
sPymbed | 0:1387ff3eed4a | 223 | } |
sPymbed | 0:1387ff3eed4a | 224 | |
sPymbed | 0:1387ff3eed4a | 225 | STATIC WC_INLINE void ShowMemoryTracker(void) |
sPymbed | 0:1387ff3eed4a | 226 | { |
sPymbed | 0:1387ff3eed4a | 227 | #ifdef DO_MEM_STATS |
sPymbed | 0:1387ff3eed4a | 228 | printf("total Allocs = %9lu\n", |
sPymbed | 0:1387ff3eed4a | 229 | (unsigned long)ourMemStats.totalAllocs); |
sPymbed | 0:1387ff3eed4a | 230 | printf("total Deallocs = %9lu\n", |
sPymbed | 0:1387ff3eed4a | 231 | (unsigned long)ourMemStats.totalDeallocs); |
sPymbed | 0:1387ff3eed4a | 232 | printf("total Bytes = %9lu\n", |
sPymbed | 0:1387ff3eed4a | 233 | (unsigned long)ourMemStats.totalBytes); |
sPymbed | 0:1387ff3eed4a | 234 | printf("peak Bytes = %9lu\n", |
sPymbed | 0:1387ff3eed4a | 235 | (unsigned long)ourMemStats.peakBytes); |
sPymbed | 0:1387ff3eed4a | 236 | printf("current Bytes = %9lu\n", |
sPymbed | 0:1387ff3eed4a | 237 | (unsigned long)ourMemStats.currentBytes); |
sPymbed | 0:1387ff3eed4a | 238 | #endif |
sPymbed | 0:1387ff3eed4a | 239 | } |
sPymbed | 0:1387ff3eed4a | 240 | |
sPymbed | 0:1387ff3eed4a | 241 | STATIC WC_INLINE int CleanupMemoryTracker(void) |
sPymbed | 0:1387ff3eed4a | 242 | { |
sPymbed | 0:1387ff3eed4a | 243 | /* restore default allocators */ |
sPymbed | 0:1387ff3eed4a | 244 | return wolfSSL_ResetAllocators(); |
sPymbed | 0:1387ff3eed4a | 245 | } |
sPymbed | 0:1387ff3eed4a | 246 | #endif |
sPymbed | 0:1387ff3eed4a | 247 | |
sPymbed | 0:1387ff3eed4a | 248 | #endif /* USE_WOLFSSL_MEMORY */ |
sPymbed | 0:1387ff3eed4a | 249 | |
sPymbed | 0:1387ff3eed4a | 250 | #endif /* WOLFSSL_MEM_TRACK_H */ |
sPymbed | 0:1387ff3eed4a | 251 | |
sPymbed | 0:1387ff3eed4a | 252 |