1 ///////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2011. 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 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
21 #include <boost/intrusive/pointer_traits.hpp>
23 #include <boost/interprocess/interprocess_fwd.hpp>
24 #include <boost/interprocess/containers/allocation_type.hpp>
25 #include <boost/container/detail/multiallocation_chain.hpp>
26 #include <boost/interprocess/allocators/detail/allocator_common.hpp>
27 #include <boost/interprocess/detail/utilities.hpp>
28 #include <boost/interprocess/containers/version_type.hpp>
29 #include <boost/interprocess/exceptions.hpp>
30 #include <boost/assert.hpp>
31 #include <boost/utility/addressof.hpp>
32 #include <boost/interprocess/detail/type_traits.hpp>
41 //!Describes an allocator that allocates portions of fixed size
42 //!memory buffer (shared memory, mapped file...)
45 namespace interprocess {
48 //!An STL compatible allocator that uses a segment manager as
49 //!memory source. The internal pointer type will of the same type (raw, smart) as
50 //!"typename SegmentManager::void_pointer" type. This allows
51 //!placing the allocator in shared memory, memory mapped-files, etc...
52 template<class T, class SegmentManager>
57 typedef SegmentManager segment_manager;
58 typedef typename SegmentManager::void_pointer void_pointer;
64 typedef allocator<T, SegmentManager> self_t;
67 typedef typename segment_manager::void_pointer aux_pointer_t;
69 //Typedef to const void pointer
70 typedef typename boost::intrusive::
71 pointer_traits<aux_pointer_t>::template
72 rebind_pointer<const void>::type cvoid_ptr;
74 //Pointer to the allocator
75 typedef typename boost::intrusive::
76 pointer_traits<cvoid_ptr>::template
77 rebind_pointer<segment_manager>::type alloc_ptr_t;
79 //Not assignable from related allocator
80 template<class T2, class SegmentManager2>
81 allocator& operator=(const allocator<T2, SegmentManager2>&);
83 //Not assignable from other allocator
84 allocator& operator=(const allocator&);
86 //Pointer to the allocator
92 typedef typename boost::intrusive::
93 pointer_traits<cvoid_ptr>::template
94 rebind_pointer<T>::type pointer;
95 typedef typename boost::intrusive::
96 pointer_traits<pointer>::template
97 rebind_pointer<const T>::type const_pointer;
98 typedef typename ipcdetail::add_reference
99 <value_type>::type reference;
100 typedef typename ipcdetail::add_reference
101 <const value_type>::type const_reference;
102 typedef typename segment_manager::size_type size_type;
103 typedef typename segment_manager::difference_type difference_type;
105 typedef boost::interprocess::version_type<allocator, 2> version;
109 //Experimental. Don't use.
110 typedef boost::container::container_detail::transform_multiallocation_chain
111 <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
114 //!Obtains an allocator that allocates
115 //!objects of type T2
119 typedef allocator<T2, SegmentManager> other;
122 //!Returns the segment manager.
124 segment_manager* get_segment_manager()const
125 { return ipcdetail::to_raw_pointer(mp_mngr); }
127 //!Constructor from the segment manager.
129 allocator(segment_manager *segment_mngr)
130 : mp_mngr(segment_mngr) { }
132 //!Constructor from other allocator.
134 allocator(const allocator &other)
135 : mp_mngr(other.get_segment_manager()){ }
137 //!Constructor from related allocator.
140 allocator(const allocator<T2, SegmentManager> &other)
141 : mp_mngr(other.get_segment_manager()){}
143 //!Allocates memory for an array of count elements.
144 //!Throws boost::interprocess::bad_alloc if there is no enough memory
145 pointer allocate(size_type count, cvoid_ptr hint = 0)
148 if(count > this->max_size())
150 return pointer(static_cast<value_type*>(mp_mngr->allocate(count*sizeof(T))));
153 //!Deallocates memory previously allocated.
155 void deallocate(const pointer &ptr, size_type)
156 { mp_mngr->deallocate((void*)ipcdetail::to_raw_pointer(ptr)); }
158 //!Returns the number of elements that could be allocated.
160 size_type max_size() const
161 { return mp_mngr->get_size()/sizeof(T); }
163 //!Swap segment manager. Does not throw. If each allocator is placed in
164 //!different memory segments, the result is undefined.
165 friend void swap(self_t &alloc1, self_t &alloc2)
166 { ipcdetail::do_swap(alloc1.mp_mngr, alloc2.mp_mngr); }
168 //!Returns maximum the number of objects the previously allocated memory
169 //!pointed by p can hold. This size only works for memory allocated with
170 //!allocate, allocation_command and allocate_many.
171 size_type size(const pointer &p) const
173 return (size_type)mp_mngr->size(ipcdetail::to_raw_pointer(p))/sizeof(T);
176 std::pair<pointer, bool>
177 allocation_command(boost::interprocess::allocation_type command,
178 size_type limit_size,
179 size_type preferred_size,
180 size_type &received_size, const pointer &reuse = 0)
182 return mp_mngr->allocation_command
183 (command, limit_size, preferred_size, received_size, ipcdetail::to_raw_pointer(reuse));
186 //!Allocates many elements of size elem_size in a contiguous block
187 //!of memory. The minimum number to be allocated is min_elements,
188 //!the preferred and maximum number is
189 //!preferred_elements. The number of actually allocated elements is
190 //!will be assigned to received_size. The elements must be deallocated
191 //!with deallocate(...)
192 multiallocation_chain allocate_many
193 (size_type elem_size, size_type num_elements)
195 return multiallocation_chain(mp_mngr->allocate_many(sizeof(T)*elem_size, num_elements));
198 //!Allocates n_elements elements, each one of size elem_sizes[i]in a
200 //!of memory. The elements must be deallocated
201 multiallocation_chain allocate_many
202 (const size_type *elem_sizes, size_type n_elements)
204 multiallocation_chain(mp_mngr->allocate_many(elem_sizes, n_elements, sizeof(T)));
207 //!Allocates many elements of size elem_size in a contiguous block
208 //!of memory. The minimum number to be allocated is min_elements,
209 //!the preferred and maximum number is
210 //!preferred_elements. The number of actually allocated elements is
211 //!will be assigned to received_size. The elements must be deallocated
212 //!with deallocate(...)
213 void deallocate_many(multiallocation_chain chain)
215 return mp_mngr->deallocate_many(chain.extract_multiallocation_chain());
218 //!Allocates just one object. Memory allocated with this function
219 //!must be deallocated only with deallocate_one().
220 //!Throws boost::interprocess::bad_alloc if there is no enough memory
221 pointer allocate_one()
222 { return this->allocate(1); }
224 //!Allocates many elements of size == 1 in a contiguous block
225 //!of memory. The minimum number to be allocated is min_elements,
226 //!the preferred and maximum number is
227 //!preferred_elements. The number of actually allocated elements is
228 //!will be assigned to received_size. Memory allocated with this function
229 //!must be deallocated only with deallocate_one().
230 multiallocation_chain allocate_individual
231 (size_type num_elements)
232 { return this->allocate_many(1, num_elements); }
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 { return this->deallocate_many(boost::move(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)) 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 {
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;
302 } //namespace boost {
304 #include <boost/interprocess/detail/config_end.hpp>
306 #endif //BOOST_INTERPROCESS_ALLOCATOR_HPP