xsi_shared_memory.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2009-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_XSI_SHARED_MEMORY_HPP
  11. #define BOOST_INTERPROCESS_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/creation_tags.hpp>
  25. #include <boost/interprocess/exceptions.hpp>
  26. #include <boost/interprocess/detail/utilities.hpp>
  27. #include <boost/interprocess/detail/os_file_functions.hpp>
  28. #include <boost/interprocess/interprocess_fwd.hpp>
  29. #include <boost/interprocess/exceptions.hpp>
  30. #include <boost/interprocess/xsi_key.hpp>
  31. #include <boost/interprocess/permissions.hpp>
  32. #include <boost/interprocess/detail/simple_swap.hpp>
  33. // move
  34. #include <boost/move/utility_core.hpp>
  35. // other boost
  36. #include <boost/cstdint.hpp>
  37. // std
  38. #include <cstddef>
  39. // OS
  40. #include <sys/shm.h>
  41. //!\file
  42. //!Describes a class representing a native xsi shared memory.
  43. namespace boost {
  44. namespace interprocess {
  45. //!A class that wraps XSI (System V) shared memory.
  46. //!Unlike shared_memory_object, xsi_shared_memory needs a valid
  47. //!xsi_key to identify a shared memory object.
  48. //!
  49. //!Warning: XSI shared memory and interprocess portable
  50. //!shared memory (boost::interprocess::shared_memory_object)
  51. //!can't communicate between them.
  52. class xsi_shared_memory
  53. {
  54. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  55. //Non-copyable and non-assignable
  56. BOOST_MOVABLE_BUT_NOT_COPYABLE(xsi_shared_memory)
  57. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  58. public:
  59. //!Default constructor.
  60. //!Represents an empty xsi_shared_memory.
  61. xsi_shared_memory() BOOST_NOEXCEPT;
  62. //!Initializes *this with a shmid previously obtained (possibly from another process)
  63. //!This lower-level initializer allows shared memory mapping without having a key.
  64. xsi_shared_memory(open_only_t, int shmid)
  65. : m_shmid (shmid)
  66. {}
  67. //!Creates a new XSI shared memory from 'key', with size "size" and permissions "perm".
  68. //!If the shared memory previously exists, throws an error.
  69. xsi_shared_memory(create_only_t, const xsi_key &key, std::size_t size, const permissions& perm = permissions())
  70. { this->priv_open_or_create(ipcdetail::DoCreate, key, perm, size); }
  71. //!Opens an existing shared memory with identifier 'key' or creates a new XSI shared memory from
  72. //!identifier 'key', with size "size" and permissions "perm".
  73. xsi_shared_memory(open_or_create_t, const xsi_key &key, std::size_t size, const permissions& perm = permissions())
  74. { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, key, perm, size); }
  75. //!Tries to open a XSI shared memory with identifier 'key'
  76. //!If the shared memory does not previously exist, it throws an error.
  77. xsi_shared_memory(open_only_t, const xsi_key &key)
  78. { this->priv_open_or_create(ipcdetail::DoOpen, key, permissions(), 0); }
  79. //!Moves the ownership of "moved"'s shared memory object to *this.
  80. //!After the call, "moved" does not represent any shared memory object.
  81. //!Does not throw
  82. xsi_shared_memory(BOOST_RV_REF(xsi_shared_memory) moved) BOOST_NOEXCEPT
  83. : m_shmid(-1)
  84. { this->swap(moved); }
  85. //!Moves the ownership of "moved"'s shared memory to *this.
  86. //!After the call, "moved" does not represent any shared memory.
  87. //!Does not throw
  88. xsi_shared_memory &operator=(BOOST_RV_REF(xsi_shared_memory) moved) BOOST_NOEXCEPT
  89. {
  90. xsi_shared_memory tmp(boost::move(moved));
  91. this->swap(tmp);
  92. return *this;
  93. }
  94. //!Swaps two xsi_shared_memorys. Does not throw
  95. void swap(xsi_shared_memory &other) BOOST_NOEXCEPT;
  96. //!Destroys *this. The shared memory won't be destroyed, just
  97. //!this connection to it. Use remove() to destroy the shared memory.
  98. ~xsi_shared_memory();
  99. //!Returns the shared memory ID that
  100. //!identifies the shared memory
  101. int get_shmid() const BOOST_NOEXCEPT;
  102. //!Returns the mapping handle.
  103. //!Never throws
  104. mapping_handle_t get_mapping_handle() const BOOST_NOEXCEPT;
  105. //!Erases the XSI shared memory object identified by shmid
  106. //!from the system.
  107. //!Returns false on error. Never throws
  108. static bool remove(int shmid);
  109. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  110. private:
  111. //!Closes a previously opened file mapping. Never throws.
  112. bool priv_open_or_create( ipcdetail::create_enum_t type
  113. , const xsi_key &key
  114. , const permissions& perm
  115. , std::size_t size);
  116. int m_shmid;
  117. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  118. };
  119. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  120. inline xsi_shared_memory::xsi_shared_memory() BOOST_NOEXCEPT
  121. : m_shmid(-1)
  122. {}
  123. inline xsi_shared_memory::~xsi_shared_memory()
  124. {}
  125. inline int xsi_shared_memory::get_shmid() const BOOST_NOEXCEPT
  126. { return m_shmid; }
  127. inline void xsi_shared_memory::swap(xsi_shared_memory &other) BOOST_NOEXCEPT
  128. {
  129. (simple_swap)(m_shmid, other.m_shmid);
  130. }
  131. inline mapping_handle_t xsi_shared_memory::get_mapping_handle() const BOOST_NOEXCEPT
  132. { mapping_handle_t mhnd = { m_shmid, true}; return mhnd; }
  133. inline bool xsi_shared_memory::priv_open_or_create
  134. (ipcdetail::create_enum_t type, const xsi_key &key, const permissions& permissions, std::size_t size)
  135. {
  136. int perm = permissions.get_permissions();
  137. perm &= 0x01FF;
  138. int shmflg = perm;
  139. switch(type){
  140. case ipcdetail::DoOpen:
  141. shmflg |= 0;
  142. break;
  143. case ipcdetail::DoCreate:
  144. shmflg |= IPC_CREAT | IPC_EXCL;
  145. break;
  146. case ipcdetail::DoOpenOrCreate:
  147. shmflg |= IPC_CREAT;
  148. break;
  149. default:
  150. {
  151. error_info err = other_error;
  152. throw interprocess_exception(err);
  153. }
  154. }
  155. int ret = ::shmget(key.get_key(), size, shmflg);
  156. int shmid = ret;
  157. if((type == ipcdetail::DoOpen) && (-1 != ret)){
  158. //Now get the size
  159. ::shmid_ds xsi_ds;
  160. ret = ::shmctl(ret, IPC_STAT, &xsi_ds);
  161. size = xsi_ds.shm_segsz;
  162. }
  163. if(-1 == ret){
  164. error_info err = system_error_code();
  165. throw interprocess_exception(err);
  166. }
  167. m_shmid = shmid;
  168. return true;
  169. }
  170. inline bool xsi_shared_memory::remove(int shmid)
  171. { return -1 != ::shmctl(shmid, IPC_RMID, 0); }
  172. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  173. } //namespace interprocess {
  174. } //namespace boost {
  175. #include <boost/interprocess/detail/config_end.hpp>
  176. #endif //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_HPP