managed_mapped_file.hpp 7.9 KB

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