/Src/Dependencies/Boost/boost/interprocess/windows_shared_memory.hpp

http://hadesmem.googlecode.com/ · C++ Header · 232 lines · 137 code · 40 blank · 55 comment · 8 complexity · 5a848c5d436a7e0e50ce509c8b917ff8 MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2009. 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_INTERPROCESS_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(detail::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(detail::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(detail::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_INTERPROCESS_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_INTERPROCESS_RV_REF(windows_shared_memory) moved)
  78. {
  79. windows_shared_memory tmp(boost::interprocess::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(detail::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. (detail::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 file_map_access = 0;
  130. unsigned long map_access = 0;
  131. switch(mode)
  132. {
  133. case read_only:
  134. file_map_access |= winapi::page_readonly;
  135. map_access |= winapi::file_map_read;
  136. break;
  137. case read_write:
  138. file_map_access |= winapi::page_readwrite;
  139. map_access |= winapi::file_map_write;
  140. break;
  141. case copy_on_write:
  142. file_map_access |= winapi::page_writecopy;
  143. map_access |= winapi::file_map_copy;
  144. break;
  145. default:
  146. {
  147. error_info err(mode_error);
  148. throw interprocess_exception(err);
  149. }
  150. break;
  151. }
  152. switch(type){
  153. case detail::DoOpen:
  154. m_handle = winapi::open_file_mapping
  155. (map_access, filename);
  156. break;
  157. case detail::DoCreate:
  158. case detail::DoOpenOrCreate:
  159. {
  160. __int64 s = size;
  161. unsigned long high_size(s >> 32), low_size((boost::uint32_t)s);
  162. m_handle = winapi::create_file_mapping
  163. ( winapi::invalid_handle_value, file_map_access, high_size, low_size, filename
  164. , (winapi::interprocess_security_attributes*)perm.get_permissions());
  165. }
  166. break;
  167. default:
  168. {
  169. error_info err = other_error;
  170. throw interprocess_exception(err);
  171. }
  172. }
  173. if(!m_handle || (type == detail::DoCreate && winapi::get_last_error() == winapi::error_already_exists)){
  174. error_info err = system_error_code();
  175. this->priv_close();
  176. throw interprocess_exception(err);
  177. }
  178. m_mode = mode;
  179. return true;
  180. }
  181. inline void windows_shared_memory::priv_close()
  182. {
  183. if(m_handle){
  184. winapi::close_handle(m_handle);
  185. m_handle = 0;
  186. }
  187. }
  188. ///@endcond
  189. } //namespace interprocess {
  190. } //namespace boost {
  191. #include <boost/interprocess/detail/config_end.hpp>
  192. #endif //BOOST_INTERPROCESS_WINDOWS_SHARED_MEMORY_HPP