PageRenderTime 26ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/boost_1_57_0/boost/interprocess/windows_shared_memory.hpp

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