Eneboo - Documentación para desarrolladores
|
00001 /* -*- C++ -*- */ 00002 00003 #ifndef _PROFILEHEAP_H_ 00004 #define _PROFILEHEAP_H_ 00005 00006 00007 #include <cstdio> 00008 00009 extern int memRequested; 00010 extern int maxMemRequested; 00011 00012 // Maintain & print memory usage info. 00013 // Requires a superheap with the size() method (e.g., SizeHeap). 00014 00015 template <class super, int HeapNumber> 00016 class ProfileHeap : public super { 00017 public: 00018 00019 ProfileHeap (void) 00020 { 00021 memRequested = 0; 00022 maxMemRequested = 0; 00023 } 00024 00025 ~ProfileHeap (void) 00026 { 00027 if (maxMemRequested > 0) { 00028 stats(); 00029 } 00030 } 00031 00032 inline void * malloc (size_t sz) { 00033 void * ptr = super::malloc (sz); 00034 // Notice that we use the size reported by the allocator 00035 // for the object rather than the requested size. 00036 memRequested += super::getSize(ptr); 00037 if (memRequested > maxMemRequested) { 00038 maxMemRequested = memRequested; 00039 } 00040 return ptr; 00041 } 00042 00043 inline void free (void * ptr) { 00044 memRequested -= super::getSize (ptr); 00045 super::free (ptr); 00046 } 00047 00048 private: 00049 void stats (void) { 00050 printf ("Heap: %d\n", HeapNumber); 00051 printf ("Max memory requested = %d\n", maxMemRequested); 00052 printf ("Memory still in use = %d\n", memRequested); 00053 } 00054 00055 int memRequested; 00056 int maxMemRequested; 00057 00058 }; 00059 00060 00061 #endif