1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_FILE_MAPPING_HPP
12 #define BOOST_INTERPROCESS_FILE_MAPPING_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
25 #if !defined(BOOST_INTERPROCESS_MAPPED_FILES)
26 #error "Boost.Interprocess: This platform does not support memory mapped files!"
29 #include <boost/interprocess/interprocess_fwd.hpp>
30 #include <boost/interprocess/exceptions.hpp>
31 #include <boost/interprocess/detail/utilities.hpp>
32 #include <boost/interprocess/creation_tags.hpp>
33 #include <boost/interprocess/detail/os_file_functions.hpp>
34 #include <boost/interprocess/detail/simple_swap.hpp>
35 #include <boost/move/utility_core.hpp>
36 #include <string> //std::string
39 //!Describes file_mapping and mapped region classes
42 namespace interprocess {
44 //!A class that wraps a file-mapping that can be used to
45 //!create mapped regions from the mapped files
48 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
49 BOOST_MOVABLE_BUT_NOT_COPYABLE(file_mapping)
50 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
53 //!Constructs an empty file mapping.
57 //!Opens a file mapping of file "filename", starting in offset
58 //!"file_offset", and the mapping's size will be "size". The mapping
59 //!can be opened for read-only "read_only" or read-write "read_write"
60 //!modes. Throws interprocess_exception on error.
61 file_mapping(const char *filename, mode_t mode);
63 //!Moves the ownership of "moved"'s file mapping object to *this.
64 //!After the call, "moved" does not represent any file mapping object.
66 file_mapping(BOOST_RV_REF(file_mapping) moved)
67 : m_handle(file_handle_t(ipcdetail::invalid_file()))
69 { this->swap(moved); }
71 //!Moves the ownership of "moved"'s file mapping to *this.
72 //!After the call, "moved" does not represent any file mapping.
74 file_mapping &operator=(BOOST_RV_REF(file_mapping) moved)
76 file_mapping tmp(boost::move(moved));
81 //!Swaps to file_mappings.
83 void swap(file_mapping &other);
85 //!Returns access mode
86 //!used in the constructor
87 mode_t get_mode() const;
89 //!Obtains the mapping handle
90 //!to be used with mapped_region
91 mapping_handle_t get_mapping_handle() const;
93 //!Destroys the file mapping. All mapped regions created from this are still
94 //!valid. Does not throw
97 //!Returns the name of the file
98 //!used in the constructor.
99 const char *get_name() const;
101 //!Removes the file named "filename" even if it's been memory mapped.
102 //!Returns true on success.
103 //!The function might fail in some operating systems if the file is
104 //!being used other processes and no deletion permission was shared.
105 static bool remove(const char *filename);
107 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
109 //!Closes a previously opened file mapping. Never throws.
111 file_handle_t m_handle;
113 std::string m_filename;
114 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
117 inline file_mapping::file_mapping()
118 : m_handle(file_handle_t(ipcdetail::invalid_file()))
122 inline file_mapping::~file_mapping()
123 { this->priv_close(); }
125 inline const char *file_mapping::get_name() const
126 { return m_filename.c_str(); }
128 inline void file_mapping::swap(file_mapping &other)
130 (simple_swap)(m_handle, other.m_handle);
131 (simple_swap)(m_mode, other.m_mode);
132 m_filename.swap(other.m_filename);
135 inline mapping_handle_t file_mapping::get_mapping_handle() const
136 { return ipcdetail::mapping_handle_from_file_handle(m_handle); }
138 inline mode_t file_mapping::get_mode() const
141 inline file_mapping::file_mapping
142 (const char *filename, mode_t mode)
143 : m_filename(filename)
146 if (mode != read_write && mode != read_only){
147 error_info err = other_error;
148 throw interprocess_exception(err);
152 m_handle = ipcdetail::open_existing_file(filename, mode);
155 if(m_handle == ipcdetail::invalid_file()){
156 error_info err = system_error_code();
158 throw interprocess_exception(err);
163 inline bool file_mapping::remove(const char *filename)
164 { return ipcdetail::delete_file(filename); }
166 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
168 inline void file_mapping::priv_close()
170 if(m_handle != ipcdetail::invalid_file()){
171 ipcdetail::close_file(m_handle);
172 m_handle = ipcdetail::invalid_file();
176 //!A class that stores the name of a file
177 //!and tries to remove it in its destructor
178 //!Useful to remove temporary files in the presence
180 class remove_file_on_destroy
184 remove_file_on_destroy(const char *name)
188 ~remove_file_on_destroy()
189 { ipcdetail::delete_file(m_name); }
192 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
194 } //namespace interprocess {
195 } //namespace boost {
197 #include <boost/interprocess/detail/config_end.hpp>
199 #endif //BOOST_INTERPROCESS_FILE_MAPPING_HPP