windows_shared_memory.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #if !defined(BOOST_INTERPROCESS_WINDOWS)
  24. #error "This header can only be used in Windows operating systems"
  25. #endif
  26. #include <boost/interprocess/creation_tags.hpp>
  27. #include <boost/interprocess/exceptions.hpp>
  28. #include <boost/interprocess/detail/utilities.hpp>
  29. #include <boost/interprocess/detail/os_file_functions.hpp>
  30. #include <boost/interprocess/interprocess_fwd.hpp>
  31. #include <boost/interprocess/exceptions.hpp>
  32. #include <boost/interprocess/detail/win32_api.hpp>
  33. #include <cstddef>
  34. #include <boost/cstdint.hpp>
  35. #include <string>
  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();
  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. //!Moves the ownership of "moved"'s shared memory object to *this.
  76. //!After the call, "moved" does not represent any shared memory object.
  77. //!Does not throw
  78. windows_shared_memory(BOOST_RV_REF(windows_shared_memory) moved)
  79. : m_handle(0)
  80. { this->swap(moved); }
  81. //!Moves the ownership of "moved"'s shared memory to *this.
  82. //!After the call, "moved" does not represent any shared memory.
  83. //!Does not throw
  84. windows_shared_memory &operator=(BOOST_RV_REF(windows_shared_memory) moved)
  85. {
  86. windows_shared_memory tmp(boost::move(moved));
  87. this->swap(tmp);
  88. return *this;
  89. }
  90. //!Swaps to shared_memory_objects. Does not throw
  91. void swap(windows_shared_memory &other);
  92. //!Destroys *this. All mapped regions are still valid after
  93. //!destruction. When all mapped regions and windows_shared_memory
  94. //!objects referring the shared memory are destroyed, the
  95. //!operating system will destroy the shared memory.
  96. ~windows_shared_memory();
  97. //!Returns the name of the shared memory.
  98. const char *get_name() const;
  99. //!Returns access mode
  100. mode_t get_mode() const;
  101. //!Returns the mapping handle. Never throws
  102. mapping_handle_t get_mapping_handle() const;
  103. //!Returns the size of the windows shared memory. It will be a 4K rounded
  104. //!size of the "size" passed in the constructor.
  105. offset_t get_size() const;
  106. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  107. private:
  108. //!Closes a previously opened file mapping. Never throws.
  109. void priv_close();
  110. //!Closes a previously opened file mapping. Never throws.
  111. bool priv_open_or_create(ipcdetail::create_enum_t type, const char *filename, mode_t mode, std::size_t size, const permissions& perm = permissions());
  112. void * m_handle;
  113. mode_t m_mode;
  114. std::string m_name;
  115. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  116. };
  117. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  118. inline windows_shared_memory::windows_shared_memory()
  119. : m_handle(0)
  120. {}
  121. inline windows_shared_memory::~windows_shared_memory()
  122. { this->priv_close(); }
  123. inline const char *windows_shared_memory::get_name() const
  124. { return m_name.c_str(); }
  125. inline void windows_shared_memory::swap(windows_shared_memory &other)
  126. {
  127. (simple_swap)(m_handle, other.m_handle);
  128. (simple_swap)(m_mode, other.m_mode);
  129. m_name.swap(other.m_name);
  130. }
  131. inline mapping_handle_t windows_shared_memory::get_mapping_handle() const
  132. { mapping_handle_t mhnd = { m_handle, true}; return mhnd; }
  133. inline mode_t windows_shared_memory::get_mode() const
  134. { return m_mode; }
  135. inline offset_t windows_shared_memory::get_size() const
  136. {
  137. offset_t size; //This shall never fail
  138. return (m_handle && winapi::get_file_mapping_size(m_handle, size)) ? size : 0;
  139. }
  140. inline bool windows_shared_memory::priv_open_or_create
  141. (ipcdetail::create_enum_t type, const char *filename, mode_t mode, std::size_t size, const permissions& perm)
  142. {
  143. m_name = filename ? filename : "";
  144. unsigned long protection = 0;
  145. unsigned long map_access = 0;
  146. switch(mode)
  147. {
  148. //"protection" is for "create_file_mapping"
  149. //"map_access" is for "open_file_mapping"
  150. //Add section query (strange that read or access does not grant it...)
  151. //to obtain the size of the mapping. copy_on_write is equal to section_query.
  152. case read_only:
  153. protection |= winapi::page_readonly;
  154. map_access |= winapi::file_map_read | winapi::section_query;
  155. break;
  156. case read_write:
  157. protection |= winapi::page_readwrite;
  158. map_access |= winapi::file_map_write | winapi::section_query;
  159. break;
  160. case copy_on_write:
  161. protection |= winapi::page_writecopy;
  162. map_access |= winapi::file_map_copy;
  163. break;
  164. default:
  165. {
  166. error_info err(mode_error);
  167. throw interprocess_exception(err);
  168. }
  169. break;
  170. }
  171. switch(type){
  172. case ipcdetail::DoOpen:
  173. m_handle = winapi::open_file_mapping(map_access, filename);
  174. break;
  175. case ipcdetail::DoCreate:
  176. case ipcdetail::DoOpenOrCreate:
  177. {
  178. m_handle = winapi::create_file_mapping
  179. ( winapi::invalid_handle_value, protection, size, filename
  180. , (winapi::interprocess_security_attributes*)perm.get_permissions());
  181. }
  182. break;
  183. default:
  184. {
  185. error_info err = other_error;
  186. throw interprocess_exception(err);
  187. }
  188. }
  189. if(!m_handle || (type == ipcdetail::DoCreate && winapi::get_last_error() == winapi::error_already_exists)){
  190. error_info err = system_error_code();
  191. this->priv_close();
  192. throw interprocess_exception(err);
  193. }
  194. m_mode = mode;
  195. return true;
  196. }
  197. inline void windows_shared_memory::priv_close()
  198. {
  199. if(m_handle){
  200. winapi::close_handle(m_handle);
  201. m_handle = 0;
  202. }
  203. }
  204. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  205. } //namespace interprocess {
  206. } //namespace boost {
  207. #include <boost/interprocess/detail/config_end.hpp>
  208. #endif //BOOST_INTERPROCESS_WINDOWS_SHARED_MEMORY_HPP