file_mapping.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. //!Removes the file named "filename" even if it's been memory mapped.
  94. //!Returns true on success.
  95. //!The function might fail in some operating systems if the file is
  96. //!being used other processes and no deletion permission was shared.
  97. static bool remove(const char *filename);
  98. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  99. //!Removes the file named "filename" even if it's been memory mapped.
  100. //!Returns true on success.
  101. //!The function might fail in some operating systems if the file is
  102. //!being used other processes and no deletion permission was shared.
  103. //!
  104. //!Note: This function is only available on operating systems with
  105. //! native wchar_t APIs (e.g. Windows).
  106. static bool remove(const wchar_t *filename);
  107. #endif
  108. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  109. private:
  110. //!Closes a previously opened file mapping. Never throws.
  111. void priv_close();
  112. file_handle_t m_handle;
  113. mode_t m_mode;
  114. char_wchar_holder m_filename;
  115. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  116. };
  117. inline file_mapping::file_mapping() BOOST_NOEXCEPT
  118. : m_handle(file_handle_t(ipcdetail::invalid_file()))
  119. , m_mode(read_only)
  120. {}
  121. inline file_mapping::~file_mapping()
  122. { this->priv_close(); }
  123. inline const char *file_mapping::get_name() const BOOST_NOEXCEPT
  124. { return m_filename.getn(); }
  125. inline void file_mapping::swap(file_mapping &other) BOOST_NOEXCEPT
  126. {
  127. (simple_swap)(m_handle, other.m_handle);
  128. (simple_swap)(m_mode, other.m_mode);
  129. m_filename.swap(other.m_filename);
  130. }
  131. inline mapping_handle_t file_mapping::get_mapping_handle() const BOOST_NOEXCEPT
  132. { return ipcdetail::mapping_handle_from_file_handle(m_handle); }
  133. inline mode_t file_mapping::get_mode() const BOOST_NOEXCEPT
  134. { return m_mode; }
  135. inline file_mapping::file_mapping
  136. (const char *filename, mode_t mode)
  137. : m_filename(filename)
  138. {
  139. //Check accesses
  140. if (mode != read_write && mode != read_only){
  141. error_info err = other_error;
  142. throw interprocess_exception(err);
  143. }
  144. //Open file
  145. m_handle = ipcdetail::open_existing_file(filename, mode);
  146. //Check for error
  147. if(m_handle == ipcdetail::invalid_file()){
  148. error_info err = system_error_code();
  149. this->priv_close();
  150. throw interprocess_exception(err);
  151. }
  152. m_mode = mode;
  153. }
  154. inline bool file_mapping::remove(const char *filename)
  155. { return ipcdetail::delete_file(filename); }
  156. #ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES
  157. inline file_mapping::file_mapping
  158. (const wchar_t *filename, mode_t mode)
  159. : m_filename(filename)
  160. {
  161. //Check accesses
  162. if (mode != read_write && mode != read_only){
  163. error_info err = other_error;
  164. throw interprocess_exception(err);
  165. }
  166. //Open file
  167. m_handle = ipcdetail::open_existing_file(filename, mode);
  168. //Check for error
  169. if(m_handle == ipcdetail::invalid_file()){
  170. error_info err = system_error_code();
  171. this->priv_close();
  172. throw interprocess_exception(err);
  173. }
  174. m_mode = mode;
  175. }
  176. inline bool file_mapping::remove(const wchar_t *filename)
  177. { return ipcdetail::delete_file(filename); }
  178. #endif
  179. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  180. inline void file_mapping::priv_close()
  181. {
  182. if(m_handle != ipcdetail::invalid_file()){
  183. ipcdetail::close_file(m_handle);
  184. m_handle = ipcdetail::invalid_file();
  185. }
  186. }
  187. //!A class that stores the name of a file
  188. //!and tries to remove it in its destructor
  189. //!Useful to remove temporary files in the presence
  190. //!of exceptions
  191. class remove_file_on_destroy
  192. {
  193. const char * m_name;
  194. public:
  195. remove_file_on_destroy(const char *name)
  196. : m_name(name)
  197. {}
  198. ~remove_file_on_destroy()
  199. { ipcdetail::delete_file(m_name); }
  200. };
  201. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  202. } //namespace interprocess {
  203. } //namespace boost {
  204. #include <boost/interprocess/detail/config_end.hpp>
  205. #endif //BOOST_INTERPROCESS_FILE_MAPPING_HPP