allocator.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  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)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_ALLOCATOR_HPP
  11. #define BOOST_INTERPROCESS_ALLOCATOR_HPP
  12. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. #include <boost/interprocess/detail/config_begin.hpp>
  16. #include <boost/interprocess/detail/workaround.hpp>
  17. #include <boost/intrusive/pointer_traits.hpp>
  18. #include <boost/interprocess/interprocess_fwd.hpp>
  19. #include <boost/interprocess/containers/allocation_type.hpp>
  20. #include <boost/container/detail/multiallocation_chain.hpp>
  21. #include <boost/interprocess/allocators/detail/allocator_common.hpp>
  22. #include <boost/interprocess/detail/utilities.hpp>
  23. #include <boost/interprocess/containers/version_type.hpp>
  24. #include <boost/interprocess/exceptions.hpp>
  25. #include <boost/assert.hpp>
  26. #include <boost/utility/addressof.hpp>
  27. #include <boost/interprocess/detail/type_traits.hpp>
  28. #include <memory>
  29. #include <new>
  30. #include <algorithm>
  31. #include <cstddef>
  32. #include <stdexcept>
  33. //!\file
  34. //!Describes an allocator that allocates portions of fixed size
  35. //!memory buffer (shared memory, mapped file...)
  36. namespace boost {
  37. namespace interprocess {
  38. //!An STL compatible allocator that uses a segment manager as
  39. //!memory source. The internal pointer type will of the same type (raw, smart) as
  40. //!"typename SegmentManager::void_pointer" type. This allows
  41. //!placing the allocator in shared memory, memory mapped-files, etc...
  42. template<class T, class SegmentManager>
  43. class allocator
  44. {
  45. public:
  46. //Segment manager
  47. typedef SegmentManager segment_manager;
  48. typedef typename SegmentManager::void_pointer void_pointer;
  49. /// @cond
  50. private:
  51. //Self type
  52. typedef allocator<T, SegmentManager> self_t;
  53. //Pointer to void
  54. typedef typename segment_manager::void_pointer aux_pointer_t;
  55. //Typedef to const void pointer
  56. typedef typename boost::intrusive::
  57. pointer_traits<aux_pointer_t>::template
  58. rebind_pointer<const void>::type cvoid_ptr;
  59. //Pointer to the allocator
  60. typedef typename boost::intrusive::
  61. pointer_traits<cvoid_ptr>::template
  62. rebind_pointer<segment_manager>::type alloc_ptr_t;
  63. //Not assignable from related allocator
  64. template<class T2, class SegmentManager2>
  65. allocator& operator=(const allocator<T2, SegmentManager2>&);
  66. //Not assignable from other allocator
  67. allocator& operator=(const allocator&);
  68. //Pointer to the allocator
  69. alloc_ptr_t mp_mngr;
  70. /// @endcond
  71. public:
  72. typedef T value_type;
  73. typedef typename boost::intrusive::
  74. pointer_traits<cvoid_ptr>::template
  75. rebind_pointer<T>::type pointer;
  76. typedef typename boost::intrusive::
  77. pointer_traits<pointer>::template
  78. rebind_pointer<const T>::type const_pointer;
  79. typedef typename ipcdetail::add_reference
  80. <value_type>::type reference;
  81. typedef typename ipcdetail::add_reference
  82. <const value_type>::type const_reference;
  83. typedef typename segment_manager::size_type size_type;
  84. typedef typename segment_manager::difference_type difference_type;
  85. typedef boost::interprocess::version_type<allocator, 2> version;
  86. /// @cond
  87. //Experimental. Don't use.
  88. typedef boost::container::container_detail::transform_multiallocation_chain
  89. <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
  90. /// @endcond
  91. //!Obtains an allocator that allocates
  92. //!objects of type T2
  93. template<class T2>
  94. struct rebind
  95. {
  96. typedef allocator<T2, SegmentManager> other;
  97. };
  98. //!Returns the segment manager.
  99. //!Never throws
  100. segment_manager* get_segment_manager()const
  101. { return ipcdetail::to_raw_pointer(mp_mngr); }
  102. //!Constructor from the segment manager.
  103. //!Never throws
  104. allocator(segment_manager *segment_mngr)
  105. : mp_mngr(segment_mngr) { }
  106. //!Constructor from other allocator.
  107. //!Never throws
  108. allocator(const allocator &other)
  109. : mp_mngr(other.get_segment_manager()){ }
  110. //!Constructor from related allocator.
  111. //!Never throws
  112. template<class T2>
  113. allocator(const allocator<T2, SegmentManager> &other)
  114. : mp_mngr(other.get_segment_manager()){}
  115. //!Allocates memory for an array of count elements.
  116. //!Throws boost::interprocess::bad_alloc if there is no enough memory
  117. pointer allocate(size_type count, cvoid_ptr hint = 0)
  118. {
  119. (void)hint;
  120. if(size_overflows<sizeof(T)>(count)){
  121. throw bad_alloc();
  122. }
  123. return pointer(static_cast<value_type*>(mp_mngr->allocate(count*sizeof(T))));
  124. }
  125. //!Deallocates memory previously allocated.
  126. //!Never throws
  127. void deallocate(const pointer &ptr, size_type)
  128. { mp_mngr->deallocate((void*)ipcdetail::to_raw_pointer(ptr)); }
  129. //!Returns the number of elements that could be allocated.
  130. //!Never throws
  131. size_type max_size() const
  132. { return mp_mngr->get_size()/sizeof(T); }
  133. //!Swap segment manager. Does not throw. If each allocator is placed in
  134. //!different memory segments, the result is undefined.
  135. friend void swap(self_t &alloc1, self_t &alloc2)
  136. { ipcdetail::do_swap(alloc1.mp_mngr, alloc2.mp_mngr); }
  137. //!Returns maximum the number of objects the previously allocated memory
  138. //!pointed by p can hold. This size only works for memory allocated with
  139. //!allocate, allocation_command and allocate_many.
  140. size_type size(const pointer &p) const
  141. {
  142. return (size_type)mp_mngr->size(ipcdetail::to_raw_pointer(p))/sizeof(T);
  143. }
  144. std::pair<pointer, bool>
  145. allocation_command(boost::interprocess::allocation_type command,
  146. size_type limit_size,
  147. size_type preferred_size,
  148. size_type &received_size, const pointer &reuse = 0)
  149. {
  150. return mp_mngr->allocation_command
  151. (command, limit_size, preferred_size, received_size, ipcdetail::to_raw_pointer(reuse));
  152. }
  153. //!Allocates many elements of size elem_size in a contiguous block
  154. //!of memory. The minimum number to be allocated is min_elements,
  155. //!the preferred and maximum number is
  156. //!preferred_elements. The number of actually allocated elements is
  157. //!will be assigned to received_size. The elements must be deallocated
  158. //!with deallocate(...)
  159. void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain)
  160. {
  161. if(size_overflows<sizeof(T)>(elem_size)){
  162. throw bad_alloc();
  163. }
  164. mp_mngr->allocate_many(elem_size*sizeof(T), num_elements, chain);
  165. }
  166. //!Allocates n_elements elements, each one of size elem_sizes[i]in a
  167. //!contiguous block
  168. //!of memory. The elements must be deallocated
  169. void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
  170. {
  171. mp_mngr->allocate_many(elem_sizes, n_elements, sizeof(T), chain);
  172. }
  173. //!Allocates many elements of size elem_size in a contiguous block
  174. //!of memory. The minimum number to be allocated is min_elements,
  175. //!the preferred and maximum number is
  176. //!preferred_elements. The number of actually allocated elements is
  177. //!will be assigned to received_size. The elements must be deallocated
  178. //!with deallocate(...)
  179. void deallocate_many(multiallocation_chain &chain)
  180. { mp_mngr->deallocate_many(chain); }
  181. //!Allocates just one object. Memory allocated with this function
  182. //!must be deallocated only with deallocate_one().
  183. //!Throws boost::interprocess::bad_alloc if there is no enough memory
  184. pointer allocate_one()
  185. { return this->allocate(1); }
  186. //!Allocates many elements of size == 1 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. Memory allocated with this function
  191. //!must be deallocated only with deallocate_one().
  192. void allocate_individual(size_type num_elements, multiallocation_chain &chain)
  193. { this->allocate_many(1, num_elements, chain); }
  194. //!Deallocates memory previously allocated with allocate_one().
  195. //!You should never use deallocate_one to deallocate memory allocated
  196. //!with other functions different from allocate_one(). Never throws
  197. void deallocate_one(const pointer &p)
  198. { return this->deallocate(p, 1); }
  199. //!Allocates many elements of size == 1 in a contiguous block
  200. //!of memory. The minimum number to be allocated is min_elements,
  201. //!the preferred and maximum number is
  202. //!preferred_elements. The number of actually allocated elements is
  203. //!will be assigned to received_size. Memory allocated with this function
  204. //!must be deallocated only with deallocate_one().
  205. void deallocate_individual(multiallocation_chain &chain)
  206. { this->deallocate_many(chain); }
  207. //!Returns address of mutable object.
  208. //!Never throws
  209. pointer address(reference value) const
  210. { return pointer(boost::addressof(value)); }
  211. //!Returns address of non mutable object.
  212. //!Never throws
  213. const_pointer address(const_reference value) const
  214. { return const_pointer(boost::addressof(value)); }
  215. //!Constructs an object
  216. //!Throws if T's constructor throws
  217. //!For backwards compatibility with libraries using C++03 allocators
  218. template<class P>
  219. void construct(const pointer &ptr, BOOST_FWD_REF(P) p)
  220. { ::new((void*)ipcdetail::to_raw_pointer(ptr)) value_type(::boost::forward<P>(p)); }
  221. //!Destroys object. Throws if object's
  222. //!destructor throws
  223. void destroy(const pointer &ptr)
  224. { BOOST_ASSERT(ptr != 0); (*ptr).~value_type(); }
  225. };
  226. //!Equality test for same type
  227. //!of allocator
  228. template<class T, class SegmentManager> inline
  229. bool operator==(const allocator<T , SegmentManager> &alloc1,
  230. const allocator<T, SegmentManager> &alloc2)
  231. { return alloc1.get_segment_manager() == alloc2.get_segment_manager(); }
  232. //!Inequality test for same type
  233. //!of allocator
  234. template<class T, class SegmentManager> inline
  235. bool operator!=(const allocator<T, SegmentManager> &alloc1,
  236. const allocator<T, SegmentManager> &alloc2)
  237. { return alloc1.get_segment_manager() != alloc2.get_segment_manager(); }
  238. } //namespace interprocess {
  239. /// @cond
  240. template<class T>
  241. struct has_trivial_destructor;
  242. template<class T, class SegmentManager>
  243. struct has_trivial_destructor
  244. <boost::interprocess::allocator <T, SegmentManager> >
  245. {
  246. static const bool value = true;
  247. };
  248. /// @endcond
  249. } //namespace boost {
  250. #include <boost/interprocess/detail/config_end.hpp>
  251. #endif //BOOST_INTERPROCESS_ALLOCATOR_HPP