00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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 }
00128
00129 #endif // __INTRUISVE_PTR_OBJECT_POOL_H
00130
00131