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 }; 00072 00078 template<class HeapAccess> 00079 class AllocatorHeapBase : protected HeapAccess 00080 { 00081 #if defined(CS_MEMORY_TRACKER) 00082 csMemTrackerInfo* mti; 00083 #endif 00084 public: 00085 #if defined(CS_MEMORY_TRACKER) 00086 AllocatorHeapBase () : mti (0) { } 00087 AllocatorHeapBase (const HeapAccess& heap) : HeapAccess (heap), mti (0) {} 00088 #else 00089 AllocatorHeapBase () { } 00090 AllocatorHeapBase (const HeapAccess& heap) : HeapAccess (heap) {} 00091 #endif 00093 void* Alloc (const size_t n) 00094 { 00095 #if defined(CS_MEMORY_TRACKER) 00096 size_t* p = (size_t*)HeapAccess::Alloc (n + sizeof (size_t)); 00097 *p = n; 00098 p++; 00099 if (mti == 0) 00100 { 00101 /*csString mtiInfo; 00102 mtiInfo.Format ("%s with %p", typeid(*this).name(), HeapAccess::GetHeap());*/ 00103 mti = mtiRegisterAlloc (n, /*mtiInfo*/typeid(*this).name()); 00104 } 00105 else 00106 mtiUpdateAmount (mti, 1, int (n)); 00107 return p; 00108 #else 00109 return HeapAccess::Alloc (n); 00110 #endif 00111 } 00113 void Free (void* p) 00114 { 00115 #if defined(CS_MEMORY_TRACKER) 00116 size_t* x = (size_t*)p; 00117 x--; 00118 size_t allocSize = *x; 00119 HeapAccess::Free (x); 00120 if (mti) mtiUpdateAmount (mti, -1, -int (allocSize)); 00121 #else 00122 HeapAccess::Free (p); 00123 #endif 00124 } 00126 void* Realloc (void* p, size_t newSize) 00127 { 00128 #ifdef CS_MEMORY_TRACKER 00129 if (p == 0) return Alloc (newSize); 00130 size_t* x = (size_t*)p; 00131 x--; 00132 if (mti) mtiUpdateAmount (mti, -1, -int (*x)); 00133 size_t* np = 00134 (size_t*)HeapAccess::Realloc (x, newSize + sizeof (size_t)); 00135 *np = newSize; 00136 np++; 00137 if (mti) mtiUpdateAmount (mti, 1, int (newSize)); 00138 return np; 00139 #else 00140 return HeapAccess::Realloc (p, newSize); 00141 #endif 00142 } 00144 void SetMemTrackerInfo (const char* info) 00145 { 00146 #ifdef CS_MEMORY_TRACKER 00147 if (mti != 0) return; 00148 /*csString mtiInfo; 00149 mtiInfo.Format ("%s with %p for %s", typeid(*this).name(), 00150 HeapAccess::GetHeap(), info);*/ 00151 mti = mtiRegister (/*mtiInfo*/info); 00152 #else 00153 (void)info; 00154 #endif 00155 } 00156 }; 00157 00163 template<class HeapContainer = Heap*> 00164 struct HeapAccessPointer 00165 { 00166 HeapContainer heap; 00167 void* Alloc (const size_t n) 00168 { 00169 return heap->Alloc (n); 00170 } 00171 void Free (void* p) 00172 { 00173 heap->Free (p); 00174 } 00175 void* Realloc (void* p, size_t newSize) 00176 { 00177 return heap->Realloc (p, newSize); 00178 } 00179 const HeapContainer& GetHeap () 00180 { 00181 return heap; 00182 } 00183 }; 00184 00190 template<class HeapPtr = Heap*> 00191 class AllocatorHeap : public AllocatorHeapBase<HeapAccessPointer<HeapPtr> > 00192 { 00193 public: 00194 }; 00195 } // namespace Memory 00196 } // namespace CS 00197 00200 #endif // __CS_CSUTIL_MEMHEAP_H__
Generated for Crystal Space by doxygen 1.4.7