From 1370963723917eed6c93e28c9970a2b27be57ea4 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 4 Jun 2023 20:26:14 +0200 Subject: libio: remove buffer_offset from istream_t Instead, make the buffer const, let the user adjust the pointer and size. The offset can then be inferred in precache. Signed-off-by: David Oberhollenzer --- lib/io/src/mem.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'lib/io/src/mem.c') diff --git a/lib/io/src/mem.c b/lib/io/src/mem.c index d305a83..b1abbdb 100644 --- a/lib/io/src/mem.c +++ b/lib/io/src/mem.c @@ -10,6 +10,7 @@ #include #include +#include typedef struct { istream_t base; @@ -28,17 +29,16 @@ static int mem_in_precache(istream_t *strm) mem_istream_t *mem = (mem_istream_t *)strm; size_t diff; - if (strm->buffer_offset >= strm->buffer_used) { - strm->buffer_offset = 0; - strm->buffer_used = 0; - } else if (strm->buffer_offset > 0) { - memmove(strm->buffer, - strm->buffer + strm->buffer_offset, - strm->buffer_used - strm->buffer_offset); + assert(strm->buffer >= mem->buffer); + assert(strm->buffer <= (mem->buffer + mem->bufsz)); + assert(strm->buffer_used <= mem->bufsz); + assert((size_t)(strm->buffer - mem->buffer) <= + (mem->bufsz - strm->buffer_used)); - strm->buffer_used -= strm->buffer_offset; - strm->buffer_offset = 0; - } + if (strm->buffer_used > 0) + memmove(mem->buffer, strm->buffer, strm->buffer_used); + + strm->buffer = mem->buffer; diff = mem->bufsz - strm->buffer_used; if (diff > mem->size) -- cgit v1.2.3