segment_manager_helper.hpp 15 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. // other boost
  35. #include <boost/assert.hpp> //BOOST_ASSERT
  36. #include <boost/core/no_exceptions_support.hpp>
  37. // std
  38. #include <cstddef> //std::size_t
  39. //!\file
  40. //!Describes the object placed in a memory segment that provides
  41. //!named object allocation capabilities.
  42. namespace boost{
  43. namespace interprocess{
  44. template<class MemoryManager>
  45. class segment_manager_base;
  46. //!An integer that describes the type of the
  47. //!instance constructed in memory
  48. enum instance_type { anonymous_type, named_type, unique_type, max_allocation_type };
  49. namespace ipcdetail{
  50. template<class MemoryAlgorithm>
  51. class mem_algo_deallocator
  52. {
  53. void * m_ptr;
  54. MemoryAlgorithm & m_algo;
  55. public:
  56. mem_algo_deallocator(void *ptr, MemoryAlgorithm &algo)
  57. : m_ptr(ptr), m_algo(algo)
  58. {}
  59. void release()
  60. { m_ptr = 0; }
  61. ~mem_algo_deallocator()
  62. { if(m_ptr) m_algo.deallocate(m_ptr); }
  63. };
  64. template<class size_type>
  65. struct block_header
  66. {
  67. size_type m_value_bytes;
  68. unsigned short m_num_char;
  69. unsigned char m_value_alignment;
  70. unsigned char m_alloc_type_sizeof_char;
  71. block_header(size_type val_bytes
  72. ,size_type val_alignment
  73. ,unsigned char al_type
  74. ,std::size_t szof_char
  75. ,std::size_t num_char
  76. )
  77. : m_value_bytes(val_bytes)
  78. , m_num_char((unsigned short)num_char)
  79. , m_value_alignment((unsigned char)val_alignment)
  80. , m_alloc_type_sizeof_char( (al_type << 5u) | ((unsigned char)szof_char & 0x1F) )
  81. {};
  82. template<class T>
  83. block_header &operator= (const T& )
  84. { return *this; }
  85. size_type total_size() const
  86. {
  87. if(alloc_type() != anonymous_type){
  88. return name_offset() + (m_num_char+1)*sizeof_char();
  89. }
  90. else{
  91. return this->value_offset() + m_value_bytes;
  92. }
  93. }
  94. size_type value_bytes() const
  95. { return m_value_bytes; }
  96. template<class Header>
  97. size_type total_size_with_header() const
  98. {
  99. return get_rounded_size
  100. ( size_type(sizeof(Header))
  101. , size_type(::boost::container::dtl::alignment_of<block_header<size_type> >::value))
  102. + total_size();
  103. }
  104. unsigned char alloc_type() const
  105. { return (m_alloc_type_sizeof_char >> 5u)&(unsigned char)0x7; }
  106. unsigned char sizeof_char() const
  107. { return m_alloc_type_sizeof_char & (unsigned char)0x1F; }
  108. template<class CharType>
  109. CharType *name() const
  110. {
  111. return const_cast<CharType*>(reinterpret_cast<const CharType*>
  112. (reinterpret_cast<const char*>(this) + name_offset()));
  113. }
  114. unsigned short name_length() const
  115. { return m_num_char; }
  116. size_type name_offset() const
  117. {
  118. return this->value_offset() + get_rounded_size(size_type(m_value_bytes), size_type(sizeof_char()));
  119. }
  120. void *value() const
  121. {
  122. return const_cast<char*>((reinterpret_cast<const char*>(this) + this->value_offset()));
  123. }
  124. size_type value_offset() const
  125. {
  126. return get_rounded_size(size_type(sizeof(block_header<size_type>)), size_type(m_value_alignment));
  127. }
  128. template<class CharType>
  129. bool less_comp(const block_header<size_type> &b) const
  130. {
  131. return m_num_char < b.m_num_char ||
  132. (m_num_char < b.m_num_char &&
  133. std::char_traits<CharType>::compare(name<CharType>(), b.name<CharType>(), m_num_char) < 0);
  134. }
  135. template<class CharType>
  136. bool equal_comp(const block_header<size_type> &b) const
  137. {
  138. return m_num_char == b.m_num_char &&
  139. std::char_traits<CharType>::compare(name<CharType>(), b.name<CharType>(), m_num_char) == 0;
  140. }
  141. template<class T>
  142. static block_header<size_type> *block_header_from_value(T *value)
  143. { return block_header_from_value(value, sizeof(T), ::boost::container::dtl::alignment_of<T>::value); }
  144. static block_header<size_type> *block_header_from_value(const void *value, std::size_t sz, std::size_t algn)
  145. {
  146. block_header * hdr =
  147. const_cast<block_header*>
  148. (reinterpret_cast<const block_header*>(reinterpret_cast<const char*>(value) -
  149. get_rounded_size(sizeof(block_header), algn)));
  150. (void)sz;
  151. //Some sanity checks
  152. BOOST_ASSERT(hdr->m_value_alignment == algn);
  153. BOOST_ASSERT(hdr->m_value_bytes % sz == 0);
  154. return hdr;
  155. }
  156. template<class Header>
  157. static block_header<size_type> *from_first_header(Header *header)
  158. {
  159. block_header<size_type> * hdr =
  160. reinterpret_cast<block_header<size_type>*>(reinterpret_cast<char*>(header) +
  161. get_rounded_size( size_type(sizeof(Header))
  162. , size_type(::boost::container::dtl::alignment_of<block_header<size_type> >::value)));
  163. //Some sanity checks
  164. return hdr;
  165. }
  166. template<class Header>
  167. static Header *to_first_header(block_header<size_type> *bheader)
  168. {
  169. Header * hdr =
  170. reinterpret_cast<Header*>(reinterpret_cast<char*>(bheader) -
  171. get_rounded_size( size_type(sizeof(Header))
  172. , size_type(::boost::container::dtl::alignment_of<block_header<size_type> >::value)));
  173. //Some sanity checks
  174. return hdr;
  175. }
  176. };
  177. inline void array_construct(void *mem, std::size_t num, in_place_interface &table)
  178. {
  179. //Try constructors
  180. std::size_t constructed = 0;
  181. BOOST_TRY{
  182. table.construct_n(mem, num, constructed);
  183. }
  184. //If there is an exception call destructors and erase index node
  185. BOOST_CATCH(...){
  186. std::size_t destroyed = 0;
  187. table.destroy_n(mem, constructed, destroyed);
  188. BOOST_RETHROW
  189. }
  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. (reinterpret_cast<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 reinterpret_cast<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 reinterpret_cast<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