include/boost/corosio/native/native_random_access_file.hpp

95.1% Lines (39/41) 100.0% List of functions (22/22)
native_random_access_file.hpp
f(x) Functions (22)
Function Calls Lines Blocks
boost::corosio::native_random_access_file<boost::corosio::epoll_t{}>::get_impl() :83 2x 100.0% 100.0% boost::corosio::native_random_access_file<boost::corosio::select_t{}>::get_impl() :83 2x 100.0% 100.0% boost::corosio::native_random_access_file<boost::corosio::epoll_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::native_read_at_awaitable(boost::corosio::native_random_access_file<boost::corosio::epoll_t{}>&, unsigned long, boost::capy::mutable_buffer) :98 1x 100.0% 100.0% boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::native_read_at_awaitable(boost::corosio::native_random_access_file<boost::corosio::select_t{}>&, unsigned long, boost::capy::mutable_buffer) :98 1x 100.0% 100.0% boost::corosio::native_random_access_file<boost::corosio::epoll_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::await_ready() const :108 1x 100.0% 100.0% boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::await_ready() const :108 1x 100.0% 100.0% boost::corosio::native_random_access_file<boost::corosio::epoll_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::await_resume() const :113 1x 75.0% 67.0% boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::await_resume() const :113 1x 75.0% 67.0% boost::corosio::native_random_access_file<boost::corosio::epoll_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :120 1x 100.0% 77.0% boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :120 1x 100.0% 77.0% boost::corosio::native_random_access_file<boost::corosio::epoll_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::native_write_at_awaitable(boost::corosio::native_random_access_file<boost::corosio::epoll_t{}>&, unsigned long, boost::capy::const_buffer) :140 1x 100.0% 100.0% boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::native_write_at_awaitable(boost::corosio::native_random_access_file<boost::corosio::select_t{}>&, unsigned long, boost::capy::const_buffer) :140 1x 100.0% 100.0% boost::corosio::native_random_access_file<boost::corosio::epoll_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::await_ready() const :150 1x 100.0% 100.0% boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::await_ready() const :150 1x 100.0% 100.0% boost::corosio::native_random_access_file<boost::corosio::epoll_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::await_resume() const :155 1x 75.0% 67.0% boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::await_resume() const :155 1x 75.0% 67.0% boost::corosio::native_random_access_file<boost::corosio::epoll_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :162 1x 100.0% 77.0% boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*) :162 1x 100.0% 77.0% boost::corosio::native_random_access_file<boost::corosio::epoll_t{}>::native_random_access_file(boost::capy::execution_context&) :177 5x 100.0% 100.0% boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_random_access_file(boost::capy::execution_context&) :177 5x 100.0% 100.0% auto boost::corosio::native_random_access_file<boost::corosio::epoll_t{}>::read_some_at<boost::capy::mutable_buffer>(unsigned long, boost::capy::mutable_buffer const&) :213 1x 100.0% 100.0% auto boost::corosio::native_random_access_file<boost::corosio::select_t{}>::read_some_at<boost::capy::mutable_buffer>(unsigned long, boost::capy::mutable_buffer const&) :213 1x 100.0% 100.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Steve Gerbino
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #ifndef BOOST_COROSIO_NATIVE_NATIVE_RANDOM_ACCESS_FILE_HPP
11 #define BOOST_COROSIO_NATIVE_NATIVE_RANDOM_ACCESS_FILE_HPP
12
13 #include <boost/corosio/random_access_file.hpp>
14 #include <boost/corosio/backend.hpp>
15
16 #ifndef BOOST_COROSIO_MRDOCS
17 #if BOOST_COROSIO_HAS_EPOLL || BOOST_COROSIO_HAS_SELECT || \
18 BOOST_COROSIO_HAS_KQUEUE
19 #include <boost/corosio/native/detail/posix/posix_random_access_file_service.hpp>
20 #endif
21
22 #if BOOST_COROSIO_HAS_IO_URING
23 #include <boost/corosio/native/detail/io_uring/io_uring_random_access_file.hpp>
24 #endif
25
26 #if BOOST_COROSIO_HAS_IOCP
27 #include <boost/corosio/native/detail/iocp/win_random_access_file_service.hpp>
28 #endif
29 #endif // !BOOST_COROSIO_MRDOCS
30
31 namespace boost::corosio {
32
33 /** A random-access file with devirtualized async I/O operations.
34
35 This class template inherits from @ref random_access_file and
36 shadows `read_some_at` / `write_some_at` with versions that
37 call the backend implementation directly, allowing the compiler
38 to inline through the entire call chain.
39
40 Non-async operations (`open`, `close`, `size`, `resize`,
41 `sync_data`, `sync_all`) remain unchanged and dispatch through
42 the compiled library.
43
44 A `native_random_access_file` IS-A `random_access_file` and
45 can be passed to any function expecting `random_access_file&`,
46 in which case virtual dispatch is used transparently.
47
48 @note On POSIX platforms, file I/O is dispatched to a thread
49 pool regardless of the chosen reactor backend, so all three
50 reactor tags (`epoll`, `select`, `kqueue`) resolve to the same
51 underlying implementation. The `Backend` template parameter
52 exists for API symmetry with @ref native_tcp_socket and friends.
53 The vtable savings are smaller relative to the thread-pool /
54 overlapped-I/O cost than they are for socket operations.
55
56 @tparam Backend A backend tag value (e.g., `epoll`, `iocp`).
57
58 @par Thread Safety
59 Same as @ref random_access_file.
60
61 @par Example
62 @code
63 #include <boost/corosio/native/native_random_access_file.hpp>
64
65 native_io_context<epoll> ctx;
66 native_random_access_file<epoll> f(ctx);
67 f.open("data.bin", file_base::read_only);
68 char buf[4096];
69 auto [ec, n] = co_await f.read_some_at(
70 0, capy::mutable_buffer(buf, sizeof(buf)));
71 @endcode
72
73 @see random_access_file, epoll_t, iocp_t
74 */
75 template<auto Backend>
76 class native_random_access_file : public random_access_file
77 {
78 using backend_type = decltype(Backend);
79 using impl_type = typename backend_type::random_access_file_type;
80 using service_type =
81 typename backend_type::random_access_file_service_type;
82
83 4x impl_type& get_impl() noexcept
84 {
85 4x return *static_cast<impl_type*>(h_.get());
86 }
87
88 template<class MutableBufferSequence>
89 struct native_read_at_awaitable
90 {
91 native_random_access_file& self_;
92 std::uint64_t offset_;
93 MutableBufferSequence buffers_;
94 std::stop_token token_;
95 mutable std::error_code ec_;
96 mutable std::size_t bytes_transferred_ = 0;
97
98 2x native_read_at_awaitable(
99 native_random_access_file& self,
100 std::uint64_t offset,
101 MutableBufferSequence buffers) noexcept
102 2x : self_(self)
103 2x , offset_(offset)
104 2x , buffers_(std::move(buffers))
105 {
106 2x }
107
108 2x bool await_ready() const noexcept
109 {
110 2x return token_.stop_requested();
111 }
112
113 2x capy::io_result<std::size_t> await_resume() const noexcept
114 {
115 2x if (token_.stop_requested())
116 return {make_error_code(std::errc::operation_canceled), 0};
117 2x return {ec_, bytes_transferred_};
118 }
119
120 2x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
121 -> std::coroutine_handle<>
122 {
123 2x token_ = env->stop_token;
124 6x return self_.get_impl().read_some_at(
125 2x offset_, h, env->executor, buffers_,
126 6x token_, &ec_, &bytes_transferred_);
127 }
128 };
129
130 template<class ConstBufferSequence>
131 struct native_write_at_awaitable
132 {
133 native_random_access_file& self_;
134 std::uint64_t offset_;
135 ConstBufferSequence buffers_;
136 std::stop_token token_;
137 mutable std::error_code ec_;
138 mutable std::size_t bytes_transferred_ = 0;
139
140 2x native_write_at_awaitable(
141 native_random_access_file& self,
142 std::uint64_t offset,
143 ConstBufferSequence buffers) noexcept
144 2x : self_(self)
145 2x , offset_(offset)
146 2x , buffers_(std::move(buffers))
147 {
148 2x }
149
150 2x bool await_ready() const noexcept
151 {
152 2x return token_.stop_requested();
153 }
154
155 2x capy::io_result<std::size_t> await_resume() const noexcept
156 {
157 2x if (token_.stop_requested())
158 return {make_error_code(std::errc::operation_canceled), 0};
159 2x return {ec_, bytes_transferred_};
160 }
161
162 2x auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
163 -> std::coroutine_handle<>
164 {
165 2x token_ = env->stop_token;
166 6x return self_.get_impl().write_some_at(
167 2x offset_, h, env->executor, buffers_,
168 6x token_, &ec_, &bytes_transferred_);
169 }
170 };
171
172 public:
173 /** Construct a native random-access file from an execution context.
174
175 @param ctx The execution context that will own this file.
176 */
177 10x explicit native_random_access_file(capy::execution_context& ctx)
178 10x : random_access_file(create_handle<service_type>(ctx))
179 {
180 10x }
181
182 /** Construct a native random-access file from an executor.
183
184 @param ex The executor whose context will own this file.
185 */
186 template<class Ex>
187 requires(!std::same_as<
188 std::remove_cvref_t<Ex>,
189 native_random_access_file>) &&
190 capy::Executor<Ex>
191 explicit native_random_access_file(Ex const& ex)
192 : native_random_access_file(ex.context())
193 {
194 }
195
196 /// Move construct.
197 native_random_access_file(native_random_access_file&&) noexcept = default;
198
199 /// Move assign.
200 native_random_access_file&
201 operator=(native_random_access_file&&) noexcept = default;
202
203 native_random_access_file(native_random_access_file const&) = delete;
204 native_random_access_file&
205 operator=(native_random_access_file const&) = delete;
206
207 /** Asynchronously read at the given offset.
208
209 Calls the backend implementation directly, bypassing virtual
210 dispatch. Otherwise identical to @ref random_access_file::read_some_at.
211 */
212 template<capy::MutableBufferSequence MB>
213 2x auto read_some_at(std::uint64_t offset, MB const& buffers)
214 {
215 2x return native_read_at_awaitable<MB>(*this, offset, buffers);
216 }
217
218 /** Asynchronously write at the given offset.
219
220 Calls the backend implementation directly, bypassing virtual
221 dispatch. Otherwise identical to @ref random_access_file::write_some_at.
222 */
223 template<capy::ConstBufferSequence CB>
224 2x auto write_some_at(std::uint64_t offset, CB const& buffers)
225 {
226 2x return native_write_at_awaitable<CB>(*this, offset, buffers);
227 }
228 };
229
230 } // namespace boost::corosio
231
232 #endif // BOOST_COROSIO_NATIVE_NATIVE_RANDOM_ACCESS_FILE_HPP
233