Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-21 08:49:40

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