managed_xsi_shared_memory.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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<class AllocationAlgorithm>
  37. struct xsishmem_open_or_create
  38. {
  39. typedef ipcdetail::managed_open_or_create_impl //!FileBased, StoreDevice
  40. < xsi_shared_memory_file_wrapper, AllocationAlgorithm::Alignment, false, true> type;
  41. };
  42. } //namespace ipcdetail {
  43. //!A basic X/Open System Interface (XSI) shared memory named object creation class. Initializes the
  44. //!shared memory segment. 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_xsi_shared_memory
  53. : public ipcdetail::basic_managed_memory_impl
  54. <CharType, AllocationAlgorithm, IndexType
  55. ,ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset>
  56. , private ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type
  57. {
  58. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  59. public:
  60. typedef xsi_shared_memory_file_wrapper device_type;
  61. public:
  62. typedef typename ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type base2_t;
  63. typedef ipcdetail::basic_managed_memory_impl
  64. <CharType, AllocationAlgorithm, IndexType,
  65. base2_t::ManagedOpenOrCreateUserOffset> base_t;
  66. typedef ipcdetail::create_open_func<base_t> create_open_func_t;
  67. basic_managed_xsi_shared_memory *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_xsi_shared_memory)
  72. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  73. public: //functions
  74. typedef typename base_t::size_type size_type;
  75. //!Destroys *this and indicates that the calling process is finished using
  76. //!the resource. The destructor function will deallocate
  77. //!any system resources allocated by the system for use by this process for
  78. //!this resource. The resource can still be opened again calling
  79. //!the open constructor overload. To erase the resource from the system
  80. //!use remove().
  81. ~basic_managed_xsi_shared_memory()
  82. {}
  83. //!Default constructor. Does nothing.
  84. //!Useful in combination with move semantics
  85. basic_managed_xsi_shared_memory()
  86. {}
  87. //!Creates shared memory and creates and places the segment manager.
  88. //!This can throw.
  89. basic_managed_xsi_shared_memory(create_only_t, const xsi_key &key,
  90. std::size_t size, const void *addr = 0, const permissions& perm = permissions())
  91. : base_t()
  92. , base2_t(create_only, key, size, read_write, addr,
  93. create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
  94. {}
  95. //!Creates shared memory and creates and places the segment manager if
  96. //!segment was not created. If segment was created it connects to the
  97. //!segment.
  98. //!This can throw.
  99. basic_managed_xsi_shared_memory (open_or_create_t,
  100. const xsi_key &key, std::size_t size,
  101. const void *addr = 0, const permissions& perm = permissions())
  102. : base_t()
  103. , base2_t(open_or_create, key, size, read_write, addr,
  104. create_open_func_t(get_this_pointer(),
  105. ipcdetail::DoOpenOrCreate), perm)
  106. {}
  107. //!Connects to a created shared memory and its segment manager.
  108. //!in read-only mode.
  109. //!This can throw.
  110. basic_managed_xsi_shared_memory (open_read_only_t, const xsi_key &key,
  111. const void *addr = 0)
  112. : base_t()
  113. , base2_t(open_only, key, read_only, addr,
  114. create_open_func_t(get_this_pointer(),
  115. ipcdetail::DoOpen))
  116. {}
  117. //!Connects to a created shared memory and its segment manager.
  118. //!This can throw.
  119. basic_managed_xsi_shared_memory (open_only_t, const xsi_key &key,
  120. const void *addr = 0)
  121. : base_t()
  122. , base2_t(open_only, key, read_write, addr,
  123. create_open_func_t(get_this_pointer(),
  124. ipcdetail::DoOpen))
  125. {}
  126. //!Moves the ownership of "moved"'s managed memory to *this.
  127. //!Does not throw
  128. basic_managed_xsi_shared_memory(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved)
  129. {
  130. basic_managed_xsi_shared_memory tmp;
  131. this->swap(moved);
  132. tmp.swap(moved);
  133. }
  134. //!Moves the ownership of "moved"'s managed memory to *this.
  135. //!Does not throw
  136. basic_managed_xsi_shared_memory &operator=(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved)
  137. {
  138. basic_managed_xsi_shared_memory tmp(boost::move(moved));
  139. this->swap(tmp);
  140. return *this;
  141. }
  142. //!Swaps the ownership of the managed shared memories managed by *this and other.
  143. //!Never throws.
  144. void swap(basic_managed_xsi_shared_memory &other)
  145. {
  146. base_t::swap(other);
  147. base2_t::swap(other);
  148. }
  149. //!Erases a XSI shared memory object identified by shmid
  150. //!from the system.
  151. //!Returns false on error. Never throws
  152. static bool remove(int shmid)
  153. { return device_type::remove(shmid); }
  154. int get_shmid() const
  155. { return base2_t::get_device().get_shmid(); }
  156. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  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(base2_t::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. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  171. };
  172. #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  173. //!Typedef for a default basic_managed_xsi_shared_memory
  174. //!of narrow characters
  175. typedef basic_managed_xsi_shared_memory
  176. <char
  177. ,rbtree_best_fit<mutex_family>
  178. ,iset_index>
  179. managed_xsi_shared_memory;
  180. //!Typedef for a default basic_managed_xsi_shared_memory
  181. //!of wide characters
  182. typedef basic_managed_xsi_shared_memory
  183. <wchar_t
  184. ,rbtree_best_fit<mutex_family>
  185. ,iset_index>
  186. wmanaged_xsi_shared_memory;
  187. #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  188. } //namespace interprocess {
  189. } //namespace boost {
  190. #include <boost/interprocess/detail/config_end.hpp>
  191. #endif //BOOST_INTERPROCESS_MANAGED_XSI_SHARED_MEMORY_HPP