Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / interprocess / managed_xsi_shared_memory.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_INTERPROCESS_MANAGED_XSI_SHARED_MEMORY_HPP
12 #define BOOST_INTERPROCESS_MANAGED_XSI_SHARED_MEMORY_HPP
13
14 #if defined(_MSC_VER)
15 #  pragma once
16 #endif
17
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20
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"
23 #endif
24
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>
34
35 namespace boost {
36
37 namespace interprocess {
38
39 namespace ipcdetail {
40
41 template<class AllocationAlgorithm>
42 struct xsishmem_open_or_create
43 {
44    typedef  ipcdetail::managed_open_or_create_impl                 //!FileBased, StoreDevice
45       < xsi_shared_memory_file_wrapper, AllocationAlgorithm::Alignment, false, true> type;
46 };
47
48 }  //namespace ipcdetail {
49
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>
53 template
54       <
55          class CharType,
56          class AllocationAlgorithm,
57          template<class IndexConfig> class IndexType
58       >
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
64 {
65    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
66    public:
67    typedef xsi_shared_memory_file_wrapper device_type;
68
69    public:
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;
74
75    typedef ipcdetail::create_open_func<base_t>        create_open_func_t;
76
77    basic_managed_xsi_shared_memory *get_this_pointer()
78    {  return this;   }
79
80    private:
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
84
85    public: //functions
86    typedef typename base_t::size_type              size_type;
87
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
93    //!use remove().
94    ~basic_managed_xsi_shared_memory()
95    {}
96
97    //!Default constructor. Does nothing.
98    //!Useful in combination with move semantics
99    basic_managed_xsi_shared_memory()
100    {}
101
102    //!Creates shared memory and creates and places the segment manager.
103    //!This can throw.
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())
106       : base_t()
107       , base2_t(create_only, key, size, read_write, addr,
108                 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
109    {}
110
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
113    //!segment.
114    //!This can throw.
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())
118       : base_t()
119       , base2_t(open_or_create, key, size, read_write, addr,
120                 create_open_func_t(get_this_pointer(),
121                 ipcdetail::DoOpenOrCreate), perm)
122    {}
123
124    //!Connects to a created shared memory and its segment manager.
125    //!in read-only mode.
126    //!This can throw.
127    basic_managed_xsi_shared_memory (open_read_only_t, const xsi_key &key,
128                                 const void *addr = 0)
129       : base_t()
130       , base2_t(open_only, key, read_only, addr,
131                 create_open_func_t(get_this_pointer(),
132                 ipcdetail::DoOpen))
133    {}
134
135    //!Connects to a created shared memory and its segment manager.
136    //!This can throw.
137    basic_managed_xsi_shared_memory (open_only_t, const xsi_key &key,
138                                 const void *addr = 0)
139       : base_t()
140       , base2_t(open_only, key, read_write, addr,
141                 create_open_func_t(get_this_pointer(),
142                 ipcdetail::DoOpen))
143    {}
144
145    //!Moves the ownership of "moved"'s managed memory to *this.
146    //!Does not throw
147    basic_managed_xsi_shared_memory(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved)
148    {
149       basic_managed_xsi_shared_memory tmp;
150       this->swap(moved);
151       tmp.swap(moved);
152    }
153
154    //!Moves the ownership of "moved"'s managed memory to *this.
155    //!Does not throw
156    basic_managed_xsi_shared_memory &operator=(BOOST_RV_REF(basic_managed_xsi_shared_memory) moved)
157    {
158       basic_managed_xsi_shared_memory tmp(boost::move(moved));
159       this->swap(tmp);
160       return *this;
161    }
162
163    //!Swaps the ownership of the managed shared memories managed by *this and other.
164    //!Never throws.
165    void swap(basic_managed_xsi_shared_memory &other)
166    {
167       base_t::swap(other);
168       base2_t::swap(other);
169    }
170
171    //!Erases a XSI shared memory object identified by shmid
172    //!from the system.
173    //!Returns false on error. Never throws
174    static bool remove(int shmid)
175    {  return device_type::remove(shmid); }
176
177    int get_shmid() const
178    {  return base2_t::get_device().get_shmid(); }
179
180    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
181
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.
184    //!Never throws.
185    template <class T>
186    std::pair<T*, std::size_t> find  (char_ptr_holder_t name)
187    {
188       if(base2_t::get_mapped_region().get_mode() == read_only){
189          return base_t::template find_no_lock<T>(name);
190       }
191       else{
192          return base_t::template find<T>(name);
193       }
194    }
195
196    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
197 };
198
199 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
200
201 //!Typedef for a default basic_managed_xsi_shared_memory
202 //!of narrow characters
203 typedef basic_managed_xsi_shared_memory
204    <char
205    ,rbtree_best_fit<mutex_family>
206    ,iset_index>
207 managed_xsi_shared_memory;
208
209 //!Typedef for a default basic_managed_xsi_shared_memory
210 //!of wide characters
211 typedef basic_managed_xsi_shared_memory
212    <wchar_t
213    ,rbtree_best_fit<mutex_family>
214    ,iset_index>
215 wmanaged_xsi_shared_memory;
216
217 #endif   //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
218
219 }  //namespace interprocess {
220 }  //namespace boost {
221
222 #include <boost/interprocess/detail/config_end.hpp>
223
224 #endif   //BOOST_INTERPROCESS_MANAGED_XSI_SHARED_MEMORY_HPP
225