CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BlockWipedAllocator.h
Go to the documentation of this file.
1 #ifndef BlockWipedAllocator_H
2 #define BlockWipedAllocator_H
3 
4 #include<vector>
5 #include<list>
6 // #include<map>
7 #include <ext/hash_map>
8 
9 #include <algorithm>
10 #include<memory>
11 
12 #include<boost/bind.hpp>
13 
15 
16 
17 // #include<iostream>
18 
19 /* Allocator that never removes single allocations
20  * it "wipes" or "clears" the whole allocation when not needed anymore
21  * if not wiped it may easily run out of memory
22  */
24 public:
25  BlockWipedAllocator( std::size_t typeSize,
26  std::size_t blockSize,
27  std::size_t maxRecycle
28  );
29 
30 
31  /* copy constructor clone the allocator and the memory it manages
32  * it needs to be reinitialized to avoid pointing to "rh"
33  *
34  */
36 
39 
40  void * alloc();
41 
42  void dealloc(void *);
43 
44  // redime memory to the system heap
45  void clear() const;
46 
47  // reset allocator status. does not redime memory
48  void wipe(bool force=true) const;
49 
50 
51  // longliving (static) local caches: to be reset at wipe
52  struct LocalCache {
53  virtual ~LocalCache(){}
54  virtual void reset()=0;
55  };
56 
58  localCaches.push_back(c);
59  }
60 
61 private:
62  std::vector<LocalCache*> localCaches;
63  std::vector<void *> recycled;
64 
65 
66 protected:
67 
68  BlockWipedAllocator & me() const;
69 
70 public:
71 
72  struct Stat {
73  size_t typeSize;
74  size_t blockSize;
78  size_t nBlocks;
79  int alive;
80  };
81 
82  Stat stat() const;
83 
84 private:
85  void nextBlock(bool advance) dso_internal;
86 
87 
88  struct Block {
89  std::size_t m_allocated;
90  std::vector<unsigned char> m_data;
91  };
92 
93  typedef unsigned char * pointer;
94  typedef std::list<Block> Blocks;
95  typedef Blocks::iterator iterator;
96  typedef Blocks::const_iterator const_iterator;
97 
98 
99  std::size_t m_typeSize;
100  std::size_t m_blockSize;
101  std::size_t m_maxRecycle;
105 
106  int m_alive; // for stat purposes
107 
108 };
109 
110 
112 public:
114  // typedef std::map<std::size_t, Allocator> Pool;
115  typedef __gnu_cxx::hash_map<std::size_t, Allocator> Pool;
116 
117  BlockWipedPool(std::size_t blockSize, std::size_t maxRecycle);
118  ~BlockWipedPool();
119 
120  Allocator & allocator( std::size_t typeSize);
121 
122  void wipe(bool force=true);
123 
124  void clear();
125 
126  template<typename Visitor>
127  void visit(Visitor& visitor) const {
128  std::for_each(m_pool.begin(),m_pool.end(),boost::bind(&Visitor::visit,visitor,
129  boost::bind(&Pool::value_type::second,_1)
130  ));
131  }
132 
133 
134 private:
135  std::size_t m_blockSize;
136  std::size_t m_maxRecycle;
139  std::size_t m_lastSize;
140 };
141 
142 
143 
144 /* generaric Basic class
145  *
146  */
148 public:
150  // instance counter...
151  static int s_alive;
152  static void * operator new(size_t s, void * p);
153  static void * operator new(size_t s);
154 
155  static void operator delete(void * p, size_t s);
156 
157  static BlockWipedAllocator & allocator(size_t s);
158 
159 
160  static BlockWipedAllocator::Stat stat(size_t s);
161 
162  // throw id s_alive!=0???
163  static void usePool();
164 
165 
166  // private:
167  static bool s_usePool;
168  // static BlockAllocator * s_allocator;
169 };
170 
171 // singleton
173 
174 template<size_t S>
176  static BlockWipedAllocator & local = blockWipedPool().allocator(S);
177  return local;
178 }
179 
180 template<typename T>
182  std::auto_ptr<T> ptr;
185  blockWipedAllocator<sizeof(T)>().registerCache(this);
186  }
188  void reset(){ ptr.reset();}
189 };
190 
191 
192 /* Allocator by type
193  *
194  */
195 template<typename T>
197 public:
198  static void * operator new(size_t) {
199  return alloc();
200  }
201 
202  static void operator delete(void * p) {
203  dealloc(p);
204  }
205 
206  static void * operator new(size_t, void * p) {
207  return p;
208  }
209 
210  static void * alloc() {
212  return (BlockWipedPoolAllocated::s_usePool) ? allocator().alloc() : ::operator new(sizeof(T));
213  }
214 
215  static void dealloc(void * p) {
216  if (0==p) return;
219  }
220 
221 #ifdef __GXX_EXPERIMENTAL_CXX0X__
222  template<typename... Args>
223  static void
224  construct(T * p, Args&&... args)
225  { ::new(p) T(std::forward<Args>(args)...); }
226 #endif
227 
228 
229  static void destroy( T * p) { p->~T(); }
230 
231 
233  static BlockWipedAllocator & local = blockWipedPool().allocator(sizeof(T));
234  return local;
235  }
236 
237 
239  return allocator().stat();
240  }
241 
242 
243 private:
244 
245  // static BlockAllocator * s_allocator;
246 };
247 
248 #ifdef __GXX_EXPERIMENTAL_CXX0X__
249 template<typename B>
250 struct BWADestroy {
251  BWADestroy() {}
252  void operator () (B* p) {
253  if (0==p) return;
256  BlockWipedAllocator & local = blockWipedPool().allocator(p->size());
257  p->~B();
258  local.dealloc(p);
259  }
260  else { delete p; }
261  }
262  };
263 
264 template<typename B>
265 struct BWAFactory {
266  typedef BWADestroy<B> Destroy;
267  typedef std::unique_ptr<B, BWAFactory<B>::Destroy> UP;
268  template<typename T, typename... Args>
269  static UP create(Args&&... args) {
270  // create derived, destroy base
272  Destroy()
273  );
274  BlockWipedAllocated<T>::construct((T*)ret.get(),std::forward<Args>(args)...);
275  return ret;
276  }
277 };
278 #endif
279 
280 
281 
282 /* Allocator by size (w/o pool)
283  *
284  */
285 template<typename T>
287 public:
288  static void * operator new(size_t) {
289  return allocator().alloc();
290  }
291 
292  static void operator delete(void * p) {
293  allocator().dealloc(p);
294  }
295 
297  static BlockWipedAllocator & local = blockWipedAllocator<sizeof(T)>();
298  return local;
299  }
300 
301 
303  return allocator().stat();
304  }
305 
306 private:
307 
308  // static BlockAllocator * s_allocator;
309 };
310 
311 
312 #endif // BlockAllocator_H
std::vector< void * > recycled
static void destroy(T *p)
std::vector< LocalCache * > localCaches
__gnu_cxx::hash_map< std::size_t, Allocator > Pool
std::size_t m_maxRecycle
Allocator & allocator(std::size_t typeSize)
static BlockWipedAllocator & allocator()
Blocks::iterator iterator
static BlockWipedAllocator & allocator(size_t s)
static void dealloc(void *p)
std::vector< unsigned char > m_data
BlockWipedPool & blockWipedPool(BlockWipedPool *p=0)
def visit
Retrieve data from a perf suite output (sub) directory, only examines TimeSize at the moment...
BlockWipedAllocator & blockWipedAllocator()
U second(std::pair< T, U > const &p)
BlockWipedAllocator & operator=(BlockWipedAllocator const &rh)
BlockWipedAllocator Allocator
BlockWipedPool(std::size_t blockSize, std::size_t maxRecycle)
std::size_t m_blockSize
static BlockWipedAllocator::Stat stat(size_t s)
#define dso_internal
Definition: Visibility.h:13
void registerCache(LocalCache *c)
BlockWipedAllocator & me() const
std::size_t m_lastSize
void visit(Visitor &visitor) const
dictionary args
static BlockWipedAllocator & allocator()
void wipe(bool force=true)
Blocks::const_iterator const_iterator
static BlockWipedAllocator::Stat stat()
void wipe(bool force=true) const
void nextBlock(bool advance)
std::list< Block > Blocks
unsigned char * pointer
BlockWipedAllocator(std::size_t typeSize, std::size_t blockSize, std::size_t maxRecycle)
long double T
SurfaceDeformation * create(int type, const std::vector< double > &params)
std::auto_ptr< T > ptr
static BlockWipedAllocator::Stat stat()