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