managed_mapped_file.hpp 8.5 KB

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