LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
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 
42  debugging_allocator(debugging_allocator const&) throw() : dummy('c') {}
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 } // art
115 
116 #endif /* art_Persistency_Common_debugging_allocator_h */
117 
118 // Local Variables:
119 // mode: c++
120 // End:
Float_t x
Definition: compare.C:6
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 &)
Int_t max
Definition: plot.C:27
void deallocate(pointer p, size_type num)
pointer address(reference value) const
pointer allocate(size_type num, void const *hint=0)
std::string value(boost::any const &)
void construct(pointer p, T const &value)
HLT enums.
size_type max_size() const
bool operator==(Provenance const &a, Provenance const &b)
Definition: Provenance.h:168
const_pointer address(const_reference value) const