| 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_SHARED_MEMORY_HPP |
| 12 | #define BOOST_INTERPROCESS_MANAGED_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 | #include <boost/interprocess/detail/managed_memory_impl.hpp> |
| 26 | #include <boost/interprocess/detail/managed_open_or_create_impl.hpp> |
| 27 | #include <boost/interprocess/shared_memory_object.hpp> |
| 28 | #include <boost/interprocess/creation_tags.hpp> |
| 29 | #include <boost/interprocess/permissions.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 | |
| 35 | namespace boost { |
| 36 | namespace interprocess { |
| 37 | |
| 38 | namespace ipcdetail { |
| 39 | |
| 40 | template |
| 41 | < |
| 42 | class CharType, |
| 43 | class AllocationAlgorithm, |
| 44 | template<class IndexConfig> class IndexType |
| 45 | > |
| 46 | struct shmem_open_or_create |
| 47 | { |
| 48 | static const std::size_t segment_manager_alignment = boost::move_detail::alignment_of |
| 49 | < segment_manager |
| 50 | < CharType |
| 51 | , AllocationAlgorithm |
| 52 | , IndexType> |
| 53 | >::value; |
| 54 | static const std::size_t final_segment_manager_alignment |
| 55 | = segment_manager_alignment > AllocationAlgorithm::Alignment |
| 56 | ? segment_manager_alignment : AllocationAlgorithm::Alignment; |
| 57 | |
| 58 | typedef ipcdetail::managed_open_or_create_impl |
| 59 | < shared_memory_object |
| 60 | , final_segment_manager_alignment |
| 61 | , true |
| 62 | , false> type; |
| 63 | }; |
| 64 | |
| 65 | } //namespace ipcdetail { |
| 66 | |
| 67 | //!A basic shared memory named object creation class. Initializes the |
| 68 | //!shared memory segment. Inherits all basic functionality from |
| 69 | //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/ |
| 70 | template |
| 71 | < |
| 72 | class CharType, |
| 73 | class AllocationAlgorithm, |
| 74 | template<class IndexConfig> class IndexType |
| 75 | > |
| 76 | class basic_managed_shared_memory |
| 77 | : public ipcdetail::basic_managed_memory_impl |
| 78 | < CharType, AllocationAlgorithm, IndexType |
| 79 | , ipcdetail::shmem_open_or_create<CharType, AllocationAlgorithm, IndexType>::type::ManagedOpenOrCreateUserOffset> |
| 80 | , private ipcdetail::shmem_open_or_create<CharType, AllocationAlgorithm, IndexType>::type |
| 81 | { |
| 82 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
| 83 | typedef typename ipcdetail::shmem_open_or_create |
| 84 | < CharType |
| 85 | , AllocationAlgorithm |
| 86 | , IndexType>::type base2_t; |
| 87 | typedef ipcdetail::basic_managed_memory_impl |
| 88 | <CharType, AllocationAlgorithm, IndexType, |
| 89 | base2_t::ManagedOpenOrCreateUserOffset> base_t; |
| 90 | |
| 91 | typedef ipcdetail::create_open_func<base_t> create_open_func_t; |
| 92 | |
| 93 | basic_managed_shared_memory *get_this_pointer() |
| 94 | { return this; } |
| 95 | |
| 96 | public: |
| 97 | typedef shared_memory_object device_type; |
| 98 | typedef typename base_t::size_type size_type; |
| 99 | |
| 100 | private: |
| 101 | typedef typename base_t::char_ptr_holder_t char_ptr_holder_t; |
| 102 | BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_shared_memory) |
| 103 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 104 | |
| 105 | public: //functions |
| 106 | |
| 107 | //!Destroys *this and indicates that the calling process is finished using |
| 108 | //!the resource. The destructor function will deallocate |
| 109 | //!any system resources allocated by the system for use by this process for |
| 110 | //!this resource. The resource can still be opened again calling |
| 111 | //!the open constructor overload. To erase the resource from the system |
| 112 | //!use remove(). |
| 113 | ~basic_managed_shared_memory() |
| 114 | {} |
| 115 | |
| 116 | //!Default constructor. Does nothing. |
| 117 | //!Useful in combination with move semantics |
| 118 | basic_managed_shared_memory() |
| 119 | {} |
| 120 | |
| 121 | //!Creates shared memory and creates and places the segment manager. |
| 122 | //!This can throw. |
| 123 | basic_managed_shared_memory(create_only_t, const char *name, |
| 124 | size_type size, const void *addr = 0, const permissions& perm = permissions()) |
| 125 | : base_t() |
| 126 | , base2_t(create_only, name, size, read_write, addr, |
| 127 | create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm) |
| 128 | {} |
| 129 | |
| 130 | //!Creates shared memory and creates and places the segment manager if |
| 131 | //!segment was not created. If segment was created it connects to the |
| 132 | //!segment. |
| 133 | //!This can throw. |
| 134 | basic_managed_shared_memory (open_or_create_t, |
| 135 | const char *name, size_type size, |
| 136 | const void *addr = 0, const permissions& perm = permissions()) |
| 137 | : base_t() |
| 138 | , base2_t(open_or_create, name, size, read_write, addr, |
| 139 | create_open_func_t(get_this_pointer(), |
| 140 | ipcdetail::DoOpenOrCreate), perm) |
| 141 | {} |
| 142 | |
| 143 | //!Connects to a created shared memory and its segment manager. |
| 144 | //!in copy_on_write mode. |
| 145 | //!This can throw. |
| 146 | basic_managed_shared_memory (open_copy_on_write_t, const char* name, |
| 147 | const void *addr = 0) |
| 148 | : base_t() |
| 149 | , base2_t(open_only, name, copy_on_write, addr, |
| 150 | create_open_func_t(get_this_pointer(), |
| 151 | ipcdetail::DoOpen)) |
| 152 | {} |
| 153 | |
| 154 | //!Connects to a created shared memory and its segment manager. |
| 155 | //!in read-only mode. |
| 156 | //!This can throw. |
| 157 | basic_managed_shared_memory (open_read_only_t, const char* name, |
| 158 | const void *addr = 0) |
| 159 | : base_t() |
| 160 | , base2_t(open_only, name, read_only, addr, |
| 161 | create_open_func_t(get_this_pointer(), |
| 162 | ipcdetail::DoOpen)) |
| 163 | {} |
| 164 | |
| 165 | //!Connects to a created shared memory and its segment manager. |
| 166 | //!This can throw. |
| 167 | basic_managed_shared_memory (open_only_t, const char* name, |
| 168 | const void *addr = 0) |
| 169 | : base_t() |
| 170 | , base2_t(open_only, name, read_write, addr, |
| 171 | create_open_func_t(get_this_pointer(), |
| 172 | ipcdetail::DoOpen)) |
| 173 | {} |
| 174 | |
| 175 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
| 176 | |
| 177 | //!Creates shared memory and creates and places the segment manager. |
| 178 | //!This can throw. |
| 179 | //! |
| 180 | //!Note: This function is only available on operating systems with |
| 181 | //! native wchar_t APIs (e.g. Windows). |
| 182 | basic_managed_shared_memory(create_only_t, const wchar_t *name, |
| 183 | size_type size, const void *addr = 0, const permissions& perm = permissions()) |
| 184 | : base_t() |
| 185 | , base2_t(create_only, name, size, read_write, addr, |
| 186 | create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm) |
| 187 | {} |
| 188 | |
| 189 | //!Creates shared memory and creates and places the segment manager if |
| 190 | //!segment was not created. If segment was created it connects to the |
| 191 | //!segment. |
| 192 | //!This can throw. |
| 193 | //! |
| 194 | //!Note: This function is only available on operating systems with |
| 195 | //! native wchar_t APIs (e.g. Windows). |
| 196 | basic_managed_shared_memory (open_or_create_t, |
| 197 | const wchar_t *name, size_type size, |
| 198 | const void *addr = 0, const permissions& perm = permissions()) |
| 199 | : base_t() |
| 200 | , base2_t(open_or_create, name, size, read_write, addr, |
| 201 | create_open_func_t(get_this_pointer(), |
| 202 | ipcdetail::DoOpenOrCreate), perm) |
| 203 | {} |
| 204 | |
| 205 | //!Connects to a created shared memory and its segment manager. |
| 206 | //!in copy_on_write mode. |
| 207 | //!This can throw. |
| 208 | //! |
| 209 | //!Note: This function is only available on operating systems with |
| 210 | //! native wchar_t APIs (e.g. Windows). |
| 211 | basic_managed_shared_memory (open_copy_on_write_t, const wchar_t* name, |
| 212 | const void *addr = 0) |
| 213 | : base_t() |
| 214 | , base2_t(open_only, name, copy_on_write, addr, |
| 215 | create_open_func_t(get_this_pointer(), |
| 216 | ipcdetail::DoOpen)) |
| 217 | {} |
| 218 | |
| 219 | //!Connects to a created shared memory and its segment manager. |
| 220 | //!in read-only mode. |
| 221 | //!This can throw. |
| 222 | //! |
| 223 | //!Note: This function is only available on operating systems with |
| 224 | //! native wchar_t APIs (e.g. Windows). |
| 225 | basic_managed_shared_memory (open_read_only_t, const wchar_t* name, |
| 226 | const void *addr = 0) |
| 227 | : base_t() |
| 228 | , base2_t(open_only, name, read_only, addr, |
| 229 | create_open_func_t(get_this_pointer(), |
| 230 | ipcdetail::DoOpen)) |
| 231 | {} |
| 232 | |
| 233 | //!Connects to a created shared memory and its segment manager. |
| 234 | //!This can throw. |
| 235 | //! |
| 236 | //!Note: This function is only available on operating systems with |
| 237 | //! native wchar_t APIs (e.g. Windows). |
| 238 | basic_managed_shared_memory (open_only_t, const wchar_t* name, |
| 239 | const void *addr = 0) |
| 240 | : base_t() |
| 241 | , base2_t(open_only, name, read_write, addr, |
| 242 | create_open_func_t(get_this_pointer(), |
| 243 | ipcdetail::DoOpen)) |
| 244 | {} |
| 245 | |
| 246 | #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
| 247 | |
| 248 | //!Moves the ownership of "moved"'s managed memory to *this. |
| 249 | //!Does not throw |
| 250 | basic_managed_shared_memory(BOOST_RV_REF(basic_managed_shared_memory) moved) |
| 251 | { |
| 252 | basic_managed_shared_memory tmp; |
| 253 | this->swap(other&: moved); |
| 254 | tmp.swap(other&: moved); |
| 255 | } |
| 256 | |
| 257 | //!Moves the ownership of "moved"'s managed memory to *this. |
| 258 | //!Does not throw |
| 259 | basic_managed_shared_memory &operator=(BOOST_RV_REF(basic_managed_shared_memory) moved) |
| 260 | { |
| 261 | basic_managed_shared_memory tmp(boost::move(moved)); |
| 262 | this->swap(other&: tmp); |
| 263 | return *this; |
| 264 | } |
| 265 | |
| 266 | //!Swaps the ownership of the managed shared memories managed by *this and other. |
| 267 | //!Never throws. |
| 268 | void swap(basic_managed_shared_memory &other) |
| 269 | { |
| 270 | base_t::swap(other); |
| 271 | base2_t::swap(other); |
| 272 | } |
| 273 | |
| 274 | //!Tries to resize the managed shared memory object so that we have |
| 275 | //!room for more objects. |
| 276 | //! |
| 277 | //!This function is not synchronized so no other thread or process should |
| 278 | //!be reading or writing the file |
| 279 | static bool grow(const char *shmname, size_type ) |
| 280 | { |
| 281 | return base_t::template grow |
| 282 | <basic_managed_shared_memory>(shmname, extra_bytes); |
| 283 | } |
| 284 | |
| 285 | //!Tries to resize the managed shared memory to minimized the size of the file. |
| 286 | //! |
| 287 | //!This function is not synchronized so no other thread or process should |
| 288 | //!be reading or writing the file |
| 289 | static bool shrink_to_fit(const char *shmname) |
| 290 | { |
| 291 | return base_t::template shrink_to_fit |
| 292 | <basic_managed_shared_memory>(shmname); |
| 293 | } |
| 294 | |
| 295 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
| 296 | |
| 297 | //!Tries to resize the managed shared memory object so that we have |
| 298 | //!room for more objects. |
| 299 | //! |
| 300 | //!This function is not synchronized so no other thread or process should |
| 301 | //!be reading or writing the file |
| 302 | //! |
| 303 | //!Note: This function is only available on operating systems with |
| 304 | //! native wchar_t APIs (e.g. Windows). |
| 305 | static bool grow(const wchar_t *shmname, size_type extra_bytes) |
| 306 | { |
| 307 | return base_t::template grow |
| 308 | <basic_managed_shared_memory>(shmname, extra_bytes); |
| 309 | } |
| 310 | |
| 311 | //!Tries to resize the managed shared memory to minimized the size of the file. |
| 312 | //! |
| 313 | //!This function is not synchronized so no other thread or process should |
| 314 | //!be reading or writing the file |
| 315 | //! |
| 316 | //!Note: This function is only available on operating systems with |
| 317 | //! native wchar_t APIs (e.g. Windows). |
| 318 | static bool shrink_to_fit(const wchar_t *shmname) |
| 319 | { |
| 320 | return base_t::template shrink_to_fit |
| 321 | <basic_managed_shared_memory>(shmname); |
| 322 | } |
| 323 | |
| 324 | #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
| 325 | |
| 326 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
| 327 | |
| 328 | //!Tries to find a previous named allocation address. Returns a memory |
| 329 | //!buffer and the object count. If not found returned pointer is 0. |
| 330 | //!Never throws. |
| 331 | template <class T> |
| 332 | std::pair<T*, size_type> find (char_ptr_holder_t name) |
| 333 | { |
| 334 | if(base2_t::get_mapped_region().get_mode() == read_only){ |
| 335 | return base_t::template find_no_lock<T>(name); |
| 336 | } |
| 337 | else{ |
| 338 | return base_t::template find<T>(name); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 343 | }; |
| 344 | |
| 345 | #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 346 | |
| 347 | //!Typedef for a default basic_managed_shared_memory |
| 348 | //!of narrow characters |
| 349 | typedef basic_managed_shared_memory |
| 350 | <char |
| 351 | ,rbtree_best_fit<mutex_family> |
| 352 | ,iset_index> |
| 353 | managed_shared_memory; |
| 354 | |
| 355 | //!Typedef for a default basic_managed_shared_memory |
| 356 | //!of wide characters |
| 357 | typedef basic_managed_shared_memory |
| 358 | <wchar_t |
| 359 | ,rbtree_best_fit<mutex_family> |
| 360 | ,iset_index> |
| 361 | wmanaged_shared_memory; |
| 362 | |
| 363 | //!Typedef for a default basic_managed_shared_memory |
| 364 | //!of narrow characters to be placed in a fixed address |
| 365 | typedef basic_managed_shared_memory |
| 366 | <char |
| 367 | ,rbtree_best_fit<mutex_family, void*> |
| 368 | ,iset_index> |
| 369 | fixed_managed_shared_memory; |
| 370 | |
| 371 | //!Typedef for a default basic_managed_shared_memory |
| 372 | //!of narrow characters to be placed in a fixed address |
| 373 | typedef basic_managed_shared_memory |
| 374 | <wchar_t |
| 375 | ,rbtree_best_fit<mutex_family, void*> |
| 376 | ,iset_index> |
| 377 | wfixed_managed_shared_memory; |
| 378 | |
| 379 | |
| 380 | #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 381 | |
| 382 | } //namespace interprocess { |
| 383 | } //namespace boost { |
| 384 | |
| 385 | #include <boost/interprocess/detail/config_end.hpp> |
| 386 | |
| 387 | #endif //BOOST_INTERPROCESS_MANAGED_SHARED_MEMORY_HPP |
| 388 | |
| 389 | |