managed_mapped_file.hpp 13 KB

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