managed_shared_memory.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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
  34. <
  35. class CharType,
  36. class AllocationAlgorithm,
  37. template<class IndexConfig> class IndexType
  38. >
  39. struct shmem_open_or_create
  40. {
  41. static const std::size_t segment_manager_alignment = boost::move_detail::alignment_of
  42. < segment_manager
  43. < CharType
  44. , AllocationAlgorithm
  45. , IndexType>
  46. >::value;
  47. static const std::size_t final_segment_manager_alignment
  48. = segment_manager_alignment > AllocationAlgorithm::Alignment
  49. ? segment_manager_alignment : AllocationAlgorithm::Alignment;
  50. typedef ipcdetail::managed_open_or_create_impl
  51. < shared_memory_object
  52. , final_segment_manager_alignment
  53. , true
  54. , false> type;
  55. };
  56. } //namespace ipcdetail {
  57. //!A basic shared memory named object creation class. Initializes the
  58. //!shared memory segment. Inherits all basic functionality from
  59. //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
  60. template
  61. <
  62. class CharType,
  63. class AllocationAlgorithm,
  64. template<class IndexConfig> class IndexType
  65. >
  66. class basic_managed_shared_memory
  67. : public ipcdetail::basic_managed_memory_impl
  68. < CharType, AllocationAlgorithm, IndexType
  69. , ipcdetail::shmem_open_or_create<CharType, AllocationAlgorithm, IndexType>::type::ManagedOpenOrCreateUserOffset>
  70. , private ipcdetail::shmem_open_or_create<CharType, AllocationAlgorithm, IndexType>::type
  71. {
  72. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  73. typedef typename ipcdetail::shmem_open_or_create
  74. < CharType
  75. , AllocationAlgorithm
  76. , IndexType>::type base2_t;
  77. typedef ipcdetail::basic_managed_memory_impl
  78. <CharType, AllocationAlgorithm, IndexType,
  79. base2_t::ManagedOpenOrCreateUserOffset> base_t;
  80. typedef ipcdetail::create_open_func<base_t> create_open_func_t;
  81. basic_managed_shared_memory *get_this_pointer()
  82. { return this; }
  83. public:
  84. typedef shared_memory_object device_type;
  85. typedef typename base_t::size_type size_type;
  86. private:
  87. typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
  88. BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_shared_memory)
  89. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  90. public: //functions
  91. //!Destroys *this and indicates that the calling process is finished using
  92. //!the resource. The destructor function will deallocate
  93. //!any system resources allocated by the system for use by this process for
  94. //!this resource. The resource can still be opened again calling
  95. //!the open constructor overload. To erase the resource from the system
  96. //!use remove().
  97. ~basic_managed_shared_memory()
  98. {}
  99. //!Default constructor. Does nothing.
  100. //!Useful in combination with move semantics
  101. basic_managed_shared_memory()
  102. {}
  103. //!Creates shared memory and creates and places the segment manager.
  104. //!This can throw.
  105. basic_managed_shared_memory(create_only_t, const char *name,
  106. size_type size, const void *addr = 0, const permissions& perm = permissions())
  107. : base_t()
  108. , base2_t(create_only, name, size, read_write, addr,
  109. create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
  110. {}
  111. //!Creates shared memory and creates and places the segment manager if
  112. //!segment was not created. If segment was created it connects to the
  113. //!segment.
  114. //!This can throw.
  115. basic_managed_shared_memory (open_or_create_t,
  116. const char *name, size_type size,
  117. const void *addr = 0, const permissions& perm = permissions())
  118. : base_t()
  119. , base2_t(open_or_create, name, size, read_write, addr,
  120. create_open_func_t(get_this_pointer(),
  121. ipcdetail::DoOpenOrCreate), perm)
  122. {}
  123. //!Connects to a created shared memory and its segment manager.
  124. //!in copy_on_write mode.
  125. //!This can throw.
  126. basic_managed_shared_memory (open_copy_on_write_t, const char* name,
  127. const void *addr = 0)
  128. : base_t()
  129. , base2_t(open_only, name, copy_on_write, addr,
  130. create_open_func_t(get_this_pointer(),
  131. ipcdetail::DoOpen))
  132. {}
  133. //!Connects to a created shared memory and its segment manager.
  134. //!in read-only mode.
  135. //!This can throw.
  136. basic_managed_shared_memory (open_read_only_t, const char* name,
  137. const void *addr = 0)
  138. : base_t()
  139. , base2_t(open_only, name, read_only, addr,
  140. create_open_func_t(get_this_pointer(),
  141. ipcdetail::DoOpen))
  142. {}
  143. //!Connects to a created shared memory and its segment manager.
  144. //!This can throw.
  145. basic_managed_shared_memory (open_only_t, const char* name,
  146. const void *addr = 0)
  147. : base_t()
  148. , base2_t(open_only, name, read_write, addr,
  149. create_open_func_t(get_this_pointer(),
  150. ipcdetail::DoOpen))
  151. {}
  152. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  153. //!Creates shared memory and creates and places the segment manager.
  154. //!This can throw.
  155. //!
  156. //!Note: This function is only available on operating systems with
  157. //! native wchar_t APIs (e.g. Windows).
  158. basic_managed_shared_memory(create_only_t, const wchar_t *name,
  159. size_type size, const void *addr = 0, const permissions& perm = permissions())
  160. : base_t()
  161. , base2_t(create_only, name, size, read_write, addr,
  162. create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
  163. {}
  164. //!Creates shared memory and creates and places the segment manager if
  165. //!segment was not created. If segment was created it connects to the
  166. //!segment.
  167. //!This can throw.
  168. //!
  169. //!Note: This function is only available on operating systems with
  170. //! native wchar_t APIs (e.g. Windows).
  171. basic_managed_shared_memory (open_or_create_t,
  172. const wchar_t *name, size_type size,
  173. const void *addr = 0, const permissions& perm = permissions())
  174. : base_t()
  175. , base2_t(open_or_create, name, size, read_write, addr,
  176. create_open_func_t(get_this_pointer(),
  177. ipcdetail::DoOpenOrCreate), perm)
  178. {}
  179. //!Connects to a created shared memory and its segment manager.
  180. //!in copy_on_write mode.
  181. //!This can throw.
  182. //!
  183. //!Note: This function is only available on operating systems with
  184. //! native wchar_t APIs (e.g. Windows).
  185. basic_managed_shared_memory (open_copy_on_write_t, const wchar_t* name,
  186. const void *addr = 0)
  187. : base_t()
  188. , base2_t(open_only, name, copy_on_write, addr,
  189. create_open_func_t(get_this_pointer(),
  190. ipcdetail::DoOpen))
  191. {}
  192. //!Connects to a created shared memory and its segment manager.
  193. //!in read-only mode.
  194. //!This can throw.
  195. //!
  196. //!Note: This function is only available on operating systems with
  197. //! native wchar_t APIs (e.g. Windows).
  198. basic_managed_shared_memory (open_read_only_t, const wchar_t* name,
  199. const void *addr = 0)
  200. : base_t()
  201. , base2_t(open_only, name, read_only, addr,
  202. create_open_func_t(get_this_pointer(),
  203. ipcdetail::DoOpen))
  204. {}
  205. //!Connects to a created shared memory and its segment manager.
  206. //!This can throw.
  207. //!
  208. //!Note: This function is only available on operating systems with
  209. //! native wchar_t APIs (e.g. Windows).
  210. basic_managed_shared_memory (open_only_t, const wchar_t* name,
  211. const void *addr = 0)
  212. : base_t()
  213. , base2_t(open_only, name, read_write, addr,
  214. create_open_func_t(get_this_pointer(),
  215. ipcdetail::DoOpen))
  216. {}
  217. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  218. //!Moves the ownership of "moved"'s managed memory to *this.
  219. //!Does not throw
  220. basic_managed_shared_memory(BOOST_RV_REF(basic_managed_shared_memory) moved)
  221. {
  222. basic_managed_shared_memory tmp;
  223. this->swap(moved);
  224. tmp.swap(moved);
  225. }
  226. //!Moves the ownership of "moved"'s managed memory to *this.
  227. //!Does not throw
  228. basic_managed_shared_memory &operator=(BOOST_RV_REF(basic_managed_shared_memory) moved)
  229. {
  230. basic_managed_shared_memory tmp(boost::move(moved));
  231. this->swap(tmp);
  232. return *this;
  233. }
  234. //!Swaps the ownership of the managed shared memories managed by *this and other.
  235. //!Never throws.
  236. void swap(basic_managed_shared_memory &other)
  237. {
  238. base_t::swap(other);
  239. base2_t::swap(other);
  240. }
  241. //!Tries to resize the managed shared memory object so that we have
  242. //!room for more objects.
  243. //!
  244. //!This function is not synchronized so no other thread or process should
  245. //!be reading or writing the file
  246. static bool grow(const char *shmname, size_type extra_bytes)
  247. {
  248. return base_t::template grow
  249. <basic_managed_shared_memory>(shmname, extra_bytes);
  250. }
  251. //!Tries to resize the managed shared memory to minimized the size of the file.
  252. //!
  253. //!This function is not synchronized so no other thread or process should
  254. //!be reading or writing the file
  255. static bool shrink_to_fit(const char *shmname)
  256. {
  257. return base_t::template shrink_to_fit
  258. <basic_managed_shared_memory>(shmname);
  259. }
  260. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  261. //!Tries to resize the managed shared memory object so that we have
  262. //!room for more objects.
  263. //!
  264. //!This function is not synchronized so no other thread or process should
  265. //!be reading or writing the file
  266. //!
  267. //!Note: This function is only available on operating systems with
  268. //! native wchar_t APIs (e.g. Windows).
  269. static bool grow(const wchar_t *shmname, size_type extra_bytes)
  270. {
  271. return base_t::template grow
  272. <basic_managed_shared_memory>(shmname, extra_bytes);
  273. }
  274. //!Tries to resize the managed shared memory to minimized the size of the file.
  275. //!
  276. //!This function is not synchronized so no other thread or process should
  277. //!be reading or writing the file
  278. //!
  279. //!Note: This function is only available on operating systems with
  280. //! native wchar_t APIs (e.g. Windows).
  281. static bool shrink_to_fit(const wchar_t *shmname)
  282. {
  283. return base_t::template shrink_to_fit
  284. <basic_managed_shared_memory>(shmname);
  285. }
  286. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  287. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  288. //!Tries to find a previous named allocation address. Returns a memory
  289. //!buffer and the object count. If not found returned pointer is 0.
  290. //!Never throws.
  291. template <class T>
  292. std::pair<T*, size_type> find (char_ptr_holder_t name)
  293. {
  294. if(base2_t::get_mapped_region().get_mode() == read_only){
  295. return base_t::template find_no_lock<T>(name);
  296. }
  297. else{
  298. return base_t::template find<T>(name);
  299. }
  300. }
  301. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  302. };
  303. #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  304. //!Typedef for a default basic_managed_shared_memory
  305. //!of narrow characters
  306. typedef basic_managed_shared_memory
  307. <char
  308. ,rbtree_best_fit<mutex_family>
  309. ,iset_index>
  310. managed_shared_memory;
  311. //!Typedef for a default basic_managed_shared_memory
  312. //!of wide characters
  313. typedef basic_managed_shared_memory
  314. <wchar_t
  315. ,rbtree_best_fit<mutex_family>
  316. ,iset_index>
  317. wmanaged_shared_memory;
  318. //!Typedef for a default basic_managed_shared_memory
  319. //!of narrow characters to be placed in a fixed address
  320. typedef basic_managed_shared_memory
  321. <char
  322. ,rbtree_best_fit<mutex_family, void*>
  323. ,iset_index>
  324. fixed_managed_shared_memory;
  325. //!Typedef for a default basic_managed_shared_memory
  326. //!of narrow characters to be placed in a fixed address
  327. typedef basic_managed_shared_memory
  328. <wchar_t
  329. ,rbtree_best_fit<mutex_family, void*>
  330. ,iset_index>
  331. wfixed_managed_shared_memory;
  332. #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  333. } //namespace interprocess {
  334. } //namespace boost {
  335. #include <boost/interprocess/detail/config_end.hpp>
  336. #endif //BOOST_INTERPROCESS_MANAGED_SHARED_MEMORY_HPP