Teuchos - Trilinos Tools Package  Version of the Day
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 <memory> // Teuchos_ConfigDefs.hpp includes it
55 //#include <iostream> // Teuchos_ConfigDefs.hpp includes it
56 //#include <limits> // Ditto; for std::numeric_limits<size_type>::max()
57 #ifdef HAVE_TEUCHOSCORE_CXX11
58 # include <type_traits>
59 #endif // HAVE_TEUCHOSCORE_CXX11
60 
61 namespace Teuchos {
62 namespace Details {
63 
78 public:
82  typedef std::size_t size_type;
83 
107  static void
108  logAllocation (std::ostream& out,
109  const size_type numEntries,
110  const size_type numBytes,
111  const char typeName[],
112  const bool verbose);
113 
141  static void
142  logDeallocation (std::ostream& out,
143  const size_type numEntries,
144  const size_type numBytes,
145  const char typeName[],
146  const bool verbose);
147 
151  static size_type curAllocInBytes ();
152 
156  static size_type maxAllocInBytes ();
157 
161  static void resetAllocationCounts ();
162 
163 private:
165  static size_type curAllocInBytes_;
166 
168  static size_type maxAllocInBytes_;
169 };
170 
189 template<class T>
190 class Allocator {
191 private:
194  enum EAllocatorOp {
195  ALLOCATOR_ALLOCATE,
196  ALLOCATOR_DEALLOCATE
197  };
198 
200  bool tracking () const { return track_; }
201 
203  bool verbose () const { return verbose_; }
204 
205  // This lets tracking() and verbose() stay private,
206  // without breaking the templated copy constructor.
207  template<class U>
208  friend class Allocator;
209 
210 public:
212  typedef T value_type;
213 
217  typedef T* pointer;
221  typedef const T* const_pointer;
225  typedef T& reference;
229  typedef const T& const_reference;
230 
237 
245 #ifdef HAVE_TEUCHOSCORE_CXX11
246  typedef std::make_signed<size_type>::type difference_type;
247 #else
248  typedef std::ptrdiff_t difference_type;
249 #endif // HAVE_TEUCHOSCORE_CXX11
250 
253  track_ (true), verbose_ (false)
254  {}
255 
263  Allocator (const bool trackMemory,
264  const bool verboseOutput) :
265  track_ (trackMemory), verbose_ (verboseOutput)
266  {}
267 
269  template<class U>
270  Allocator (const Allocator<U>& src) :
271  track_ (src.tracking ()), verbose_ (src.verbose ())
272  {}
273 
283  template<class U>
284  struct rebind { typedef Allocator<U> other; };
285 
290  size_type max_size() const {
291  return std::numeric_limits<size_type>::max();
292  }
293 
302  value_type* allocate (const size_type& n, const void* = 0) {
303  if (tracking ()) {
304  AllocationLogger::logAllocation (std::cerr, n, n * sizeof (value_type),
305  typeid (value_type).name (), verbose_);
306  }
307  return (value_type*) (::operator new (n * sizeof (T)));
308  }
309 
314  void deallocate (value_type* p, const size_type& n) {
315  if (tracking ()) {
316  // Thankfully, this method accepts the array size. Thus, we don't
317  // have to do tricks like allocating extra space and stashing the
318  // size in the array.
319  AllocationLogger::logDeallocation (std::cerr, n, n * sizeof (value_type),
320  typeid (value_type).name (), verbose_);
321  }
322  ::operator delete ((void*) p);
323  }
324 
328  }
329 
333  }
334 
335 #ifndef HAVE_TEUCHOSCORE_CXX11
336  void construct (pointer p, const_reference val) {
348  new ((void*) p) T (val);
349  }
350 #endif // HAVE_TEUCHOSCORE_CXX11
351 
352 #ifndef HAVE_TEUCHOSCORE_CXX11
353  void destroy (pointer p) {
363  ((T*)p)->~T ();
364  }
365 #endif // HAVE_TEUCHOSCORE_CXX11
366 
367 private:
368  bool track_;
369  bool verbose_;
370 };
371 
372 
380 template<class T, class U>
381 bool operator== (const Allocator<T>&, const Allocator<U>&) {
382  return true;
383 }
384 
386 template<class T, class U>
387 bool operator!= (const Allocator<T>& a_t, const Allocator<U>& a_u) {
388  return ! (a_t == a_u);
389 }
390 
391 } // namespace Details
392 } // namespace Teuchos
393 
394 #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<U> 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 ("high water mark") in bytes, over all Allocator<U>.
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.
size_type max_size() const
Upper bound (possibly loose) on maximum allocation size.
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.
Namespace of implementation details.
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.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
T value_type
Type of the template parameter of this class.
void destroy(pointer p)
Invoke the destructor of an instance of T, without deallocating.
static size_type maxAllocInBytes()
Max total allocation ("high water mark") 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<U>.