source: proiecte/swift/trunk/lib/hoard-371/src/heaplayers/traceheap.h @ 176

Last change on this file since 176 was 176, checked in by (none), 14 years ago
  • imported repo from "guagal"
File size: 3.6 KB
Line 
1/* -*- C++ -*- */
2
3#ifndef _TRACEHEAP_H_
4#define _TRACEHEAP_H_
5
6#include <stdlib.h>
7
8#ifdef WIN32
9#include <io.h>
10#endif
11
12#include <fcntl.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <string.h>
16#include <stdio.h>
17
18// An object that manages direct writing to a file.
19
20class FileObject {
21public:
22
23  FileObject (void)
24    : isOpen (0)
25  {}
26 
27  ~FileObject (void) {
28    close();
29  }
30 
31  void open (char * fname)
32  {
33#if 1
34    file = fopen(fname, "w+");
35#else
36#ifdef WIN32
37    file = _open(fname, _O_WRONLY | _O_CREAT, _S_IREAD | _S_IWRITE);
38#else
39    file = ::open(fname, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
40#endif
41#endif
42  }
43 
44  int is_open (void) {
45    return isOpen;
46  }
47 
48  void close (void)
49  {
50    if (isOpen) {
51#if 1
52      fclose (file);
53#else
54#ifdef WIN32
55      _close (file);
56#else
57      ::close (file);
58#endif
59#endif
60      isOpen = 0;
61    }
62  }
63 
64 
65  friend FileObject& operator<<(FileObject& f, int n) {
66    //          fprintf (f.file, "%d", n);
67    char buf[255];
68    sprintf (buf, "%d", n);
69    f.writeString (buf);
70    return f;
71  }
72 
73  friend FileObject& operator<<(FileObject& f, char * s) {
74    //                      fprintf (f.file, "%s", s);
75    char buf[255];
76    sprintf (buf, "%s", s);
77    f.writeString (buf);
78    return f;
79  }
80 
81  friend FileObject& operator<<(FileObject& f, void * s) {
82    //                      fprintf (f.file, "%p", s);
83    char buf[255];
84    sprintf (buf, "%p", s);
85    f.writeString (buf);
86    return f;
87  }
88 
89private:
90
91  void writeString (char * buf) {
92#if 1
93          fprintf(file, buf);
94#else
95#ifdef WIN32
96    _write (file, buf, strlen(buf));
97#else
98    write (file, buf, strlen(buf));
99#endif
100#endif
101  }
102
103#if 1
104  FILE * file;
105#else
106  int file;
107#endif
108
109  bool isOpen;
110
111};
112
113
114// The TraceHeap itself.
115// Traces malloc & free calls.
116 
117template <class Super, int Number>
118class TraceHeap : public Super {
119public:
120
121  TraceHeap (void)
122  {
123    if (!theFile().is_open()) {
124      char fname[255];
125      sprintf (fname, "trace-%d", Number);
126      theFile().open (fname);
127      printf ("OPEN %s\n", fname);
128    }
129    getRefs()++;
130  }
131 
132  ~TraceHeap (void)
133  {
134    //                      theFile() << ::std::flush;
135    --getRefs();
136    if (getRefs() == 0) {
137      if (theFile().is_open())
138        theFile().close();
139    }
140  }
141 
142  inline void * malloc (size_t sz) {
143    void * ptr = Super::malloc (sz);
144    theFile() << "M " << Number << "\t" << sz << "\t" << ptr << "\n";
145    return ptr;
146  }
147 
148  inline void free (void * ptr) {
149    theFile() << "F " << Number << "\t" << ptr << "\n";
150    Super::free (ptr);
151  }
152 
153private:
154 
155  FileObject& theFile (void) {
156    static FileObject f;
157    return f;
158  }
159 
160  int& getRefs (void) {
161    static int refs = 0;
162    return refs;
163  }
164 
165};
166 
167 
168#endif
169
170#if 0
171/* -*- C++ -*- */
172#ifndef _TRACEHEAP_H_
173#define _TRACEHEAP_H_
174// #include <iostream.h>
175//#include <fstream.h>
176#include <fstream>
177#include <map>
178
179template <class Super, int Number>
180class TraceHeap : public Super {
181public:
182 
183  TraceHeap (void)
184  {
185    if (!theFile().is_open()) {
186      printf ("OPEN\n");
187      theFile().open ("trace");
188    }
189    getRefs()++;
190  }
191  ~TraceHeap (void)
192  {
193    theFile() << ::std::flush;
194    --getRefs();
195    if (getRefs() == 0) {
196      if (theFile().is_open())
197        theFile().close();
198    }
199  }
200  inline void * malloc (size_t sz) {
201    void * ptr = Super::malloc (sz);
202    theFile() << "M\t" << sz << "\t" << ptr << "\n";
203    return ptr;
204  }
205 
206  inline void free (void * ptr) {
207    theFile() << "F\t" << ptr << "\n";
208    Super::free (ptr);
209  }
210private:
211  std::ofstream& theFile (void) {
212    static std::ofstream f;
213    return f;
214  }
215  int& getRefs (void) {
216    static int refs = 0;
217    return refs;
218  }
219};
220
221#endif
222#endif
Note: See TracBrowser for help on using the repository browser.