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
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/detail/managed_memory_impl.hpp>
30 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
31 #include <boost/interprocess/detail/xsi_shared_memory_file_wrapper.hpp>
32 #include <boost/interprocess/creation_tags.hpp>
33 //These includes needed to fulfill default template parameters of
34 //predeclarations in interprocess_fwd.hpp
35 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
36 #include <boost/interprocess/sync/mutex_family.hpp>
37 #include <boost/interprocess/indexes/iset_index.hpp>
41 namespace interprocess {
45 template<class AllocationAlgorithm>
46 struct xsishmem_open_or_create
48 typedef ipcdetail::managed_open_or_create_impl //!FileBased, StoreDevice
49 < xsi_shared_memory_file_wrapper, AllocationAlgorithm::Alignment, false, true> type;
52 } //namespace ipcdetail {
54 //!A basic X/Open System Interface (XSI) shared memory named object creation class. Initializes the
55 //!shared memory segment. Inherits all basic functionality from
56 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
60 class AllocationAlgorithm,
61 template<class IndexConfig> class IndexType
63 class basic_managed_xsi_shared_memory
64 : public ipcdetail::basic_managed_memory_impl
65 <CharType, AllocationAlgorithm, IndexType
66 ,ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset>
67 , private ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type
69 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
71 typedef xsi_shared_memory_file_wrapper device_type;
74 typedef typename ipcdetail::xsishmem_open_or_create<AllocationAlgorithm>::type base2_t;
75 typedef ipcdetail::basic_managed_memory_impl
76 <CharType, AllocationAlgorithm, IndexType,
77 base2_t::ManagedOpenOrCreateUserOffset> base_t;
79 typedef ipcdetail::create_open_func<base_t> create_open_func_t;
81 basic_managed_xsi_shared_memory *get_this_pointer()
85 typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
86 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_xsi_shared_memory)
87 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
90 typedef typename base_t::size_type size_type;
92 //!Destroys *this and indicates that the calling process is finished using
93 //!the resource. The destructor function will deallocate
94 //!any system resources allocated by the system for use by this process for
95 //!this resource. The resource can still be opened again calling
96 //!the open constructor overload. To erase the resource from the system
98 ~basic_managed_xsi_shared_memory()
101 //!Default constructor. Does nothing.
102 //!Useful in combination with move semantics
103 basic_managed_xsi_shared_memory()
106 //!Creates shared memory and creates and places the segment manager.
108 basic_managed_xsi_shared_memory(create_only_t, const xsi_key &key,
109 std::size_t size, const void *addr = 0, const permissions& perm = permissions())
111 , base2_t(create_only, key, size, read_write, addr,
112 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
115 //!Creates shared memory and creates and places the segment manager if
116 //!segment was not created. If segment was created it connects to the
119 basic_managed_xsi_shared_memory (open_or_create_t,
120 const xsi_key &key, std::size_t size,
121 const void *addr = 0, const permissions& perm = permissions())
123 , base2_t(open_or_create, key, size, read_write, addr,
124 create_open_func_t(get_this_pointer(),
125 ipcdetail::DoOpenOrCreate), perm)
128 //!Connects to a created shared memory and its segment manager.
129 //!in read-only mode.
131 basic_managed_xsi_shared_memory (open_read_only_t, const xsi_key &key,
132 const void *addr = 0)
134 , base2_t(open_only, key, read_only, addr,
135 create_open_func_t(get_this_pointer(),
139 //!Connects to a created shared memory and its segment manager.
141 basic_managed_xsi_shared_memory (open_only_t, const xsi_key &key,
142 const void *addr = 0)
144 , base2_t(open_only, key, read_write, addr,
145 create_open_func_t(get_this_pointer(),
149 //!Moves the ownership of "moved"'s managed memory to *this.
151 basic_managed_xsi_shared_memory(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved)
153 basic_managed_xsi_shared_memory tmp;
158 //!Moves the ownership of "moved"'s managed memory to *this.
160 basic_managed_xsi_shared_memory &operator=(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved)
162 basic_managed_xsi_shared_memory tmp(boost::move(moved));
167 //!Swaps the ownership of the managed shared memories managed by *this and other.
169 void swap(basic_managed_xsi_shared_memory &other)
172 base2_t::swap(other);
175 //!Erases a XSI shared memory object identified by shmid
177 //!Returns false on error. Never throws
178 static bool remove(int shmid)
179 { return device_type::remove(shmid); }
181 int get_shmid() const
182 { return base2_t::get_device().get_shmid(); }
184 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
186 //!Tries to find a previous named allocation address. Returns a memory
187 //!buffer and the object count. If not found returned pointer is 0.
190 std::pair<T*, std::size_t> find (char_ptr_holder_t name)
192 if(base2_t::get_mapped_region().get_mode() == read_only){
193 return base_t::template find_no_lock<T>(name);
196 return base_t::template find<T>(name);
200 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
203 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
205 //!Typedef for a default basic_managed_xsi_shared_memory
206 //!of narrow characters
207 typedef basic_managed_xsi_shared_memory
209 ,rbtree_best_fit<mutex_family>
211 managed_xsi_shared_memory;
213 //!Typedef for a default basic_managed_xsi_shared_memory
214 //!of wide characters
215 typedef basic_managed_xsi_shared_memory
217 ,rbtree_best_fit<mutex_family>
219 wmanaged_xsi_shared_memory;
221 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
223 } //namespace interprocess {
224 } //namespace boost {
226 #include <boost/interprocess/detail/config_end.hpp>
228 #endif //BOOST_INTERPROCESS_MANAGED_XSI_SHARED_MEMORY_HPP