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