pool.hh

Go to the documentation of this file.
1 
6 #ifndef pool_hh
7 #define pool_hh
8 
9 // debugging
10 #include "basics/debug.hh"
11 
12 #define PoolConstr_D 0
13 
14 namespace concepts {
15 
16  // ****************************************************************** Pool **
17 
22  template <class T>
23  class Pool {
24  union link {
25  void* lnk;
26  double align;
27  };
28 
29  const unsigned blkSz;
30  void* blk;
31 
33  unsigned free;
34 
35  T* next;
36 
38  void operator=(Pool&);
39  public:
42  Pool(unsigned sz = 0) : blkSz(sz), blk(0), free(0), next(0) {
43  DEBUGL(PoolConstr_D, "done.");
44  }
45  ~Pool();
46 
50  T* alloc();
51  float memory() const;
52  };
53 
54  template <class T>
56  while (blk) {
57  char* b = (char*)blk; blk = ((link*) blk)->lnk;
58  delete[] b;
59  }
60  }
61 
62  template <class T>
64  if (!free) {
65  void* b = new char[blkSz * sizeof(T) + sizeof(link)];
66  ((link*) b)->lnk = blk; blk = b;
67  next = (T*)((link*) blk + 1); free = blkSz;
68  }
69  free--; return next++;
70  }
71 
72  template <class T>
73  float Pool<T>::memory() const {
74  float mem = 0.0;
75  float blkmem = blkSz * sizeof(T) + sizeof(link);
76  for(void* b = blk; b != NULL; b = ((link*)b)->lnk) mem += blkmem;
77  return sizeof(Pool) + mem;
78  }
79 
80 } // namespace concepts
81 
82 #endif // pool_hh
T * next
Definition: pool.hh:35
void * blk
Definition: pool.hh:30
T * alloc()
Returns a pointer to a available location.
Definition: pool.hh:63
Pool(unsigned sz=0)
Constructor.
Definition: pool.hh:42
#define DEBUGL(doit, msg)
float memory() const
Definition: pool.hh:73
unsigned free
Stores the number of free entries.
Definition: pool.hh:33
void operator=(Pool &)
const unsigned blkSz
Definition: pool.hh:29
#define PoolConstr_D
Definition: pool.hh:12
Pool for blockwise memory allocation.
Definition: pool.hh:23
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich