dune-common  2.3.0
poolallocator.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // $Id$
4 #ifndef DUNE_COMMON_POOLALLOCATOR_HH
5 #define DUNE_COMMON_POOLALLOCATOR_HH
6 
11 #include "alignment.hh"
12 #include "static_assert.hh"
13 #include "lcm.hh"
14 #include <typeinfo>
15 #include <iostream>
16 #include <cassert>
17 #include <new>
18 
19 //forward declarations.
20 
21 // we need to know the test function to declare it friend
22 template<std::size_t size, typename T>
23 struct testPoolMain;
24 
25 namespace Dune
26 {
27 
28  template<typename T, std::size_t s>
29  class Pool;
30 
31  template<typename T, std::size_t s>
32  class PoolAllocator;
33 
34 }
35 
36 namespace std
37 {
38  /*
39  template<class T, std::size_t S>
40  inline ostream& operator<<(ostream& os, Dune::Pool<T,S>& pool)
41  {
42  os<<"pool="<<&pool<<" allocated_="<<pool.allocated_;
43  return os;
44  }
45 
46  template<class T, std::size_t S>
47  inline ostream& operator<<(ostream& os, Dune::PoolAllocator<T,S>& pool)
48  {
49  os<<pool.memoryPool_<<std::endl;
50  return os;
51  }
52  */
53 }
54 
55 
56 namespace Dune
57 {
88  template<class T, std::size_t s>
89  class Pool
90  {
91  // make the test function friend
92  friend struct ::testPoolMain<s,T>;
93 
94  //friend std::ostream& std::operator<<<>(std::ostream&,Pool<T,s>&);
95  template< class, std::size_t > friend class PoolAllocator;
96 
97  private:
98 
100  struct Reference
101  {
102  Reference *next_;
103  };
104 
105  public:
106 
108  typedef T MemberType;
109  enum
110  {
114  unionSize = ((sizeof(MemberType) < sizeof(Reference)) ?
115  sizeof(Reference) : sizeof(MemberType)),
116 
121  size = ((sizeof(MemberType) <= s && sizeof(Reference) <= s) ?
122  s : unionSize),
123 
129 
136  alignedSize = ((unionSize % alignment == 0) ?
137  unionSize :
138  ((unionSize / alignment + 1) * alignment)),
139 
147  chunkSize = ((size % alignment == 0) ?
148  size : ((size / alignment + 1)* alignment))
149  + alignment - 1,
150 
155  };
156 
157  private:
159  struct Chunk
160  {
161 
162  //friend int testPool<s,T>();
163 
165  char chunk_[chunkSize];
166 
171  char* memory_;
172 
174  Chunk *next_;
175 
179  Chunk()
180  {
181  // Make sure the alignment is correct!
182  // long long should be 64bit safe!
183  unsigned long long lmemory = reinterpret_cast<unsigned long long>(chunk_);
184  if(lmemory % alignment != 0)
185  lmemory = (lmemory / alignment + 1)
186  * alignment;
187 
188  memory_ = reinterpret_cast<char *>(lmemory);
189  }
190  };
191 
192  public:
194  inline Pool();
196  inline ~Pool();
201  inline void* allocate();
206  inline void free(void* o);
207 
211  inline void print(std::ostream& os);
212 
213  private:
214 
215  // Prevent Copying!
216  Pool(const Pool<MemberType,s>&);
217 
218  void operator=(const Pool<MemberType,s>& pool) const;
220  inline void grow();
222  Reference *head_;
224  Chunk *chunks_;
225  /* @brief The number of currently allocated elements. */
226  //size_t allocated_;
227 
228  };
229 
247  template<class T, std::size_t s>
249  {
250  //friend std::ostream& std::operator<<<>(std::ostream&,PoolAllocator<T,s>&);
251 
252  public:
256  typedef T value_type;
257 
258  enum
259  {
264  size=s*sizeof(value_type)
265  };
266 
270  typedef T* pointer;
271 
275  typedef const T* const_pointer;
276 
280  typedef T& reference;
281 
285  typedef const T& const_reference;
286 
290  typedef std::size_t size_type;
291 
295  typedef std::ptrdiff_t difference_type;
296 
300  inline PoolAllocator();
301 
305  template<typename U, std::size_t u>
307  {}
308 
315  inline pointer allocate(std::size_t n, const_pointer hint=0);
316 
324  inline void deallocate(pointer p, std::size_t n);
325 
331  inline void construct(pointer p, const_reference value);
332 
337  inline void destroy(pointer p);
338 
342  inline pointer address(reference x) const { return &x; }
343 
344 
348  inline const_pointer address(const_reference x) const { return &x; }
349 
353  inline int max_size() const throw(){ return 1;}
354 
358  template<class U>
359  struct rebind
360  {
362  };
363 
366 
367  private:
371  PoolType memoryPool_;
372  };
373 
374  // specialization for void
375  template <std::size_t s>
376  class PoolAllocator<void,s>
377  {
378  public:
379  typedef void* pointer;
380  typedef const void* const_pointer;
381  // reference to void members are impossible.
382  typedef void value_type;
383  template <class U> struct rebind
384  {
386  };
387 
388  template<typename T, std::size_t t>
390  {}
391 
392  };
393 
394 
395  template<typename T1, std::size_t t1, typename T2, std::size_t t2>
397  {
398  return false;
399  }
400 
401 
402  template<typename T1, std::size_t t1, typename T2, std::size_t t2>
404  {
405  return true;
406  }
407 
408  template<typename T, std::size_t t1, std::size_t t2>
410  {
411  return &p1==&p2;
412  }
413 
414 
415  template<typename T, std::size_t t1, std::size_t t2>
417  {
418  return &p1 != &p2;
419  }
420 
421  template<typename T, std::size_t t1, std::size_t t2>
423  {
424  return false;
425  }
426 
427 
428  template<typename T, std::size_t t1, std::size_t t2>
430  {
431  return true;
432  }
433 
434  template<std::size_t t1, std::size_t t2>
436  {
437  return &p1==&p2;
438  }
439 
440  template<std::size_t t1, std::size_t t2>
442  {
443  return &p1!=&p2;
444  }
445 
446  template<class T, std::size_t S>
448  : head_(0), chunks_(0) //, allocated_(0)
449  {
450  dune_static_assert(sizeof(T)<=unionSize, "Library Error: type T is too big");
451  dune_static_assert(sizeof(Reference)<=unionSize, "Library Error: type of referene is too big");
452  dune_static_assert(unionSize<=alignedSize, "Library Error: alignedSize too small");
453  dune_static_assert(sizeof(T)<=chunkSize, "Library Error: chunkSize must be able to hold at least one value");
454  dune_static_assert(sizeof(Reference)<=chunkSize, "Library Error: chunkSize must be able to hold at least one reference");
455  dune_static_assert((chunkSize - (alignment - 1)) % alignment == 0, "Library Error: compiler cannot calculate!");
456  dune_static_assert(elements>=1, "Library Error: we need to hold at least one element!");
457  dune_static_assert(elements*alignedSize<=chunkSize, "Library Error: aligned elements must fit into chuck!");
458  /* std::cout<<"s= "<<S<<" : T: "<<sizeof(T)<<" Reference: "<<sizeof(Reference)<<" union: "<<unionSize<<" alignment: "<<alignment<<
459  "aligned: "<<alignedSize<<" chunk: "<< chunkSize<<" elements: "<<elements<<std::endl;*/
460  }
461 
462  template<class T, std::size_t S>
464  {
465  /*
466  if(allocated_!=0)
467  std::cerr<<"There are still "<<allocated_<<" allocated elements by the Pool<"<<typeid(T).name()<<","<<S<<"> "
468  <<static_cast<void*>(this)<<"! This is a memory leak and might result in segfaults"
469  <<std::endl;
470  */
471  // delete the allocated chunks.
472  Chunk *current=chunks_;
473 
474  while(current!=0)
475  {
476  Chunk *tmp = current;
477  current = current->next_;
478  delete tmp;
479  }
480  }
481 
482  template<class T, std::size_t S>
483  inline void Pool<T,S>::print(std::ostream& os)
484  {
485  Chunk* current=chunks_;
486  while(current) {
487  os<<current<<" ";
488  current=current->next_;
489  }
490  os<<current<<" ";
491  }
492 
493  template<class T, std::size_t S>
494  inline void Pool<T,S>::grow()
495  {
496  Chunk *newChunk = new Chunk;
497  newChunk->next_ = chunks_;
498  chunks_ = newChunk;
499 
500  char* start = chunks_->memory_;
501  char* last = &start[elements*alignedSize];
502  Reference* ref = new (start) (Reference);
503 
504  // grow is only called if head==0,
505  assert(!head_);
506 
507  head_ = ref;
508 
509  for(char* element=start+alignedSize; element<last; element=element+alignedSize) {
510  Reference* next = new (element) (Reference);
511  ref->next_ = next;
512  ref = next;
513  }
514  ref->next_=0;
515  }
516 
517  template<class T, std::size_t S>
518  inline void Pool<T,S>::free(void* b)
519  {
520  if(b) {
521  Reference* freed = static_cast<Reference*>(b);
522  freed->next_ = head_;
523  head_ = freed;
524  //--allocated_;
525  }else
526  std::cerr<< "Tried to free null pointer! "<<b<<std::endl;
527  }
528 
529  template<class T, std::size_t S>
530  inline void* Pool<T,S>::allocate()
531  {
532  if(!head_)
533  grow();
534 
535  Reference* p = head_;
536  head_ = p->next_;
537  //++allocated_;
538  return p;
539  }
540 
541  template<class T, std::size_t s>
543  { }
544 
545  template<class T, std::size_t s>
546  inline typename PoolAllocator<T,s>::pointer
548  {
549  if(n==1)
550  return static_cast<T*>(memoryPool_.allocate());
551  else
552  throw std::bad_alloc();
553  }
554 
555  template<class T, std::size_t s>
556  inline void PoolAllocator<T,s>::deallocate(pointer p, std::size_t n)
557  {
558  for(size_t i=0; i<n; i++)
559  memoryPool_.free(p++);
560  }
561 
562  template<class T, std::size_t s>
564  {
565  ::new (static_cast<void*>(p))T(value);
566  }
567 
568  template<class T, std::size_t s>
570  {
571  p->~T();
572  }
573 
575 }
576 #endif