pool_allocator.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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  explicit PoolAllocator(const PoolAllocator & o) :
71  boost::pool_allocator<T>(o)
72  {}
73 
78  static bool release_memory ()
79  {
80  return boost::singleton_pool<boost::pool_allocator_tag, sizeof(T)>::release_memory();
81  }
82 
89  static bool purge_memory ()
90  {
91  return boost::singleton_pool<boost::pool_allocator_tag, sizeof(T)>::purge_memory();
92  }
93 };
94 
95 
96 
110 template <typename T>
111 class FastPoolAllocator : public boost::fast_pool_allocator<T>
112 {
113 public:
114 
118  template<typename U>
119  struct rebind {
121  };
122 
123 
125  boost::fast_pool_allocator<T>()
126  {}
127 
128  explicit FastPoolAllocator(const FastPoolAllocator & o) :
129  boost::fast_pool_allocator<T>(o)
130  {}
131 
132 
137  static bool release_memory ()
138  {
139  return boost::singleton_pool<boost::fast_pool_allocator_tag, sizeof(T)>::release_memory();
140  }
141 
148  static bool purge_memory ()
149  {
150  return boost::singleton_pool<boost::fast_pool_allocator_tag, sizeof(T)>::purge_memory();
151  }
152 };
153 
154 // Otherwise fall back to std::allocator<>.
155 #else
156 
165 template <typename T>
166 class PoolAllocator : public std::allocator<T>
167 {
168 public:
169 
173  template<typename U>
174  struct rebind {
176  };
177 
179  std::allocator<T>()
180  {}
181 
182  explicit PoolAllocator(const PoolAllocator & o) :
183  std::allocator<T>(o)
184  {}
185 
190  static bool release_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
191 
198  static bool purge_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
199 };
200 
201 
202 
211 template <typename T>
212 class FastPoolAllocator : public std::allocator<T>
213 {
214 public:
215 
219  template<typename U>
220  struct rebind {
222  };
223 
225  std::allocator<T>()
226  {}
227 
228  explicit FastPoolAllocator(const FastPoolAllocator & o) :
229  std::allocator<T>(o)
230  {}
231 
236  static bool release_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
237 
244  static bool purge_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
245 };
246 
247 #endif
248 
249 } // end namespace libMesh
250 
251 
252 #endif // LIBMESH_POOL_ALLOCATOR_H
Boost-derived allocator that can be used with std::containers.
PoolAllocator(const PoolAllocator &o)
static bool release_memory()
FastPoolAllocator(const FastPoolAllocator &o)
Boost-derived allocator that can be used with std::containers.
static bool purge_memory()