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
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
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>
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>
36 //!Describes a named heap memory allocation user class.
39 namespace interprocess {
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>*/
47 class AllocationAlgorithm,
48 template<class IndexConfig> class IndexType
50 class basic_managed_heap_memory
51 : public ipcdetail::basic_managed_memory_impl <CharType, AllocationAlgorithm, IndexType>
53 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
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
62 typedef typename base_t::size_type size_type;
64 //!Default constructor. Does nothing.
65 //!Useful in combination with move semantics
66 basic_managed_heap_memory(){}
68 //!Destructor. Liberates the heap memory holding the managed data.
70 ~basic_managed_heap_memory()
71 { this->priv_close(); }
73 //!Creates heap memory and initializes the segment manager.
75 basic_managed_heap_memory(size_type size)
76 : m_heapmem(size, char(0))
78 if(!base_t::create_impl(&m_heapmem[0], size)){
80 throw interprocess_exception("Could not initialize heap in basic_managed_heap_memory constructor");
84 //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
85 basic_managed_heap_memory(BOOST_RV_REF(basic_managed_heap_memory) moved)
86 { this->swap(moved); }
88 //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
89 basic_managed_heap_memory &operator=(BOOST_RV_REF(basic_managed_heap_memory) moved)
91 basic_managed_heap_memory tmp(boost::move(moved));
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.
105 bool grow(size_type extra_bytes)
107 //If memory is reallocated, data will
108 //be automatically copied
110 m_heapmem.resize(m_heapmem.size()+extra_bytes);
118 base_t::close_impl();
119 base_t::open_impl(&m_heapmem[0], m_heapmem.size());
120 base_t::grow(extra_bytes);
124 //!Swaps the ownership of the managed heap memories managed by *this and other.
126 void swap(basic_managed_heap_memory &other)
129 m_heapmem.swap(other.m_heapmem);
132 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
134 //!Frees resources. Never throws.
137 base_t::destroy_impl();
138 std::vector<char>().swap(m_heapmem);
141 std::vector<char> m_heapmem;
142 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
145 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
147 //!Typedef for a default basic_managed_heap_memory
148 //!of narrow characters
149 typedef basic_managed_heap_memory
151 ,rbtree_best_fit<null_mutex_family>
155 //!Typedef for a default basic_managed_heap_memory
156 //!of wide characters
157 typedef basic_managed_heap_memory
159 ,rbtree_best_fit<null_mutex_family>
161 wmanaged_heap_memory;
163 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
165 } //namespace interprocess {
166 } //namespace boost {
168 #include <boost/interprocess/detail/config_end.hpp>
170 #endif //BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP