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_FILE_MAPPING_HPP
0012 #define BOOST_INTERPROCESS_FILE_MAPPING_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 #if !defined(BOOST_INTERPROCESS_MAPPED_FILES)
0026 #error "Boost.Interprocess: This platform does not support memory mapped files!"
0027 #endif
0028 
0029 #include <boost/interprocess/interprocess_fwd.hpp>
0030 #include <boost/interprocess/exceptions.hpp>
0031 #include <boost/interprocess/detail/utilities.hpp>
0032 #include <boost/interprocess/creation_tags.hpp>
0033 #include <boost/interprocess/detail/os_file_functions.hpp>
0034 #include <boost/interprocess/detail/simple_swap.hpp>
0035 #include <boost/interprocess/detail/char_wchar_holder.hpp>
0036 #include <boost/move/utility_core.hpp>
0037 
0038 //!\file
0039 //!Describes file_mapping and mapped region classes
0040 
0041 namespace boost {
0042 namespace interprocess {
0043 
0044 //!A class that wraps a file-mapping that can be used to
0045 //!create mapped regions from the mapped files
0046 class file_mapping
0047 {
0048    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0049    BOOST_MOVABLE_BUT_NOT_COPYABLE(file_mapping)
0050    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0051 
0052    public:
0053    //!Constructs an empty file mapping.
0054    //!Does not throw
0055    file_mapping() BOOST_NOEXCEPT;
0056 
0057    //!Opens a file mapping of file "filename", starting in offset
0058    //!"file_offset", and the mapping's size will be "size". The mapping
0059    //!can be opened for read-only "read_only" or read-write "read_write"
0060    //!modes. Throws interprocess_exception on error.
0061    file_mapping(const char *filename, mode_t mode);
0062 
0063    #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0064    //!Opens a file mapping of file "filename", starting in offset
0065    //!"file_offset", and the mapping's size will be "size". The mapping
0066    //!can be opened for read-only "read_only" or read-write "read_write"
0067    //!modes. Throws interprocess_exception on error.
0068    //! 
0069    //!Note: This function is only available on operating systems with
0070    //!      native wchar_t APIs (e.g. Windows).
0071    file_mapping(const wchar_t *filename, mode_t mode);
0072    #endif
0073 
0074    //!Moves the ownership of "moved"'s file mapping object to *this.
0075    //!After the call, "moved" does not represent any file mapping object.
0076    //!Does not throw
0077    file_mapping(BOOST_RV_REF(file_mapping) moved) BOOST_NOEXCEPT
0078       :  m_handle(file_handle_t(ipcdetail::invalid_file()))
0079       ,  m_mode(read_only)
0080    {  this->swap(moved);   }
0081 
0082    //!Moves the ownership of "moved"'s file mapping to *this.
0083    //!After the call, "moved" does not represent any file mapping.
0084    //!Does not throw
0085    file_mapping &operator=(BOOST_RV_REF(file_mapping) moved) BOOST_NOEXCEPT
0086    {
0087       file_mapping tmp(boost::move(moved));
0088       this->swap(tmp);
0089       return *this;
0090    }
0091 
0092    //!Swaps to file_mappings.
0093    //!Does not throw.
0094    void swap(file_mapping &other) BOOST_NOEXCEPT;
0095 
0096    //!Returns access mode
0097    //!used in the constructor
0098    mode_t get_mode() const BOOST_NOEXCEPT;
0099 
0100    //!Obtains the mapping handle
0101    //!to be used with mapped_region
0102    mapping_handle_t get_mapping_handle() const BOOST_NOEXCEPT;
0103 
0104    //!Destroys the file mapping. All mapped regions created from this are still
0105    //!valid. Does not throw
0106    ~file_mapping();
0107 
0108    //!Returns the name of the file
0109    //!used in the constructor.
0110    const char *get_name() const BOOST_NOEXCEPT;
0111 
0112    //!Removes the file named "filename" even if it's been memory mapped.
0113    //!Returns true on success.
0114    //!The function might fail in some operating systems if the file is
0115    //!being used other processes and no deletion permission was shared.
0116    static bool remove(const char *filename);
0117 
0118    #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0119    //!Removes the file named "filename" even if it's been memory mapped.
0120    //!Returns true on success.
0121    //!The function might fail in some operating systems if the file is
0122    //!being used other processes and no deletion permission was shared.
0123    //! 
0124    //!Note: This function is only available on operating systems with
0125    //!      native wchar_t APIs (e.g. Windows).
0126    static bool remove(const wchar_t *filename);
0127    #endif
0128 
0129    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0130    private:
0131    //!Closes a previously opened file mapping. Never throws.
0132    void priv_close();
0133    file_handle_t  m_handle;
0134    mode_t         m_mode;
0135    char_wchar_holder    m_filename;
0136    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0137 };
0138 
0139 inline file_mapping::file_mapping() BOOST_NOEXCEPT
0140    :  m_handle(file_handle_t(ipcdetail::invalid_file()))
0141    ,  m_mode(read_only)
0142 {}
0143 
0144 inline file_mapping::~file_mapping()
0145 {  this->priv_close(); }
0146 
0147 inline const char *file_mapping::get_name() const BOOST_NOEXCEPT
0148 {  return m_filename.getn(); }
0149 
0150 inline void file_mapping::swap(file_mapping &other) BOOST_NOEXCEPT
0151 {
0152    (simple_swap)(m_handle, other.m_handle);
0153    (simple_swap)(m_mode, other.m_mode);
0154    m_filename.swap(other.m_filename);
0155 }
0156 
0157 inline mapping_handle_t file_mapping::get_mapping_handle() const BOOST_NOEXCEPT
0158 {  return ipcdetail::mapping_handle_from_file_handle(m_handle);  }
0159 
0160 inline mode_t file_mapping::get_mode() const BOOST_NOEXCEPT
0161 {  return m_mode; }
0162 
0163 inline file_mapping::file_mapping
0164    (const char *filename, mode_t mode)
0165    :  m_filename(filename)
0166 {
0167    //Check accesses
0168    if (mode != read_write && mode != read_only){
0169       error_info err = other_error;
0170       throw interprocess_exception(err);
0171    }
0172 
0173    //Open file
0174    m_handle = ipcdetail::open_existing_file(filename, mode);
0175 
0176    //Check for error
0177    if(m_handle == ipcdetail::invalid_file()){
0178       error_info err = system_error_code();
0179       this->priv_close();
0180       throw interprocess_exception(err);
0181    }
0182    m_mode = mode;
0183 }
0184 
0185 inline bool file_mapping::remove(const char *filename)
0186 {  return ipcdetail::delete_file(filename);  }
0187 
0188 #ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES
0189 inline file_mapping::file_mapping
0190    (const wchar_t *filename, mode_t mode)
0191    :  m_filename(filename)
0192 {
0193    //Check accesses
0194    if (mode != read_write && mode != read_only){
0195       error_info err = other_error;
0196       throw interprocess_exception(err);
0197    }
0198 
0199    //Open file
0200    m_handle = ipcdetail::open_existing_file(filename, mode);
0201 
0202    //Check for error
0203    if(m_handle == ipcdetail::invalid_file()){
0204       error_info err = system_error_code();
0205       this->priv_close();
0206       throw interprocess_exception(err);
0207    }
0208    m_mode = mode;
0209 }
0210 
0211 inline bool file_mapping::remove(const wchar_t *filename)
0212 {  return ipcdetail::delete_file(filename);  }
0213 
0214 #endif
0215 
0216 
0217 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0218 
0219 inline void file_mapping::priv_close()
0220 {
0221    if(m_handle != ipcdetail::invalid_file()){
0222       ipcdetail::close_file(m_handle);
0223       m_handle = ipcdetail::invalid_file();
0224    }
0225 }
0226 
0227 //!A class that stores the name of a file
0228 //!and tries to remove it in its destructor
0229 //!Useful to remove temporary files in the presence
0230 //!of exceptions
0231 class remove_file_on_destroy
0232 {
0233    const char * m_name;
0234    public:
0235    remove_file_on_destroy(const char *name)
0236       :  m_name(name)
0237    {}
0238 
0239    ~remove_file_on_destroy()
0240    {  ipcdetail::delete_file(m_name);  }
0241 };
0242 
0243 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0244 
0245 }  //namespace interprocess {
0246 }  //namespace boost {
0247 
0248 #include <boost/interprocess/detail/config_end.hpp>
0249 
0250 #endif   //BOOST_INTERPROCESS_FILE_MAPPING_HPP