Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:37:09

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_SHARED_MEMORY_HPP
0012 #define BOOST_INTERPROCESS_MANAGED_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 
0025 #include <boost/interprocess/detail/managed_memory_impl.hpp>
0026 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
0027 #include <boost/interprocess/shared_memory_object.hpp>
0028 #include <boost/interprocess/creation_tags.hpp>
0029 #include <boost/interprocess/permissions.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 
0035 namespace boost {
0036 namespace interprocess {
0037 
0038 namespace ipcdetail {
0039 
0040 template
0041       <
0042          class CharType,
0043          class AllocationAlgorithm,
0044          template<class IndexConfig> class IndexType
0045       >
0046 struct shmem_open_or_create
0047 {
0048    static const std::size_t segment_manager_alignment = boost::move_detail::alignment_of
0049          < segment_manager
0050                < CharType
0051                , AllocationAlgorithm
0052                , IndexType>
0053          >::value;
0054    static const std::size_t final_segment_manager_alignment
0055       = segment_manager_alignment > AllocationAlgorithm::Alignment
0056       ? segment_manager_alignment : AllocationAlgorithm::Alignment;
0057 
0058    typedef ipcdetail::managed_open_or_create_impl
0059       < shared_memory_object
0060       , final_segment_manager_alignment
0061       , true
0062       , false> type;
0063 };
0064 
0065 }  //namespace ipcdetail {
0066 
0067 //!A basic shared memory named object creation class. Initializes the
0068 //!shared memory segment. Inherits all basic functionality from
0069 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
0070 template
0071       <
0072          class CharType,
0073          class AllocationAlgorithm,
0074          template<class IndexConfig> class IndexType
0075       >
0076 class basic_managed_shared_memory
0077    : public ipcdetail::basic_managed_memory_impl
0078       < CharType, AllocationAlgorithm, IndexType
0079       , ipcdetail::shmem_open_or_create<CharType, AllocationAlgorithm, IndexType>::type::ManagedOpenOrCreateUserOffset>
0080    , private ipcdetail::shmem_open_or_create<CharType, AllocationAlgorithm, IndexType>::type
0081 {
0082    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0083    typedef typename ipcdetail::shmem_open_or_create
0084       < CharType
0085       , AllocationAlgorithm
0086       , IndexType>::type                                 base2_t;
0087    typedef ipcdetail::basic_managed_memory_impl
0088       <CharType, AllocationAlgorithm, IndexType,
0089       base2_t::ManagedOpenOrCreateUserOffset>            base_t;
0090 
0091    typedef ipcdetail::create_open_func<base_t>        create_open_func_t;
0092 
0093    basic_managed_shared_memory *get_this_pointer()
0094    {  return this;   }
0095 
0096    public:
0097    typedef shared_memory_object                    device_type;
0098    typedef typename base_t::size_type              size_type;
0099 
0100    private:
0101    typedef typename base_t::char_ptr_holder_t   char_ptr_holder_t;
0102    BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_shared_memory)
0103    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0104 
0105    public: //functions
0106 
0107    //!Destroys *this and indicates that the calling process is finished using
0108    //!the resource. The destructor function will deallocate
0109    //!any system resources allocated by the system for use by this process for
0110    //!this resource. The resource can still be opened again calling
0111    //!the open constructor overload. To erase the resource from the system
0112    //!use remove().
0113    ~basic_managed_shared_memory()
0114    {}
0115 
0116    //!Default constructor. Does nothing.
0117    //!Useful in combination with move semantics
0118    basic_managed_shared_memory()
0119    {}
0120 
0121    //!Creates shared memory and creates and places the segment manager.
0122    //!This can throw.
0123    basic_managed_shared_memory(create_only_t, const char *name,
0124                              size_type size, const void *addr = 0, const permissions& perm = permissions())
0125       : base_t()
0126       , base2_t(create_only, name, size, read_write, addr,
0127                 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
0128    {}
0129 
0130    //!Creates shared memory and creates and places the segment manager if
0131    //!segment was not created. If segment was created it connects to the
0132    //!segment.
0133    //!This can throw.
0134    basic_managed_shared_memory (open_or_create_t,
0135                               const char *name, size_type size,
0136                               const void *addr = 0, const permissions& perm = permissions())
0137       : base_t()
0138       , base2_t(open_or_create, name, size, read_write, addr,
0139                 create_open_func_t(get_this_pointer(),
0140                 ipcdetail::DoOpenOrCreate), perm)
0141    {}
0142 
0143    //!Connects to a created shared memory and its segment manager.
0144    //!in copy_on_write mode.
0145    //!This can throw.
0146    basic_managed_shared_memory (open_copy_on_write_t, const char* name,
0147                                 const void *addr = 0)
0148       : base_t()
0149       , base2_t(open_only, name, copy_on_write, addr,
0150                 create_open_func_t(get_this_pointer(),
0151                 ipcdetail::DoOpen))
0152    {}
0153 
0154    //!Connects to a created shared memory and its segment manager.
0155    //!in read-only mode.
0156    //!This can throw.
0157    basic_managed_shared_memory (open_read_only_t, const char* name,
0158                                 const void *addr = 0)
0159       : base_t()
0160       , base2_t(open_only, name, read_only, addr,
0161                 create_open_func_t(get_this_pointer(),
0162                 ipcdetail::DoOpen))
0163    {}
0164 
0165    //!Connects to a created shared memory and its segment manager.
0166    //!This can throw.
0167    basic_managed_shared_memory (open_only_t, const char* name,
0168                                 const void *addr = 0)
0169       : base_t()
0170       , base2_t(open_only, name, read_write, addr,
0171                 create_open_func_t(get_this_pointer(),
0172                 ipcdetail::DoOpen))
0173    {}
0174 
0175    #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0176 
0177    //!Creates shared memory and creates and places the segment manager.
0178    //!This can throw.
0179    //! 
0180    //!Note: This function is only available on operating systems with
0181    //!      native wchar_t APIs (e.g. Windows).
0182    basic_managed_shared_memory(create_only_t, const wchar_t *name,
0183                              size_type size, const void *addr = 0, const permissions& perm = permissions())
0184       : base_t()
0185       , base2_t(create_only, name, size, read_write, addr,
0186                 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
0187    {}
0188 
0189    //!Creates shared memory and creates and places the segment manager if
0190    //!segment was not created. If segment was created it connects to the
0191    //!segment.
0192    //!This can throw.
0193    //! 
0194    //!Note: This function is only available on operating systems with
0195    //!      native wchar_t APIs (e.g. Windows).
0196    basic_managed_shared_memory (open_or_create_t,
0197                               const wchar_t *name, size_type size,
0198                               const void *addr = 0, const permissions& perm = permissions())
0199       : base_t()
0200       , base2_t(open_or_create, name, size, read_write, addr,
0201                 create_open_func_t(get_this_pointer(),
0202                 ipcdetail::DoOpenOrCreate), perm)
0203    {}
0204 
0205    //!Connects to a created shared memory and its segment manager.
0206    //!in copy_on_write mode.
0207    //!This can throw.
0208    //! 
0209    //!Note: This function is only available on operating systems with
0210    //!      native wchar_t APIs (e.g. Windows).
0211    basic_managed_shared_memory (open_copy_on_write_t, const wchar_t* name,
0212                                 const void *addr = 0)
0213       : base_t()
0214       , base2_t(open_only, name, copy_on_write, addr,
0215                 create_open_func_t(get_this_pointer(),
0216                 ipcdetail::DoOpen))
0217    {}
0218 
0219    //!Connects to a created shared memory and its segment manager.
0220    //!in read-only mode.
0221    //!This can throw.
0222    //! 
0223    //!Note: This function is only available on operating systems with
0224    //!      native wchar_t APIs (e.g. Windows).
0225    basic_managed_shared_memory (open_read_only_t, const wchar_t* name,
0226                                 const void *addr = 0)
0227       : base_t()
0228       , base2_t(open_only, name, read_only, addr,
0229                 create_open_func_t(get_this_pointer(),
0230                 ipcdetail::DoOpen))
0231    {}
0232 
0233    //!Connects to a created shared memory and its segment manager.
0234    //!This can throw.
0235    //! 
0236    //!Note: This function is only available on operating systems with
0237    //!      native wchar_t APIs (e.g. Windows).
0238    basic_managed_shared_memory (open_only_t, const wchar_t* name,
0239                                 const void *addr = 0)
0240       : base_t()
0241       , base2_t(open_only, name, read_write, addr,
0242                 create_open_func_t(get_this_pointer(),
0243                 ipcdetail::DoOpen))
0244    {}
0245 
0246    #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0247 
0248    //!Moves the ownership of "moved"'s managed memory to *this.
0249    //!Does not throw
0250    basic_managed_shared_memory(BOOST_RV_REF(basic_managed_shared_memory) moved)
0251    {
0252       basic_managed_shared_memory tmp;
0253       this->swap(moved);
0254       tmp.swap(moved);
0255    }
0256 
0257    //!Moves the ownership of "moved"'s managed memory to *this.
0258    //!Does not throw
0259    basic_managed_shared_memory &operator=(BOOST_RV_REF(basic_managed_shared_memory) moved)
0260    {
0261       basic_managed_shared_memory tmp(boost::move(moved));
0262       this->swap(tmp);
0263       return *this;
0264    }
0265 
0266    //!Swaps the ownership of the managed shared memories managed by *this and other.
0267    //!Never throws.
0268    void swap(basic_managed_shared_memory &other)
0269    {
0270       base_t::swap(other);
0271       base2_t::swap(other);
0272    }
0273 
0274    //!Tries to resize the managed shared memory object so that we have
0275    //!room for more objects.
0276    //!
0277    //!This function is not synchronized so no other thread or process should
0278    //!be reading or writing the file
0279    //!
0280    //!Since the memory will be remapped after the underlying shared memory
0281    //!is grown, it can't work with segments using raw pointers.
0282    static bool grow(const char *shmname, size_type extra_bytes)
0283    {
0284       return base_t::template grow
0285          <basic_managed_shared_memory>(shmname, extra_bytes);
0286    }
0287 
0288    //!Tries to resize the managed shared memory to minimized the size of the file.
0289    //!
0290    //!This function is not synchronized so no other thread or process should
0291    //!be reading or writing the file
0292    static bool shrink_to_fit(const char *shmname)
0293    {
0294       return base_t::template shrink_to_fit
0295          <basic_managed_shared_memory>(shmname);
0296    }
0297 
0298    #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0299 
0300    //!Tries to resize the managed shared memory object so that we have
0301    //!room for more objects.
0302    //!
0303    //!This function is not synchronized so no other thread or process should
0304    //!be reading or writing the file
0305    //! 
0306    //!Note: This function is only available on operating systems with
0307    //!      native wchar_t APIs (e.g. Windows).
0308    static bool grow(const wchar_t *shmname, size_type extra_bytes)
0309    {
0310       return base_t::template grow
0311          <basic_managed_shared_memory>(shmname, extra_bytes);
0312    }
0313 
0314    //!Tries to resize the managed shared memory to minimized the size of the file.
0315    //!
0316    //!This function is not synchronized so no other thread or process should
0317    //!be reading or writing the file
0318    //! 
0319    //!Note: This function is only available on operating systems with
0320    //!      native wchar_t APIs (e.g. Windows).
0321    static bool shrink_to_fit(const wchar_t *shmname)
0322    {
0323       return base_t::template shrink_to_fit
0324          <basic_managed_shared_memory>(shmname);
0325    }
0326 
0327    #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0328 
0329    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0330 
0331    //!Tries to find a previous named allocation address. Returns a memory
0332    //!buffer and the object count. If not found returned pointer is 0.
0333    //!Never throws.
0334    template <class T>
0335    std::pair<T*, size_type> find  (char_ptr_holder_t name)
0336    {
0337       if(base2_t::get_mapped_region().get_mode() == read_only){
0338          return base_t::template find_no_lock<T>(name);
0339       }
0340       else{
0341          return base_t::template find<T>(name);
0342       }
0343    }
0344 
0345    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0346 };
0347 
0348 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0349 
0350 //!Typedef for a default basic_managed_shared_memory
0351 //!of narrow characters
0352 typedef basic_managed_shared_memory
0353    <char
0354    ,rbtree_best_fit<mutex_family>
0355    ,iset_index>
0356 managed_shared_memory;
0357 
0358 //!Typedef for a default basic_managed_shared_memory
0359 //!of wide characters
0360 typedef basic_managed_shared_memory
0361    <wchar_t
0362    ,rbtree_best_fit<mutex_family>
0363    ,iset_index>
0364 wmanaged_shared_memory;
0365 
0366 //!Typedef for a default basic_managed_shared_memory
0367 //!of narrow characters to be placed in a fixed address
0368 typedef basic_managed_shared_memory
0369    <char
0370    ,rbtree_best_fit<mutex_family, void*>
0371    ,iset_index>
0372 fixed_managed_shared_memory;
0373 
0374 //!Typedef for a default basic_managed_shared_memory
0375 //!of narrow characters to be placed in a fixed address
0376 typedef basic_managed_shared_memory
0377    <wchar_t
0378    ,rbtree_best_fit<mutex_family, void*>
0379    ,iset_index>
0380 wfixed_managed_shared_memory;
0381 
0382 
0383 #endif   //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0384 
0385 }  //namespace interprocess {
0386 }  //namespace boost {
0387 
0388 #include <boost/interprocess/detail/config_end.hpp>
0389 
0390 #endif   //BOOST_INTERPROCESS_MANAGED_SHARED_MEMORY_HPP
0391