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