From 36ca9d01fbd9cd996472f4ebcbceb25b955d707c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 20 Aug 2018 15:21:05 +0200 Subject: [PATCH] util/ForeignFifoBuffer: shift the buffer in MoveFrom() on demand The API documentation says "move as much data as possible", and if there is room at the head of the buffer, we should use that if the room after the tail is not large enough. --- src/util/ForeignFifoBuffer.hxx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/util/ForeignFifoBuffer.hxx b/src/util/ForeignFifoBuffer.hxx index da0058ad3..bcc2b9d67 100644 --- a/src/util/ForeignFifoBuffer.hxx +++ b/src/util/ForeignFifoBuffer.hxx @@ -223,6 +223,15 @@ public: size_type MoveFrom(ForeignFifoBuffer &src) noexcept { auto r = src.Read(); auto w = Write(); + + if (w.size < r.size && head > 0) { + /* if the source contains more data than we + can append at the tail, try to make more + room by shifting the head to 0 */ + Shift(); + w = Write(); + } + size_t n = std::min(r.size, w.size); std::move(r.data, r.data + n, w.data);