Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:34

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