1 #ifndef BOOST_INTERPROCESS_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
2 #define BOOST_INTERPROCESS_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
4 #ifndef BOOST_CONFIG_HPP
5 # include <boost/config.hpp>
8 #if defined(BOOST_HAS_PRAGMA_ONCE)
13 // This file is the adaptation for shared memory memory mapped
14 // files of boost/detail/sp_counted_impl.hpp
16 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
17 // Copyright 2004-2005 Peter Dimov
18 // Copyright 2006 Ion Gaztanaga
20 // Distributed under the Boost Software License, Version 1.0. (See
21 // accompanying file LICENSE_1_0.txt or copy at
22 // http://www.boost.org/LICENSE_1_0.txt)
25 #include <boost/interprocess/detail/config_begin.hpp>
26 #include <boost/interprocess/detail/workaround.hpp>
28 #include <boost/interprocess/containers/version_type.hpp>
29 #include <boost/interprocess/smart_ptr/detail/sp_counted_base.hpp>
30 #include <boost/interprocess/smart_ptr/scoped_ptr.hpp>
31 #include <boost/interprocess/detail/utilities.hpp>
32 #include <boost/container/allocator_traits.hpp>
33 #include <boost/intrusive/pointer_traits.hpp>
37 namespace interprocess {
41 //!A deleter for scoped_ptr that deallocates the memory
42 //!allocated for an object using a STL allocator.
43 template <class Allocator>
44 struct scoped_ptr_dealloc_functor
46 typedef typename Allocator::pointer pointer;
47 typedef ipcdetail::integral_constant<unsigned,
48 boost::interprocess::version<Allocator>::value> alloc_version;
49 typedef ipcdetail::integral_constant<unsigned, 1> allocator_v1;
50 typedef ipcdetail::integral_constant<unsigned, 2> allocator_v2;
53 void priv_deallocate(const typename Allocator::pointer &p, allocator_v1)
54 { m_alloc.deallocate(p, 1); }
56 void priv_deallocate(const typename Allocator::pointer &p, allocator_v2)
57 { m_alloc.deallocate_one(p); }
62 scoped_ptr_dealloc_functor(Allocator& a)
65 void operator()(pointer ptr)
66 { if (ptr) priv_deallocate(ptr, alloc_version()); }
71 template<class A, class D>
72 class sp_counted_impl_pd
73 : public sp_counted_base
74 , boost::container::allocator_traits<A>::template
75 portable_rebind_alloc< sp_counted_impl_pd<A, D> >::type
76 , D // copy constructor must not throw
79 typedef sp_counted_impl_pd<A, D> this_type;
80 typedef typename boost::container::
81 allocator_traits<A>::template
83 < this_type >::type this_allocator;
84 typedef typename boost::container::
85 allocator_traits<A>::template
87 < const this_type >::type const_this_allocator;
88 typedef typename this_allocator::pointer this_pointer;
89 typedef typename boost::intrusive::
90 pointer_traits<this_pointer> this_pointer_traits;
92 sp_counted_impl_pd( sp_counted_impl_pd const & );
93 sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
95 typedef typename boost::intrusive::
96 pointer_traits<typename A::pointer>::template
97 rebind_pointer<const D>::type const_deleter_pointer;
98 typedef typename boost::intrusive::
99 pointer_traits<typename A::pointer>::template
100 rebind_pointer<const A>::type const_allocator_pointer;
102 typedef typename D::pointer pointer;
106 // pre: d(p) must not throw
108 sp_counted_impl_pd(const Ptr & p, const A &a, const D &d )
109 : this_allocator(a), D(d), m_ptr(p)
112 const_deleter_pointer get_deleter() const
113 { return const_deleter_pointer(&static_cast<const D&>(*this)); }
115 const_allocator_pointer get_allocator() const
116 { return const_allocator_pointer(&static_cast<const A&>(*this)); }
118 void dispose() // nothrow
119 { static_cast<D&>(*this)(m_ptr); }
121 void destroy() // nothrow
123 //Self destruction, so move the allocator
124 this_allocator a_copy(::boost::move(static_cast<this_allocator&>(*this)));
125 BOOST_ASSERT(a_copy == *this);
126 this_pointer this_ptr(this_pointer_traits::pointer_to(*this));
128 scoped_ptr< this_type, scoped_ptr_dealloc_functor<this_allocator> >
129 deleter_ptr(this_ptr, a_copy);
130 typedef typename this_allocator::value_type value_type;
131 ipcdetail::to_raw_pointer(this_ptr)->~value_type();
134 void release() // nothrow
136 if(this->ref_release()){
138 this->weak_release();
142 void weak_release() // nothrow
144 if(sp_counted_base::weak_release()){
151 } // namespace ipcdetail
153 } // namespace interprocess
157 #include <boost/interprocess/detail/config_end.hpp>
159 #endif // #ifndef BOOST_INTERPROCESS_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED