1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2008. 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 #include <boost/interprocess/detail/config_begin.hpp>
15 #include <boost/interprocess/detail/workaround.hpp>
17 #include <boost/interprocess/interprocess_fwd.hpp>
18 #include <boost/interprocess/exceptions.hpp>
19 #include <boost/interprocess/detail/utilities.hpp>
20 #include <boost/interprocess/creation_tags.hpp>
21 #include <boost/interprocess/detail/os_file_functions.hpp>
22 #include <boost/interprocess/detail/move.hpp>
23 #include <string> //std::string
26 //!Describes file_mapping and mapped region classes
29 namespace interprocess
{
31 //!A class that wraps a file-mapping that can be used to
32 //!create mapped regions from the mapped files
36 //Non-copyable and non-assignable
37 file_mapping(file_mapping
&);
38 file_mapping
&operator=(file_mapping
&);
42 BOOST_INTERPROCESS_ENABLE_MOVE_EMULATION(file_mapping
)
44 //!Constructs an empty file mapping.
48 //!Opens a file mapping of file "filename", starting in offset
49 //!"file_offset", and the mapping's size will be "size". The mapping
50 //!can be opened for read-only "read_only" or read-write "read_write"
51 //!modes. Throws interprocess_exception on error.
52 file_mapping(const char *filename
, mode_t mode
);
54 //!Moves the ownership of "moved"'s file mapping object to *this.
55 //!After the call, "moved" does not represent any file mapping object.
57 file_mapping(BOOST_INTERPROCESS_RV_REF(file_mapping
) moved
)
58 : m_handle(file_handle_t(detail::invalid_file()))
59 { this->swap(moved
); }
61 //!Moves the ownership of "moved"'s file mapping to *this.
62 //!After the call, "moved" does not represent any file mapping.
64 file_mapping
&operator=(BOOST_INTERPROCESS_RV_REF(file_mapping
) moved
)
66 file_mapping
tmp(boost::interprocess::move(moved
));
71 //!Swaps to file_mappings.
73 void swap(file_mapping
&other
);
75 //!Returns access mode
76 //!used in the constructor
77 mode_t
get_mode() const;
79 //!Obtains the mapping handle
80 //!to be used with mapped_region
81 mapping_handle_t
get_mapping_handle() const;
83 //!Destroys the file mapping. All mapped regions created from this are still
84 //!valid. Does not throw
87 //!Returns the name of the file
88 //!used in the constructor.
89 const char *get_name() const;
91 //!Removes the file named "filename" even if it's been memory mapped.
92 //!Returns true on success.
93 //!The function might fail in some operating systems if the file is
94 //!being used other processes and no deletion permission was shared.
95 static bool remove(const char *filename
);
99 //!Closes a previously opened file mapping. Never throws.
101 file_handle_t m_handle
;
103 std::string m_filename
;
107 inline file_mapping::file_mapping()
108 : m_handle(file_handle_t(detail::invalid_file()))
111 inline file_mapping::~file_mapping()
112 { this->priv_close(); }
114 inline const char *file_mapping::get_name() const
115 { return m_filename
.c_str(); }
117 inline void file_mapping::swap(file_mapping
&other
)
119 std::swap(m_handle
, other
.m_handle
);
120 std::swap(m_mode
, other
.m_mode
);
121 m_filename
.swap(other
.m_filename
);
124 inline mapping_handle_t
file_mapping::get_mapping_handle() const
125 { return detail::mapping_handle_from_file_handle(m_handle
); }
127 inline mode_t
file_mapping::get_mode() const
130 inline file_mapping::file_mapping
131 (const char *filename
, mode_t mode
)
132 : m_filename(filename
)
135 if (mode
!= read_write
&& mode
!= read_only
){
136 error_info err
= other_error
;
137 throw interprocess_exception(err
);
141 m_handle
= detail::open_existing_file(filename
, mode
);
144 if(m_handle
== detail::invalid_file()){
145 error_info err
= system_error_code();
147 throw interprocess_exception(err
);
152 inline bool file_mapping::remove(const char *filename
)
153 { return detail::delete_file(filename
); }
157 inline void file_mapping::priv_close()
159 if(m_handle
!= detail::invalid_file()){
160 detail::close_file(m_handle
);
161 m_handle
= detail::invalid_file();
167 //!A class that stores the name of a file
168 //!and tries to remove it in its destructor
169 //!Useful to remove temporary files in the presence
171 class remove_file_on_destroy
175 remove_file_on_destroy(const char *name
)
179 ~remove_file_on_destroy()
180 { detail::delete_file(m_name
); }
183 } //namespace interprocess {
184 } //namespace boost {
186 #include <boost/interprocess/detail/config_end.hpp>
188 #endif //BOOST_INTERPROCESS_FILE_MAPPING_HPP