managed_windows_shared_memory.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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<class AllocationAlgorithm>
  36. struct wshmem_open_or_create
  37. {
  38. typedef ipcdetail::managed_open_or_create_impl
  39. < windows_shared_memory, AllocationAlgorithm::Alignment, false, false> type;
  40. };
  41. } //namespace ipcdetail {
  42. //!A basic managed windows shared memory creation class. Initializes the
  43. //!shared memory segment. Inherits all basic functionality from
  44. //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
  45. //!Unlike basic_managed_shared_memory, it has
  46. //!no kernel persistence and the shared memory is destroyed
  47. //!when all processes destroy all their windows_shared_memory
  48. //!objects and mapped regions for the same shared memory
  49. //!or the processes end/crash.
  50. //!
  51. //!Warning: basic_managed_windows_shared_memory and
  52. //!basic_managed_shared_memory can't communicate between them.
  53. template
  54. <
  55. class CharType,
  56. class AllocationAlgorithm,
  57. template<class IndexConfig> class IndexType
  58. >
  59. class basic_managed_windows_shared_memory
  60. : public ipcdetail::basic_managed_memory_impl
  61. < CharType, AllocationAlgorithm, IndexType
  62. , ipcdetail::wshmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset>
  63. {
  64. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  65. private:
  66. typedef ipcdetail::basic_managed_memory_impl
  67. <CharType, AllocationAlgorithm, IndexType,
  68. ipcdetail::wshmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset> base_t;
  69. typedef ipcdetail::create_open_func<base_t> create_open_func_t;
  70. basic_managed_windows_shared_memory *get_this_pointer()
  71. { return this; }
  72. private:
  73. typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
  74. BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_windows_shared_memory)
  75. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  76. public: //functions
  77. typedef typename base_t::size_type size_type;
  78. //!Default constructor. Does nothing.
  79. //!Useful in combination with move semantics
  80. basic_managed_windows_shared_memory()
  81. {}
  82. //!Creates shared memory and creates and places the segment manager.
  83. //!This can throw.
  84. basic_managed_windows_shared_memory
  85. (create_only_t, const char *name,
  86. size_type size, const void *addr = 0, const permissions &perm = permissions())
  87. : m_wshm(create_only, name, size, read_write, addr,
  88. create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
  89. {}
  90. //!Creates shared memory and creates and places the segment manager if
  91. //!segment was not created. If segment was created it connects to the
  92. //!segment.
  93. //!This can throw.
  94. basic_managed_windows_shared_memory
  95. (open_or_create_t,
  96. const char *name, size_type size,
  97. const void *addr = 0,
  98. const permissions &perm = permissions())
  99. : m_wshm(open_or_create, name, size, read_write, addr,
  100. create_open_func_t(get_this_pointer(),
  101. ipcdetail::DoOpenOrCreate), perm)
  102. {}
  103. //!Connects to a created shared memory and its segment manager.
  104. //!This can throw.
  105. basic_managed_windows_shared_memory
  106. (open_only_t, const char* name, const void *addr = 0)
  107. : m_wshm(open_only, name, read_write, addr,
  108. create_open_func_t(get_this_pointer(),
  109. ipcdetail::DoOpen))
  110. {}
  111. //!Connects to a created shared memory and its segment manager
  112. //!in copy_on_write mode.
  113. //!This can throw.
  114. basic_managed_windows_shared_memory
  115. (open_copy_on_write_t, const char* name, const void *addr = 0)
  116. : m_wshm(open_only, name, copy_on_write, addr,
  117. create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
  118. {}
  119. //!Connects to a created shared memory and its segment manager
  120. //!in read-only mode.
  121. //!This can throw.
  122. basic_managed_windows_shared_memory
  123. (open_read_only_t, const char* name, const void *addr = 0)
  124. : base_t()
  125. , m_wshm(open_only, name, read_only, addr,
  126. create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
  127. {}
  128. //!Moves the ownership of "moved"'s managed memory to *this.
  129. //!Does not throw
  130. basic_managed_windows_shared_memory
  131. (BOOST_RV_REF(basic_managed_windows_shared_memory) moved)
  132. { this->swap(moved); }
  133. //!Moves the ownership of "moved"'s managed memory to *this.
  134. //!Does not throw
  135. basic_managed_windows_shared_memory &operator=(BOOST_RV_REF(basic_managed_windows_shared_memory) moved)
  136. {
  137. basic_managed_windows_shared_memory tmp(boost::move(moved));
  138. this->swap(tmp);
  139. return *this;
  140. }
  141. //!Destroys *this and indicates that the calling process is finished using
  142. //!the resource. All mapped regions are still valid after
  143. //!destruction. When all mapped regions and basic_managed_windows_shared_memory
  144. //!objects referring the shared memory are destroyed, the
  145. //!operating system will destroy the shared memory.
  146. ~basic_managed_windows_shared_memory()
  147. {}
  148. //!Swaps the ownership of the managed mapped memories managed by *this and other.
  149. //!Never throws.
  150. void swap(basic_managed_windows_shared_memory &other)
  151. {
  152. base_t::swap(other);
  153. m_wshm.swap(other.m_wshm);
  154. }
  155. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  156. //!Tries to find a previous named allocation address. Returns a memory
  157. //!buffer and the object count. If not found returned pointer is 0.
  158. //!Never throws.
  159. template <class T>
  160. std::pair<T*, size_type> find (char_ptr_holder_t name)
  161. {
  162. if(m_wshm.get_mapped_region().get_mode() == read_only){
  163. return base_t::template find_no_lock<T>(name);
  164. }
  165. else{
  166. return base_t::template find<T>(name);
  167. }
  168. }
  169. private:
  170. typename ipcdetail::wshmem_open_or_create<AllocationAlgorithm>::type m_wshm;
  171. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  172. };
  173. #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  174. //!Typedef for a default basic_managed_windows_shared_memory
  175. //!of narrow characters
  176. typedef basic_managed_windows_shared_memory
  177. <char
  178. ,rbtree_best_fit<mutex_family>
  179. ,iset_index>
  180. managed_windows_shared_memory;
  181. //!Typedef for a default basic_managed_windows_shared_memory
  182. //!of wide characters
  183. typedef basic_managed_windows_shared_memory
  184. <wchar_t
  185. ,rbtree_best_fit<mutex_family>
  186. ,iset_index>
  187. wmanaged_windows_shared_memory;
  188. #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  189. } //namespace interprocess {
  190. } //namespace boost {
  191. #include <boost/interprocess/detail/config_end.hpp>
  192. #endif //BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP