/Src/Dependencies/Boost/boost/interprocess/detail/segment_manager_helper.hpp

http://hadesmem.googlecode.com/ · C++ Header · 504 lines · 370 code · 98 blank · 36 comment · 16 complexity · 6b39c568f7235027ce63fa4bb92eee6a MD5 · raw file

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