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

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