file_mapping.hpp 5.3 KB

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