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_SHARED_MEMORY_HPP Chris@16: #define BOOST_INTERPROCESS_MANAGED_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: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #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: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@16: namespace ipcdetail { Chris@16: Chris@16: template Chris@16: struct shmem_open_or_create Chris@16: { Chris@16: typedef ipcdetail::managed_open_or_create_impl Chris@16: < shared_memory_object, AllocationAlgorithm::Alignment, true, false> type; Chris@16: }; Chris@16: Chris@16: } //namespace ipcdetail { Chris@16: Chris@16: //!A basic shared memory named object creation class. Initializes the Chris@16: //!shared memory segment. Inherits all basic functionality from Chris@16: //!basic_managed_memory_impl*/ 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_shared_memory Chris@16: : public ipcdetail::basic_managed_memory_impl Chris@16: ::type::ManagedOpenOrCreateUserOffset> Chris@16: , private ipcdetail::shmem_open_or_create::type Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: typedef ipcdetail::basic_managed_memory_impl Chris@16: ::type::ManagedOpenOrCreateUserOffset> base_t; Chris@16: typedef typename ipcdetail::shmem_open_or_create::type base2_t; Chris@16: Chris@16: typedef ipcdetail::create_open_func create_open_func_t; Chris@16: Chris@16: basic_managed_shared_memory *get_this_pointer() Chris@16: { return this; } Chris@16: Chris@16: public: Chris@16: typedef shared_memory_object device_type; Chris@16: typedef typename base_t::size_type size_type; 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_shared_memory) Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: public: //functions Chris@16: Chris@16: //!Destroys *this and indicates that the calling process is finished using Chris@16: //!the resource. The destructor function will deallocate Chris@16: //!any system resources allocated by the system for use by this process for Chris@16: //!this resource. The resource can still be opened again calling Chris@16: //!the open constructor overload. To erase the resource from the system Chris@16: //!use remove(). Chris@16: ~basic_managed_shared_memory() Chris@16: {} Chris@16: Chris@16: //!Default constructor. Does nothing. Chris@16: //!Useful in combination with move semantics Chris@16: basic_managed_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_shared_memory(create_only_t, const char *name, Chris@16: size_type size, const void *addr = 0, const permissions& perm = permissions()) Chris@16: : base_t() Chris@16: , base2_t(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_shared_memory (open_or_create_t, Chris@16: const char *name, size_type size, Chris@16: const void *addr = 0, const permissions& perm = permissions()) Chris@16: : base_t() Chris@16: , base2_t(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: //!in copy_on_write mode. Chris@16: //!This can throw. Chris@16: basic_managed_shared_memory (open_copy_on_write_t, const char* name, Chris@16: const void *addr = 0) Chris@16: : base_t() Chris@16: , base2_t(open_only, name, copy_on_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 read-only mode. Chris@16: //!This can throw. Chris@16: basic_managed_shared_memory (open_read_only_t, const char* name, Chris@16: const void *addr = 0) Chris@16: : base_t() Chris@16: , base2_t(open_only, name, read_only, 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: //!This can throw. Chris@16: basic_managed_shared_memory (open_only_t, const char* name, Chris@16: const void *addr = 0) Chris@16: : base_t() Chris@16: , base2_t(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: //!Moves the ownership of "moved"'s managed memory to *this. Chris@16: //!Does not throw Chris@16: basic_managed_shared_memory(BOOST_RV_REF(basic_managed_shared_memory) moved) Chris@16: { Chris@16: basic_managed_shared_memory tmp; Chris@16: this->swap(moved); Chris@16: tmp.swap(moved); 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_shared_memory &operator=(BOOST_RV_REF(basic_managed_shared_memory) moved) Chris@16: { Chris@16: basic_managed_shared_memory tmp(boost::move(moved)); Chris@16: this->swap(tmp); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //!Swaps the ownership of the managed shared memories managed by *this and other. Chris@16: //!Never throws. Chris@16: void swap(basic_managed_shared_memory &other) Chris@16: { Chris@16: base_t::swap(other); Chris@16: base2_t::swap(other); Chris@16: } Chris@16: Chris@16: //!Tries to resize the managed shared memory object so that we have Chris@16: //!room for more objects. Chris@16: //! Chris@16: //!This function is not synchronized so no other thread or process should Chris@16: //!be reading or writing the file Chris@16: static bool grow(const char *shmname, size_type extra_bytes) Chris@16: { Chris@16: return base_t::template grow Chris@16: (shmname, extra_bytes); Chris@16: } Chris@16: Chris@16: //!Tries to resize the managed shared memory to minimized the size of the file. Chris@16: //! Chris@16: //!This function is not synchronized so no other thread or process should Chris@16: //!be reading or writing the file Chris@16: static bool shrink_to_fit(const char *shmname) Chris@16: { Chris@16: return base_t::template shrink_to_fit Chris@16: (shmname); 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(base2_t::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@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_shared_memory Chris@101: //!of narrow characters Chris@101: typedef basic_managed_shared_memory Chris@101: Chris@101: ,iset_index> Chris@101: managed_shared_memory; Chris@101: Chris@101: //!Typedef for a default basic_managed_shared_memory Chris@101: //!of wide characters Chris@101: typedef basic_managed_shared_memory Chris@101: Chris@101: ,iset_index> Chris@101: wmanaged_shared_memory; Chris@101: Chris@101: //!Typedef for a default basic_managed_shared_memory Chris@101: //!of narrow characters to be placed in a fixed address Chris@101: typedef basic_managed_shared_memory Chris@101: Chris@101: ,iset_index> Chris@101: fixed_managed_shared_memory; Chris@101: Chris@101: //!Typedef for a default basic_managed_shared_memory Chris@101: //!of narrow characters to be placed in a fixed address Chris@101: typedef basic_managed_shared_memory Chris@101: Chris@101: ,iset_index> Chris@101: wfixed_managed_shared_memory; Chris@101: Chris@101: Chris@101: #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@101: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTERPROCESS_MANAGED_SHARED_MEMORY_HPP Chris@16: