File indexing completed on 2025-01-18 09:38:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_FILE_MAPPING_HPP
0012 #define BOOST_INTERPROCESS_FILE_MAPPING_HPP
0013
0014 #ifndef BOOST_CONFIG_HPP
0015 # include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 # pragma once
0020 #endif
0021
0022 #include <boost/interprocess/detail/config_begin.hpp>
0023 #include <boost/interprocess/detail/workaround.hpp>
0024
0025 #if !defined(BOOST_INTERPROCESS_MAPPED_FILES)
0026 #error "Boost.Interprocess: This platform does not support memory mapped files!"
0027 #endif
0028
0029 #include <boost/interprocess/interprocess_fwd.hpp>
0030 #include <boost/interprocess/exceptions.hpp>
0031 #include <boost/interprocess/detail/utilities.hpp>
0032 #include <boost/interprocess/creation_tags.hpp>
0033 #include <boost/interprocess/detail/os_file_functions.hpp>
0034 #include <boost/interprocess/detail/simple_swap.hpp>
0035 #include <boost/interprocess/detail/char_wchar_holder.hpp>
0036 #include <boost/move/utility_core.hpp>
0037
0038
0039
0040
0041 namespace boost {
0042 namespace interprocess {
0043
0044
0045
0046 class file_mapping
0047 {
0048 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0049 BOOST_MOVABLE_BUT_NOT_COPYABLE(file_mapping)
0050 #endif
0051
0052 public:
0053
0054
0055 file_mapping() BOOST_NOEXCEPT;
0056
0057
0058
0059
0060
0061 file_mapping(const char *filename, mode_t mode);
0062
0063 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0064
0065
0066
0067
0068
0069
0070
0071 file_mapping(const wchar_t *filename, mode_t mode);
0072 #endif
0073
0074
0075
0076
0077 file_mapping(BOOST_RV_REF(file_mapping) moved) BOOST_NOEXCEPT
0078 : m_handle(file_handle_t(ipcdetail::invalid_file()))
0079 , m_mode(read_only)
0080 { this->swap(moved); }
0081
0082
0083
0084
0085 file_mapping &operator=(BOOST_RV_REF(file_mapping) moved) BOOST_NOEXCEPT
0086 {
0087 file_mapping tmp(boost::move(moved));
0088 this->swap(tmp);
0089 return *this;
0090 }
0091
0092
0093
0094 void swap(file_mapping &other) BOOST_NOEXCEPT;
0095
0096
0097
0098 mode_t get_mode() const BOOST_NOEXCEPT;
0099
0100
0101
0102 mapping_handle_t get_mapping_handle() const BOOST_NOEXCEPT;
0103
0104
0105
0106 ~file_mapping();
0107
0108
0109
0110 const char *get_name() const BOOST_NOEXCEPT;
0111
0112
0113
0114
0115
0116 static bool remove(const char *filename);
0117
0118 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0119
0120
0121
0122
0123
0124
0125
0126 static bool remove(const wchar_t *filename);
0127 #endif
0128
0129 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0130 private:
0131
0132 void priv_close();
0133 file_handle_t m_handle;
0134 mode_t m_mode;
0135 char_wchar_holder m_filename;
0136 #endif
0137 };
0138
0139 inline file_mapping::file_mapping() BOOST_NOEXCEPT
0140 : m_handle(file_handle_t(ipcdetail::invalid_file()))
0141 , m_mode(read_only)
0142 {}
0143
0144 inline file_mapping::~file_mapping()
0145 { this->priv_close(); }
0146
0147 inline const char *file_mapping::get_name() const BOOST_NOEXCEPT
0148 { return m_filename.getn(); }
0149
0150 inline void file_mapping::swap(file_mapping &other) BOOST_NOEXCEPT
0151 {
0152 (simple_swap)(m_handle, other.m_handle);
0153 (simple_swap)(m_mode, other.m_mode);
0154 m_filename.swap(other.m_filename);
0155 }
0156
0157 inline mapping_handle_t file_mapping::get_mapping_handle() const BOOST_NOEXCEPT
0158 { return ipcdetail::mapping_handle_from_file_handle(m_handle); }
0159
0160 inline mode_t file_mapping::get_mode() const BOOST_NOEXCEPT
0161 { return m_mode; }
0162
0163 inline file_mapping::file_mapping
0164 (const char *filename, mode_t mode)
0165 : m_filename(filename)
0166 {
0167
0168 if (mode != read_write && mode != read_only){
0169 error_info err = other_error;
0170 throw interprocess_exception(err);
0171 }
0172
0173
0174 m_handle = ipcdetail::open_existing_file(filename, mode);
0175
0176
0177 if(m_handle == ipcdetail::invalid_file()){
0178 error_info err = system_error_code();
0179 this->priv_close();
0180 throw interprocess_exception(err);
0181 }
0182 m_mode = mode;
0183 }
0184
0185 inline bool file_mapping::remove(const char *filename)
0186 { return ipcdetail::delete_file(filename); }
0187
0188 #ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES
0189 inline file_mapping::file_mapping
0190 (const wchar_t *filename, mode_t mode)
0191 : m_filename(filename)
0192 {
0193
0194 if (mode != read_write && mode != read_only){
0195 error_info err = other_error;
0196 throw interprocess_exception(err);
0197 }
0198
0199
0200 m_handle = ipcdetail::open_existing_file(filename, mode);
0201
0202
0203 if(m_handle == ipcdetail::invalid_file()){
0204 error_info err = system_error_code();
0205 this->priv_close();
0206 throw interprocess_exception(err);
0207 }
0208 m_mode = mode;
0209 }
0210
0211 inline bool file_mapping::remove(const wchar_t *filename)
0212 { return ipcdetail::delete_file(filename); }
0213
0214 #endif
0215
0216
0217 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0218
0219 inline void file_mapping::priv_close()
0220 {
0221 if(m_handle != ipcdetail::invalid_file()){
0222 ipcdetail::close_file(m_handle);
0223 m_handle = ipcdetail::invalid_file();
0224 }
0225 }
0226
0227
0228
0229
0230
0231 class remove_file_on_destroy
0232 {
0233 const char * m_name;
0234 public:
0235 remove_file_on_destroy(const char *name)
0236 : m_name(name)
0237 {}
0238
0239 ~remove_file_on_destroy()
0240 { ipcdetail::delete_file(m_name); }
0241 };
0242
0243 #endif
0244
0245 }
0246 }
0247
0248 #include <boost/interprocess/detail/config_end.hpp>
0249
0250 #endif