1 | /* -*- C++ -*- */ |
---|
2 | |
---|
3 | #ifndef _UNIQUEHEAP_H_ |
---|
4 | #define _UNIQUEHEAP_H_ |
---|
5 | |
---|
6 | #include <stdlib.h> |
---|
7 | #include <new> |
---|
8 | |
---|
9 | /** |
---|
10 | * |
---|
11 | * @class UniqueHeap |
---|
12 | * @brief Instantiates one instance of a class used for every malloc & free. |
---|
13 | * @author Emery Berger |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | template <class SuperHeap, class Child = int> |
---|
18 | class UniqueHeap { |
---|
19 | public: |
---|
20 | |
---|
21 | /** |
---|
22 | * Ensure that the super heap gets created, |
---|
23 | * and add a reference for every instance of unique heap. |
---|
24 | */ |
---|
25 | UniqueHeap (void) |
---|
26 | { |
---|
27 | volatile SuperHeap * forceCreationOfSuperHeap = getSuperHeap(); |
---|
28 | addRef(); |
---|
29 | } |
---|
30 | |
---|
31 | /** |
---|
32 | * @brief Delete one reference to the unique heap. |
---|
33 | * When the number of references goes to zero, |
---|
34 | * delete the super heap. |
---|
35 | */ |
---|
36 | ~UniqueHeap (void) { |
---|
37 | int r = delRef(); |
---|
38 | if (r <= 0) { |
---|
39 | getSuperHeap()->SuperHeap::~SuperHeap(); |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | // The remaining public methods are just |
---|
44 | // thin wrappers that route calls to the static object. |
---|
45 | |
---|
46 | inline void * malloc (size_t sz) { |
---|
47 | return getSuperHeap()->malloc (sz); |
---|
48 | } |
---|
49 | |
---|
50 | inline void free (void * ptr) { |
---|
51 | getSuperHeap()->free (ptr); |
---|
52 | } |
---|
53 | |
---|
54 | inline size_t getSize (void * ptr) { |
---|
55 | return getSuperHeap()->getSize (ptr); |
---|
56 | } |
---|
57 | |
---|
58 | inline int remove (void * ptr) { |
---|
59 | return getSuperHeap()->remove (ptr); |
---|
60 | } |
---|
61 | |
---|
62 | inline void clear (void) { |
---|
63 | getSuperHeap()->clear(); |
---|
64 | } |
---|
65 | |
---|
66 | #if 0 |
---|
67 | inline int getAllocated (void) { |
---|
68 | return getSuperHeap()->getAllocated(); |
---|
69 | } |
---|
70 | |
---|
71 | inline int getMaxAllocated (void) { |
---|
72 | return getSuperHeap()->getMaxAllocated(); |
---|
73 | } |
---|
74 | |
---|
75 | inline int getMaxInUse (void) { |
---|
76 | return getSuperHeap()->getMaxInUse(); |
---|
77 | } |
---|
78 | #endif |
---|
79 | |
---|
80 | private: |
---|
81 | |
---|
82 | /// Add one reference. |
---|
83 | void addRef (void) { |
---|
84 | getRefs() += 1; |
---|
85 | } |
---|
86 | |
---|
87 | /// Delete one reference count. |
---|
88 | int delRef (void) { |
---|
89 | getRefs() -= 1; |
---|
90 | return getRefs(); |
---|
91 | } |
---|
92 | |
---|
93 | /// Internal accessor for reference count. |
---|
94 | int& getRefs (void) { |
---|
95 | static int numRefs = 0; |
---|
96 | return numRefs; |
---|
97 | } |
---|
98 | |
---|
99 | SuperHeap * getSuperHeap (void) { |
---|
100 | static char superHeapBuffer[sizeof(SuperHeap)]; |
---|
101 | static SuperHeap * aSuperHeap = (SuperHeap *) (new ((char *) &superHeapBuffer) SuperHeap); |
---|
102 | return aSuperHeap; |
---|
103 | } |
---|
104 | |
---|
105 | void doNothing (Child *) {} |
---|
106 | }; |
---|
107 | |
---|
108 | |
---|
109 | #endif // _UNIQUE_H_ |
---|