pstore2
memory_mapper.hpp
Go to the documentation of this file.
1 //===- include/pstore/os/memory_mapper.hpp ----------------*- mode: C++ -*-===//
2 //* *
3 //* _ __ ___ ___ _ __ ___ ___ _ __ _ _ *
4 //* | '_ ` _ \ / _ \ '_ ` _ \ / _ \| '__| | | | *
5 //* | | | | | | __/ | | | | | (_) | | | |_| | *
6 //* |_| |_| |_|\___|_| |_| |_|\___/|_| \__, | *
7 //* |___/ *
8 //* *
9 //* _ __ ___ __ _ _ __ _ __ ___ _ __ *
10 //* | '_ ` _ \ / _` | '_ \| '_ \ / _ \ '__| *
11 //* | | | | | | (_| | |_) | |_) | __/ | *
12 //* |_| |_| |_|\__,_| .__/| .__/ \___|_| *
13 //* |_| |_| *
14 //===----------------------------------------------------------------------===//
15 //
16 // Part of the pstore project, under the Apache License v2.0 with LLVM Exceptions.
17 // See https://github.com/SNSystems/pstore/blob/master/LICENSE.txt for license
18 // information.
19 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
20 //
21 //===----------------------------------------------------------------------===//
26 
27 #ifndef PSTORE_OS_MEMORY_MAPPER_HPP
28 #define PSTORE_OS_MEMORY_MAPPER_HPP
29 
30 #include "pstore/os/file.hpp"
31 
32 namespace pstore {
33 
37  std::shared_ptr<std::uint8_t> aligned_valloc (std::size_t size, unsigned align);
38 
41  public:
42  system_page_size_interface () = default;
45 
46  virtual ~system_page_size_interface () noexcept;
47 
48  system_page_size_interface & operator= (system_page_size_interface &&) noexcept = default;
49  system_page_size_interface & operator= (system_page_size_interface const &) = default;
50 
51  virtual unsigned get () const = 0;
52  };
53 
54 
56  public:
57  system_page_size () = default;
58  ~system_page_size () noexcept override;
59 
60  unsigned get () const override { return size_; }
61 
62  private:
63  unsigned size_ = sysconf ();
64  static unsigned sysconf ();
65  };
66 
67 
69  public:
70  virtual ~memory_mapper_base () = 0;
71 
72  memory_mapper_base (memory_mapper_base &&) noexcept = default;
73  memory_mapper_base (memory_mapper_base const &) = default;
74  memory_mapper_base & operator= (memory_mapper_base &&) noexcept = default;
75  memory_mapper_base & operator= (memory_mapper_base const &) = default;
76 
78  std::shared_ptr<void> const & data () { return ptr_; }
81  std::shared_ptr<void const> data () const { return ptr_; }
83 
87  bool is_writable () const { return is_writable_; }
88 
90  std::uint64_t offset () const { return offset_; }
91 
93  std::uint64_t size () const { return size_; }
94 
97  std::uint64_t end () const { return offset () + size (); }
98 
99  static unsigned long page_size (system_page_size_interface const & intf);
100 
110  virtual void read_only (void * addr, std::size_t len);
111 
112  protected:
118  memory_mapper_base (std::shared_ptr<void> ptr, bool const is_writable,
119  std::uint64_t const offset, std::uint64_t const size)
120  : ptr_{std::move (ptr)}
121  , is_writable_{is_writable}
122  , offset_{offset}
123  , size_{size} {}
124 
125  private:
134  void read_only_impl (void * addr, std::size_t len);
135 
137  std::shared_ptr<void> ptr_;
139  bool is_writable_;
142  std::uint64_t offset_;
144  std::uint64_t size_;
145  };
146 
147  std::ostream & operator<< (std::ostream & os, memory_mapper_base const & mm);
148 
156 
157  class memory_mapper final : public memory_mapper_base {
158  public:
166 
167  memory_mapper (file::file_handle & file, bool write_enabled, std::uint64_t offset,
168  std::uint64_t length);
169  ~memory_mapper () noexcept override;
170 
171  private:
172  static std::shared_ptr<void> mmap (file::file_handle & file, bool write_enabled,
173  std::uint64_t offset, std::uint64_t length);
174  };
175 
176 
178  public:
179  in_memory_mapper (file::in_memory & file, bool const write_enabled,
180  std::uint64_t const offset, std::uint64_t const length)
181  : memory_mapper_base (pointer (file, offset), write_enabled, offset, length) {}
182  ~in_memory_mapper () noexcept override;
183 
184  static std::shared_ptr<std::uint8_t> pointer (pstore::file::in_memory & file,
185  std::uint64_t const offset) {
186  auto const p = std::static_pointer_cast<std::uint8_t> (file.data ());
187  return std::shared_ptr<std::uint8_t> (p, p.get () + offset);
188  }
189  };
190 
191 } // namespace pstore
192 #endif // PSTORE_OS_MEMORY_MAPPER_HPP
std::uint64_t end() const
A convenience method which returns the file offset of the end of the memory represented by this objec...
Definition: memory_mapper.hpp:97
std::uint64_t size() const
Returns the size of the memory region owned by this object.
Definition: memory_mapper.hpp:93
Cross platform file management functions and classes.
memory_mapper provides an operating system independent interface for memory mapping of files...
Definition: memory_mapper.hpp:157
Definition: memory_mapper.hpp:68
Implements a portable file access API.
Definition: file.hpp:566
std::uint64_t offset() const
Returns the file offset of the start of the memory represented by this object.
Definition: memory_mapper.hpp:90
An interface for accessing the fundamental virtual memory page size on the host.
Definition: memory_mapper.hpp:40
Definition: memory_mapper.hpp:55
Implements an in-memory file which provides a file-like API to a chunk of pre-allocated memory...
Definition: file.hpp:483
bool is_writable() const
Returns true if the memory is to be writable.
Definition: memory_mapper.hpp:87
Definition: nonpod2.cpp:40
std::shared_ptr< void > data()
Returns the underlying memory managed by the file object.
Definition: file.hpp:519
std::shared_ptr< std::uint8_t > aligned_valloc(std::size_t size, unsigned align)
Allocates memory whose start address is a multiple of &#39;align&#39;.
Definition: memory_mapper_posix.cpp:68
Definition: memory_mapper.hpp:177
memory_mapper_base(std::shared_ptr< void > ptr, bool const is_writable, std::uint64_t const offset, std::uint64_t const size)
Definition: memory_mapper.hpp:118