managed_xsi_shared_memory.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #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. #if !defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
  18. #error "This header can't be used in operating systems without XSI (System V) shared memory support"
  19. #endif
  20. #include <boost/interprocess/detail/managed_memory_impl.hpp>
  21. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  22. #include <boost/interprocess/detail/xsi_shared_memory_file_wrapper.hpp>
  23. #include <boost/interprocess/creation_tags.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. namespace ipcdetail {
  32. template<class AllocationAlgorithm>
  33. struct xsishmem_open_or_create
  34. {
  35. typedef ipcdetail::managed_open_or_create_impl //!FileBased, StoreDevice
  36. < xsi_shared_memory_file_wrapper, AllocationAlgorithm::Alignment, false, true> type;
  37. };
  38. } //namespace ipcdetail {
  39. //!A basic X/Open System Interface (XSI) shared memory named object creation class. Initializes the
  40. //!shared memory segment. Inherits all basic functionality from
  41. //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
  42. template
  43. <
  44. class CharType,
  45. class AllocationAlgorithm,
  46. template<class IndexConfig> class IndexType
  47. >
  48. class basic_managed_xsi_shared_memory
  49. : public ipcdetail::basic_managed_memory_impl
  50. <CharType, AllocationAlgorithm, IndexType
  51. ,ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset>
  52. , private ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type
  53. {
  54. /// @cond
  55. public:
  56. typedef xsi_shared_memory_file_wrapper device_type;
  57. public:
  58. typedef typename ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type base2_t;
  59. typedef ipcdetail::basic_managed_memory_impl
  60. <CharType, AllocationAlgorithm, IndexType,
  61. base2_t::ManagedOpenOrCreateUserOffset> base_t;
  62. typedef ipcdetail::create_open_func<base_t> create_open_func_t;
  63. basic_managed_xsi_shared_memory *get_this_pointer()
  64. { return this; }
  65. private:
  66. typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
  67. BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_xsi_shared_memory)
  68. /// @endcond
  69. public: //functions
  70. typedef typename base_t::size_type size_type;
  71. //!Destroys *this and indicates that the calling process is finished using
  72. //!the resource. The destructor function will deallocate
  73. //!any system resources allocated by the system for use by this process for
  74. //!this resource. The resource can still be opened again calling
  75. //!the open constructor overload. To erase the resource from the system
  76. //!use remove().
  77. ~basic_managed_xsi_shared_memory()
  78. {}
  79. //!Default constructor. Does nothing.
  80. //!Useful in combination with move semantics
  81. basic_managed_xsi_shared_memory()
  82. {}
  83. //!Creates shared memory and creates and places the segment manager.
  84. //!This can throw.
  85. basic_managed_xsi_shared_memory(create_only_t, const xsi_key &key,
  86. std::size_t size, const void *addr = 0, const permissions& perm = permissions())
  87. : base_t()
  88. , base2_t(create_only, key, size, read_write, addr,
  89. create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
  90. {}
  91. //!Creates shared memory and creates and places the segment manager if
  92. //!segment was not created. If segment was created it connects to the
  93. //!segment.
  94. //!This can throw.
  95. basic_managed_xsi_shared_memory (open_or_create_t,
  96. const xsi_key &key, std::size_t size,
  97. const void *addr = 0, const permissions& perm = permissions())
  98. : base_t()
  99. , base2_t(open_or_create, key, size, read_write, addr,
  100. create_open_func_t(get_this_pointer(),
  101. ipcdetail::DoOpenOrCreate), perm)
  102. {}
  103. //!Connects to a created shared memory and its segment manager.
  104. //!in read-only mode.
  105. //!This can throw.
  106. basic_managed_xsi_shared_memory (open_read_only_t, const xsi_key &key,
  107. const void *addr = 0)
  108. : base_t()
  109. , base2_t(open_only, key, read_only, addr,
  110. create_open_func_t(get_this_pointer(),
  111. ipcdetail::DoOpen))
  112. {}
  113. //!Connects to a created shared memory and its segment manager.
  114. //!This can throw.
  115. basic_managed_xsi_shared_memory (open_only_t, const xsi_key &key,
  116. const void *addr = 0)
  117. : base_t()
  118. , base2_t(open_only, key, read_write, addr,
  119. create_open_func_t(get_this_pointer(),
  120. ipcdetail::DoOpen))
  121. {}
  122. //!Moves the ownership of "moved"'s managed memory to *this.
  123. //!Does not throw
  124. basic_managed_xsi_shared_memory(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved)
  125. {
  126. basic_managed_xsi_shared_memory tmp;
  127. this->swap(moved);
  128. tmp.swap(moved);
  129. }
  130. //!Moves the ownership of "moved"'s managed memory to *this.
  131. //!Does not throw
  132. basic_managed_xsi_shared_memory &operator=(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved)
  133. {
  134. basic_managed_xsi_shared_memory tmp(boost::move(moved));
  135. this->swap(tmp);
  136. return *this;
  137. }
  138. //!Swaps the ownership of the managed shared memories managed by *this and other.
  139. //!Never throws.
  140. void swap(basic_managed_xsi_shared_memory &other)
  141. {
  142. base_t::swap(other);
  143. base2_t::swap(other);
  144. }
  145. //!Erases a XSI shared memory object identified by shmid
  146. //!from the system.
  147. //!Returns false on error. Never throws
  148. static bool remove(int shmid)
  149. { return device_type::remove(shmid); }
  150. int get_shmid() const
  151. { return base2_t::get_device().get_shmid(); }
  152. /// @cond
  153. //!Tries to find a previous named allocation address. Returns a memory
  154. //!buffer and the object count. If not found returned pointer is 0.
  155. //!Never throws.
  156. template <class T>
  157. std::pair<T*, std::size_t> find (char_ptr_holder_t name)
  158. {
  159. if(base2_t::get_mapped_region().get_mode() == read_only){
  160. return base_t::template find_no_lock<T>(name);
  161. }
  162. else{
  163. return base_t::template find<T>(name);
  164. }
  165. }
  166. /// @endcond
  167. };
  168. } //namespace interprocess {
  169. } //namespace boost {
  170. #include <boost/interprocess/detail/config_end.hpp>
  171. #endif //BOOST_INTERPROCESS_MANAGED_XSI_SHARED_MEMORY_HPP