iunordered_set_index.hpp 14 KB

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