file_mapping.hpp 8.0 KB

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