GEOS
BufferAllocator.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 Total, S.A
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_COMMON_BUFFERALLOCATOR_HPP
21 #define GEOS_COMMON_BUFFERALLOCATOR_HPP
22 
23 #include "common/GeosxConfig.hpp"
24 
25 #ifdef GEOS_USE_CHAI
26 #include <umpire/ResourceManager.hpp>
27 #include <umpire/TypedAllocator.hpp>
28 
29 namespace geos
30 {
36 void setPreferPinned( bool p );
37 
43 bool getPreferPinned( );
44 
59 template< typename T >
60 class BufferAllocator
61 {
62 public:
63  // The type used to instantiate the class, and the underlying umpire allocator.
64  using value_type = T;
65 private:
66  // An umpire allocator allocating the type for which this class is instantiated.
67  umpire::TypedAllocator< value_type > m_alloc;
68  bool m_prefer_pinned_l;
69 public:
70 
76  BufferAllocator()
77  : m_alloc( umpire::TypedAllocator< T >( umpire::ResourceManager::getInstance().getAllocator( umpire::resource::Host ) ) )
78  , m_prefer_pinned_l( getPreferPinned( ) )
79  {
80  #if defined(UMPIRE_ENABLE_PINNED)
81  if( m_prefer_pinned_l )
82  {
83  m_alloc = umpire::TypedAllocator< T >( umpire::ResourceManager::getInstance().getAllocator( umpire::resource::Pinned ) );
84  }
85  #endif
86  }
87 
93  value_type * allocate( size_t sz )
94  {
95  return m_alloc.allocate( sz );
96  }
97 
103  void deallocate( value_type * buffer, size_t sz )
104  {
105  if( buffer != nullptr )
106  m_alloc.deallocate( buffer, sz );
107  }
108 
115  bool operator!=( const BufferAllocator & )
116  {
117  return false;
118  }
119 
126  bool operator==( const BufferAllocator & other )
127  {
128  return !operator!=( other );
129  }
130 };
131 
132 }
133 #endif
134 
135 #endif
bool operator!=(InputFlags const left, InputFlags const right)
Comparison operator for InputFlags enumeration.
Definition: InputFlags.hpp:155
bool operator==(InputFlags const left, InputFlags const right)
Comparison operator for InputFlags enumeration.
Definition: InputFlags.hpp:147