/Src/Dependencies/Boost/boost/interprocess/managed_mapped_file.hpp

http://hadesmem.googlecode.com/ · C++ Header · 206 lines · 119 code · 29 blank · 58 comment · 3 complexity · 05ee7ac4b106e2212ead8615ab517190 MD5 · raw file

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