Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:34

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP
0012 #define BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 #  pragma once
0020 #endif
0021 
0022 #include <boost/interprocess/detail/config_begin.hpp>
0023 #include <boost/interprocess/detail/workaround.hpp>
0024 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
0025 #include <boost/interprocess/detail/managed_memory_impl.hpp>
0026 #include <boost/interprocess/creation_tags.hpp>
0027 #include <boost/interprocess/windows_shared_memory.hpp>
0028 #include <boost/interprocess/permissions.hpp>
0029 #include <boost/move/utility_core.hpp>
0030 //These includes needed to fulfill default template parameters of
0031 //predeclarations in interprocess_fwd.hpp
0032 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
0033 #include <boost/interprocess/sync/mutex_family.hpp>
0034 #include <boost/interprocess/indexes/iset_index.hpp>
0035 
0036 namespace boost {
0037 namespace interprocess {
0038 
0039 namespace ipcdetail {
0040 
0041 template
0042       <
0043          class CharType,
0044          class AllocationAlgorithm,
0045          template<class IndexConfig> class IndexType
0046       >
0047 struct wshmem_open_or_create
0048 {
0049    static const std::size_t segment_manager_alignment = boost::move_detail::alignment_of
0050          < segment_manager
0051                < CharType
0052                , AllocationAlgorithm
0053                , IndexType>
0054          >::value;
0055    static const std::size_t final_segment_manager_alignment
0056       = segment_manager_alignment > AllocationAlgorithm::Alignment
0057       ? segment_manager_alignment : AllocationAlgorithm::Alignment;
0058 
0059    typedef  ipcdetail::managed_open_or_create_impl
0060       < windows_shared_memory
0061       , final_segment_manager_alignment
0062       , false
0063       , false> type;
0064 };
0065 
0066 
0067 }  //namespace ipcdetail {
0068 
0069 //!A basic managed windows shared memory creation class. Initializes the
0070 //!shared memory segment. Inherits all basic functionality from
0071 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
0072 //!Unlike basic_managed_shared_memory, it has
0073 //!no kernel persistence and the shared memory is destroyed
0074 //!when all processes destroy all their windows_shared_memory
0075 //!objects and mapped regions for the same shared memory
0076 //!or the processes end/crash.
0077 //!
0078 //!Warning: basic_managed_windows_shared_memory and
0079 //!basic_managed_shared_memory can't communicate between them.
0080 template
0081       <
0082          class CharType,
0083          class AllocationAlgorithm,
0084          template<class IndexConfig> class IndexType
0085       >
0086 class basic_managed_windows_shared_memory
0087    : public ipcdetail::basic_managed_memory_impl
0088       < CharType, AllocationAlgorithm, IndexType
0089       , ipcdetail::wshmem_open_or_create
0090          <CharType, AllocationAlgorithm, IndexType>::type::ManagedOpenOrCreateUserOffset>
0091 {
0092    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0093    private:
0094    typedef ipcdetail::basic_managed_memory_impl
0095       < CharType, AllocationAlgorithm, IndexType
0096       , ipcdetail::wshmem_open_or_create
0097          <CharType, AllocationAlgorithm, IndexType>::type::ManagedOpenOrCreateUserOffset>   base_t;
0098    typedef ipcdetail::create_open_func<base_t>        create_open_func_t;
0099 
0100    basic_managed_windows_shared_memory *get_this_pointer()
0101    {  return this;   }
0102 
0103    private:
0104    typedef typename base_t::char_ptr_holder_t   char_ptr_holder_t;
0105    BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_windows_shared_memory)
0106    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0107 
0108    public: //functions
0109    typedef typename base_t::size_type              size_type;
0110 
0111    //!Default constructor. Does nothing.
0112    //!Useful in combination with move semantics
0113    basic_managed_windows_shared_memory() BOOST_NOEXCEPT
0114    {}
0115 
0116    //!Creates shared memory and creates and places the segment manager.
0117    //!This can throw.
0118    basic_managed_windows_shared_memory
0119       (create_only_t, const char *name,
0120      size_type size, const void *addr = 0, const permissions &perm = permissions())
0121       : m_wshm(create_only, name, size, read_write, addr,
0122                 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
0123    {}
0124 
0125    //!Creates shared memory and creates and places the segment manager if
0126    //!segment was not created. If segment was created it connects to the
0127    //!segment.
0128    //!This can throw.
0129    basic_managed_windows_shared_memory
0130       (open_or_create_t,
0131       const char *name, size_type size,
0132       const void *addr = 0,
0133       const permissions &perm = permissions())
0134       : m_wshm(open_or_create, name, size, read_write, addr,
0135                 create_open_func_t(get_this_pointer(),
0136                 ipcdetail::DoOpenOrCreate), perm)
0137    {}
0138 
0139    //!Connects to a created shared memory and its segment manager.
0140    //!This can throw.
0141    basic_managed_windows_shared_memory
0142       (open_only_t, const char* name, const void *addr = 0)
0143       : m_wshm(open_only, name, read_write, addr,
0144                 create_open_func_t(get_this_pointer(),
0145                 ipcdetail::DoOpen))
0146    {}
0147 
0148    //!Connects to a created shared memory and its segment manager
0149    //!in copy_on_write mode.
0150    //!This can throw.
0151    basic_managed_windows_shared_memory
0152       (open_copy_on_write_t, const char* name, const void *addr = 0)
0153       : m_wshm(open_only, name, copy_on_write, addr,
0154                 create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
0155    {}
0156 
0157    //!Connects to a created shared memory and its segment manager
0158    //!in read-only mode.
0159    //!This can throw.
0160    basic_managed_windows_shared_memory
0161       (open_read_only_t, const char* name, const void *addr = 0)
0162       : base_t()
0163       , m_wshm(open_only, name, read_only, addr,
0164                 create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
0165    {}
0166 
0167    //!Creates shared memory and creates and places the segment manager if
0168    //!segment was not created. If segment was created it connects to the
0169    //!segment.
0170    //!This can throw.
0171    basic_managed_windows_shared_memory
0172       (open_or_create_t,
0173       const wchar_t *name, size_type size,
0174       const void *addr = 0,
0175       const permissions &perm = permissions())
0176       : m_wshm(open_or_create, name, size, read_write, addr,
0177                 create_open_func_t(get_this_pointer(),
0178                 ipcdetail::DoOpenOrCreate), perm)
0179    {}
0180 
0181    //!Connects to a created shared memory and its segment manager.
0182    //!This can throw.
0183    basic_managed_windows_shared_memory
0184       (open_only_t, const wchar_t* name, const void *addr = 0)
0185       : m_wshm(open_only, name, read_write, addr,
0186                 create_open_func_t(get_this_pointer(),
0187                 ipcdetail::DoOpen))
0188    {}
0189 
0190    //!Connects to a created shared memory and its segment manager
0191    //!in copy_on_write mode.
0192    //!This can throw.
0193    basic_managed_windows_shared_memory
0194       (open_copy_on_write_t, const wchar_t* name, const void *addr = 0)
0195       : m_wshm(open_only, name, copy_on_write, addr,
0196                 create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
0197    {}
0198 
0199    //!Connects to a created shared memory and its segment manager
0200    //!in read-only mode.
0201    //!This can throw.
0202    basic_managed_windows_shared_memory
0203       (open_read_only_t, const wchar_t* name, const void *addr = 0)
0204       : base_t()
0205       , m_wshm(open_only, name, read_only, addr,
0206                 create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
0207    {}
0208 
0209    //!Moves the ownership of "moved"'s managed memory to *this.
0210    //!Does not throw
0211    basic_managed_windows_shared_memory
0212       (BOOST_RV_REF(basic_managed_windows_shared_memory) moved) BOOST_NOEXCEPT
0213    {  this->swap(moved);   }
0214 
0215    //!Moves the ownership of "moved"'s managed memory to *this.
0216    //!Does not throw
0217    basic_managed_windows_shared_memory &operator=(BOOST_RV_REF(basic_managed_windows_shared_memory) moved) BOOST_NOEXCEPT
0218    {
0219       basic_managed_windows_shared_memory tmp(boost::move(moved));
0220       this->swap(tmp);
0221       return *this;
0222    }
0223 
0224    //!Destroys *this and indicates that the calling process is finished using
0225    //!the resource. All mapped regions are still valid after
0226    //!destruction. When all mapped regions and basic_managed_windows_shared_memory
0227    //!objects referring the shared memory are destroyed, the
0228    //!operating system will destroy the shared memory.
0229    ~basic_managed_windows_shared_memory()
0230    {}
0231 
0232    //!Swaps the ownership of the managed mapped memories managed by *this and other.
0233    //!Never throws.
0234    void swap(basic_managed_windows_shared_memory &other) BOOST_NOEXCEPT
0235    {
0236       base_t::swap(other);
0237       m_wshm.swap(other.m_wshm);
0238    }
0239 
0240    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0241 
0242    //!Tries to find a previous named allocation address. Returns a memory
0243    //!buffer and the object count. If not found returned pointer is 0.
0244    //!Never throws.
0245    template <class T>
0246    std::pair<T*, size_type> find  (char_ptr_holder_t name)
0247    {
0248       if(m_wshm.get_mapped_region().get_mode() == read_only){
0249          return base_t::template find_no_lock<T>(name);
0250       }
0251       else{
0252          return base_t::template find<T>(name);
0253       }
0254    }
0255 
0256    private:
0257    typename ipcdetail::wshmem_open_or_create
0258       <CharType, AllocationAlgorithm, IndexType>::type m_wshm;
0259    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0260 };
0261 
0262 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0263 
0264 //!Typedef for a default basic_managed_windows_shared_memory
0265 //!of narrow characters
0266 typedef basic_managed_windows_shared_memory
0267    <char
0268    ,rbtree_best_fit<mutex_family>
0269    ,iset_index>
0270 managed_windows_shared_memory;
0271 
0272 //!Typedef for a default basic_managed_windows_shared_memory
0273 //!of wide characters
0274 typedef basic_managed_windows_shared_memory
0275    <wchar_t
0276    ,rbtree_best_fit<mutex_family>
0277    ,iset_index>
0278 wmanaged_windows_shared_memory;
0279 
0280 #endif   //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0281 
0282 
0283 }  //namespace interprocess {
0284 }  //namespace boost {
0285 
0286 #include <boost/interprocess/detail/config_end.hpp>
0287 
0288 #endif   //BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP