Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
memory_mapped_file.hpp
Go to the documentation of this file.
1#pragma once
2
3// Memory-mapped file — RAII wrapper for mmap/MapViewOfFile
4// Provides read-only or read-write memory-mapped access to files.
5
6#include <cstddef>
7#include <cstdint>
8#include <limits>
9#include <string>
10#include <string_view>
11
12namespace pulp::runtime {
13
14enum class MapMode { ReadOnly, ReadWrite };
15
17 std::uint64_t volume = 0;
18 std::uint64_t file = 0;
19 std::uint64_t generation = 0;
20 bool valid = false;
21 friend bool operator==(const FileIdentity&, const FileIdentity&) = default;
22};
23
24FileIdentity file_identity(std::string_view path) noexcept;
25
27 public:
28 AccessPolicyTarget() = default;
31 AccessPolicyTarget& operator=(AccessPolicyTarget&&) noexcept;
33 AccessPolicyTarget& operator=(const AccessPolicyTarget&) = delete;
34
35 explicit operator bool() const noexcept;
36 // Call after atomically moving the target to its final parent. On Windows
37 // this restores the source DACL's inheritance state through the retained
38 // handle; on POSIX the copied policy is already final.
39 bool finalize_after_move() noexcept;
40 bool sync() noexcept;
41
42 private:
43 friend class MemoryMappedFile;
44#ifdef _WIN32
45 void* handle_ = nullptr;
46 void* descriptor_ = nullptr;
47 void* dacl_ = nullptr;
48 bool source_dacl_protected_ = true;
49#else
50 int fd_ = -1;
51#endif
52};
53
55
56 public:
57 MemoryMappedFile() = default;
59
60 // Open and map a file. Returns false on failure.
61 bool open(std::string_view path, MapMode mode = MapMode::ReadOnly);
62
63 bool open(std::string_view path, MapMode mode, std::size_t maximum_bytes);
64
65 // Open only when the final path component is a regular file rather than a
66 // symbolic link or Windows reparse point.
67 bool open_no_follow(std::string_view path, MapMode mode, std::size_t maximum_bytes);
68
69 // Unmap and close.
70 void close();
71
72 // Mapped data pointer (nullptr if not open)
73 const uint8_t* data() const {
74 return data_;
75 }
76 uint8_t* mutable_data() {
77 return data_;
78 }
79
80 // File size in bytes
81 size_t size() const {
82 return size_;
83 }
84
85 // Whether a file is currently mapped
86 bool is_open() const {
87 return data_ != nullptr;
88 }
89
90 // Copy the access policy of the opened file identity to an existing
91 // regular file. The source policy is read from this object's retained
92 // handle, not by reopening its pathname.
93 bool copy_access_policy_to(std::string_view destination) const noexcept;
94 AccessPolicyTarget prepare_access_policy_target(std::string_view destination) const noexcept;
95
96 // True only when path currently resolves to the same file generation as
97 // the retained open handle.
98 bool path_refers_to_open_file(std::string_view path) const noexcept;
100
101 // Copy exactly the bytes captured by the retained source handle into a new
102 // regular file. The destination must not already exist. This avoids
103 // reopening a mutable source pathname while establishing a stable snapshot.
104 bool copy_contents_to_new_file(std::string_view destination) const noexcept;
105
106 // No copy
108 MemoryMappedFile& operator=(const MemoryMappedFile&) = delete;
109
110 // Move
113
114 private:
115 bool open_impl(std::string_view path, MapMode mode, std::size_t maximum_bytes,
116 bool no_follow);
117 uint8_t* data_ = nullptr;
118 size_t size_ = 0;
119#ifdef _WIN32
120 std::string path_;
121 void* file_handle_ = nullptr;
122 void* mapping_handle_ = nullptr;
123#else
124 int fd_ = -1;
125#endif
126};
127
128} // namespace pulp::runtime
Definition memory_mapped_file.hpp:26
AccessPolicyTarget(AccessPolicyTarget &&) noexcept
friend class MemoryMappedFile
Definition memory_mapped_file.hpp:43
const uint8_t * data() const
Definition memory_mapped_file.hpp:73
FileIdentity opened_file_identity() const noexcept
bool open(std::string_view path, MapMode mode, std::size_t maximum_bytes)
uint8_t * mutable_data()
Definition memory_mapped_file.hpp:76
bool open(std::string_view path, MapMode mode=MapMode::ReadOnly)
AccessPolicyTarget prepare_access_policy_target(std::string_view destination) const noexcept
bool is_open() const
Definition memory_mapped_file.hpp:86
bool copy_access_policy_to(std::string_view destination) const noexcept
bool copy_contents_to_new_file(std::string_view destination) const noexcept
bool open_no_follow(std::string_view path, MapMode mode, std::size_t maximum_bytes)
bool path_refers_to_open_file(std::string_view path) const noexcept
size_t size() const
Definition memory_mapped_file.hpp:81
Definition generated_event_source.hpp:33
@ other
Definition udev_monitor.hpp:26
FileIdentity file_identity(std::string_view path) noexcept
MapMode
Definition memory_mapped_file.hpp:14
@ ReadOnly
Definition memory_mapped_file.hpp:14
@ ReadWrite
Definition memory_mapped_file.hpp:14
STL namespace.
Definition memory_mapped_file.hpp:16
friend bool operator==(const FileIdentity &, const FileIdentity &)=default
bool valid
Definition memory_mapped_file.hpp:20
std::uint64_t generation
Definition memory_mapped_file.hpp:19
std::uint64_t file
Definition memory_mapped_file.hpp:18
std::uint64_t volume
Definition memory_mapped_file.hpp:17