managed_windows_shared_memory.hpp 7.1 KB

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