csutil/blockallocator.h
Go to the documentation of this file.00001 /* 00002 Crystal Space Generic Object Block Allocator 00003 Copyright (C) 2005 by Eric Sunshine <[email protected]> 00004 (C) 2006 by Frank Richter 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public 00017 License along with this library; if not, write to the Free 00018 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 */ 00020 #ifndef __CSUTIL_BLOCKALLOCATOR_H__ 00021 #define __CSUTIL_BLOCKALLOCATOR_H__ 00022 00027 #include "csutil/fixedsizeallocator.h" 00028 00029 #include "csutil/custom_new_disable.h" 00030 00037 template<typename T> 00038 class csBlockAllocatorDisposeDelete 00039 { 00040 public: 00044 template<typename BA> 00045 csBlockAllocatorDisposeDelete (const BA&, bool legit) 00046 { (void)legit; } 00048 void Dispose (void* p) 00049 { 00050 ((T*)p)->~T(); 00051 } 00052 }; 00053 00058 template<typename T> 00059 class csBlockAllocatorDisposeLeaky 00060 { 00061 bool doWarn; 00062 #ifdef CS_DEBUG 00063 const char* parentClass; 00064 const void* parent; 00065 size_t count; 00066 #endif 00067 public: 00068 #ifdef CS_DEBUG 00069 00076 template<typename BA> 00077 csBlockAllocatorDisposeLeaky (const BA& ba, bool legit) : 00078 doWarn (!legit), parentClass (typeid(BA).name()), parent (&ba), 00079 count (0) 00080 { 00081 } 00082 #else 00083 template<typename BA> 00084 csBlockAllocatorDisposeLeaky (const BA&, bool legit) : doWarn (!legit) 00085 { } 00086 #endif 00087 ~csBlockAllocatorDisposeLeaky() 00088 { 00089 #ifdef CS_DEBUG 00090 if ((count > 0) && doWarn) 00091 { 00092 csPrintfErr("%s %p leaked %zu objects.\n", parentClass, (void*)this, 00093 count); 00094 } 00095 #endif 00096 } 00098 void Dispose (void* p) 00099 { 00100 if (!doWarn) ((T*)p)->~T(); 00101 #ifdef CS_DEBUG 00102 count++; 00103 #endif 00104 } 00105 }; 00106 00127 template <class T, 00128 class Allocator = CS::Memory::AllocatorMalloc, 00129 class ObjectDispose = csBlockAllocatorDisposeDelete<T> > 00130 class csBlockAllocator : public csFixedSizeAllocator<sizeof (T), Allocator> 00131 { 00132 public: 00133 typedef csBlockAllocator<T, Allocator> ThisType; 00134 typedef T ValueType; 00135 typedef Allocator AllocatorType; 00136 00137 protected: 00138 typedef csFixedSizeAllocator<sizeof (T), Allocator> superclass; 00139 private: 00140 void* Alloc (size_t /*n*/) { return 0; } // Illegal 00141 void* Alloc (void* /*p*/, size_t /*newSize*/) { return 0; } // Illegal 00142 void SetMemTrackerInfo (const char* /*info*/) { } // Illegal 00143 public: 00163 csBlockAllocator(size_t nelem = 32) : superclass (nelem) 00164 { 00165 } 00166 00170 ~csBlockAllocator() 00171 { 00172 ObjectDispose dispose (*this, false); 00173 DisposeAll (dispose); 00174 } 00175 00181 void Empty() 00182 { 00183 ObjectDispose dispose (*this, true); 00184 DisposeAll (dispose); 00185 } 00186 00191 T* Alloc () 00192 { 00193 return new (superclass::Alloc()) T; 00194 } 00195 00200 void Free (T* p) 00201 { 00202 ObjectDispose dispose (*this, true); 00203 superclass::Free (dispose, p); 00204 } 00210 bool TryFree (T* p) 00211 { 00212 ObjectDispose dispose (*this, true); 00213 return superclass::TryFree (dispose, p); 00214 } 00215 }; 00216 00219 #include "csutil/custom_new_enable.h" 00220 00221 #endif // __CSUTIL_BLOCKALLOCATOR_H__
Generated for Crystal Space by doxygen 1.4.7