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_WINDOWS_SHARED_MEMORY_HPP 12 #define BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_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/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> 35 36 namespace boost { 37 namespace interprocess { 38 39 namespace ipcdetail { 40 41 template<class AllocationAlgorithm> 42 struct wshmem_open_or_create 43 { 44 typedef ipcdetail::managed_open_or_create_impl 45 < windows_shared_memory, AllocationAlgorithm::Alignment, false, false> type; 46 }; 47 48 } //namespace ipcdetail { 49 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. 58 //! 59 //!Warning: basic_managed_windows_shared_memory and 60 //!basic_managed_shared_memory can't communicate between them. 61 template 62 < 63 class CharType, 64 class AllocationAlgorithm, 65 template<class IndexConfig> class IndexType 66 > 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> 71 { 72 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 73 private: 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; 78 get_this_pointer()79 basic_managed_windows_shared_memory *get_this_pointer() 80 { return this; } 81 82 private: 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 86 87 public: //functions 88 typedef typename base_t::size_type size_type; 89 90 //!Default constructor. Does nothing. 91 //!Useful in combination with move semantics basic_managed_windows_shared_memory()92 basic_managed_windows_shared_memory() 93 {} 94 95 //!Creates shared memory and creates and places the segment manager. 96 //!This can throw. basic_managed_windows_shared_memory(create_only_t,const char * name,size_type size,const void * addr=0,const permissions & perm=permissions ())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) 102 {} 103 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 106 //!segment. 107 //!This can throw. basic_managed_windows_shared_memory(open_or_create_t,const char * name,size_type size,const void * addr=0,const permissions & perm=permissions ())108 basic_managed_windows_shared_memory 109 (open_or_create_t, 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) 116 {} 117 118 //!Connects to a created shared memory and its segment manager. 119 //!This can throw. basic_managed_windows_shared_memory(open_only_t,const char * name,const void * addr=0)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(), 124 ipcdetail::DoOpen)) 125 {} 126 127 //!Connects to a created shared memory and its segment manager 128 //!in copy_on_write mode. 129 //!This can throw. basic_managed_windows_shared_memory(open_copy_on_write_t,const char * name,const void * addr=0)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)) 134 {} 135 136 //!Connects to a created shared memory and its segment manager 137 //!in read-only mode. 138 //!This can throw. basic_managed_windows_shared_memory(open_read_only_t,const char * name,const void * addr=0)139 basic_managed_windows_shared_memory 140 (open_read_only_t, const char* name, const void *addr = 0) 141 : base_t() 142 , m_wshm(open_only, name, read_only, addr, 143 create_open_func_t(get_this_pointer(), ipcdetail::DoOpen)) 144 {} 145 146 //!Moves the ownership of "moved"'s managed memory to *this. 147 //!Does not throw basic_managed_windows_shared_memory(BOOST_RV_REF (basic_managed_windows_shared_memory)moved)148 basic_managed_windows_shared_memory 149 (BOOST_RV_REF(basic_managed_windows_shared_memory) moved) 150 { this->swap(moved); } 151 152 //!Moves the ownership of "moved"'s managed memory to *this. 153 //!Does not throw operator =(BOOST_RV_REF (basic_managed_windows_shared_memory)moved)154 basic_managed_windows_shared_memory &operator=(BOOST_RV_REF(basic_managed_windows_shared_memory) moved) 155 { 156 basic_managed_windows_shared_memory tmp(boost::move(moved)); 157 this->swap(tmp); 158 return *this; 159 } 160 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. ~basic_managed_windows_shared_memory()166 ~basic_managed_windows_shared_memory() 167 {} 168 169 //!Swaps the ownership of the managed mapped memories managed by *this and other. 170 //!Never throws. swap(basic_managed_windows_shared_memory & other)171 void swap(basic_managed_windows_shared_memory &other) 172 { 173 base_t::swap(other); 174 m_wshm.swap(other.m_wshm); 175 } 176 177 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 178 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. 181 //!Never throws. 182 template <class T> find(char_ptr_holder_t name)183 std::pair<T*, size_type> find (char_ptr_holder_t name) 184 { 185 if(m_wshm.get_mapped_region().get_mode() == read_only){ 186 return base_t::template find_no_lock<T>(name); 187 } 188 else{ 189 return base_t::template find<T>(name); 190 } 191 } 192 193 private: 194 typename ipcdetail::wshmem_open_or_create<AllocationAlgorithm>::type m_wshm; 195 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 196 }; 197 198 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED 199 200 //!Typedef for a default basic_managed_windows_shared_memory 201 //!of narrow characters 202 typedef basic_managed_windows_shared_memory 203 <char 204 ,rbtree_best_fit<mutex_family> 205 ,iset_index> 206 managed_windows_shared_memory; 207 208 //!Typedef for a default basic_managed_windows_shared_memory 209 //!of wide characters 210 typedef basic_managed_windows_shared_memory 211 <wchar_t 212 ,rbtree_best_fit<mutex_family> 213 ,iset_index> 214 wmanaged_windows_shared_memory; 215 216 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED 217 218 219 } //namespace interprocess { 220 } //namespace boost { 221 222 #include <boost/interprocess/detail/config_end.hpp> 223 224 #endif //BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP 225