1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP
12 #define BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20 #include <boost/interprocess/creation_tags.hpp>
21 #include <boost/move/utility_core.hpp>
23 #include <boost/interprocess/detail/managed_memory_impl.hpp>
24 #include <boost/detail/no_exceptions_support.hpp>
25 //These includes needed to fulfill default template parameters of
26 //predeclarations in interprocess_fwd.hpp
27 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
28 #include <boost/interprocess/sync/mutex_family.hpp>
29 #include <boost/interprocess/indexes/iset_index.hpp>
32 //!Describes a named heap memory allocation user class.
35 namespace interprocess {
37 //!A basic heap memory named object creation class. Initializes the
38 //!heap memory segment. Inherits all basic functionality from
39 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
43 class AllocationAlgorithm,
44 template<class IndexConfig> class IndexType
46 class basic_managed_heap_memory
47 : public ipcdetail::basic_managed_memory_impl <CharType, AllocationAlgorithm, IndexType>
49 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
52 typedef ipcdetail::basic_managed_memory_impl
53 <CharType, AllocationAlgorithm, IndexType> base_t;
54 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_heap_memory)
55 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
58 typedef typename base_t::size_type size_type;
60 //!Default constructor. Does nothing.
61 //!Useful in combination with move semantics
62 basic_managed_heap_memory(){}
64 //!Destructor. Liberates the heap memory holding the managed data.
66 ~basic_managed_heap_memory()
67 { this->priv_close(); }
69 //!Creates heap memory and initializes the segment manager.
71 basic_managed_heap_memory(size_type size)
72 : m_heapmem(size, char(0))
74 if(!base_t::create_impl(&m_heapmem[0], size)){
76 throw interprocess_exception("Could not initialize heap in basic_managed_heap_memory constructor");
80 //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
81 basic_managed_heap_memory(BOOST_RV_REF(basic_managed_heap_memory) moved)
82 { this->swap(moved); }
84 //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
85 basic_managed_heap_memory &operator=(BOOST_RV_REF(basic_managed_heap_memory) moved)
87 basic_managed_heap_memory tmp(boost::move(moved));
92 //!Tries to resize internal heap memory so that
93 //!we have room for more objects.
94 //!WARNING: If memory is reallocated, all the objects will
95 //!be binary-copied to the new buffer. To be able to use
96 //!this function, all pointers constructed in this buffer
97 //!must be offset pointers. Otherwise, the result is undefined.
98 //!Returns true if the growth has been successful, so you will
99 //!have some extra bytes to allocate new objects. If returns
100 //!false, the heap allocation has failed.
101 bool grow(size_type extra_bytes)
103 //If memory is reallocated, data will
104 //be automatically copied
106 m_heapmem.resize(m_heapmem.size()+extra_bytes);
114 base_t::close_impl();
115 base_t::open_impl(&m_heapmem[0], m_heapmem.size());
116 base_t::grow(extra_bytes);
120 //!Swaps the ownership of the managed heap memories managed by *this and other.
122 void swap(basic_managed_heap_memory &other)
125 m_heapmem.swap(other.m_heapmem);
128 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
130 //!Frees resources. Never throws.
133 base_t::destroy_impl();
134 std::vector<char>().swap(m_heapmem);
137 std::vector<char> m_heapmem;
138 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
141 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
143 //!Typedef for a default basic_managed_heap_memory
144 //!of narrow characters
145 typedef basic_managed_heap_memory
147 ,rbtree_best_fit<null_mutex_family>
151 //!Typedef for a default basic_managed_heap_memory
152 //!of wide characters
153 typedef basic_managed_heap_memory
155 ,rbtree_best_fit<null_mutex_family>
157 wmanaged_heap_memory;
159 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
161 } //namespace interprocess {
162 } //namespace boost {
164 #include <boost/interprocess/detail/config_end.hpp>
166 #endif //BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP