1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2009. 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_SHARED_MEMORY_HPP
12 #define BOOST_INTERPROCESS_MANAGED_SHARED_MEMORY_HPP
14 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
21 #include <boost/interprocess/detail/managed_memory_impl.hpp>
22 #include <boost/interprocess/exceptions.hpp>
23 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
24 #include <boost/interprocess/shared_memory_object.hpp>
25 #include <boost/interprocess/creation_tags.hpp>
26 #include <boost/interprocess/permissions.hpp>
30 namespace interprocess {
32 //!A basic shared memory named object creation class. Initializes the
33 //!shared memory segment. Inherits all basic functionality from
34 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
38 class AllocationAlgorithm,
39 template<class IndexConfig> class IndexType
41 class basic_managed_shared_memory
42 : public detail::basic_managed_memory_impl
43 <CharType, AllocationAlgorithm, IndexType
44 ,detail::managed_open_or_create_impl<shared_memory_object>::ManagedOpenOrCreateUserOffset>
45 , private detail::managed_open_or_create_impl<shared_memory_object>
49 typedef shared_memory_object device_type;
52 typedef detail::basic_managed_memory_impl
53 <CharType, AllocationAlgorithm, IndexType,
54 detail::managed_open_or_create_impl<shared_memory_object>::ManagedOpenOrCreateUserOffset> base_t;
55 typedef detail::managed_open_or_create_impl
56 <shared_memory_object> base2_t;
58 typedef detail::create_open_func<base_t> create_open_func_t;
60 basic_managed_shared_memory *get_this_pointer()
64 typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
65 BOOST_INTERPROCESS_MOVABLE_BUT_NOT_COPYABLE(basic_managed_shared_memory)
70 //!Destroys *this and indicates that the calling process is finished using
71 //!the resource. The destructor function will deallocate
72 //!any system resources allocated by the system for use by this process for
73 //!this resource. The resource can still be opened again calling
74 //!the open constructor overload. To erase the resource from the system
76 ~basic_managed_shared_memory()
79 //!Default constructor. Does nothing.
80 //!Useful in combination with move semantics
81 basic_managed_shared_memory()
84 //!Creates shared memory and creates and places the segment manager.
86 basic_managed_shared_memory(create_only_t create_only, const char *name,
87 std::size_t size, const void *addr = 0, const permissions& perm = permissions())
89 , base2_t(create_only, name, size, read_write, addr,
90 create_open_func_t(get_this_pointer(), detail::DoCreate), perm)
93 //!Creates shared memory and creates and places the segment manager if
94 //!segment was not created. If segment was created it connects to the
97 basic_managed_shared_memory (open_or_create_t open_or_create,
98 const char *name, std::size_t size,
99 const void *addr = 0, const permissions& perm = permissions())
101 , base2_t(open_or_create, name, size, read_write, addr,
102 create_open_func_t(get_this_pointer(),
103 detail::DoOpenOrCreate), perm)
106 //!Connects to a created shared memory and its segment manager.
107 //!in copy_on_write mode.
109 basic_managed_shared_memory (open_copy_on_write_t, const char* name,
110 const void *addr = 0)
112 , base2_t(open_only, name, copy_on_write, addr,
113 create_open_func_t(get_this_pointer(),
117 //!Connects to a created shared memory and its segment manager.
118 //!in read-only mode.
120 basic_managed_shared_memory (open_read_only_t, const char* name,
121 const void *addr = 0)
123 , base2_t(open_only, name, read_only, addr,
124 create_open_func_t(get_this_pointer(),
128 //!Connects to a created shared memory and its segment manager.
130 basic_managed_shared_memory (open_only_t open_only, const char* name,
131 const void *addr = 0)
133 , base2_t(open_only, name, read_write, addr,
134 create_open_func_t(get_this_pointer(),
138 //!Moves the ownership of "moved"'s managed memory to *this.
140 basic_managed_shared_memory(BOOST_INTERPROCESS_RV_REF(basic_managed_shared_memory) moved)
142 basic_managed_shared_memory tmp;
147 //!Moves the ownership of "moved"'s managed memory to *this.
149 basic_managed_shared_memory &operator=(BOOST_INTERPROCESS_RV_REF(basic_managed_shared_memory) moved)
151 basic_managed_shared_memory tmp(boost::interprocess::move(moved));
156 //!Swaps the ownership of the managed shared memories managed by *this and other.
158 void swap(basic_managed_shared_memory &other)
161 base2_t::swap(other);
164 //!Tries to resize the managed shared memory object so that we have
165 //!room for more objects.
167 //!This function is not synchronized so no other thread or process should
168 //!be reading or writing the file
169 static bool grow(const char *shmname, std::size_t extra_bytes)
171 return base_t::template grow
172 <basic_managed_shared_memory>(shmname, extra_bytes);
175 //!Tries to resize the managed shared memory to minimized the size of the file.
177 //!This function is not synchronized so no other thread or process should
178 //!be reading or writing the file
179 static bool shrink_to_fit(const char *shmname)
181 return base_t::template shrink_to_fit
182 <basic_managed_shared_memory>(shmname);
187 return this->base2_t::flush();
192 //!Tries to find a previous named allocation address. Returns a memory
193 //!buffer and the object count. If not found returned pointer is 0.
196 std::pair<T*, std::size_t> find (char_ptr_holder_t name)
198 if(base2_t::get_mapped_region().get_mode() == read_only){
199 return base_t::template find_no_lock<T>(name);
202 return base_t::template find<T>(name);
209 } //namespace interprocess {
210 } //namespace boost {
212 #include <boost/interprocess/detail/config_end.hpp>
214 #endif //BOOST_INTERPROCESS_MANAGED_SHARED_MEMORY_HPP