Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 08:47:01

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_MANAGED_HEAP_MEMORY_HPP
0012 #define BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_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 #include <boost/interprocess/creation_tags.hpp>
0025 #include <boost/move/utility_core.hpp>
0026 #include <vector>
0027 #include <boost/interprocess/detail/managed_memory_impl.hpp>
0028 //These includes needed to fulfill default template parameters of
0029 //predeclarations in interprocess_fwd.hpp
0030 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
0031 #include <boost/interprocess/sync/mutex_family.hpp>
0032 #include <boost/interprocess/indexes/iset_index.hpp>
0033 
0034 //!\file
0035 //!Describes a named heap memory allocation user class.
0036 
0037 namespace boost {
0038 namespace interprocess {
0039 
0040 //!A basic heap memory named object creation class. Initializes the
0041 //!heap memory segment. Inherits all basic functionality from
0042 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
0043 template
0044       <
0045          class CharType,
0046          class AllocationAlgorithm,
0047          template<class IndexConfig> class IndexType
0048       >
0049 class basic_managed_heap_memory
0050    : public ipcdetail::basic_managed_memory_impl <CharType, AllocationAlgorithm, IndexType>
0051 {
0052    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0053    private:
0054 
0055    typedef ipcdetail::basic_managed_memory_impl
0056       <CharType, AllocationAlgorithm, IndexType>             base_t;
0057    BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_heap_memory)
0058    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0059 
0060    public: //functions
0061    typedef typename base_t::size_type              size_type;
0062 
0063    //!Default constructor. Does nothing.
0064    //!Useful in combination with move semantics
0065    basic_managed_heap_memory() BOOST_NOEXCEPT
0066    {}
0067 
0068    //!Destructor. Liberates the heap memory holding the managed data.
0069    //!Never throws.
0070    ~basic_managed_heap_memory()
0071    {  this->priv_close();  }
0072 
0073    //!Creates heap memory and initializes the segment manager.
0074    //!This can throw.
0075    basic_managed_heap_memory(size_type size)
0076       :  m_heapmem(size, char(0))
0077    {
0078       if(!base_t::create_impl(&m_heapmem[0], size)){
0079          this->priv_close();
0080          throw interprocess_exception("Could not initialize heap in basic_managed_heap_memory constructor");
0081       }
0082    }
0083 
0084    //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
0085    basic_managed_heap_memory(BOOST_RV_REF(basic_managed_heap_memory) moved) BOOST_NOEXCEPT
0086    {  this->swap(moved);   }
0087 
0088    //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
0089    basic_managed_heap_memory &operator=(BOOST_RV_REF(basic_managed_heap_memory) moved) BOOST_NOEXCEPT
0090    {
0091       basic_managed_heap_memory tmp(boost::move(moved));
0092       this->swap(tmp);
0093       return *this;
0094    }
0095 
0096    //!Tries to resize internal heap memory so that
0097    //!we have room for more objects.
0098    //!WARNING: If memory is reallocated, all the objects will
0099    //!be binary-copied to the new buffer. To be able to use
0100    //!this function, all pointers constructed in this buffer
0101    //!must be offset pointers. Otherwise, the result is undefined.
0102    //!Returns true if the growth has been successful, so you will
0103    //!have some extra bytes to allocate new objects. If returns
0104    //!false, the heap allocation has failed.
0105    bool grow(size_type extra_bytes)
0106    {
0107       //If memory is reallocated, data will
0108       //be automatically copied
0109       BOOST_INTERPROCESS_TRY{
0110         m_heapmem.resize(m_heapmem.size()+extra_bytes);
0111       }
0112       BOOST_INTERPROCESS_CATCH(...){
0113          return false;
0114       }
0115       BOOST_INTERPROCESS_CATCH_END
0116 
0117       //Grow always works
0118       base_t::close_impl();
0119       base_t::open_impl(&m_heapmem[0], m_heapmem.size());
0120       base_t::grow(extra_bytes);
0121       return true;
0122    }
0123 
0124    //!Swaps the ownership of the managed heap memories managed by *this and other.
0125    //!Never throws.
0126    void swap(basic_managed_heap_memory &other) BOOST_NOEXCEPT
0127    {
0128       base_t::swap(other);
0129       m_heapmem.swap(other.m_heapmem);
0130    }
0131 
0132    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0133    private:
0134    //!Frees resources. Never throws.
0135    void priv_close()
0136    {
0137       base_t::destroy_impl();
0138       std::vector<char>().swap(m_heapmem);
0139    }
0140 
0141    std::vector<char>  m_heapmem;
0142    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0143 };
0144 
0145 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0146 
0147 //!Typedef for a default basic_managed_heap_memory
0148 //!of narrow characters
0149 typedef basic_managed_heap_memory
0150    <char
0151    ,rbtree_best_fit<null_mutex_family>
0152    ,iset_index>
0153 managed_heap_memory;
0154 
0155 //!Typedef for a default basic_managed_heap_memory
0156 //!of wide characters
0157 typedef basic_managed_heap_memory
0158    <wchar_t
0159    ,rbtree_best_fit<null_mutex_family>
0160    ,iset_index>
0161 wmanaged_heap_memory;
0162 
0163 #endif   //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0164 
0165 }  //namespace interprocess {
0166 }  //namespace boost {
0167 
0168 #include <boost/interprocess/detail/config_end.hpp>
0169 
0170 #endif   //BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP
0171