managed_xsi_shared_memory.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2008-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_XSI_SHARED_MEMORY_HPP
  11. #define BOOST_INTERPROCESS_MANAGED_XSI_SHARED_MEMORY_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. #if !defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
  22. #error "This header can't be used in operating systems without XSI (System V) shared memory support"
  23. #endif
  24. #include <boost/interprocess/detail/managed_memory_impl.hpp>
  25. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  26. #include <boost/interprocess/detail/xsi_shared_memory_file_wrapper.hpp>
  27. #include <boost/interprocess/creation_tags.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 xsishmem_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. < xsi_shared_memory_file_wrapper
  55. , final_segment_manager_alignment
  56. , false
  57. , true> type;
  58. };
  59. } //namespace ipcdetail {
  60. //!A basic X/Open System Interface (XSI) shared memory named object creation class. Initializes the
  61. //!shared memory segment. 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_xsi_shared_memory
  70. : public ipcdetail::basic_managed_memory_impl
  71. <CharType, AllocationAlgorithm, IndexType
  72. ,ipcdetail::xsishmem_open_or_create<CharType, AllocationAlgorithm, IndexType>
  73. ::type::ManagedOpenOrCreateUserOffset>
  74. , private ipcdetail::xsishmem_open_or_create
  75. <CharType, AllocationAlgorithm, IndexType>::type
  76. {
  77. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  78. public:
  79. typedef xsi_shared_memory_file_wrapper device_type;
  80. public:
  81. typedef typename ipcdetail::xsishmem_open_or_create
  82. <CharType, AllocationAlgorithm, IndexType>::type base2_t;
  83. typedef ipcdetail::basic_managed_memory_impl
  84. <CharType, AllocationAlgorithm, IndexType,
  85. base2_t::ManagedOpenOrCreateUserOffset> base_t;
  86. typedef ipcdetail::create_open_func<base_t> create_open_func_t;
  87. basic_managed_xsi_shared_memory *get_this_pointer()
  88. { return this; }
  89. private:
  90. typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
  91. BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_xsi_shared_memory)
  92. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  93. public: //functions
  94. typedef typename base_t::size_type size_type;
  95. //!Destroys *this and indicates that the calling process is finished using
  96. //!the resource. The destructor function will deallocate
  97. //!any system resources allocated by the system for use by this process for
  98. //!this resource. The resource can still be opened again calling
  99. //!the open constructor overload. To erase the resource from the system
  100. //!use remove().
  101. ~basic_managed_xsi_shared_memory()
  102. {}
  103. //!Default constructor. Does nothing.
  104. //!Useful in combination with move semantics
  105. basic_managed_xsi_shared_memory() BOOST_NOEXCEPT
  106. {}
  107. //!Creates shared memory and creates and places the segment manager.
  108. //!This can throw.
  109. basic_managed_xsi_shared_memory(create_only_t, const xsi_key &key,
  110. std::size_t size, const void *addr = 0, const permissions& perm = permissions())
  111. : base_t()
  112. , base2_t(create_only, key, size, read_write, addr,
  113. create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
  114. {}
  115. //!Creates shared memory and creates and places the segment manager if
  116. //!segment was not created. If segment was created it connects to the
  117. //!segment.
  118. //!This can throw.
  119. basic_managed_xsi_shared_memory (open_or_create_t,
  120. const xsi_key &key, std::size_t size,
  121. const void *addr = 0, const permissions& perm = permissions())
  122. : base_t()
  123. , base2_t(open_or_create, key, size, read_write, addr,
  124. create_open_func_t(get_this_pointer(),
  125. ipcdetail::DoOpenOrCreate), perm)
  126. {}
  127. //!Connects to a created shared memory and its segment manager.
  128. //!in read-only mode.
  129. //!This can throw.
  130. basic_managed_xsi_shared_memory (open_read_only_t, const xsi_key &key,
  131. const void *addr = 0)
  132. : base_t()
  133. , base2_t(open_only, key, read_only, addr,
  134. create_open_func_t(get_this_pointer(),
  135. ipcdetail::DoOpen))
  136. {}
  137. //!Connects to a created shared memory and its segment manager.
  138. //!This can throw.
  139. basic_managed_xsi_shared_memory (open_only_t, const xsi_key &key,
  140. const void *addr = 0)
  141. : base_t()
  142. , base2_t(open_only, key, read_write, addr,
  143. create_open_func_t(get_this_pointer(),
  144. ipcdetail::DoOpen))
  145. {}
  146. //!Moves the ownership of "moved"'s managed memory to *this.
  147. //!Does not throw
  148. basic_managed_xsi_shared_memory(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved) BOOST_NOEXCEPT
  149. {
  150. basic_managed_xsi_shared_memory tmp;
  151. this->swap(moved);
  152. tmp.swap(moved);
  153. }
  154. //!Moves the ownership of "moved"'s managed memory to *this.
  155. //!Does not throw
  156. basic_managed_xsi_shared_memory &operator=(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved) BOOST_NOEXCEPT
  157. {
  158. basic_managed_xsi_shared_memory tmp(boost::move(moved));
  159. this->swap(tmp);
  160. return *this;
  161. }
  162. //!Swaps the ownership of the managed shared memories managed by *this and other.
  163. //!Never throws.
  164. void swap(basic_managed_xsi_shared_memory &other) BOOST_NOEXCEPT
  165. {
  166. base_t::swap(other);
  167. base2_t::swap(other);
  168. }
  169. //!Erases a XSI shared memory object identified by shmid
  170. //!from the system.
  171. //!Returns false on error. Never throws
  172. static bool remove(int shmid)
  173. { return device_type::remove(shmid); }
  174. int get_shmid() const BOOST_NOEXCEPT
  175. { return base2_t::get_device().get_shmid(); }
  176. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  177. //!Tries to find a previous named allocation address. Returns a memory
  178. //!buffer and the object count. If not found returned pointer is 0.
  179. //!Never throws.
  180. template <class T>
  181. std::pair<T*, std::size_t> find (char_ptr_holder_t name)
  182. {
  183. if(base2_t::get_mapped_region().get_mode() == read_only){
  184. return base_t::template find_no_lock<T>(name);
  185. }
  186. else{
  187. return base_t::template find<T>(name);
  188. }
  189. }
  190. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  191. };
  192. #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  193. //!Typedef for a default basic_managed_xsi_shared_memory
  194. //!of narrow characters
  195. typedef basic_managed_xsi_shared_memory
  196. <char
  197. ,rbtree_best_fit<mutex_family>
  198. ,iset_index>
  199. managed_xsi_shared_memory;
  200. //!Typedef for a default basic_managed_xsi_shared_memory
  201. //!of wide characters
  202. typedef basic_managed_xsi_shared_memory
  203. <wchar_t
  204. ,rbtree_best_fit<mutex_family>
  205. ,iset_index>
  206. wmanaged_xsi_shared_memory;
  207. #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  208. } //namespace interprocess {
  209. } //namespace boost {
  210. #include <boost/interprocess/detail/config_end.hpp>
  211. #endif //BOOST_INTERPROCESS_MANAGED_XSI_SHARED_MEMORY_HPP