Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2009-2012. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_XSI_SHARED_MEMORY_HPP
|
Chris@16
|
13
|
Chris@101
|
14 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
15 # include <boost/config.hpp>
|
Chris@101
|
16 #endif
|
Chris@101
|
17 #
|
Chris@101
|
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@101
|
19 # pragma once
|
Chris@101
|
20 #endif
|
Chris@101
|
21
|
Chris@16
|
22 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
23 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 #if !defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
|
Chris@16
|
26 #error "This header can't be used in operating systems without XSI (System V) shared memory support"
|
Chris@16
|
27 #endif
|
Chris@16
|
28
|
Chris@16
|
29 #include <boost/interprocess/creation_tags.hpp>
|
Chris@16
|
30 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
31 #include <boost/interprocess/detail/utilities.hpp>
|
Chris@101
|
32
|
Chris@16
|
33 #include <boost/interprocess/detail/os_file_functions.hpp>
|
Chris@16
|
34 #include <boost/interprocess/interprocess_fwd.hpp>
|
Chris@16
|
35 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
36 #include <boost/interprocess/xsi_key.hpp>
|
Chris@16
|
37 #include <boost/interprocess/permissions.hpp>
|
Chris@101
|
38 #include <boost/interprocess/detail/simple_swap.hpp>
|
Chris@101
|
39 // move
|
Chris@101
|
40 #include <boost/move/utility_core.hpp>
|
Chris@101
|
41 // other boost
|
Chris@101
|
42 #include <boost/cstdint.hpp>
|
Chris@101
|
43 // std
|
Chris@101
|
44 #include <cstddef>
|
Chris@101
|
45 // OS
|
Chris@16
|
46 #include <sys/shm.h>
|
Chris@101
|
47
|
Chris@16
|
48
|
Chris@16
|
49 //!\file
|
Chris@16
|
50 //!Describes a class representing a native xsi shared memory.
|
Chris@16
|
51
|
Chris@16
|
52 namespace boost {
|
Chris@16
|
53 namespace interprocess {
|
Chris@16
|
54
|
Chris@16
|
55 //!A class that wraps XSI (System V) shared memory.
|
Chris@16
|
56 //!Unlike shared_memory_object, xsi_shared_memory needs a valid
|
Chris@16
|
57 //!xsi_key to identify a shared memory object.
|
Chris@16
|
58 //!
|
Chris@16
|
59 //!Warning: XSI shared memory and interprocess portable
|
Chris@16
|
60 //!shared memory (boost::interprocess::shared_memory_object)
|
Chris@16
|
61 //!can't communicate between them.
|
Chris@16
|
62 class xsi_shared_memory
|
Chris@16
|
63 {
|
Chris@101
|
64 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
65 //Non-copyable and non-assignable
|
Chris@16
|
66 BOOST_MOVABLE_BUT_NOT_COPYABLE(xsi_shared_memory)
|
Chris@101
|
67 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
68
|
Chris@16
|
69 public:
|
Chris@16
|
70 //!Default constructor.
|
Chris@16
|
71 //!Represents an empty xsi_shared_memory.
|
Chris@16
|
72 xsi_shared_memory();
|
Chris@16
|
73
|
Chris@16
|
74 //!Initializes *this with a shmid previously obtained (possibly from another process)
|
Chris@16
|
75 //!This lower-level initializer allows shared memory mapping without having a key.
|
Chris@16
|
76 xsi_shared_memory(open_only_t, int shmid)
|
Chris@16
|
77 : m_shmid (shmid)
|
Chris@16
|
78 {}
|
Chris@16
|
79
|
Chris@16
|
80 //!Creates a new XSI shared memory from 'key', with size "size" and permissions "perm".
|
Chris@16
|
81 //!If the shared memory previously exists, throws an error.
|
Chris@16
|
82 xsi_shared_memory(create_only_t, const xsi_key &key, std::size_t size, const permissions& perm = permissions())
|
Chris@16
|
83 { this->priv_open_or_create(ipcdetail::DoCreate, key, perm, size); }
|
Chris@16
|
84
|
Chris@16
|
85 //!Opens an existing shared memory with identifier 'key' or creates a new XSI shared memory from
|
Chris@16
|
86 //!identifier 'key', with size "size" and permissions "perm".
|
Chris@16
|
87 xsi_shared_memory(open_or_create_t, const xsi_key &key, std::size_t size, const permissions& perm = permissions())
|
Chris@16
|
88 { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, key, perm, size); }
|
Chris@16
|
89
|
Chris@16
|
90 //!Tries to open a XSI shared memory with identifier 'key'
|
Chris@16
|
91 //!If the shared memory does not previously exist, it throws an error.
|
Chris@16
|
92 xsi_shared_memory(open_only_t, const xsi_key &key)
|
Chris@16
|
93 { this->priv_open_or_create(ipcdetail::DoOpen, key, permissions(), 0); }
|
Chris@16
|
94
|
Chris@16
|
95 //!Moves the ownership of "moved"'s shared memory object to *this.
|
Chris@16
|
96 //!After the call, "moved" does not represent any shared memory object.
|
Chris@16
|
97 //!Does not throw
|
Chris@16
|
98 xsi_shared_memory(BOOST_RV_REF(xsi_shared_memory) moved)
|
Chris@16
|
99 : m_shmid(-1)
|
Chris@16
|
100 { this->swap(moved); }
|
Chris@16
|
101
|
Chris@16
|
102 //!Moves the ownership of "moved"'s shared memory to *this.
|
Chris@16
|
103 //!After the call, "moved" does not represent any shared memory.
|
Chris@16
|
104 //!Does not throw
|
Chris@16
|
105 xsi_shared_memory &operator=(BOOST_RV_REF(xsi_shared_memory) moved)
|
Chris@16
|
106 {
|
Chris@16
|
107 xsi_shared_memory tmp(boost::move(moved));
|
Chris@16
|
108 this->swap(tmp);
|
Chris@16
|
109 return *this;
|
Chris@16
|
110 }
|
Chris@16
|
111
|
Chris@16
|
112 //!Swaps two xsi_shared_memorys. Does not throw
|
Chris@16
|
113 void swap(xsi_shared_memory &other);
|
Chris@16
|
114
|
Chris@16
|
115 //!Destroys *this. The shared memory won't be destroyed, just
|
Chris@16
|
116 //!this connection to it. Use remove() to destroy the shared memory.
|
Chris@16
|
117 ~xsi_shared_memory();
|
Chris@16
|
118
|
Chris@16
|
119 //!Returns the shared memory ID that
|
Chris@16
|
120 //!identifies the shared memory
|
Chris@16
|
121 int get_shmid() const;
|
Chris@16
|
122
|
Chris@16
|
123 //!Returns the mapping handle.
|
Chris@16
|
124 //!Never throws
|
Chris@16
|
125 mapping_handle_t get_mapping_handle() const;
|
Chris@16
|
126
|
Chris@16
|
127 //!Erases the XSI shared memory object identified by shmid
|
Chris@16
|
128 //!from the system.
|
Chris@16
|
129 //!Returns false on error. Never throws
|
Chris@16
|
130 static bool remove(int shmid);
|
Chris@16
|
131
|
Chris@101
|
132 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
133 private:
|
Chris@16
|
134
|
Chris@16
|
135 //!Closes a previously opened file mapping. Never throws.
|
Chris@16
|
136 bool priv_open_or_create( ipcdetail::create_enum_t type
|
Chris@16
|
137 , const xsi_key &key
|
Chris@16
|
138 , const permissions& perm
|
Chris@16
|
139 , std::size_t size);
|
Chris@16
|
140 int m_shmid;
|
Chris@101
|
141 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
142 };
|
Chris@16
|
143
|
Chris@101
|
144 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
145
|
Chris@16
|
146 inline xsi_shared_memory::xsi_shared_memory()
|
Chris@16
|
147 : m_shmid(-1)
|
Chris@16
|
148 {}
|
Chris@16
|
149
|
Chris@16
|
150 inline xsi_shared_memory::~xsi_shared_memory()
|
Chris@16
|
151 {}
|
Chris@16
|
152
|
Chris@16
|
153 inline int xsi_shared_memory::get_shmid() const
|
Chris@16
|
154 { return m_shmid; }
|
Chris@16
|
155
|
Chris@16
|
156 inline void xsi_shared_memory::swap(xsi_shared_memory &other)
|
Chris@16
|
157 {
|
Chris@101
|
158 (simple_swap)(m_shmid, other.m_shmid);
|
Chris@16
|
159 }
|
Chris@16
|
160
|
Chris@16
|
161 inline mapping_handle_t xsi_shared_memory::get_mapping_handle() const
|
Chris@16
|
162 { mapping_handle_t mhnd = { m_shmid, true}; return mhnd; }
|
Chris@16
|
163
|
Chris@16
|
164 inline bool xsi_shared_memory::priv_open_or_create
|
Chris@16
|
165 (ipcdetail::create_enum_t type, const xsi_key &key, const permissions& permissions, std::size_t size)
|
Chris@16
|
166 {
|
Chris@16
|
167 int perm = permissions.get_permissions();
|
Chris@16
|
168 perm &= 0x01FF;
|
Chris@16
|
169 int shmflg = perm;
|
Chris@16
|
170
|
Chris@16
|
171 switch(type){
|
Chris@16
|
172 case ipcdetail::DoOpen:
|
Chris@16
|
173 shmflg |= 0;
|
Chris@16
|
174 break;
|
Chris@16
|
175 case ipcdetail::DoCreate:
|
Chris@16
|
176 shmflg |= IPC_CREAT | IPC_EXCL;
|
Chris@16
|
177 break;
|
Chris@16
|
178 case ipcdetail::DoOpenOrCreate:
|
Chris@16
|
179 shmflg |= IPC_CREAT;
|
Chris@16
|
180 break;
|
Chris@16
|
181 default:
|
Chris@16
|
182 {
|
Chris@16
|
183 error_info err = other_error;
|
Chris@16
|
184 throw interprocess_exception(err);
|
Chris@16
|
185 }
|
Chris@16
|
186 }
|
Chris@16
|
187
|
Chris@16
|
188 int ret = ::shmget(key.get_key(), size, shmflg);
|
Chris@16
|
189 int shmid = ret;
|
Chris@16
|
190 if((type == ipcdetail::DoOpen) && (-1 != ret)){
|
Chris@16
|
191 //Now get the size
|
Chris@16
|
192 ::shmid_ds xsi_ds;
|
Chris@16
|
193 ret = ::shmctl(ret, IPC_STAT, &xsi_ds);
|
Chris@16
|
194 size = xsi_ds.shm_segsz;
|
Chris@16
|
195 }
|
Chris@16
|
196 if(-1 == ret){
|
Chris@16
|
197 error_info err = system_error_code();
|
Chris@16
|
198 throw interprocess_exception(err);
|
Chris@16
|
199 }
|
Chris@16
|
200
|
Chris@16
|
201 m_shmid = shmid;
|
Chris@16
|
202 return true;
|
Chris@16
|
203 }
|
Chris@16
|
204
|
Chris@16
|
205 inline bool xsi_shared_memory::remove(int shmid)
|
Chris@16
|
206 { return -1 != ::shmctl(shmid, IPC_RMID, 0); }
|
Chris@16
|
207
|
Chris@101
|
208 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
209
|
Chris@16
|
210 } //namespace interprocess {
|
Chris@16
|
211 } //namespace boost {
|
Chris@16
|
212
|
Chris@16
|
213 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
214
|
Chris@16
|
215 #endif //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_HPP
|