1 ///////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_ALLOCATOR_HPP
12 #define BOOST_INTERPROCESS_ALLOCATOR_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
25 #include <boost/intrusive/pointer_traits.hpp>
27 #include <boost/interprocess/interprocess_fwd.hpp>
28 #include <boost/interprocess/containers/allocation_type.hpp>
29 #include <boost/container/detail/multiallocation_chain.hpp>
30 #include <boost/interprocess/allocators/detail/allocator_common.hpp>
31 #include <boost/interprocess/detail/utilities.hpp>
32 #include <boost/interprocess/containers/version_type.hpp>
33 #include <boost/interprocess/exceptions.hpp>
34 #include <boost/assert.hpp>
35 #include <boost/utility/addressof.hpp>
36 #include <boost/interprocess/detail/type_traits.hpp>
37 #include <boost/container/detail/placement_new.hpp>
43 //!Describes an allocator that allocates portions of fixed size
44 //!memory buffer (shared memory, mapped file...)
47 namespace interprocess {
50 //!An STL compatible allocator that uses a segment manager as
51 //!memory source. The internal pointer type will of the same type (raw, smart) as
52 //!"typename SegmentManager::void_pointer" type. This allows
53 //!placing the allocator in shared memory, memory mapped-files, etc...
54 template<class T, class SegmentManager>
59 typedef SegmentManager segment_manager;
60 typedef typename SegmentManager::void_pointer void_pointer;
62 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
66 typedef allocator<T, SegmentManager> self_t;
69 typedef typename segment_manager::void_pointer aux_pointer_t;
71 //Typedef to const void pointer
72 typedef typename boost::intrusive::
73 pointer_traits<aux_pointer_t>::template
74 rebind_pointer<const void>::type cvoid_ptr;
76 //Pointer to the allocator
77 typedef typename boost::intrusive::
78 pointer_traits<cvoid_ptr>::template
79 rebind_pointer<segment_manager>::type alloc_ptr_t;
81 //Not assignable from related allocator
82 template<class T2, class SegmentManager2>
83 allocator& operator=(const allocator<T2, SegmentManager2>&);
85 //Not assignable from other allocator
86 allocator& operator=(const allocator&);
88 //Pointer to the allocator
90 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
94 typedef typename boost::intrusive::
95 pointer_traits<cvoid_ptr>::template
96 rebind_pointer<T>::type pointer;
97 typedef typename boost::intrusive::
98 pointer_traits<pointer>::template
99 rebind_pointer<const T>::type const_pointer;
100 typedef typename ipcdetail::add_reference
101 <value_type>::type reference;
102 typedef typename ipcdetail::add_reference
103 <const value_type>::type const_reference;
104 typedef typename segment_manager::size_type size_type;
105 typedef typename segment_manager::difference_type difference_type;
107 typedef boost::interprocess::version_type<allocator, 2> version;
109 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
111 //Experimental. Don't use.
112 typedef boost::container::container_detail::transform_multiallocation_chain
113 <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
114 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
116 //!Obtains an allocator that allocates
117 //!objects of type T2
121 typedef allocator<T2, SegmentManager> other;
124 //!Returns the segment manager.
126 segment_manager* get_segment_manager()const
127 { return ipcdetail::to_raw_pointer(mp_mngr); }
129 //!Constructor from the segment manager.
131 allocator(segment_manager *segment_mngr)
132 : mp_mngr(segment_mngr) { }
134 //!Constructor from other allocator.
136 allocator(const allocator &other)
137 : mp_mngr(other.get_segment_manager()){ }
139 //!Constructor from related allocator.
142 allocator(const allocator<T2, SegmentManager> &other)
143 : mp_mngr(other.get_segment_manager()){}
145 //!Allocates memory for an array of count elements.
146 //!Throws boost::interprocess::bad_alloc if there is no enough memory
147 pointer allocate(size_type count, cvoid_ptr hint = 0)
150 if(size_overflows<sizeof(T)>(count)){
153 return pointer(static_cast<value_type*>(mp_mngr->allocate(count*sizeof(T))));
156 //!Deallocates memory previously allocated.
158 void deallocate(const pointer &ptr, size_type)
159 { mp_mngr->deallocate((void*)ipcdetail::to_raw_pointer(ptr)); }
161 //!Returns the number of elements that could be allocated.
163 size_type max_size() const
164 { return mp_mngr->get_size()/sizeof(T); }
166 //!Swap segment manager. Does not throw. If each allocator is placed in
167 //!different memory segments, the result is undefined.
168 friend void swap(self_t &alloc1, self_t &alloc2)
169 { boost::adl_move_swap(alloc1.mp_mngr, alloc2.mp_mngr); }
171 //!Returns maximum the number of objects the previously allocated memory
172 //!pointed by p can hold. This size only works for memory allocated with
173 //!allocate, allocation_command and allocate_many.
174 size_type size(const pointer &p) const
176 return (size_type)mp_mngr->size(ipcdetail::to_raw_pointer(p))/sizeof(T);
179 pointer allocation_command(boost::interprocess::allocation_type command,
180 size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse)
182 value_type *reuse_raw = ipcdetail::to_raw_pointer(reuse);
183 pointer const p = mp_mngr->allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse_raw);
188 //!Allocates many elements of size elem_size in a contiguous block
189 //!of memory. The minimum number to be allocated is min_elements,
190 //!the preferred and maximum number is
191 //!preferred_elements. The number of actually allocated elements is
192 //!will be assigned to received_size. The elements must be deallocated
193 //!with deallocate(...)
194 void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain)
196 if(size_overflows<sizeof(T)>(elem_size)){
199 mp_mngr->allocate_many(elem_size*sizeof(T), num_elements, chain);
202 //!Allocates n_elements elements, each one of size elem_sizes[i]in a
204 //!of memory. The elements must be deallocated
205 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
207 mp_mngr->allocate_many(elem_sizes, n_elements, sizeof(T), chain);
210 //!Allocates many elements of size elem_size in a contiguous block
211 //!of memory. The minimum number to be allocated is min_elements,
212 //!the preferred and maximum number is
213 //!preferred_elements. The number of actually allocated elements is
214 //!will be assigned to received_size. The elements must be deallocated
215 //!with deallocate(...)
216 void deallocate_many(multiallocation_chain &chain)
217 { mp_mngr->deallocate_many(chain); }
219 //!Allocates just one object. Memory allocated with this function
220 //!must be deallocated only with deallocate_one().
221 //!Throws boost::interprocess::bad_alloc if there is no enough memory
222 pointer allocate_one()
223 { return this->allocate(1); }
225 //!Allocates many elements of size == 1 in a contiguous block
226 //!of memory. The minimum number to be allocated is min_elements,
227 //!the preferred and maximum number is
228 //!preferred_elements. The number of actually allocated elements is
229 //!will be assigned to received_size. Memory allocated with this function
230 //!must be deallocated only with deallocate_one().
231 void allocate_individual(size_type num_elements, multiallocation_chain &chain)
232 { this->allocate_many(1, num_elements, chain); }
234 //!Deallocates memory previously allocated with allocate_one().
235 //!You should never use deallocate_one to deallocate memory allocated
236 //!with other functions different from allocate_one(). Never throws
237 void deallocate_one(const pointer &p)
238 { return this->deallocate(p, 1); }
240 //!Allocates many elements of size == 1 in a contiguous block
241 //!of memory. The minimum number to be allocated is min_elements,
242 //!the preferred and maximum number is
243 //!preferred_elements. The number of actually allocated elements is
244 //!will be assigned to received_size. Memory allocated with this function
245 //!must be deallocated only with deallocate_one().
246 void deallocate_individual(multiallocation_chain &chain)
247 { this->deallocate_many(chain); }
249 //!Returns address of mutable object.
251 pointer address(reference value) const
252 { return pointer(boost::addressof(value)); }
254 //!Returns address of non mutable object.
256 const_pointer address(const_reference value) const
257 { return const_pointer(boost::addressof(value)); }
259 //!Constructs an object
260 //!Throws if T's constructor throws
261 //!For backwards compatibility with libraries using C++03 allocators
263 void construct(const pointer &ptr, BOOST_FWD_REF(P) p)
264 { ::new((void*)ipcdetail::to_raw_pointer(ptr), boost_container_new_t()) value_type(::boost::forward<P>(p)); }
266 //!Destroys object. Throws if object's
268 void destroy(const pointer &ptr)
269 { BOOST_ASSERT(ptr != 0); (*ptr).~value_type(); }
273 //!Equality test for same type
275 template<class T, class SegmentManager> inline
276 bool operator==(const allocator<T , SegmentManager> &alloc1,
277 const allocator<T, SegmentManager> &alloc2)
278 { return alloc1.get_segment_manager() == alloc2.get_segment_manager(); }
280 //!Inequality test for same type
282 template<class T, class SegmentManager> inline
283 bool operator!=(const allocator<T, SegmentManager> &alloc1,
284 const allocator<T, SegmentManager> &alloc2)
285 { return alloc1.get_segment_manager() != alloc2.get_segment_manager(); }
287 } //namespace interprocess {
289 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
292 struct has_trivial_destructor;
294 template<class T, class SegmentManager>
295 struct has_trivial_destructor
296 <boost::interprocess::allocator <T, SegmentManager> >
298 static const bool value = true;
300 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
302 } //namespace boost {
304 #include <boost/interprocess/detail/config_end.hpp>
306 #endif //BOOST_INTERPROCESS_ALLOCATOR_HPP