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 <sunshine@sunshineco.com> 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 00123 template <class T, 00124 class Allocator = CS::Memory::AllocatorMalloc, 00125 class ObjectDispose = csBlockAllocatorDisposeDelete<T> > 00126 class csBlockAllocator : public csFixedSizeAllocator<sizeof (T), Allocator> 00127 { 00128 public: 00129 typedef csBlockAllocator<T, Allocator> ThisType; 00130 typedef T ValueType; 00131 typedef Allocator AllocatorType; 00132 00133 protected: 00134 typedef csFixedSizeAllocator<sizeof (T), Allocator> superclass; 00135 private: 00136 void* Alloc (size_t /*n*/) { return 0; } // Illegal 00137 void* Alloc (void* /*p*/, size_t /*newSize*/) { return 0; } // Illegal 00138 void SetMemTrackerInfo (const char* /*info*/) { } // Illegal 00139 public: 00159 csBlockAllocator(size_t nelem = 32) : superclass (nelem) 00160 { 00161 } 00162 00166 ~csBlockAllocator() 00167 { 00168 ObjectDispose dispose (*this, false); 00169 DisposeAll (dispose); 00170 } 00171 00177 void Empty() 00178 { 00179 ObjectDispose dispose (*this, true); 00180 DisposeAll (dispose); 00181 } 00182 00187 T* Alloc () 00188 { 00189 return new (superclass::Alloc()) T; 00190 } 00191 00196 template<typename A1, typename A2> 00197 T* Alloc (A1& a1, A2& a2) 00198 { 00199 return new (superclass::Alloc()) T (a1, a2); 00200 } 00201 00206 template<typename A1, typename A2, typename A3> 00207 T* Alloc (A1& a1, A2& a2, A3& a3) 00208 { 00209 return new (superclass::Alloc()) T (a1, a2, a3); 00210 } 00211 00216 void Free (T* p) 00217 { 00218 ObjectDispose dispose (*this, true); 00219 superclass::Free (dispose, p); 00220 } 00226 bool TryFree (T* p) 00227 { 00228 ObjectDispose dispose (*this, true); 00229 return superclass::TryFree (dispose, p); 00230 } 00231 }; 00232 00235 #include "csutil/custom_new_enable.h" 00236 00237 #endif // __CSUTIL_BLOCKALLOCATOR_H__
Generated for Crystal Space 1.4.0 by doxygen 1.5.8