1 ////////////////////////////////////////////////////////////////////////////// 2 // 3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost 4 // Software License, Version 1.0. (See accompanying file 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 // 7 // See http://www.boost.org/libs/interprocess for documentation. 8 // 9 ////////////////////////////////////////////////////////////////////////////// 10 11 #ifndef BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP 12 #define BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP 13 14 #ifndef BOOST_CONFIG_HPP 15 # include <boost/config.hpp> 16 #endif 17 # 18 #if defined(BOOST_HAS_PRAGMA_ONCE) 19 # pragma once 20 #endif 21 22 #include <boost/interprocess/detail/config_begin.hpp> 23 #include <boost/interprocess/detail/workaround.hpp> 24 #include <boost/interprocess/creation_tags.hpp> 25 #include <boost/move/utility_core.hpp> 26 #include <vector> 27 #include <boost/interprocess/detail/managed_memory_impl.hpp> 28 #include <boost/core/no_exceptions_support.hpp> 29 //These includes needed to fulfill default template parameters of 30 //predeclarations in interprocess_fwd.hpp 31 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp> 32 #include <boost/interprocess/sync/mutex_family.hpp> 33 #include <boost/interprocess/indexes/iset_index.hpp> 34 35 //!\file 36 //!Describes a named heap memory allocation user class. 37 38 namespace boost { 39 namespace interprocess { 40 41 //!A basic heap memory named object creation class. Initializes the 42 //!heap memory segment. Inherits all basic functionality from 43 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/ 44 template 45 < 46 class CharType, 47 class AllocationAlgorithm, 48 template<class IndexConfig> class IndexType 49 > 50 class basic_managed_heap_memory 51 : public ipcdetail::basic_managed_memory_impl <CharType, AllocationAlgorithm, IndexType> 52 { 53 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 54 private: 55 56 typedef ipcdetail::basic_managed_memory_impl 57 <CharType, AllocationAlgorithm, IndexType> base_t; 58 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_heap_memory) 59 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 60 61 public: //functions 62 typedef typename base_t::size_type size_type; 63 64 //!Default constructor. Does nothing. 65 //!Useful in combination with move semantics basic_managed_heap_memory()66 basic_managed_heap_memory(){} 67 68 //!Destructor. Liberates the heap memory holding the managed data. 69 //!Never throws. ~basic_managed_heap_memory()70 ~basic_managed_heap_memory() 71 { this->priv_close(); } 72 73 //!Creates heap memory and initializes the segment manager. 74 //!This can throw. basic_managed_heap_memory(size_type size)75 basic_managed_heap_memory(size_type size) 76 : m_heapmem(size, char(0)) 77 { 78 if(!base_t::create_impl(&m_heapmem[0], size)){ 79 this->priv_close(); 80 throw interprocess_exception("Could not initialize heap in basic_managed_heap_memory constructor"); 81 } 82 } 83 84 //!Moves the ownership of "moved"'s managed memory to *this. Does not throw basic_managed_heap_memory(BOOST_RV_REF (basic_managed_heap_memory)moved)85 basic_managed_heap_memory(BOOST_RV_REF(basic_managed_heap_memory) moved) 86 { this->swap(moved); } 87 88 //!Moves the ownership of "moved"'s managed memory to *this. Does not throw operator =(BOOST_RV_REF (basic_managed_heap_memory)moved)89 basic_managed_heap_memory &operator=(BOOST_RV_REF(basic_managed_heap_memory) moved) 90 { 91 basic_managed_heap_memory tmp(boost::move(moved)); 92 this->swap(tmp); 93 return *this; 94 } 95 96 //!Tries to resize internal heap memory so that 97 //!we have room for more objects. 98 //!WARNING: If memory is reallocated, all the objects will 99 //!be binary-copied to the new buffer. To be able to use 100 //!this function, all pointers constructed in this buffer 101 //!must be offset pointers. Otherwise, the result is undefined. 102 //!Returns true if the growth has been successful, so you will 103 //!have some extra bytes to allocate new objects. If returns 104 //!false, the heap allocation has failed. grow(size_type extra_bytes)105 bool grow(size_type extra_bytes) 106 { 107 //If memory is reallocated, data will 108 //be automatically copied 109 BOOST_TRY{ 110 m_heapmem.resize(m_heapmem.size()+extra_bytes); 111 } 112 BOOST_CATCH(...){ 113 return false; 114 } 115 BOOST_CATCH_END 116 117 //Grow always works 118 base_t::close_impl(); 119 base_t::open_impl(&m_heapmem[0], m_heapmem.size()); 120 base_t::grow(extra_bytes); 121 return true; 122 } 123 124 //!Swaps the ownership of the managed heap memories managed by *this and other. 125 //!Never throws. swap(basic_managed_heap_memory & other)126 void swap(basic_managed_heap_memory &other) 127 { 128 base_t::swap(other); 129 m_heapmem.swap(other.m_heapmem); 130 } 131 132 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 133 private: 134 //!Frees resources. Never throws. priv_close()135 void priv_close() 136 { 137 base_t::destroy_impl(); 138 std::vector<char>().swap(m_heapmem); 139 } 140 141 std::vector<char> m_heapmem; 142 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 143 }; 144 145 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED 146 147 //!Typedef for a default basic_managed_heap_memory 148 //!of narrow characters 149 typedef basic_managed_heap_memory 150 <char 151 ,rbtree_best_fit<null_mutex_family> 152 ,iset_index> 153 managed_heap_memory; 154 155 //!Typedef for a default basic_managed_heap_memory 156 //!of wide characters 157 typedef basic_managed_heap_memory 158 <wchar_t 159 ,rbtree_best_fit<null_mutex_family> 160 ,iset_index> 161 wmanaged_heap_memory; 162 163 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED 164 165 } //namespace interprocess { 166 } //namespace boost { 167 168 #include <boost/interprocess/detail/config_end.hpp> 169 170 #endif //BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP 171 172