[401] | 1 | #ifdef XIOS_MEMTRACK |
---|
| 2 | /* |
---|
| 3 | Copyright (c) 2002, 2008 Curtis Bartley |
---|
| 4 | All rights reserved. |
---|
| 5 | Redistribution and use in source and binary forms, with or without |
---|
| 6 | modification, are permitted provided that the following conditions |
---|
| 7 | are met: |
---|
| 8 | |
---|
| 9 | - Redistributions of source code must retain the above copyright |
---|
| 10 | notice, this list of conditions and the following disclaimer. |
---|
| 11 | |
---|
| 12 | - Redistributions in binary form must reproduce the above copyright |
---|
| 13 | notice, this list of conditions and the following disclaimer in the |
---|
| 14 | documentation and/or other materials provided with the |
---|
| 15 | distribution. |
---|
| 16 | |
---|
| 17 | - Neither the name of Curtis Bartley nor the names of any other |
---|
| 18 | contributors may be used to endorse or promote products derived |
---|
| 19 | from this software without specific prior written permission. |
---|
| 20 | |
---|
| 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 22 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 23 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
---|
| 24 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
---|
| 25 | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
| 26 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
| 27 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
---|
| 28 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 29 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
---|
| 30 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
---|
| 32 | OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #ifndef MemTrack_H_ |
---|
| 36 | #define MemTrack_H_ |
---|
| 37 | |
---|
| 38 | #include <typeinfo> |
---|
| 39 | #include <cstring> |
---|
[2212] | 40 | #include <fstream> |
---|
[401] | 41 | |
---|
| 42 | namespace MemTrack |
---|
| 43 | { |
---|
| 44 | /* ---------------------------------------- class MemStamp */ |
---|
| 45 | |
---|
| 46 | class MemStamp |
---|
| 47 | { |
---|
| 48 | public: // member variables |
---|
| 49 | char const * const filename; |
---|
| 50 | int const lineNum; |
---|
| 51 | public: // construction/destruction |
---|
| 52 | MemStamp(char const *filename, int lineNum) |
---|
| 53 | : filename(filename), lineNum(lineNum) { } |
---|
| 54 | ~MemStamp() { } |
---|
| 55 | }; |
---|
| 56 | |
---|
| 57 | /* ---------------------------------------- memory allocation and stamping prototypes */ |
---|
| 58 | |
---|
| 59 | void *TrackMalloc(size_t size); |
---|
| 60 | void TrackFree(void *p); |
---|
| 61 | void TrackStamp(void *p, const MemStamp &stamp, char const *typeName); |
---|
[2212] | 62 | void TrackDumpBlocks(std::ofstream& memReport, size_t memtrack_blocks, size_t memtrack_size); |
---|
[401] | 63 | void TrackListMemoryUsage(); |
---|
[1158] | 64 | size_t getCurrentMemorySize(void) ; |
---|
| 65 | size_t getMaxMemorySize(void) ; |
---|
[401] | 66 | /* ---------------------------------------- operator * (MemStamp, ptr) */ |
---|
| 67 | |
---|
| 68 | template <class T> inline T *operator*(const MemStamp &stamp, T *p) |
---|
| 69 | { |
---|
| 70 | TrackStamp(p, stamp, typeid(T).name()); |
---|
| 71 | return p; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | } // namespace MemTrack |
---|
| 75 | |
---|
| 76 | /* ---------------------------------------- new macro */ |
---|
| 77 | |
---|
| 78 | #define MEMTRACK_NEW MemTrack::MemStamp(__FILE__, __LINE__) * new |
---|
| 79 | #define new MEMTRACK_NEW |
---|
| 80 | |
---|
| 81 | #endif // MemTrack_H_ |
---|
| 82 | |
---|
| 83 | #endif |
---|