Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-05 08:35:59

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_SEGMENT_MANAGER_HPP
0012 #define BOOST_INTERPROCESS_SEGMENT_MANAGER_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/core/no_exceptions_support.hpp>
0026 #include <boost/interprocess/detail/type_traits.hpp>
0027 
0028 #include <boost/interprocess/detail/transform_iterator.hpp>
0029 
0030 #include <boost/interprocess/detail/mpl.hpp>
0031 #include <boost/interprocess/detail/nothrow.hpp>
0032 #include <boost/interprocess/detail/segment_manager_helper.hpp>
0033 #include <boost/interprocess/detail/named_proxy.hpp>
0034 #include <boost/interprocess/detail/utilities.hpp>
0035 #include <boost/interprocess/offset_ptr.hpp>
0036 #include <boost/interprocess/indexes/iset_index.hpp>
0037 #include <boost/interprocess/exceptions.hpp>
0038 #include <boost/interprocess/allocators/allocator.hpp>
0039 #include <boost/interprocess/smart_ptr/deleter.hpp>
0040 #include <boost/move/utility_core.hpp>
0041 #include <boost/interprocess/sync/scoped_lock.hpp>
0042 // container/detail
0043 #include <boost/container/detail/minimal_char_traits_header.hpp>
0044 #include <boost/container/detail/placement_new.hpp>
0045 // std
0046 #include <cstddef>   //std::size_t
0047 #include <boost/intrusive/detail/minimal_pair_header.hpp>
0048 #include <boost/assert.hpp>
0049 #ifndef BOOST_NO_EXCEPTIONS
0050 #include <exception>
0051 #endif
0052 #include <typeinfo>
0053 
0054 //!\file
0055 //!Describes the object placed in a memory segment that provides
0056 //!named object allocation capabilities for single-segment and
0057 //!multi-segment allocations.
0058 
0059 namespace boost{
0060 namespace interprocess{
0061 
0062 //!This object is the public base class of segment manager.
0063 //!This class only depends on the memory allocation algorithm
0064 //!and implements all the allocation features not related
0065 //!to named or unique objects.
0066 //!
0067 //!Storing a reference to segment_manager forces
0068 //!the holder class to be dependent on index types and character types.
0069 //!When such dependence is not desirable and only anonymous and raw
0070 //!allocations are needed, segment_manager_base is the correct answer.
0071 template<class MemoryAlgorithm>
0072 class segment_manager_base
0073    :  private MemoryAlgorithm
0074 {
0075    public:
0076    typedef segment_manager_base<MemoryAlgorithm> segment_manager_base_type;
0077    typedef typename MemoryAlgorithm::void_pointer  void_pointer;
0078    typedef typename MemoryAlgorithm::mutex_family  mutex_family;
0079    typedef MemoryAlgorithm memory_algorithm;
0080 
0081    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0082 
0083    //Experimental. Don't use
0084    typedef typename MemoryAlgorithm::multiallocation_chain    multiallocation_chain;
0085    typedef typename MemoryAlgorithm::difference_type  difference_type;
0086    typedef typename MemoryAlgorithm::size_type        size_type;
0087 
0088    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0089 
0090    //!This constant indicates the payload size
0091    //!associated with each allocation of the memory algorithm
0092    static const size_type PayloadPerAllocation = MemoryAlgorithm::PayloadPerAllocation;
0093 
0094    //!Constructor of the segment_manager_base
0095    //!
0096    //!"size" is the size of the memory segment where
0097    //!the basic segment manager is being constructed.
0098    //!
0099    //!"reserved_bytes" is the number of bytes
0100    //!after the end of the memory algorithm object itself
0101    //!that the memory algorithm will exclude from
0102    //!dynamic allocation
0103    //!
0104    //!Can throw
0105    segment_manager_base(size_type sz, size_type reserved_bytes)
0106       :  MemoryAlgorithm(sz, reserved_bytes)
0107    {
0108       BOOST_ASSERT((sizeof(segment_manager_base<MemoryAlgorithm>) == sizeof(MemoryAlgorithm)));
0109    }
0110 
0111    //!Returns the size of the memory
0112    //!segment
0113    size_type get_size() const
0114    {  return MemoryAlgorithm::get_size();  }
0115 
0116    //!Returns the number of free bytes of the memory
0117    //!segment
0118    size_type get_free_memory() const
0119    {  return MemoryAlgorithm::get_free_memory();  }
0120 
0121    //!Obtains the minimum size needed by
0122    //!the segment manager
0123    static size_type get_min_size (size_type size)
0124    {  return MemoryAlgorithm::get_min_size(size);  }
0125 
0126    //!Allocates nbytes bytes. This function is only used in
0127    //!single-segment management. Never throws
0128    void * allocate (size_type nbytes, const std::nothrow_t &)
0129    {  return MemoryAlgorithm::allocate(nbytes);   }
0130 
0131    //!Returns a reference to the internal memory algorithm.
0132    //!This function is useful for custom memory algorithms that
0133    //!need additional configuration options after construction. Never throws.
0134    //!This function should be only used by advanced users.
0135    MemoryAlgorithm &get_memory_algorithm()
0136    {  return static_cast<MemoryAlgorithm&>(*this);   }
0137 
0138    //!Returns a const reference to the internal memory algorithm.
0139    //!This function is useful for custom memory algorithms that
0140    //!need additional configuration options after construction. Never throws.
0141    //!This function should be only used by advanced users.
0142    const MemoryAlgorithm &get_memory_algorithm() const
0143    {  return static_cast<const MemoryAlgorithm&>(*this);   }
0144 
0145    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0146 
0147    //Experimental. Dont' use.
0148    //!Allocates n_elements of elem_bytes bytes.
0149    //!Throws bad_alloc on failure. chain.size() is not increased on failure.
0150    void allocate_many(size_type elem_bytes, size_type n_elements, multiallocation_chain &chain)
0151    {
0152       size_type prev_size = chain.size();
0153       MemoryAlgorithm::allocate_many(elem_bytes, n_elements, chain);
0154       if(!elem_bytes || chain.size() == prev_size){
0155          throw bad_alloc();
0156       }
0157    }
0158 
0159    //!Allocates n_elements, each one of element_lengths[i]*sizeof_element bytes.
0160    //!Throws bad_alloc on failure. chain.size() is not increased on failure.
0161    void allocate_many(const size_type *element_lengths, size_type n_elements, size_type sizeof_element, multiallocation_chain &chain)
0162    {
0163       size_type prev_size = chain.size();
0164       MemoryAlgorithm::allocate_many(element_lengths, n_elements, sizeof_element, chain);
0165       if(!sizeof_element || chain.size() == prev_size){
0166          throw bad_alloc();
0167       }
0168    }
0169 
0170    //!Allocates n_elements of elem_bytes bytes.
0171    //!Non-throwing version. chain.size() is not increased on failure.
0172    void allocate_many(const std::nothrow_t &, size_type elem_bytes, size_type n_elements, multiallocation_chain &chain)
0173    {  MemoryAlgorithm::allocate_many(elem_bytes, n_elements, chain); }
0174 
0175    //!Allocates n_elements, each one of
0176    //!element_lengths[i]*sizeof_element bytes.
0177    //!Non-throwing version. chain.size() is not increased on failure.
0178    void allocate_many(const std::nothrow_t &, const size_type *elem_sizes, size_type n_elements, size_type sizeof_element, multiallocation_chain &chain)
0179    {  MemoryAlgorithm::allocate_many(elem_sizes, n_elements, sizeof_element, chain); }
0180 
0181    //!Deallocates all elements contained in chain.
0182    //!Never throws.
0183    void deallocate_many(multiallocation_chain &chain)
0184    {  MemoryAlgorithm::deallocate_many(chain); }
0185 
0186    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0187 
0188    //!Allocates nbytes bytes. Throws boost::interprocess::bad_alloc
0189    //!on failure
0190    void * allocate(size_type nbytes)
0191    {
0192       void * ret = MemoryAlgorithm::allocate(nbytes);
0193       if(!ret)
0194          throw bad_alloc();
0195       return ret;
0196    }
0197 
0198    //!Allocates nbytes bytes. This function is only used in
0199    //!single-segment management. Never throws
0200    void * allocate_aligned (size_type nbytes, size_type alignment, const std::nothrow_t &)
0201    {  return MemoryAlgorithm::allocate_aligned(nbytes, alignment);   }
0202 
0203    //!Allocates nbytes bytes. This function is only used in
0204    //!single-segment management. Throws bad_alloc when fails
0205    void * allocate_aligned(size_type nbytes, size_type alignment)
0206    {
0207       void * ret = MemoryAlgorithm::allocate_aligned(nbytes, alignment);
0208       if(!ret)
0209          throw bad_alloc();
0210       return ret;
0211    }
0212 
0213    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0214 
0215    template<class T>
0216    T *allocation_command  (boost::interprocess::allocation_type command, size_type limit_size,
0217                            size_type &prefer_in_recvd_out_size, T *&reuse)
0218    {
0219       T *ret = MemoryAlgorithm::allocation_command
0220          (command | boost::interprocess::nothrow_allocation, limit_size, prefer_in_recvd_out_size, reuse);
0221       if(!(command & boost::interprocess::nothrow_allocation) && !ret)
0222          throw bad_alloc();
0223       return ret;
0224    }
0225 
0226    void *raw_allocation_command  (boost::interprocess::allocation_type command,   size_type limit_objects,
0227                            size_type &prefer_in_recvd_out_size, void *&reuse, size_type sizeof_object = 1)
0228    {
0229       void *ret = MemoryAlgorithm::raw_allocation_command
0230          ( command | boost::interprocess::nothrow_allocation, limit_objects,
0231            prefer_in_recvd_out_size, reuse, sizeof_object);
0232       if(!(command & boost::interprocess::nothrow_allocation) && !ret)
0233          throw bad_alloc();
0234       return ret;
0235    }
0236 
0237    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0238 
0239    //!Deallocates the bytes allocated with allocate/allocate_many()
0240    //!pointed by addr
0241    void   deallocate          (void *addr)
0242    {  MemoryAlgorithm::deallocate(addr);   }
0243 
0244    //!Increases managed memory in extra_size bytes more. This only works
0245    //!with single-segment management.
0246    void grow(size_type extra_size)
0247    {  MemoryAlgorithm::grow(extra_size);   }
0248 
0249    //!Decreases managed memory to the minimum. This only works
0250    //!with single-segment management.
0251    void shrink_to_fit()
0252    {  MemoryAlgorithm::shrink_to_fit();   }
0253 
0254    //!Returns the result of "all_memory_deallocated()" function
0255    //!of the used memory algorithm
0256    bool all_memory_deallocated()
0257    {   return MemoryAlgorithm::all_memory_deallocated(); }
0258 
0259    //!Returns the result of "check_sanity()" function
0260    //!of the used memory algorithm
0261    bool check_sanity()
0262    {   return MemoryAlgorithm::check_sanity(); }
0263 
0264    //!Writes to zero free memory (memory not yet allocated)
0265    //!of the memory algorithm
0266    void zero_free_memory()
0267    {   MemoryAlgorithm::zero_free_memory(); }
0268 
0269    //!Returns the size of the buffer previously allocated pointed by ptr
0270    size_type size(const void *ptr) const
0271    {   return MemoryAlgorithm::size(ptr); }
0272 
0273    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0274    protected:
0275    void * prot_anonymous_construct
0276       (size_type num, bool dothrow, ipcdetail::in_place_interface &table)
0277    {
0278       typedef ipcdetail::block_header<size_type> block_header_t;
0279       block_header_t block_info (  size_type(table.size*num)
0280                                  , size_type(table.alignment)
0281                                  , anonymous_type
0282                                  , 1
0283                                  , 0);
0284 
0285       //Allocate memory
0286       void *ptr_struct = this->allocate(block_info.total_size(), nothrow<>::get());
0287 
0288       //Check if there is enough memory
0289       if(!ptr_struct){
0290          if(dothrow){
0291             throw bad_alloc();
0292          }
0293          else{
0294             return 0;
0295          }
0296       }
0297 
0298       //Build scoped ptr to avoid leaks with constructor exception
0299       ipcdetail::mem_algo_deallocator<MemoryAlgorithm> mem(ptr_struct, *this);
0300 
0301       //Now construct the header
0302       block_header_t * hdr = ::new(ptr_struct, boost_container_new_t()) block_header_t(block_info);
0303       void *ptr = 0; //avoid gcc warning
0304       ptr = hdr->value();
0305 
0306       //Now call constructors
0307       table.construct_n(ptr, num);
0308 
0309       //All constructors successful, we don't want erase memory
0310       mem.release();
0311       return ptr;
0312    }
0313 
0314    //!Calls the destructor and makes an anonymous deallocate
0315    void prot_anonymous_destroy(const void *object, ipcdetail::in_place_interface &table)
0316    {
0317 
0318       //Get control data from associated with this object
0319       typedef ipcdetail::block_header<size_type> block_header_t;
0320       block_header_t *ctrl_data = block_header_t::block_header_from_value(object, table.size, table.alignment);
0321 
0322       //-------------------------------
0323       //scoped_lock<rmutex> guard(m_header);
0324       //-------------------------------
0325 
0326       if(ctrl_data->alloc_type() != anonymous_type){
0327          //This is not an anonymous object, the pointer is wrong!
0328          BOOST_ASSERT(0);
0329       }
0330 
0331       //Call destructors and free memory
0332       //Build scoped ptr to avoid leaks with destructor exception
0333       table.destroy_n(const_cast<void*>(object), ctrl_data->m_value_bytes/table.size);
0334       this->deallocate(ctrl_data);
0335    }
0336    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0337 };
0338 
0339 //!This object is placed in the beginning of memory segment and
0340 //!implements the allocation (named or anonymous) of portions
0341 //!of the segment. This object contains two indexes that
0342 //!maintain an association between a name and a portion of the segment.
0343 //!
0344 //!The first index contains the mappings for normal named objects using the
0345 //!char type specified in the template parameter.
0346 //!
0347 //!The second index contains the association for unique instances. The key will
0348 //!be the const char * returned from type_info.name() function for the unique
0349 //!type to be constructed.
0350 //!
0351 //!segment_manager<CharType, MemoryAlgorithm, IndexType> inherits publicly
0352 //!from segment_manager_base<MemoryAlgorithm> and inherits from it
0353 //!many public functions related to anonymous object and raw memory allocation.
0354 //!See segment_manager_base reference to know about those functions.
0355 template<class CharType
0356         ,class MemoryAlgorithm
0357         ,template<class IndexConfig> class IndexType>
0358 class segment_manager
0359    :  public segment_manager_base<MemoryAlgorithm>
0360 {
0361    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0362    //Non-copyable
0363    segment_manager();
0364    segment_manager(const segment_manager &);
0365    segment_manager &operator=(const segment_manager &);
0366    typedef segment_manager_base<MemoryAlgorithm> segment_manager_base_t;
0367    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0368 
0369    public:
0370    typedef MemoryAlgorithm                                  memory_algorithm;
0371    typedef typename segment_manager_base_t::void_pointer    void_pointer;
0372    typedef typename segment_manager_base_t::size_type       size_type;
0373    typedef typename segment_manager_base_t::difference_type difference_type;
0374    typedef CharType                                         char_type;
0375 
0376    typedef segment_manager_base<MemoryAlgorithm>   segment_manager_base_type;
0377 
0378    static const size_type PayloadPerAllocation = segment_manager_base_t::PayloadPerAllocation;
0379 
0380    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0381    private:
0382    typedef ipcdetail::block_header<size_type> block_header_t;
0383    typedef ipcdetail::index_config<CharType, MemoryAlgorithm>  index_config_named;
0384    typedef ipcdetail::index_config<char, MemoryAlgorithm>      index_config_unique;
0385    typedef IndexType<index_config_named>                    index_type;
0386    typedef ipcdetail::bool_<is_intrusive_index<index_type>::value >    is_intrusive_t;
0387    typedef ipcdetail::bool_<is_node_index<index_type>::value>          is_node_index_t;
0388 
0389    public:
0390    typedef IndexType<index_config_named>                    named_index_t;
0391    typedef IndexType<index_config_unique>                   unique_index_t;
0392    typedef ipcdetail::char_ptr_holder<CharType>                char_ptr_holder_t;
0393    typedef ipcdetail::segment_manager_iterator_transform
0394       <typename named_index_t::const_iterator
0395       ,is_intrusive_index<index_type>::value>   named_transform;
0396 
0397    typedef ipcdetail::segment_manager_iterator_transform
0398       <typename unique_index_t::const_iterator
0399       ,is_intrusive_index<index_type>::value>   unique_transform;
0400    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0401 
0402    typedef typename segment_manager_base_t::mutex_family       mutex_family;
0403 
0404    typedef transform_iterator
0405       <typename named_index_t::const_iterator, named_transform> const_named_iterator;
0406    typedef transform_iterator
0407       <typename unique_index_t::const_iterator, unique_transform> const_unique_iterator;
0408 
0409    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0410 
0411    //!Constructor proxy object definition helper class
0412    template<class T>
0413    struct construct_proxy
0414    {
0415       typedef ipcdetail::named_proxy<segment_manager, T, false>   type;
0416    };
0417 
0418    //!Constructor proxy object definition helper class
0419    template<class T>
0420    struct construct_iter_proxy
0421    {
0422       typedef ipcdetail::named_proxy<segment_manager, T, true>   type;
0423    };
0424 
0425    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0426 
0427    //!Constructor of the segment manager
0428    //!"size" is the size of the memory segment where
0429    //!the segment manager is being constructed.
0430    //!Can throw
0431    explicit segment_manager(size_type segment_size)
0432       :  segment_manager_base_t(segment_size, priv_get_reserved_bytes())
0433       ,  m_header(static_cast<segment_manager_base_t*>(get_this_pointer()))
0434    {
0435       (void) anonymous_instance;   (void) unique_instance;
0436       //Check EBO is applied, it's required
0437       const void * const this_addr = this;
0438       const void *const segm_addr  = static_cast<segment_manager_base_t*>(this);
0439       (void)this_addr;  (void)segm_addr;
0440       BOOST_ASSERT( this_addr == segm_addr);
0441       const std::size_t void_ptr_alignment = boost::move_detail::alignment_of<void_pointer>::value; (void)void_ptr_alignment;
0442       BOOST_ASSERT((0 == (std::size_t)this_addr % boost::move_detail::alignment_of<segment_manager>::value));
0443    }
0444 
0445    //!Tries to find a previous named/unique allocation. Returns the address
0446    //!and the object count. On failure the first member of the
0447    //!returned pair is 0.
0448    template <class T>
0449    std::pair<T*, size_type> find  (char_ptr_holder_t name)
0450    {  return this->priv_find_impl<T>(name, true);  }
0451 
0452    //!Tries to find a previous named/unique allocation. Returns the address
0453    //!and the object count. On failure the first member of the
0454    //!returned pair is 0. This search is not mutex-protected!
0455    //!Use it only inside atomic_func() calls, where the internal mutex
0456    //!is guaranteed to be locked.
0457    template <class T>
0458    std::pair<T*, size_type> find_no_lock  (char_ptr_holder_t name)
0459    {  return this->priv_find_impl<T>(name, false);  }
0460 
0461    //!Returns throwing "construct" proxy
0462    //!object
0463    template <class T>
0464    typename construct_proxy<T>::type
0465       construct(char_ptr_holder_t name)
0466    {  return typename construct_proxy<T>::type (this, name, false, true);  }
0467 
0468    //!Returns throwing "search or construct" proxy
0469    //!object
0470    template <class T>
0471    typename construct_proxy<T>::type find_or_construct(char_ptr_holder_t name)
0472    {  return typename construct_proxy<T>::type (this, name, true, true);  }
0473 
0474    //!Returns no throwing "construct" proxy
0475    //!object
0476    template <class T>
0477    typename construct_proxy<T>::type
0478       construct(char_ptr_holder_t name, const std::nothrow_t &)
0479    {  return typename construct_proxy<T>::type (this, name, false, false);  }
0480 
0481    //!Returns no throwing "search or construct"
0482    //!proxy object
0483    template <class T>
0484    typename construct_proxy<T>::type
0485       find_or_construct(char_ptr_holder_t name, const std::nothrow_t &)
0486    {  return typename construct_proxy<T>::type (this, name, true, false);  }
0487 
0488    //!Returns throwing "construct from iterators" proxy object
0489    template <class T>
0490    typename construct_iter_proxy<T>::type
0491       construct_it(char_ptr_holder_t name)
0492    {  return typename construct_iter_proxy<T>::type (this, name, false, true);  }
0493 
0494    //!Returns throwing "search or construct from iterators"
0495    //!proxy object
0496    template <class T>
0497    typename construct_iter_proxy<T>::type
0498       find_or_construct_it(char_ptr_holder_t name)
0499    {  return typename construct_iter_proxy<T>::type (this, name, true, true);  }
0500 
0501    //!Returns no throwing "construct from iterators"
0502    //!proxy object
0503    template <class T>
0504    typename construct_iter_proxy<T>::type
0505       construct_it(char_ptr_holder_t name, const std::nothrow_t &)
0506    {  return typename construct_iter_proxy<T>::type (this, name, false, false);  }
0507 
0508    //!Returns no throwing "search or construct from iterators"
0509    //!proxy object
0510    template <class T>
0511    typename construct_iter_proxy<T>::type
0512       find_or_construct_it(char_ptr_holder_t name, const std::nothrow_t &)
0513    {  return typename construct_iter_proxy<T>::type (this, name, true, false);  }
0514 
0515    //!Calls object function blocking recursive interprocess_mutex and guarantees that
0516    //!no new named_alloc or destroy will be executed by any process while
0517    //!executing the object function call
0518    template <class Func>
0519    void atomic_func(Func &f)
0520    {  scoped_lock<rmutex> guard(m_header);  f();  }
0521 
0522    //!Tries to calls a functor guaranteeing that no new construction, search or
0523    //!destruction will be executed by any process while executing the object
0524    //!function call. If the atomic function can't be immediatelly executed
0525    //!because the internal mutex is already locked, returns false.
0526    //!If the functor throws, this function throws.
0527    template <class Func>
0528    bool try_atomic_func(Func &f)
0529    {
0530       scoped_lock<rmutex> guard(m_header, try_to_lock);
0531       if(guard){
0532          f();
0533          return true;
0534       }
0535       else{
0536          return false;
0537       }
0538    }
0539 
0540    //!Destroys a previously created named/unique instance.
0541    //!Returns false if the object was not present.
0542    template <class T>
0543    bool destroy(char_ptr_holder_t name)
0544    {
0545       BOOST_ASSERT(!name.is_anonymous());
0546       ipcdetail::placement_destroy<T> dtor;
0547 
0548       if(name.is_unique()){
0549          return this->priv_generic_named_destroy<char>
0550             ( typeid(T).name(), m_header.m_unique_index , dtor, is_intrusive_t());
0551       }
0552       else{
0553          return this->priv_generic_named_destroy<CharType>
0554             ( name.get(), m_header.m_named_index, dtor, is_intrusive_t());
0555       }
0556    }
0557 
0558    //!Destroys an anonymous, unique or named object
0559    //!using its address
0560    template <class T>
0561    void destroy_ptr(const T *p)
0562    {
0563       //If T is void transform it to char
0564       typedef typename ipcdetail::char_if_void<T>::type data_t;
0565       ipcdetail::placement_destroy<data_t> dtor;
0566       priv_destroy_ptr(p, dtor);
0567    }
0568 
0569    //!Returns the name of an object created with construct/find_or_construct
0570    //!functions. Does not throw
0571    template<class T>
0572    static const CharType *get_instance_name(const T *ptr)
0573    { return priv_get_instance_name(block_header_t::block_header_from_value(ptr));  }
0574 
0575    //!Returns the length of an object created with construct/find_or_construct
0576    //!functions. Does not throw.
0577    template<class T>
0578    static size_type get_instance_length(const T *ptr)
0579    {  return priv_get_instance_length(block_header_t::block_header_from_value(ptr), sizeof(T));  }
0580 
0581    //!Returns is the the name of an object created with construct/find_or_construct
0582    //!functions. Does not throw
0583    template<class T>
0584    static instance_type get_instance_type(const T *ptr)
0585    {  return priv_get_instance_type(block_header_t::block_header_from_value(ptr));  }
0586 
0587    //!Preallocates needed index resources to optimize the
0588    //!creation of "num" named objects in the managed memory segment.
0589    //!Can throw boost::interprocess::bad_alloc if there is no enough memory.
0590    void reserve_named_objects(size_type num)
0591    {
0592       //-------------------------------
0593       scoped_lock<rmutex> guard(m_header);
0594       //-------------------------------
0595       m_header.m_named_index.reserve(num);
0596    }
0597 
0598    //!Preallocates needed index resources to optimize the
0599    //!creation of "num" unique objects in the managed memory segment.
0600    //!Can throw boost::interprocess::bad_alloc if there is no enough memory.
0601    void reserve_unique_objects(size_type num)
0602    {
0603       //-------------------------------
0604       scoped_lock<rmutex> guard(m_header);
0605       //-------------------------------
0606       m_header.m_unique_index.reserve(num);
0607    }
0608 
0609    //!Calls shrink_to_fit in both named and unique object indexes
0610    //!to try to free unused memory from those indexes.
0611    void shrink_to_fit_indexes()
0612    {
0613       //-------------------------------
0614       scoped_lock<rmutex> guard(m_header);
0615       //-------------------------------
0616       m_header.m_named_index.shrink_to_fit();
0617       m_header.m_unique_index.shrink_to_fit();
0618    }
0619 
0620    //!Returns the number of named objects stored in
0621    //!the segment.
0622    size_type get_num_named_objects()
0623    {
0624       //-------------------------------
0625       scoped_lock<rmutex> guard(m_header);
0626       //-------------------------------
0627       return m_header.m_named_index.size();
0628    }
0629 
0630    //!Returns the number of unique objects stored in
0631    //!the segment.
0632    size_type get_num_unique_objects()
0633    {
0634       //-------------------------------
0635       scoped_lock<rmutex> guard(m_header);
0636       //-------------------------------
0637       return m_header.m_unique_index.size();
0638    }
0639 
0640    //!Obtains the minimum size needed by the
0641    //!segment manager
0642    static size_type get_min_size()
0643    {  return segment_manager_base_t::get_min_size(priv_get_reserved_bytes());  }
0644 
0645    //!Returns a constant iterator to the beginning of the information about
0646    //!the named allocations performed in this segment manager
0647    const_named_iterator named_begin() const
0648    {
0649       return (make_transform_iterator)
0650          (m_header.m_named_index.begin(), named_transform());
0651    }
0652 
0653    //!Returns a constant iterator to the end of the information about
0654    //!the named allocations performed in this segment manager
0655    const_named_iterator named_end() const
0656    {
0657       return (make_transform_iterator)
0658          (m_header.m_named_index.end(), named_transform());
0659    }
0660 
0661    //!Returns a constant iterator to the beginning of the information about
0662    //!the unique allocations performed in this segment manager
0663    const_unique_iterator unique_begin() const
0664    {
0665       return (make_transform_iterator)
0666          (m_header.m_unique_index.begin(), unique_transform());
0667    }
0668 
0669    //!Returns a constant iterator to the end of the information about
0670    //!the unique allocations performed in this segment manager
0671    const_unique_iterator unique_end() const
0672    {
0673       return (make_transform_iterator)
0674          (m_header.m_unique_index.end(), unique_transform());
0675    }
0676 
0677    //!This is the default allocator to allocate types T
0678    //!from this managed segment
0679    template<class T>
0680    struct allocator
0681    {
0682       typedef boost::interprocess::allocator<T, segment_manager> type;
0683    };
0684 
0685    //!Returns an instance of the default allocator for type T
0686    //!initialized that allocates memory from this segment manager.
0687    template<class T>
0688    typename allocator<T>::type
0689       get_allocator()
0690    {   return typename allocator<T>::type(this); }
0691 
0692    //!This is the default deleter to delete types T
0693    //!from this managed segment.
0694    template<class T>
0695    struct deleter
0696    {
0697       typedef boost::interprocess::deleter<T, segment_manager> type;
0698    };
0699 
0700    //!Returns an instance of the default deleter for type T
0701    //!that will delete an object constructed in this segment manager.
0702    template<class T>
0703    typename deleter<T>::type
0704       get_deleter()
0705    {   return typename deleter<T>::type(this); }
0706 
0707    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0708 
0709    //!Generic named/anonymous new function. Offers all the possibilities,
0710    //!such as throwing, search before creating, and the constructor is
0711    //!encapsulated in an object function.
0712    template<class T>
0713    T *generic_construct(const CharType *name,
0714                         size_type num,
0715                          bool try2find,
0716                          bool dothrow,
0717                          ipcdetail::in_place_interface &table)
0718    {
0719       return static_cast<T*>
0720          (priv_generic_construct(name, num, try2find, dothrow, table));
0721    }
0722 
0723    private:
0724    //!Tries to find a previous named allocation. Returns the address
0725    //!and the object count. On failure the first member of the
0726    //!returned pair is 0.
0727    template <class T>
0728    std::pair<T*, size_type> priv_find_impl (const CharType* name, bool lock)
0729    {
0730       //The name can't be null, no anonymous object can be found by name
0731       BOOST_ASSERT(name != 0);
0732       ipcdetail::placement_destroy<T> table;
0733       size_type sz;
0734       void *ret;
0735 
0736       if(name == reinterpret_cast<const CharType*>(-1)){
0737          ret = priv_generic_find<char> (typeid(T).name(), m_header.m_unique_index, table, sz, is_intrusive_t(), lock);
0738       }
0739       else{
0740          ret = priv_generic_find<CharType> (name, m_header.m_named_index, table, sz, is_intrusive_t(), lock);
0741       }
0742       return std::pair<T*, size_type>(static_cast<T*>(ret), sz);
0743    }
0744 
0745    //!Tries to find a previous unique allocation. Returns the address
0746    //!and the object count. On failure the first member of the
0747    //!returned pair is 0.
0748    template <class T>
0749    std::pair<T*, size_type> priv_find_impl (const ipcdetail::unique_instance_t* name, bool lock)
0750    {
0751       ipcdetail::placement_destroy<T> table;
0752       size_type size;
0753       void *ret = priv_generic_find<char>(name, m_header.m_unique_index, table, size, is_intrusive_t(), lock);
0754       return std::pair<T*, size_type>(static_cast<T*>(ret), size);
0755    }
0756 
0757    void *priv_generic_construct
0758       (const CharType *name, size_type num, bool try2find, bool dothrow, ipcdetail::in_place_interface &table)
0759    {
0760       void *ret;
0761       //Security overflow check
0762       if(num > ((std::size_t)-1)/table.size){
0763          if(dothrow)
0764             throw bad_alloc();
0765          else
0766             return 0;
0767       }
0768       if(name == 0){
0769          ret = this->prot_anonymous_construct(num, dothrow, table);
0770       }
0771       else if(name == reinterpret_cast<const CharType*>(-1)){
0772          ret = this->priv_generic_named_construct<char>
0773             (unique_type, table.type_name, num, try2find, dothrow, table, m_header.m_unique_index, is_intrusive_t());
0774       }
0775       else{
0776          ret = this->priv_generic_named_construct<CharType>
0777             (named_type, name, num, try2find, dothrow, table, m_header.m_named_index, is_intrusive_t());
0778       }
0779       return ret;
0780    }
0781 
0782    void priv_destroy_ptr(const void *ptr, ipcdetail::in_place_interface &dtor)
0783    {
0784       block_header_t *ctrl_data = block_header_t::block_header_from_value(ptr, dtor.size, dtor.alignment);
0785       switch(ctrl_data->alloc_type()){
0786          case anonymous_type:
0787             this->prot_anonymous_destroy(ptr, dtor);
0788          break;
0789 
0790          case named_type:
0791             this->priv_generic_named_destroy<CharType>
0792                (ctrl_data, m_header.m_named_index, dtor, is_node_index_t());
0793          break;
0794 
0795          case unique_type:
0796             this->priv_generic_named_destroy<char>
0797                (ctrl_data, m_header.m_unique_index, dtor, is_node_index_t());
0798          break;
0799 
0800          default:
0801             //This type is unknown, bad pointer passed to this function!
0802             BOOST_ASSERT(0);
0803          break;
0804       }
0805    }
0806 
0807    //!Returns the name of an object created with construct/find_or_construct
0808    //!functions. Does not throw
0809    static const CharType *priv_get_instance_name(block_header_t *ctrl_data)
0810    {
0811       boost::interprocess::allocation_type type = ctrl_data->alloc_type();
0812       if(type == anonymous_type){
0813          BOOST_ASSERT((type == anonymous_type && ctrl_data->m_num_char == 0) ||
0814                 (type == unique_type    && ctrl_data->m_num_char != 0) );
0815          return 0;
0816       }
0817       CharType *name = static_cast<CharType*>(ctrl_data->template name<CharType>());
0818 
0819       //Sanity checks
0820       BOOST_ASSERT(ctrl_data->sizeof_char() == sizeof(CharType));
0821       BOOST_ASSERT(ctrl_data->m_num_char == std::char_traits<CharType>::length(name));
0822       return name;
0823    }
0824 
0825    static size_type priv_get_instance_length(block_header_t *ctrl_data, size_type sizeofvalue)
0826    {
0827       //Get header
0828       BOOST_ASSERT((ctrl_data->value_bytes() %sizeofvalue) == 0);
0829       return ctrl_data->value_bytes()/sizeofvalue;
0830    }
0831 
0832    //!Returns is the the name of an object created with construct/find_or_construct
0833    //!functions. Does not throw
0834    static instance_type priv_get_instance_type(block_header_t *ctrl_data)
0835    {
0836       //Get header
0837       BOOST_ASSERT((instance_type)ctrl_data->alloc_type() < max_allocation_type);
0838       return (instance_type)ctrl_data->alloc_type();
0839    }
0840 
0841    static size_type priv_get_reserved_bytes()
0842    {
0843       //Get the number of bytes until the end of (*this)
0844       //beginning in the end of the segment_manager_base_t base.
0845       return sizeof(segment_manager) - sizeof(segment_manager_base_t);
0846    }
0847 
0848    template <class CharT>
0849    void *priv_generic_find
0850       (const CharT* name,
0851        IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
0852        ipcdetail::in_place_interface &table,
0853        size_type &length, ipcdetail::true_ is_intrusive, bool use_lock)
0854    {
0855       (void)is_intrusive;
0856       typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >         index_type_t;
0857       typedef typename index_type_t::iterator           index_it;
0858 
0859       //-------------------------------
0860       scoped_lock<rmutex> guard(priv_get_lock(use_lock));
0861       //-------------------------------
0862       //Find name in index
0863       ipcdetail::intrusive_compare_key<CharT> key
0864          (name, std::char_traits<CharT>::length(name));
0865       index_it it = index.find(key);
0866 
0867       //Initialize return values
0868       void *ret_ptr  = 0;
0869       length         = 0;
0870 
0871       //If found, assign values
0872       if(it != index.end()){
0873          //Get header
0874          block_header_t *ctrl_data = it->get_block_header();
0875 
0876          //Sanity check
0877          BOOST_ASSERT((ctrl_data->m_value_bytes % table.size) == 0);
0878          BOOST_ASSERT(ctrl_data->sizeof_char() == sizeof(CharT));
0879          ret_ptr  = ctrl_data->value();
0880          length  = ctrl_data->m_value_bytes/table.size;
0881       }
0882       return ret_ptr;
0883    }
0884 
0885    template <class CharT>
0886    void *priv_generic_find
0887       (const CharT* name,
0888        IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
0889        ipcdetail::in_place_interface &table,
0890        size_type &length, ipcdetail::false_ is_intrusive, bool use_lock)
0891    {
0892       (void)is_intrusive;
0893       typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >      char_aware_index_type;
0894       typedef typename char_aware_index_type::key_type        key_type;
0895       typedef typename char_aware_index_type::iterator        index_it;
0896 
0897       //-------------------------------
0898       scoped_lock<rmutex> guard(priv_get_lock(use_lock));
0899       //-------------------------------
0900       //Find name in index
0901       index_it it = index.find(key_type(name, std::char_traits<CharT>::length(name)));
0902 
0903       //Initialize return values
0904       void *ret_ptr  = 0;
0905       length         = 0;
0906 
0907       //If found, assign values
0908       if(it != index.end()){
0909          //Get header
0910          block_header_t *ctrl_data = reinterpret_cast<block_header_t*>
0911                                     (ipcdetail::to_raw_pointer(it->second.m_ptr));
0912 
0913          //Sanity check
0914          BOOST_ASSERT((ctrl_data->m_value_bytes % table.size) == 0);
0915          BOOST_ASSERT(ctrl_data->sizeof_char() == sizeof(CharT));
0916          ret_ptr  = ctrl_data->value();
0917          length  = ctrl_data->m_value_bytes/table.size;
0918       }
0919       return ret_ptr;
0920    }
0921 
0922    template <class CharT>
0923    bool priv_generic_named_destroy
0924      (block_header_t *block_header,
0925       IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
0926       ipcdetail::in_place_interface &table, ipcdetail::true_ is_node_index)
0927    {
0928       (void)is_node_index;
0929       typedef typename IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >::iterator index_it;
0930 
0931       index_it *ihdr = block_header_t::template to_first_header<index_it>(block_header);
0932       return this->priv_generic_named_destroy_impl<CharT>(*ihdr, index, table);
0933    }
0934 
0935    template <class CharT>
0936    bool priv_generic_named_destroy
0937      (block_header_t *block_header,
0938       IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
0939       ipcdetail::in_place_interface &table,
0940       ipcdetail::false_ is_node_index)
0941    {
0942       (void)is_node_index;
0943       CharT *name = static_cast<CharT*>(block_header->template name<CharT>());
0944       return this->priv_generic_named_destroy<CharT>(name, index, table, is_intrusive_t());
0945    }
0946 
0947    template <class CharT>
0948    bool priv_generic_named_destroy(const CharT *name,
0949                                    IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
0950                                    ipcdetail::in_place_interface &table, ipcdetail::true_ is_intrusive_index)
0951    {
0952       (void)is_intrusive_index;
0953       typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >         index_type_t;
0954       typedef typename index_type_t::iterator           index_it;
0955       typedef typename index_type_t::value_type         intrusive_value_type;
0956 
0957       //-------------------------------
0958       scoped_lock<rmutex> guard(m_header);
0959       //-------------------------------
0960       //Find name in index
0961       ipcdetail::intrusive_compare_key<CharT> key
0962          (name, std::char_traits<CharT>::length(name));
0963       index_it it = index.find(key);
0964 
0965       //If not found, return false
0966       if(it == index.end()){
0967          //This name is not present in the index, wrong pointer or name!
0968          //BOOST_ASSERT(0);
0969          return false;
0970       }
0971 
0972       block_header_t *ctrl_data = it->get_block_header();
0973       intrusive_value_type *iv = intrusive_value_type::get_intrusive_value_type(ctrl_data);
0974       void *memory = iv;
0975       void *values = ctrl_data->value();
0976       std::size_t num = ctrl_data->m_value_bytes/table.size;
0977 
0978       //Sanity check
0979       BOOST_ASSERT((ctrl_data->m_value_bytes % table.size) == 0);
0980       BOOST_ASSERT(sizeof(CharT) == ctrl_data->sizeof_char());
0981 
0982       //Erase node from index
0983       index.erase(it);
0984 
0985       //Destroy the headers
0986       ctrl_data->~block_header_t();
0987       iv->~intrusive_value_type();
0988 
0989       //Call destructors and free memory
0990       table.destroy_n(values, num);
0991       this->deallocate(memory);
0992       return true;
0993    }
0994 
0995    template <class CharT>
0996    bool priv_generic_named_destroy(const CharT *name,
0997                                    IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
0998                                    ipcdetail::in_place_interface &table,
0999                                    ipcdetail::false_ is_intrusive_index)
1000    {
1001       (void)is_intrusive_index;
1002       typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >            char_aware_index_type;
1003       typedef typename char_aware_index_type::iterator              index_it;
1004       typedef typename char_aware_index_type::key_type              key_type;
1005 
1006       //-------------------------------
1007       scoped_lock<rmutex> guard(m_header);
1008       //-------------------------------
1009       //Try to find the name in the index
1010       index_it it = index.find(key_type (name,
1011                                      std::char_traits<CharT>::length(name)));
1012 
1013       //If not found, return false
1014       if(it == index.end()){
1015          //This name is not present in the index, wrong pointer or name!
1016          //BOOST_ASSERT(0);
1017          return false;
1018       }
1019       return this->priv_generic_named_destroy_impl<CharT>(it, index, table);
1020    }
1021 
1022    template <class CharT>
1023    bool priv_generic_named_destroy_impl
1024       (const typename IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >::iterator &it,
1025       IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
1026       ipcdetail::in_place_interface &table)
1027    {
1028       typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >      char_aware_index_type;
1029       typedef typename char_aware_index_type::iterator        index_it;
1030 
1031       //Get allocation parameters
1032       block_header_t *ctrl_data = reinterpret_cast<block_header_t*>
1033                                  (ipcdetail::to_raw_pointer(it->second.m_ptr));
1034       char *stored_name       = static_cast<char*>(static_cast<void*>(const_cast<CharT*>(it->first.name())));
1035       (void)stored_name;
1036 
1037       //Check if the distance between the name pointer and the memory pointer
1038       //is correct (this can detect incorrect type in destruction)
1039       std::size_t num = ctrl_data->m_value_bytes/table.size;
1040       void *values = ctrl_data->value();
1041 
1042       //Sanity check
1043       BOOST_ASSERT((ctrl_data->m_value_bytes % table.size) == 0);
1044       BOOST_ASSERT(static_cast<void*>(stored_name) == static_cast<void*>(ctrl_data->template name<CharT>()));
1045       BOOST_ASSERT(sizeof(CharT) == ctrl_data->sizeof_char());
1046 
1047       //Erase node from index
1048       index.erase(it);
1049 
1050       //Destroy the header
1051       ctrl_data->~block_header_t();
1052 
1053       void *memory;
1054       if(is_node_index_t::value){
1055          index_it *ihdr = block_header_t::template
1056             to_first_header<index_it>(ctrl_data);
1057          ihdr->~index_it();
1058          memory = ihdr;
1059       }
1060       else{
1061          memory = ctrl_data;
1062       }
1063 
1064       //Call destructors and free memory
1065       table.destroy_n(values, num);
1066       this->deallocate(memory);
1067       return true;
1068    }
1069 
1070    template<class CharT>
1071    void * priv_generic_named_construct
1072       (unsigned char type, const CharT *name, size_type num, bool try2find,
1073       bool dothrow, ipcdetail::in_place_interface &table, 
1074       IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index, ipcdetail::true_ is_intrusive)
1075    {
1076       (void)is_intrusive;
1077      std::size_t namelen  = std::char_traits<CharT>::length(name);
1078 
1079       block_header_t block_info ( size_type(table.size*num)
1080                                  , size_type(table.alignment)
1081                                  , type
1082                                  , sizeof(CharT)
1083                                  , namelen);
1084 
1085       typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >            index_type_t;
1086       typedef typename index_type_t::iterator              index_it;
1087       typedef std::pair<index_it, bool>                  index_ib;
1088 
1089       //-------------------------------
1090       scoped_lock<rmutex> guard(m_header);
1091       //-------------------------------
1092       //Insert the node. This can throw.
1093       //First, we want to know if the key is already present before
1094       //we allocate any memory, and if the key is not present, we
1095       //want to allocate all memory in a single buffer that will
1096       //contain the name and the user buffer.
1097       //
1098       //Since equal_range(key) + insert(hint, value) approach is
1099       //quite inefficient in container implementations
1100       //(they re-test if the position is correct), I've chosen
1101       //to insert the node, do an ugly un-const cast and modify
1102       //the key (which is a smart pointer) to an equivalent one
1103       index_ib insert_ret;
1104 
1105       typename index_type_t::insert_commit_data   commit_data;
1106       typedef typename index_type_t::value_type   intrusive_value_type;
1107 
1108       BOOST_TRY{
1109          ipcdetail::intrusive_compare_key<CharT> key(name, namelen);
1110          insert_ret = index.insert_check(key, commit_data);
1111       }
1112       //Ignore exceptions
1113       BOOST_CATCH(...){
1114          if(dothrow)
1115             BOOST_RETHROW
1116          return 0;
1117       }
1118       BOOST_CATCH_END
1119 
1120       index_it it = insert_ret.first;
1121 
1122       //If found and this is find or construct, return data
1123       //else return null
1124       if(!insert_ret.second){
1125          if(try2find){
1126             return it->get_block_header()->value();
1127          }
1128          if(dothrow){
1129             throw interprocess_exception(already_exists_error);
1130          }
1131          else{
1132             return 0;
1133          }
1134       }
1135 
1136       //Allocates buffer for name + data, this can throw (it hurts)
1137       void *buffer_ptr;
1138 
1139       //Check if there is enough memory
1140       if(dothrow){
1141          buffer_ptr = this->allocate
1142             (block_info.template total_size_with_header<intrusive_value_type>());
1143       }
1144       else{
1145          buffer_ptr = this->allocate
1146             (block_info.template total_size_with_header<intrusive_value_type>(), nothrow<>::get());
1147          if(!buffer_ptr)
1148             return 0;
1149       }
1150 
1151       //Now construct the intrusive hook plus the header
1152       intrusive_value_type * intrusive_hdr = ::new(buffer_ptr, boost_container_new_t()) intrusive_value_type();
1153       block_header_t * hdr = ::new(intrusive_hdr->get_block_header(), boost_container_new_t())block_header_t(block_info);
1154       void *ptr = 0; //avoid gcc warning
1155       ptr = hdr->value();
1156 
1157       //Copy name to memory segment and insert data
1158       CharT *name_ptr = static_cast<CharT *>(hdr->template name<CharT>());
1159       std::char_traits<CharT>::copy(name_ptr, name, namelen+1);
1160 
1161       BOOST_TRY{
1162          //Now commit the insertion using previous context data
1163          it = index.insert_commit(*intrusive_hdr, commit_data);
1164       }
1165       //Ignore exceptions
1166       BOOST_CATCH(...){
1167          if(dothrow)
1168             BOOST_RETHROW
1169          return 0;
1170       }
1171       BOOST_CATCH_END
1172 
1173       //Avoid constructions if constructor is trivial
1174       //Build scoped ptr to avoid leaks with constructor exception
1175       ipcdetail::mem_algo_deallocator<segment_manager_base_type> mem
1176          (buffer_ptr, *static_cast<segment_manager_base_type*>(this));
1177 
1178       //Initialize the node value_eraser to erase inserted node
1179       //if something goes wrong. This will be executed *before*
1180       //the memory allocation as the intrusive value is built in that
1181       //memory
1182       value_eraser<index_type_t> v_eraser(index, it);
1183 
1184       //Construct array, this can throw
1185       table.construct_n(ptr, num);
1186 
1187       //Release rollbacks since construction was successful
1188       v_eraser.release();
1189       mem.release();
1190       return ptr;
1191    }
1192 
1193    //!Generic named new function for
1194    //!named functions
1195    template<class CharT>
1196    void * priv_generic_named_construct
1197       (unsigned char type, const CharT *name, size_type num, bool try2find, bool dothrow,
1198       ipcdetail::in_place_interface &table, 
1199       IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index, ipcdetail::false_ is_intrusive)
1200    {
1201       (void)is_intrusive;
1202       std::size_t namelen  = std::char_traits<CharT>::length(name);
1203 
1204       block_header_t block_info ( size_type(table.size*num)
1205                                  , size_type(table.alignment)
1206                                  , type
1207                                  , sizeof(CharT)
1208                                  , namelen);
1209 
1210       typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >            index_type_t;
1211       typedef typename index_type_t::key_type              key_type;
1212       typedef typename index_type_t::mapped_type           mapped_type;
1213       typedef typename index_type_t::value_type            value_type;
1214       typedef typename index_type_t::iterator              index_it;
1215       typedef std::pair<index_it, bool>                  index_ib;
1216 
1217       //-------------------------------
1218       scoped_lock<rmutex> guard(m_header);
1219       //-------------------------------
1220       //Insert the node. This can throw.
1221       //First, we want to know if the key is already present before
1222       //we allocate any memory, and if the key is not present, we
1223       //want to allocate all memory in a single buffer that will
1224       //contain the name and the user buffer.
1225       //
1226       //Since equal_range(key) + insert(hint, value) approach is
1227       //quite inefficient in container implementations
1228       //(they re-test if the position is correct), I've chosen
1229       //to insert the node, do an ugly un-const cast and modify
1230       //the key (which is a smart pointer) to an equivalent one
1231       index_ib insert_ret;
1232       BOOST_TRY{
1233          insert_ret = index.insert(value_type(key_type (name, namelen), mapped_type(0)));
1234       }
1235       //Ignore exceptions
1236       BOOST_CATCH(...){
1237          if(dothrow)
1238             BOOST_RETHROW;
1239          return 0;
1240       }
1241       BOOST_CATCH_END
1242 
1243       index_it it = insert_ret.first;
1244 
1245       //If found and this is find or construct, return data
1246       //else return null
1247       if(!insert_ret.second){
1248          if(try2find){
1249             block_header_t *hdr = static_cast<block_header_t*>
1250                (ipcdetail::to_raw_pointer(it->second.m_ptr));
1251             return hdr->value();
1252          }
1253          return 0;
1254       }
1255       //Initialize the node value_eraser to erase inserted node
1256       //if something goes wrong
1257       value_eraser<index_type_t> v_eraser(index, it);
1258 
1259       //Allocates buffer for name + data, this can throw (it hurts)
1260       void *buffer_ptr;
1261       block_header_t * hdr;
1262 
1263       //Allocate and construct the headers
1264       if(is_node_index_t::value){
1265          size_type total_size = block_info.template total_size_with_header<index_it>();
1266          if(dothrow){
1267             buffer_ptr = this->allocate(total_size);
1268          }
1269          else{
1270             buffer_ptr = this->allocate(total_size, nothrow<>::get());
1271             if(!buffer_ptr)
1272                return 0;
1273          }
1274          index_it *idr = ::new(buffer_ptr, boost_container_new_t()) index_it(it);
1275          hdr = block_header_t::template from_first_header<index_it>(idr);
1276       }
1277       else{
1278          if(dothrow){
1279             buffer_ptr = this->allocate(block_info.total_size());
1280          }
1281          else{
1282             buffer_ptr = this->allocate(block_info.total_size(), nothrow<>::get());
1283             if(!buffer_ptr)
1284                return 0;
1285          }
1286          hdr = static_cast<block_header_t*>(buffer_ptr);
1287       }
1288 
1289       hdr = ::new(hdr, boost_container_new_t())block_header_t(block_info);
1290       void *ptr = 0; //avoid gcc warning
1291       ptr = hdr->value();
1292 
1293       //Copy name to memory segment and insert data
1294       CharT *name_ptr = static_cast<CharT *>(hdr->template name<CharT>());
1295       std::char_traits<CharT>::copy(name_ptr, name, namelen+1);
1296 
1297       //Do the ugly cast, please mama, forgive me!
1298       //This new key points to an identical string, so it must have the
1299       //same position than the overwritten key according to the predicate
1300       const_cast<key_type &>(it->first).name(name_ptr);
1301       it->second.m_ptr  = hdr;
1302 
1303       //Build scoped ptr to avoid leaks with constructor exception
1304       ipcdetail::mem_algo_deallocator<segment_manager_base_type> mem
1305          (buffer_ptr, *static_cast<segment_manager_base_type*>(this));
1306 
1307       //Construct array, this can throw
1308       table.construct_n(ptr, num);
1309 
1310       //All constructors successful, we don't want to release memory
1311       mem.release();
1312 
1313       //Release node v_eraser since construction was successful
1314       v_eraser.release();
1315       return ptr;
1316    }
1317 
1318    private:
1319    //!Returns the this pointer
1320    segment_manager *get_this_pointer()
1321    {  return this;  }
1322 
1323    typedef typename MemoryAlgorithm::mutex_family::recursive_mutex_type   rmutex;
1324 
1325    scoped_lock<rmutex> priv_get_lock(bool use_lock)
1326    {
1327       scoped_lock<rmutex> local(m_header, defer_lock);
1328       if(use_lock){
1329          local.lock();
1330       }
1331       return scoped_lock<rmutex>(boost::move(local));
1332    }
1333 
1334    //!This struct includes needed data and derives from
1335    //!rmutex to allow EBO when using null interprocess_mutex
1336    struct header_t
1337       :  public rmutex
1338    {
1339       named_index_t           m_named_index;
1340       unique_index_t          m_unique_index;
1341 
1342       header_t(segment_manager_base_t *segment_mngr_base)
1343          :  m_named_index (segment_mngr_base)
1344          ,  m_unique_index(segment_mngr_base)
1345       {}
1346    }  m_header;
1347 
1348    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
1349 };
1350 
1351 
1352 }} //namespace boost { namespace interprocess
1353 
1354 #include <boost/interprocess/detail/config_end.hpp>
1355 
1356 #endif //#ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_HPP
1357