segment_manager_helper.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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_SEGMENT_MANAGER_BASE_HPP
  11. #define BOOST_INTERPROCESS_SEGMENT_MANAGER_BASE_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. // interprocess
  22. #include <boost/interprocess/exceptions.hpp>
  23. // interprocess/detail
  24. #include <boost/interprocess/detail/type_traits.hpp>
  25. #include <boost/interprocess/detail/utilities.hpp>
  26. #include <boost/interprocess/detail/in_place_interface.hpp>
  27. // container/detail
  28. #include <boost/container/detail/type_traits.hpp> //alignment_of
  29. #include <boost/container/detail/minimal_char_traits_header.hpp>
  30. // intrusive
  31. #include <boost/intrusive/pointer_traits.hpp>
  32. // move/detail
  33. #include <boost/move/detail/type_traits.hpp> //make_unsigned
  34. #include <boost/move/detail/force_ptr.hpp>
  35. // other boost
  36. #include <boost/assert.hpp> //BOOST_ASSERT
  37. #include <boost/core/no_exceptions_support.hpp>
  38. // std
  39. #include <cstddef> //std::size_t
  40. //!\file
  41. //!Describes the object placed in a memory segment that provides
  42. //!named object allocation capabilities.
  43. namespace boost{
  44. namespace interprocess{
  45. template<class MemoryManager>
  46. class segment_manager_base;
  47. //!An integer that describes the type of the
  48. //!instance constructed in memory
  49. enum instance_type { anonymous_type, named_type, unique_type, max_allocation_type };
  50. namespace ipcdetail{
  51. template<class MemoryAlgorithm>
  52. class mem_algo_deallocator
  53. {
  54. void * m_ptr;
  55. MemoryAlgorithm & m_algo;
  56. public:
  57. mem_algo_deallocator(void *ptr, MemoryAlgorithm &algo)
  58. : m_ptr(ptr), m_algo(algo)
  59. {}
  60. void release()
  61. { m_ptr = 0; }
  62. ~mem_algo_deallocator()
  63. { if(m_ptr) m_algo.deallocate(m_ptr); }
  64. };
  65. template<class size_type>
  66. struct block_header
  67. {
  68. size_type m_value_bytes;
  69. unsigned short m_num_char;
  70. unsigned char m_value_alignment;
  71. unsigned char m_alloc_type_sizeof_char;
  72. block_header(size_type val_bytes
  73. ,size_type val_alignment
  74. ,unsigned char al_type
  75. ,std::size_t szof_char
  76. ,std::size_t num_char
  77. )
  78. : m_value_bytes(val_bytes)
  79. , m_num_char((unsigned short)num_char)
  80. , m_value_alignment((unsigned char)val_alignment)
  81. , m_alloc_type_sizeof_char( (unsigned char)((al_type << 5u) | ((unsigned char)szof_char & 0x1F)) )
  82. {};
  83. template<class T>
  84. block_header &operator= (const T& )
  85. { return *this; }
  86. size_type total_size() const
  87. {
  88. if(alloc_type() != anonymous_type){
  89. return name_offset() + (m_num_char+1u)*sizeof_char();
  90. }
  91. else{
  92. return this->value_offset() + m_value_bytes;
  93. }
  94. }
  95. size_type value_bytes() const
  96. { return m_value_bytes; }
  97. template<class Header>
  98. size_type total_size_with_header() const
  99. {
  100. return get_rounded_size
  101. ( size_type(sizeof(Header))
  102. , size_type(::boost::container::dtl::alignment_of<block_header<size_type> >::value))
  103. + total_size();
  104. }
  105. unsigned char alloc_type() const
  106. { return (m_alloc_type_sizeof_char >> 5u)&(unsigned char)0x7; }
  107. unsigned char sizeof_char() const
  108. { return m_alloc_type_sizeof_char & (unsigned char)0x1F; }
  109. template<class CharType>
  110. CharType *name() const
  111. {
  112. return const_cast<CharType*>(move_detail::force_ptr<const CharType*>
  113. (reinterpret_cast<const char*>(this) + name_offset()));
  114. }
  115. unsigned short name_length() const
  116. { return m_num_char; }
  117. size_type name_offset() const
  118. {
  119. return this->value_offset() + get_rounded_size(size_type(m_value_bytes), size_type(sizeof_char()));
  120. }
  121. void *value() const
  122. {
  123. return const_cast<char*>((reinterpret_cast<const char*>(this) + this->value_offset()));
  124. }
  125. size_type value_offset() const
  126. {
  127. return get_rounded_size(size_type(sizeof(block_header<size_type>)), size_type(m_value_alignment));
  128. }
  129. template<class CharType>
  130. bool less_comp(const block_header<size_type> &b) const
  131. {
  132. return m_num_char < b.m_num_char ||
  133. (m_num_char < b.m_num_char &&
  134. std::char_traits<CharType>::compare(name<CharType>(), b.name<CharType>(), m_num_char) < 0);
  135. }
  136. template<class CharType>
  137. bool equal_comp(const block_header<size_type> &b) const
  138. {
  139. return m_num_char == b.m_num_char &&
  140. std::char_traits<CharType>::compare(name<CharType>(), b.name<CharType>(), m_num_char) == 0;
  141. }
  142. template<class T>
  143. static block_header<size_type> *block_header_from_value(T *value)
  144. { return block_header_from_value(value, sizeof(T), ::boost::container::dtl::alignment_of<T>::value); }
  145. static block_header<size_type> *block_header_from_value(const void *value, std::size_t sz, std::size_t algn)
  146. {
  147. block_header * hdr =
  148. const_cast<block_header*>
  149. (move_detail::force_ptr<const block_header*>(reinterpret_cast<const char*>(value) -
  150. get_rounded_size(sizeof(block_header), algn)));
  151. (void)sz;
  152. //Some sanity checks
  153. BOOST_ASSERT(hdr->m_value_alignment == algn);
  154. BOOST_ASSERT(hdr->m_value_bytes % sz == 0);
  155. return hdr;
  156. }
  157. template<class Header>
  158. static block_header<size_type> *from_first_header(Header *header)
  159. {
  160. block_header<size_type> * hdr =
  161. move_detail::force_ptr<block_header<size_type>*>(reinterpret_cast<char*>(header) +
  162. get_rounded_size( size_type(sizeof(Header))
  163. , size_type(::boost::container::dtl::alignment_of<block_header<size_type> >::value)));
  164. //Some sanity checks
  165. return hdr;
  166. }
  167. template<class Header>
  168. static Header *to_first_header(block_header<size_type> *bheader)
  169. {
  170. Header * hdr =
  171. move_detail::force_ptr<Header*>(reinterpret_cast<char*>(bheader) -
  172. get_rounded_size( size_type(sizeof(Header))
  173. , size_type(::boost::container::dtl::alignment_of<block_header<size_type> >::value)));
  174. //Some sanity checks
  175. return hdr;
  176. }
  177. };
  178. template<class CharT>
  179. struct intrusive_compare_key
  180. {
  181. typedef CharT char_type;
  182. intrusive_compare_key(const CharT *str, std::size_t len)
  183. : mp_str(str), m_len(len)
  184. {}
  185. const CharT * mp_str;
  186. std::size_t m_len;
  187. };
  188. //!This struct indicates an anonymous object creation
  189. //!allocation
  190. template<instance_type type>
  191. class instance_t
  192. {
  193. instance_t(){}
  194. };
  195. template<class T>
  196. struct char_if_void
  197. {
  198. typedef T type;
  199. };
  200. template<>
  201. struct char_if_void<void>
  202. {
  203. typedef char type;
  204. };
  205. typedef instance_t<anonymous_type> anonymous_instance_t;
  206. typedef instance_t<unique_type> unique_instance_t;
  207. template<class Hook, class CharType, class SizeType>
  208. struct intrusive_value_type_impl
  209. : public Hook
  210. {
  211. private:
  212. //Non-copyable
  213. intrusive_value_type_impl(const intrusive_value_type_impl &);
  214. intrusive_value_type_impl& operator=(const intrusive_value_type_impl &);
  215. public:
  216. typedef CharType char_type;
  217. typedef SizeType size_type;
  218. intrusive_value_type_impl(){}
  219. enum { BlockHdrAlignment = ::boost::container::dtl::alignment_of<block_header<size_type> >::value };
  220. block_header<size_type> *get_block_header() const
  221. {
  222. return const_cast<block_header<size_type>*>
  223. (move_detail::force_ptr<const block_header<size_type> *>(reinterpret_cast<const char*>(this) +
  224. get_rounded_size(size_type(sizeof(*this)), size_type(BlockHdrAlignment))));
  225. }
  226. bool operator <(const intrusive_value_type_impl<Hook, CharType, SizeType> & other) const
  227. { return (this->get_block_header())->template less_comp<CharType>(*other.get_block_header()); }
  228. bool operator ==(const intrusive_value_type_impl<Hook, CharType, SizeType> & other) const
  229. { return (this->get_block_header())->template equal_comp<CharType>(*other.get_block_header()); }
  230. static intrusive_value_type_impl *get_intrusive_value_type(block_header<size_type> *hdr)
  231. {
  232. return move_detail::force_ptr<intrusive_value_type_impl*>(reinterpret_cast<char*>(hdr) -
  233. get_rounded_size(size_type(sizeof(intrusive_value_type_impl)), size_type(BlockHdrAlignment)));
  234. }
  235. CharType *name() const
  236. { return get_block_header()->template name<CharType>(); }
  237. unsigned short name_length() const
  238. { return get_block_header()->name_length(); }
  239. void *value() const
  240. { return get_block_header()->value(); }
  241. };
  242. template<class CharType>
  243. class char_ptr_holder
  244. {
  245. public:
  246. char_ptr_holder(const CharType *name)
  247. : m_name(name)
  248. {}
  249. char_ptr_holder(const anonymous_instance_t *)
  250. : m_name(static_cast<CharType*>(0))
  251. {}
  252. char_ptr_holder(const unique_instance_t *)
  253. : m_name(reinterpret_cast<CharType*>(-1))
  254. {}
  255. operator const CharType *()
  256. { return m_name; }
  257. const CharType *get() const
  258. { return m_name; }
  259. bool is_unique() const
  260. { return m_name == reinterpret_cast<CharType*>(-1); }
  261. bool is_anonymous() const
  262. { return m_name == static_cast<CharType*>(0); }
  263. private:
  264. const CharType *m_name;
  265. };
  266. //!The key of the the named allocation information index. Stores an offset pointer
  267. //!to a null terminated string and the length of the string to speed up sorting
  268. template<class CharT, class VoidPointer>
  269. struct index_key
  270. {
  271. typedef typename boost::intrusive::
  272. pointer_traits<VoidPointer>::template
  273. rebind_pointer<const CharT>::type const_char_ptr_t;
  274. typedef CharT char_type;
  275. typedef typename boost::intrusive::pointer_traits<const_char_ptr_t>::difference_type difference_type;
  276. typedef typename boost::move_detail::make_unsigned<difference_type>::type size_type;
  277. private:
  278. //Offset pointer to the object's name
  279. const_char_ptr_t mp_str;
  280. //Length of the name buffer (null NOT included)
  281. size_type m_len;
  282. public:
  283. //!Constructor of the key
  284. index_key (const char_type *nm, size_type length)
  285. : mp_str(nm), m_len(length)
  286. {}
  287. //!Less than function for index ordering
  288. bool operator < (const index_key & right) const
  289. {
  290. return (m_len < right.m_len) ||
  291. (m_len == right.m_len &&
  292. std::char_traits<char_type>::compare
  293. (to_raw_pointer(mp_str),to_raw_pointer(right.mp_str), m_len) < 0);
  294. }
  295. //!Equal to function for index ordering
  296. bool operator == (const index_key & right) const
  297. {
  298. return m_len == right.m_len &&
  299. std::char_traits<char_type>::compare
  300. (to_raw_pointer(mp_str), to_raw_pointer(right.mp_str), m_len) == 0;
  301. }
  302. void name(const CharT *nm)
  303. { mp_str = nm; }
  304. void name_length(size_type len)
  305. { m_len = len; }
  306. const CharT *name() const
  307. { return to_raw_pointer(mp_str); }
  308. size_type name_length() const
  309. { return m_len; }
  310. };
  311. //!The index_data stores a pointer to a buffer and the element count needed
  312. //!to know how many destructors must be called when calling destroy
  313. template<class VoidPointer>
  314. struct index_data
  315. {
  316. typedef VoidPointer void_pointer;
  317. void_pointer m_ptr;
  318. explicit index_data(void *ptr) : m_ptr(ptr){}
  319. void *value() const
  320. { return static_cast<void*>(to_raw_pointer(m_ptr)); }
  321. };
  322. template<class MemoryAlgorithm>
  323. struct segment_manager_base_type
  324. { typedef segment_manager_base<MemoryAlgorithm> type; };
  325. template<class CharT, class MemoryAlgorithm>
  326. struct index_config
  327. {
  328. typedef typename MemoryAlgorithm::void_pointer void_pointer;
  329. typedef CharT char_type;
  330. typedef index_key<CharT, void_pointer> key_type;
  331. typedef index_data<void_pointer> mapped_type;
  332. typedef typename segment_manager_base_type
  333. <MemoryAlgorithm>::type segment_manager_base;
  334. template<class HeaderBase>
  335. struct intrusive_value_type
  336. { typedef intrusive_value_type_impl<HeaderBase, CharT, typename segment_manager_base::size_type> type; };
  337. typedef intrusive_compare_key<CharT> intrusive_compare_key_type;
  338. };
  339. template<class Iterator, bool intrusive>
  340. class segment_manager_iterator_value_adaptor
  341. {
  342. typedef typename Iterator::value_type iterator_val_t;
  343. typedef typename iterator_val_t::char_type char_type;
  344. public:
  345. segment_manager_iterator_value_adaptor(const typename Iterator::value_type &val)
  346. : m_val(&val)
  347. {}
  348. const char_type *name() const
  349. { return m_val->name(); }
  350. unsigned short name_length() const
  351. { return m_val->name_length(); }
  352. const void *value() const
  353. { return m_val->value(); }
  354. const typename Iterator::value_type *m_val;
  355. };
  356. template<class Iterator>
  357. class segment_manager_iterator_value_adaptor<Iterator, false>
  358. {
  359. typedef typename Iterator::value_type iterator_val_t;
  360. typedef typename iterator_val_t::first_type first_type;
  361. typedef typename iterator_val_t::second_type second_type;
  362. typedef typename first_type::char_type char_type;
  363. typedef typename first_type::size_type size_type;
  364. public:
  365. segment_manager_iterator_value_adaptor(const typename Iterator::value_type &val)
  366. : m_val(&val)
  367. {}
  368. const char_type *name() const
  369. { return m_val->first.name(); }
  370. size_type name_length() const
  371. { return m_val->first.name_length(); }
  372. const void *value() const
  373. {
  374. return move_detail::force_ptr<block_header<size_type>*>
  375. (to_raw_pointer(m_val->second.m_ptr))->value();
  376. }
  377. const typename Iterator::value_type *m_val;
  378. };
  379. template<class Iterator, bool intrusive>
  380. struct segment_manager_iterator_transform
  381. {
  382. typedef segment_manager_iterator_value_adaptor<Iterator, intrusive> result_type;
  383. template <class T> result_type operator()(const T &arg) const
  384. { return result_type(arg); }
  385. };
  386. } //namespace ipcdetail {
  387. //These pointers are the ones the user will use to
  388. //indicate previous allocation types
  389. static const ipcdetail::anonymous_instance_t * anonymous_instance = 0;
  390. static const ipcdetail::unique_instance_t * unique_instance = 0;
  391. namespace ipcdetail_really_deep_namespace {
  392. //Otherwise, gcc issues a warning of previously defined
  393. //anonymous_instance and unique_instance
  394. struct dummy
  395. {
  396. dummy()
  397. {
  398. (void)anonymous_instance;
  399. (void)unique_instance;
  400. }
  401. };
  402. } //detail_really_deep_namespace
  403. }} //namespace boost { namespace interprocess
  404. #include <boost/interprocess/detail/config_end.hpp>
  405. #endif //#ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_BASE_HPP