| 1 | // -*- C++ -*-cxx . . $ `<file>/vendor/Heap-Layers/heaps/utility/traceheap.h`/ |
| 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_Hcxx . . $ `vendor/Heap-Layers/heaps/utility/traceheap.h:19:9`! |
| 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 HLcxx . . $ HL/ { |
| 36 | |
| 37 | class FileObjectcxx . . $ HL/FileObject# { |
| 38 | public: |
| 39 | |
| 40 | FileObjectcxx . . $ HL/FileObject#FileObject(49f6e7a06ebc5aa8).() |
| 41 | : _isOpencxx . . $ HL/FileObject#_isOpen. (false), |
| 42 | _filecxx . . $ HL/FileObject#_file. (0) |
| 43 | {} |
| 44 | |
| 45 | FileObjectcxx . . $ HL/FileObject#FileObject(659f5672bdd886f0). (const FileObjectcxx . . $ HL/FileObject#& f) |
| 46 | : _isOpencxx . . $ HL/FileObject#_isOpen. (f._isOpencxx . . $ HL/FileObject#_isOpen.), |
| 47 | _filecxx . . $ HL/FileObject#_file. (f._filecxx . . $ HL/FileObject#_file.) |
| 48 | {} |
| 49 | |
| 50 | FileObjectcxx . . $ HL/FileObject#& operatorcxx . . $ HL/FileObject#`operator=`(a46a05aa415e4374).=(const FileObjectcxx . . $ HL/FileObject#& f) { |
| 51 | _isOpencxx . . $ HL/FileObject#_isOpen. = f._isOpencxx . . $ HL/FileObject#_isOpen.; |
| 52 | _filecxx . . $ HL/FileObject#_file. = f._filecxx . . $ HL/FileObject#_file.; |
| 53 | return *this; |
| 54 | } |
| 55 | |
| 56 | ~cxx . . $ HL/FileObject#`~FileObject`(49f6e7a06ebc5aa8).FileObjectcxx . . $ HL/FileObject#() { |
| 57 | closecxx . . $ HL/FileObject#close(49f6e7a06ebc5aa8).(); |
| 58 | } |
| 59 | |
| 60 | void opencxx . . $ HL/FileObject#open(fda57c8427c60377). (char * fname) |
| 61 | { |
| 62 | #if 1 |
| 63 | _filecxx . . $ HL/FileObject#_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_opencxx . . $ HL/FileObject#is_open(c9857a73d68e59ba).() { |
| 74 | return _isOpencxx . . $ HL/FileObject#_isOpen.; |
| 75 | } |
| 76 | |
| 77 | void closecxx . . $ HL/FileObject#close(49f6e7a06ebc5aa8).() |
| 78 | { |
| 79 | if (_isOpencxx . . $ HL/FileObject#_isOpen.) { |
| 80 | #if 1 |
| 81 | fclose (_filecxx . . $ HL/FileObject#_file.); |
| 82 | #else |
| 83 | #ifdef WIN32 |
| 84 | _close (_file); |
| 85 | #else |
| 86 | ::close (_file); |
| 87 | #endif |
| 88 | #endif |
| 89 | _isOpencxx . . $ HL/FileObject#_isOpen. = false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | friend FileObjectcxx . . $ HL/FileObject#& operatorcxx . . $ HL/`operator<<`(b416f9254041915c).<<(FileObjectcxx . . $ HL/FileObject#& f, int n) { |
| 95 | char buf[255]; |
| 96 | snprintf (buf, 255, "%d", n); |
| 97 | f.writeStringcxx . . $ HL/FileObject#writeString(fda57c8427c60377). (buf); |
| 98 | return f; |
| 99 | } |
| 100 | |
| 101 | friend FileObjectcxx . . $ HL/FileObject#& operatorcxx . . $ HL/`operator<<`(53620b8c76265577).<<(FileObjectcxx . . $ HL/FileObject#& f, char * s) { |
| 102 | char buf[255]; |
| 103 | snprintf (buf, 255, "%s", s); |
| 104 | f.writeStringcxx . . $ HL/FileObject#writeString(fda57c8427c60377). (buf); |
| 105 | return f; |
| 106 | } |
| 107 | |
| 108 | friend FileObjectcxx . . $ HL/FileObject#& operatorcxx . . $ HL/`operator<<`(8d2721b60712eb98).<<(FileObjectcxx . . $ HL/FileObject#& f, void * s) { |
| 109 | char buf[255]; |
| 110 | snprintf (buf, 255, "%p", s); |
| 111 | f.writeStringcxx . . $ HL/FileObject#writeString(fda57c8427c60377). (buf); |
| 112 | return f; |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | |
| 117 | void writeStringcxx . . $ HL/FileObject#writeString(fda57c8427c60377). (char * buf) { |
| 118 | #if 1 |
| 119 | fprintf (_filecxx . . $ HL/FileObject#_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 _isOpencxx . . $ HL/FileObject#_isOpen.; |
| 130 | |
| 131 | #if 1 |
| 132 | FILE * _filecxx . . $ HL/FileObject#_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 TraceHeapcxx . . $ HL/TraceHeap# : public Super { |
| 144 | public: |
| 145 | |
| 146 | TraceHeapcxx . . $ HL/TraceHeap#`TraceHeap<Super, Number>`(49f6e7a06ebc5aa8).() |
| 147 | { |
| 148 | if (!theFilecxx . . $ HL/TraceHeap#theFile(7475bb6353444cb0).().is_open()) { |
| 149 | char fname[255]; |
| 150 | snprintf (fname, 255, "trace-%d", Number); |
| 151 | theFilecxx . . $ HL/TraceHeap#theFile(7475bb6353444cb0).().open (fname); |
| 152 | printf ("OPEN %s\n", fname); |
| 153 | } |
| 154 | getRefscxx . . $ HL/TraceHeap#getRefs(ffa05e89dbf2c50f).()++; |
| 155 | } |
| 156 | |
| 157 | ~cxx . . $ HL/TraceHeap#`~TraceHeap<Super, Number>`(49f6e7a06ebc5aa8).TraceHeap() |
| 158 | { |
| 159 | // theFile() << ::std::flush; |
| 160 | --getRefscxx . . $ HL/TraceHeap#getRefs(ffa05e89dbf2c50f).(); |
| 161 | if (getRefscxx . . $ HL/TraceHeap#getRefs(ffa05e89dbf2c50f).() ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). 0) { |
| 162 | if (theFilecxx . . $ HL/TraceHeap#theFile(7475bb6353444cb0).().is_open()) |
| 163 | theFilecxx . . $ HL/TraceHeap#theFile(7475bb6353444cb0).().close(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | inline void * malloccxx . . $ HL/TraceHeap#malloc(bfcfa4d6b7f7ef64). (size_t sz) { |
| 168 | void * ptr = Super::malloc (sz); |
| 169 | theFilecxx . . $ HL/TraceHeap#theFile(7475bb6353444cb0).() <<cxx . . $ std/`operator<<`(f00c5788794c253e). "M " <<cxx . . $ std/`operator<<`(f00c5788794c253e). Number <<cxx . . $ std/`operator<<`(f00c5788794c253e). "\t" <<cxx . . $ std/`operator<<`(f00c5788794c253e). sz <<cxx . . $ std/`operator<<`(f00c5788794c253e). "\t" <<cxx . . $ std/`operator<<`(f00c5788794c253e). ptr <<cxx . . $ std/`operator<<`(f00c5788794c253e). "\n"; |
| 170 | return ptr; |
| 171 | } |
| 172 | |
| 173 | inline void freecxx . . $ HL/TraceHeap#free(5d22bdacc48458e8). (void * ptr) { |
| 174 | theFilecxx . . $ HL/TraceHeap#theFile(7475bb6353444cb0).() <<cxx . . $ std/`operator<<`(f00c5788794c253e). "F " <<cxx . . $ std/`operator<<`(f00c5788794c253e). Number <<cxx . . $ std/`operator<<`(f00c5788794c253e). "\t" <<cxx . . $ std/`operator<<`(f00c5788794c253e). ptr <<cxx . . $ std/`operator<<`(f00c5788794c253e). "\n"; |
| 175 | Super::free (ptr); |
| 176 | } |
| 177 | |
| 178 | private: |
| 179 | |
| 180 | FileObjectcxx . . $ HL/FileObject#& theFilecxx . . $ HL/TraceHeap#theFile(7475bb6353444cb0).() { |
| 181 | static FileObjectcxx . . $ HL/FileObject# fcxx . . $ HL/FileObject#FileObject(49f6e7a06ebc5aa8).; |
| 182 | return f; |
| 183 | } |
| 184 | |
| 185 | int& getRefscxx . . $ HL/TraceHeap#getRefs(ffa05e89dbf2c50f).() { |
| 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 |