Renesas / SecureDweet
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mem_track.h Source File

mem_track.h

00001 /* mem_track.h
00002  *
00003  * Copyright (C) 2006-2016 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 
00023 
00024 #ifndef WOLFSSL_MEM_TRACK_H
00025 #define WOLFSSL_MEM_TRACK_H
00026 
00027 #ifdef USE_WOLFSSL_MEMORY
00028 
00029     #include "wolfssl/wolfcrypt/logging.h"
00030 
00031     typedef struct memoryStats {
00032         size_t totalAllocs;     /* number of allocations */
00033         size_t totalBytes;      /* total number of bytes allocated */
00034         size_t peakBytes;       /* concurrent max bytes */
00035         size_t currentBytes;    /* total current bytes in use */
00036     } memoryStats;
00037 
00038     typedef struct memHint {
00039         size_t thisSize;      /* size of this memory */
00040         void*  thisMemory;    /* actual memory for user */
00041     } memHint;
00042 
00043     typedef struct memoryTrack {
00044         union {
00045             memHint hint;
00046             byte    alignit[16];   /* make sure we have strong alignment */
00047         } u;
00048     } memoryTrack;
00049 
00050     #if defined(WOLFSSL_TRACK_MEMORY)
00051         #define DO_MEM_STATS
00052         static memoryStats ourMemStats;
00053     #endif
00054 
00055     static INLINE void* TrackMalloc(size_t sz)
00056     {
00057         memoryTrack* mt;
00058 
00059         if (sz == 0)
00060             return NULL;
00061 
00062         mt = (memoryTrack*)malloc(sizeof(memoryTrack) + sz);
00063         if (mt == NULL)
00064             return NULL;
00065 
00066         mt->u.hint.thisSize   = sz;
00067         mt->u.hint.thisMemory = (byte*)mt + sizeof(memoryTrack);
00068 
00069 #ifdef DO_MEM_STATS
00070         ourMemStats.totalAllocs++;
00071         ourMemStats.totalBytes   += sz;
00072         ourMemStats.currentBytes += sz;
00073         if (ourMemStats.currentBytes > ourMemStats.peakBytes)
00074             ourMemStats.peakBytes = ourMemStats.currentBytes;
00075 #endif
00076 
00077         return mt->u.hint.thisMemory;
00078     }
00079 
00080 
00081     static INLINE void TrackFree(void* ptr)
00082     {
00083         memoryTrack* mt;
00084 
00085         if (ptr == NULL) {
00086             return;
00087         }
00088 
00089         mt = (memoryTrack*)ptr;
00090         --mt;   /* same as minus sizeof(memoryTrack), removes header */
00091 
00092 #ifdef DO_MEM_STATS
00093         ourMemStats.currentBytes -= mt->u.hint.thisSize;
00094 #endif
00095 
00096         free(mt);
00097     }
00098 
00099 
00100     static INLINE void* TrackRealloc(void* ptr, size_t sz)
00101     {
00102         void* ret = TrackMalloc(sz);
00103 
00104         if (ptr) {
00105             /* if realloc is bigger, don't overread old ptr */
00106             memoryTrack* mt = (memoryTrack*)ptr;
00107             --mt;  /* same as minus sizeof(memoryTrack), removes header */
00108 
00109             if (mt->u.hint.thisSize < sz)
00110                 sz = mt->u.hint.thisSize;
00111         }
00112 
00113         if (ret && ptr)
00114             memcpy(ret, ptr, sz);
00115 
00116         if (ret)
00117             TrackFree(ptr);
00118 
00119         return ret;
00120     }
00121 
00122     static INLINE int InitMemoryTracker(void)
00123     {
00124         int ret = wolfSSL_SetAllocators(TrackMalloc, TrackFree, TrackRealloc);
00125         if (ret < 0) {
00126             printf("wolfSSL SetAllocators failed for track memory\n");
00127             return ret;
00128         }
00129 
00130     #ifdef DO_MEM_STATS
00131         ourMemStats.totalAllocs  = 0;
00132         ourMemStats.totalBytes   = 0;
00133         ourMemStats.peakBytes    = 0;
00134         ourMemStats.currentBytes = 0;
00135     #endif
00136         
00137         return ret;
00138     }
00139 
00140     static INLINE void ShowMemoryTracker(void)
00141     {
00142     #ifdef DO_MEM_STATS
00143         printf("total   Allocs = %9lu\n",
00144                                        (unsigned long)ourMemStats.totalAllocs);
00145         printf("total   Bytes  = %9lu\n",
00146                                        (unsigned long)ourMemStats.totalBytes);
00147         printf("peak    Bytes  = %9lu\n",
00148                                        (unsigned long)ourMemStats.peakBytes);
00149         printf("current Bytes  = %9lu\n",
00150                                        (unsigned long)ourMemStats.currentBytes);
00151     #endif
00152     }
00153 
00154 #endif /* USE_WOLFSSL_MEMORY */
00155 
00156 #endif /* WOLFSSL_MEM_TRACK_H */
00157