segment_manager_helper.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. inline void array_construct(void *mem, std::size_t num, in_place_interface &table)
  179. {
  180. //Try constructors
  181. std::size_t constructed = 0;
  182. BOOST_TRY{
  183. table.construct_n(mem, num, constructed);
  184. }
  185. //If there is an exception call destructors and erase index node
  186. BOOST_CATCH(...){
  187. std::size_t destroyed = 0;
  188. table.destroy_n(mem, constructed, destroyed);
  189. BOOST_RETHROW
  190. } BOOST_CATCH_END
  191. }
  192. template<class CharT>
  193. struct intrusive_compare_key
  194. {
  195. typedef CharT char_type;
  196. intrusive_compare_key(const CharT *str, std::size_t len)
  197. : mp_str(str), m_len(len)
  198. {}
  199. const CharT * mp_str;
  200. std::size_t m_len;
  201. };
  202. //!This struct indicates an anonymous object creation
  203. //!allocation
  204. template<instance_type type>
  205. class instance_t
  206. {
  207. instance_t(){}
  208. };
  209. template<class T>
  210. struct char_if_void
  211. {
  212. typedef T type;
  213. };
  214. template<>
  215. struct char_if_void<void>
  216. {
  217. typedef char type;
  218. };
  219. typedef instance_t<anonymous_type> anonymous_instance_t;
  220. typedef instance_t<unique_type> unique_instance_t;
  221. template<class Hook, class CharType, class SizeType>
  222. struct intrusive_value_type_impl
  223. : public Hook
  224. {
  225. private:
  226. //Non-copyable
  227. intrusive_value_type_impl(const intrusive_value_type_impl &);
  228. intrusive_value_type_impl& operator=(const intrusive_value_type_impl &);
  229. public:
  230. typedef CharType char_type;
  231. typedef SizeType size_type;
  232. intrusive_value_type_impl(){}
  233. enum { BlockHdrAlignment = ::boost::container::dtl::alignment_of<block_header<size_type> >::value };
  234. block_header<size_type> *get_block_header() const
  235. {
  236. return const_cast<block_header<size_type>*>
  237. (move_detail::force_ptr<const block_header<size_type> *>(reinterpret_cast<const char*>(this) +
  238. get_rounded_size(size_type(sizeof(*this)), size_type(BlockHdrAlignment))));
  239. }
  240. bool operator <(const intrusive_value_type_impl<Hook, CharType, SizeType> & other) const
  241. { return (this->get_block_header())->template less_comp<CharType>(*other.get_block_header()); }
  242. bool operator ==(const intrusive_value_type_impl<Hook, CharType, SizeType> & other) const
  243. { return (this->get_block_header())->template equal_comp<CharType>(*other.get_block_header()); }
  244. static intrusive_value_type_impl *get_intrusive_value_type(block_header<size_type> *hdr)
  245. {
  246. return move_detail::force_ptr<intrusive_value_type_impl*>(reinterpret_cast<char*>(hdr) -
  247. get_rounded_size(size_type(sizeof(intrusive_value_type_impl)), size_type(BlockHdrAlignment)));
  248. }
  249. CharType *name() const
  250. { return get_block_header()->template name<CharType>(); }
  251. unsigned short name_length() const
  252. { return get_block_header()->name_length(); }
  253. void *value() const
  254. { return get_block_header()->value(); }
  255. };
  256. template<class CharType>
  257. class char_ptr_holder
  258. {
  259. public:
  260. char_ptr_holder(const CharType *name)
  261. : m_name(name)
  262. {}
  263. char_ptr_holder(const anonymous_instance_t *)
  264. : m_name(static_cast<CharType*>(0))
  265. {}
  266. char_ptr_holder(const unique_instance_t *)
  267. : m_name(reinterpret_cast<CharType*>(-1))
  268. {}
  269. operator const CharType *()
  270. { return m_name; }
  271. const CharType *get() const
  272. { return m_name; }
  273. bool is_unique() const
  274. { return m_name == reinterpret_cast<CharType*>(-1); }
  275. bool is_anonymous() const
  276. { return m_name == static_cast<CharType*>(0); }
  277. private:
  278. const CharType *m_name;
  279. };
  280. //!The key of the the named allocation information index. Stores an offset pointer
  281. //!to a null terminated string and the length of the string to speed up sorting
  282. template<class CharT, class VoidPointer>
  283. struct index_key
  284. {
  285. typedef typename boost::intrusive::
  286. pointer_traits<VoidPointer>::template
  287. rebind_pointer<const CharT>::type const_char_ptr_t;
  288. typedef CharT char_type;
  289. typedef typename boost::intrusive::pointer_traits<const_char_ptr_t>::difference_type difference_type;
  290. typedef typename boost::move_detail::make_unsigned<difference_type>::type size_type;
  291. private:
  292. //Offset pointer to the object's name
  293. const_char_ptr_t mp_str;
  294. //Length of the name buffer (null NOT included)
  295. size_type m_len;
  296. public:
  297. //!Constructor of the key
  298. index_key (const char_type *nm, size_type length)
  299. : mp_str(nm), m_len(length)
  300. {}
  301. //!Less than function for index ordering
  302. bool operator < (const index_key & right) const
  303. {
  304. return (m_len < right.m_len) ||
  305. (m_len == right.m_len &&
  306. std::char_traits<char_type>::compare
  307. (to_raw_pointer(mp_str),to_raw_pointer(right.mp_str), m_len) < 0);
  308. }
  309. //!Equal to function for index ordering
  310. bool operator == (const index_key & right) const
  311. {
  312. return m_len == right.m_len &&
  313. std::char_traits<char_type>::compare
  314. (to_raw_pointer(mp_str), to_raw_pointer(right.mp_str), m_len) == 0;
  315. }
  316. void name(const CharT *nm)
  317. { mp_str = nm; }
  318. void name_length(size_type len)
  319. { m_len = len; }
  320. const CharT *name() const
  321. { return to_raw_pointer(mp_str); }
  322. size_type name_length() const
  323. { return m_len; }
  324. };
  325. //!The index_data stores a pointer to a buffer and the element count needed
  326. //!to know how many destructors must be called when calling destroy
  327. template<class VoidPointer>
  328. struct index_data
  329. {
  330. typedef VoidPointer void_pointer;
  331. void_pointer m_ptr;
  332. explicit index_data(void *ptr) : m_ptr(ptr){}
  333. void *value() const
  334. { return static_cast<void*>(to_raw_pointer(m_ptr)); }
  335. };
  336. template<class MemoryAlgorithm>
  337. struct segment_manager_base_type
  338. { typedef segment_manager_base<MemoryAlgorithm> type; };
  339. template<class CharT, class MemoryAlgorithm>
  340. struct index_config
  341. {
  342. typedef typename MemoryAlgorithm::void_pointer void_pointer;
  343. typedef CharT char_type;
  344. typedef index_key<CharT, void_pointer> key_type;
  345. typedef index_data<void_pointer> mapped_type;
  346. typedef typename segment_manager_base_type
  347. <MemoryAlgorithm>::type segment_manager_base;
  348. template<class HeaderBase>
  349. struct intrusive_value_type
  350. { typedef intrusive_value_type_impl<HeaderBase, CharT, typename segment_manager_base::size_type> type; };
  351. typedef intrusive_compare_key<CharT> intrusive_compare_key_type;
  352. };
  353. template<class Iterator, bool intrusive>
  354. class segment_manager_iterator_value_adaptor
  355. {
  356. typedef typename Iterator::value_type iterator_val_t;
  357. typedef typename iterator_val_t::char_type char_type;
  358. public:
  359. segment_manager_iterator_value_adaptor(const typename Iterator::value_type &val)
  360. : m_val(&val)
  361. {}
  362. const char_type *name() const
  363. { return m_val->name(); }
  364. unsigned short name_length() const
  365. { return m_val->name_length(); }
  366. const void *value() const
  367. { return m_val->value(); }
  368. const typename Iterator::value_type *m_val;
  369. };
  370. template<class Iterator>
  371. class segment_manager_iterator_value_adaptor<Iterator, false>
  372. {
  373. typedef typename Iterator::value_type iterator_val_t;
  374. typedef typename iterator_val_t::first_type first_type;
  375. typedef typename iterator_val_t::second_type second_type;
  376. typedef typename first_type::char_type char_type;
  377. typedef typename first_type::size_type size_type;
  378. public:
  379. segment_manager_iterator_value_adaptor(const typename Iterator::value_type &val)
  380. : m_val(&val)
  381. {}
  382. const char_type *name() const
  383. { return m_val->first.name(); }
  384. size_type name_length() const
  385. { return m_val->first.name_length(); }
  386. const void *value() const
  387. {
  388. return move_detail::force_ptr<block_header<size_type>*>
  389. (to_raw_pointer(m_val->second.m_ptr))->value();
  390. }
  391. const typename Iterator::value_type *m_val;
  392. };
  393. template<class Iterator, bool intrusive>
  394. struct segment_manager_iterator_transform
  395. {
  396. typedef segment_manager_iterator_value_adaptor<Iterator, intrusive> result_type;
  397. template <class T> result_type operator()(const T &arg) const
  398. { return result_type(arg); }
  399. };
  400. } //namespace ipcdetail {
  401. //These pointers are the ones the user will use to
  402. //indicate previous allocation types
  403. static const ipcdetail::anonymous_instance_t * anonymous_instance = 0;
  404. static const ipcdetail::unique_instance_t * unique_instance = 0;
  405. namespace ipcdetail_really_deep_namespace {
  406. //Otherwise, gcc issues a warning of previously defined
  407. //anonymous_instance and unique_instance
  408. struct dummy
  409. {
  410. dummy()
  411. {
  412. (void)anonymous_instance;
  413. (void)unique_instance;
  414. }
  415. };
  416. } //detail_really_deep_namespace
  417. }} //namespace boost { namespace interprocess
  418. #include <boost/interprocess/detail/config_end.hpp>
  419. #endif //#ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_BASE_HPP