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_FILE_MAPPING_HPP
12 #define BOOST_INTERPROCESS_FILE_MAPPING_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 #if !defined(BOOST_INTERPROCESS_MAPPED_FILES)
26 #error "Boost.Interprocess: This platform does not support memory mapped files!"
27 #endif
28 
29 #include <boost/interprocess/interprocess_fwd.hpp>
30 #include <boost/interprocess/exceptions.hpp>
31 #include <boost/interprocess/detail/utilities.hpp>
32 #include <boost/interprocess/creation_tags.hpp>
33 #include <boost/interprocess/detail/os_file_functions.hpp>
34 #include <boost/interprocess/detail/simple_swap.hpp>
35 #include <boost/move/utility_core.hpp>
36 #include <string>    //std::string
37 
38 //!\file
39 //!Describes file_mapping and mapped region classes
40 
41 namespace boost {
42 namespace interprocess {
43 
44 //!A class that wraps a file-mapping that can be used to
45 //!create mapped regions from the mapped files
46 class file_mapping
47 {
48    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
49    BOOST_MOVABLE_BUT_NOT_COPYABLE(file_mapping)
50    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
51 
52    public:
53    //!Constructs an empty file mapping.
54    //!Does not throw
55    file_mapping();
56 
57    //!Opens a file mapping of file "filename", starting in offset
58    //!"file_offset", and the mapping's size will be "size". The mapping
59    //!can be opened for read-only "read_only" or read-write "read_write"
60    //!modes. Throws interprocess_exception on error.
61    file_mapping(const char *filename, mode_t mode);
62 
63    //!Moves the ownership of "moved"'s file mapping object to *this.
64    //!After the call, "moved" does not represent any file mapping object.
65    //!Does not throw
file_mapping(BOOST_RV_REF (file_mapping)moved)66    file_mapping(BOOST_RV_REF(file_mapping) moved)
67       :  m_handle(file_handle_t(ipcdetail::invalid_file()))
68       ,  m_mode(read_only)
69    {  this->swap(moved);   }
70 
71    //!Moves the ownership of "moved"'s file mapping to *this.
72    //!After the call, "moved" does not represent any file mapping.
73    //!Does not throw
operator =(BOOST_RV_REF (file_mapping)moved)74    file_mapping &operator=(BOOST_RV_REF(file_mapping) moved)
75    {
76       file_mapping tmp(boost::move(moved));
77       this->swap(tmp);
78       return *this;
79    }
80 
81    //!Swaps to file_mappings.
82    //!Does not throw.
83    void swap(file_mapping &other);
84 
85    //!Returns access mode
86    //!used in the constructor
87    mode_t get_mode() const;
88 
89    //!Obtains the mapping handle
90    //!to be used with mapped_region
91    mapping_handle_t get_mapping_handle() const;
92 
93    //!Destroys the file mapping. All mapped regions created from this are still
94    //!valid. Does not throw
95    ~file_mapping();
96 
97    //!Returns the name of the file
98    //!used in the constructor.
99    const char *get_name() const;
100 
101    //!Removes the file named "filename" even if it's been memory mapped.
102    //!Returns true on success.
103    //!The function might fail in some operating systems if the file is
104    //!being used other processes and no deletion permission was shared.
105    static bool remove(const char *filename);
106 
107    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
108    private:
109    //!Closes a previously opened file mapping. Never throws.
110    void priv_close();
111    file_handle_t  m_handle;
112    mode_t         m_mode;
113    std::string    m_filename;
114    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
115 };
116 
file_mapping()117 inline file_mapping::file_mapping()
118    :  m_handle(file_handle_t(ipcdetail::invalid_file()))
119    ,  m_mode(read_only)
120 {}
121 
~file_mapping()122 inline file_mapping::~file_mapping()
123 {  this->priv_close(); }
124 
get_name() const125 inline const char *file_mapping::get_name() const
126 {  return m_filename.c_str(); }
127 
swap(file_mapping & other)128 inline void file_mapping::swap(file_mapping &other)
129 {
130    (simple_swap)(m_handle, other.m_handle);
131    (simple_swap)(m_mode, other.m_mode);
132    m_filename.swap(other.m_filename);
133 }
134 
get_mapping_handle() const135 inline mapping_handle_t file_mapping::get_mapping_handle() const
136 {  return ipcdetail::mapping_handle_from_file_handle(m_handle);  }
137 
get_mode() const138 inline mode_t file_mapping::get_mode() const
139 {  return m_mode; }
140 
file_mapping(const char * filename,mode_t mode)141 inline file_mapping::file_mapping
142    (const char *filename, mode_t mode)
143    :  m_filename(filename)
144 {
145    //Check accesses
146    if (mode != read_write && mode != read_only){
147       error_info err = other_error;
148       throw interprocess_exception(err);
149    }
150 
151    //Open file
152    m_handle = ipcdetail::open_existing_file(filename, mode);
153 
154    //Check for error
155    if(m_handle == ipcdetail::invalid_file()){
156       error_info err = system_error_code();
157       this->priv_close();
158       throw interprocess_exception(err);
159    }
160    m_mode = mode;
161 }
162 
remove(const char * filename)163 inline bool file_mapping::remove(const char *filename)
164 {  return ipcdetail::delete_file(filename);  }
165 
166 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
167 
priv_close()168 inline void file_mapping::priv_close()
169 {
170    if(m_handle != ipcdetail::invalid_file()){
171       ipcdetail::close_file(m_handle);
172       m_handle = ipcdetail::invalid_file();
173    }
174 }
175 
176 //!A class that stores the name of a file
177 //!and tries to remove it in its destructor
178 //!Useful to remove temporary files in the presence
179 //!of exceptions
180 class remove_file_on_destroy
181 {
182    const char * m_name;
183    public:
remove_file_on_destroy(const char * name)184    remove_file_on_destroy(const char *name)
185       :  m_name(name)
186    {}
187 
~remove_file_on_destroy()188    ~remove_file_on_destroy()
189    {  ipcdetail::delete_file(m_name);  }
190 };
191 
192 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
193 
194 }  //namespace interprocess {
195 }  //namespace boost {
196 
197 #include <boost/interprocess/detail/config_end.hpp>
198 
199 #endif   //BOOST_INTERPROCESS_FILE_MAPPING_HPP
200