MemoryPool.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_MEMORYPOOL_H_
36 #define _BLAZE_UTIL_MEMORYPOOL_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <vector>
44 #include <blaze/util/Assert.h>
45 #include <blaze/util/NonCopyable.h>
46 #include <blaze/util/Types.h>
47 
48 
49 namespace blaze {
50 
51 //=================================================================================================
52 //
53 // CLASS DEFINITION
54 //
55 //=================================================================================================
56 
57 //*************************************************************************************************
67 template< typename Type, size_t Blocksize >
69  : private NonCopyable
70 {
71  private:
72  //**union FreeObject****************************************************************************
75  union FreeObject {
77  byte_t dummy_[ sizeof(Type) ];
78  };
79  //**********************************************************************************************
80 
81  //**struct Block********************************************************************************
86  struct Block
87  {
88  public:
89  //**Memory management functions**************************************************************
92  void init();
93  void free();
95  //*******************************************************************************************
96 
97  //**Member variables*************************************************************************
101 
102  //*******************************************************************************************
103  };
104  //**********************************************************************************************
105 
106  //**Type definitions****************************************************************************
107  using Blocks = std::vector<Block>;
108  //**********************************************************************************************
109 
110  public:
111  //**Constructor*********************************************************************************
114  inline MemoryPool();
116  //**********************************************************************************************
117 
118  //**Destructor**********************************************************************************
121  inline ~MemoryPool();
123  //**********************************************************************************************
124 
125  //**Memory management functions*****************************************************************
128  inline void* malloc();
129  inline void free( void* rawMemory );
131  //**********************************************************************************************
132 
133  private:
134  //**Memory management functions*****************************************************************
137  inline bool checkMemory( FreeObject* rawMemory ) const;
139  //**********************************************************************************************
140 
141  //**Member variables****************************************************************************
146 
147  //**********************************************************************************************
148 };
149 
150 
151 
152 
153 //=================================================================================================
154 //
155 // CLASS MEMORYPOOL::BLOCK
156 //
157 //=================================================================================================
158 
159 //*************************************************************************************************
167 template< typename Type, size_t Blocksize >
169 {
170  rawMemory_ = new FreeObject[ Blocksize ];
171  for( size_t i=0; i<Blocksize-1; ++i ) {
172  rawMemory_[i].next_ = &rawMemory_[i+1];
173  }
174  rawMemory_[Blocksize-1].next_ = 0;
175 }
176 //*************************************************************************************************
177 
178 
179 //*************************************************************************************************
184 template< typename Type, size_t Blocksize >
186 {
187  delete [] rawMemory_;
188 }
189 //*************************************************************************************************
190 
191 
192 
193 
194 //=================================================================================================
195 //
196 // CLASS MEMORYPOOL
197 //
198 //=================================================================================================
199 
200 //*************************************************************************************************
203 template< typename Type, size_t Blocksize >
205 {
206  blocks_.push_back( Block() );
207  Block& block = blocks_.back();
208  block.init();
209  freeList_ = block.rawMemory_;
210 }
211 //*************************************************************************************************
212 
213 
214 //*************************************************************************************************
217 template< typename Type, size_t Blocksize >
219 {
220  for( typename Blocks::iterator it=blocks_.begin(); it!=blocks_.end(); ++it )
221  it->free();
222 }
223 //*************************************************************************************************
224 
225 
226 //*************************************************************************************************
231 template< typename Type, size_t Blocksize >
233 {
234  if( !freeList_ ) {
235  blocks_.push_back( Block() );
236  Block& block = blocks_.back();
237  block.init();
238  freeList_ = block.rawMemory_;
239  }
240 
241  void* ptr = freeList_;
243  return ptr;
244 }
245 //*************************************************************************************************
246 
247 
248 //*************************************************************************************************
254 template< typename Type, size_t Blocksize >
255 inline void MemoryPool<Type,Blocksize>::free( void* rawMemory )
256 {
257  FreeObject* ptr = reinterpret_cast<FreeObject*>( rawMemory );
258  BLAZE_INTERNAL_ASSERT( checkMemory( ptr ), "Memory pool check failed" );
259  ptr->next_ = freeList_;
260  freeList_ = ptr;
261 }
262 //*************************************************************************************************
263 
264 
265 //*************************************************************************************************
271 template< typename Type, size_t Blocksize >
273 {
274  for( typename Blocks::const_iterator it=blocks_.begin(); it!=blocks_.end(); ++it )
275  {
276  // Range check
277  if( toRelease >= it->rawMemory_ && toRelease < it->rawMemory_+Blocksize )
278  {
279  // Alignment check
280  const byte_t* const ptr1( reinterpret_cast<const byte_t*>(toRelease) );
281  const byte_t* const ptr2( reinterpret_cast<const byte_t*>(it->rawMemory_) );
282 
283  if( ( ptr1 - ptr2 ) % sizeof(FreeObject) != 0 ) return false;
284 
285  // Duplicate free check
286  FreeObject* ptr( freeList_ );
287  while( ptr ) {
288  if( ptr == toRelease ) return false;
289  ptr = ptr->next_;
290  }
291 
292  return true;
293  }
294  }
295  return false;
296 }
297 //*************************************************************************************************
298 
299 } // namespace blaze
300 
301 #endif
std::vector< Block > Blocks
Vector of memory blocks.
Definition: MemoryPool.h:107
FreeObject * freeList_
Head of the free list.
Definition: MemoryPool.h:144
Header file for basic type definitions.
bool checkMemory(FreeObject *rawMemory) const
Performing a number of checks on the memory to be released.
Definition: MemoryPool.h:272
unsigned char byte_t
Byte data type of the Blaze library.The byte data type is guaranteed to be an integral data type of s...
Definition: Types.h:79
FreeObject * rawMemory_
Allocated memory pool of the block.
Definition: MemoryPool.h:100
Base class for non-copyable class instances.
Blocks blocks_
Vector of available memory blocks.
Definition: MemoryPool.h:145
Memory pool for small objects.The memory pool efficiently improves the performance of dynamic memory ...
Definition: MemoryPool.h:68
byte_t dummy_[sizeof(Type)]
Dummy array to create an object of the appropriate size.
Definition: MemoryPool.h:77
void init()
Initialization of a memory block.
Definition: MemoryPool.h:168
void free()
Release of the entire memory block.
Definition: MemoryPool.h:185
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Base class for non-copyable class instances.The NonCopyable class is intended to work as a base class...
Definition: NonCopyable.h:63
A single element of the free list of the memory pool.
Definition: MemoryPool.h:75
Header file for run time assertion macros.
void free(void *rawMemory)
Deallocation of raw memory for an object of type Type.
Definition: MemoryPool.h:255
Memory block within the memory bool.
Definition: MemoryPool.h:86
void * malloc()
Allocation of raw memory for an object of type Type.
Definition: MemoryPool.h:232
~MemoryPool()
Destructor of the memory pool.
Definition: MemoryPool.h:218
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
MemoryPool()
Constructor of the memory pool.
Definition: MemoryPool.h:204
FreeObject * next_
Pointer to the next free object.
Definition: MemoryPool.h:76