From cad35a83fb2a4ad2952888d258bd6ad32cff7c02 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 27 Sep 2023 10:46:43 +0200 Subject: [PATCH] io/FileDescriptor: add Read()/Write() overloads with std::span --- src/event/InotifyEvent.cxx | 3 +-- src/io/FileDescriptor.hxx | 8 ++++++++ src/output/plugins/OssOutputPlugin.cxx | 2 +- src/output/plugins/SolarisOutputPlugin.cxx | 2 +- src/system/EventFD.cxx | 2 +- test/run_convert.cxx | 2 +- test/run_output.cxx | 2 +- 7 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/event/InotifyEvent.cxx b/src/event/InotifyEvent.cxx index e5ce9abcd..643190159 100644 --- a/src/event/InotifyEvent.cxx +++ b/src/event/InotifyEvent.cxx @@ -64,8 +64,7 @@ try { static_assert(sizeof(buffer) >= sizeof(struct inotify_event) + NAME_MAX + 1, "inotify buffer too small"); - ssize_t nbytes = event.GetFileDescriptor().Read(buffer.data(), - buffer.size()); + ssize_t nbytes = event.GetFileDescriptor().Read(buffer); if (nbytes <= 0) [[unlikely]] { if (nbytes == 0) throw std::runtime_error{"EOF from inotify"}; diff --git a/src/io/FileDescriptor.hxx b/src/io/FileDescriptor.hxx index 283566147..6da440a35 100644 --- a/src/io/FileDescriptor.hxx +++ b/src/io/FileDescriptor.hxx @@ -217,6 +217,10 @@ public: } #endif + ssize_t Read(std::span dest) const noexcept { + return ::read(fd, dest.data(), dest.size()); + } + ssize_t Read(void *buffer, std::size_t length) const noexcept { return ::read(fd, buffer, length); } @@ -227,6 +231,10 @@ public: */ void FullRead(std::span dest) const; + ssize_t Write(std::span src) const noexcept { + return ::write(fd, src.data(), src.size()); + } + ssize_t Write(const void *buffer, std::size_t length) const noexcept { return ::write(fd, buffer, length); } diff --git a/src/output/plugins/OssOutputPlugin.cxx b/src/output/plugins/OssOutputPlugin.cxx index 40cb9a054..7b6a8f981 100644 --- a/src/output/plugins/OssOutputPlugin.cxx +++ b/src/output/plugins/OssOutputPlugin.cxx @@ -671,7 +671,7 @@ OssOutput::Play(std::span src) return src.size(); while (true) { - const ssize_t ret = fd.Write(e.data(), e.size()); + const ssize_t ret = fd.Write(e); if (ret > 0) return pcm_export->CalcInputSize(ret); diff --git a/src/output/plugins/SolarisOutputPlugin.cxx b/src/output/plugins/SolarisOutputPlugin.cxx index d77533076..5572b18d9 100644 --- a/src/output/plugins/SolarisOutputPlugin.cxx +++ b/src/output/plugins/SolarisOutputPlugin.cxx @@ -125,7 +125,7 @@ SolarisOutput::Close() noexcept std::size_t SolarisOutput::Play(std::span src) { - ssize_t nbytes = fd.Write(src.data(), src.size()); + ssize_t nbytes = fd.Write(src); if (nbytes <= 0) throw MakeErrno("Write failed"); diff --git a/src/system/EventFD.cxx b/src/system/EventFD.cxx index d0916d4ab..b3a740165 100644 --- a/src/system/EventFD.cxx +++ b/src/system/EventFD.cxx @@ -21,7 +21,7 @@ EventFD::Read() noexcept assert(fd.IsDefined()); eventfd_t value; - return fd.Read(&value, sizeof(value)) == (ssize_t)sizeof(value); + return fd.Read(std::as_writable_bytes(std::span{&value, 1})) == (ssize_t)sizeof(value); } void diff --git a/test/run_convert.cxx b/test/run_convert.cxx index 75c9e58ab..fd6355c2b 100644 --- a/test/run_convert.cxx +++ b/test/run_convert.cxx @@ -99,7 +99,7 @@ RunConvert(PcmConvert &convert, size_t in_frame_size, const auto dest = buffer.Write(); assert(!dest.empty()); - ssize_t nbytes = in_fd.Read(dest.data(), dest.size()); + ssize_t nbytes = in_fd.Read(dest); if (nbytes <= 0) break; diff --git a/test/run_output.cxx b/test/run_output.cxx index dad7c34e3..4a918f811 100644 --- a/test/run_output.cxx +++ b/test/run_output.cxx @@ -124,7 +124,7 @@ RunOutput(AudioOutput &ao, AudioFormat audio_format, const auto dest = buffer.Write(); assert(!dest.empty()); - ssize_t nbytes = in_fd.Read(dest.data(), dest.size()); + ssize_t nbytes = in_fd.Read(dest); if (nbytes <= 0) break;