Skip to content

Commit

Permalink
io/FileDescriptor: pass std::span to Full{Read,Write}()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 27, 2023
1 parent 9a0b377 commit 09a2da8
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 23 deletions.
22 changes: 8 additions & 14 deletions src/io/FileDescriptor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -249,38 +249,32 @@ FileDescriptor::GetSize() const noexcept
}

void
FileDescriptor::FullRead(void *_buffer, std::size_t length) const
FileDescriptor::FullRead(std::span<std::byte> 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<const std::byte> 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);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/io/FileDescriptor.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include <cstddef>
#include <span>
#include <utility>

#include <unistd.h>
Expand Down Expand Up @@ -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<std::byte> dest) const;

ssize_t Write(const void *buffer, std::size_t length) const noexcept {
return ::write(fd, buffer, length);
Expand All @@ -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<const std::byte> src) const;

#ifndef _WIN32
int Poll(short events, int timeout) const noexcept;
Expand Down
2 changes: 1 addition & 1 deletion test/ReadFrames.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions test/run_convert.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ 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) {
auto output = convert.Flush();
if (output.data() == nullptr)
break;

out_fd.FullWrite(output.data(), output.size());
out_fd.FullWrite(output);
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/run_filter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions test/run_input.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 09a2da8

Please sign in to comment.