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 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
49 
50 #ifndef TEUCHOS_DETAILS_ALLOCATOR
51 #define TEUCHOS_DETAILS_ALLOCATOR
52 
53 #include <Teuchos_ConfigDefs.hpp>
54 #include <iostream>
55 #include <limits>
56 #include <type_traits>
57 #include <typeinfo>
58 
59 namespace Teuchos {
60 namespace Details {
61 
76 public:
80  typedef std::size_t size_type;
81 
105  static void
106  logAllocation (std::ostream& out,
107  const size_type numEntries,
108  const size_type numBytes,
109  const char typeName[],
110  const bool verbose);
111 
139  static void
140  logDeallocation (std::ostream& out,
141  const size_type numEntries,
142  const size_type numBytes,
143  const char typeName[],
144  const bool verbose);
145 
149  static size_type curAllocInBytes ();
150 
154  static size_type maxAllocInBytes ();
155 
159  static void resetAllocationCounts ();
160 
161 private:
163  static size_type curAllocInBytes_;
164 
166  static size_type maxAllocInBytes_;
167 };
168 
187 template<class T>
188 class Allocator {
189 private:
192  enum EAllocatorOp {
193  ALLOCATOR_ALLOCATE,
194  ALLOCATOR_DEALLOCATE
195  };
196 
198  bool tracking () const { return track_; }
199 
201  bool verbose () const { return verbose_; }
202 
203  // This lets tracking() and verbose() stay private,
204  // without breaking the templated copy constructor.
205  template<class U>
206  friend class Allocator;
207 
208 public:
210  typedef T value_type;
211 
215  typedef T* pointer;
219  typedef const T* const_pointer;
223  typedef T& reference;
227  typedef const T& const_reference;
228 
235 
243 #ifdef HAVE_TEUCHOSCORE_CXX11
244  typedef std::make_signed<size_type>::type difference_type;
245 #else
246  typedef std::ptrdiff_t difference_type;
247 #endif // HAVE_TEUCHOSCORE_CXX11
248 
251  track_ (true), verbose_ (false)
252  {}
253 
261  Allocator (const bool trackMemory,
262  const bool verboseOutput) :
263  track_ (trackMemory), verbose_ (verboseOutput)
264  {}
265 
267  template<class U>
268  Allocator (const Allocator<U>& src) :
269  track_ (src.tracking ()), verbose_ (src.verbose ())
270  {}
271 
281  template<class U>
282  struct rebind { typedef Allocator<U> other; };
283 
288  size_type max_size() const {
289  return std::numeric_limits<size_type>::max();
290  }
291 
300  value_type* allocate (const size_type& n, const void* = 0) {
301  if (tracking ()) {
302  AllocationLogger::logAllocation (std::cerr, n, n * sizeof (value_type),
303  typeid (value_type).name (), verbose_);
304  }
305  return (value_type*) (::operator new (n * sizeof (T)));
306  }
307 
312  void deallocate (value_type* p, const size_type& n) {
313  if (tracking ()) {
314  // Thankfully, this method accepts the array size. Thus, we don't
315  // have to do tricks like allocating extra space and stashing the
316  // size in the array.
317  AllocationLogger::logDeallocation (std::cerr, n, n * sizeof (value_type),
318  typeid (value_type).name (), verbose_);
319  }
320  ::operator delete ((void*) p);
321  }
322 
326  }
327 
331  }
332 
333 #ifndef HAVE_TEUCHOSCORE_CXX11
334  void construct (pointer p, const_reference val) {
346  new ((void*) p) T (val);
347  }
348 #endif // HAVE_TEUCHOSCORE_CXX11
349 
350 #ifndef HAVE_TEUCHOSCORE_CXX11
351  void destroy (pointer p) {
361  ((T*)p)->~T ();
362  }
363 #endif // HAVE_TEUCHOSCORE_CXX11
364 
365 private:
366  bool track_;
367  bool verbose_;
368 };
369 
370 
378 template<class T, class U>
379 bool operator== (const Allocator<T>&, const Allocator<U>&) {
380  return true;
381 }
382 
384 template<class T, class U>
385 bool operator!= (const Allocator<T>& a_t, const Allocator<U>& a_u) {
386  return ! (a_t == a_u);
387 }
388 
389 } // namespace Details
390 } // namespace Teuchos
391 
392 #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;.