00001
00002
00003 #ifndef _TRACEHEAP_H_
00004 #define _TRACEHEAP_H_
00005
00006 #include <cstdlib>
00007
00008 #ifdef WIN32
00009 #include <io.h>
00010 #endif
00011
00012 #include <fcntl.h>
00013 #include <sys/types.h>
00014 #include <sys/stat.h>
00015 #include <string.h>
00016 #include <cstdio>
00017
00018
00019
00020 class FileObject {
00021 public:
00022
00023 FileObject (void)
00024 : isOpen (0)
00025 {}
00026
00027 ~FileObject (void) {
00028 close();
00029 }
00030
00031 void open (char * fname)
00032 {
00033 #if 1
00034 file = fopen(fname, "w+");
00035 #else
00036 #ifdef WIN32
00037 file = _open(fname, _O_WRONLY | _O_CREAT, _S_IREAD | _S_IWRITE);
00038 #else
00039 file = ::open(fname, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
00040 #endif
00041 #endif
00042 }
00043
00044 int is_open (void) {
00045 return isOpen;
00046 }
00047
00048 void close (void)
00049 {
00050 if (isOpen) {
00051 #if 1
00052 fclose (file);
00053 #else
00054 #ifdef WIN32
00055 _close (file);
00056 #else
00057 ::close (file);
00058 #endif
00059 #endif
00060 isOpen = 0;
00061 }
00062 }
00063
00064
00065 friend FileObject& operator<<(FileObject& f, int n) {
00066
00067 char buf[255];
00068 sprintf (buf, "%d", n);
00069 f.writeString (buf);
00070 return f;
00071 }
00072
00073 friend FileObject& operator<<(FileObject& f, char * s) {
00074
00075 char buf[255];
00076 sprintf (buf, "%s", s);
00077 f.writeString (buf);
00078 return f;
00079 }
00080
00081 friend FileObject& operator<<(FileObject& f, void * s) {
00082
00083 char buf[255];
00084 sprintf (buf, "%p", s);
00085 f.writeString (buf);
00086 return f;
00087 }
00088
00089 private:
00090
00091 void writeString (char * buf) {
00092 #if 1
00093 fprintf(file, buf);
00094 #else
00095 #ifdef WIN32
00096 _write (file, buf, strlen(buf));
00097 #else
00098 write (file, buf, strlen(buf));
00099 #endif
00100 #endif
00101 }
00102
00103 #if 1
00104 FILE * file;
00105 #else
00106 int file;
00107 #endif
00108
00109 bool isOpen;
00110
00111 };
00112
00113
00114
00115
00116
00117 template <class Super, int Number>
00118 class TraceHeap : public Super {
00119 public:
00120
00121 TraceHeap (void)
00122 {
00123 if (!theFile().is_open()) {
00124 char fname[255];
00125 sprintf (fname, "trace-%d", Number);
00126 theFile().open (fname);
00127 printf ("OPEN %s\n", fname);
00128 }
00129 getRefs()++;
00130 }
00131
00132 ~TraceHeap (void)
00133 {
00134
00135 --getRefs();
00136 if (getRefs() == 0) {
00137 if (theFile().is_open())
00138 theFile().close();
00139 }
00140 }
00141
00142 inline void * malloc (size_t sz) {
00143 void * ptr = Super::malloc (sz);
00144 theFile() << "M " << Number << "\t" << sz << "\t" << ptr << "\n";
00145 return ptr;
00146 }
00147
00148 inline void free (void * ptr) {
00149 theFile() << "F " << Number << "\t" << ptr << "\n";
00150 Super::free (ptr);
00151 }
00152
00153 private:
00154
00155 FileObject& theFile (void) {
00156 static FileObject f;
00157 return f;
00158 }
00159
00160 int& getRefs (void) {
00161 static int refs = 0;
00162 return refs;
00163 }
00164
00165 };
00166
00167
00168 #endif
00169
00170 #if 0
00171
00172 #ifndef _TRACEHEAP_H_
00173 #define _TRACEHEAP_H_
00174
00175
00176 #include <fstream>
00177 #include <map>
00178
00179 template <class Super, int Number>
00180 class TraceHeap : public Super {
00181 public:
00182
00183 TraceHeap (void)
00184 {
00185 if (!theFile().is_open()) {
00186 printf ("OPEN\n");
00187 theFile().open ("trace");
00188 }
00189 getRefs()++;
00190 }
00191 ~TraceHeap (void)
00192 {
00193 theFile() << ::std::flush;
00194 --getRefs();
00195 if (getRefs() == 0) {
00196 if (theFile().is_open())
00197 theFile().close();
00198 }
00199 }
00200 inline void * malloc (size_t sz) {
00201 void * ptr = Super::malloc (sz);
00202 theFile() << "M\t" << sz << "\t" << ptr << "\n";
00203 return ptr;
00204 }
00205
00206 inline void free (void * ptr) {
00207 theFile() << "F\t" << ptr << "\n";
00208 Super::free (ptr);
00209 }
00210 private:
00211 std::ofstream& theFile (void) {
00212 static std::ofstream f;
00213 return f;
00214 }
00215 int& getRefs (void) {
00216 static int refs = 0;
00217 return refs;
00218 }
00219 };
00220
00221 #endif
00222 #endif