1 ////////////////////////////////////////////////////////////////////////////// 2 // 3 // (C) Copyright Ion Gaztanaga 2008-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_XSI_SHARED_MEMORY_HPP 12 #define BOOST_INTERPROCESS_MANAGED_XSI_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 25 #if !defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS) 26 #error "This header can't be used in operating systems without XSI (System V) shared memory support" 27 #endif 28 29 #include <boost/interprocess/detail/managed_memory_impl.hpp> 30 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp> 31 #include <boost/interprocess/detail/xsi_shared_memory_file_wrapper.hpp> 32 #include <boost/interprocess/creation_tags.hpp> 33 //These includes needed to fulfill default template parameters of 34 //predeclarations in interprocess_fwd.hpp 35 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp> 36 #include <boost/interprocess/sync/mutex_family.hpp> 37 #include <boost/interprocess/indexes/iset_index.hpp> 38 39 namespace boost { 40 41 namespace interprocess { 42 43 namespace ipcdetail { 44 45 template<class AllocationAlgorithm> 46 struct xsishmem_open_or_create 47 { 48 typedef ipcdetail::managed_open_or_create_impl //!FileBased, StoreDevice 49 < xsi_shared_memory_file_wrapper, AllocationAlgorithm::Alignment, false, true> type; 50 }; 51 52 } //namespace ipcdetail { 53 54 //!A basic X/Open System Interface (XSI) shared memory named object creation class. Initializes the 55 //!shared memory segment. Inherits all basic functionality from 56 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType> 57 template 58 < 59 class CharType, 60 class AllocationAlgorithm, 61 template<class IndexConfig> class IndexType 62 > 63 class basic_managed_xsi_shared_memory 64 : public ipcdetail::basic_managed_memory_impl 65 <CharType, AllocationAlgorithm, IndexType 66 ,ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset> 67 , private ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type 68 { 69 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 70 public: 71 typedef xsi_shared_memory_file_wrapper device_type; 72 73 public: 74 typedef typename ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type base2_t; 75 typedef ipcdetail::basic_managed_memory_impl 76 <CharType, AllocationAlgorithm, IndexType, 77 base2_t::ManagedOpenOrCreateUserOffset> base_t; 78 79 typedef ipcdetail::create_open_func<base_t> create_open_func_t; 80 get_this_pointer()81 basic_managed_xsi_shared_memory *get_this_pointer() 82 { return this; } 83 84 private: 85 typedef typename base_t::char_ptr_holder_t char_ptr_holder_t; 86 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_xsi_shared_memory) 87 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 88 89 public: //functions 90 typedef typename base_t::size_type size_type; 91 92 //!Destroys *this and indicates that the calling process is finished using 93 //!the resource. The destructor function will deallocate 94 //!any system resources allocated by the system for use by this process for 95 //!this resource. The resource can still be opened again calling 96 //!the open constructor overload. To erase the resource from the system 97 //!use remove(). ~basic_managed_xsi_shared_memory()98 ~basic_managed_xsi_shared_memory() 99 {} 100 101 //!Default constructor. Does nothing. 102 //!Useful in combination with move semantics basic_managed_xsi_shared_memory()103 basic_managed_xsi_shared_memory() 104 {} 105 106 //!Creates shared memory and creates and places the segment manager. 107 //!This can throw. basic_managed_xsi_shared_memory(create_only_t,const xsi_key & key,std::size_t size,const void * addr=0,const permissions & perm=permissions ())108 basic_managed_xsi_shared_memory(create_only_t, const xsi_key &key, 109 std::size_t size, const void *addr = 0, const permissions& perm = permissions()) 110 : base_t() 111 , base2_t(create_only, key, size, read_write, addr, 112 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm) 113 {} 114 115 //!Creates shared memory and creates and places the segment manager if 116 //!segment was not created. If segment was created it connects to the 117 //!segment. 118 //!This can throw. basic_managed_xsi_shared_memory(open_or_create_t,const xsi_key & key,std::size_t size,const void * addr=0,const permissions & perm=permissions ())119 basic_managed_xsi_shared_memory (open_or_create_t, 120 const xsi_key &key, std::size_t size, 121 const void *addr = 0, const permissions& perm = permissions()) 122 : base_t() 123 , base2_t(open_or_create, key, size, read_write, addr, 124 create_open_func_t(get_this_pointer(), 125 ipcdetail::DoOpenOrCreate), perm) 126 {} 127 128 //!Connects to a created shared memory and its segment manager. 129 //!in read-only mode. 130 //!This can throw. basic_managed_xsi_shared_memory(open_read_only_t,const xsi_key & key,const void * addr=0)131 basic_managed_xsi_shared_memory (open_read_only_t, const xsi_key &key, 132 const void *addr = 0) 133 : base_t() 134 , base2_t(open_only, key, read_only, addr, 135 create_open_func_t(get_this_pointer(), 136 ipcdetail::DoOpen)) 137 {} 138 139 //!Connects to a created shared memory and its segment manager. 140 //!This can throw. basic_managed_xsi_shared_memory(open_only_t,const xsi_key & key,const void * addr=0)141 basic_managed_xsi_shared_memory (open_only_t, const xsi_key &key, 142 const void *addr = 0) 143 : base_t() 144 , base2_t(open_only, key, read_write, addr, 145 create_open_func_t(get_this_pointer(), 146 ipcdetail::DoOpen)) 147 {} 148 149 //!Moves the ownership of "moved"'s managed memory to *this. 150 //!Does not throw basic_managed_xsi_shared_memory(BOOST_RV_REF (basic_managed_xsi_shared_memory)moved)151 basic_managed_xsi_shared_memory(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved) 152 { 153 basic_managed_xsi_shared_memory tmp; 154 this->swap(moved); 155 tmp.swap(moved); 156 } 157 158 //!Moves the ownership of "moved"'s managed memory to *this. 159 //!Does not throw operator =(BOOST_RV_REF (basic_managed_xsi_shared_memory)moved)160 basic_managed_xsi_shared_memory &operator=(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved) 161 { 162 basic_managed_xsi_shared_memory tmp(boost::move(moved)); 163 this->swap(tmp); 164 return *this; 165 } 166 167 //!Swaps the ownership of the managed shared memories managed by *this and other. 168 //!Never throws. swap(basic_managed_xsi_shared_memory & other)169 void swap(basic_managed_xsi_shared_memory &other) 170 { 171 base_t::swap(other); 172 base2_t::swap(other); 173 } 174 175 //!Erases a XSI shared memory object identified by shmid 176 //!from the system. 177 //!Returns false on error. Never throws remove(int shmid)178 static bool remove(int shmid) 179 { return device_type::remove(shmid); } 180 get_shmid() const181 int get_shmid() const 182 { return base2_t::get_device().get_shmid(); } 183 184 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 185 186 //!Tries to find a previous named allocation address. Returns a memory 187 //!buffer and the object count. If not found returned pointer is 0. 188 //!Never throws. 189 template <class T> find(char_ptr_holder_t name)190 std::pair<T*, std::size_t> find (char_ptr_holder_t name) 191 { 192 if(base2_t::get_mapped_region().get_mode() == read_only){ 193 return base_t::template find_no_lock<T>(name); 194 } 195 else{ 196 return base_t::template find<T>(name); 197 } 198 } 199 200 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 201 }; 202 203 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED 204 205 //!Typedef for a default basic_managed_xsi_shared_memory 206 //!of narrow characters 207 typedef basic_managed_xsi_shared_memory 208 <char 209 ,rbtree_best_fit<mutex_family> 210 ,iset_index> 211 managed_xsi_shared_memory; 212 213 //!Typedef for a default basic_managed_xsi_shared_memory 214 //!of wide characters 215 typedef basic_managed_xsi_shared_memory 216 <wchar_t 217 ,rbtree_best_fit<mutex_family> 218 ,iset_index> 219 wmanaged_xsi_shared_memory; 220 221 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED 222 223 } //namespace interprocess { 224 } //namespace boost { 225 226 #include <boost/interprocess/detail/config_end.hpp> 227 228 #endif //BOOST_INTERPROCESS_MANAGED_XSI_SHARED_MEMORY_HPP 229 230