mmap.hpp
1 /*
2  * Copyright (c) 2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <cstddef>
19 #include <future>
20 
21 #include <kvikio/defaults.hpp>
22 #include <kvikio/file_handle.hpp>
23 #include <optional>
24 
25 namespace kvikio {
26 
38 class MmapHandle {
39  private:
40  void* _buf{};
41  std::size_t _initial_map_size{};
42  std::size_t _initial_map_offset{};
43  std::size_t _file_size{};
44  std::size_t _map_offset{};
45  std::size_t _map_size{};
46  void* _map_addr{};
47  bool _initialized{};
48  int _map_protection{};
49  int _map_flags{};
50  FileWrapper _file_wrapper{};
51 
65  std::size_t validate_and_adjust_read_args(std::optional<std::size_t> const& size,
66  std::size_t offset);
67 
68  public:
73  MmapHandle() noexcept = default;
74 
90  MmapHandle(std::string const& file_path,
91  std::string const& flags = "r",
92  std::optional<std::size_t> initial_map_size = std::nullopt,
93  std::size_t initial_map_offset = 0,
94  mode_t mode = FileHandle::m644,
95  std::optional<int> map_flags = std::nullopt);
96 
97  MmapHandle(MmapHandle const&) = delete;
98  MmapHandle& operator=(MmapHandle const&) = delete;
99  MmapHandle(MmapHandle&& o) noexcept;
100  MmapHandle& operator=(MmapHandle&& o) noexcept;
101  ~MmapHandle() noexcept;
102 
108  [[nodiscard]] std::size_t initial_map_size() const noexcept;
109 
115  [[nodiscard]] std::size_t initial_map_offset() const noexcept;
116 
124  [[nodiscard]] std::size_t file_size() const;
125 
131  [[nodiscard]] std::size_t nbytes() const;
132 
138  [[nodiscard]] bool closed() const noexcept;
139 
143  void close() noexcept;
144 
160  std::size_t read(void* buf,
161  std::optional<std::size_t> size = std::nullopt,
162  std::size_t offset = 0);
163 
183  std::future<std::size_t> pread(void* buf,
184  std::optional<std::size_t> size = std::nullopt,
185  std::size_t offset = 0,
186  std::size_t task_size = defaults::task_size());
187 };
188 
189 } // namespace kvikio
Handle of an open file registered with cufile.
Definition: file_handle.hpp:47
Class that provides RAII for file handling.
Definition: file_utils.hpp:27
Handle of a memory-mapped file.
Definition: mmap.hpp:38
std::size_t read(void *buf, std::optional< std::size_t > size=std::nullopt, std::size_t offset=0)
Sequential read size bytes from the file (with the offset offset) to the destination buffer buf
std::future< std::size_t > pread(void *buf, std::optional< std::size_t > size=std::nullopt, std::size_t offset=0, std::size_t task_size=defaults::task_size())
Parallel read size bytes from the file (with the offset offset) to the destination buffer buf
bool closed() const noexcept
Whether the mapping handle is closed.
std::size_t file_size() const
Get the file size if the file is open. Returns 0 if the file is closed.
void close() noexcept
Close the mapping handle if it is open; do nothing otherwise.
std::size_t nbytes() const
Alias of file_size
std::size_t initial_map_offset() const noexcept
File offset of the mapped region when the mapping handle was constructed.
MmapHandle() noexcept=default
Construct an empty memory-mapped file.
std::size_t initial_map_size() const noexcept
Size in bytes of the mapped region when the mapping handle was constructed.
Singleton class of default values used throughout KvikIO.
Definition: defaults.hpp:123
KvikIO namespace.
Definition: batch.hpp:27