include/boost/corosio/native/detail/iocp/win_random_access_file.hpp

100.0% Lines (4 / 4) -% Branches (0 / 0) 100.0% Functions (1 / 1)
win_random_access_file.hpp
f(x) Functions (1)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Michael Vandeberg
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_DETAIL_IOCP_WIN_RANDOM_ACCESS_FILE_HPP
11 #define BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_RANDOM_ACCESS_FILE_HPP
12
13 #include <boost/corosio/detail/platform.hpp>
14
15 #if BOOST_COROSIO_HAS_IOCP
16
17 #include <boost/corosio/detail/config.hpp>
18 #include <boost/corosio/random_access_file.hpp>
19 #include <boost/corosio/file_base.hpp>
20 #include <boost/capy/ex/executor_ref.hpp>
21 #include <boost/corosio/detail/intrusive.hpp>
22 #include <boost/corosio/native/detail/iocp/win_overlapped_handle.hpp>
23 #include <boost/corosio/native/detail/iocp/win_windows.hpp>
24
25 #include <coroutine>
26 #include <cstdint>
27
28 namespace boost::corosio::detail {
29
30 class win_random_access_file_service;
31
32 /** Random-access file state: the concurrent model. */
33 class win_random_access_file_internal : public win_concurrent_handle
34 {
35 friend class win_random_access_file_service;
36
37 public:
38 117x win_random_access_file_internal(
39 win_scheduler& sched,
40 io_object::implementation& io,
41 win_random_access_file_service& svc) noexcept
42 117x : win_concurrent_handle(sched, io)
43 117x , svc_(svc)
44 {
45 117x }
46
47 capy::results<std::uint64_t> size() const noexcept;
48 std::error_code resize(std::uint64_t new_size) noexcept;
49 std::error_code sync_data() noexcept;
50 std::error_code sync_all() noexcept;
51
52 private:
53 win_random_access_file_service& svc_;
54 };
55
56 /** Random-access file implementation for IOCP-based I/O.
57
58 Embeds its handle state and delegates every virtual call to it.
59 `win_random_access_file_service` recycles instances through its
60 pool instead of freeing them on every close.
61 */
62 class win_random_access_file final
63 : public random_access_file::implementation
64 , public intrusive_list<win_random_access_file>::node
65 {
66 win_random_access_file_service& svc_;
67 win_random_access_file_internal internal_;
68
69 public:
70 explicit win_random_access_file(
71 win_random_access_file_service& svc) noexcept;
72
73 /// Recycle into the owning service's pool. Defined out-of-line
74 /// after `win_random_access_file_service` is complete.
75 void retire() noexcept override;
76
77 /** Assert the closed state a recycled file starts from.
78
79 @pre refs_ == 0, handle closed, no op in flight.
80 */
81 void reuse() noexcept;
82
83 std::coroutine_handle<> read_some_at(
84 std::uint64_t offset,
85 capy::continuation& cont,
86 capy::executor_ref d,
87 buffer_param buf,
88 std::stop_token token,
89 std::error_code* ec,
90 std::size_t* bytes) override;
91
92 std::coroutine_handle<> write_some_at(
93 std::uint64_t offset,
94 capy::continuation& cont,
95 capy::executor_ref d,
96 buffer_param buf,
97 std::stop_token token,
98 std::error_code* ec,
99 std::size_t* bytes) override;
100
101 native_handle_type native_handle() const noexcept override;
102 void cancel() noexcept override;
103 capy::results<std::uint64_t> size() const noexcept override;
104 std::error_code resize(std::uint64_t new_size) noexcept override;
105 std::error_code sync_data() noexcept override;
106 std::error_code sync_all() noexcept override;
107 capy::results<native_handle_type> release() noexcept override;
108 std::error_code assign(native_handle_type handle) noexcept override;
109
110 win_random_access_file_internal* get_internal() noexcept;
111 };
112
113 } // namespace boost::corosio::detail
114
115 #endif // BOOST_COROSIO_HAS_IOCP
116
117 #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_RANDOM_ACCESS_FILE_HPP
118