Home | nevrax.com |
|
pool_memory.hGo to the documentation of this file.00001 00007 /* Copyright, 2001 Nevrax Ltd. 00008 * 00009 * This file is part of NEVRAX NEL. 00010 * NEVRAX NEL is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2, or (at your option) 00013 * any later version. 00014 00015 * NEVRAX NEL is distributed in the hope that it will be useful, but 00016 * WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * General Public License for more details. 00019 00020 * You should have received a copy of the GNU General Public License 00021 * along with NEVRAX NEL; see the file COPYING. If not, write to the 00022 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 00023 * MA 02111-1307, USA. 00024 */ 00025 00026 #ifndef NL_POOL_MEMORY_H 00027 #define NL_POOL_MEMORY_H 00028 00029 #include "nel/misc/types_nl.h" 00030 #include <list> 00031 #include <vector> 00032 00033 namespace NLMISC 00034 { 00035 00036 00048 template<class T> 00049 class CPoolMemory 00050 { 00051 public: 00052 /* 00053 * Constructor 00054 * 00055 * \param blockSize is the default bloc size 00056 */ 00057 CPoolMemory (uint blockSize=10) 00058 { 00059 _BlockSize=blockSize; 00060 _BlockPointer=_BlockList.end(); 00061 } 00062 00063 /* 00064 * Allocate an element. 00065 * 00066 * Return a pointer on the element. 00067 */ 00068 T* allocate () 00069 { 00070 // End of block ? 00071 if ( (_BlockPointer!=_BlockList.end()) && (_BlockPointer->size()>=_BlockSize) ) 00072 { 00073 // Get next block 00074 _BlockPointer++; 00075 } 00076 00077 // Out of memory ? 00078 if (_BlockPointer==_BlockList.end()) 00079 { 00080 // Add a block 00081 _BlockList.resize (_BlockList.size()+1); 00082 00083 // Pointer on the new block 00084 _BlockPointer=_BlockList.end (); 00085 _BlockPointer--; 00086 00087 // Reserve it 00088 _BlockPointer->reserve (_BlockSize); 00089 } 00090 00091 // Allocate 00092 _BlockPointer->resize (_BlockPointer->size()+1); 00093 00094 // Return the pointer 00095 return &*((_BlockPointer->end ()-1)); 00096 } 00097 00098 /* 00099 * Free all the elements allocated since last free(). Memory is kept for next allocations. 00100 */ 00101 void free () 00102 { 00103 std::list< std::vector<T> >::iterator ite=_BlockList.begin(); 00104 while (ite!=_BlockList.end()) 00105 { 00106 // Clear the block 00107 ite->clear (); 00108 00109 // Check size in not zero 00110 nlassert (ite->capacity ()>0); 00111 00112 ite++; 00113 } 00114 // Pointer at the begining 00115 _BlockPointer=_BlockList.begin(); 00116 } 00117 00118 /* 00119 * Purge memory. All the memory used by the allocator is freid for real. 00120 */ 00121 void purge () 00122 { 00123 _BlockList.clear(); 00124 _BlockPointer=_BlockList.end(); 00125 } 00126 00127 private: 00128 uint _BlockSize; 00129 std::list< std::vector<T> > _BlockList; 00130 std::list< std::vector<T> >::iterator _BlockPointer; 00131 }; 00132 00133 00134 } // NLMISC 00135 00136 00137 #endif // NL_POOL_MEMORY_H 00138 00139 /* End of pool_memory.h */ |