]> git.ipfire.org Git - thirdparty/ccache.git/blob - src/util/MemoryMap.hpp
chore: Clean up usage of #include in headers
[thirdparty/ccache.git] / src / util / MemoryMap.hpp
1 // Copyright (C) 2024 ccache contributors
2 //
3 // See doc/AUTHORS.adoc for a complete list of contributors.
4 //
5 // This program is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3 of the License, or (at your option)
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 // more details.
14 //
15 // You should have received a copy of the GNU General Public License along with
16 // this program; if not, write to the Free Software Foundation, Inc., 51
17 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 #pragma once
20
21 #include <util/NonCopyable.hpp>
22
23 #include <third_party/tl/expected.hpp>
24
25 #include <cstddef>
26 #include <string>
27
28 namespace util {
29
30 class MemoryMap : util::NonCopyable
31 {
32 public:
33 MemoryMap() = default;
34 ~MemoryMap();
35
36 MemoryMap(MemoryMap&& other) noexcept;
37 MemoryMap& operator=(MemoryMap&& other) noexcept;
38
39 void unmap();
40
41 void* ptr();
42
43 static tl::expected<MemoryMap, std::string> map(int fd, size_t size);
44
45 private:
46 void* m_ptr = nullptr;
47 #ifndef _WIN32
48 size_t m_size = 0; // munmap needs the size, not needed on Windows
49 #else
50 void* m_file_mapping_handle =
51 nullptr; // On Windows a handle on a file mapping is needed
52 #endif
53 };
54
55 } // namespace util