CMS 3D CMS Logo

ChurnAllocator.h
Go to the documentation of this file.
1 #ifndef Tracker_ChurnAllocator_H
2 #define Tracker_ChurnAllocator_H
3 #include <memory>
4 
5 template <typename T>
6 class churn_allocator: public std::allocator<T>
7 {
8 public:
9  using Base = std::allocator<T>;
10  using pointer = typename Base::pointer;
11  using size_type = typename Base::size_type;
12 
13  struct Cache {
14  pointer cache = nullptr;
15  bool gard=false;
16  };
17 
18  static Cache & cache() {
19  static thread_local Cache local;
20  return local;
21  }
22 
23 
24  template<typename _Tp1>
25  struct rebind
26  {
28  };
29 
30  pointer allocate(size_type n, const void *hint=nullptr)
31  {
32  Cache & c = cache();
33  if (!c.gard)
34  c.cache = std::allocator<T>::allocate(n, hint);
35  c.gard=false; return c.cache;
36  }
37 
39  {
40  Cache & c = cache();
41  if (p==c.cache) c.gard=true;
42  else std::allocator<T>::deallocate(p, n);
43  }
44 
45  churn_allocator() = default;
46  churn_allocator(churn_allocator const&)=default;
47  churn_allocator(churn_allocator &&)=default;
48 
49  template <class U>
50  churn_allocator(const churn_allocator<U> &a) noexcept: std::allocator<T>(a) { }
51 };
52 
53 #endif
churn_allocator(const churn_allocator< U > &a) noexcept
typename Base::pointer pointer
churn_allocator< _Tp1 > other
std::allocator< T > Base
Definition: ChurnAllocator.h:9
uint16_t size_type
pointer allocate(size_type n, const void *hint=nullptr)
typename Base::size_type size_type
#define noexcept
void deallocate(pointer p, size_type n)
churn_allocator()=default
static Cache & cache()
double a
Definition: hdecay.h:121