From 09a2da89a2319191f4dd3716d7d0d4e79d4dcbe1 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 26 Sep 2023 20:40:05 +0200 Subject: [PATCH] io/FileDescriptor: pass std::span to Full{Read,Write}() --- src/io/FileDescriptor.cxx | 22 ++++++++-------------- src/io/FileDescriptor.hxx | 5 +++-- test/ReadFrames.cxx | 2 +- test/run_convert.cxx | 4 ++-- test/run_filter.cxx | 4 ++-- test/run_input.cxx | 4 ++-- 6 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/io/FileDescriptor.cxx b/src/io/FileDescriptor.cxx index e1e437e90..45c740ee2 100644 --- a/src/io/FileDescriptor.cxx +++ b/src/io/FileDescriptor.cxx @@ -249,38 +249,32 @@ FileDescriptor::GetSize() const noexcept } void -FileDescriptor::FullRead(void *_buffer, std::size_t length) const +FileDescriptor::FullRead(std::span dest) const { - auto buffer = (std::byte *)_buffer; - - while (length > 0) { - ssize_t nbytes = Read(buffer, length); + while (!dest.empty()) { + ssize_t nbytes = Read(dest.data(), dest.size()); if (nbytes <= 0) { if (nbytes < 0) throw MakeErrno("Failed to read"); throw std::runtime_error("Unexpected end of file"); } - buffer += nbytes; - length -= nbytes; + dest = dest.subspan(nbytes); } } void -FileDescriptor::FullWrite(const void *_buffer, std::size_t length) const +FileDescriptor::FullWrite(std::span src) const { - auto buffer = (const std::byte *)_buffer; - - while (length > 0) { - ssize_t nbytes = Write(buffer, length); + while (!src.empty()) { + ssize_t nbytes = Write(src.data(), src.size()); if (nbytes <= 0) { if (nbytes < 0) throw MakeErrno("Failed to write"); throw std::runtime_error("Failed to write"); } - buffer += nbytes; - length -= nbytes; + src = src.subspan(nbytes); } } diff --git a/src/io/FileDescriptor.hxx b/src/io/FileDescriptor.hxx index 628a0f3fc..283566147 100644 --- a/src/io/FileDescriptor.hxx +++ b/src/io/FileDescriptor.hxx @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include @@ -224,7 +225,7 @@ public: * Read until all of the given buffer has been filled. Throws * on error. */ - void FullRead(void *buffer, std::size_t length) const; + void FullRead(std::span dest) const; ssize_t Write(const void *buffer, std::size_t length) const noexcept { return ::write(fd, buffer, length); @@ -234,7 +235,7 @@ public: * Write until all of the given buffer has been written. * Throws on error. */ - void FullWrite(const void *buffer, std::size_t length) const; + void FullWrite(std::span src) const; #ifndef _WIN32 int Poll(short events, int timeout) const noexcept; diff --git a/test/ReadFrames.cxx b/test/ReadFrames.cxx index dd6932ae0..cdf58baa3 100644 --- a/test/ReadFrames.cxx +++ b/test/ReadFrames.cxx @@ -28,7 +28,7 @@ ReadFrames(FileDescriptor fd, void *_buffer, std::size_t size, const size_t modulo = nbytes % frame_size; if (modulo > 0) { size_t rest = frame_size - modulo; - fd.FullRead(buffer + nbytes, rest); + fd.FullRead({(std::byte *)buffer + nbytes, rest}); nbytes += rest; } diff --git a/test/run_convert.cxx b/test/run_convert.cxx index 827084c28..75c9e58ab 100644 --- a/test/run_convert.cxx +++ b/test/run_convert.cxx @@ -116,7 +116,7 @@ RunConvert(PcmConvert &convert, size_t in_frame_size, buffer.Consume(src.size()); auto output = convert.Convert(src); - out_fd.FullWrite(output.data(), output.size()); + out_fd.FullWrite(output); } while (true) { @@ -124,7 +124,7 @@ RunConvert(PcmConvert &convert, size_t in_frame_size, if (output.data() == nullptr) break; - out_fd.FullWrite(output.data(), output.size()); + out_fd.FullWrite(output); } } diff --git a/test/run_filter.cxx b/test/run_filter.cxx index a9404c2cc..7b58aa3ba 100644 --- a/test/run_filter.cxx +++ b/test/run_filter.cxx @@ -87,14 +87,14 @@ try { break; auto dest = filter->FilterPCM(std::span{buffer}.first(nbytes)); - output_fd.FullWrite(dest.data(), dest.size()); + output_fd.FullWrite(dest); } while (true) { auto dest = filter->Flush(); if (dest.data() == nullptr) break; - output_fd.FullWrite(dest.data(), dest.size()); + output_fd.FullWrite(dest); } /* cleanup and exit */ diff --git a/test/run_input.cxx b/test/run_input.cxx index f86a1129b..4e9729869 100644 --- a/test/run_input.cxx +++ b/test/run_input.cxx @@ -170,13 +170,13 @@ dump_input_stream(InputStream &is, FileDescriptor out, } } - char buffer[MAX_CHUNK_SIZE]; + std::byte buffer[MAX_CHUNK_SIZE]; assert(chunk_size <= sizeof(buffer)); size_t num_read = is.Read(lock, buffer, chunk_size); if (num_read == 0) break; - out.FullWrite(buffer, num_read); + out.FullWrite({buffer, num_read}); } is.Check();