xsi_shared_memory.hpp 6.4 KB

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