managed_shared_memory.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_SHARED_MEMORY_HPP
  11. #define BOOST_INTERPROCESS_MANAGED_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_memory_impl.hpp>
  18. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  19. #include <boost/interprocess/shared_memory_object.hpp>
  20. #include <boost/interprocess/creation_tags.hpp>
  21. #include <boost/interprocess/permissions.hpp>
  22. //These includes needed to fulfill default template parameters of
  23. //predeclarations in interprocess_fwd.hpp
  24. #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
  25. #include <boost/interprocess/sync/mutex_family.hpp>
  26. namespace boost {
  27. namespace interprocess {
  28. namespace ipcdetail {
  29. template<class AllocationAlgorithm>
  30. struct shmem_open_or_create
  31. {
  32. typedef ipcdetail::managed_open_or_create_impl
  33. < shared_memory_object, AllocationAlgorithm::Alignment, true, false> type;
  34. };
  35. } //namespace ipcdetail {
  36. //!A basic shared memory named object creation class. Initializes the
  37. //!shared memory segment. Inherits all basic functionality from
  38. //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
  39. template
  40. <
  41. class CharType,
  42. class AllocationAlgorithm,
  43. template<class IndexConfig> class IndexType
  44. >
  45. class basic_managed_shared_memory
  46. : public ipcdetail::basic_managed_memory_impl
  47. <CharType, AllocationAlgorithm, IndexType
  48. ,ipcdetail::shmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset>
  49. , private ipcdetail::shmem_open_or_create<AllocationAlgorithm>::type
  50. {
  51. /// @cond
  52. typedef ipcdetail::basic_managed_memory_impl
  53. <CharType, AllocationAlgorithm, IndexType,
  54. ipcdetail::shmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset> base_t;
  55. typedef typename ipcdetail::shmem_open_or_create<AllocationAlgorithm>::type base2_t;
  56. typedef ipcdetail::create_open_func<base_t> create_open_func_t;
  57. basic_managed_shared_memory *get_this_pointer()
  58. { return this; }
  59. public:
  60. typedef shared_memory_object device_type;
  61. typedef typename base_t::size_type size_type;
  62. private:
  63. typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
  64. BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_shared_memory)
  65. /// @endcond
  66. public: //functions
  67. //!Destroys *this and indicates that the calling process is finished using
  68. //!the resource. The destructor function will deallocate
  69. //!any system resources allocated by the system for use by this process for
  70. //!this resource. The resource can still be opened again calling
  71. //!the open constructor overload. To erase the resource from the system
  72. //!use remove().
  73. ~basic_managed_shared_memory()
  74. {}
  75. //!Default constructor. Does nothing.
  76. //!Useful in combination with move semantics
  77. basic_managed_shared_memory()
  78. {}
  79. //!Creates shared memory and creates and places the segment manager.
  80. //!This can throw.
  81. basic_managed_shared_memory(create_only_t, const char *name,
  82. size_type size, const void *addr = 0, const permissions& perm = permissions())
  83. : base_t()
  84. , base2_t(create_only, name, size, read_write, addr,
  85. create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
  86. {}
  87. //!Creates shared memory and creates and places the segment manager if
  88. //!segment was not created. If segment was created it connects to the
  89. //!segment.
  90. //!This can throw.
  91. basic_managed_shared_memory (open_or_create_t,
  92. const char *name, size_type size,
  93. const void *addr = 0, const permissions& perm = permissions())
  94. : base_t()
  95. , base2_t(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. //!in copy_on_write mode.
  101. //!This can throw.
  102. basic_managed_shared_memory (open_copy_on_write_t, const char* name,
  103. const void *addr = 0)
  104. : base_t()
  105. , base2_t(open_only, name, copy_on_write, addr,
  106. create_open_func_t(get_this_pointer(),
  107. ipcdetail::DoOpen))
  108. {}
  109. //!Connects to a created shared memory and its segment manager.
  110. //!in read-only mode.
  111. //!This can throw.
  112. basic_managed_shared_memory (open_read_only_t, const char* name,
  113. const void *addr = 0)
  114. : base_t()
  115. , base2_t(open_only, name, read_only, addr,
  116. create_open_func_t(get_this_pointer(),
  117. ipcdetail::DoOpen))
  118. {}
  119. //!Connects to a created shared memory and its segment manager.
  120. //!This can throw.
  121. basic_managed_shared_memory (open_only_t, const char* name,
  122. const void *addr = 0)
  123. : base_t()
  124. , base2_t(open_only, name, read_write, addr,
  125. create_open_func_t(get_this_pointer(),
  126. ipcdetail::DoOpen))
  127. {}
  128. //!Moves the ownership of "moved"'s managed memory to *this.
  129. //!Does not throw
  130. basic_managed_shared_memory(BOOST_RV_REF(basic_managed_shared_memory) moved)
  131. {
  132. basic_managed_shared_memory tmp;
  133. this->swap(moved);
  134. tmp.swap(moved);
  135. }
  136. //!Moves the ownership of "moved"'s managed memory to *this.
  137. //!Does not throw
  138. basic_managed_shared_memory &operator=(BOOST_RV_REF(basic_managed_shared_memory) moved)
  139. {
  140. basic_managed_shared_memory tmp(boost::move(moved));
  141. this->swap(tmp);
  142. return *this;
  143. }
  144. //!Swaps the ownership of the managed shared memories managed by *this and other.
  145. //!Never throws.
  146. void swap(basic_managed_shared_memory &other)
  147. {
  148. base_t::swap(other);
  149. base2_t::swap(other);
  150. }
  151. //!Tries to resize the managed shared memory object so that we have
  152. //!room for more objects.
  153. //!
  154. //!This function is not synchronized so no other thread or process should
  155. //!be reading or writing the file
  156. static bool grow(const char *shmname, size_type extra_bytes)
  157. {
  158. return base_t::template grow
  159. <basic_managed_shared_memory>(shmname, extra_bytes);
  160. }
  161. //!Tries to resize the managed shared memory to minimized the size of the file.
  162. //!
  163. //!This function is not synchronized so no other thread or process should
  164. //!be reading or writing the file
  165. static bool shrink_to_fit(const char *shmname)
  166. {
  167. return base_t::template shrink_to_fit
  168. <basic_managed_shared_memory>(shmname);
  169. }
  170. /// @cond
  171. //!Tries to find a previous named allocation address. Returns a memory
  172. //!buffer and the object count. If not found returned pointer is 0.
  173. //!Never throws.
  174. template <class T>
  175. std::pair<T*, size_type> find (char_ptr_holder_t name)
  176. {
  177. if(base2_t::get_mapped_region().get_mode() == read_only){
  178. return base_t::template find_no_lock<T>(name);
  179. }
  180. else{
  181. return base_t::template find<T>(name);
  182. }
  183. }
  184. /// @endcond
  185. };
  186. } //namespace interprocess {
  187. } //namespace boost {
  188. #include <boost/interprocess/detail/config_end.hpp>
  189. #endif //BOOST_INTERPROCESS_MANAGED_SHARED_MEMORY_HPP