1 | #ifndef _HLSTATS_H_ |
---|
2 | #define _HLSTATS_H_ |
---|
3 | |
---|
4 | #include <map> |
---|
5 | |
---|
6 | template <class SuperHeap> |
---|
7 | class InUseHeap : public SuperHeap { |
---|
8 | private: |
---|
9 | typedef std::map<void *, int> mapType; |
---|
10 | public: |
---|
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 | } |
---|
44 | private: |
---|
45 | int inUse; |
---|
46 | int maxInUse; |
---|
47 | mapType allocatedObjects; |
---|
48 | }; |
---|
49 | |
---|
50 | |
---|
51 | template <class SuperHeap> |
---|
52 | class AllocatedHeap : public SuperHeap { |
---|
53 | public: |
---|
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 | } |
---|
79 | private: |
---|
80 | int allocated; |
---|
81 | int maxAllocated; |
---|
82 | }; |
---|
83 | |
---|
84 | |
---|
85 | template <class SuperHeap> |
---|
86 | class StatsHeap : public SuperHeap { |
---|
87 | public: |
---|
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 |
---|