windows_shared_memory.hpp 9.8 KB

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