input/async: pass std::span to AppendToBuffer()

This commit is contained in:
Max Kellermann 2022-11-17 06:10:55 +01:00
parent ce13d82657
commit c71958af4b
4 changed files with 18 additions and 12 deletions

View File

@ -217,23 +217,28 @@ AsyncInputStream::CommitWriteBuffer(size_t nbytes) noexcept
} }
void void
AsyncInputStream::AppendToBuffer(const void *data, size_t append_size) noexcept AsyncInputStream::AppendToBuffer(std::span<const std::byte> src) noexcept
{ {
auto w = buffer.Write(); auto w = buffer.Write();
assert(!w.empty()); assert(!w.empty());
size_t nbytes = std::min(w.size(), append_size); std::span<const std::byte> second{};
memcpy(w.data(), data, nbytes);
buffer.Append(nbytes);
const size_t remaining = append_size - nbytes; if (w.size() < src.size()) {
if (remaining > 0) { second = src.subspan(w.size());
src = src.first(w.size());
}
std::copy(src.begin(), src.end(), w.begin());
buffer.Append(src.size());
if (!second.empty()) {
w = buffer.Write(); w = buffer.Write();
assert(!w.empty()); assert(!w.empty());
assert(w.size() >= remaining); assert(w.size() >= second.size());
memcpy(w.data(), (const std::byte *)data + nbytes, remaining); std::copy(second.begin(), second.end(), w.begin());
buffer.Append(remaining); buffer.Append(second.size());
} }
if (!IsReady()) if (!IsReady())

View File

@ -139,7 +139,7 @@ protected:
* Append data to the buffer. The size must fit into the * Append data to the buffer. The size must fit into the
* buffer; see GetBufferSpace(). * buffer; see GetBufferSpace().
*/ */
void AppendToBuffer(const void *data, size_t append_size) noexcept; void AppendToBuffer(std::span<const std::byte> src) noexcept;
/** /**
* Implement code here that will resume the stream after it * Implement code here that will resume the stream after it

View File

@ -353,7 +353,7 @@ CurlInputStream::OnData(std::span<const std::byte> data)
throw CurlResponseHandler::Pause{}; throw CurlResponseHandler::Pause{};
} }
AppendToBuffer(data.data(), data.size()); AppendToBuffer(data);
} }
void void

View File

@ -164,7 +164,8 @@ NfsInputStream::OnNfsFileRead(std::span<const std::byte> src) noexcept
const std::scoped_lock<Mutex> protect(mutex); const std::scoped_lock<Mutex> protect(mutex);
assert(!IsBufferFull()); assert(!IsBufferFull());
assert(IsBufferFull() == (GetBufferSpace() == 0)); assert(IsBufferFull() == (GetBufferSpace() == 0));
AppendToBuffer(src.data(), src.size());
AppendToBuffer(src);
next_offset += src.size(); next_offset += src.size();