singleton_object_pool.h

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00029  */
00030 #if !defined(SINGLETON_OBJECT_POOL_H)
00031 #define SINGLETON_OBJECT_POOL_H
00032 
00033 #define BOOST_POOL_NO_MT
00034 #undef BOOST_HAS_THREADS
00035 
00036 #include <boost/pool/pool.hpp>
00037 #include "pool_allocators.h"
00038 
00039 // This is only safe for objects with nothrow constructors...
00040 template <typename T, typename Alloc = boost::default_user_allocator_new_delete>
00041 class singleton_object_pool
00042 {
00043  private:
00044  struct pool_impl
00045  {
00046    boost::pool<Alloc> p;
00047    pool_impl() : p(sizeof(T), 32) 
00048    {
00049    }
00050  };
00051  struct singleton
00052  {
00053     static pool_impl& instance()
00054     {
00055         static pool_impl* thePool = new pool_impl;
00056         return *thePool;
00057     }
00058  };
00059  
00060   
00061  inline static void free(T* free_me)
00062  {
00063    singleton::instance().p.free(free_me);
00064  }
00065  
00066  inline static T* malloc() 
00067   {
00068     return static_cast<T*>(singleton::instance().p.malloc());
00069   }
00070  public:
00071   inline static bool is_from(T* t)
00072   {
00073     return singleton::instance().p.is_from(t);
00074   }
00075   
00076   static T* construct()
00077   {
00078     T* const temp = malloc();
00079     if(temp == 0) return temp;
00080     new(temp) T();
00081     return temp;
00082   }
00083   template <typename A1>
00084   static T* construct(const A1& a1)
00085   {
00086     T* const temp = malloc();
00087     if(temp == 0) return temp;
00088     new(temp) T(a1);
00089     return temp;
00090   }
00091   template <typename A1, typename A2>
00092   static T* construct(const A1& a1, const A2& a2)
00093   {
00094     T* const temp = malloc();
00095     if(temp == 0) return temp;
00096     new(temp) T(a1, a2);
00097     return temp;
00098   }
00099   template <typename A1, typename A2, typename A3>
00100   static T* construct(const A1& a1, const A2& a2, const A3& a3)
00101   {
00102     T* const temp = malloc();
00103     if(temp == 0) return temp;
00104     new(temp) T(a1, a2, a3);
00105     return temp;
00106   }
00107   template <typename A1, typename A2, typename A3, typename A4>
00108   static T* construct(const A1& a1, const A2& a2, const A3& a3, const A4& a4)
00109   {
00110     T* const temp = malloc();
00111     if(temp == 0) return temp;
00112     new(temp) T(a1, a2, a3, a4);
00113     return temp;
00114   }
00115   template <typename A1, typename A2, typename A3, typename A4, typename A5>
00116   static T* construct(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5)
00117   {
00118     T* const temp = malloc();
00119     if(temp == 0) return temp;
00120     new(temp) T(a1, a2, a3, a4, a5);
00121     return temp;
00122   }
00123   inline static void destroy(T* const kill_me)
00124   {
00125     kill_me->~T();
00126     free(kill_me);
00127   }
00128   
00129   
00130 };
00131 
00132 
00133 template <typename T> 
00134 struct PoolDestructor
00135 {
00136   inline void operator()(T* e) 
00137   {
00138     // We'll see if this kills performance or not...
00139     if(singleton_object_pool<T>::is_from(e)) {
00140       singleton_object_pool<T>::destroy(e);
00141     }
00142     
00143   }
00144 };
00145 
00146 template <typename T> inline
00147 boost::shared_ptr<T> make_shared(T* t)
00148 {
00149     return boost::shared_ptr<T>(t, PoolDestructor<T>()/*, typename unlocked_fast_alloc<T>::type()*/);
00150 }
00151  
00152 
00153 #endif //!defined(SINGLETON_OBJECT_POOL_H)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1