file_mapping.hpp 5.4 KB

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