1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-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_SHARED_MEMORY_HPP
12 #define BOOST_INTERPROCESS_MANAGED_SHARED_MEMORY_HPP
13 
14 #ifndef BOOST_CONFIG_HPP
15 #  include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 #  pragma once
20 #endif
21 
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
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/shared_memory_object.hpp>
28 #include <boost/interprocess/creation_tags.hpp>
29 #include <boost/interprocess/permissions.hpp>
30 //These includes needed to fulfill default template parameters of
31 //predeclarations in interprocess_fwd.hpp
32 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
33 #include <boost/interprocess/sync/mutex_family.hpp>
34 
35 namespace boost {
36 namespace interprocess {
37 
38 namespace ipcdetail {
39 
40 template<class AllocationAlgorithm>
41 struct shmem_open_or_create
42 {
43    typedef  ipcdetail::managed_open_or_create_impl
44       < shared_memory_object, AllocationAlgorithm::Alignment, true, false> type;
45 };
46 
47 }  //namespace ipcdetail {
48 
49 //!A basic shared memory named object creation class. Initializes the
50 //!shared memory segment. Inherits all basic functionality from
51 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
52 template
53       <
54          class CharType,
55          class AllocationAlgorithm,
56          template<class IndexConfig> class IndexType
57       >
58 class basic_managed_shared_memory
59    : public ipcdetail::basic_managed_memory_impl
60       <CharType, AllocationAlgorithm, IndexType
61       ,ipcdetail::shmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset>
62    , private ipcdetail::shmem_open_or_create<AllocationAlgorithm>::type
63 {
64    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
65    typedef ipcdetail::basic_managed_memory_impl
66       <CharType, AllocationAlgorithm, IndexType,
67       ipcdetail::shmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset>   base_t;
68    typedef typename ipcdetail::shmem_open_or_create<AllocationAlgorithm>::type                     base2_t;
69 
70    typedef ipcdetail::create_open_func<base_t>        create_open_func_t;
71 
get_this_pointer()72    basic_managed_shared_memory *get_this_pointer()
73    {  return this;   }
74 
75    public:
76    typedef shared_memory_object                    device_type;
77    typedef typename base_t::size_type              size_type;
78 
79    private:
80    typedef typename base_t::char_ptr_holder_t   char_ptr_holder_t;
81    BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_shared_memory)
82    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
83 
84    public: //functions
85 
86    //!Destroys *this and indicates that the calling process is finished using
87    //!the resource. The destructor function will deallocate
88    //!any system resources allocated by the system for use by this process for
89    //!this resource. The resource can still be opened again calling
90    //!the open constructor overload. To erase the resource from the system
91    //!use remove().
~basic_managed_shared_memory()92    ~basic_managed_shared_memory()
93    {}
94 
95    //!Default constructor. Does nothing.
96    //!Useful in combination with move semantics
basic_managed_shared_memory()97    basic_managed_shared_memory()
98    {}
99 
100    //!Creates shared memory and creates and places the segment manager.
101    //!This can throw.
basic_managed_shared_memory(create_only_t,const char * name,size_type size,const void * addr=0,const permissions & perm=permissions ())102    basic_managed_shared_memory(create_only_t, const char *name,
103                              size_type size, const void *addr = 0, const permissions& perm = permissions())
104       : base_t()
105       , base2_t(create_only, name, size, read_write, addr,
106                 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
107    {}
108 
109    //!Creates shared memory and creates and places the segment manager if
110    //!segment was not created. If segment was created it connects to the
111    //!segment.
112    //!This can throw.
basic_managed_shared_memory(open_or_create_t,const char * name,size_type size,const void * addr=0,const permissions & perm=permissions ())113    basic_managed_shared_memory (open_or_create_t,
114                               const char *name, size_type size,
115                               const void *addr = 0, const permissions& perm = permissions())
116       : base_t()
117       , base2_t(open_or_create, name, size, read_write, addr,
118                 create_open_func_t(get_this_pointer(),
119                 ipcdetail::DoOpenOrCreate), perm)
120    {}
121 
122    //!Connects to a created shared memory and its segment manager.
123    //!in copy_on_write mode.
124    //!This can throw.
basic_managed_shared_memory(open_copy_on_write_t,const char * name,const void * addr=0)125    basic_managed_shared_memory (open_copy_on_write_t, const char* name,
126                                 const void *addr = 0)
127       : base_t()
128       , base2_t(open_only, name, copy_on_write, addr,
129                 create_open_func_t(get_this_pointer(),
130                 ipcdetail::DoOpen))
131    {}
132 
133    //!Connects to a created shared memory and its segment manager.
134    //!in read-only mode.
135    //!This can throw.
basic_managed_shared_memory(open_read_only_t,const char * name,const void * addr=0)136    basic_managed_shared_memory (open_read_only_t, const char* name,
137                                 const void *addr = 0)
138       : base_t()
139       , base2_t(open_only, name, read_only, addr,
140                 create_open_func_t(get_this_pointer(),
141                 ipcdetail::DoOpen))
142    {}
143 
144    //!Connects to a created shared memory and its segment manager.
145    //!This can throw.
basic_managed_shared_memory(open_only_t,const char * name,const void * addr=0)146    basic_managed_shared_memory (open_only_t, const char* name,
147                                 const void *addr = 0)
148       : base_t()
149       , base2_t(open_only, name, read_write, addr,
150                 create_open_func_t(get_this_pointer(),
151                 ipcdetail::DoOpen))
152    {}
153 
154    //!Moves the ownership of "moved"'s managed memory to *this.
155    //!Does not throw
basic_managed_shared_memory(BOOST_RV_REF (basic_managed_shared_memory)moved)156    basic_managed_shared_memory(BOOST_RV_REF(basic_managed_shared_memory) moved)
157    {
158       basic_managed_shared_memory tmp;
159       this->swap(moved);
160       tmp.swap(moved);
161    }
162 
163    //!Moves the ownership of "moved"'s managed memory to *this.
164    //!Does not throw
operator =(BOOST_RV_REF (basic_managed_shared_memory)moved)165    basic_managed_shared_memory &operator=(BOOST_RV_REF(basic_managed_shared_memory) moved)
166    {
167       basic_managed_shared_memory tmp(boost::move(moved));
168       this->swap(tmp);
169       return *this;
170    }
171 
172    //!Swaps the ownership of the managed shared memories managed by *this and other.
173    //!Never throws.
swap(basic_managed_shared_memory & other)174    void swap(basic_managed_shared_memory &other)
175    {
176       base_t::swap(other);
177       base2_t::swap(other);
178    }
179 
180    //!Tries to resize the managed shared memory object so that we have
181    //!room for more objects.
182    //!
183    //!This function is not synchronized so no other thread or process should
184    //!be reading or writing the file
grow(const char * shmname,size_type extra_bytes)185    static bool grow(const char *shmname, size_type extra_bytes)
186    {
187       return base_t::template grow
188          <basic_managed_shared_memory>(shmname, extra_bytes);
189    }
190 
191    //!Tries to resize the managed shared memory to minimized the size of the file.
192    //!
193    //!This function is not synchronized so no other thread or process should
194    //!be reading or writing the file
shrink_to_fit(const char * shmname)195    static bool shrink_to_fit(const char *shmname)
196    {
197       return base_t::template shrink_to_fit
198          <basic_managed_shared_memory>(shmname);
199    }
200    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
201 
202    //!Tries to find a previous named allocation address. Returns a memory
203    //!buffer and the object count. If not found returned pointer is 0.
204    //!Never throws.
205    template <class T>
find(char_ptr_holder_t name)206    std::pair<T*, size_type> find  (char_ptr_holder_t name)
207    {
208       if(base2_t::get_mapped_region().get_mode() == read_only){
209          return base_t::template find_no_lock<T>(name);
210       }
211       else{
212          return base_t::template find<T>(name);
213       }
214    }
215 
216    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
217 };
218 
219 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
220 
221 //!Typedef for a default basic_managed_shared_memory
222 //!of narrow characters
223 typedef basic_managed_shared_memory
224    <char
225    ,rbtree_best_fit<mutex_family>
226    ,iset_index>
227 managed_shared_memory;
228 
229 //!Typedef for a default basic_managed_shared_memory
230 //!of wide characters
231 typedef basic_managed_shared_memory
232    <wchar_t
233    ,rbtree_best_fit<mutex_family>
234    ,iset_index>
235 wmanaged_shared_memory;
236 
237 //!Typedef for a default basic_managed_shared_memory
238 //!of narrow characters to be placed in a fixed address
239 typedef basic_managed_shared_memory
240    <char
241    ,rbtree_best_fit<mutex_family, void*>
242    ,iset_index>
243 fixed_managed_shared_memory;
244 
245 //!Typedef for a default basic_managed_shared_memory
246 //!of narrow characters to be placed in a fixed address
247 typedef basic_managed_shared_memory
248    <wchar_t
249    ,rbtree_best_fit<mutex_family, void*>
250    ,iset_index>
251 wfixed_managed_shared_memory;
252 
253 
254 #endif   //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
255 
256 }  //namespace interprocess {
257 }  //namespace boost {
258 
259 #include <boost/interprocess/detail/config_end.hpp>
260 
261 #endif   //BOOST_INTERPROCESS_MANAGED_SHARED_MEMORY_HPP
262 
263