mmap.hpp
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <cstddef>
8 #include <future>
9 
10 #include <kvikio/defaults.hpp>
11 #include <kvikio/file_handle.hpp>
12 #include <kvikio/threadpool_wrapper.hpp>
13 #include <optional>
14 
15 namespace kvikio {
16 
28 class MmapHandle {
29  private:
30  void* _buf{};
31  std::size_t _initial_map_size{};
32  std::size_t _initial_map_offset{};
33  std::size_t _file_size{};
34  std::size_t _map_offset{};
35  std::size_t _map_size{};
36  void* _map_addr{};
37  bool _initialized{};
38  int _map_protection{};
39  int _map_flags{};
40  FileWrapper _file_wrapper{};
41  std::string _file_path; // Reported to the monitors, see `Observation::source`.
42 
55  std::size_t validate_and_adjust_read_args(std::optional<std::size_t> const& size,
56  std::size_t offset);
57 
58  public:
63  MmapHandle() noexcept = default;
64 
85  MmapHandle(std::string const& file_path,
86  std::string const& flags = "r",
87  std::optional<std::size_t> initial_map_size = std::nullopt,
88  std::size_t initial_map_offset = 0,
89  mode_t mode = FileHandle::m644,
90  std::optional<int> map_flags = std::nullopt);
91 
92  MmapHandle(MmapHandle const&) = delete;
93  MmapHandle& operator=(MmapHandle const&) = delete;
94  MmapHandle(MmapHandle&& o) noexcept;
95  MmapHandle& operator=(MmapHandle&& o) noexcept;
96  ~MmapHandle() noexcept;
97 
103  [[nodiscard]] std::size_t initial_map_size() const noexcept;
104 
110  [[nodiscard]] std::size_t initial_map_offset() const noexcept;
111 
119  [[nodiscard]] std::size_t file_size() const;
120 
126  [[nodiscard]] std::size_t nbytes() const;
127 
133  [[nodiscard]] bool closed() const noexcept;
134 
138  void close() noexcept;
139 
154  std::size_t read(void* buf,
155  std::optional<std::size_t> size = std::nullopt,
156  std::size_t offset = 0);
157 
180  std::future<std::size_t> pread(void* buf,
181  std::optional<std::size_t> size = std::nullopt,
182  std::size_t offset = 0,
183  std::size_t task_size = defaults::task_size(),
184  ThreadPool* thread_pool = &defaults::thread_pool());
185 };
186 
187 } // namespace kvikio
Handle of an open file registered with cufile.
Definition: file_handle.hpp:34
Class that provides RAII for file handling.
Definition: file_utils.hpp:16
Handle of a memory-mapped file.
Definition: mmap.hpp:28
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(), ThreadPool *thread_pool=&defaults::thread_pool())
Parallel read size bytes from the file (with the offset offset) to the destination buffer buf
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
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:122
KvikIO namespace.
Definition: batch.hpp:16
BS::thread_pool ThreadPool
Thread pool type used for parallel I/O operations.