Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-16 08:49:53

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