Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_Details_Allocator.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
17 
18 #ifndef TEUCHOS_DETAILS_ALLOCATOR
19 #define TEUCHOS_DETAILS_ALLOCATOR
20 
21 #include <Teuchos_ConfigDefs.hpp>
22 #include <iostream>
23 #include <limits>
24 #include <type_traits>
25 #include <typeinfo>
26 
27 namespace Teuchos {
28 namespace Details {
29 
44 public:
48  typedef std::size_t size_type;
49 
73  static void
74  logAllocation (std::ostream& out,
75  const size_type numEntries,
76  const size_type numBytes,
77  const char typeName[],
78  const bool verbose);
79 
107  static void
108  logDeallocation (std::ostream& out,
109  const size_type numEntries,
110  const size_type numBytes,
111  const char typeName[],
112  const bool verbose);
113 
117  static size_type curAllocInBytes ();
118 
122  static size_type maxAllocInBytes ();
123 
127  static void resetAllocationCounts ();
128 
129 private:
131  static size_type curAllocInBytes_;
132 
134  static size_type maxAllocInBytes_;
135 };
136 
155 template<class T>
156 class Allocator {
157 private:
160  enum EAllocatorOp {
161  ALLOCATOR_ALLOCATE,
162  ALLOCATOR_DEALLOCATE
163  };
164 
166  bool tracking () const { return track_; }
167 
169  bool verbose () const { return verbose_; }
170 
171  // This lets tracking() and verbose() stay private,
172  // without breaking the templated copy constructor.
173  template<class U>
174  friend class Allocator;
175 
176 public:
178  typedef T value_type;
179 
183  typedef T* pointer;
187  typedef const T* const_pointer;
191  typedef T& reference;
195  typedef const T& const_reference;
196 
203 
211 #ifdef HAVE_TEUCHOSCORE_CXX11
212  typedef std::make_signed<size_type>::type difference_type;
213 #else
214  typedef std::ptrdiff_t difference_type;
215 #endif // HAVE_TEUCHOSCORE_CXX11
216 
219  track_ (true), verbose_ (false)
220  {}
221 
229  Allocator (const bool trackMemory,
230  const bool verboseOutput) :
231  track_ (trackMemory), verbose_ (verboseOutput)
232  {}
233 
235  template<class U>
236  Allocator (const Allocator<U>& src) :
237  track_ (src.tracking ()), verbose_ (src.verbose ())
238  {}
239 
249  template<class U>
250  struct rebind { typedef Allocator<U> other; };
251 
256  size_type max_size() const {
257  return std::numeric_limits<size_type>::max();
258  }
259 
268  value_type* allocate (const size_type& n, const void* = 0) {
269  if (tracking ()) {
270  AllocationLogger::logAllocation (std::cerr, n, n * sizeof (value_type),
271  typeid (value_type).name (), verbose_);
272  }
273  return (value_type*) (::operator new (n * sizeof (T)));
274  }
275 
280  void deallocate (value_type* p, const size_type& n) {
281  if (tracking ()) {
282  // Thankfully, this method accepts the array size. Thus, we don't
283  // have to do tricks like allocating extra space and stashing the
284  // size in the array.
285  AllocationLogger::logDeallocation (std::cerr, n, n * sizeof (value_type),
286  typeid (value_type).name (), verbose_);
287  }
288  ::operator delete ((void*) p);
289  }
290 
294  }
295 
299  }
300 
301 #ifndef HAVE_TEUCHOSCORE_CXX11
302  void construct (pointer p, const_reference val) {
314  new ((void*) p) T (val);
315  }
316 #endif // HAVE_TEUCHOSCORE_CXX11
317 
318 #ifndef HAVE_TEUCHOSCORE_CXX11
319  void destroy (pointer p) {
329  ((T*)p)->~T ();
330  }
331 #endif // HAVE_TEUCHOSCORE_CXX11
332 
333 private:
334  bool track_;
335  bool verbose_;
336 };
337 
338 
346 template<class T, class U>
347 bool operator== (const Allocator<T>&, const Allocator<U>&) {
348  return true;
349 }
350 
352 template<class T, class U>
353 bool operator!= (const Allocator<T>& a_t, const Allocator<U>& a_u) {
354  return ! (a_t == a_u);
355 }
356 
357 } // namespace Details
358 } // namespace Teuchos
359 
360 #endif // TEUCHOS_DETAILS_ALLOCATOR
AllocationLogger::size_type size_type
Type of the size of an allocation or deallocation.
const T & const_reference
Type of a reference to const T.
Allocator(const bool trackMemory, const bool verboseOutput)
Constructor.
Allocator(const Allocator< U > &src)
Copy constructor that takes an Allocator&lt;U&gt; for any U.
void construct(pointer p, const_reference val)
Invoke the constructor of an instance of T, without allocating.
size_type maxAllocInBytes()
Max total allocation (&quot;high water mark&quot;) in bytes, over all Allocator&lt;U&gt;.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
std::ptrdiff_t difference_type
Integer type representing the difference between two pointers.
Optional tracking allocator for Teuchos Memory Management classes.
Logging implementation used by Allocator (see below).
const T * const_pointer
Type of a pointer to const T.
Mapping to an Allocator for a different type U.
T * pointer
Type of a pointer to T.
T & reference
Type of a reference to T.
std::size_t size_type
Type of the size of an allocation or deallocation.
static void resetAllocationCounts()
Reset the current and max total allocation numbers to zero.
static void logAllocation(std::ostream &out, const size_type numEntries, const size_type numBytes, const char typeName[], const bool verbose)
Log an allocation.
T value_type
Type of the template parameter of this class.
size_type max_size() const
Upper bound (possibly loose) on maximum allocation size.
void destroy(pointer p)
Invoke the destructor of an instance of T, without deallocating.
static size_type maxAllocInBytes()
Max total allocation (&quot;high water mark&quot;) in bytes.
value_type * allocate(const size_type &n, const void *=0)
Allocate an array of n instances of value_type.
static void logDeallocation(std::ostream &out, const size_type numEntries, const size_type numBytes, const char typeName[], const bool verbose)
Log a deallocation, that was previously logged using logAllocation().
static size_type curAllocInBytes()
Current total allocation in bytes.
void deallocate(value_type *p, const size_type &n)
Deallocate n instances of value_type.
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
size_type curAllocInBytes()
Current total allocation in bytes, over all Allocator&lt;U&gt;.