1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2008. 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_MAPPED_FILE_HPP
12 #define BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP
14 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
21 #include <boost/interprocess/detail/managed_memory_impl.hpp>
22 #include <boost/interprocess/creation_tags.hpp>
23 #include <boost/interprocess/detail/file_wrapper.hpp>
24 #include <boost/interprocess/detail/move.hpp>
25 #include <boost/interprocess/file_mapping.hpp>
28 namespace interprocess
{
30 //!A basic mapped file named object creation class. Initializes the
31 //!mapped file. Inherits all basic functionality from
32 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
36 class AllocationAlgorithm
,
37 template<class IndexConfig
> class IndexType
39 class basic_managed_mapped_file
40 : public detail::basic_managed_memory_impl
41 <CharType
, AllocationAlgorithm
, IndexType
42 ,detail::managed_open_or_create_impl
<detail::file_wrapper
>::ManagedOpenOrCreateUserOffset
>
46 typedef detail::basic_managed_memory_impl
47 <CharType
, AllocationAlgorithm
, IndexType
,
48 detail::managed_open_or_create_impl
<detail::file_wrapper
>::ManagedOpenOrCreateUserOffset
> base_t
;
49 typedef detail::file_wrapper device_type
;
50 basic_managed_mapped_file(basic_managed_mapped_file
&);
51 basic_managed_mapped_file
& operator=(basic_managed_mapped_file
&);
55 typedef detail::create_open_func
<base_t
> create_open_func_t
;
56 typedef detail::managed_open_or_create_impl
<detail::file_wrapper
> managed_open_or_create_type
;
58 basic_managed_mapped_file
*get_this_pointer()
62 typedef typename
base_t::char_ptr_holder_t char_ptr_holder_t
;
66 BOOST_INTERPROCESS_ENABLE_MOVE_EMULATION(basic_managed_mapped_file
)
68 //!Creates mapped file and creates and places the segment manager.
70 basic_managed_mapped_file()
73 //!Creates mapped file and creates and places the segment manager.
75 basic_managed_mapped_file(create_only_t create_only
, const char *name
,
76 std::size_t size
, const void *addr
= 0)
77 : m_mfile(create_only
, name
, size
, read_write
, addr
,
78 create_open_func_t(get_this_pointer(), detail::DoCreate
))
81 //!Creates mapped file and creates and places the segment manager if
82 //!segment was not created. If segment was created it connects to the
85 basic_managed_mapped_file (open_or_create_t open_or_create
,
86 const char *name
, std::size_t size
,
88 : m_mfile(open_or_create
, name
, size
, read_write
, addr
,
89 create_open_func_t(get_this_pointer(),
90 detail::DoOpenOrCreate
))
93 //!Connects to a created mapped file and its segment manager.
95 basic_managed_mapped_file (open_only_t open_only
, const char* name
,
97 : m_mfile(open_only
, name
, read_write
, addr
,
98 create_open_func_t(get_this_pointer(),
102 //!Connects to a created mapped file and its segment manager
103 //!in copy_on_write mode.
105 basic_managed_mapped_file (open_copy_on_write_t
, const char* name
,
106 const void *addr
= 0)
107 : m_mfile(open_only
, name
, copy_on_write
, addr
,
108 create_open_func_t(get_this_pointer(),
112 //!Connects to a created mapped file and its segment manager
113 //!in read-only mode.
115 basic_managed_mapped_file (open_read_only_t
, const char* name
,
116 const void *addr
= 0)
117 : m_mfile(open_only
, name
, read_only
, addr
,
118 create_open_func_t(get_this_pointer(),
122 //!Moves the ownership of "moved"'s managed memory to *this.
124 basic_managed_mapped_file(BOOST_INTERPROCESS_RV_REF(basic_managed_mapped_file
) moved
)
129 //!Moves the ownership of "moved"'s managed memory to *this.
131 basic_managed_mapped_file
&operator=(BOOST_INTERPROCESS_RV_REF(basic_managed_mapped_file
) moved
)
133 basic_managed_mapped_file
tmp(boost::interprocess::move(moved
));
138 //!Destroys *this and indicates that the calling process is finished using
139 //!the resource. The destructor function will deallocate
140 //!any system resources allocated by the system for use by this process for
141 //!this resource. The resource can still be opened again calling
142 //!the open constructor overload. To erase the resource from the system
144 ~basic_managed_mapped_file()
147 //!Swaps the ownership of the managed mapped memories managed by *this and other.
149 void swap(basic_managed_mapped_file
&other
)
152 m_mfile
.swap(other
.m_mfile
);
155 //!Flushes cached data to file.
158 { return m_mfile
.flush(); }
160 //!Tries to resize mapped file so that we have room for
163 //!This function is not synchronized so no other thread or process should
164 //!be reading or writing the file
165 static bool grow(const char *filename
, std::size_t extra_bytes
)
167 return base_t::template grow
168 <basic_managed_mapped_file
>(filename
, extra_bytes
);
171 //!Tries to resize mapped file to minimized the size of the file.
173 //!This function is not synchronized so no other thread or process should
174 //!be reading or writing the file
175 static bool shrink_to_fit(const char *filename
)
177 return base_t::template shrink_to_fit
178 <basic_managed_mapped_file
>(filename
);
183 //!Tries to find a previous named allocation address. Returns a memory
184 //!buffer and the object count. If not found returned pointer is 0.
187 std::pair
<T
*, std::size_t> find (char_ptr_holder_t name
)
189 if(m_mfile
.get_mapped_region().get_mode() == read_only
){
190 return base_t::template find_no_lock
<T
>(name
);
193 return base_t::template find
<T
>(name
);
198 managed_open_or_create_type m_mfile
;
202 } //namespace interprocess {
203 } //namespace boost {
205 #include <boost/interprocess/detail/config_end.hpp>
207 #endif //BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP