From f850d82e656a9add1bd286f901f18c5213580606 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 7 Nov 2019 14:53:19 +0100 Subject: stream: avoid a duplicate condition stream_read_peek() duplicated what stream_read_more() checks for anyway (whether the forward buffer is large enough). This can be skipped by making the stream_read_more() return value more consistent. --- stream/stream.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/stream/stream.c b/stream/stream.c index fdaecf9ec8..504d217b46 100644 --- a/stream/stream.c +++ b/stream/stream.c @@ -470,14 +470,14 @@ static int stream_read_unbuffered(stream_t *s, void *buf, int len) // Ask for having at most "forward" bytes ready to read in the buffer. // To read everything, you may have to call this in a loop. // forward: desired amount of bytes in buffer after s->cur_pos -// returns: progress (false on EOF or memory allocation failure) +// returns: progress (false on EOF or on OOM or if enough data was available) static bool stream_read_more(struct stream *s, int forward) { assert(forward >= 0); int forward_avail = s->buf_end - s->buf_cur; if (forward_avail >= forward) - return true; + return false; // Avoid that many small reads will lead to many low-level read calls. forward = MPMAX(forward, s->requested_buffer_size / 2); @@ -580,10 +580,7 @@ int stream_read(stream_t *s, char *mem, int total) // the buffer to satisfy the read request. int stream_read_peek(stream_t *s, void* buf, int buf_size) { - while (s->buf_end - s->buf_cur < buf_size) { - if (!stream_read_more(s, buf_size)) - break; - } + while (stream_read_more(s, buf_size)) {} return ring_copy(s, buf, buf_size, s->buf_cur); } -- cgit v1.2.3