1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_HPP
12 #define BOOST_INTERPROCESS_XSI_SHARED_MEMORY_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
25 #if !defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
26 #error "This header can't be used in operating systems without XSI (System V) shared memory support"
29 #include <boost/interprocess/creation_tags.hpp>
30 #include <boost/interprocess/exceptions.hpp>
31 #include <boost/interprocess/detail/utilities.hpp>
33 #include <boost/interprocess/detail/os_file_functions.hpp>
34 #include <boost/interprocess/interprocess_fwd.hpp>
35 #include <boost/interprocess/exceptions.hpp>
36 #include <boost/interprocess/xsi_key.hpp>
37 #include <boost/interprocess/permissions.hpp>
38 #include <boost/interprocess/detail/simple_swap.hpp>
40 #include <boost/move/utility_core.hpp>
42 #include <boost/cstdint.hpp>
50 //!Describes a class representing a native xsi shared memory.
53 namespace interprocess {
55 //!A class that wraps XSI (System V) shared memory.
56 //!Unlike shared_memory_object, xsi_shared_memory needs a valid
57 //!xsi_key to identify a shared memory object.
59 //!Warning: XSI shared memory and interprocess portable
60 //!shared memory (boost::interprocess::shared_memory_object)
61 //!can't communicate between them.
62 class xsi_shared_memory
64 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
65 //Non-copyable and non-assignable
66 BOOST_MOVABLE_BUT_NOT_COPYABLE(xsi_shared_memory)
67 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
70 //!Default constructor.
71 //!Represents an empty xsi_shared_memory.
74 //!Initializes *this with a shmid previously obtained (possibly from another process)
75 //!This lower-level initializer allows shared memory mapping without having a key.
76 xsi_shared_memory(open_only_t, int shmid)
80 //!Creates a new XSI shared memory from 'key', with size "size" and permissions "perm".
81 //!If the shared memory previously exists, throws an error.
82 xsi_shared_memory(create_only_t, const xsi_key &key, std::size_t size, const permissions& perm = permissions())
83 { this->priv_open_or_create(ipcdetail::DoCreate, key, perm, size); }
85 //!Opens an existing shared memory with identifier 'key' or creates a new XSI shared memory from
86 //!identifier 'key', with size "size" and permissions "perm".
87 xsi_shared_memory(open_or_create_t, const xsi_key &key, std::size_t size, const permissions& perm = permissions())
88 { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, key, perm, size); }
90 //!Tries to open a XSI shared memory with identifier 'key'
91 //!If the shared memory does not previously exist, it throws an error.
92 xsi_shared_memory(open_only_t, const xsi_key &key)
93 { this->priv_open_or_create(ipcdetail::DoOpen, key, permissions(), 0); }
95 //!Moves the ownership of "moved"'s shared memory object to *this.
96 //!After the call, "moved" does not represent any shared memory object.
98 xsi_shared_memory(BOOST_RV_REF(xsi_shared_memory) moved)
100 { this->swap(moved); }
102 //!Moves the ownership of "moved"'s shared memory to *this.
103 //!After the call, "moved" does not represent any shared memory.
105 xsi_shared_memory &operator=(BOOST_RV_REF(xsi_shared_memory) moved)
107 xsi_shared_memory tmp(boost::move(moved));
112 //!Swaps two xsi_shared_memorys. Does not throw
113 void swap(xsi_shared_memory &other);
115 //!Destroys *this. The shared memory won't be destroyed, just
116 //!this connection to it. Use remove() to destroy the shared memory.
117 ~xsi_shared_memory();
119 //!Returns the shared memory ID that
120 //!identifies the shared memory
121 int get_shmid() const;
123 //!Returns the mapping handle.
125 mapping_handle_t get_mapping_handle() const;
127 //!Erases the XSI shared memory object identified by shmid
129 //!Returns false on error. Never throws
130 static bool remove(int shmid);
132 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
135 //!Closes a previously opened file mapping. Never throws.
136 bool priv_open_or_create( ipcdetail::create_enum_t type
138 , const permissions& perm
141 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
144 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
146 inline xsi_shared_memory::xsi_shared_memory()
150 inline xsi_shared_memory::~xsi_shared_memory()
153 inline int xsi_shared_memory::get_shmid() const
156 inline void xsi_shared_memory::swap(xsi_shared_memory &other)
158 (simple_swap)(m_shmid, other.m_shmid);
161 inline mapping_handle_t xsi_shared_memory::get_mapping_handle() const
162 { mapping_handle_t mhnd = { m_shmid, true}; return mhnd; }
164 inline bool xsi_shared_memory::priv_open_or_create
165 (ipcdetail::create_enum_t type, const xsi_key &key, const permissions& permissions, std::size_t size)
167 int perm = permissions.get_permissions();
172 case ipcdetail::DoOpen:
175 case ipcdetail::DoCreate:
176 shmflg |= IPC_CREAT | IPC_EXCL;
178 case ipcdetail::DoOpenOrCreate:
183 error_info err = other_error;
184 throw interprocess_exception(err);
188 int ret = ::shmget(key.get_key(), size, shmflg);
190 if((type == ipcdetail::DoOpen) && (-1 != ret)){
193 ret = ::shmctl(ret, IPC_STAT, &xsi_ds);
194 size = xsi_ds.shm_segsz;
197 error_info err = system_error_code();
198 throw interprocess_exception(err);
205 inline bool xsi_shared_memory::remove(int shmid)
206 { return -1 != ::shmctl(shmid, IPC_RMID, 0); }
208 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
210 } //namespace interprocess {
211 } //namespace boost {
213 #include <boost/interprocess/detail/config_end.hpp>
215 #endif //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_HPP