Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __CS_CSUTIL_MEMHEAP_H__
00020 #define __CS_CSUTIL_MEMHEAP_H__
00021
00026 #if defined(CS_MEMORY_TRACKER)
00027 #include "csutil/csstring.h"
00028 #include "csutil/memdebug.h"
00029 #include <typeinfo>
00030 #endif
00031
00032 #include "csutil/spinlock.h"
00033
00037 namespace CS
00038 {
00039 namespace Memory
00040 {
00046 class CS_CRYSTALSPACE_EXPORT Heap
00047 {
00049 void* mspace;
00050 SpinLock lock;
00051
00052 Heap (Heap const&);
00053 void operator= (Heap const&);
00054 public:
00055 Heap();
00056 ~Heap();
00057
00059 void* Alloc (const size_t n);
00061 void Free (void* p);
00063 void* Realloc (void* p, size_t newSize);
00064
00070 void Trim (size_t pad = 0);
00071
00075 size_t Footprint ();
00076 };
00077
00083 template<class HeapAccess>
00084 class AllocatorHeapBase : protected HeapAccess
00085 {
00086 #if defined(CS_MEMORY_TRACKER)
00087 const char* mti;
00088 #endif
00089 public:
00090 #if defined(CS_MEMORY_TRACKER)
00091 AllocatorHeapBase () : mti (0) { }
00092 AllocatorHeapBase (const HeapAccess& heap) : HeapAccess (heap), mti (0) {}
00093 #else
00094 AllocatorHeapBase () { }
00095 AllocatorHeapBase (const HeapAccess& heap) : HeapAccess (heap) {}
00096 #endif
00097
00098 void* Alloc (const size_t n)
00099 {
00100 #if defined(CS_MEMORY_TRACKER)
00101 void* p = HeapAccess::Alloc (n);
00102 if (mti == 0)
00103 {
00104
00105
00106 mti = typeid(*this).name();
00107 }
00108 CS::Debug::MemTracker::RegisterAlloc (p, n, mti);
00109 return p;
00110 #else
00111 return HeapAccess::Alloc (n);
00112 #endif
00113 }
00115 void Free (void* p)
00116 {
00117 #if defined(CS_MEMORY_TRACKER)
00118 CS::Debug::MemTracker::RegisterFree (p);
00119 #endif
00120 HeapAccess::Free (p);
00121 }
00123 void* Realloc (void* p, size_t newSize)
00124 {
00125 #ifdef CS_MEMORY_TRACKER
00126 if (p == 0) return Alloc (newSize);
00127 void* np = HeapAccess::Realloc (p, newSize);
00128 CS::Debug::MemTracker::UpdateSize (p, np, newSize);
00129 return np;
00130 #else
00131 return HeapAccess::Realloc (p, newSize);
00132 #endif
00133 }
00135 void SetMemTrackerInfo (const char* info)
00136 {
00137 #ifdef CS_MEMORY_TRACKER
00138 if (mti != 0) return;
00139
00140
00141
00142 mti = info;
00143 #else
00144 (void)info;
00145 #endif
00146 }
00147 };
00148
00154 template<class HeapContainer = Heap*>
00155 struct HeapAccessPointer
00156 {
00157 HeapContainer heap;
00158
00159 CS_DEPRECATED_METHOD_MSG ("HeapAccessPointer instance uninitialized")
00160 HeapAccessPointer () {}
00161 HeapAccessPointer (HeapContainer heap) : heap (heap) {}
00162
00163 void* Alloc (const size_t n)
00164 {
00165 return heap->Alloc (n);
00166 }
00167 void Free (void* p)
00168 {
00169 heap->Free (p);
00170 }
00171 void* Realloc (void* p, size_t newSize)
00172 {
00173 return heap->Realloc (p, newSize);
00174 }
00175 const HeapContainer& GetHeap ()
00176 {
00177 return heap;
00178 }
00179 };
00180
00186 template<class HeapPtr = Heap*>
00187 class AllocatorHeap : public AllocatorHeapBase<HeapAccessPointer<HeapPtr> >
00188 {
00189 public:
00190 CS_DEPRECATED_METHOD_MSG ("AllocatorHeap instance uninitialized")
00191 AllocatorHeap () {}
00192
00193 AllocatorHeap (HeapPtr heap) :
00194 AllocatorHeapBase<HeapAccessPointer<HeapPtr> > (
00195 HeapAccessPointer<HeapPtr> (heap)) {}
00196 };
00197 }
00198 }
00199
00202 #endif // __CS_CSUTIL_MEMHEAP_H__