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