source: proiecte/swift/trunk/lib/hoard-371/src/heaplayers/profileheap.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.2 KB
Line 
1/* -*- C++ -*- */
2
3#ifndef _PROFILEHEAP_H_
4#define _PROFILEHEAP_H_
5
6
7#include <stdio.h>
8
9extern int memRequested;
10extern int maxMemRequested;
11
12// Maintain & print memory usage info.
13// Requires a superheap with the size() method (e.g., SizeHeap).
14
15template <class super, int HeapNumber>
16class ProfileHeap : public super {
17public:
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 
48private:
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.