segment_manager_helper.hpp 15 KB

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