iunordered_set_index.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  141. private:
  142. typedef typename index_aux::
  143. segment_manager_base segment_manager_base;
  144. static const std::size_t InitBufferSize = 64;
  145. static bucket_ptr create_buckets(allocator_type &alloc, size_type num)
  146. {
  147. num = index_type::suggested_upper_bucket_count(num);
  148. bucket_ptr buckets = alloc.allocate(num);
  149. bucket_ptr buckets_init = buckets;
  150. for(size_type i = 0; i < num; ++i){
  151. ::new(to_raw_pointer(buckets_init++), boost_container_new_t())bucket_type();
  152. }
  153. return buckets;
  154. }
  155. static size_type shrink_buckets
  156. ( bucket_ptr buckets, size_type old_size
  157. , allocator_type &alloc, size_type new_size)
  158. {
  159. if(old_size <= new_size )
  160. return old_size;
  161. size_type received_size = new_size;
  162. if(!alloc.allocation_command
  163. (boost::interprocess::try_shrink_in_place | boost::interprocess::nothrow_allocation, old_size, received_size, buckets)){
  164. return old_size;
  165. }
  166. for( bucket_type *p = ipcdetail::to_raw_pointer(buckets) + received_size
  167. , *pend = ipcdetail::to_raw_pointer(buckets) + old_size
  168. ; p != pend
  169. ; ++p){
  170. p->~bucket_type();
  171. }
  172. bucket_ptr shunk_p = alloc.allocation_command
  173. (boost::interprocess::shrink_in_place | boost::interprocess::nothrow_allocation, received_size, received_size, buckets);
  174. BOOST_ASSERT(buckets == shunk_p); (void)shunk_p;
  175. bucket_ptr buckets_init = buckets + received_size;
  176. for(size_type i = 0; i < (old_size - received_size); ++i){
  177. to_raw_pointer(buckets_init++)->~bucket_type();
  178. }
  179. return received_size;
  180. }
  181. static bucket_ptr expand_or_create_buckets
  182. ( bucket_ptr old_buckets, const size_type old_num
  183. , allocator_type &alloc, const size_type new_num)
  184. {
  185. size_type received_size = new_num;
  186. bucket_ptr reuse(old_buckets);
  187. bucket_ptr ret = alloc.allocation_command
  188. (boost::interprocess::expand_fwd | boost::interprocess::allocate_new, new_num, received_size, reuse);
  189. if(ret == old_buckets){
  190. bucket_ptr buckets_init = old_buckets + old_num;
  191. for(size_type i = 0; i < (new_num - old_num); ++i){
  192. ::new(to_raw_pointer(buckets_init++), boost_container_new_t())bucket_type();
  193. }
  194. }
  195. else{
  196. bucket_ptr buckets_init = ret;
  197. for(size_type i = 0; i < new_num; ++i){
  198. ::new(to_raw_pointer(buckets_init++), boost_container_new_t())bucket_type();
  199. }
  200. }
  201. return ret;
  202. }
  203. static void destroy_buckets
  204. (allocator_type &alloc, bucket_ptr buckets, size_type num)
  205. {
  206. bucket_ptr buckets_destroy = buckets;
  207. for(size_type i = 0; i < num; ++i){
  208. to_raw_pointer(buckets_destroy++)->~bucket_type();
  209. }
  210. alloc.deallocate(buckets, num);
  211. }
  212. iunordered_set_index<MapConfig>* get_this_pointer()
  213. { return this; }
  214. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  215. public:
  216. //!Constructor. Takes a pointer to the
  217. //!segment manager. Can throw
  218. iunordered_set_index(segment_manager_base *mngr)
  219. : allocator_holder(mngr)
  220. , index_type(bucket_traits(&get_this_pointer()->init_bucket, 1))
  221. {}
  222. ~iunordered_set_index()
  223. {
  224. index_type::clear();
  225. if(index_type::bucket_pointer() != bucket_ptr(&this->init_bucket)){
  226. destroy_buckets(this->alloc, index_type::bucket_pointer(), index_type::bucket_count());
  227. }
  228. }
  229. //!This reserves memory to optimize the insertion of n
  230. //!elements in the index
  231. void reserve(size_type new_n)
  232. {
  233. //Let's maintain a 1.0f load factor
  234. size_type old_n = this->bucket_count();
  235. if(new_n <= old_n)
  236. return;
  237. bucket_ptr old_p = this->bucket_pointer();
  238. new_n = index_type::suggested_upper_bucket_count(new_n);
  239. bucket_ptr new_p;
  240. //This can throw
  241. try{
  242. if(old_p != bucket_ptr(&this->init_bucket))
  243. new_p = expand_or_create_buckets(old_p, old_n, this->alloc, new_n);
  244. else
  245. new_p = create_buckets(this->alloc, new_n);
  246. }
  247. catch(...){
  248. return;
  249. }
  250. //Rehashing does not throw, since neither the hash nor the
  251. //comparison function can throw
  252. this->rehash(bucket_traits(new_p, new_n));
  253. if(new_p != old_p && old_p != bucket_ptr(&this->init_bucket)){
  254. destroy_buckets(this->alloc, old_p, old_n);
  255. }
  256. }
  257. //!This tries to free unused memory
  258. //!previously allocated.
  259. void shrink_to_fit()
  260. {
  261. size_type cur_size = this->size();
  262. size_type cur_count = this->bucket_count();
  263. bucket_ptr old_p = this->bucket_pointer();
  264. if(!this->size() && old_p != bucket_ptr(&this->init_bucket)){
  265. this->rehash(bucket_traits(bucket_ptr(&this->init_bucket), 1));
  266. destroy_buckets(this->alloc, old_p, cur_count);
  267. }
  268. else{
  269. size_type sug_count = 0; //gcc warning
  270. sug_count = index_type::suggested_upper_bucket_count(cur_size);
  271. if(sug_count >= cur_count)
  272. return;
  273. try{
  274. shrink_buckets(old_p, cur_count, this->alloc, sug_count);
  275. }
  276. catch(...){
  277. return;
  278. }
  279. //Rehashing does not throw, since neither the hash nor the
  280. //comparison function can throw
  281. this->rehash(bucket_traits(old_p, sug_count));
  282. }
  283. }
  284. iterator find(const intrusive_compare_key_type &key)
  285. { return index_type::find(key, hash_function(), equal_function()); }
  286. const_iterator find(const intrusive_compare_key_type &key) const
  287. { return index_type::find(key, hash_function(), equal_function()); }
  288. std::pair<iterator, bool>insert_check
  289. (const intrusive_compare_key_type &key, insert_commit_data &commit_data)
  290. { return index_type::insert_check(key, hash_function(), equal_function(), commit_data); }
  291. iterator insert_commit(value_type &val, insert_commit_data &commit_data)
  292. {
  293. iterator it = index_type::insert_commit(val, commit_data);
  294. size_type cur_size = this->size();
  295. if(cur_size > this->bucket_count()){
  296. try{
  297. this->reserve(cur_size);
  298. }
  299. catch(...){
  300. //Strong guarantee: if something goes wrong
  301. //we should remove the insertion.
  302. //
  303. //We can use the iterator because the hash function
  304. //can't throw and this means that "reserve" will
  305. //throw only because of the memory allocation:
  306. //the iterator has not been invalidated.
  307. index_type::erase(it);
  308. throw;
  309. }
  310. }
  311. return it;
  312. }
  313. };
  314. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  315. //!Trait class to detect if an index is an intrusive
  316. //!index
  317. template<class MapConfig>
  318. struct is_intrusive_index
  319. <boost::interprocess::iunordered_set_index<MapConfig> >
  320. {
  321. static const bool value = true;
  322. };
  323. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  324. }} //namespace boost { namespace interprocess {
  325. #include <boost/interprocess/detail/config_end.hpp>
  326. #endif //#ifndef BOOST_INTERPROCESS_IUNORDERED_SET_INDEX_HPP