Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/interprocess for documentation. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP Chris@16: #define BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: # Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: //These includes needed to fulfill default template parameters of Chris@16: //predeclarations in interprocess_fwd.hpp Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@16: namespace ipcdetail { Chris@16: Chris@16: template Chris@16: struct wshmem_open_or_create Chris@16: { Chris@16: typedef ipcdetail::managed_open_or_create_impl Chris@16: < windows_shared_memory, AllocationAlgorithm::Alignment, false, false> type; Chris@16: }; Chris@16: Chris@16: } //namespace ipcdetail { Chris@16: Chris@16: //!A basic managed windows shared memory creation class. Initializes the Chris@16: //!shared memory segment. Inherits all basic functionality from Chris@16: //!basic_managed_memory_impl Chris@16: //!Unlike basic_managed_shared_memory, it has Chris@16: //!no kernel persistence and the shared memory is destroyed Chris@16: //!when all processes destroy all their windows_shared_memory Chris@16: //!objects and mapped regions for the same shared memory Chris@16: //!or the processes end/crash. Chris@16: //! Chris@16: //!Warning: basic_managed_windows_shared_memory and Chris@16: //!basic_managed_shared_memory can't communicate between them. Chris@16: template Chris@16: < Chris@16: class CharType, Chris@16: class AllocationAlgorithm, Chris@16: template class IndexType Chris@16: > Chris@16: class basic_managed_windows_shared_memory Chris@16: : public ipcdetail::basic_managed_memory_impl Chris@16: < CharType, AllocationAlgorithm, IndexType Chris@16: , ipcdetail::wshmem_open_or_create::type::ManagedOpenOrCreateUserOffset> Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: typedef ipcdetail::basic_managed_memory_impl Chris@16: ::type::ManagedOpenOrCreateUserOffset> base_t; Chris@16: typedef ipcdetail::create_open_func create_open_func_t; Chris@16: Chris@16: basic_managed_windows_shared_memory *get_this_pointer() Chris@16: { return this; } Chris@16: Chris@16: private: Chris@16: typedef typename base_t::char_ptr_holder_t char_ptr_holder_t; Chris@16: BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_windows_shared_memory) Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: public: //functions Chris@16: typedef typename base_t::size_type size_type; Chris@16: Chris@16: //!Default constructor. Does nothing. Chris@16: //!Useful in combination with move semantics Chris@16: basic_managed_windows_shared_memory() Chris@16: {} Chris@16: Chris@16: //!Creates shared memory and creates and places the segment manager. Chris@16: //!This can throw. Chris@16: basic_managed_windows_shared_memory Chris@16: (create_only_t, const char *name, Chris@16: size_type size, const void *addr = 0, const permissions &perm = permissions()) Chris@16: : m_wshm(create_only, name, size, read_write, addr, Chris@16: create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm) Chris@16: {} Chris@16: Chris@16: //!Creates shared memory and creates and places the segment manager if Chris@16: //!segment was not created. If segment was created it connects to the Chris@16: //!segment. Chris@16: //!This can throw. Chris@16: basic_managed_windows_shared_memory Chris@16: (open_or_create_t, Chris@16: const char *name, size_type size, Chris@16: const void *addr = 0, Chris@16: const permissions &perm = permissions()) Chris@16: : m_wshm(open_or_create, name, size, read_write, addr, Chris@16: create_open_func_t(get_this_pointer(), Chris@16: ipcdetail::DoOpenOrCreate), perm) Chris@16: {} Chris@16: Chris@16: //!Connects to a created shared memory and its segment manager. Chris@16: //!This can throw. Chris@16: basic_managed_windows_shared_memory Chris@16: (open_only_t, const char* name, const void *addr = 0) Chris@16: : m_wshm(open_only, name, read_write, addr, Chris@16: create_open_func_t(get_this_pointer(), Chris@16: ipcdetail::DoOpen)) Chris@16: {} Chris@16: Chris@16: //!Connects to a created shared memory and its segment manager Chris@16: //!in copy_on_write mode. Chris@16: //!This can throw. Chris@16: basic_managed_windows_shared_memory Chris@16: (open_copy_on_write_t, const char* name, const void *addr = 0) Chris@16: : m_wshm(open_only, name, copy_on_write, addr, Chris@16: create_open_func_t(get_this_pointer(), ipcdetail::DoOpen)) Chris@16: {} Chris@16: Chris@16: //!Connects to a created shared memory and its segment manager Chris@16: //!in read-only mode. Chris@16: //!This can throw. Chris@16: basic_managed_windows_shared_memory Chris@16: (open_read_only_t, const char* name, const void *addr = 0) Chris@16: : base_t() Chris@16: , m_wshm(open_only, name, read_only, addr, Chris@16: create_open_func_t(get_this_pointer(), ipcdetail::DoOpen)) Chris@16: {} Chris@16: Chris@16: //!Moves the ownership of "moved"'s managed memory to *this. Chris@16: //!Does not throw Chris@16: basic_managed_windows_shared_memory Chris@16: (BOOST_RV_REF(basic_managed_windows_shared_memory) moved) Chris@16: { this->swap(moved); } Chris@16: Chris@16: //!Moves the ownership of "moved"'s managed memory to *this. Chris@16: //!Does not throw Chris@16: basic_managed_windows_shared_memory &operator=(BOOST_RV_REF(basic_managed_windows_shared_memory) moved) Chris@16: { Chris@16: basic_managed_windows_shared_memory tmp(boost::move(moved)); Chris@16: this->swap(tmp); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //!Destroys *this and indicates that the calling process is finished using Chris@16: //!the resource. All mapped regions are still valid after Chris@16: //!destruction. When all mapped regions and basic_managed_windows_shared_memory Chris@16: //!objects referring the shared memory are destroyed, the Chris@16: //!operating system will destroy the shared memory. Chris@16: ~basic_managed_windows_shared_memory() Chris@16: {} Chris@16: Chris@16: //!Swaps the ownership of the managed mapped memories managed by *this and other. Chris@16: //!Never throws. Chris@16: void swap(basic_managed_windows_shared_memory &other) Chris@16: { Chris@16: base_t::swap(other); Chris@16: m_wshm.swap(other.m_wshm); Chris@16: } Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: //!Tries to find a previous named allocation address. Returns a memory Chris@16: //!buffer and the object count. If not found returned pointer is 0. Chris@16: //!Never throws. Chris@16: template Chris@16: std::pair find (char_ptr_holder_t name) Chris@16: { Chris@16: if(m_wshm.get_mapped_region().get_mode() == read_only){ Chris@16: return base_t::template find_no_lock(name); Chris@16: } Chris@16: else{ Chris@16: return base_t::template find(name); Chris@16: } Chris@16: } Chris@16: Chris@16: private: Chris@16: typename ipcdetail::wshmem_open_or_create::type m_wshm; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: }; Chris@16: Chris@101: #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@101: Chris@101: //!Typedef for a default basic_managed_windows_shared_memory Chris@101: //!of narrow characters Chris@101: typedef basic_managed_windows_shared_memory Chris@101: Chris@101: ,iset_index> Chris@101: managed_windows_shared_memory; Chris@101: Chris@101: //!Typedef for a default basic_managed_windows_shared_memory Chris@101: //!of wide characters Chris@101: typedef basic_managed_windows_shared_memory Chris@101: Chris@101: ,iset_index> Chris@101: wmanaged_windows_shared_memory; Chris@101: Chris@101: #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@101: Chris@101: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP