src/Utility/IntrusivePtrObjectPool.h

説明を見る。
00001 // 
00002 // Copyright (c) 2005-2008 Kenichi Watanabe.
00003 // Copyright (c) 2005-2008 Yasuhiro Watari.
00004 // Copyright (c) 2005-2008 Hironori Ichibayashi.
00005 // Copyright (c) 2008-2009 Kazuo Horio.
00006 // Copyright (c) 2009-2013 Naruki Kurata.
00007 // Copyright (c) 2005-2013 Ryota Shioya.
00008 // Copyright (c) 2005-2013 Masahiro Goshima.
00009 // 
00010 // This software is provided 'as-is', without any express or implied
00011 // warranty. In no event will the authors be held liable for any damages
00012 // arising from the use of this software.
00013 // 
00014 // Permission is granted to anyone to use this software for any purpose,
00015 // including commercial applications, and to alter it and redistribute it
00016 // freely, subject to the following restrictions:
00017 // 
00018 // 1. The origin of this software must not be misrepresented; you must not
00019 // claim that you wrote the original software. If you use this software
00020 // in a product, an acknowledgment in the product documentation would be
00021 // appreciated but is not required.
00022 // 
00023 // 2. Altered source versions must be plainly marked as such, and must not be
00024 // misrepresented as being the original software.
00025 // 
00026 // 3. This notice may not be removed or altered from any source
00027 // distribution.
00028 // 
00029 // 
00030 
00031 
00032 #ifndef __INTRUISVE_PTR_OBJECT_POOL_H
00033 #define __INTRUISVE_PTR_OBJECT_POOL_H
00034 #include "Utility/Collection/pool/pool_allocator.h"
00035 namespace Onikiri
00036 {
00037 
00038     struct IntrusiveObjectPoolTag
00039     {
00040     };
00041 
00042     template<typename T, typename PtrT = T > 
00043     class PooledIntrusivePtrObject
00044     {
00045 #if 1
00046         INLINE static T* Allocate()
00047         {
00048             typedef boost::singleton_pool<
00049                 IntrusiveObjectPoolTag, 
00050                 sizeof(T)
00051             > Pool;
00052             return (T*)Pool::malloc();
00053         }
00054 
00055         INLINE static void Deallocate(T* ptr)
00056         {
00057             typedef boost::singleton_pool<
00058                 IntrusiveObjectPoolTag, 
00059                 sizeof(T)
00060             > Pool;
00061             Pool::free(ptr);
00062         }
00063 #else
00064         NOINLINE static T* Allocate()
00065         {
00066             typedef pool_allocator<T> Pool;
00067             return (T*)Pool().allocate(1);
00068         }
00069 
00070         INLINE static void Deallocate(T* ptr)
00071         {
00072             typedef pool_allocator<T> Pool;
00073             Pool().deallocate(ptr,1);
00074         }
00075 #endif
00076 
00077     public:
00078 
00079         INLINE static void Destruct( T* ptr )
00080         {
00081             ptr->~T();
00082             Deallocate(ptr);
00083         }
00084 
00085         INLINE static boost::intrusive_ptr<PtrT> Construct()
00086         {
00087             T* const ret = Allocate();
00088             new (ret) T();
00089             return boost::intrusive_ptr<PtrT>(  ret );
00090         };
00091 
00092         template <typename Arg0>
00093         INLINE static boost::intrusive_ptr<PtrT> Construct(const Arg0& a0)
00094         {
00095             T* const ret = Allocate();
00096             new (ret) T(a0);
00097             return boost::intrusive_ptr<PtrT>(  ret );
00098         };
00099 
00100         template <typename Arg0, typename Arg1>
00101         INLINE static boost::intrusive_ptr<PtrT> Construct(const Arg0& a0, const Arg1& a1)
00102         {
00103             T* const ret = Allocate();
00104             new (ret) T(a0, a1);
00105             return boost::intrusive_ptr<PtrT>(  ret );
00106         };
00107 
00108         template <typename Arg0, typename Arg1, typename Arg2>
00109         INLINE static boost::intrusive_ptr<PtrT> Construct(const Arg0& a0, const Arg1& a1, const Arg2& a2)
00110         {
00111             T* const ret = Allocate();
00112             new (ret) T(a0, a1, a2);
00113             return boost::intrusive_ptr<PtrT>(  ret );
00114         };
00115 
00116         template <typename Arg0, typename Arg1, typename Arg2, typename Arg3>
00117         INLINE static boost::intrusive_ptr<PtrT> Construct(
00118             const Arg0& a0, const Arg1& a1, const Arg2& a2, const Arg3& a3)
00119         {
00120             T* const ret = Allocate();
00121             new (ret) T(a0, a1, a2, a3);
00122             return boost::intrusive_ptr<PtrT>(  ret );
00123         };
00124 
00125 
00126     };
00127 } //namespace Onikiri
00128 
00129 #endif  // __INTRUISVE_PTR_OBJECT_POOL_H
00130 
00131 

Onikiri2に対してTue Jun 18 14:34:27 2013に生成されました。  doxygen 1.4.7