| 1 | // -*- C++ -*- |
| 2 | |
| 3 | /* |
| 4 | |
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
| 6 | |
| 7 | Copyright (C) 2000-2020 by Emery Berger |
| 8 | http://www.emeryberger.com |
| 9 | [email protected] |
| 10 | |
| 11 | Heap Layers is distributed under the terms of the Apache 2.0 license. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 16 | */ |
| 17 | |
| 18 | #ifndef HL_TRACEHEAP_H |
| 19 | #define HL_TRACEHEAP_H |
| 20 | |
| 21 | #include <cstdlib> |
| 22 | |
| 23 | #ifdef WIN32 |
| 24 | #include <io.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <fcntl.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <string.h> |
| 31 | #include <cstdio> |
| 32 | |
| 33 | // An object that manages direct writing to a file. |
| 34 | |
| 35 | namespace HL { |
| 36 | |
| 37 | class FileObject { |
| 38 | public: |
| 39 | |
| 40 | FileObject() |
| 41 | : _isOpen (false), |
| 42 | _file (0) |
| 43 | {} |
| 44 | |
| 45 | FileObject (const FileObject& f) |
| 46 | : _isOpen (f._isOpen), |
| 47 | _file (f._file) |
| 48 | {} |
| 49 | |
| 50 | FileObject& operator=(const FileObject& f) { |
| 51 | _isOpen = f._isOpen; |
| 52 | _file = f._file; |
| 53 | return *this; |
| 54 | } |
| 55 | |
| 56 | ~FileObject() { |
| 57 | close(); |
| 58 | } |
| 59 | |
| 60 | void open (char * fname) |
| 61 | { |
| 62 | #if 1 |
| 63 | _file = fopen(fname, "w+"); |
| 64 | #else |
| 65 | #ifdef WIN32 |
| 66 | _file = _open(fname, _O_WRONLY | _O_CREAT, _S_IREAD | _S_IWRITE); |
| 67 | #else |
| 68 | _file = ::open(fname, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE); |
| 69 | #endif |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | bool is_open() { |
| 74 | return _isOpen; |
| 75 | } |
| 76 | |
| 77 | void close() |
| 78 | { |
| 79 | if (_isOpen) { |
| 80 | #if 1 |
| 81 | fclose (_file); |
| 82 | #else |
| 83 | #ifdef WIN32 |
| 84 | _close (_file); |
| 85 | #else |
| 86 | ::close (_file); |
| 87 | #endif |
| 88 | #endif |
| 89 | _isOpen = false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | friend FileObject& operator<<(FileObject& f, int n) { |
| 95 | char buf[255]; |
| 96 | snprintf (buf, 255, "%d", n); |
| 97 | f.writeString (buf); |
| 98 | return f; |
| 99 | } |
| 100 | |
| 101 | friend FileObject& operator<<(FileObject& f, char * s) { |
| 102 | char buf[255]; |
| 103 | snprintf (buf, 255, "%s", s); |
| 104 | f.writeString (buf); |
| 105 | return f; |
| 106 | } |
| 107 | |
| 108 | friend FileObject& operator<<(FileObject& f, void * s) { |
| 109 | char buf[255]; |
| 110 | snprintf (buf, 255, "%p", s); |
| 111 | f.writeString (buf); |
| 112 | return f; |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | |
| 117 | void writeString (char * buf) { |
| 118 | #if 1 |
| 119 | fprintf (_file, "%s", buf); |
| 120 | #else |
| 121 | #ifdef WIN32 |
| 122 | _write (_file, buf, strlen(buf)); |
| 123 | #else |
| 124 | write (_file, buf, strlen(buf)); |
| 125 | #endif |
| 126 | #endif |
| 127 | } |
| 128 | |
| 129 | bool _isOpen; |
| 130 | |
| 131 | #if 1 |
| 132 | FILE * _file; |
| 133 | #else |
| 134 | int _file; |
| 135 | #endif |
| 136 | |
| 137 | }; |
| 138 | |
| 139 | // The TraceHeap itself. |
| 140 | // Traces malloc & free calls. |
| 141 | |
| 142 | template <class Super, int Number> |
| 143 | class TraceHeap : public Super { |
| 144 | public: |
| 145 | |
| 146 | TraceHeap() |
| 147 | { |
| 148 | if (!theFile().is_open()) { |
| 149 | char fname[255]; |
| 150 | snprintf (fname, 255, "trace-%d", Number); |
| 151 | theFile().open (fname); |
| 152 | printf ("OPEN %s\n", fname); |
| 153 | } |
| 154 | getRefs()++; |
| 155 | } |
| 156 | |
| 157 | ~TraceHeap() |
| 158 | { |
| 159 | // theFile() << ::std::flush; |
| 160 | --getRefs(); |
| 161 | if (getRefs() == 0) { |
| 162 | if (theFile().is_open()) |
| 163 | theFile().close(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | inline void * malloc (size_t sz) { |
| 168 | void * ptr = Super::malloc (sz); |
| 169 | theFile() << "M " << Number << "\t" << sz << "\t" << ptr << "\n"; |
| 170 | return ptr; |
| 171 | } |
| 172 | |
| 173 | inline void free (void * ptr) { |
| 174 | theFile() << "F " << Number << "\t" << ptr << "\n"; |
| 175 | Super::free (ptr); |
| 176 | } |
| 177 | |
| 178 | private: |
| 179 | |
| 180 | FileObject& theFile() { |
| 181 | static FileObject f; |
| 182 | return f; |
| 183 | } |
| 184 | |
| 185 | int& getRefs() { |
| 186 | static int refs = 0; |
| 187 | return refs; |
| 188 | } |
| 189 | |
| 190 | }; |
| 191 | |
| 192 | } |
| 193 | |
| 194 | #endif |
| 195 | |
| 196 | #if 0 |
| 197 | /* -*- C++ -*- */ |
| 198 | #ifndef _TRACEHEAP_H_ |
| 199 | #define _TRACEHEAP_H_ |
| 200 | // #include <iostream.h> |
| 201 | //#include <fstream.h> |
| 202 | #include <fstream> |
| 203 | #include <map> |
| 204 | |
| 205 | template <class Super, int Number> |
| 206 | class TraceHeap : public Super { |
| 207 | public: |
| 208 | |
| 209 | TraceHeap() |
| 210 | { |
| 211 | if (!theFile().is_open()) { |
| 212 | printf ("OPEN\n"); |
| 213 | theFile().open ("trace"); |
| 214 | } |
| 215 | getRefs()++; |
| 216 | } |
| 217 | ~TraceHeap() |
| 218 | { |
| 219 | theFile() << ::std::flush; |
| 220 | --getRefs(); |
| 221 | if (getRefs() == 0) { |
| 222 | if (theFile().is_open()) |
| 223 | theFile().close(); |
| 224 | } |
| 225 | } |
| 226 | inline void * malloc (size_t sz) { |
| 227 | void * ptr = Super::malloc (sz); |
| 228 | theFile() << "M\t" << sz << "\t" << ptr << "\n"; |
| 229 | return ptr; |
| 230 | } |
| 231 | |
| 232 | inline void free (void * ptr) { |
| 233 | theFile() << "F\t" << ptr << "\n"; |
| 234 | Super::free (ptr); |
| 235 | } |
| 236 | private: |
| 237 | std::ofstream& theFile() { |
| 238 | static std::ofstream f; |
| 239 | return f; |
| 240 | } |
| 241 | int& getRefs() { |
| 242 | static int refs = 0; |
| 243 | return refs; |
| 244 | } |
| 245 | }; |
| 246 | |
| 247 | #endif |
| 248 | #endif |