Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/interprocess for documentation. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP Chris@16: #define BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: # Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: //These includes needed to fulfill default template parameters of Chris@16: //predeclarations in interprocess_fwd.hpp Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: //!\file Chris@16: //!Describes a named heap memory allocation user class. Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@16: //!A basic heap memory named object creation class. Initializes the Chris@16: //!heap memory segment. Inherits all basic functionality from Chris@16: //!basic_managed_memory_impl*/ Chris@16: template Chris@16: < Chris@16: class CharType, Chris@16: class AllocationAlgorithm, Chris@16: template class IndexType Chris@16: > Chris@16: class basic_managed_heap_memory Chris@16: : public ipcdetail::basic_managed_memory_impl Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: Chris@16: typedef ipcdetail::basic_managed_memory_impl Chris@16: base_t; Chris@16: BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_heap_memory) Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: public: //functions Chris@16: typedef typename base_t::size_type size_type; Chris@16: Chris@16: //!Default constructor. Does nothing. Chris@16: //!Useful in combination with move semantics Chris@16: basic_managed_heap_memory(){} Chris@16: Chris@16: //!Destructor. Liberates the heap memory holding the managed data. Chris@16: //!Never throws. Chris@16: ~basic_managed_heap_memory() Chris@16: { this->priv_close(); } Chris@16: Chris@16: //!Creates heap memory and initializes the segment manager. Chris@16: //!This can throw. Chris@16: basic_managed_heap_memory(size_type size) Chris@16: : m_heapmem(size, char(0)) Chris@16: { Chris@16: if(!base_t::create_impl(&m_heapmem[0], size)){ Chris@16: this->priv_close(); Chris@16: throw interprocess_exception("Could not initialize heap in basic_managed_heap_memory constructor"); Chris@16: } Chris@16: } Chris@16: Chris@16: //!Moves the ownership of "moved"'s managed memory to *this. Does not throw Chris@16: basic_managed_heap_memory(BOOST_RV_REF(basic_managed_heap_memory) moved) Chris@16: { this->swap(moved); } Chris@16: Chris@16: //!Moves the ownership of "moved"'s managed memory to *this. Does not throw Chris@16: basic_managed_heap_memory &operator=(BOOST_RV_REF(basic_managed_heap_memory) moved) Chris@16: { Chris@16: basic_managed_heap_memory tmp(boost::move(moved)); Chris@16: this->swap(tmp); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //!Tries to resize internal heap memory so that Chris@16: //!we have room for more objects. Chris@16: //!WARNING: If memory is reallocated, all the objects will Chris@16: //!be binary-copied to the new buffer. To be able to use Chris@16: //!this function, all pointers constructed in this buffer Chris@16: //!must be offset pointers. Otherwise, the result is undefined. Chris@16: //!Returns true if the growth has been successful, so you will Chris@16: //!have some extra bytes to allocate new objects. If returns Chris@16: //!false, the heap allocation has failed. Chris@16: bool grow(size_type extra_bytes) Chris@16: { Chris@16: //If memory is reallocated, data will Chris@16: //be automatically copied Chris@16: BOOST_TRY{ Chris@16: m_heapmem.resize(m_heapmem.size()+extra_bytes); Chris@16: } Chris@16: BOOST_CATCH(...){ Chris@16: return false; Chris@16: } Chris@16: BOOST_CATCH_END Chris@16: Chris@16: //Grow always works Chris@16: base_t::close_impl(); Chris@16: base_t::open_impl(&m_heapmem[0], m_heapmem.size()); Chris@16: base_t::grow(extra_bytes); Chris@16: return true; Chris@16: } Chris@16: Chris@16: //!Swaps the ownership of the managed heap memories managed by *this and other. Chris@16: //!Never throws. Chris@16: void swap(basic_managed_heap_memory &other) Chris@16: { Chris@16: base_t::swap(other); Chris@16: m_heapmem.swap(other.m_heapmem); Chris@16: } Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: //!Frees resources. Never throws. Chris@16: void priv_close() Chris@16: { Chris@16: base_t::destroy_impl(); Chris@16: std::vector().swap(m_heapmem); Chris@16: } Chris@16: Chris@16: std::vector m_heapmem; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: }; Chris@16: Chris@101: #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@101: Chris@101: //!Typedef for a default basic_managed_heap_memory Chris@101: //!of narrow characters Chris@101: typedef basic_managed_heap_memory Chris@101: Chris@101: ,iset_index> Chris@101: managed_heap_memory; Chris@101: Chris@101: //!Typedef for a default basic_managed_heap_memory Chris@101: //!of wide characters Chris@101: typedef basic_managed_heap_memory Chris@101: Chris@101: ,iset_index> Chris@101: wmanaged_heap_memory; Chris@101: Chris@101: #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP Chris@16: