windows_shared_memory.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_WINDOWS_SHARED_MEMORY_HPP
  11. #define BOOST_INTERPROCESS_WINDOWS_SHARED_MEMORY_HPP
  12. #include <boost/interprocess/detail/config_begin.hpp>
  13. #include <boost/interprocess/detail/workaround.hpp>
  14. #include <boost/detail/workaround.hpp>
  15. #include <boost/interprocess/permissions.hpp>
  16. #if !defined(BOOST_INTERPROCESS_WINDOWS)
  17. #error "This header can only be used in Windows operating systems"
  18. #endif
  19. #include <boost/interprocess/creation_tags.hpp>
  20. #include <boost/interprocess/exceptions.hpp>
  21. #include <boost/interprocess/detail/utilities.hpp>
  22. #include <boost/interprocess/detail/os_file_functions.hpp>
  23. #include <boost/interprocess/interprocess_fwd.hpp>
  24. #include <boost/interprocess/exceptions.hpp>
  25. #include <boost/interprocess/detail/win32_api.hpp>
  26. #include <cstddef>
  27. #include <boost/cstdint.hpp>
  28. #include <string>
  29. //!\file
  30. //!Describes a class representing a native windows shared memory.
  31. namespace boost {
  32. namespace interprocess {
  33. //!A class that wraps the native Windows shared memory
  34. //!that is implemented as a file mapping of the paging file.
  35. //!Unlike shared_memory_object, windows_shared_memory has
  36. //!no kernel persistence and the shared memory is destroyed
  37. //!when all processes destroy all their windows_shared_memory
  38. //!objects and mapped regions for the same shared memory
  39. //!or the processes end/crash.
  40. //!
  41. //!Warning: Windows native shared memory and interprocess portable
  42. //!shared memory (boost::interprocess::shared_memory_object)
  43. //!can't communicate between them.
  44. class windows_shared_memory
  45. {
  46. /// @cond
  47. //Non-copyable and non-assignable
  48. BOOST_MOVABLE_BUT_NOT_COPYABLE(windows_shared_memory)
  49. /// @endcond
  50. public:
  51. //!Default constructor.
  52. //!Represents an empty windows_shared_memory.
  53. windows_shared_memory();
  54. //!Creates a new native shared memory with name "name" and mode "mode",
  55. //!with the access mode "mode".
  56. //!If the file previously exists, throws an error.
  57. windows_shared_memory(create_only_t, const char *name, mode_t mode, std::size_t size, const permissions& perm = permissions())
  58. { this->priv_open_or_create(ipcdetail::DoCreate, name, mode, size, perm); }
  59. //!Tries to create a shared memory object with name "name" and mode "mode", with the
  60. //!access mode "mode". If the file previously exists, it tries to open it with mode "mode".
  61. //!Otherwise throws an error.
  62. windows_shared_memory(open_or_create_t, const char *name, mode_t mode, std::size_t size, const permissions& perm = permissions())
  63. { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, name, mode, size, perm); }
  64. //!Tries to open a shared memory object with name "name", with the access mode "mode".
  65. //!If the file does not previously exist, it throws an error.
  66. windows_shared_memory(open_only_t, const char *name, mode_t mode)
  67. { this->priv_open_or_create(ipcdetail::DoOpen, name, mode, 0, permissions()); }
  68. //!Moves the ownership of "moved"'s shared memory object to *this.
  69. //!After the call, "moved" does not represent any shared memory object.
  70. //!Does not throw
  71. windows_shared_memory(BOOST_RV_REF(windows_shared_memory) moved)
  72. : m_handle(0)
  73. { this->swap(moved); }
  74. //!Moves the ownership of "moved"'s shared memory to *this.
  75. //!After the call, "moved" does not represent any shared memory.
  76. //!Does not throw
  77. windows_shared_memory &operator=(BOOST_RV_REF(windows_shared_memory) moved)
  78. {
  79. windows_shared_memory tmp(boost::move(moved));
  80. this->swap(tmp);
  81. return *this;
  82. }
  83. //!Swaps to shared_memory_objects. Does not throw
  84. void swap(windows_shared_memory &other);
  85. //!Destroys *this. All mapped regions are still valid after
  86. //!destruction. When all mapped regions and windows_shared_memory
  87. //!objects referring the shared memory are destroyed, the
  88. //!operating system will destroy the shared memory.
  89. ~windows_shared_memory();
  90. //!Returns the name of the shared memory.
  91. const char *get_name() const;
  92. //!Returns access mode
  93. mode_t get_mode() const;
  94. //!Returns the mapping handle. Never throws
  95. mapping_handle_t get_mapping_handle() const;
  96. /// @cond
  97. private:
  98. //!Closes a previously opened file mapping. Never throws.
  99. void priv_close();
  100. //!Closes a previously opened file mapping. Never throws.
  101. bool priv_open_or_create(ipcdetail::create_enum_t type, const char *filename, mode_t mode, std::size_t size, const permissions& perm = permissions());
  102. void * m_handle;
  103. mode_t m_mode;
  104. std::string m_name;
  105. /// @endcond
  106. };
  107. /// @cond
  108. inline windows_shared_memory::windows_shared_memory()
  109. : m_handle(0)
  110. {}
  111. inline windows_shared_memory::~windows_shared_memory()
  112. { this->priv_close(); }
  113. inline const char *windows_shared_memory::get_name() const
  114. { return m_name.c_str(); }
  115. inline void windows_shared_memory::swap(windows_shared_memory &other)
  116. {
  117. std::swap(m_handle, other.m_handle);
  118. std::swap(m_mode, other.m_mode);
  119. m_name.swap(other.m_name);
  120. }
  121. inline mapping_handle_t windows_shared_memory::get_mapping_handle() const
  122. { mapping_handle_t mhnd = { m_handle, true}; return mhnd; }
  123. inline mode_t windows_shared_memory::get_mode() const
  124. { return m_mode; }
  125. inline bool windows_shared_memory::priv_open_or_create
  126. (ipcdetail::create_enum_t type, const char *filename, mode_t mode, std::size_t size, const permissions& perm)
  127. {
  128. m_name = filename ? filename : "";
  129. unsigned long protection = 0;
  130. unsigned long map_access = 0;
  131. switch(mode)
  132. {
  133. //"protection" is for "create_file_mapping"
  134. //"map_access" is for "open_file_mapping"
  135. //Add section query (strange that read or access does not grant it...)
  136. //to obtain the size of the mapping. copy_on_write is equal to section_query.
  137. case read_only:
  138. protection |= winapi::page_readonly;
  139. map_access |= winapi::file_map_read | winapi::section_query;
  140. break;
  141. case read_write:
  142. protection |= winapi::page_readwrite;
  143. map_access |= winapi::file_map_write | winapi::section_query;
  144. break;
  145. case copy_on_write:
  146. protection |= winapi::page_writecopy;
  147. map_access |= winapi::file_map_copy;
  148. break;
  149. default:
  150. {
  151. error_info err(mode_error);
  152. throw interprocess_exception(err);
  153. }
  154. break;
  155. }
  156. switch(type){
  157. case ipcdetail::DoOpen:
  158. m_handle = winapi::open_file_mapping(map_access, filename);
  159. break;
  160. case ipcdetail::DoCreate:
  161. case ipcdetail::DoOpenOrCreate:
  162. {
  163. m_handle = winapi::create_file_mapping
  164. ( winapi::invalid_handle_value, protection, size, filename
  165. , (winapi::interprocess_security_attributes*)perm.get_permissions());
  166. }
  167. break;
  168. default:
  169. {
  170. error_info err = other_error;
  171. throw interprocess_exception(err);
  172. }
  173. }
  174. if(!m_handle || (type == ipcdetail::DoCreate && winapi::get_last_error() == winapi::error_already_exists)){
  175. error_info err = system_error_code();
  176. this->priv_close();
  177. throw interprocess_exception(err);
  178. }
  179. m_mode = mode;
  180. return true;
  181. }
  182. inline void windows_shared_memory::priv_close()
  183. {
  184. if(m_handle){
  185. winapi::close_handle(m_handle);
  186. m_handle = 0;
  187. }
  188. }
  189. ///@endcond
  190. } //namespace interprocess {
  191. } //namespace boost {
  192. #include <boost/interprocess/detail/config_end.hpp>
  193. #endif //BOOST_INTERPROCESS_WINDOWS_SHARED_MEMORY_HPP