managed_windows_shared_memory.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_MANAGED_WINDOWS_SHARED_MEMORY_HPP
  11. #define BOOST_INTERPROCESS_MANAGED_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/detail/managed_open_or_create_impl.hpp>
  22. #include <boost/interprocess/detail/managed_memory_impl.hpp>
  23. #include <boost/interprocess/creation_tags.hpp>
  24. #include <boost/interprocess/windows_shared_memory.hpp>
  25. #include <boost/interprocess/permissions.hpp>
  26. #include <boost/move/utility_core.hpp>
  27. //These includes needed to fulfill default template parameters of
  28. //predeclarations in interprocess_fwd.hpp
  29. #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
  30. #include <boost/interprocess/sync/mutex_family.hpp>
  31. #include <boost/interprocess/indexes/iset_index.hpp>
  32. namespace boost {
  33. namespace interprocess {
  34. namespace ipcdetail {
  35. template
  36. <
  37. class CharType,
  38. class AllocationAlgorithm,
  39. template<class IndexConfig> class IndexType
  40. >
  41. struct wshmem_open_or_create
  42. {
  43. static const std::size_t segment_manager_alignment = boost::move_detail::alignment_of
  44. < segment_manager
  45. < CharType
  46. , AllocationAlgorithm
  47. , IndexType>
  48. >::value;
  49. static const std::size_t final_segment_manager_alignment
  50. = segment_manager_alignment > AllocationAlgorithm::Alignment
  51. ? segment_manager_alignment : AllocationAlgorithm::Alignment;
  52. typedef ipcdetail::managed_open_or_create_impl
  53. < windows_shared_memory
  54. , final_segment_manager_alignment
  55. , false
  56. , false> type;
  57. };
  58. } //namespace ipcdetail {
  59. //!A basic managed windows shared memory creation class. Initializes the
  60. //!shared memory segment. Inherits all basic functionality from
  61. //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
  62. //!Unlike basic_managed_shared_memory, it has
  63. //!no kernel persistence and the shared memory is destroyed
  64. //!when all processes destroy all their windows_shared_memory
  65. //!objects and mapped regions for the same shared memory
  66. //!or the processes end/crash.
  67. //!
  68. //!Warning: basic_managed_windows_shared_memory and
  69. //!basic_managed_shared_memory can't communicate between them.
  70. template
  71. <
  72. class CharType,
  73. class AllocationAlgorithm,
  74. template<class IndexConfig> class IndexType
  75. >
  76. class basic_managed_windows_shared_memory
  77. : public ipcdetail::basic_managed_memory_impl
  78. < CharType, AllocationAlgorithm, IndexType
  79. , ipcdetail::wshmem_open_or_create
  80. <CharType, AllocationAlgorithm, IndexType>::type::ManagedOpenOrCreateUserOffset>
  81. {
  82. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  83. private:
  84. typedef ipcdetail::basic_managed_memory_impl
  85. < CharType, AllocationAlgorithm, IndexType
  86. , ipcdetail::wshmem_open_or_create
  87. <CharType, AllocationAlgorithm, IndexType>::type::ManagedOpenOrCreateUserOffset> base_t;
  88. typedef ipcdetail::create_open_func<base_t> create_open_func_t;
  89. basic_managed_windows_shared_memory *get_this_pointer()
  90. { return this; }
  91. private:
  92. typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
  93. BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_windows_shared_memory)
  94. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  95. public: //functions
  96. typedef typename base_t::size_type size_type;
  97. //!Default constructor. Does nothing.
  98. //!Useful in combination with move semantics
  99. basic_managed_windows_shared_memory() BOOST_NOEXCEPT
  100. {}
  101. //!Creates shared memory and creates and places the segment manager.
  102. //!This can throw.
  103. basic_managed_windows_shared_memory
  104. (create_only_t, const char *name,
  105. size_type size, const void *addr = 0, const permissions &perm = permissions())
  106. : m_wshm(create_only, name, size, read_write, addr,
  107. create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
  108. {}
  109. //!Creates shared memory and creates and places the segment manager if
  110. //!segment was not created. If segment was created it connects to the
  111. //!segment.
  112. //!This can throw.
  113. basic_managed_windows_shared_memory
  114. (open_or_create_t,
  115. const char *name, size_type size,
  116. const void *addr = 0,
  117. const permissions &perm = permissions())
  118. : m_wshm(open_or_create, name, size, read_write, addr,
  119. create_open_func_t(get_this_pointer(),
  120. ipcdetail::DoOpenOrCreate), perm)
  121. {}
  122. //!Connects to a created shared memory and its segment manager.
  123. //!This can throw.
  124. basic_managed_windows_shared_memory
  125. (open_only_t, const char* name, const void *addr = 0)
  126. : m_wshm(open_only, name, read_write, addr,
  127. create_open_func_t(get_this_pointer(),
  128. ipcdetail::DoOpen))
  129. {}
  130. //!Connects to a created shared memory and its segment manager
  131. //!in copy_on_write mode.
  132. //!This can throw.
  133. basic_managed_windows_shared_memory
  134. (open_copy_on_write_t, const char* name, const void *addr = 0)
  135. : m_wshm(open_only, name, copy_on_write, addr,
  136. create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
  137. {}
  138. //!Connects to a created shared memory and its segment manager
  139. //!in read-only mode.
  140. //!This can throw.
  141. basic_managed_windows_shared_memory
  142. (open_read_only_t, const char* name, const void *addr = 0)
  143. : base_t()
  144. , m_wshm(open_only, name, read_only, addr,
  145. create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
  146. {}
  147. //!Creates shared memory and creates and places the segment manager if
  148. //!segment was not created. If segment was created it connects to the
  149. //!segment.
  150. //!This can throw.
  151. basic_managed_windows_shared_memory
  152. (open_or_create_t,
  153. const wchar_t *name, size_type size,
  154. const void *addr = 0,
  155. const permissions &perm = permissions())
  156. : m_wshm(open_or_create, name, size, read_write, addr,
  157. create_open_func_t(get_this_pointer(),
  158. ipcdetail::DoOpenOrCreate), perm)
  159. {}
  160. //!Connects to a created shared memory and its segment manager.
  161. //!This can throw.
  162. basic_managed_windows_shared_memory
  163. (open_only_t, const wchar_t* name, const void *addr = 0)
  164. : m_wshm(open_only, name, read_write, addr,
  165. create_open_func_t(get_this_pointer(),
  166. ipcdetail::DoOpen))
  167. {}
  168. //!Connects to a created shared memory and its segment manager
  169. //!in copy_on_write mode.
  170. //!This can throw.
  171. basic_managed_windows_shared_memory
  172. (open_copy_on_write_t, const wchar_t* name, const void *addr = 0)
  173. : m_wshm(open_only, name, copy_on_write, addr,
  174. create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
  175. {}
  176. //!Connects to a created shared memory and its segment manager
  177. //!in read-only mode.
  178. //!This can throw.
  179. basic_managed_windows_shared_memory
  180. (open_read_only_t, const wchar_t* name, const void *addr = 0)
  181. : base_t()
  182. , m_wshm(open_only, name, read_only, addr,
  183. create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
  184. {}
  185. //!Moves the ownership of "moved"'s managed memory to *this.
  186. //!Does not throw
  187. basic_managed_windows_shared_memory
  188. (BOOST_RV_REF(basic_managed_windows_shared_memory) moved) BOOST_NOEXCEPT
  189. { this->swap(moved); }
  190. //!Moves the ownership of "moved"'s managed memory to *this.
  191. //!Does not throw
  192. basic_managed_windows_shared_memory &operator=(BOOST_RV_REF(basic_managed_windows_shared_memory) moved) BOOST_NOEXCEPT
  193. {
  194. basic_managed_windows_shared_memory tmp(boost::move(moved));
  195. this->swap(tmp);
  196. return *this;
  197. }
  198. //!Destroys *this and indicates that the calling process is finished using
  199. //!the resource. All mapped regions are still valid after
  200. //!destruction. When all mapped regions and basic_managed_windows_shared_memory
  201. //!objects referring the shared memory are destroyed, the
  202. //!operating system will destroy the shared memory.
  203. ~basic_managed_windows_shared_memory()
  204. {}
  205. //!Swaps the ownership of the managed mapped memories managed by *this and other.
  206. //!Never throws.
  207. void swap(basic_managed_windows_shared_memory &other) BOOST_NOEXCEPT
  208. {
  209. base_t::swap(other);
  210. m_wshm.swap(other.m_wshm);
  211. }
  212. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  213. //!Tries to find a previous named allocation address. Returns a memory
  214. //!buffer and the object count. If not found returned pointer is 0.
  215. //!Never throws.
  216. template <class T>
  217. std::pair<T*, size_type> find (char_ptr_holder_t name)
  218. {
  219. if(m_wshm.get_mapped_region().get_mode() == read_only){
  220. return base_t::template find_no_lock<T>(name);
  221. }
  222. else{
  223. return base_t::template find<T>(name);
  224. }
  225. }
  226. private:
  227. typename ipcdetail::wshmem_open_or_create
  228. <CharType, AllocationAlgorithm, IndexType>::type m_wshm;
  229. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  230. };
  231. #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  232. //!Typedef for a default basic_managed_windows_shared_memory
  233. //!of narrow characters
  234. typedef basic_managed_windows_shared_memory
  235. <char
  236. ,rbtree_best_fit<mutex_family>
  237. ,iset_index>
  238. managed_windows_shared_memory;
  239. //!Typedef for a default basic_managed_windows_shared_memory
  240. //!of wide characters
  241. typedef basic_managed_windows_shared_memory
  242. <wchar_t
  243. ,rbtree_best_fit<mutex_family>
  244. ,iset_index>
  245. wmanaged_windows_shared_memory;
  246. #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  247. } //namespace interprocess {
  248. } //namespace boost {
  249. #include <boost/interprocess/detail/config_end.hpp>
  250. #endif //BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP