• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *              Copyright Andrey Semashev 2016.
3  * Distributed under the Boost Software License, Version 1.0.
4  *    (See accompanying file LICENSE_1_0.txt or copy at
5  *          http://www.boost.org/LICENSE_1_0.txt)
6  */
7 /*!
8  * \file   windows/mapped_shared_memory.hpp
9  * \author Andrey Semashev
10  * \date   13.02.2016
11  *
12  * \brief  This header is the Boost.Log library implementation, see the library documentation
13  *         at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
14  */
15 
16 #ifndef BOOST_LOG_WINDOWS_MAPPED_SHARED_MEMORY_HPP_INCLUDED_
17 #define BOOST_LOG_WINDOWS_MAPPED_SHARED_MEMORY_HPP_INCLUDED_
18 
19 #include <boost/log/detail/config.hpp>
20 #include <boost/winapi/basic_types.hpp>
21 #include <cstddef>
22 #include <boost/assert.hpp>
23 #include <boost/atomic/atomic.hpp>
24 #include <boost/log/utility/permissions.hpp>
25 #include <boost/log/detail/header.hpp>
26 
27 namespace boost {
28 
29 BOOST_LOG_OPEN_NAMESPACE
30 
31 namespace ipc {
32 
33 namespace aux {
34 
35 /*!
36  * A replacement for to \c mapped_shared_memory and \c mapped_region from Boost.Interprocess.
37  * The significant difference is that the shared memory name is passed as a UTF-16 string and
38  * errors are reported as Boost.Log exceptions.
39  */
40 class mapped_shared_memory
41 {
42 private:
43     struct section_basic_information
44     {
45         void* base_address;
46         boost::winapi::ULONG_ section_attributes;
47         boost::winapi::LARGE_INTEGER_ section_size;
48     };
49     typedef boost::winapi::NTSTATUS_ (__stdcall *nt_query_section_t)(boost::winapi::HANDLE_ h, unsigned int info_class, section_basic_information* pinfo, boost::winapi::ULONG_ info_size, boost::winapi::ULONG_* ret_len);
50 
51 private:
52     boost::winapi::HANDLE_ m_handle;
53     void* m_mapped_address;
54     std::size_t m_size;
55     static boost::atomic< nt_query_section_t > nt_query_section;
56 
57 public:
mapped_shared_memory()58     BOOST_CONSTEXPR mapped_shared_memory() BOOST_NOEXCEPT :
59         m_handle(NULL),
60         m_mapped_address(NULL),
61         m_size(0u)
62     {
63     }
64 
65     ~mapped_shared_memory();
66 
67     //! Creates a new file mapping for the shared memory segment
68     void create(const wchar_t* name, std::size_t size, permissions const& perms = permissions());
69     //! Creates a new file mapping for the shared memory segment or opens the existing one. Returns \c true if the region was created and \c false if an existing one was opened.
70     bool create_or_open(const wchar_t* name, std::size_t size, permissions const& perms = permissions());
71     //! Opens the existing file mapping for the shared memory segment
72     void open(const wchar_t* name);
73 
74     //! Maps the file mapping into the current process memory
75     void map();
76     //! Unmaps the file mapping
77     void unmap();
78 
79     //! Returns the size of the opened shared memory segment
size() const80     std::size_t size() const BOOST_NOEXCEPT { return m_size; }
81     //! Returns the address of the mapped shared memory
address() const82     void* address() const BOOST_NOEXCEPT { return m_mapped_address; }
83 
84     BOOST_DELETED_FUNCTION(mapped_shared_memory(mapped_shared_memory const&))
85     BOOST_DELETED_FUNCTION(mapped_shared_memory& operator=(mapped_shared_memory const&))
86 
87 private:
88     //! Returns the size of the file mapping identified by the handle
89     static std::size_t obtain_size(boost::winapi::HANDLE_ h);
90 };
91 
92 } // namespace aux
93 
94 } // namespace ipc
95 
96 BOOST_LOG_CLOSE_NAMESPACE // namespace log
97 
98 } // namespace boost
99 
100 #include <boost/log/detail/footer.hpp>
101 
102 #endif // BOOST_LOG_WINDOWS_MAPPED_SHARED_MEMORY_HPP_INCLUDED_
103