managed_shared_memory.hpp 8.9 KB

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