pool_allocator.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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 
46  template <typename T>
47  class PoolAllocator : public boost::pool_allocator<T>
48  {
49  public:
50 
54  template<typename U>
55  struct rebind {
57  };
58 
59 
61  boost::pool_allocator<T>()
62  {}
63 
64  explicit PoolAllocator(const PoolAllocator &o) :
65  boost::pool_allocator<T>(o)
66  {}
67 
72  static bool release_memory ()
73  {
74  return boost::singleton_pool<boost::pool_allocator_tag, sizeof(T)>::release_memory();
75  }
76 
81  static bool purge_memory ()
82  {
83  return boost::singleton_pool<boost::pool_allocator_tag, sizeof(T)>::purge_memory();
84  }
85  };
86 
87 
88 
96  template <typename T>
97  class FastPoolAllocator : public boost::fast_pool_allocator<T>
98  {
99  public:
100 
104  template<typename U>
105  struct rebind {
107  };
108 
109 
111  boost::fast_pool_allocator<T>()
112  {}
113 
115  boost::fast_pool_allocator<T>(o)
116  {}
117 
118 
123  static bool release_memory ()
124  {
125  return boost::singleton_pool<boost::fast_pool_allocator_tag, sizeof(T)>::release_memory();
126  }
127 
132  static bool purge_memory ()
133  {
134  return boost::singleton_pool<boost::fast_pool_allocator_tag, sizeof(T)>::purge_memory();
135  }
136  };
137 
138  // Otherwise fall back to std::allocator<>.
139 #else
140 
145  template <typename T>
146  class PoolAllocator : public std::allocator<T>
147  {
148  public:
149 
153  template<typename U>
154  struct rebind {
156  };
157 
159  std::allocator<T>()
160  {}
161 
162  explicit PoolAllocator(const PoolAllocator &o) :
163  std::allocator<T>(o)
164  {}
165 
170  static bool release_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
171 
176  static bool purge_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
177  };
178 
179 
180 
185  template <typename T>
186  class FastPoolAllocator : public std::allocator<T>
187  {
188  public:
189 
193  template<typename U>
194  struct rebind {
196  };
197 
199  std::allocator<T>()
200  {}
201 
203  std::allocator<T>(o)
204  {}
205 
210  static bool release_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
211 
216  static bool purge_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
217  };
218 
219 #endif
220 
221 } // end namespace libMesh
222 
223 
224 #endif // LIBMESH_POOL_ALLOCATOR_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:06 UTC

Hosted By:
SourceForge.net Logo