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