All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MemoryPool.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_MEMORYPOOL_H_
23 #define _BLAZE_UTIL_MEMORYPOOL_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <vector>
31 #include <blaze/util/Assert.h>
32 #include <blaze/util/Byte.h>
33 #include <blaze/util/NonCopyable.h>
34 #include <blaze/util/Types.h>
35 
36 
37 namespace blaze {
38 
39 //=================================================================================================
40 //
41 // CLASS DEFINITION
42 //
43 //=================================================================================================
44 
45 //*************************************************************************************************
55 template< typename Type, size_t Blocksize >
56 class MemoryPool : private NonCopyable
57 {
58  private:
59  //**union FreeObject****************************************************************************
62  union FreeObject {
64  byte dummy_[ sizeof(Type) ];
65  };
66  //**********************************************************************************************
67 
68  //**struct Block********************************************************************************
73  struct Block
74  {
75  public:
76  //**Memory management functions**************************************************************
79  void init();
80  void free();
82  //*******************************************************************************************
83 
84  //**Member variables*************************************************************************
88 
89  //*******************************************************************************************
90  };
91  //**********************************************************************************************
92 
93  //**Type definitions****************************************************************************
94  typedef std::vector<Block> Blocks;
95  //**********************************************************************************************
96 
97  public:
98  //**Constructor*********************************************************************************
101  inline MemoryPool();
103  //**********************************************************************************************
104 
105  //**Destructor**********************************************************************************
108  inline ~MemoryPool();
110  //**********************************************************************************************
111 
112  //**Memory management functions*****************************************************************
115  inline void* malloc();
116  inline void free( void* rawMemory );
118  //**********************************************************************************************
119 
120  private:
121  //**Memory management functions*****************************************************************
124  inline bool checkMemory( FreeObject* rawMemory ) const;
126  //**********************************************************************************************
127 
128  //**Member variables****************************************************************************
133 
134  //**********************************************************************************************
135 };
136 
137 
138 
139 
140 //=================================================================================================
141 //
142 // CLASS MEMORYPOOL::BLOCK
143 //
144 //=================================================================================================
145 
146 //*************************************************************************************************
154 template< typename Type, size_t Blocksize >
156 {
157  rawMemory_ = new FreeObject[ Blocksize ];
158  for( size_t i=0; i<Blocksize-1; ++i ) {
159  rawMemory_[i].next_ = &rawMemory_[i+1];
160  }
161  rawMemory_[Blocksize-1].next_ = 0;
162 }
163 //*************************************************************************************************
164 
165 
166 //*************************************************************************************************
171 template< typename Type, size_t Blocksize >
173 {
174  delete [] rawMemory_;
175 }
176 //*************************************************************************************************
177 
178 
179 
180 
181 //=================================================================================================
182 //
183 // CLASS MEMORYPOOL
184 //
185 //=================================================================================================
186 
187 //*************************************************************************************************
190 template< typename Type, size_t Blocksize >
192 {
193  blocks_.push_back( Block() );
194  Block& block = blocks_.back();
195  block.init();
196  freeList_ = block.rawMemory_;
197 }
198 //*************************************************************************************************
199 
200 
201 //*************************************************************************************************
204 template< typename Type, size_t Blocksize >
206 {
207  for( typename Blocks::iterator it=blocks_.begin(); it!=blocks_.end(); ++it )
208  it->free();
209 }
210 //*************************************************************************************************
211 
212 
213 //*************************************************************************************************
218 template< typename Type, size_t Blocksize >
220 {
221  if( !freeList_ ) {
222  blocks_.push_back( Block() );
223  Block& block = blocks_.back();
224  block.init();
225  freeList_ = block.rawMemory_;
226  }
227 
228  void* ptr = freeList_;
230  return ptr;
231 }
232 //*************************************************************************************************
233 
234 
235 //*************************************************************************************************
241 template< typename Type, size_t Blocksize >
242 inline void MemoryPool<Type,Blocksize>::free( void* rawMemory )
243 {
244  FreeObject* ptr = reinterpret_cast<FreeObject*>( rawMemory );
245  BLAZE_INTERNAL_ASSERT( checkMemory( ptr ), "Memory pool check failed" );
246  ptr->next_ = freeList_;
247  freeList_ = ptr;
248 }
249 //*************************************************************************************************
250 
251 
252 //*************************************************************************************************
258 template< typename Type, size_t Blocksize >
260 {
261  for( typename Blocks::const_iterator it=blocks_.begin(); it!=blocks_.end(); ++it )
262  {
263  // Range check
264  if( toRelease >= it->rawMemory_ && toRelease < it->rawMemory_+Blocksize )
265  {
266  // Alignment check
267  const byte* const ptr1( reinterpret_cast<const byte*>(toRelease) );
268  const byte* const ptr2( reinterpret_cast<const byte*>(it->rawMemory_) );
269 
270  if( ( ptr1 - ptr2 ) % sizeof(FreeObject) != 0 ) return false;
271 
272  // Duplicate free check
273  FreeObject* ptr( freeList_ );
274  while( ptr ) {
275  if( ptr == toRelease ) return false;
276  ptr = ptr->next_;
277  }
278 
279  return true;
280  }
281  }
282  return false;
283 }
284 //*************************************************************************************************
285 
286 } // namespace blaze
287 
288 #endif