Rev | Line | |
---|
[176] | 1 | /* -*- C++ -*- */ |
---|
| 2 | |
---|
| 3 | #ifndef _PROFILEHEAP_H_ |
---|
| 4 | #define _PROFILEHEAP_H_ |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | #include <stdio.h> |
---|
| 8 | |
---|
| 9 | extern int memRequested; |
---|
| 10 | extern int maxMemRequested; |
---|
| 11 | |
---|
| 12 | // Maintain & print memory usage info. |
---|
| 13 | // Requires a superheap with the size() method (e.g., SizeHeap). |
---|
| 14 | |
---|
| 15 | template <class super, int HeapNumber> |
---|
| 16 | class ProfileHeap : public super { |
---|
| 17 | public: |
---|
| 18 | |
---|
| 19 | ProfileHeap (void) |
---|
| 20 | { |
---|
| 21 | memRequested = 0; |
---|
| 22 | maxMemRequested = 0; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | ~ProfileHeap (void) |
---|
| 26 | { |
---|
| 27 | if (maxMemRequested > 0) { |
---|
| 28 | stats(); |
---|
| 29 | } |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | inline void * malloc (size_t sz) { |
---|
| 33 | void * ptr = super::malloc (sz); |
---|
| 34 | // Notice that we use the size reported by the allocator |
---|
| 35 | // for the object rather than the requested size. |
---|
| 36 | memRequested += super::getSize(ptr); |
---|
| 37 | if (memRequested > maxMemRequested) { |
---|
| 38 | maxMemRequested = memRequested; |
---|
| 39 | } |
---|
| 40 | return ptr; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | inline void free (void * ptr) { |
---|
| 44 | memRequested -= super::getSize (ptr); |
---|
| 45 | super::free (ptr); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | private: |
---|
| 49 | void stats (void) { |
---|
| 50 | printf ("Heap: %d\n", HeapNumber); |
---|
| 51 | printf ("Max memory requested = %d\n", maxMemRequested); |
---|
| 52 | printf ("Memory still in use = %d\n", memRequested); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | int memRequested; |
---|
| 56 | int maxMemRequested; |
---|
| 57 | |
---|
| 58 | }; |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.