wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue May 02 08:44:26 2017 +0000
Revision:
6:fa3bd0ca5896
wolfSSL3.10.2;

Who changed what in which revision?

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