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 >
68 class MemoryPool : private NonCopyable
69 {
70  private:
71  //**union FreeObject****************************************************************************
74  union FreeObject {
76  byte_t dummy_[ sizeof(Type) ];
77  };
78  //**********************************************************************************************
79 
80  //**struct Block********************************************************************************
85  struct Block
86  {
87  public:
88  //**Memory management functions**************************************************************
91  void init();
92  void free();
94  //*******************************************************************************************
95 
96  //**Member variables*************************************************************************
100 
101  //*******************************************************************************************
102  };
103  //**********************************************************************************************
104 
105  //**Type definitions****************************************************************************
106  typedef std::vector<Block> Blocks;
107  //**********************************************************************************************
108 
109  public:
110  //**Constructor*********************************************************************************
113  inline MemoryPool();
115  //**********************************************************************************************
116 
117  //**Destructor**********************************************************************************
120  inline ~MemoryPool();
122  //**********************************************************************************************
123 
124  //**Memory management functions*****************************************************************
127  inline void* malloc();
128  inline void free( void* rawMemory );
130  //**********************************************************************************************
131 
132  private:
133  //**Memory management functions*****************************************************************
136  inline bool checkMemory( FreeObject* rawMemory ) const;
138  //**********************************************************************************************
139 
140  //**Member variables****************************************************************************
144  Blocks blocks_;
145 
146  //**********************************************************************************************
147 };
148 
149 
150 
151 
152 //=================================================================================================
153 //
154 // CLASS MEMORYPOOL::BLOCK
155 //
156 //=================================================================================================
157 
158 //*************************************************************************************************
166 template< typename Type, size_t Blocksize >
168 {
169  rawMemory_ = new FreeObject[ Blocksize ];
170  for( size_t i=0; i<Blocksize-1; ++i ) {
171  rawMemory_[i].next_ = &rawMemory_[i+1];
172  }
173  rawMemory_[Blocksize-1].next_ = 0;
174 }
175 //*************************************************************************************************
176 
177 
178 //*************************************************************************************************
183 template< typename Type, size_t Blocksize >
185 {
186  delete [] rawMemory_;
187 }
188 //*************************************************************************************************
189 
190 
191 
192 
193 //=================================================================================================
194 //
195 // CLASS MEMORYPOOL
196 //
197 //=================================================================================================
198 
199 //*************************************************************************************************
202 template< typename Type, size_t Blocksize >
204 {
205  blocks_.push_back( Block() );
206  Block& block = blocks_.back();
207  block.init();
208  freeList_ = block.rawMemory_;
209 }
210 //*************************************************************************************************
211 
212 
213 //*************************************************************************************************
216 template< typename Type, size_t Blocksize >
218 {
219  for( typename Blocks::iterator it=blocks_.begin(); it!=blocks_.end(); ++it )
220  it->free();
221 }
222 //*************************************************************************************************
223 
224 
225 //*************************************************************************************************
230 template< typename Type, size_t Blocksize >
232 {
233  if( !freeList_ ) {
234  blocks_.push_back( Block() );
235  Block& block = blocks_.back();
236  block.init();
237  freeList_ = block.rawMemory_;
238  }
239 
240  void* ptr = freeList_;
242  return ptr;
243 }
244 //*************************************************************************************************
245 
246 
247 //*************************************************************************************************
253 template< typename Type, size_t Blocksize >
254 inline void MemoryPool<Type,Blocksize>::free( void* rawMemory )
255 {
256  FreeObject* ptr = reinterpret_cast<FreeObject*>( rawMemory );
257  BLAZE_INTERNAL_ASSERT( checkMemory( ptr ), "Memory pool check failed" );
258  ptr->next_ = freeList_;
259  freeList_ = ptr;
260 }
261 //*************************************************************************************************
262 
263 
264 //*************************************************************************************************
270 template< typename Type, size_t Blocksize >
272 {
273  for( typename Blocks::const_iterator it=blocks_.begin(); it!=blocks_.end(); ++it )
274  {
275  // Range check
276  if( toRelease >= it->rawMemory_ && toRelease < it->rawMemory_+Blocksize )
277  {
278  // Alignment check
279  const byte_t* const ptr1( reinterpret_cast<const byte_t*>(toRelease) );
280  const byte_t* const ptr2( reinterpret_cast<const byte_t*>(it->rawMemory_) );
281 
282  if( ( ptr1 - ptr2 ) % sizeof(FreeObject) != 0 ) return false;
283 
284  // Duplicate free check
285  FreeObject* ptr( freeList_ );
286  while( ptr ) {
287  if( ptr == toRelease ) return false;
288  ptr = ptr->next_;
289  }
290 
291  return true;
292  }
293  }
294  return false;
295 }
296 //*************************************************************************************************
297 
298 } // namespace blaze
299 
300 #endif
FreeObject * freeList_
Head of the free list.
Definition: MemoryPool.h:143
Header file for basic type definitions.
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:99
Base class for non-copyable class instances.
Blocks blocks_
Vector of available memory blocks.
Definition: MemoryPool.h:144
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:76
void init()
Initialization of a memory block.
Definition: MemoryPool.h:167
void free()
Release of the entire memory block.
Definition: MemoryPool.h:184
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:74
bool checkMemory(FreeObject *rawMemory) const
Performing a number of checks on the memory to be released.
Definition: MemoryPool.h:271
Header file for run time assertion macros.
std::vector< Block > Blocks
Vector of memory blocks.
Definition: MemoryPool.h:106
void free(void *rawMemory)
Deallocation of raw memory for an object of type Type.
Definition: MemoryPool.h:254
Memory block within the memory bool.
Definition: MemoryPool.h:85
void * malloc()
Allocation of raw memory for an object of type Type.
Definition: MemoryPool.h:231
~MemoryPool()
Destructor of the memory pool.
Definition: MemoryPool.h:217
#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:203
FreeObject * next_
Pointer to the next free object.
Definition: MemoryPool.h:75