00001
00002
00003 #ifndef _UNIQUEHEAP_H_
00004 #define _UNIQUEHEAP_H_
00005
00006 #include <cstdlib>
00007 #include <new>
00008
00017 template <class SuperHeap, class Child = int>
00018 class UniqueHeap {
00019 public:
00020
00025 UniqueHeap (void)
00026 {
00027 volatile SuperHeap * forceCreationOfSuperHeap = getSuperHeap();
00028 addRef();
00029 }
00030
00036 ~UniqueHeap (void) {
00037 int r = delRef();
00038 if (r <= 0) {
00039 getSuperHeap()->SuperHeap::~SuperHeap();
00040 }
00041 }
00042
00043
00044
00045
00046 inline void * malloc (size_t sz) {
00047 return getSuperHeap()->malloc (sz);
00048 }
00049
00050 inline void free (void * ptr) {
00051 getSuperHeap()->free (ptr);
00052 }
00053
00054 inline size_t getSize (void * ptr) {
00055 return getSuperHeap()->getSize (ptr);
00056 }
00057
00058 inline int remove (void * ptr) {
00059 return getSuperHeap()->remove (ptr);
00060 }
00061
00062 inline void clear (void) {
00063 getSuperHeap()->clear();
00064 }
00065
00066 #if 0
00067 inline int getAllocated (void) {
00068 return getSuperHeap()->getAllocated();
00069 }
00070
00071 inline int getMaxAllocated (void) {
00072 return getSuperHeap()->getMaxAllocated();
00073 }
00074
00075 inline int getMaxInUse (void) {
00076 return getSuperHeap()->getMaxInUse();
00077 }
00078 #endif
00079
00080 private:
00081
00083 void addRef (void) {
00084 getRefs() += 1;
00085 }
00086
00088 int delRef (void) {
00089 getRefs() -= 1;
00090 return getRefs();
00091 }
00092
00094 int& getRefs (void) {
00095 static int numRefs = 0;
00096 return numRefs;
00097 }
00098
00099 SuperHeap * getSuperHeap (void) {
00100 static char superHeapBuffer[sizeof(SuperHeap)];
00101 static SuperHeap * aSuperHeap = (SuperHeap *) (new ((char *) &superHeapBuffer) SuperHeap);
00102 return aSuperHeap;
00103 }
00104
00105 void doNothing (Child *) {}
00106 };
00107
00108
00109 #endif // _UNIQUE_H_