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

Last change on this file since 176 was 176, checked in by (none), 14 years ago
  • imported repo from "guagal"
File size: 1.8 KB
Line 
1#ifndef _HLSTATS_H_
2#define _HLSTATS_H_
3
4#include <map>
5
6template <class SuperHeap>
7class InUseHeap : public SuperHeap {
8private:
9  typedef std::map<void *, int> mapType;
10public:
11  InUseHeap (void)
12    : inUse (0),
13      maxInUse (0)
14  {}
15  void * malloc (size_t sz) {
16    void * ptr = SuperHeap::malloc (sz);
17    if (ptr != NULL) {
18      inUse += sz;
19      if (maxInUse < inUse) {
20        maxInUse = inUse;
21      }
22      allocatedObjects.insert (std::pair<void *, int>(ptr, sz));
23    }
24    return ptr;
25  }
26  void free (void * ptr) {
27    mapType::iterator i;
28    i = allocatedObjects.find (ptr);
29    if (i == allocatedObjects.end()) {
30      // oops -- called free on object i didn't allocate.
31      assert (0);
32    }
33    inUse -= (*i).second;
34    allocatedObjects.erase (i);
35    SuperHeap::free (ptr);
36  }
37
38  int getInUse (void) const {
39    return inUse;
40  }
41  int getMaxInUse (void) const {
42    return maxInUse;
43  }
44private:
45  int inUse;
46  int maxInUse;
47  mapType allocatedObjects;
48};
49
50
51template <class SuperHeap>
52class AllocatedHeap : public SuperHeap {
53public:
54  AllocatedHeap (void)
55    : allocated (0),
56      maxAllocated (0)
57  {}
58  void * malloc (size_t sz) {
59    void * ptr = SuperHeap::malloc (sz);
60    if (ptr != NULL) {
61      allocated += getSize(ptr);
62      if (maxAllocated < allocated) {
63        maxAllocated = allocated;
64      }
65    }
66    return ptr;
67  }
68  void free (void * ptr) {
69    allocated -= getSize(ptr);
70    SuperHeap::free (ptr);
71  }
72
73  int getAllocated (void) const {
74    return allocated;
75  }
76  int getMaxAllocated (void) const {
77    return maxAllocated;
78  }
79private:
80  int allocated;
81  int maxAllocated;
82};
83
84
85template <class SuperHeap>
86class StatsHeap : public SuperHeap {
87public:
88  ~StatsHeap (void) {
89    printf ("In use = %d, allocated = %d\n", getInUse(), getAllocated());
90    printf ("Max in use = %d, max allocated = %d\n", getMaxInUse(), getMaxAllocated());
91  }
92};
93
94#endif
Note: See TracBrowser for help on using the repository browser.