Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_INTERPROCESS_FILE_MAPPING_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_FILE_MAPPING_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
15 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 #include <boost/interprocess/interprocess_fwd.hpp>
|
Chris@16
|
18 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
19 #include <boost/interprocess/detail/utilities.hpp>
|
Chris@16
|
20 #include <boost/interprocess/creation_tags.hpp>
|
Chris@16
|
21 #include <boost/interprocess/detail/os_file_functions.hpp>
|
Chris@16
|
22 #include <boost/move/move.hpp>
|
Chris@16
|
23 #include <string> //std::string
|
Chris@16
|
24
|
Chris@16
|
25 //!\file
|
Chris@16
|
26 //!Describes file_mapping and mapped region classes
|
Chris@16
|
27
|
Chris@16
|
28 namespace boost {
|
Chris@16
|
29 namespace interprocess {
|
Chris@16
|
30
|
Chris@16
|
31 //!A class that wraps a file-mapping that can be used to
|
Chris@16
|
32 //!create mapped regions from the mapped files
|
Chris@16
|
33 class file_mapping
|
Chris@16
|
34 {
|
Chris@16
|
35 /// @cond
|
Chris@16
|
36 BOOST_MOVABLE_BUT_NOT_COPYABLE(file_mapping)
|
Chris@16
|
37 /// @endcond
|
Chris@16
|
38
|
Chris@16
|
39 public:
|
Chris@16
|
40 //!Constructs an empty file mapping.
|
Chris@16
|
41 //!Does not throw
|
Chris@16
|
42 file_mapping();
|
Chris@16
|
43
|
Chris@16
|
44 //!Opens a file mapping of file "filename", starting in offset
|
Chris@16
|
45 //!"file_offset", and the mapping's size will be "size". The mapping
|
Chris@16
|
46 //!can be opened for read-only "read_only" or read-write "read_write"
|
Chris@16
|
47 //!modes. Throws interprocess_exception on error.
|
Chris@16
|
48 file_mapping(const char *filename, mode_t mode);
|
Chris@16
|
49
|
Chris@16
|
50 //!Moves the ownership of "moved"'s file mapping object to *this.
|
Chris@16
|
51 //!After the call, "moved" does not represent any file mapping object.
|
Chris@16
|
52 //!Does not throw
|
Chris@16
|
53 file_mapping(BOOST_RV_REF(file_mapping) moved)
|
Chris@16
|
54 : m_handle(file_handle_t(ipcdetail::invalid_file()))
|
Chris@16
|
55 , m_mode(read_only)
|
Chris@16
|
56 { this->swap(moved); }
|
Chris@16
|
57
|
Chris@16
|
58 //!Moves the ownership of "moved"'s file mapping to *this.
|
Chris@16
|
59 //!After the call, "moved" does not represent any file mapping.
|
Chris@16
|
60 //!Does not throw
|
Chris@16
|
61 file_mapping &operator=(BOOST_RV_REF(file_mapping) moved)
|
Chris@16
|
62 {
|
Chris@16
|
63 file_mapping tmp(boost::move(moved));
|
Chris@16
|
64 this->swap(tmp);
|
Chris@16
|
65 return *this;
|
Chris@16
|
66 }
|
Chris@16
|
67
|
Chris@16
|
68 //!Swaps to file_mappings.
|
Chris@16
|
69 //!Does not throw.
|
Chris@16
|
70 void swap(file_mapping &other);
|
Chris@16
|
71
|
Chris@16
|
72 //!Returns access mode
|
Chris@16
|
73 //!used in the constructor
|
Chris@16
|
74 mode_t get_mode() const;
|
Chris@16
|
75
|
Chris@16
|
76 //!Obtains the mapping handle
|
Chris@16
|
77 //!to be used with mapped_region
|
Chris@16
|
78 mapping_handle_t get_mapping_handle() const;
|
Chris@16
|
79
|
Chris@16
|
80 //!Destroys the file mapping. All mapped regions created from this are still
|
Chris@16
|
81 //!valid. Does not throw
|
Chris@16
|
82 ~file_mapping();
|
Chris@16
|
83
|
Chris@16
|
84 //!Returns the name of the file
|
Chris@16
|
85 //!used in the constructor.
|
Chris@16
|
86 const char *get_name() const;
|
Chris@16
|
87
|
Chris@16
|
88 //!Removes the file named "filename" even if it's been memory mapped.
|
Chris@16
|
89 //!Returns true on success.
|
Chris@16
|
90 //!The function might fail in some operating systems if the file is
|
Chris@16
|
91 //!being used other processes and no deletion permission was shared.
|
Chris@16
|
92 static bool remove(const char *filename);
|
Chris@16
|
93
|
Chris@16
|
94 /// @cond
|
Chris@16
|
95 private:
|
Chris@16
|
96 //!Closes a previously opened file mapping. Never throws.
|
Chris@16
|
97 void priv_close();
|
Chris@16
|
98 file_handle_t m_handle;
|
Chris@16
|
99 mode_t m_mode;
|
Chris@16
|
100 std::string m_filename;
|
Chris@16
|
101 /// @endcond
|
Chris@16
|
102 };
|
Chris@16
|
103
|
Chris@16
|
104 inline file_mapping::file_mapping()
|
Chris@16
|
105 : m_handle(file_handle_t(ipcdetail::invalid_file()))
|
Chris@16
|
106 , m_mode(read_only)
|
Chris@16
|
107 {}
|
Chris@16
|
108
|
Chris@16
|
109 inline file_mapping::~file_mapping()
|
Chris@16
|
110 { this->priv_close(); }
|
Chris@16
|
111
|
Chris@16
|
112 inline const char *file_mapping::get_name() const
|
Chris@16
|
113 { return m_filename.c_str(); }
|
Chris@16
|
114
|
Chris@16
|
115 inline void file_mapping::swap(file_mapping &other)
|
Chris@16
|
116 {
|
Chris@16
|
117 std::swap(m_handle, other.m_handle);
|
Chris@16
|
118 std::swap(m_mode, other.m_mode);
|
Chris@16
|
119 m_filename.swap(other.m_filename);
|
Chris@16
|
120 }
|
Chris@16
|
121
|
Chris@16
|
122 inline mapping_handle_t file_mapping::get_mapping_handle() const
|
Chris@16
|
123 { return ipcdetail::mapping_handle_from_file_handle(m_handle); }
|
Chris@16
|
124
|
Chris@16
|
125 inline mode_t file_mapping::get_mode() const
|
Chris@16
|
126 { return m_mode; }
|
Chris@16
|
127
|
Chris@16
|
128 inline file_mapping::file_mapping
|
Chris@16
|
129 (const char *filename, mode_t mode)
|
Chris@16
|
130 : m_filename(filename)
|
Chris@16
|
131 {
|
Chris@16
|
132 //Check accesses
|
Chris@16
|
133 if (mode != read_write && mode != read_only){
|
Chris@16
|
134 error_info err = other_error;
|
Chris@16
|
135 throw interprocess_exception(err);
|
Chris@16
|
136 }
|
Chris@16
|
137
|
Chris@16
|
138 //Open file
|
Chris@16
|
139 m_handle = ipcdetail::open_existing_file(filename, mode);
|
Chris@16
|
140
|
Chris@16
|
141 //Check for error
|
Chris@16
|
142 if(m_handle == ipcdetail::invalid_file()){
|
Chris@16
|
143 error_info err = system_error_code();
|
Chris@16
|
144 this->priv_close();
|
Chris@16
|
145 throw interprocess_exception(err);
|
Chris@16
|
146 }
|
Chris@16
|
147 m_mode = mode;
|
Chris@16
|
148 }
|
Chris@16
|
149
|
Chris@16
|
150 inline bool file_mapping::remove(const char *filename)
|
Chris@16
|
151 { return ipcdetail::delete_file(filename); }
|
Chris@16
|
152
|
Chris@16
|
153 ///@cond
|
Chris@16
|
154
|
Chris@16
|
155 inline void file_mapping::priv_close()
|
Chris@16
|
156 {
|
Chris@16
|
157 if(m_handle != ipcdetail::invalid_file()){
|
Chris@16
|
158 ipcdetail::close_file(m_handle);
|
Chris@16
|
159 m_handle = ipcdetail::invalid_file();
|
Chris@16
|
160 }
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 ///@endcond
|
Chris@16
|
164
|
Chris@16
|
165 //!A class that stores the name of a file
|
Chris@16
|
166 //!and tries to remove it in its destructor
|
Chris@16
|
167 //!Useful to remove temporary files in the presence
|
Chris@16
|
168 //!of exceptions
|
Chris@16
|
169 class remove_file_on_destroy
|
Chris@16
|
170 {
|
Chris@16
|
171 const char * m_name;
|
Chris@16
|
172 public:
|
Chris@16
|
173 remove_file_on_destroy(const char *name)
|
Chris@16
|
174 : m_name(name)
|
Chris@16
|
175 {}
|
Chris@16
|
176
|
Chris@16
|
177 ~remove_file_on_destroy()
|
Chris@16
|
178 { ipcdetail::delete_file(m_name); }
|
Chris@16
|
179 };
|
Chris@16
|
180
|
Chris@16
|
181 } //namespace interprocess {
|
Chris@16
|
182 } //namespace boost {
|
Chris@16
|
183
|
Chris@16
|
184 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
185
|
Chris@16
|
186 #endif //BOOST_INTERPROCESS_FILE_MAPPING_HPP
|