file_mapping.hpp 5.8 KB

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