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_MAPPED_FILE_HPP 12 #define BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_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/detail/file_wrapper.hpp> 28 #include <boost/move/utility_core.hpp> 29 #include <boost/interprocess/file_mapping.hpp> 30 #include <boost/interprocess/permissions.hpp> 31 //These includes needed to fulfill default template parameters of 32 //predeclarations in interprocess_fwd.hpp 33 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp> 34 #include <boost/interprocess/sync/mutex_family.hpp> 35 #include <boost/interprocess/indexes/iset_index.hpp> 36 37 namespace boost { 38 namespace interprocess { 39 namespace ipcdetail { 40 41 template<class AllocationAlgorithm> 42 struct mfile_open_or_create 43 { 44 typedef ipcdetail::managed_open_or_create_impl 45 < file_wrapper, AllocationAlgorithm::Alignment, true, false> type; 46 }; 47 48 } //namespace ipcdetail { 49 50 //!A basic mapped file named object creation class. Initializes the 51 //!mapped file. Inherits all basic functionality from 52 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType> 53 template 54 < 55 class CharType, 56 class AllocationAlgorithm, 57 template<class IndexConfig> class IndexType 58 > 59 class basic_managed_mapped_file 60 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 61 : public ipcdetail::basic_managed_memory_impl 62 <CharType, AllocationAlgorithm, IndexType 63 ,ipcdetail::mfile_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset> 64 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 65 { 66 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 67 public: 68 typedef ipcdetail::basic_managed_memory_impl 69 <CharType, AllocationAlgorithm, IndexType, 70 ipcdetail::mfile_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset> base_t; 71 typedef ipcdetail::file_wrapper device_type; 72 73 private: 74 75 typedef ipcdetail::create_open_func<base_t> create_open_func_t; 76 get_this_pointer()77 basic_managed_mapped_file *get_this_pointer() 78 { return this; } 79 80 private: 81 typedef typename base_t::char_ptr_holder_t char_ptr_holder_t; 82 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_mapped_file) 83 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 84 85 public: //functions 86 87 //!Unsigned integral type enough to represent 88 //!the size of a basic_managed_mapped_file. 89 typedef typename BOOST_INTERPROCESS_IMPDEF(base_t::size_type) size_type; 90 91 //!Creates mapped file and creates and places the segment manager. 92 //!This can throw. basic_managed_mapped_file()93 basic_managed_mapped_file() 94 {} 95 96 //!Creates mapped file and creates and places the segment manager. 97 //!This can throw. basic_managed_mapped_file(create_only_t,const char * name,size_type size,const void * addr=0,const permissions & perm=permissions ())98 basic_managed_mapped_file(create_only_t, const char *name, 99 size_type size, const void *addr = 0, const permissions &perm = permissions()) 100 : m_mfile(create_only, name, size, read_write, addr, 101 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm) 102 {} 103 104 //!Creates mapped file 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_mapped_file(open_or_create_t,const char * name,size_type size,const void * addr=0,const permissions & perm=permissions ())108 basic_managed_mapped_file (open_or_create_t, 109 const char *name, size_type size, 110 const void *addr = 0, const permissions &perm = permissions()) 111 : m_mfile(open_or_create, name, size, read_write, addr, 112 create_open_func_t(get_this_pointer(), 113 ipcdetail::DoOpenOrCreate), perm) 114 {} 115 116 //!Connects to a created mapped file and its segment manager. 117 //!This can throw. basic_managed_mapped_file(open_only_t,const char * name,const void * addr=0)118 basic_managed_mapped_file (open_only_t, const char* name, 119 const void *addr = 0) 120 : m_mfile(open_only, name, read_write, addr, 121 create_open_func_t(get_this_pointer(), 122 ipcdetail::DoOpen)) 123 {} 124 125 //!Connects to a created mapped file and its segment manager 126 //!in copy_on_write mode. 127 //!This can throw. basic_managed_mapped_file(open_copy_on_write_t,const char * name,const void * addr=0)128 basic_managed_mapped_file (open_copy_on_write_t, const char* name, 129 const void *addr = 0) 130 : m_mfile(open_only, name, copy_on_write, addr, 131 create_open_func_t(get_this_pointer(), 132 ipcdetail::DoOpen)) 133 {} 134 135 //!Connects to a created mapped file and its segment manager 136 //!in read-only mode. 137 //!This can throw. basic_managed_mapped_file(open_read_only_t,const char * name,const void * addr=0)138 basic_managed_mapped_file (open_read_only_t, const char* name, 139 const void *addr = 0) 140 : m_mfile(open_only, name, read_only, addr, 141 create_open_func_t(get_this_pointer(), 142 ipcdetail::DoOpen)) 143 {} 144 145 //!Moves the ownership of "moved"'s managed memory to *this. 146 //!Does not throw basic_managed_mapped_file(BOOST_RV_REF (basic_managed_mapped_file)moved)147 basic_managed_mapped_file(BOOST_RV_REF(basic_managed_mapped_file) moved) 148 { 149 this->swap(moved); 150 } 151 152 //!Moves the ownership of "moved"'s managed memory to *this. 153 //!Does not throw operator =(BOOST_RV_REF (basic_managed_mapped_file)moved)154 basic_managed_mapped_file &operator=(BOOST_RV_REF(basic_managed_mapped_file) moved) 155 { 156 basic_managed_mapped_file 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. The destructor function will deallocate 163 //!any system resources allocated by the system for use by this process for 164 //!this resource. The resource can still be opened again calling 165 //!the open constructor overload. To erase the resource from the system 166 //!use remove(). ~basic_managed_mapped_file()167 ~basic_managed_mapped_file() 168 {} 169 170 //!Swaps the ownership of the managed mapped memories managed by *this and other. 171 //!Never throws. swap(basic_managed_mapped_file & other)172 void swap(basic_managed_mapped_file &other) 173 { 174 base_t::swap(other); 175 m_mfile.swap(other.m_mfile); 176 } 177 178 //!Flushes cached data to file. 179 //!Never throws flush()180 bool flush() 181 { return m_mfile.flush(); } 182 183 //!Tries to resize mapped file so that we have room for 184 //!more objects. 185 //! 186 //!This function is not synchronized so no other thread or process should 187 //!be reading or writing the file grow(const char * filename,size_type extra_bytes)188 static bool grow(const char *filename, size_type extra_bytes) 189 { 190 return base_t::template grow 191 <basic_managed_mapped_file>(filename, extra_bytes); 192 } 193 194 //!Tries to resize mapped file to minimized the size of the file. 195 //! 196 //!This function is not synchronized so no other thread or process should 197 //!be reading or writing the file shrink_to_fit(const char * filename)198 static bool shrink_to_fit(const char *filename) 199 { 200 return base_t::template shrink_to_fit 201 <basic_managed_mapped_file>(filename); 202 } 203 204 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 205 206 //!Tries to find a previous named allocation address. Returns a memory 207 //!buffer and the object count. If not found returned pointer is 0. 208 //!Never throws. 209 template <class T> find(char_ptr_holder_t name)210 std::pair<T*, size_type> find (char_ptr_holder_t name) 211 { 212 if(m_mfile.get_mapped_region().get_mode() == read_only){ 213 return base_t::template find_no_lock<T>(name); 214 } 215 else{ 216 return base_t::template find<T>(name); 217 } 218 } 219 220 private: 221 typename ipcdetail::mfile_open_or_create<AllocationAlgorithm>::type m_mfile; 222 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 223 }; 224 225 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED 226 227 //!Typedef for a default basic_managed_mapped_file 228 //!of narrow characters 229 typedef basic_managed_mapped_file 230 <char 231 ,rbtree_best_fit<mutex_family> 232 ,iset_index> 233 managed_mapped_file; 234 235 //!Typedef for a default basic_managed_mapped_file 236 //!of wide characters 237 typedef basic_managed_mapped_file 238 <wchar_t 239 ,rbtree_best_fit<mutex_family> 240 ,iset_index> 241 wmanaged_mapped_file; 242 243 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED 244 245 } //namespace interprocess { 246 } //namespace boost { 247 248 #include <boost/interprocess/detail/config_end.hpp> 249 250 #endif //BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP 251