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 public:
8  using Base = std::allocator<T>;
9  using pointer = typename Base::pointer;
10  using size_type = typename Base::size_type;
11 
12  struct Cache {
13  pointer cache = nullptr;
14  bool gard = false;
15  };
16 
17  static Cache &cache() {
18  static thread_local Cache local;
19  return local;
20  }
21 
22  template <typename _Tp1>
23  struct rebind {
25  };
26 
27  pointer allocate(size_type n, const void *hint = nullptr) {
28  Cache &c = cache();
29  if (!c.gard)
30  c.cache = std::allocator<T>::allocate(n, hint);
31  c.gard = false;
32  return c.cache;
33  }
34 
36  Cache &c = cache();
37  if (p == c.cache)
38  c.gard = true;
39  else
40  std::allocator<T>::deallocate(p, n);
41  }
42 
43  churn_allocator() = default;
44  churn_allocator(churn_allocator const &) = default;
45  churn_allocator(churn_allocator &&) = default;
46 
47  template <class U>
48  churn_allocator(const churn_allocator<U> &a) noexcept : std::allocator<T>(a) {}
49 };
50 
51 #endif
churn_allocator(const churn_allocator< U > &a) noexcept
typename Base::pointer pointer
Definition: ChurnAllocator.h:9
churn_allocator< _Tp1 > other
std::allocator< T > Base
Definition: ChurnAllocator.h:8
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:119