1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2008-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_MANAGED_XSI_SHARED_MEMORY_HPP
12 #define BOOST_INTERPROCESS_MANAGED_XSI_SHARED_MEMORY_HPP
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #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"
25 #include <boost/interprocess/detail/managed_memory_impl.hpp>
26 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
27 #include <boost/interprocess/detail/xsi_shared_memory_file_wrapper.hpp>
28 #include <boost/interprocess/creation_tags.hpp>
29 //These includes needed to fulfill default template parameters of
30 //predeclarations in interprocess_fwd.hpp
31 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
32 #include <boost/interprocess/sync/mutex_family.hpp>
33 #include <boost/interprocess/indexes/iset_index.hpp>
37 namespace interprocess {
41 template<class AllocationAlgorithm>
42 struct xsishmem_open_or_create
44 typedef ipcdetail::managed_open_or_create_impl //!FileBased, StoreDevice
45 < xsi_shared_memory_file_wrapper, AllocationAlgorithm::Alignment, false, true> type;
48 } //namespace ipcdetail {
50 //!A basic X/Open System Interface (XSI) shared memory named object creation class. Initializes the
51 //!shared memory segment. Inherits all basic functionality from
52 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
56 class AllocationAlgorithm,
57 template<class IndexConfig> class IndexType
59 class basic_managed_xsi_shared_memory
60 : public ipcdetail::basic_managed_memory_impl
61 <CharType, AllocationAlgorithm, IndexType
62 ,ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset>
63 , private ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type
65 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
67 typedef xsi_shared_memory_file_wrapper device_type;
70 typedef typename ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type base2_t;
71 typedef ipcdetail::basic_managed_memory_impl
72 <CharType, AllocationAlgorithm, IndexType,
73 base2_t::ManagedOpenOrCreateUserOffset> base_t;
75 typedef ipcdetail::create_open_func<base_t> create_open_func_t;
77 basic_managed_xsi_shared_memory *get_this_pointer()
81 typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
82 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_xsi_shared_memory)
83 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
86 typedef typename base_t::size_type size_type;
88 //!Destroys *this and indicates that the calling process is finished using
89 //!the resource. The destructor function will deallocate
90 //!any system resources allocated by the system for use by this process for
91 //!this resource. The resource can still be opened again calling
92 //!the open constructor overload. To erase the resource from the system
94 ~basic_managed_xsi_shared_memory()
97 //!Default constructor. Does nothing.
98 //!Useful in combination with move semantics
99 basic_managed_xsi_shared_memory()
102 //!Creates shared memory and creates and places the segment manager.
104 basic_managed_xsi_shared_memory(create_only_t, const xsi_key &key,
105 std::size_t size, const void *addr = 0, const permissions& perm = permissions())
107 , base2_t(create_only, key, size, read_write, addr,
108 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
111 //!Creates shared memory and creates and places the segment manager if
112 //!segment was not created. If segment was created it connects to the
115 basic_managed_xsi_shared_memory (open_or_create_t,
116 const xsi_key &key, std::size_t size,
117 const void *addr = 0, const permissions& perm = permissions())
119 , base2_t(open_or_create, key, size, read_write, addr,
120 create_open_func_t(get_this_pointer(),
121 ipcdetail::DoOpenOrCreate), perm)
124 //!Connects to a created shared memory and its segment manager.
125 //!in read-only mode.
127 basic_managed_xsi_shared_memory (open_read_only_t, const xsi_key &key,
128 const void *addr = 0)
130 , base2_t(open_only, key, read_only, addr,
131 create_open_func_t(get_this_pointer(),
135 //!Connects to a created shared memory and its segment manager.
137 basic_managed_xsi_shared_memory (open_only_t, const xsi_key &key,
138 const void *addr = 0)
140 , base2_t(open_only, key, read_write, addr,
141 create_open_func_t(get_this_pointer(),
145 //!Moves the ownership of "moved"'s managed memory to *this.
147 basic_managed_xsi_shared_memory(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved)
149 basic_managed_xsi_shared_memory tmp;
154 //!Moves the ownership of "moved"'s managed memory to *this.
156 basic_managed_xsi_shared_memory &operator=(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved)
158 basic_managed_xsi_shared_memory tmp(boost::move(moved));
163 //!Swaps the ownership of the managed shared memories managed by *this and other.
165 void swap(basic_managed_xsi_shared_memory &other)
168 base2_t::swap(other);
171 //!Erases a XSI shared memory object identified by shmid
173 //!Returns false on error. Never throws
174 static bool remove(int shmid)
175 { return device_type::remove(shmid); }
177 int get_shmid() const
178 { return base2_t::get_device().get_shmid(); }
180 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
182 //!Tries to find a previous named allocation address. Returns a memory
183 //!buffer and the object count. If not found returned pointer is 0.
186 std::pair<T*, std::size_t> find (char_ptr_holder_t name)
188 if(base2_t::get_mapped_region().get_mode() == read_only){
189 return base_t::template find_no_lock<T>(name);
192 return base_t::template find<T>(name);
196 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
199 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
201 //!Typedef for a default basic_managed_xsi_shared_memory
202 //!of narrow characters
203 typedef basic_managed_xsi_shared_memory
205 ,rbtree_best_fit<mutex_family>
207 managed_xsi_shared_memory;
209 //!Typedef for a default basic_managed_xsi_shared_memory
210 //!of wide characters
211 typedef basic_managed_xsi_shared_memory
213 ,rbtree_best_fit<mutex_family>
215 wmanaged_xsi_shared_memory;
217 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
219 } //namespace interprocess {
220 } //namespace boost {
222 #include <boost/interprocess/detail/config_end.hpp>
224 #endif //BOOST_INTERPROCESS_MANAGED_XSI_SHARED_MEMORY_HPP