csutil/memheap.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2006 by Frank Richter 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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&); // Illegal; unimplemented. 00053 void operator= (Heap const&); // Illegal; unimplemented. 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 csMemTrackerInfo* 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 00098 void* Alloc (const size_t n) 00099 { 00100 #if defined(CS_MEMORY_TRACKER) 00101 size_t* p = (size_t*)HeapAccess::Alloc (n + sizeof (size_t)); 00102 *p = n; 00103 p++; 00104 if (mti == 0) 00105 { 00106 /*csString mtiInfo; 00107 mtiInfo.Format ("%s with %p", typeid(*this).name(), HeapAccess::GetHeap());*/ 00108 mti = mtiRegisterAlloc (n, /*mtiInfo*/typeid(*this).name()); 00109 } 00110 else 00111 mtiUpdateAmount (mti, 1, int (n)); 00112 return p; 00113 #else 00114 return HeapAccess::Alloc (n); 00115 #endif 00116 } 00118 void Free (void* p) 00119 { 00120 #if defined(CS_MEMORY_TRACKER) 00121 size_t* x = (size_t*)p; 00122 x--; 00123 size_t allocSize = *x; 00124 HeapAccess::Free (x); 00125 if (mti) mtiUpdateAmount (mti, -1, -int (allocSize)); 00126 #else 00127 HeapAccess::Free (p); 00128 #endif 00129 } 00131 void* Realloc (void* p, size_t newSize) 00132 { 00133 #ifdef CS_MEMORY_TRACKER 00134 if (p == 0) return Alloc (newSize); 00135 size_t* x = (size_t*)p; 00136 x--; 00137 if (mti) mtiUpdateAmount (mti, -1, -int (*x)); 00138 size_t* np = 00139 (size_t*)HeapAccess::Realloc (x, newSize + sizeof (size_t)); 00140 *np = newSize; 00141 np++; 00142 if (mti) mtiUpdateAmount (mti, 1, int (newSize)); 00143 return np; 00144 #else 00145 return HeapAccess::Realloc (p, newSize); 00146 #endif 00147 } 00149 void SetMemTrackerInfo (const char* info) 00150 { 00151 #ifdef CS_MEMORY_TRACKER 00152 if (mti != 0) return; 00153 /*csString mtiInfo; 00154 mtiInfo.Format ("%s with %p for %s", typeid(*this).name(), 00155 HeapAccess::GetHeap(), info);*/ 00156 mti = mtiRegister (/*mtiInfo*/info); 00157 #else 00158 (void)info; 00159 #endif 00160 } 00161 }; 00162 00168 template<class HeapContainer = Heap*> 00169 struct HeapAccessPointer 00170 { 00171 HeapContainer heap; 00172 00173 CS_DEPRECATED_METHOD_MSG ("HeapAccessPointer instance uninitialized") 00174 HeapAccessPointer () {} 00175 HeapAccessPointer (HeapContainer heap) : heap (heap) {} 00176 00177 void* Alloc (const size_t n) 00178 { 00179 return heap->Alloc (n); 00180 } 00181 void Free (void* p) 00182 { 00183 heap->Free (p); 00184 } 00185 void* Realloc (void* p, size_t newSize) 00186 { 00187 return heap->Realloc (p, newSize); 00188 } 00189 const HeapContainer& GetHeap () 00190 { 00191 return heap; 00192 } 00193 }; 00194 00200 template<class HeapPtr = Heap*> 00201 class AllocatorHeap : public AllocatorHeapBase<HeapAccessPointer<HeapPtr> > 00202 { 00203 public: 00204 CS_DEPRECATED_METHOD_MSG ("AllocatorHeap instance uninitialized") 00205 AllocatorHeap () {} 00206 00207 AllocatorHeap (HeapPtr heap) : 00208 AllocatorHeapBase<HeapAccessPointer<HeapPtr> > ( 00209 HeapAccessPointer<HeapPtr> (heap)) {} 00210 }; 00211 } // namespace Memory 00212 } // namespace CS 00213 00216 #endif // __CS_CSUTIL_MEMHEAP_H__
Generated for Crystal Space 1.2.1 by doxygen 1.5.3