From 366ccf20745b23f1eb8554cbe17e6972271de002 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 11 Jun 2023 15:40:26 +0200 Subject: libio: remove precache from istream_advance_buffer Since the user has to call istream_get_buffered_data afterwards anyway, we can do the precache lazily. Signed-off-by: David Oberhollenzer --- include/io/istream.h | 13 ++----------- lib/io/src/get_line.c | 3 +-- lib/io/src/istream.c | 12 +++--------- lib/io/src/xfrm/istream.c | 7 ++----- lib/io/test/istream_mem.c | 5 +---- lib/tar/src/iterator.c | 4 +--- 6 files changed, 10 insertions(+), 34 deletions(-) diff --git a/include/io/istream.h b/include/io/istream.h index 8edd61d..fca1440 100644 --- a/include/io/istream.h +++ b/include/io/istream.h @@ -141,24 +141,15 @@ SQFS_INLINE int istream_get_buffered_data(istream_t *strm, const sqfs_u8 **out, * * This marks the first `count` bytes of the internal buffer as used, * forcing @ref istream_get_buffered_data to return data afterwards - * and potentially to load more data. + * and potentially try to load more data. * * @param strm A pointer to an istream_t implementation. * @param count The number of bytes used up. - * - * @return Zero on success, a negative error code on failure. */ -SQFS_INLINE int istream_advance_buffer(istream_t *strm, size_t count) +SQFS_INLINE void istream_advance_buffer(istream_t *strm, size_t count) { - if (count >= strm->buffer_used) { - strm->buffer += strm->buffer_used; - strm->buffer_used = 0; - return strm->precache(strm); - } - strm->buffer += count; strm->buffer_used -= count; - return 0; } /** diff --git a/lib/io/src/get_line.c b/lib/io/src/get_line.c index 94ae9ce..4dc221a 100644 --- a/lib/io/src/get_line.c +++ b/lib/io/src/get_line.c @@ -85,8 +85,7 @@ int istream_get_line(istream_t *strm, char **out, line_len += count; line[line_len] = '\0'; - if (istream_advance_buffer(strm, i)) - goto fail_free; + istream_advance_buffer(strm, i); if (have_line) { if (line_len > 0 && line[line_len - 1] == '\r') diff --git a/lib/io/src/istream.c b/lib/io/src/istream.c index b4c709c..9340dd6 100644 --- a/lib/io/src/istream.c +++ b/lib/io/src/istream.c @@ -29,13 +29,10 @@ sqfs_s32 istream_read(istream_t *strm, void *data, size_t size) diff = size; memcpy(data, ptr, diff); + istream_advance_buffer(strm, diff); data = (char *)data + diff; size -= diff; total += diff; - - ret = istream_advance_buffer(strm, diff); - if (ret) - return -1; } return total; @@ -61,9 +58,7 @@ int istream_skip(istream_t *strm, sqfs_u64 size) diff = size; size -= diff; - - if (istream_advance_buffer(strm, diff)) - return -1; + istream_advance_buffer(strm, diff); } return 0; @@ -92,11 +87,10 @@ sqfs_s32 istream_splice(istream_t *in, ostream_t *out, sqfs_u32 size) if (ostream_append(out, ptr, diff)) return -1; - if (istream_advance_buffer(in, diff)) - return -1; total += diff; size -= diff; + istream_advance_buffer(in, diff); } return total; diff --git a/lib/io/src/xfrm/istream.c b/lib/io/src/xfrm/istream.c index 8755cb6..bad3e22 100644 --- a/lib/io/src/xfrm/istream.c +++ b/lib/io/src/xfrm/istream.c @@ -18,7 +18,7 @@ typedef struct istream_xfrm_t { static int xfrm_precache(istream_t *base) { istream_xfrm_t *xfrm = (istream_xfrm_t *)base; - int ret, sret; + int ret; assert(base->buffer >= xfrm->uncompressed); assert(base->buffer <= (xfrm->uncompressed + BUFSZ)); @@ -59,10 +59,7 @@ static int xfrm_precache(istream_t *base) } base->buffer_used = out_off; - - sret = istream_advance_buffer(xfrm->wrapped, in_off); - if (sret != 0) - return sret; + istream_advance_buffer(xfrm->wrapped, in_off); if (ret == XFRM_STREAM_BUFFER_FULL || out_off >= BUFSZ) break; diff --git a/lib/io/test/istream_mem.c b/lib/io/test/istream_mem.c index f1849dd..84da9a9 100644 --- a/lib/io/test/istream_mem.c +++ b/lib/io/test/istream_mem.c @@ -63,11 +63,8 @@ int main(int argc, char **argv) TEST_EQUAL_UI(ptr[j], byte_at_offset(i + j)); } - diff = eat_all ? size : (size / 2); + istream_advance_buffer(in, eat_all ? size : (size / 2)); eat_all = !eat_all; - - ret = istream_advance_buffer(in, diff); - TEST_EQUAL_I(ret, 0); } sqfs_drop(in); diff --git a/lib/tar/src/iterator.c b/lib/tar/src/iterator.c index f46a9c2..df5c446 100644 --- a/lib/tar/src/iterator.c +++ b/lib/tar/src/iterator.c @@ -109,9 +109,7 @@ static int strm_precache(istream_t *strm) return tar->state; if (!tar->parent->last_sparse) { - int ret = istream_advance_buffer(tar->parent->stream, diff); - if (ret != 0) - return ret; + istream_advance_buffer(tar->parent->stream, diff); tar->parent->record_size -= diff; } -- cgit v1.2.3