mmap.hpp
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION.
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 <optional>
13 
14 namespace kvikio {
15 
27 class MmapHandle {
28  private:
29  void* _buf{};
30  std::size_t _initial_map_size{};
31  std::size_t _initial_map_offset{};
32  std::size_t _file_size{};
33  std::size_t _map_offset{};
34  std::size_t _map_size{};
35  void* _map_addr{};
36  bool _initialized{};
37  int _map_protection{};
38  int _map_flags{};
39  FileWrapper _file_wrapper{};
40 
53  std::size_t validate_and_adjust_read_args(std::optional<std::size_t> const& size,
54  std::size_t offset);
55 
56  public:
61  MmapHandle() noexcept = default;
62 
83  MmapHandle(std::string const& file_path,
84  std::string const& flags = "r",
85  std::optional<std::size_t> initial_map_size = std::nullopt,
86  std::size_t initial_map_offset = 0,
87  mode_t mode = FileHandle::m644,
88  std::optional<int> map_flags = std::nullopt);
89 
90  MmapHandle(MmapHandle const&) = delete;
91  MmapHandle& operator=(MmapHandle const&) = delete;
92  MmapHandle(MmapHandle&& o) noexcept;
93  MmapHandle& operator=(MmapHandle&& o) noexcept;
94  ~MmapHandle() noexcept;
95 
101  [[nodiscard]] std::size_t initial_map_size() const noexcept;
102 
108  [[nodiscard]] std::size_t initial_map_offset() const noexcept;
109 
117  [[nodiscard]] std::size_t file_size() const;
118 
124  [[nodiscard]] std::size_t nbytes() const;
125 
131  [[nodiscard]] bool closed() const noexcept;
132 
136  void close() noexcept;
137 
152  std::size_t read(void* buf,
153  std::optional<std::size_t> size = std::nullopt,
154  std::size_t offset = 0);
155 
174  std::future<std::size_t> pread(void* buf,
175  std::optional<std::size_t> size = std::nullopt,
176  std::size_t offset = 0,
177  std::size_t task_size = defaults::task_size());
178 };
179 
180 } // namespace kvikio
Handle of an open file registered with cufile.
Definition: file_handle.hpp:32
Class that provides RAII for file handling.
Definition: file_utils.hpp:16
Handle of a memory-mapped file.
Definition: mmap.hpp:27
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:112
KvikIO namespace.
Definition: batch.hpp:16