Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:37:04

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_IUNORDERED_SET_INDEX_HPP
0012 #define BOOST_INTERPROCESS_IUNORDERED_SET_INDEX_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 #  pragma once
0020 #endif
0021 
0022 #include <boost/interprocess/detail/config_begin.hpp>
0023 #include <boost/interprocess/detail/workaround.hpp>
0024 
0025 #include <boost/interprocess/detail/utilities.hpp>
0026 #include <boost/interprocess/allocators/allocator.hpp>
0027 #include <boost/container/vector.hpp>
0028 #include <boost/intrusive/unordered_set.hpp>
0029 #include <boost/intrusive/detail/minimal_pair_header.hpp>
0030 #include <boost/intrusive/detail/minimal_less_equal_header.hpp>   //std::less
0031 #include <boost/container/detail/minimal_char_traits_header.hpp>  //std::char_traits
0032 #include <boost/container/detail/placement_new.hpp>
0033 #include <boost/intrusive/detail/hash.hpp>
0034 
0035 //!\file
0036 //!Describes index adaptor of boost::intrusive::unordered_set container, to use it
0037 //!as name/shared memory index
0038 
0039 namespace boost { namespace interprocess {
0040 
0041 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0042 
0043 //!Helper class to define typedefs
0044 //!from IndexTraits
0045 template <class MapConfig>
0046 struct iunordered_set_index_aux
0047 {
0048    typedef typename
0049       MapConfig::segment_manager_base                 segment_manager_base;
0050 
0051    typedef typename
0052       segment_manager_base::void_pointer              void_pointer;
0053 
0054    typedef typename bi::make_unordered_set_base_hook
0055       < bi::void_pointer<void_pointer>
0056       >::type        derivation_hook;
0057 
0058    typedef typename MapConfig::template
0059       intrusive_value_type<derivation_hook>::type     value_type;
0060 
0061    typedef typename MapConfig::compare_key_type       compare_key_type;
0062 
0063    typedef std::equal_to<value_type>                  value_equal;
0064 
0065    typedef typename MapConfig::char_type              char_type;
0066 
0067    struct equal_function
0068    {
0069       bool operator()(const compare_key_type&i, const value_type &b) const
0070       {
0071          return (i.m_len == b.name_length()) &&
0072                   (std::char_traits<char_type>::compare
0073                      (i.mp_str, b.name(), i.m_len) == 0);
0074       }
0075 
0076       bool operator()(const value_type &b, const compare_key_type&i) const
0077       {
0078          return (i.m_len == b.name_length()) &&
0079                   (std::char_traits<char_type>::compare
0080                      (i.mp_str, b.name(), i.m_len) == 0);
0081       }
0082 
0083       bool operator()(const value_type &b1, const value_type &b2) const
0084       {
0085          return (b1.name_length() == b2.name_length()) &&
0086                   (std::char_traits<char_type>::compare
0087                      (b1.name(), b2.name(), b1.name_length()) == 0);
0088       }
0089    };
0090 
0091    struct hash_function
0092    {
0093       typedef value_type argument_type;
0094       typedef std::size_t result_type;
0095 
0096       std::size_t hash_char_range(const char_type* beg, const char_type* end) const
0097       {
0098          std::size_t seed = 0;
0099          while (beg != end) {
0100             boost::intrusive::detail::hash_combine_size_t(seed, boost::intrusive::detail::internal_hash_functor<char_type>()(*beg));
0101             ++beg;
0102          }
0103          return seed;
0104       }
0105 
0106       std::size_t operator()(const value_type &val) const
0107       {
0108          const char_type* beg = ipcdetail::to_raw_pointer(val.name()),
0109                         * end = beg + val.name_length();
0110          return hash_char_range(beg, end);
0111       }
0112 
0113       std::size_t operator()(const compare_key_type&i) const
0114       {
0115          const char_type *beg = i.mp_str,
0116                          *end = beg + i.m_len;
0117          return hash_char_range(beg, end);
0118       }
0119    };
0120 
0121    typedef typename bi::make_unordered_set
0122       < value_type
0123       , bi::hash<hash_function>
0124       , bi::equal<equal_function>
0125      , bi::size_type<typename segment_manager_base::size_type>
0126       >::type                                         index_t;
0127    typedef typename index_t::bucket_type              bucket_type;
0128    typedef allocator
0129       <bucket_type, segment_manager_base>             allocator_type;
0130 
0131    struct allocator_holder
0132    {
0133       allocator_holder(segment_manager_base *mngr)
0134          :  alloc(mngr)
0135       {}
0136       allocator_type alloc;
0137       bucket_type init_bucket;
0138    };
0139 };
0140 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0141 
0142 //!Index type based in boost::intrusive::set.
0143 //!Just derives from boost::intrusive::set
0144 //!and defines the interface needed by managed memory segments
0145 template <class MapConfig>
0146 class iunordered_set_index
0147       //Derive class from map specialization
0148    :  private iunordered_set_index_aux<MapConfig>::allocator_holder
0149    ,  private iunordered_set_index_aux<MapConfig>::index_t
0150 {
0151    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0152    typedef iunordered_set_index_aux<MapConfig>           index_aux;
0153    typedef typename index_aux::index_t                   index_type;
0154    typedef typename index_aux::equal_function            equal_function;
0155    typedef typename index_aux::hash_function             hash_function;
0156    typedef typename MapConfig::char_type                 char_type;
0157    typedef typename
0158       iunordered_set_index_aux<MapConfig>::allocator_type      allocator_type;
0159    typedef typename
0160       iunordered_set_index_aux<MapConfig>::allocator_holder    allocator_holder;
0161    public:
0162    typedef typename MapConfig::compare_key_type          compare_key_type;
0163    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0164 
0165    public:
0166    typedef typename index_type::iterator                 iterator;
0167    typedef typename index_type::const_iterator           const_iterator;
0168    typedef typename index_type::insert_commit_data       insert_commit_data;
0169    typedef typename index_type::value_type               value_type;
0170    typedef typename index_type::bucket_ptr               bucket_ptr;
0171    typedef typename index_type::bucket_type              bucket_type;
0172    typedef typename index_type::bucket_traits            bucket_traits;
0173    typedef typename index_type::size_type                size_type;
0174    typedef typename index_type::difference_type          difference_type;
0175    typedef value_type                                    index_data_t;
0176 
0177    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0178    private:
0179    typedef typename index_aux::
0180       segment_manager_base             segment_manager_base;
0181 
0182    static const std::size_t InitBufferSize = 64;
0183 
0184    static bucket_ptr create_buckets(allocator_type &alloc, size_type num)
0185    {
0186       num = index_type::suggested_upper_bucket_count(num);
0187       bucket_ptr buckets = alloc.allocate(num);
0188       bucket_ptr buckets_init = buckets;
0189       for(size_type i = 0; i < num; ++i){
0190          ::new(to_raw_pointer(buckets_init++), boost_container_new_t())bucket_type();
0191       }
0192       return buckets;
0193    }
0194 
0195    static size_type shrink_buckets
0196       ( bucket_ptr buckets, size_type old_size
0197       , allocator_type &alloc, size_type new_size)
0198    {
0199       if(old_size <= new_size )
0200          return old_size;
0201       size_type received_size = new_size;
0202       if(!alloc.allocation_command
0203          (boost::interprocess::try_shrink_in_place | boost::interprocess::nothrow_allocation, old_size, received_size, buckets)){
0204          return old_size;
0205       }
0206 
0207       for( bucket_type *p = ipcdetail::to_raw_pointer(buckets) + received_size
0208          , *pend = ipcdetail::to_raw_pointer(buckets) + old_size
0209          ; p != pend
0210          ; ++p){
0211          p->~bucket_type();
0212       }
0213 
0214       bucket_ptr shunk_p = alloc.allocation_command
0215          (boost::interprocess::shrink_in_place | boost::interprocess::nothrow_allocation, received_size, received_size, buckets);
0216       BOOST_ASSERT(buckets == shunk_p); (void)shunk_p;
0217 
0218       bucket_ptr buckets_init = buckets + difference_type(received_size);
0219       for(size_type i = 0; i < (old_size - received_size); ++i){
0220          to_raw_pointer(buckets_init++)->~bucket_type();
0221       }
0222       return received_size;
0223    }
0224 
0225    static bucket_ptr expand_or_create_buckets
0226       ( bucket_ptr old_buckets, const size_type old_num
0227       , allocator_type &alloc,  const size_type new_num)
0228    {
0229       size_type received_size = new_num;
0230       bucket_ptr reuse(old_buckets);
0231       bucket_ptr ret = alloc.allocation_command
0232             (boost::interprocess::expand_fwd | boost::interprocess::allocate_new, new_num, received_size, reuse);
0233       if(ret == old_buckets){
0234          bucket_ptr buckets_init = old_buckets + difference_type(old_num);
0235          for(size_type i = 0; i < (new_num - old_num); ++i){
0236             ::new(to_raw_pointer(buckets_init++), boost_container_new_t())bucket_type();
0237          }
0238       }
0239       else{
0240          bucket_ptr buckets_init = ret;
0241          for(size_type i = 0; i < new_num; ++i){
0242             ::new(to_raw_pointer(buckets_init++), boost_container_new_t())bucket_type();
0243          }
0244       }
0245       return ret;
0246    }
0247 
0248    static void destroy_buckets
0249       (allocator_type &alloc, bucket_ptr buckets, size_type num)
0250    {
0251       bucket_ptr buckets_destroy = buckets;
0252       for(size_type i = 0; i < num; ++i){
0253          to_raw_pointer(buckets_destroy++)->~bucket_type();
0254       }
0255       alloc.deallocate(buckets, num);
0256    }
0257 
0258    iunordered_set_index<MapConfig>* get_this_pointer()
0259    {  return this;   }
0260 
0261    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0262 
0263    public:
0264    using index_type::begin;
0265    using index_type::end;
0266    using index_type::size;
0267    using index_type::erase;
0268 
0269    //!Constructor. Takes a pointer to the
0270    //!segment manager. Can throw
0271    iunordered_set_index(segment_manager_base *mngr)
0272       :  allocator_holder(mngr)
0273       ,  index_type(bucket_traits(&get_this_pointer()->init_bucket, 1))
0274    {}
0275 
0276    ~iunordered_set_index()
0277    {
0278       index_type::clear();
0279       if(index_type::bucket_pointer() != bucket_ptr(&this->init_bucket)){
0280          destroy_buckets(this->alloc, index_type::bucket_pointer(), index_type::bucket_count());
0281       }
0282    }
0283 
0284    //!This reserves memory to optimize the insertion of n
0285    //!elements in the index
0286    void reserve(size_type new_n)
0287    {
0288       //Let's maintain a 1.0f load factor
0289       size_type old_n  = this->bucket_count();
0290       if(new_n <= old_n)
0291          return;
0292       bucket_ptr old_p = this->bucket_pointer();
0293       new_n = index_type::suggested_upper_bucket_count(new_n);
0294       bucket_ptr new_p;
0295       //This can throw
0296       BOOST_INTERPROCESS_TRY{
0297          if(old_p != bucket_ptr(&this->init_bucket))
0298             new_p = expand_or_create_buckets(old_p, old_n, this->alloc, new_n);
0299          else
0300             new_p = create_buckets(this->alloc, new_n);
0301       }
0302       BOOST_INTERPROCESS_CATCH(...){
0303          return;
0304       } BOOST_INTERPROCESS_CATCH_END
0305       //Rehashing does not throw, since neither the hash nor the
0306       //comparison function can throw
0307       this->rehash(bucket_traits(new_p, new_n));
0308       if(new_p != old_p && old_p != bucket_ptr(&this->init_bucket)){
0309          destroy_buckets(this->alloc, old_p, old_n);
0310       }
0311    }
0312 
0313    //!This tries to free unused memory
0314    //!previously allocated.
0315    void shrink_to_fit()
0316    {
0317       size_type cur_size   = this->size();
0318       size_type cur_count  = this->bucket_count();
0319       bucket_ptr old_p = this->bucket_pointer();
0320 
0321       if(!this->size() && old_p != bucket_ptr(&this->init_bucket)){
0322          this->rehash(bucket_traits(bucket_ptr(&this->init_bucket), 1));
0323          destroy_buckets(this->alloc, old_p, cur_count);
0324       }
0325       else{
0326          size_type sug_count = 0; //gcc warning
0327          sug_count = index_type::suggested_upper_bucket_count(cur_size);
0328 
0329          if(sug_count >= cur_count)
0330             return;
0331 
0332          BOOST_INTERPROCESS_TRY{
0333             shrink_buckets(old_p, cur_count, this->alloc, sug_count);
0334          }
0335          BOOST_INTERPROCESS_CATCH(...){
0336             return;
0337          } BOOST_INTERPROCESS_CATCH_END
0338 
0339          //Rehashing does not throw, since neither the hash nor the
0340          //comparison function can throw
0341          this->rehash(bucket_traits(old_p, sug_count));
0342       }
0343    }
0344 
0345    iterator find(const compare_key_type&key)
0346    {  return index_type::find(key, hash_function(), equal_function());  }
0347 
0348    const_iterator find(const compare_key_type&key) const
0349    {  return index_type::find(key, hash_function(), equal_function());  }
0350 
0351    std::pair<iterator, bool>insert_check
0352       (const compare_key_type&key, insert_commit_data &commit_data)
0353    {  return index_type::insert_check(key, hash_function(), equal_function(), commit_data); }
0354 
0355    iterator insert_commit
0356       (const compare_key_type &, void *, index_data_t &val, insert_commit_data& commit_data)
0357    {
0358       iterator it = index_type::insert_commit(val, commit_data);
0359       size_type cur_size      = this->size();
0360       if(cur_size > this->bucket_count()){
0361          BOOST_INTERPROCESS_TRY{
0362             this->reserve(cur_size);
0363          }
0364          BOOST_INTERPROCESS_CATCH(...){
0365             //Strong guarantee: if something goes wrong
0366             //we should remove the insertion.
0367             //
0368             //We can use the iterator because the hash function
0369             //can't throw and this means that "reserve" will
0370             //throw only because of the memory allocation:
0371             //the iterator has not been invalidated.
0372             index_type::erase(it);
0373             BOOST_INTERPROCESS_RETHROW
0374          } BOOST_INTERPROCESS_CATCH_END
0375       }
0376       return it;
0377    }
0378 };
0379 
0380 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0381 
0382 //!Trait class to detect if an index is an intrusive
0383 //!index
0384 template<class MapConfig>
0385 struct is_intrusive_index
0386    <boost::interprocess::iunordered_set_index<MapConfig> >
0387 {
0388    static const bool value = true;
0389 };
0390 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0391 
0392 }}   //namespace boost { namespace interprocess {
0393 
0394 #include <boost/interprocess/detail/config_end.hpp>
0395 
0396 #endif   //#ifndef BOOST_INTERPROCESS_IUNORDERED_SET_INDEX_HPP