iunordered_set_index.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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_IUNORDERED_SET_INDEX_HPP
  11. #define BOOST_INTERPROCESS_IUNORDERED_SET_INDEX_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. #include <boost/interprocess/detail/utilities.hpp>
  22. #include <boost/interprocess/allocators/allocator.hpp>
  23. #include <boost/interprocess/containers/vector.hpp>
  24. #include <boost/intrusive/unordered_set.hpp>
  25. #include <boost/intrusive/detail/minimal_pair_header.hpp>
  26. #include <boost/intrusive/detail/minimal_less_equal_header.hpp> //std::less
  27. #include <boost/container/detail/minimal_char_traits_header.hpp> //std::char_traits
  28. #include <boost/container/detail/placement_new.hpp>
  29. #include <boost/intrusive/detail/hash.hpp>
  30. //!\file
  31. //!Describes index adaptor of boost::intrusive::unordered_set container, to use it
  32. //!as name/shared memory index
  33. namespace boost { namespace interprocess {
  34. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  35. //!Helper class to define typedefs
  36. //!from IndexTraits
  37. template <class MapConfig>
  38. struct iunordered_set_index_aux
  39. {
  40. typedef typename
  41. MapConfig::segment_manager_base segment_manager_base;
  42. typedef typename
  43. segment_manager_base::void_pointer void_pointer;
  44. typedef typename bi::make_unordered_set_base_hook
  45. < bi::void_pointer<void_pointer>
  46. >::type derivation_hook;
  47. typedef typename MapConfig::template
  48. intrusive_value_type<derivation_hook>::type value_type;
  49. typedef typename MapConfig::
  50. intrusive_compare_key_type intrusive_compare_key_type;
  51. typedef std::equal_to<value_type> value_equal;
  52. typedef typename MapConfig::char_type char_type;
  53. struct equal_function
  54. {
  55. bool operator()(const intrusive_compare_key_type &i, const value_type &b) const
  56. {
  57. return (i.m_len == b.name_length()) &&
  58. (std::char_traits<char_type>::compare
  59. (i.mp_str, b.name(), i.m_len) == 0);
  60. }
  61. bool operator()(const value_type &b, const intrusive_compare_key_type &i) const
  62. {
  63. return (i.m_len == b.name_length()) &&
  64. (std::char_traits<char_type>::compare
  65. (i.mp_str, b.name(), i.m_len) == 0);
  66. }
  67. bool operator()(const value_type &b1, const value_type &b2) const
  68. {
  69. return (b1.name_length() == b2.name_length()) &&
  70. (std::char_traits<char_type>::compare
  71. (b1.name(), b2.name(), b1.name_length()) == 0);
  72. }
  73. };
  74. struct hash_function
  75. {
  76. typedef value_type argument_type;
  77. typedef std::size_t result_type;
  78. std::size_t hash_char_range(const char_type* beg, const char_type* end) const
  79. {
  80. std::size_t seed = 0;
  81. while (beg != end) {
  82. boost::intrusive::detail::hash_combine_size_t(seed, boost::intrusive::detail::internal_hash_functor<char_type>()(*beg));
  83. ++beg;
  84. }
  85. return seed;
  86. }
  87. std::size_t operator()(const value_type &val) const
  88. {
  89. const char_type* beg = ipcdetail::to_raw_pointer(val.name()),
  90. * end = beg + val.name_length();
  91. return hash_char_range(beg, end);
  92. }
  93. std::size_t operator()(const intrusive_compare_key_type &i) const
  94. {
  95. const char_type *beg = i.mp_str,
  96. *end = beg + i.m_len;
  97. return hash_char_range(beg, end);
  98. }
  99. };
  100. typedef typename bi::make_unordered_set
  101. < value_type
  102. , bi::hash<hash_function>
  103. , bi::equal<equal_function>
  104. , bi::size_type<typename segment_manager_base::size_type>
  105. >::type index_t;
  106. typedef typename index_t::bucket_type bucket_type;
  107. typedef allocator
  108. <bucket_type, segment_manager_base> allocator_type;
  109. struct allocator_holder
  110. {
  111. allocator_holder(segment_manager_base *mngr)
  112. : alloc(mngr)
  113. {}
  114. allocator_type alloc;
  115. bucket_type init_bucket;
  116. };
  117. };
  118. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  119. //!Index type based in boost::intrusive::set.
  120. //!Just derives from boost::intrusive::set
  121. //!and defines the interface needed by managed memory segments
  122. template <class MapConfig>
  123. class iunordered_set_index
  124. //Derive class from map specialization
  125. : private iunordered_set_index_aux<MapConfig>::allocator_holder
  126. , public iunordered_set_index_aux<MapConfig>::index_t
  127. {
  128. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  129. typedef iunordered_set_index_aux<MapConfig> index_aux;
  130. typedef typename index_aux::index_t index_type;
  131. typedef typename MapConfig::
  132. intrusive_compare_key_type intrusive_compare_key_type;
  133. typedef typename index_aux::equal_function equal_function;
  134. typedef typename index_aux::hash_function hash_function;
  135. typedef typename MapConfig::char_type char_type;
  136. typedef typename
  137. iunordered_set_index_aux<MapConfig>::allocator_type allocator_type;
  138. typedef typename
  139. iunordered_set_index_aux<MapConfig>::allocator_holder allocator_holder;
  140. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  141. public:
  142. typedef typename index_type::iterator iterator;
  143. typedef typename index_type::const_iterator const_iterator;
  144. typedef typename index_type::insert_commit_data insert_commit_data;
  145. typedef typename index_type::value_type value_type;
  146. typedef typename index_type::bucket_ptr bucket_ptr;
  147. typedef typename index_type::bucket_type bucket_type;
  148. typedef typename index_type::bucket_traits bucket_traits;
  149. typedef typename index_type::size_type size_type;
  150. typedef typename index_type::difference_type difference_type;
  151. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  152. private:
  153. typedef typename index_aux::
  154. segment_manager_base segment_manager_base;
  155. static const std::size_t InitBufferSize = 64;
  156. static bucket_ptr create_buckets(allocator_type &alloc, size_type num)
  157. {
  158. num = index_type::suggested_upper_bucket_count(num);
  159. bucket_ptr buckets = alloc.allocate(num);
  160. bucket_ptr buckets_init = buckets;
  161. for(size_type i = 0; i < num; ++i){
  162. ::new(to_raw_pointer(buckets_init++), boost_container_new_t())bucket_type();
  163. }
  164. return buckets;
  165. }
  166. static size_type shrink_buckets
  167. ( bucket_ptr buckets, size_type old_size
  168. , allocator_type &alloc, size_type new_size)
  169. {
  170. if(old_size <= new_size )
  171. return old_size;
  172. size_type received_size = new_size;
  173. if(!alloc.allocation_command
  174. (boost::interprocess::try_shrink_in_place | boost::interprocess::nothrow_allocation, old_size, received_size, buckets)){
  175. return old_size;
  176. }
  177. for( bucket_type *p = ipcdetail::to_raw_pointer(buckets) + received_size
  178. , *pend = ipcdetail::to_raw_pointer(buckets) + old_size
  179. ; p != pend
  180. ; ++p){
  181. p->~bucket_type();
  182. }
  183. bucket_ptr shunk_p = alloc.allocation_command
  184. (boost::interprocess::shrink_in_place | boost::interprocess::nothrow_allocation, received_size, received_size, buckets);
  185. BOOST_ASSERT(buckets == shunk_p); (void)shunk_p;
  186. bucket_ptr buckets_init = buckets + difference_type(received_size);
  187. for(size_type i = 0; i < (old_size - received_size); ++i){
  188. to_raw_pointer(buckets_init++)->~bucket_type();
  189. }
  190. return received_size;
  191. }
  192. static bucket_ptr expand_or_create_buckets
  193. ( bucket_ptr old_buckets, const size_type old_num
  194. , allocator_type &alloc, const size_type new_num)
  195. {
  196. size_type received_size = new_num;
  197. bucket_ptr reuse(old_buckets);
  198. bucket_ptr ret = alloc.allocation_command
  199. (boost::interprocess::expand_fwd | boost::interprocess::allocate_new, new_num, received_size, reuse);
  200. if(ret == old_buckets){
  201. bucket_ptr buckets_init = old_buckets + difference_type(old_num);
  202. for(size_type i = 0; i < (new_num - old_num); ++i){
  203. ::new(to_raw_pointer(buckets_init++), boost_container_new_t())bucket_type();
  204. }
  205. }
  206. else{
  207. bucket_ptr buckets_init = ret;
  208. for(size_type i = 0; i < new_num; ++i){
  209. ::new(to_raw_pointer(buckets_init++), boost_container_new_t())bucket_type();
  210. }
  211. }
  212. return ret;
  213. }
  214. static void destroy_buckets
  215. (allocator_type &alloc, bucket_ptr buckets, size_type num)
  216. {
  217. bucket_ptr buckets_destroy = buckets;
  218. for(size_type i = 0; i < num; ++i){
  219. to_raw_pointer(buckets_destroy++)->~bucket_type();
  220. }
  221. alloc.deallocate(buckets, num);
  222. }
  223. iunordered_set_index<MapConfig>* get_this_pointer()
  224. { return this; }
  225. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  226. public:
  227. //!Constructor. Takes a pointer to the
  228. //!segment manager. Can throw
  229. iunordered_set_index(segment_manager_base *mngr)
  230. : allocator_holder(mngr)
  231. , index_type(bucket_traits(&get_this_pointer()->init_bucket, 1))
  232. {}
  233. ~iunordered_set_index()
  234. {
  235. index_type::clear();
  236. if(index_type::bucket_pointer() != bucket_ptr(&this->init_bucket)){
  237. destroy_buckets(this->alloc, index_type::bucket_pointer(), index_type::bucket_count());
  238. }
  239. }
  240. //!This reserves memory to optimize the insertion of n
  241. //!elements in the index
  242. void reserve(size_type new_n)
  243. {
  244. //Let's maintain a 1.0f load factor
  245. size_type old_n = this->bucket_count();
  246. if(new_n <= old_n)
  247. return;
  248. bucket_ptr old_p = this->bucket_pointer();
  249. new_n = index_type::suggested_upper_bucket_count(new_n);
  250. bucket_ptr new_p;
  251. //This can throw
  252. BOOST_TRY{
  253. if(old_p != bucket_ptr(&this->init_bucket))
  254. new_p = expand_or_create_buckets(old_p, old_n, this->alloc, new_n);
  255. else
  256. new_p = create_buckets(this->alloc, new_n);
  257. }
  258. BOOST_CATCH(...){
  259. return;
  260. } BOOST_CATCH_END
  261. //Rehashing does not throw, since neither the hash nor the
  262. //comparison function can throw
  263. this->rehash(bucket_traits(new_p, new_n));
  264. if(new_p != old_p && old_p != bucket_ptr(&this->init_bucket)){
  265. destroy_buckets(this->alloc, old_p, old_n);
  266. }
  267. }
  268. //!This tries to free unused memory
  269. //!previously allocated.
  270. void shrink_to_fit()
  271. {
  272. size_type cur_size = this->size();
  273. size_type cur_count = this->bucket_count();
  274. bucket_ptr old_p = this->bucket_pointer();
  275. if(!this->size() && old_p != bucket_ptr(&this->init_bucket)){
  276. this->rehash(bucket_traits(bucket_ptr(&this->init_bucket), 1));
  277. destroy_buckets(this->alloc, old_p, cur_count);
  278. }
  279. else{
  280. size_type sug_count = 0; //gcc warning
  281. sug_count = index_type::suggested_upper_bucket_count(cur_size);
  282. if(sug_count >= cur_count)
  283. return;
  284. BOOST_TRY{
  285. shrink_buckets(old_p, cur_count, this->alloc, sug_count);
  286. }
  287. BOOST_CATCH(...){
  288. return;
  289. } BOOST_CATCH_END
  290. //Rehashing does not throw, since neither the hash nor the
  291. //comparison function can throw
  292. this->rehash(bucket_traits(old_p, sug_count));
  293. }
  294. }
  295. iterator find(const intrusive_compare_key_type &key)
  296. { return index_type::find(key, hash_function(), equal_function()); }
  297. const_iterator find(const intrusive_compare_key_type &key) const
  298. { return index_type::find(key, hash_function(), equal_function()); }
  299. std::pair<iterator, bool>insert_check
  300. (const intrusive_compare_key_type &key, insert_commit_data &commit_data)
  301. { return index_type::insert_check(key, hash_function(), equal_function(), commit_data); }
  302. iterator insert_commit(value_type &val, insert_commit_data &commit_data)
  303. {
  304. iterator it = index_type::insert_commit(val, commit_data);
  305. size_type cur_size = this->size();
  306. if(cur_size > this->bucket_count()){
  307. BOOST_TRY{
  308. this->reserve(cur_size);
  309. }
  310. BOOST_CATCH(...){
  311. //Strong guarantee: if something goes wrong
  312. //we should remove the insertion.
  313. //
  314. //We can use the iterator because the hash function
  315. //can't throw and this means that "reserve" will
  316. //throw only because of the memory allocation:
  317. //the iterator has not been invalidated.
  318. index_type::erase(it);
  319. BOOST_RETHROW
  320. } BOOST_CATCH_END
  321. }
  322. return it;
  323. }
  324. };
  325. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  326. //!Trait class to detect if an index is an intrusive
  327. //!index
  328. template<class MapConfig>
  329. struct is_intrusive_index
  330. <boost::interprocess::iunordered_set_index<MapConfig> >
  331. {
  332. static const bool value = true;
  333. };
  334. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  335. }} //namespace boost { namespace interprocess {
  336. #include <boost/interprocess/detail/config_end.hpp>
  337. #endif //#ifndef BOOST_INTERPROCESS_IUNORDERED_SET_INDEX_HPP