Class documentation of Concepts

Loading...
Searching...
No Matches
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
14namespace 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
37 Pool(Pool&);
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
Pool(unsigned sz=0)
Definition pool.hh:42
T * alloc()
Definition pool.hh:63
#define DEBUGL(doit, msg)
Definition debug.hh:40
Set< F > makeSet(uint n, const F &first,...)
Definition set.hh:320