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_WINDOWS_SHARED_MEMORY_HPP
12 #define BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_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/detail/managed_open_or_create_impl.hpp>
25 #include <boost/interprocess/detail/managed_memory_impl.hpp>
26 #include <boost/interprocess/creation_tags.hpp>
27 #include <boost/interprocess/windows_shared_memory.hpp>
28 #include <boost/interprocess/permissions.hpp>
29 #include <boost/move/utility_core.hpp>
30 //These includes needed to fulfill default template parameters of
31 //predeclarations in interprocess_fwd.hpp
32 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
33 #include <boost/interprocess/sync/mutex_family.hpp>
34 #include <boost/interprocess/indexes/iset_index.hpp>
37 namespace interprocess {
41 template<class AllocationAlgorithm>
42 struct wshmem_open_or_create
44 typedef ipcdetail::managed_open_or_create_impl
45 < windows_shared_memory, AllocationAlgorithm::Alignment, false, false> type;
48 } //namespace ipcdetail {
50 //!A basic managed windows shared memory creation class. Initializes the
51 //!shared memory segment. Inherits all basic functionality from
52 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
53 //!Unlike basic_managed_shared_memory, it has
54 //!no kernel persistence and the shared memory is destroyed
55 //!when all processes destroy all their windows_shared_memory
56 //!objects and mapped regions for the same shared memory
57 //!or the processes end/crash.
59 //!Warning: basic_managed_windows_shared_memory and
60 //!basic_managed_shared_memory can't communicate between them.
64 class AllocationAlgorithm,
65 template<class IndexConfig> class IndexType
67 class basic_managed_windows_shared_memory
68 : public ipcdetail::basic_managed_memory_impl
69 < CharType, AllocationAlgorithm, IndexType
70 , ipcdetail::wshmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset>
72 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
74 typedef ipcdetail::basic_managed_memory_impl
75 <CharType, AllocationAlgorithm, IndexType,
76 ipcdetail::wshmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset> base_t;
77 typedef ipcdetail::create_open_func<base_t> create_open_func_t;
79 basic_managed_windows_shared_memory *get_this_pointer()
83 typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
84 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_windows_shared_memory)
85 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
88 typedef typename base_t::size_type size_type;
90 //!Default constructor. Does nothing.
91 //!Useful in combination with move semantics
92 basic_managed_windows_shared_memory()
95 //!Creates shared memory and creates and places the segment manager.
97 basic_managed_windows_shared_memory
98 (create_only_t, const char *name,
99 size_type size, const void *addr = 0, const permissions &perm = permissions())
100 : m_wshm(create_only, name, size, read_write, addr,
101 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
104 //!Creates shared memory and creates and places the segment manager if
105 //!segment was not created. If segment was created it connects to the
108 basic_managed_windows_shared_memory
110 const char *name, size_type size,
111 const void *addr = 0,
112 const permissions &perm = permissions())
113 : m_wshm(open_or_create, name, size, read_write, addr,
114 create_open_func_t(get_this_pointer(),
115 ipcdetail::DoOpenOrCreate), perm)
118 //!Connects to a created shared memory and its segment manager.
120 basic_managed_windows_shared_memory
121 (open_only_t, const char* name, const void *addr = 0)
122 : m_wshm(open_only, name, read_write, addr,
123 create_open_func_t(get_this_pointer(),
127 //!Connects to a created shared memory and its segment manager
128 //!in copy_on_write mode.
130 basic_managed_windows_shared_memory
131 (open_copy_on_write_t, const char* name, const void *addr = 0)
132 : m_wshm(open_only, name, copy_on_write, addr,
133 create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
136 //!Connects to a created shared memory and its segment manager
137 //!in read-only mode.
139 basic_managed_windows_shared_memory
140 (open_read_only_t, const char* name, const void *addr = 0)
142 , m_wshm(open_only, name, read_only, addr,
143 create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
146 //!Moves the ownership of "moved"'s managed memory to *this.
148 basic_managed_windows_shared_memory
149 (BOOST_RV_REF(basic_managed_windows_shared_memory) moved)
150 { this->swap(moved); }
152 //!Moves the ownership of "moved"'s managed memory to *this.
154 basic_managed_windows_shared_memory &operator=(BOOST_RV_REF(basic_managed_windows_shared_memory) moved)
156 basic_managed_windows_shared_memory tmp(boost::move(moved));
161 //!Destroys *this and indicates that the calling process is finished using
162 //!the resource. All mapped regions are still valid after
163 //!destruction. When all mapped regions and basic_managed_windows_shared_memory
164 //!objects referring the shared memory are destroyed, the
165 //!operating system will destroy the shared memory.
166 ~basic_managed_windows_shared_memory()
169 //!Swaps the ownership of the managed mapped memories managed by *this and other.
171 void swap(basic_managed_windows_shared_memory &other)
174 m_wshm.swap(other.m_wshm);
177 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
179 //!Tries to find a previous named allocation address. Returns a memory
180 //!buffer and the object count. If not found returned pointer is 0.
183 std::pair<T*, size_type> find (char_ptr_holder_t name)
185 if(m_wshm.get_mapped_region().get_mode() == read_only){
186 return base_t::template find_no_lock<T>(name);
189 return base_t::template find<T>(name);
194 typename ipcdetail::wshmem_open_or_create<AllocationAlgorithm>::type m_wshm;
195 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
198 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
200 //!Typedef for a default basic_managed_windows_shared_memory
201 //!of narrow characters
202 typedef basic_managed_windows_shared_memory
204 ,rbtree_best_fit<mutex_family>
206 managed_windows_shared_memory;
208 //!Typedef for a default basic_managed_windows_shared_memory
209 //!of wide characters
210 typedef basic_managed_windows_shared_memory
212 ,rbtree_best_fit<mutex_family>
214 wmanaged_windows_shared_memory;
216 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
219 } //namespace interprocess {
220 } //namespace boost {
222 #include <boost/interprocess/detail/config_end.hpp>
224 #endif //BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP