1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2009-2011. 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_DEVICE_HPP
12 #define BOOST_INTERPROCESS_XSI_SHARED_MEMORY_DEVICE_HPP
14 #include <boost/interprocess/detail/config_begin.hpp>
15 #include <boost/interprocess/detail/workaround.hpp>
16 #include <boost/detail/workaround.hpp>
18 #if defined(BOOST_INTERPROCESS_WINDOWS)
19 #error "This header can't be used in Windows operating systems"
22 #include <boost/interprocess/creation_tags.hpp>
23 #include <boost/interprocess/exceptions.hpp>
24 #include <boost/interprocess/detail/utilities.hpp>
25 #include <boost/interprocess/detail/os_file_functions.hpp>
26 #include <boost/interprocess/detail/tmp_dir_helpers.hpp>
27 #include <boost/interprocess/interprocess_fwd.hpp>
28 #include <boost/interprocess/exceptions.hpp>
30 #include <boost/interprocess/xsi_shared_memory.hpp>
31 #include <boost/interprocess/sync/xsi/xsi_named_mutex.hpp>
32 #include <boost/interprocess/mapped_region.hpp>
33 #include <boost/interprocess/sync/scoped_lock.hpp>
35 #include <boost/cstdint.hpp>
40 //!Describes a class representing a native xsi shared memory.
43 namespace interprocess {
45 class xsi_shared_memory_device
48 BOOST_MOVABLE_BUT_NOT_COPYABLE(xsi_shared_memory_file_wrapper)
53 xsi_shared_memory_device();
55 xsi_shared_memory_device(create_only_t, const char *name, mode_t mode, std::size_t size)
56 { this->priv_open_or_create_name_only(ipcdetail::DoCreate, name, mode, size); }
58 xsi_shared_memory_device(open_or_create_t, const char *name, mode_t mode, std::size_t size)
59 { this->priv_open_or_create_name_only(ipcdetail::DoOpenOrCreate, name, mode, size); }
61 xsi_shared_memory_device(open_only_t, const char *name, mode_t mode)
62 { this->priv_open_or_create_name_only(ipcdetail::DoOpen, name, mode, 0); }
64 xsi_shared_memory_device(create_only_t, const char *filepath, boost::uint8_t id, mode_t mode, std::size_t size)
65 { this->priv_open_or_create_name_id(ipcdetail::DoCreate, name, id, mode, size); }
67 xsi_shared_memory_device(open_or_create_t, const char *filepath, boost::uint8_t id, mode_t mode, std::size_t size)
68 { this->priv_open_or_create_name_id(ipcdetail::DoOpenOrCreate, id, name, mode, size); }
70 xsi_shared_memory_device(open_only_t, const char *filepath, boost::uint8_t id, mode_t mode)
71 { this->priv_open_or_create_name_id(ipcdetail::DoOpen, name, id, mode, 0); }
73 xsi_shared_memory_device(BOOST_RV_REF(xsi_shared_memory_device) moved)
74 { this->swap(moved); }
76 xsi_shared_memory_device &operator=(BOOST_RV_REF(xsi_shared_memory_device) moved)
78 xsi_shared_memory_device tmp(boost::move(moved));
83 //!Swaps two xsi_shared_memory_device. Does not throw
84 void swap(xsi_shared_memory_device &other);
86 //!Destroys *this. The shared memory won't be destroyed, just
87 //!this connection to it. Use remove() to destroy the shared memory.
88 ~xsi_shared_memory_device();
90 //!Returns the name of the
92 const char *get_name() const;
94 //!Returns the shared memory ID that
95 //!identifies the shared memory
96 int get_shmid() const;
100 mode_t get_mode() const;
102 //!Returns the mapping handle.
104 mapping_handle_t get_mapping_handle() const;
106 //!Erases a XSI shared memory object identified by shmname
108 //!Returns false on error. Never throws
109 static bool remove(const char *shmname);
111 //!Erases the XSI shared memory object identified by shmid
113 //!Returns false on error. Never throws
114 static bool remove(int shmid);
119 struct info_constants_t
121 static const std::size_t MaxName = 32;
122 static const std::size_t FirstID = 2;
123 static const std::size_t LastID = 256;
124 static const std::size_t NumID = LastID - FirstID;
131 char buf[info_constants_t<0>::MaxName];
132 } names[info_constants_t<0>::NumID];
135 static void priv_obtain_index(mapped_region &m, xsi_named_mutex &m, std::string &path);
136 static bool priv_remove_dead_memory(info_t *info, const char *path);
138 bool priv_open_or_create_name_only( ipcdetail::create_enum_t type
139 , const char *shmname
142 bool priv_open_or_create_name_id( ipcdetail::create_enum_t type
143 , const char *shmname
147 xsi_shared_memory m_shm;
154 const std::size_t xsi_shared_memory_device::info_constants_t<Dummy>::MaxName;
157 const std::size_t xsi_shared_memory_device::info_constants_t<Dummy>::FirstID;
160 const std::size_t xsi_shared_memory_device::info_constants_t<Dummy>::LastID;
163 const std::size_t xsi_shared_memory_device::info_constants_t<Dummy>::NumID;
167 inline xsi_shared_memory_device::xsi_shared_memory_device()
168 : m_shm(), m_mode(invalid_mode), m_name()
171 inline xsi_shared_memory_device::~xsi_shared_memory_device()
174 inline const char *xsi_shared_memory_device::get_name() const
175 { return m_name.c_str(); }
177 inline void xsi_shared_memory_device::swap(xsi_shared_memory_device &other)
179 m_shm.swap(other.m_shm);
180 std::swap(m_mode, other.m_mode);
181 m_name.swap(other.m_name);
184 inline mapping_handle_t xsi_shared_memory_device::get_mapping_handle() const
185 { return m_shm.get_mapping_handle(); }
187 inline mode_t xsi_shared_memory_device::get_mode() const
190 inline int xsi_shared_memory::get_shmid() const
191 { return m_shm.get_shmid(); }
193 inline void xsi_shared_memory_device::priv_obtain_index
194 (mapped_region ®, xsi_named_mutex &mut, std::string &path)
196 const char *const filename = "xsi_shm_emulation_file";
198 p.set_unrestricted();
199 std::string xsi_shm_emulation_file_path;
200 ipcdetail::create_tmp_and_clean_old_and_get_filename(filename, xsi_shm_emulation_file_path);
201 ipcdetail::create_or_open_file(xsi_shm_emulation_file_path.c_str(), read_write, p);
202 const std::size_t MemSize = sizeof(info_t);
204 xsi_shared_memory index_shm(open_or_create, xsi_shm_emulation_file_path.c_str(), 1, MemSize, 0666);
205 mapped_region r(index_shm, read_write, 0, MemSize, 0);
206 xsi_named_mutex m(open_or_create, xsi_shm_emulation_file_path.c_str(), 2, 0666);
207 reg = boost::move(r);
208 mut = boost::move(m);
209 path.swap(xsi_shm_emulation_file_path);
212 inline bool xsi_shared_memory_device::priv_remove_dead_memory
213 (xsi_shared_memory_device::info_t *info, const char *path)
215 bool removed = false;
216 for(std::size_t i = 0; i != info_constants_t<0>::NumID; ++i){
217 if(info->names[i].buf[0]){
219 xsi_shared_memory temp( open_only, path, i+info_constants_t<0>::FirstID, 0600);
221 catch(interprocess_exception &e){
222 if(e.get_error_code() == not_found_error){
223 std::memset(info->names[i].buf, 0, info_constants_t<0>::MaxName);
232 inline bool xsi_shared_memory_device::priv_open_or_create_name_id
233 (ipcdetail::create_enum_t type, const char *filepath, mode_t mode, std::size_t size)
236 if (mode != read_write && mode != read_only){
237 error_info err = other_error;
238 throw interprocess_exception(err);
241 int perm = (mode == read_only) ? (0444) : (0666);
243 if(type == ipcdetail::DoOpen){
245 error_info err = not_found_error;
246 throw interprocess_exception(err);
248 xsi_shared_memory temp(open_only, filepath, id, perm);
249 m_shm = boost::move(temp);
251 else if(type == ipcdetail::DoCreate){
253 xsi_shared_memory temp(create_only, filepath, id, size, perm);
254 std::strcpy(info->names[target_entry].buf, shmname);
255 m_shm = boost::move(temp);
257 else{ // if(type == ipcdetail::DoOpenOrCreate){
258 xsi_shared_memory temp(open_or_create, filepath, id, size, perm);
259 m_shm = boost::move(temp);
267 inline bool xsi_shared_memory_device::priv_open_or_create_name_only
268 (ipcdetail::create_enum_t type, const char *shmname, mode_t mode, std::size_t size)
271 if (mode != read_write && mode != read_only){
272 error_info err = other_error;
273 throw interprocess_exception(err);
276 if (std::strlen(shmname) >= (info_constants_t<0>::MaxName)){
277 error_info err = other_error;
278 throw interprocess_exception(err);
282 //Obtain index and index lock
283 mapped_region region;
285 std::string xsi_shm_emulation_file_path;
286 priv_obtain_index(region, mut, xsi_shm_emulation_file_path);
287 info_t *info = static_cast<info_t *>(region.get_address());
288 scoped_lock<xsi_named_mutex> lock(mut);
290 //Find the correct entry or the first empty index
292 int target_entry = -1;
295 for(std::size_t i = 0; i != info_constants_t<0>::NumID; ++i){
296 if(target_entry < 0 && !info->names[i].buf[0]){
297 target_entry = static_cast<int>(i);
299 else if(0 == std::strcmp(info->names[i].buf, shmname)){
301 target_entry = static_cast<int>(i);
305 if(target_entry < 0){
306 if(!tries || !priv_remove_dead_memory(info, xsi_shm_emulation_file_path.c_str())){
307 error_info err = out_of_resource_error;
308 throw interprocess_exception(err);
312 //Now handle the result
313 int perm = (mode == read_only) ? (0444) : (0666);
314 if(type == ipcdetail::DoOpen){
316 error_info err = not_found_error;
317 throw interprocess_exception(err);
319 xsi_shared_memory temp( open_only, xsi_shm_emulation_file_path.c_str()
320 , target_entry+info_constants_t<0>::FirstID, perm);
321 m_shm = boost::move(temp);
325 if(type == ipcdetail::DoCreate){
327 xsi_shared_memory temp( create_only, xsi_shm_emulation_file_path.c_str()
328 , target_entry+info_constants_t<0>::FirstID, size, perm);
329 std::strcpy(info->names[target_entry].buf, shmname);
330 m_shm = boost::move(temp);
332 else{ // if(type == ipcdetail::DoOpenOrCreate){
333 xsi_shared_memory temp( open_or_create, xsi_shm_emulation_file_path.c_str()
334 , target_entry+info_constants_t<0>::FirstID, size, perm);
336 std::memset(info->names[target_entry].buf, 0, info_constants_t<0>::MaxName);
337 std::strcpy(info->names[target_entry].buf, shmname);
339 m_shm = boost::move(temp);
349 inline bool xsi_shared_memory_device::remove(const char *shmname)
352 //Obtain index and index lockss
353 mapped_region region;
355 std::string xsi_shm_emulation_file_path;
356 priv_obtain_index(region, mut, xsi_shm_emulation_file_path);
357 scoped_lock<xsi_named_mutex> lock(mut);
358 info_t *info = static_cast<info_t *>(region.get_address());
360 //Now check and remove
361 bool removed = false;
363 for(std::size_t i = 0; i != info_constants_t<0>::NumID; ++i){
364 if(0 == std::strcmp(info->names[i].buf, name)){
365 xsi_shared_memory temp( open_only, xsi_shm_emulation_file_path.c_str()
366 , i+info_constants_t<0>::FirstID);
367 if(!xsi_shared_memory::remove(temp.get_shmid()) && (system_error_code() != invalid_argument)){
370 std::memset(info->names[i].buf, 0, info_constants_t<0>::MaxName);
382 inline bool xsi_shared_memory_device::remove(int shmid)
383 { return xsi_shared_memory::remove(shmid); }
387 } //namespace interprocess {
388 } //namespace boost {
390 #include <boost/interprocess/detail/config_end.hpp>
392 #endif //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_DEVICE_HPP