$extrastylesheet
pool_allocator.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 #ifndef LIBMESH_POOL_ALLOCATOR_H
00019 #define LIBMESH_POOL_ALLOCATOR_H
00020 
00021 #include "libmesh/libmesh_config.h"
00022 
00023 #ifdef LIBMESH_HAVE_BOOST
00024 // See: http://stackoverflow.com/questions/17000542/boost-pool-can-i-wean-it-from-boost-system
00025 #define BOOST_POOL_NO_MT       // disable multi-threading
00026 #define BOOST_THREAD_MUTEX_HPP // define the #include-guard to disable the header
00027 #  include <boost/pool/pool.hpp>
00028 #  include <boost/pool/object_pool.hpp>
00029 #  include <boost/pool/pool_alloc.hpp>
00030 #endif
00031 
00032 #include <memory> // std::allocator
00033 
00034 namespace libMesh
00035 {
00036 // If Boost is enabled, wrappers to use their allocators.
00037 #ifdef LIBMESH_HAVE_BOOST
00038 
00046 template <typename T>
00047 class PoolAllocator : public boost::pool_allocator<T>
00048 {
00049 public:
00050 
00054   template<typename U>
00055   struct rebind {
00056     typedef PoolAllocator<U> other;
00057   };
00058 
00059 
00060   PoolAllocator() :
00061     boost::pool_allocator<T>()
00062   {}
00063 
00064   explicit PoolAllocator(const PoolAllocator &o) :
00065     boost::pool_allocator<T>(o)
00066   {}
00067 
00072   static bool release_memory ()
00073   {
00074     return boost::singleton_pool<boost::pool_allocator_tag, sizeof(T)>::release_memory();
00075   }
00076 
00081   static bool purge_memory ()
00082   {
00083     return boost::singleton_pool<boost::pool_allocator_tag, sizeof(T)>::purge_memory();
00084   }
00085 };
00086 
00087 
00088 
00096 template <typename T>
00097 class FastPoolAllocator : public boost::fast_pool_allocator<T>
00098 {
00099 public:
00100 
00104   template<typename U>
00105   struct rebind {
00106     typedef FastPoolAllocator<U> other;
00107   };
00108 
00109 
00110   FastPoolAllocator() :
00111     boost::fast_pool_allocator<T>()
00112   {}
00113 
00114   explicit FastPoolAllocator(const FastPoolAllocator &o) :
00115     boost::fast_pool_allocator<T>(o)
00116   {}
00117 
00118 
00123   static bool release_memory ()
00124   {
00125     return boost::singleton_pool<boost::fast_pool_allocator_tag, sizeof(T)>::release_memory();
00126   }
00127 
00132   static bool purge_memory ()
00133   {
00134     return boost::singleton_pool<boost::fast_pool_allocator_tag, sizeof(T)>::purge_memory();
00135   }
00136 };
00137 
00138 // Otherwise fall back to std::allocator<>.
00139 #else
00140 
00145 template <typename T>
00146 class PoolAllocator : public std::allocator<T>
00147 {
00148 public:
00149 
00153   template<typename U>
00154   struct rebind {
00155     typedef PoolAllocator<U> other;
00156   };
00157 
00158   PoolAllocator() :
00159     std::allocator<T>()
00160   {}
00161 
00162   explicit PoolAllocator(const PoolAllocator &o) :
00163     std::allocator<T>(o)
00164   {}
00165 
00170   static bool release_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
00171 
00176   static bool purge_memory ()   { /* no-op for std::allocator<> - already freed. */ return false; }
00177 };
00178 
00179 
00180 
00185 template <typename T>
00186 class FastPoolAllocator : public std::allocator<T>
00187 {
00188 public:
00189 
00193   template<typename U>
00194   struct rebind {
00195     typedef FastPoolAllocator<U> other;
00196   };
00197 
00198   FastPoolAllocator() :
00199     std::allocator<T>()
00200   {}
00201 
00202   explicit FastPoolAllocator(const FastPoolAllocator &o) :
00203     std::allocator<T>(o)
00204   {}
00205 
00210   static bool release_memory () { /* no-op for std::allocator<> - already freed. */ return false; }
00211 
00216   static bool purge_memory ()   { /* no-op for std::allocator<> - already freed. */ return false; }
00217 };
00218 
00219 #endif
00220 
00221 } // end namespace libMesh
00222 
00223 
00224 #endif // LIBMESH_POOL_ALLOCATOR_H