Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/interprocess for documentation. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_FILE_MAPPING_HPP Chris@16: #define BOOST_INTERPROCESS_FILE_MAPPING_HPP Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: # Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@101: Chris@16: #include Chris@16: #include Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_MAPPED_FILES) Chris@101: #error "Boost.Interprocess: This platform does not support memory mapped files!" Chris@101: #endif Chris@101: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@16: #include //std::string Chris@16: Chris@16: //!\file Chris@16: //!Describes file_mapping and mapped region classes Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@16: //!A class that wraps a file-mapping that can be used to Chris@16: //!create mapped regions from the mapped files Chris@16: class file_mapping Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: BOOST_MOVABLE_BUT_NOT_COPYABLE(file_mapping) Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: public: Chris@16: //!Constructs an empty file mapping. Chris@16: //!Does not throw Chris@16: file_mapping(); Chris@16: Chris@16: //!Opens a file mapping of file "filename", starting in offset Chris@16: //!"file_offset", and the mapping's size will be "size". The mapping Chris@16: //!can be opened for read-only "read_only" or read-write "read_write" Chris@16: //!modes. Throws interprocess_exception on error. Chris@16: file_mapping(const char *filename, mode_t mode); Chris@16: Chris@16: //!Moves the ownership of "moved"'s file mapping object to *this. Chris@16: //!After the call, "moved" does not represent any file mapping object. Chris@16: //!Does not throw Chris@16: file_mapping(BOOST_RV_REF(file_mapping) moved) Chris@16: : m_handle(file_handle_t(ipcdetail::invalid_file())) Chris@16: , m_mode(read_only) Chris@16: { this->swap(moved); } Chris@16: Chris@16: //!Moves the ownership of "moved"'s file mapping to *this. Chris@16: //!After the call, "moved" does not represent any file mapping. Chris@16: //!Does not throw Chris@16: file_mapping &operator=(BOOST_RV_REF(file_mapping) moved) Chris@16: { Chris@16: file_mapping tmp(boost::move(moved)); Chris@16: this->swap(tmp); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //!Swaps to file_mappings. Chris@16: //!Does not throw. Chris@16: void swap(file_mapping &other); Chris@16: Chris@16: //!Returns access mode Chris@16: //!used in the constructor Chris@16: mode_t get_mode() const; Chris@16: Chris@16: //!Obtains the mapping handle Chris@16: //!to be used with mapped_region Chris@16: mapping_handle_t get_mapping_handle() const; Chris@16: Chris@16: //!Destroys the file mapping. All mapped regions created from this are still Chris@16: //!valid. Does not throw Chris@16: ~file_mapping(); Chris@16: Chris@16: //!Returns the name of the file Chris@16: //!used in the constructor. Chris@16: const char *get_name() const; Chris@16: Chris@16: //!Removes the file named "filename" even if it's been memory mapped. Chris@16: //!Returns true on success. Chris@16: //!The function might fail in some operating systems if the file is Chris@16: //!being used other processes and no deletion permission was shared. Chris@16: static bool remove(const char *filename); Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: //!Closes a previously opened file mapping. Never throws. Chris@16: void priv_close(); Chris@16: file_handle_t m_handle; Chris@16: mode_t m_mode; Chris@16: std::string m_filename; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: }; Chris@16: Chris@16: inline file_mapping::file_mapping() Chris@16: : m_handle(file_handle_t(ipcdetail::invalid_file())) Chris@16: , m_mode(read_only) Chris@16: {} Chris@16: Chris@16: inline file_mapping::~file_mapping() Chris@16: { this->priv_close(); } Chris@16: Chris@16: inline const char *file_mapping::get_name() const Chris@16: { return m_filename.c_str(); } Chris@16: Chris@16: inline void file_mapping::swap(file_mapping &other) Chris@16: { Chris@101: (simple_swap)(m_handle, other.m_handle); Chris@101: (simple_swap)(m_mode, other.m_mode); Chris@16: m_filename.swap(other.m_filename); Chris@16: } Chris@16: Chris@16: inline mapping_handle_t file_mapping::get_mapping_handle() const Chris@16: { return ipcdetail::mapping_handle_from_file_handle(m_handle); } Chris@16: Chris@16: inline mode_t file_mapping::get_mode() const Chris@16: { return m_mode; } Chris@16: Chris@16: inline file_mapping::file_mapping Chris@16: (const char *filename, mode_t mode) Chris@16: : m_filename(filename) Chris@16: { Chris@16: //Check accesses Chris@16: if (mode != read_write && mode != read_only){ Chris@16: error_info err = other_error; Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: Chris@16: //Open file Chris@16: m_handle = ipcdetail::open_existing_file(filename, mode); Chris@16: Chris@16: //Check for error Chris@16: if(m_handle == ipcdetail::invalid_file()){ Chris@16: error_info err = system_error_code(); Chris@16: this->priv_close(); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: m_mode = mode; Chris@16: } Chris@16: Chris@16: inline bool file_mapping::remove(const char *filename) Chris@16: { return ipcdetail::delete_file(filename); } Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: inline void file_mapping::priv_close() Chris@16: { Chris@16: if(m_handle != ipcdetail::invalid_file()){ Chris@16: ipcdetail::close_file(m_handle); Chris@16: m_handle = ipcdetail::invalid_file(); Chris@16: } Chris@16: } Chris@16: Chris@16: //!A class that stores the name of a file Chris@16: //!and tries to remove it in its destructor Chris@16: //!Useful to remove temporary files in the presence Chris@16: //!of exceptions Chris@16: class remove_file_on_destroy Chris@16: { Chris@16: const char * m_name; Chris@16: public: Chris@16: remove_file_on_destroy(const char *name) Chris@16: : m_name(name) Chris@16: {} Chris@16: Chris@16: ~remove_file_on_destroy() Chris@16: { ipcdetail::delete_file(m_name); } Chris@16: }; Chris@16: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@101: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTERPROCESS_FILE_MAPPING_HPP