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

http://hadesmem.googlecode.com/ · C++ Header · 189 lines · 111 code · 31 blank · 47 comment · 3 complexity · 36ee7201a87caeb00bba5e2499f160e0 MD5 · raw file

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