00001 #ifndef _HLSTATS_H_
00002 #define _HLSTATS_H_
00003
00004 #include <map>
00005
00006 template <class SuperHeap>
00007 class InUseHeap : public SuperHeap {
00008 private:
00009 typedef std::map<void *, int> mapType;
00010 public:
00011 InUseHeap (void)
00012 : inUse (0),
00013 maxInUse (0)
00014 {}
00015 void * malloc (size_t sz) {
00016 void * ptr = SuperHeap::malloc (sz);
00017 if (ptr != NULL) {
00018 inUse += sz;
00019 if (maxInUse < inUse) {
00020 maxInUse = inUse;
00021 }
00022 allocatedObjects.insert (std::pair<void *, int>(ptr, sz));
00023 }
00024 return ptr;
00025 }
00026 void free (void * ptr) {
00027 mapType::iterator i;
00028 i = allocatedObjects.find (ptr);
00029 if (i == allocatedObjects.end()) {
00030
00031 assert (0);
00032 }
00033 inUse -= (*i).second;
00034 allocatedObjects.erase (i);
00035 SuperHeap::free (ptr);
00036 }
00037
00038 int getInUse (void) const {
00039 return inUse;
00040 }
00041 int getMaxInUse (void) const {
00042 return maxInUse;
00043 }
00044 private:
00045 int inUse;
00046 int maxInUse;
00047 mapType allocatedObjects;
00048 };
00049
00050
00051 template <class SuperHeap>
00052 class AllocatedHeap : public SuperHeap {
00053 public:
00054 AllocatedHeap (void)
00055 : allocated (0),
00056 maxAllocated (0)
00057 {}
00058 void * malloc (size_t sz) {
00059 void * ptr = SuperHeap::malloc (sz);
00060 if (ptr != NULL) {
00061 allocated += getSize(ptr);
00062 if (maxAllocated < allocated) {
00063 maxAllocated = allocated;
00064 }
00065 }
00066 return ptr;
00067 }
00068 void free (void * ptr) {
00069 allocated -= getSize(ptr);
00070 SuperHeap::free (ptr);
00071 }
00072
00073 int getAllocated (void) const {
00074 return allocated;
00075 }
00076 int getMaxAllocated (void) const {
00077 return maxAllocated;
00078 }
00079 private:
00080 int allocated;
00081 int maxAllocated;
00082 };
00083
00084
00085 template <class SuperHeap>
00086 class StatsHeap : public SuperHeap {
00087 public:
00088 ~StatsHeap (void) {
00089 printf ("In use = %d, allocated = %d\n", getInUse(), getAllocated());
00090 printf ("Max in use = %d, max allocated = %d\n", getMaxInUse(), getMaxAllocated());
00091 }
00092 };
00093
00094 #endif