debugging_allocator.h
Go to the documentation of this file.
1 #ifndef art_Persistency_Common_debugging_allocator_h
2 #define art_Persistency_Common_debugging_allocator_h
3 
4 //---------------------------------------------------------------------
5 //
6 // This file declares and defines an allocator class template.
7 // This allocator is intended for use with the memory checking tool
8 // valgrind. It is a minimum conformant implementation which makes sure
9 // not use use any unitialized memory, and so it causes no spurious error
10 // reports from valgrind.
11 //
12 // The intended use is in the declarations of objects from STL templates,
13 // e.g.
14 // typedef vector<int, art::debugging_allocator<int> > vint;
15 // etc.
16 //
17 //
18 //---------------------------------------------------------------------
19 
20 #include <cstddef>
21 #include <limits>
22 
23 namespace art {
24  template <class T>
26  public:
27  typedef std::size_t size_type;
28  typedef ptrdiff_t difference_type;
29  typedef T* pointer;
30  typedef T const* const_pointer;
31  typedef T& reference;
32  typedef T const& const_reference;
33  typedef T value_type;
34 
35  template <class U>
36  struct rebind {
38  };
39 
40  debugging_allocator() throw() : dummy('x') {}
41 
43 
44  template <class U>
46  {}
47 
48  ~debugging_allocator() throw(){};
49 
50  pointer
51  address(reference value) const
52  {
53  return &value;
54  }
55 
56  const_pointer
57  address(const_reference value) const
58  {
59  return &value;
60  }
61 
62  size_type
63  max_size() const throw()
64  {
65  return std::numeric_limits<size_type>::max() / sizeof(T);
66  }
67 
68  pointer
69  allocate(size_type num, void const* hint = 0)
70  {
71  // allocate objects with global new
72  return (pointer)(::operator new(num * sizeof(T)));
73  }
74 
75  void
76  construct(pointer p, T const& value)
77  {
78  new ((void*)p) T(value);
79  }
80 
81  void
82  destroy(pointer p)
83  {
84  p->~T();
85  }
86 
87  void
88  deallocate(pointer p, size_type num)
89  {
90  ::operator delete((void*)p);
91  }
92 
93  private:
94  char dummy;
95  };
96 
97  // instances of all specializations of this allocator are equal
98  template <class X, class Y>
99  bool
101  debugging_allocator<Y> const&) throw()
102  {
103  return true;
104  }
105 
106  template <class X, class Y>
107  bool
109  debugging_allocator<Y> const&) throw()
110  {
111  return false;
112  }
113 
114 } // namespace art
115 
116 #endif /* art_Persistency_Common_debugging_allocator_h */
117 
118 // Local Variables:
119 // mode: c++
120 // End:
bool operator==(Provenance const &a, Provenance const &b) noexcept
Definition: Provenance.cc:141
debugging_allocator(debugging_allocator const &)
debugging_allocator< U > other
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
debugging_allocator(debugging_allocator< U > const &)
void deallocate(pointer p, size_type num)
pointer address(reference value) const
p
Definition: test.py:223
static int max(int a, int b)
pointer allocate(size_type num, void const *hint=0)
void construct(pointer p, T const &value)
list x
Definition: train.py:276
size_type max_size() const
const_pointer address(const_reference value) const