libMesh
pool_allocator.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 #ifndef LIBMESH_POOL_ALLOCATOR_H
19 #define LIBMESH_POOL_ALLOCATOR_H
20 
21 #include "libmesh/libmesh_config.h"
22 
23 #ifdef LIBMESH_HAVE_BOOST
24 // See: http://stackoverflow.com/questions/17000542/boost-pool-can-i-wean-it-from-boost-system
25 #define BOOST_POOL_NO_MT // disable multi-threading
26 #define BOOST_THREAD_MUTEX_HPP // define the #include-guard to disable the header
27 # include <boost/pool/pool.hpp>
28 # include <boost/pool/object_pool.hpp>
29 # include <boost/pool/pool_alloc.hpp>
30 #endif
31 
32 #include <memory> // std::allocator
33 
34 namespace libMesh
35 {
36 // If Boost is enabled, wrappers to use their allocators.
37 #ifdef LIBMESH_HAVE_BOOST
38 
52 template <typename T>
53 class PoolAllocator : public boost::pool_allocator<T>
54 {
55 public:
56 
60  template<typename U>
61  struct rebind {
63  };
64 
65 
67  boost::pool_allocator<T>()
68  {}
69 
70  template <typename U>
72  boost::pool_allocator<T>(o)
73  {}
74 
79  static bool release_memory ()
80  {
81  return boost::singleton_pool<boost::pool_allocator_tag, sizeof(T)>::release_memory();
82  }
83 
90  static bool purge_memory ()
91  {
92  return boost::singleton_pool<boost::pool_allocator_tag, sizeof(T)>::purge_memory();
93  }
94 };
95 
96 
97 
111 template <typename T>
112 class FastPoolAllocator : public boost::fast_pool_allocator<T>
113 {
114 public:
115 
119  template<typename U>
120  struct rebind {
122  };
123 
124 
126  boost::fast_pool_allocator<T>()
127  {}
128 
129  template <typename U>
131  boost::fast_pool_allocator<T>(o)
132  {}
133 
134 
139  static bool release_memory ()
140  {
141  return boost::singleton_pool<boost::fast_pool_allocator_tag, sizeof(T)>::release_memory();
142  }
143 
150  static bool purge_memory ()
151  {
152  return boost::singleton_pool<boost::fast_pool_allocator_tag, sizeof(T)>::purge_memory();
153  }
154 };
155 
156 // Otherwise fall back to std::allocator<>.
157 #else
158 
167 template <typename T>
168 class PoolAllocator : public std::allocator<T>
169 {
170 public:
171 
175  template<typename U>
176  struct rebind {
178  };
179 
181  std::allocator<T>()
182  {}
183 
184  template <typename U>
186  std::allocator<T>(o)
187  {}
188 
193  static bool release_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
194 
201  static bool purge_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
202 };
203 
204 
205 
214 template <typename T>
215 class FastPoolAllocator : public std::allocator<T>
216 {
217 public:
218 
222  template<typename U>
223  struct rebind {
225  };
226 
228  std::allocator<T>()
229  {}
230 
231  template <typename U>
233  std::allocator<T>(o)
234  {}
235 
240  static bool release_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
241 
248  static bool purge_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
249 };
250 
251 #endif
252 
253 } // end namespace libMesh
254 
255 
256 #endif // LIBMESH_POOL_ALLOCATOR_H
An allocator which can be used in standard containers.
static bool purge_memory()
Frees every memory block.
FastPoolAllocator(const FastPoolAllocator< U > &o)
The libMesh namespace provides an interface to certain functionality in the library.
static bool release_memory()
Frees every memory block that doesn&#39;t have any allocated chunks.
static bool release_memory()
Frees every memory block that doesn&#39;t have any allocated chunks.
Methods required for copy construction of containers using this allocator.
An allocator which can be used in standard containers.
PoolAllocator(const PoolAllocator< U > &o)
static bool purge_memory()
Frees every memory block.
Methods required for copy construction of containers using this allocator.